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), , “”, s);
s->addCase(k, bb);

is this reasonable? what should I put in place of (the type to cast to)?

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...

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

Nella citazione lunedì 1 agosto 2011 08:13:12, Eli Friedman 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).
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?

Hi,

What I'm missing is, therefore, how do I get a ConstantInt out of a Constant
obtained via ConstantExpr::getPointerCast?

you can't. A ConstantInt is not the same thing as a Constant with integer type.
A ConstantInt is a constant which is just a number, such as 3.

Ciao, Duncan.