Carlo Alberto Ferraris
2011-Jul-31 14:36 UTC
[LLVMdev] SwitchInst::addCase with BlockAddress
I'm trying to figure out how to feed a blockaddress to a switch condition AND destination (basically emulating an indirectbr via a switch; I know it's not a good approach, I'm just experimenting). Suppose I have the following: SwitchInst *s = SwitchInst::Create(...); BasicBlock *bb = ...; PtrToIntInst k = new PtrToIntInst(BlockAddress::get(bb), <TYPE>, "", s); s->addCase(k, bb); ... is this reasonable? what should I put in place of <TYPE> (the type to cast to)? -- Carlo Alberto Ferraris <cafxx at strayorange.com <mailto:cafxx at strayorange.com>> website/blog <http://cafxx.strayorange.com> - +39 333 7643 235 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20110731/fa1e0254/attachment.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: cafxx.vcf Type: text/x-vcard Size: 233 bytes Desc: not available URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20110731/fa1e0254/attachment.vcf>
Carlo Alberto Ferraris
2011-Aug-01 06:03 UTC
[LLVMdev] SwitchInst::addCase with BlockAddress
Nella citazione domenica 31 luglio 2011 16:36:14, Carlo Alberto Ferraris ha scritto:> I'm trying to figure out how to feed a blockaddress to a switch > condition AND destination (basically emulating an indirectbr via a > switch; I know it's not a good approach, I'm just experimenting). >Basically the question boils down to: how do I get a ConstantInt out of a BlockAddress? I was able to get a ConstantExpr via ConstantExpr::getPointerCast but now I don't know how to get a ConstantInt out of a ConstantExpr... -- Carlo Alberto Ferraris <cafxx at strayorange.com <mailto:cafxx at strayorange.com>> website/blog <http://cafxx.strayorange.com> - +39 333 7643 235 -------------- next part -------------- A non-text attachment was scrubbed... Name: cafxx.vcf Type: text/x-vcard Size: 233 bytes Desc: not available URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20110801/bc34e5d5/attachment.vcf>
On Sun, Jul 31, 2011 at 7:36 AM, Carlo Alberto Ferraris <cafxx at strayorange.com> wrote:> I'm trying to figure out how to feed a blockaddress to a switch condition > AND destination (basically emulating an indirectbr via a switch; I know it's > not a good approach, I'm just experimenting). > Suppose I have the following: > > SwitchInst *s = SwitchInst::Create(...); > BasicBlock *bb = ...; > PtrToIntInst k = new PtrToIntInst(BlockAddress::get(bb), <TYPE>, "", s); > s->addCase(k, bb); > ... > > is this reasonable? what should I put in place of <TYPE> (the type to cast > to)?You can't do that; switch cases are required to be constant integers, and a blockaddress are not constant integers (at least not at compile-time). You can use an explicit comparison+branch, though. -Eli
Carlo Alberto Ferraris
2011-Aug-01 10:03 UTC
[LLVMdev] SwitchInst::addCase with BlockAddress
Nella citazione lunedì 1 agosto 2011 08:13:12, Eli Friedman ha scritto:> On Sun, Jul 31, 2011 at 7:36 AM, Carlo Alberto Ferraris > <cafxx at strayorange.com> wrote: >> I'm trying to figure out how to feed a blockaddress to a switch condition >> AND destination (basically emulating an indirectbr via a switch; I know it's >> not a good approach, I'm just experimenting). >> Suppose I have the following: >> >> SwitchInst *s = SwitchInst::Create(...); >> BasicBlock *bb = ...; >> PtrToIntInst k = new PtrToIntInst(BlockAddress::get(bb),<TYPE>, "", s); >> s->addCase(k, bb); >> ... >> >> is this reasonable? what should I put in place of<TYPE> (the type to cast >> to)? > > You can't do that; switch cases are required to be constant integers, > and a blockaddress are not constant integers (at least not at > compile-time). > > You can use an explicit comparison+branch, though.BlockAddress is a class derived from Constant, so I thought it was meant to be, well, constant. Besides, as I wrote, it is possible to get a ConstantExpr from a BlockAddress via ConstantExpr::getPointerCast (that in turn calls ConstantExpr::getPtrToInt). Unfortunately, getPointerCast returns a Constant, not a ConstantInt; moreover, dyn_cast<ConstantInt> on that Constant returns null. What I'm missing is, therefore, how do I get a ConstantInt out of a Constant obtained via ConstantExpr::getPointerCast? -- Carlo Alberto Ferraris <cafxx at strayorange.com <mailto:cafxx at strayorange.com>> website/blog <http://cafxx.strayorange.com> - +39 333 7643 235 -------------- next part -------------- A non-text attachment was scrubbed... Name: cafxx.vcf Type: text/x-vcard Size: 233 bytes Desc: not available URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20110801/c654fa18/attachment.vcf>