Hi Mark, This will workaround the problem of "default" branch restriction on the switch instruction. The trouble with this technique is that it will trump later optimization phases such as constant propagation. When a block was part of a case, because of the knowledge of the case value, the block was a candidate for better optimization. However, when we move the body of the case into the default, the knowledge of the case value is lost and the body is less optimizable. -Milind On Wed, Jul 17, 2013 at 9:29 PM, Mark Lacey <mark.lacey at apple.com> wrote:> On Jul 17, 2013, at 9:01 PM, Milind Chabbi <Milind.Chabbi at rice.edu> wrote: >> I am performing a transformation that requires changing the targets of >> a basic block ending with a switch instruction. >> In particular, I need to delete the edge that goes to the "default" >> basic block. >> But, LLVM switch instruction always wants a default target basic block >> for a switch instruction. >> It is not clear how to accomplish this, since I don't have a >> replacement default target block. >> I could potentially fake that edge to be one of the other case label >> targets, but that is an ugly hack and I don't want to do that. >> I would appreciate if you can suggest better alternatives. > > Hi Milind, > > If you make the "default" branch to a block that has an UnreachableInst as a terminator, the SimplifyCFG pass will remove one of the switch cases and replace the block that the default branches to with the block that this removed case branches to. This sounds a lot like the "ugly hack" that you would like to avoid. Would it be a reasonable solution for what you are trying to accomplish? > > Mark > >
On Jul 17, 2013, at 10:09 PM, Milind Chabbi <Milind.Chabbi at rice.edu> wrote:> Hi Mark, > > This will workaround the problem of "default" branch restriction on > the switch instruction. The trouble with this technique is that it > will trump later optimization phases such as constant propagation. > When a block was part of a case, because of the knowledge of the case > value, the block was a candidate for better optimization. However, > when we move the body of the case into the default, the knowledge of > the case value is lost and the body is less optimizable.Yes, it is not ideal for a variety of reasons, and I am actually looking at improving how we deal with unreachable switch defaults now because of that. Could you provide any additional detail about the transforms you are doing and what you are trying to accomplish? Mark
Hi Milind, Maybe you could annotate the default case value as metadata to the swith instruction. Thanks Hongbin On Thu, Jul 18, 2013 at 1:09 PM, Milind Chabbi <Milind.Chabbi at rice.edu>wrote:> Hi Mark, > > This will workaround the problem of "default" branch restriction on > the switch instruction. The trouble with this technique is that it > will trump later optimization phases such as constant propagation. > When a block was part of a case, because of the knowledge of the case > value, the block was a candidate for better optimization. However, > when we move the body of the case into the default, the knowledge of > the case value is lost and the body is less optimizable. > > -Milind > > > On Wed, Jul 17, 2013 at 9:29 PM, Mark Lacey <mark.lacey at apple.com> wrote: > > On Jul 17, 2013, at 9:01 PM, Milind Chabbi <Milind.Chabbi at rice.edu> > wrote: > >> I am performing a transformation that requires changing the targets of > >> a basic block ending with a switch instruction. > >> In particular, I need to delete the edge that goes to the "default" > >> basic block. > >> But, LLVM switch instruction always wants a default target basic block > >> for a switch instruction. > >> It is not clear how to accomplish this, since I don't have a > >> replacement default target block. > >> I could potentially fake that edge to be one of the other case label > >> targets, but that is an ugly hack and I don't want to do that. > >> I would appreciate if you can suggest better alternatives. > > > > Hi Milind, > > > > If you make the "default" branch to a block that has an UnreachableInst > as a terminator, the SimplifyCFG pass will remove one of the switch cases > and replace the block that the default branches to with the block that this > removed case branches to. This sounds a lot like the "ugly hack" that you > would like to avoid. Would it be a reasonable solution for what you are > trying to accomplish? > > > > Mark > > > > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130718/1db419b8/attachment.html>
Mark, I am basically trying to do a specialized form of unreachable block elimination. I bumped into this issue when I was thinking of eliminating unreachable cases (including default). In the following code, assertion propagation should easily infer that the default is unreachable. But, llvm at -O3 leaves the code in default intact. Your technique of placing "unreachable" instruction in the default case makes a difference though. -------------------------------------------------- int foo(unsigned int i , unsigned int j){ if(i >= 0 && i < 3) { switch(i){ case 0: i = i + j; case 1: i = i + j; case 2: i = i + j; break; default : i = i * j; } } return i; } -------------------------------------------------- On Wed, Jul 17, 2013 at 10:28 PM, Mark Lacey <mark.lacey at apple.com> wrote:> > On Jul 17, 2013, at 10:09 PM, Milind Chabbi <Milind.Chabbi at rice.edu> wrote: >> Hi Mark, >> >> This will workaround the problem of "default" branch restriction on >> the switch instruction. The trouble with this technique is that it >> will trump later optimization phases such as constant propagation. >> When a block was part of a case, because of the knowledge of the case >> value, the block was a candidate for better optimization. However, >> when we move the body of the case into the default, the knowledge of >> the case value is lost and the body is less optimizable. > > Yes, it is not ideal for a variety of reasons, and I am actually looking at improving how we deal with unreachable switch defaults now because of that. > > Could you provide any additional detail about the transforms you are doing and what you are trying to accomplish? > > Mark > >
Hongbin Can you elaborate more on your suggestion? I am not sure I fully understand what you suggested. -Milind On Wed, Jul 17, 2013 at 11:11 PM, Hongbin Zheng <etherzhhb at gmail.com> wrote:> Hi Milind, > > Maybe you could annotate the default case value as metadata to the swith > instruction. > > Thanks > Hongbin > > > On Thu, Jul 18, 2013 at 1:09 PM, Milind Chabbi <Milind.Chabbi at rice.edu> > wrote: >> >> Hi Mark, >> >> This will workaround the problem of "default" branch restriction on >> the switch instruction. The trouble with this technique is that it >> will trump later optimization phases such as constant propagation. >> When a block was part of a case, because of the knowledge of the case >> value, the block was a candidate for better optimization. However, >> when we move the body of the case into the default, the knowledge of >> the case value is lost and the body is less optimizable. >> >> -Milind >> >> >> On Wed, Jul 17, 2013 at 9:29 PM, Mark Lacey <mark.lacey at apple.com> wrote: >> > On Jul 17, 2013, at 9:01 PM, Milind Chabbi <Milind.Chabbi at rice.edu> >> > wrote: >> >> I am performing a transformation that requires changing the targets of >> >> a basic block ending with a switch instruction. >> >> In particular, I need to delete the edge that goes to the "default" >> >> basic block. >> >> But, LLVM switch instruction always wants a default target basic block >> >> for a switch instruction. >> >> It is not clear how to accomplish this, since I don't have a >> >> replacement default target block. >> >> I could potentially fake that edge to be one of the other case label >> >> targets, but that is an ugly hack and I don't want to do that. >> >> I would appreciate if you can suggest better alternatives. >> > >> > Hi Milind, >> > >> > If you make the "default" branch to a block that has an UnreachableInst >> > as a terminator, the SimplifyCFG pass will remove one of the switch cases >> > and replace the block that the default branches to with the block that this >> > removed case branches to. This sounds a lot like the "ugly hack" that you >> > would like to avoid. Would it be a reasonable solution for what you are >> > trying to accomplish? >> > >> > Mark >> > >> > >> >> _______________________________________________ >> LLVM Developers mailing list >> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu >> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > >