Hi!> sin/cos etc should already be handled by lib/Analysis/ConstantFolding.cpp. >Thanks for the hint and it works! Now I have a new Problem: I have this function: float foo(float a, float b) { float x = a * b * 0.0f; return cos(0.5) * sin(0.5) * x; }; after compiling it with clang (cpp mode) and renaming _ZSt3sinf to sin and _ZSt3cosf to cos I get the following: define float @_Z3fooff(float %a, float %b) nounwind { entry: %mul = fmul float %a, %b ; <float> [#uses=1] %mul2 = fmul float %mul, 0.000000e+000 ; <float> [#uses=1] %mul6 = fmul float 0x3FDAED54A0000000, %mul2 ; <float> [#uses=1] ret float %mul6 } the sin and cos calls are folded, but not the mul by zero. May be this is missing in llvm::ConstantFoldInstOperands in ConsantFolding.cpp? I would expect the following optimizations, but didn't find them in the code: x + 0 = x x * 0 = 0 x * 1 = 1 x * -1 = -x -Jochen
On 3 March 2010 11:56, Jochen Wilhelmy <j.wilhelmy at arcor.de> wrote:> > the sin and cos calls are folded, but not the mul by zero. >Is x*0 => 0 true if isnan(x)? And cos(x)*sin(x) makes me desperately want to fold it to sin(2*x)/2, but I suppose that's not allowed either.
Nope, any multiply with q.nan results in q.nan, no matter what the other arguments value is. So '0 * q.nan == q.nan', and not 0. -----Original Message----- From: llvmdev-bounces at cs.uiuc.edu [mailto:llvmdev-bounces at cs.uiuc.edu] On Behalf Of me22 Sent: Wednesday, March 03, 2010 9:18 AM To: Jochen Wilhelmy Cc: llvmdev at cs.uiuc.edu Subject: Re: [LLVMdev] folding x * 0 = 0 On 3 March 2010 11:56, Jochen Wilhelmy <j.wilhelmy at arcor.de> wrote:> > the sin and cos calls are folded, but not the mul by zero. >Is x*0 => 0 true if isnan(x)? And cos(x)*sin(x) makes me desperately want to fold it to sin(2*x)/2, but I suppose that's not allowed either. _______________________________________________ LLVM Developers mailing list LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
> Is x*0 => 0 true if isnan(x)? >then where do I have to add it if I want to make it a non-standard modification of my local llvm version? would it make sense to add a subset of float to llvm or a kind of modifier (e.g. valid float to indicate that it is always valid and not nan) to allow more aggressive optimization? just like e.g. inbounds for load -Jochen
Hi Jochen, I just wanted to point out that if x = inf the result of x * 0 is in indeterminate form so reducing it to zero would give the wrong result in that case. Thanks, Javier On 3/3/2010 8:56 AM, Jochen Wilhelmy wrote:> Hi! > > > >> sin/cos etc should already be handled by lib/Analysis/ConstantFolding.cpp. >> >> > Thanks for the hint and it works! > Now I have a new Problem: > > I have this function: > > float foo(float a, float b) > { > float x = a * b * 0.0f; > return cos(0.5) * sin(0.5) * x; > }; > > after compiling it with clang (cpp mode) and renaming _ZSt3sinf to sin > and _ZSt3cosf to cos I get the following: > > define float @_Z3fooff(float %a, float %b) nounwind { > entry: > %mul = fmul float %a, %b ;<float> [#uses=1] > %mul2 = fmul float %mul, 0.000000e+000 ;<float> [#uses=1] > %mul6 = fmul float 0x3FDAED54A0000000, %mul2 ;<float> [#uses=1] > ret float %mul6 > } > > the sin and cos calls are folded, but not the mul by zero. > May be this is missing in llvm::ConstantFoldInstOperands in > ConsantFolding.cpp? > > I would expect the following optimizations, but didn't find them in the > code: > x + 0 = x > x * 0 = 0 > x * 1 = 1 > x * -1 = -x > > -Jochen > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >
> > I just wanted to point out that if x = inf the result of x * 0 is in > indeterminate form so reducing it to zero would give the wrong result > in that case.Yes, thanks for the hint, but someone else already pointed out that nan*0 is also not 0. I have a special application where inf and nan will not occur, therefore I will patch my local llvm to optimize floats more aggressively. If the result of 2.0 * 2.0 is between 3.9 and 4.1 this is sufficient for me if it's faster ;-) -Jochen