This is an archive of the discontinued LLVM Phabricator instance.

Add FNeg IR constant folding
ClosedPublic

Authored by cameron.mcinally on May 3 2019, 5:07 PM.

Details

Summary

Here's another subset of D61419. This patch contains the FNeg IR constant folding changes and some code in Analysis so that InstCombine can make use of it.

There are only two folds being performed right now:

fneg undef -> undef
fneg C -> -C

New tests for those 2 folds have been added to the existing InstCombine fneg.ll tests (which are all fsub(-0.0, X) tests right now).

In addition to the new tests, there are quite a few existing tests in place that used to operate on fsub(-0.0, X), so coverage is pretty good. If you'd like to convince yourself of this, you can comment out the call to ConstantFoldUnaryInstruction(...) in lib/IR/Constants.cpp.

Diff Detail

Repository
rL LLVM

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptMay 3 2019, 5:07 PM
spatel added inline comments.May 4 2019, 6:50 AM
llvm/include/llvm/Analysis/ConstantFolding.h
75–76

IIUC, this comment is wrong. If it fails, it returns a nullptr. No attempt is made to reduce a ConstExpr (ie, there is no SymbolicallyEvaluateUnOp()).

The existing comment below for binops looks misleading if I interpreted that correctly.

llvm/lib/Analysis/ConstantFolding.cpp
1002

I know this is modified from the existing comment line, but I don't see how either adds value. I'd remove both lines.

llvm/lib/IR/ConstantFold.cpp
931

We already asserted that we have a unary op, so can this be "default: llvm_unreachable..."?

llvm/lib/IR/Constants.cpp
1834

Code comment doesn't add anything IMO.

spatel added inline comments.May 4 2019, 6:56 AM
llvm/include/llvm/Analysis/ConstantFolding.h
75–76

Oops - disregard. I got my overloads mixed up.

llvm/test/Transforms/InstCombine/fneg.ll
158–159

Please pre-commit these tests to trunk, so we'll just show the test diffs from this code patch.

cameron.mcinally marked an inline comment as done.May 4 2019, 7:53 AM
cameron.mcinally marked 6 inline comments as done.May 4 2019, 8:28 AM
cameron.mcinally added inline comments.
llvm/lib/IR/ConstantFold.cpp
931

This was copied from ConstantFoldBinaryInstruction(...), so that should probably be updated to keep them in sync, as well as any similar switches. I can do that in a separate patch, if you'd like.

cameron.mcinally marked an inline comment as done.

Address Sanjay's comments.

spatel accepted this revision.May 5 2019, 8:29 AM

LGTM - although you probably want to change that 'default' in the switch statement back to match based on the comments in D61555.

Also (and sorry I didn't see this earlier) - the new tests for the minimal constant folding really could go in test/Analysis/ConstantFolding/ and use 'opt -constprop'. Ie, we shouldn't need to run all of instcombine or even instsimplify to see those diffs. We don't have great separation for those boundaries in several existing cases, so I'd call it a 'nice to have' change rather than a requirement.

This revision is now accepted and ready to land.May 5 2019, 8:29 AM

Revert default case change from ConstantFoldUnaryInstruction(...) and move fneg.ll tests to new file test/Analysis/ConstantFolding/fneg.ll.

Ack, forgot to include the Differential link in the commit log. Was merged with:

Author: mcinally
Date: Sun May  5 09:07:09 2019
New Revision: 359982

URL: http://llvm.org/viewvc/llvm-project?rev=359982&view=rev
Log:
Add FNeg IR constant folding support

Added:
    llvm/trunk/test/Analysis/ConstantFolding/fneg.ll
Modified:
    llvm/trunk/include/llvm/Analysis/ConstantFolding.h
    llvm/trunk/lib/Analysis/ConstantFolding.cpp
    llvm/trunk/lib/IR/ConstantFold.cpp
    llvm/trunk/lib/IR/ConstantFold.h
    llvm/trunk/lib/IR/Constants.cpp
    llvm/trunk/test/Transforms/InstCombine/fneg.ll
    llvm/trunk/test/Transforms/InstCombine/fsub.ll
    llvm/trunk/test/Transforms/InstCombine/inselt-binop.ll
    llvm/trunk/test/Transforms/Reassociate/crash2.ll
    llvm/trunk/unittests/IR/ConstantsTest.cpp