Shoaib Meenai via llvm-dev
2018-Aug-20 05:15 UTC
[llvm-dev] A question about the widely used "xxx | true" inside llvm
This is better suited to llvm-dev, so I'm directing it there. From: llvm-commits <llvm-commits-bounces at lists.llvm.org> on behalf of Qing Shan Zhang via llvm-commits <llvm-commits at lists.llvm.org> Reply-To: Qing Shan Zhang <qshanz at cn.ibm.com> Date: Sunday, August 19, 2018 at 10:10 PM To: "llvm-commits at lists.llvm.org" <llvm-commits at lists.llvm.org> Subject: A question about the widely used "xxx | true" inside llvm Hi, guys, I notice that, we are widely using the statement "xxx | true" in llvm, such as "return SimplifyCFG(BB) | true;". I have no idea if there is any downside to use the logic operator "||" here. Instead, I indeed see the potential problem to use bitwise operator "|". Because the bitwise operator do not short-circuit and the language std didn't specify the evaluation order for its lhs and rhs expression. That means, compiler is free to optimize this statement to "return true". In shorts, IMO, it is legal to do the following optimization: bool bar() { return foo() | true; } --> bool bar() { return true; // the call to foo() is missing. } Best regards steven.zhang(张青山) XLC++ Compiler Frontend Developer IBM China Development Lab, Shanghai Tel: (8621)609-28454 Mobile: +8615900986116 E-mail: qshanz at cn.ibm.com<mailto:qshanz at cn.ibm.com> 关注IBM中国编译器开发团队 - 新浪微博: http://www.weibo.com/ibmcompiler<https://urldefense.proofpoint.com/v2/url?u=http-3A__www.weibo.com_ibmcompiler&d=DwMFaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=o3kDXzdBUE3ljQXKeTWOMw&m=Pq-hk0wEUZUsW1TCR5WSeKCTC9p-kFZHFQXEwhAc7-k&s=A_Y7Fzz24WHgMcnZjkQNCxix8GiaTORg0B7J-WtGp9c&e=> | developerWorks: http://t.cn/SPHWF4<https://urldefense.proofpoint.com/v2/url?u=http-3A__t.cn_SPHWF4&d=DwMFaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=o3kDXzdBUE3ljQXKeTWOMw&m=Pq-hk0wEUZUsW1TCR5WSeKCTC9p-kFZHFQXEwhAc7-k&s=EJ9ZnKu_kAhWtR9SgL-C_ooRh7TCL-EGc0wAotg4wb4&e=> "All things are difficult before they are easy." -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180820/4e3c2453/attachment.html>
Craig Topper via llvm-dev
2018-Aug-20 05:33 UTC
[llvm-dev] A question about the widely used "xxx | true" inside llvm
The language requires both sides of the | need to be evaluated. The compiler can't remove the function call if it has side effects. ~Craig On Sun, Aug 19, 2018 at 10:15 PM Shoaib Meenai via llvm-dev < llvm-dev at lists.llvm.org> wrote:> This is better suited to llvm-dev, so I'm directing it there. > > > > *From: *llvm-commits <llvm-commits-bounces at lists.llvm.org> on behalf of > Qing Shan Zhang via llvm-commits <llvm-commits at lists.llvm.org> > *Reply-To: *Qing Shan Zhang <qshanz at cn.ibm.com> > *Date: *Sunday, August 19, 2018 at 10:10 PM > *To: *"llvm-commits at lists.llvm.org" <llvm-commits at lists.llvm.org> > *Subject: *A question about the widely used "xxx | true" inside llvm > > > > Hi, guys, > > > > I notice that, we are widely using the statement "xxx | true" in llvm, > such as "return SimplifyCFG(BB) | true;". I have no idea if there is any > downside to use the logic operator "||" here. Instead, I indeed see the > potential problem to use bitwise operator "|". Because the bitwise operator > do not short-circuit and the language std didn't specify the evaluation > order for its lhs and rhs expression. That means, compiler is free to > optimize this statement to "return true". > > > > In shorts, IMO, it is legal to do the following optimization: > bool bar() { > return foo() | true; > } > --> > bool bar() { > return true; // the call to foo() is missing. > > } > > > Best regards > > steven.zhang(张青山) > XLC++ Compiler Frontend Developer > > IBM China Development Lab, Shanghai > Tel: (8621)609-28454 Mobile: +8615900986116 > E-mail: qshanz at cn.ibm.com > 关注IBM中国编译器开发团队 - 新浪微博: http://www.weibo.com/ibmcompiler > <https://urldefense.proofpoint.com/v2/url?u=http-3A__www.weibo.com_ibmcompiler&d=DwMFaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=o3kDXzdBUE3ljQXKeTWOMw&m=Pq-hk0wEUZUsW1TCR5WSeKCTC9p-kFZHFQXEwhAc7-k&s=A_Y7Fzz24WHgMcnZjkQNCxix8GiaTORg0B7J-WtGp9c&e=> > | developerWorks: http://t.cn/SPHWF4 > <https://urldefense.proofpoint.com/v2/url?u=http-3A__t.cn_SPHWF4&d=DwMFaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=o3kDXzdBUE3ljQXKeTWOMw&m=Pq-hk0wEUZUsW1TCR5WSeKCTC9p-kFZHFQXEwhAc7-k&s=EJ9ZnKu_kAhWtR9SgL-C_ooRh7TCL-EGc0wAotg4wb4&e=> > > "All things are difficult before they are easy." > > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180819/2f94c8c5/attachment.html>
Justin Bogner via llvm-dev
2018-Aug-20 06:37 UTC
[llvm-dev] A question about the widely used "xxx | true" inside llvm
Shoaib Meenai via llvm-dev <llvm-dev at lists.llvm.org> writes:> This is better suited to llvm-dev, so I'm directing it there. > > From: llvm-commits <llvm-commits-bounces at lists.llvm.org> on behalf of > Qing Shan Zhang via llvm-commits <llvm-commits at lists.llvm.org> > Reply-To: Qing Shan Zhang <qshanz at cn.ibm.com> > Date: Sunday, August 19, 2018 at 10:10 PM > To: "llvm-commits at lists.llvm.org" <llvm-commits at lists.llvm.org> > Subject: A question about the widely used "xxx | true" inside llvm > > Hi, guys, > > I notice that, we are widely using the statement "xxx | true" in llvm, > such as "return SimplifyCFG(BB) | true;". I have no idea if there is > any downside to use the logic operator "||" here. Instead, I indeed > see the potential problem to use bitwise operator "|". Because the > bitwise operator do not short-circuit and the language std didn't > specify the evaluation order for its lhs and rhs expression. That > means, compiler is free to optimize this statement to "return true".I think "widely using" is overstating the issue. This seems to be happening in a lot of places in SimplifyCFG though, and from looking at the code and the history it's pretty clear to me that the uses of the bitwise or are either stylistic or unintentional. I've gone ahead and changed all of those to use || for clarity in r340153.> In shorts, IMO, it is legal to do the following optimization: > bool bar() { > return foo() | true; > } > --> > bool bar() { > return true; // the call to foo() is missing. > }This is only legal if the compiler can prove that foo() has no side effects, which isn't true in this case. Using bitwise or here is certainly confusing, but there isn't a correctness issue.> Best regards > > steven.zhang(张青山) > XLC++ Compiler Frontend Developer > > IBM China Development Lab, Shanghai > Tel: (8621)609-28454 Mobile: +8615900986116 > E-mail: qshanz at cn.ibm.com<mailto:qshanz at cn.ibm.com> > 关注IBM中国编译器开发团队 - 新浪微博: > http://www.weibo.com/ibmcompiler<https://urldefense.proofpoint.com/v2/url?u=http-3A__www.weibo.com_ibmcompiler&d=DwMFaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=o3kDXzdBUE3ljQXKeTWOMw&m=Pq-hk0wEUZUsW1TCR5WSeKCTC9p-kFZHFQXEwhAc7-k&s=A_Y7Fzz24WHgMcnZjkQNCxix8GiaTORg0B7J-WtGp9c&e=> > | developerWorks: > http://t.cn/SPHWF4<https://urldefense.proofpoint.com/v2/url?u=http-3A__t.cn_SPHWF4&d=DwMFaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=o3kDXzdBUE3ljQXKeTWOMw&m=Pq-hk0wEUZUsW1TCR5WSeKCTC9p-kFZHFQXEwhAc7-k&s=EJ9ZnKu_kAhWtR9SgL-C_ooRh7TCL-EGc0wAotg4wb4&e=> > > "All things are difficult before they are easy." > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Chris Lattner via llvm-dev
2018-Aug-23 04:30 UTC
[llvm-dev] A question about the widely used "xxx | true" inside llvm
On Aug 19, 2018, at 10:33 PM, Craig Topper via llvm-dev <llvm-dev at lists.llvm.org> wrote:> The language requires both sides of the | need to be evaluated. The compiler can't remove the function call if it has side effects.Correct. If you are somehow really bothered by this, we could also use: bool bar() { return foo(), true; } -Chris -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180822/30b1b148/attachment.html>