summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
Commit message (Collapse)AuthorAgeFilesLines
* [InstCombine] add (sext i1 X), 1 --> zext (not X)Sanjay Patel2017-06-251-9/+18
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | http://rise4fun.com/Alive/i8Q A narrow bitwise logic op is obviously better than math for value tracking, and zext is better than sext. Typically, the 'not' will be folded into an icmp predicate. The IR difference would even survive through codegen for x86, so we would see worse code: https://godbolt.org/g/C14HMF one_or_zero(int, int): # @one_or_zero(int, int) xorl %eax, %eax cmpl %esi, %edi setle %al retq one_or_zero_alt(int, int): # @one_or_zero_alt(int, int) xorl %ecx, %ecx cmpl %esi, %edi setg %cl movl $1, %eax subl %ecx, %eax retq llvm-svn: 306243
* [InstCombine] Pass a proper context instruction to all of the calls into ↵Craig Topper2017-06-091-6/+10
| | | | | | | | | | | | | | | | InstSimplify Summary: This matches the behavior we already had for compares and makes us consistent everywhere. Reviewers: dberlin, hfinkel, spatel Reviewed By: dberlin Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D33604 llvm-svn: 305049
* [InstCombine][InstSimplify] Use APInt::isNullValue/isOneValue to reduce ↵Craig Topper2017-06-071-3/+4
| | | | | | | | compiled code for comparing APInts with 0 and 1. NFC These methods are specifically optimized to only counting leading zeros without an additional uint64_t compare. llvm-svn: 304876
* [ValueTracking] Convert most of the calls to computeKnownBits to use the ↵Craig Topper2017-05-241-10/+4
| | | | | | | | | | version that returns the KnownBits object. This continues the changes started when computeSignBit was replaced with this new version of computeKnowBits. Differential Revision: https://reviews.llvm.org/D33431 llvm-svn: 303773
* [InstCombine] Cleanup the interface for overflow checksCraig Topper2017-05-221-12/+14
| | | | | | | | | | | | | | | | | | Summary: Fix naming conventions and const correctness. This completes the changes made in rL303029. Patch by Yoav Ben-Shalom. Reviewers: craig.topper Reviewed By: craig.topper Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D33377 llvm-svn: 303529
* [KnownBits] Use isNegative/isNonNegative to shorten some code. NFCCraig Topper2017-05-221-2/+2
| | | | llvm-svn: 303522
* [ValueTracking] Replace all uses of ComputeSignBit with computeKnownBits.Craig Topper2017-05-151-7/+3
| | | | | | | | This patch finishes off the conversion of ComputeSignBit to computeKnownBits. Differential Revision: https://reviews.llvm.org/D33166 llvm-svn: 303035
* [InstCombine] Merge duplicate functionality between InstCombine and ↵Craig Topper2017-05-151-95/+5
| | | | | | | | | | | | | | | | | | | | | | | ValueTracking Summary: Merge overflow computation for signed add, appearing both in InstCombine and ValueTracking. As part of the merge, cleanup the interface for overflow checks in InstCombine. Patch by Yoav Ben-Shalom. Reviewers: craig.topper, majnemer Reviewed By: craig.topper Subscribers: takuto.ikuta, llvm-commits Differential Revision: https://reviews.llvm.org/D32946 llvm-svn: 303029
* [InstCombine] add (ashr (shl i32 X, 31), 31), 1 --> and (not X), 1Sanjay Patel2017-05-101-0/+10
| | | | | | | | | | | | | | This is another step towards favoring 'not' ops over random 'xor' in IR: https://bugs.llvm.org/show_bug.cgi?id=32706 This transformation may have occurred in longer IR sequences using computeKnownBits, but that could be much more expensive to calculate. As the scalar result shows, we do not currently favor 'not' in all cases. The 'not' created by the transform is transformed again (unnecessarily). Vectors don't have this problem because vectors are (wrongly) excluded from several other combines. llvm-svn: 302659
* [InstCombine] add helper function for add X, C folds; NFCISanjay Patel2017-05-101-34/+45
| | | | llvm-svn: 302605
* [InstCombine][KnownBits] Use KnownBits better to detect nsw addsCraig Topper2017-05-031-32/+44
| | | | | | | | | | | Change checkRippleForAdd from a heuristic to a full check - if it is provable that the add does not overflow return true, otherwise false. Patch by Yoav Ben-Shalom Differential Revision: https://reviews.llvm.org/D32686 llvm-svn: 302093
* [APInt] Add clearSignBit method. Use it and setSignBit in a few places. NFCICraig Topper2017-04-281-1/+1
| | | | llvm-svn: 301656
* InstCombine: Use the new SimplifyQuery versions of Simplify*. Use ↵Daniel Berlin2017-04-261-6/+4
| | | | | | AssumptionCache, DominatorTree, TargetLibraryInfo everywhere. llvm-svn: 301464
* [ValueTracking] Introduce a KnownBits struct to wrap the two APInts for ↵Craig Topper2017-04-261-26/+21
| | | | | | | | | | | | | | | | computeKnownBits This patch introduces a new KnownBits struct that wraps the two APInt used by computeKnownBits. This allows us to treat them as more of a unit. Initially I've just altered the signatures of computeKnownBits and InstCombine's simplifyDemandedBits to pass a KnownBits reference instead of two separate APInt references. I'll do similar to the SelectionDAG version of computeKnownBits/simplifyDemandedBits as a separate patch. I've added a constructor that allows initializing both APInts to the same bit width with a starting value of 0. This reduces the repeated pattern of initializing both APInts. Once place default constructed the APInts so I added a default constructor for those cases. Going forward I would like to add more methods that will work on the pairs. For example trunc, zext, and sext occur on both APInts together in several places. We should probably add a clear method that can be used to clear both pieces. Maybe a method to check for conflicting information. A method to return (Zero|One) so we don't write it out everywhere. Maybe a method for (Zero|One).isAllOnesValue() to determine if all bits are known. I'm sure there are many other methods we can come up with. Differential Revision: https://reviews.llvm.org/D32376 llvm-svn: 301432
* InstCombine: Fix assert when reassociating fsub with undefMatt Arsenault2017-04-241-0/+5
| | | | | | | | | | | | | There is logic to track the expected number of instructions produced. It thought in this case an instruction would be necessary to negate the result, but here it folded into a ConstantExpr fneg when the non-undef value operand was cancelled out by the second fsub. I'm not sure why we don't fold constant FP ops with undef currently, but I think that would also avoid this problem. llvm-svn: 301199
* Fix for PR32740 - Invalid floating type, unreachable between r300969 and r301029Artur Pilipenko2017-04-221-2/+5
| | | | | | The bug was introduced by r301018 "[InstCombine] fadd double (sitofp x), y check that the promotion is valid". The patch didn't expect that fadd can be on vectors not necessarily scalars. Add vector support along with the test. llvm-svn: 301070
* [InstCombine] fadd double (sitofp x), y check that the promotion is validArtur Pilipenko2017-04-211-22/+38
| | | | | | | | | | | | | | | Doing these transformations check that the result of integer addition is representable in the FP type. (fadd double (sitofp x), fpcst) --> (sitofp (add int x, intcst)) (fadd double (sitofp x), (sitofp y)) --> (sitofp (add int x, y)) This is a fix for https://bugs.llvm.org//show_bug.cgi?id=27036 Reviewed By: andrew.w.kaylor, scanon, spatel Differential Revision: https://reviews.llvm.org/D31182 llvm-svn: 301018
* [APInt] Rename getSignBit to getSignMaskCraig Topper2017-04-201-6/+6
| | | | | | | | getSignBit is a static function that creates an APInt with only the sign bit set. getSignMask seems like a better name to convey its functionality. In fact several places use it and then store in an APInt named SignMask. Differential Revision: https://reviews.llvm.org/D32108 llvm-svn: 300856
* [InstCombine] Support folding a subtract with a constant LHS into a phi nodeCraig Topper2017-04-141-0/+5
| | | | | | | | | | | | We currently only support folding a subtract into a select but not a PHI. This fixes that. I had to fix an assumption in FoldOpIntoPhi that assumed the PHI node was always in operand 0. Now we pass it in like we do for FoldOpIntoSelect. But we still require some dancing to find the Constant when we create the BinOp or ConstantExpr. This is based code is similar to what we do for selects. Since I touched all call sites, this also renames FoldOpIntoPhi to foldOpIntoPhi to match coding standards. Differential Revision: https://reviews.llvm.org/D31686 llvm-svn: 300363
* Fix spelling compliment->complement. Mostly refering to 2s complement. NFCCraig Topper2017-04-111-2/+2
| | | | llvm-svn: 299970
* [InstCombine] Use commutable matchers and m_OneUse in visitSub to shorten ↵Craig Topper2017-04-101-15/+11
| | | | | | | | code. Add missing test cases. In one case I removed commute handling for a multiply with a constant since we'll eventually get the constant on the right hand side. llvm-svn: 299863
* [InstCombine] Use m_c_Add to shorten some code. Add testcases for this fold ↵Craig Topper2017-04-101-2/+1
| | | | | | since they were missing. NFC llvm-svn: 299853
* [InstCombine] Support folding of add instructions with vector constants into ↵Craig Topper2017-04-101-7/+2
| | | | | | | | | | select operations We currently only fold scalar add of constants into selects. This improves this to support vectors too. Differential Revision: https://reviews.llvm.org/D31683 llvm-svn: 299847
* [InstCombine] Use commutable and/or/xor matchers to simplify some codeCraig Topper2017-04-101-9/+4
| | | | | | | | | | | | | | | | | Summary: This is my first time using the commutable matchers so wanted to make sure I was doing it right. Are there any other matcher tricks to further shrink this? Can we commute the whole match so we don't have to LHS and RHS separately? Reviewers: davide, spatel Reviewed By: davide Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D31680 llvm-svn: 299840
* [InstCombine] Remove testing assert I accidentally left in r299710.Craig Topper2017-04-061-3/+1
| | | | llvm-svn: 299715
* [InstCombine] When checking to see if we can turn subtracts of 2^n - 1 into ↵Craig Topper2017-04-061-5/+7
| | | | | | | | xor, we only need to call computeKnownBits on the RHS not the whole subtract. While there use isMask instead of isPowerOf2(C+1) Calling computeKnownBits on the RHS should allows us to recurse one step further. isMask is equivalent to the isPowerOf2(C+1) except in the case where C is all ones. But that was already handled earlier by creating a not which is an Xor with all ones. So this should be fine. llvm-svn: 299710
* [InstCombine] rename variable for easier reading; NFCSanjay Patel2017-04-041-7/+8
| | | | | | We usually give constants a 'C' somewhere in the name... llvm-svn: 299474
* [InstCombine] Turn subtract of vectors of i1 into xor like we do for scalar ↵Craig Topper2017-04-041-1/+1
| | | | | | i1. Matches what we already do for add. llvm-svn: 299472
* [InstCombine] Fix typo last->least. NFCCraig Topper2017-03-301-3/+3
| | | | llvm-svn: 299123
* NFC. InstCombiner::visitFAdd extract LHSIntVal/RHSIntVal local variablesArtur Pilipenko2017-03-211-9/+11
| | | | llvm-svn: 298359
* [InstCombine] don't try SimplifyDemandedInstructionBits from add/sub because ↵Sanjay Patel2017-02-221-8/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | it's slow and unlikely to succeed Notably, no regression tests change when we remove these calls, and these are expensive calls. The motivation comes from the general acknowledgement that the compiler is getting slower: http://lists.llvm.org/pipermail/llvm-dev/2017-January/109188.html http://lists.llvm.org/pipermail/llvm-dev/2016-December/108279.html And specifically the test case attached to PR32037: https://bugs.llvm.org//show_bug.cgi?id=32037 Profiling the middle-end (opt) part of the compile: $ ./opt -O2 row_common.bc -o /dev/null ...visitAdd and visitSub are near the top of the instcombine list, and the calls to SimplifyDemandedInstructionBits() are high within each of those. Those calls account for 1%+ of the opt time in either debug or release profiles. And that's the rough win I see from this patch when testing opt built release from r295864 on an iMac with Haswell 4GHz (model 4790K). It seems unlikely that we'd be able to eliminate add/sub or change their operands given that add/sub normally affect all bits, and the PR32037 example shows no IR difference after this change using -O2. Also worth noting - the code comment in visitAdd: // This handles stuff like (X & 254)+1 -> (X&254)|1 ...isn't true. That transform is handled later with a call to haveNoCommonBitsSet(). Differential Revision: https://reviews.llvm.org/D30270 llvm-svn: 295898
* [InstCombine] add nsw/nuw X, signbit --> or X, signbitSanjay Patel2017-02-181-2/+9
| | | | | | | | | Changing to 'or' (rather than 'xor' when no wrapping flags are set) allows icmp simplifies to happen as expected. Differential Revision: https://reviews.llvm.org/D29729 llvm-svn: 295574
* [InstCombine] improve formatting; NFCSanjay Patel2017-02-151-6/+3
| | | | llvm-svn: 295237
* [InstCombine] add a wrapper for a common pair of transforms; NFCISanjay Patel2017-01-101-9/+3
| | | | | | | Some of the callers are artificially limiting this transform to integer types; this should make it easier to incrementally remove that restriction. llvm-svn: 291620
* [InstCombine] Combine adds across a zextDavid Majnemer2017-01-041-0/+12
| | | | | | | | | We can perform the following: (add (zext (add nuw X, C1)), C2) -> (zext (add nuw X, C1+C2)) This is only possible if C2 is negative and C2 is greater than or equal to negative C1. llvm-svn: 290927
* [InstCombine] Address post-commit feedbackDavid Majnemer2016-12-301-1/+2
| | | | llvm-svn: 290741
* [InstCombine] More thoroughly canonicalize the position of zextsDavid Majnemer2016-12-301-9/+47
| | | | | | | | We correctly canonicalized (add (sext x), (sext y)) to (sext (add x, y)) where possible. However, we didn't perform the same canonicalization for zexts or for muls. llvm-svn: 290733
* Revert @llvm.assume with operator bundles (r289755-r289757)Daniel Jasper2016-12-191-5/+5
| | | | | | | This creates non-linear behavior in the inliner (see more details in r289755's commit thread). llvm-svn: 290086
* Remove the AssumptionCacheHal Finkel2016-12-151-5/+5
| | | | | | | | | After r289755, the AssumptionCache is no longer needed. Variables affected by assumptions are now found by using the new operand-bundle-based scheme. This new scheme is more computationally efficient, and also we need much less code... llvm-svn: 289756
* [InstCombine] use m_APInt to allow sub with constant folds for splat vectorsSanjay Patel2016-10-141-18/+19
| | | | llvm-svn: 284247
* [InstCombine] sub X, sext(bool Y) -> add X, zext(bool Y)Sanjay Patel2016-10-141-0/+11
| | | | | | | | | | | | Prefer add/zext because they are better supported in terms of value-tracking. Note that the backend should be prepared for this IR canonicalization (including vector types) after: https://reviews.llvm.org/rL284015 Differential Revision: https://reviews.llvm.org/D25135 llvm-svn: 284241
* InstCombine: Replace some never-null pointers with references. NFCJustin Bogner2016-08-051-5/+5
| | | | llvm-svn: 277792
* [InstCombine] fold add(zext(xor X, C), C) --> sext X when C is INT_MIN in ↵Sanjay Patel2016-07-191-0/+10
| | | | | | | | | | | | | | | | | | | the source type The pattern may look more obviously like a sext if written as: define i32 @g(i16 %x) { %zext = zext i16 %x to i32 %xor = xor i32 %zext, 32768 %add = add i32 %xor, -32768 ret i32 %add } We already have that fold in visitAdd(). Differential Revision: https://reviews.llvm.org/D22477 llvm-svn: 276035
* [InstCombine] allow X + signbit --> X ^ signbit for vector splatsSanjay Patel2016-07-161-3/+10
| | | | llvm-svn: 275691
* Apply clang-tidy's modernize-loop-convert to most of lib/Transforms.Benjamin Kramer2016-06-261-6/+3
| | | | | | Only minor manual fixes. No functionality change intended. llvm-svn: 273808
* Delete more dead code.Rafael Espindola2016-06-221-22/+0
| | | | | | Found by gcc 6. llvm-svn: 273402
* Remove uses of builtin comma operator.Richard Trieu2016-02-181-5/+12
| | | | | | Cleanup for upcoming Clang warning -Wcomma. No functionality change intended. llvm-svn: 261270
* Fix Clang-tidy readability-redundant-control-flow warnings; other minor fixes.Eugene Zelenko2016-02-021-2/+0
| | | | | | Differential revision: http://reviews.llvm.org/D16793 llvm-svn: 259539
* function names start with a lowercase letter; NFCSanjay Patel2016-02-011-15/+15
| | | | llvm-svn: 259425
* [InstCombine] Fix indentation. NFC.Craig Topper2015-12-211-2/+2
| | | | llvm-svn: 256131
OpenPOWER on IntegriCloud