| Nate Begeman | 2504fe2 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 1 | //===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===// |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run |
| 11 | // both before and after the DAG is legalized. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 12 | // |
| Dan Gohman | 4539987 | 2009-04-25 17:09:45 +0000 | [diff] [blame] | 13 | // This pass is not a substitute for the LLVM IR instcombine pass. This pass is |
| 14 | // primarily intended to handle simplification opportunities that are implicit |
| 15 | // in the LLVM IR and exposed by the various codegen lowering phases. |
| 16 | // |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #define DEBUG_TYPE "dagcombine" |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/SelectionDAG.h" |
| Chris Lattner | 48fb92f | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallPtrSet.h" |
| 22 | #include "llvm/ADT/Statistic.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/AliasAnalysis.h" |
| 24 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 25 | #include "llvm/CodeGen/MachineFunction.h" |
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/DataLayout.h" |
| 27 | #include "llvm/IR/DerivedTypes.h" |
| 28 | #include "llvm/IR/Function.h" |
| 29 | #include "llvm/IR/LLVMContext.h" |
| Jim Laskey | 5d19d59 | 2006-09-21 16:28:59 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CommandLine.h" |
| Chris Lattner | 48fb92f | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
| Torok Edwin | ccb29cd | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 32 | #include "llvm/Support/ErrorHandling.h" |
| Chris Lattner | 48fb92f | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 33 | #include "llvm/Support/MathExtras.h" |
| Chris Lattner | 4dc3edd | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetLowering.h" |
| 36 | #include "llvm/Target/TargetMachine.h" |
| 37 | #include "llvm/Target/TargetOptions.h" |
| Quentin Colombet | de0e062 | 2013-10-11 18:29:42 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetRegisterInfo.h" |
| Hal Finkel | 5ef4dcc | 2013-08-29 03:29:55 +0000 | [diff] [blame] | 39 | #include "llvm/Target/TargetSubtargetInfo.h" |
| Chris Lattner | bd39c1a | 2005-09-09 23:53:39 +0000 | [diff] [blame] | 40 | #include <algorithm> |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 41 | using namespace llvm; |
| 42 | |
| Chris Lattner | aee775a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 43 | STATISTIC(NodesCombined , "Number of dag nodes combined"); |
| 44 | STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created"); |
| 45 | STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created"); |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 46 | STATISTIC(OpsNarrowed , "Number of load/op/store narrowed"); |
| Evan Cheng | d42641c | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 47 | STATISTIC(LdStFP2Int , "Number of fp load/store pairs transformed to int"); |
| Quentin Colombet | de0e062 | 2013-10-11 18:29:42 +0000 | [diff] [blame] | 48 | STATISTIC(SlicedLoads, "Number of load sliced"); |
| Chris Lattner | aee775a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 49 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 50 | namespace { |
| Jim Laskey | 0463e08 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 51 | static cl::opt<bool> |
| Owen Anderson | 7b8d2ae | 2010-09-19 21:01:26 +0000 | [diff] [blame] | 52 | CombinerAA("combiner-alias-analysis", cl::Hidden, |
| Hal Finkel | 5fb0734 | 2014-01-25 17:32:37 +0000 | [diff] [blame] | 53 | cl::desc("Enable DAG combiner alias-analysis heuristics")); |
| Jim Laskey | df2ccc3 | 2006-10-12 15:22:24 +0000 | [diff] [blame] | 54 | |
| Jim Laskey | 55e4dca | 2006-10-18 19:08:31 +0000 | [diff] [blame] | 55 | static cl::opt<bool> |
| 56 | CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden, |
| Hal Finkel | 5fb0734 | 2014-01-25 17:32:37 +0000 | [diff] [blame] | 57 | cl::desc("Enable DAG combiner's use of IR alias analysis")); |
| Jim Laskey | 55e4dca | 2006-10-18 19:08:31 +0000 | [diff] [blame] | 58 | |
| Hal Finkel | dbebb52 | 2014-01-25 19:24:54 +0000 | [diff] [blame] | 59 | // FIXME: Enable the use of TBAA. There are two known issues preventing this: |
| 60 | // 1. Stack coloring does not update TBAA when merging allocas |
| 61 | // 2. CGP inserts ptrtoint/inttoptr pairs when sinking address computations. |
| 62 | // Because BasicAA does not handle inttoptr, we'll often miss basic type |
| 63 | // punning idioms that we need to catch so we don't miscompile real-world |
| 64 | // code. |
| 65 | static cl::opt<bool> |
| 66 | UseTBAA("combiner-use-tbaa", cl::Hidden, cl::init(false), |
| 67 | cl::desc("Enable DAG combiner's use of TBAA")); |
| 68 | |
| Hal Finkel | 9b2617a | 2014-01-25 17:32:39 +0000 | [diff] [blame] | 69 | #ifndef NDEBUG |
| 70 | static cl::opt<std::string> |
| 71 | CombinerAAOnlyFunc("combiner-aa-only-func", cl::Hidden, |
| 72 | cl::desc("Only use DAG-combiner alias analysis in this" |
| 73 | " function")); |
| 74 | #endif |
| 75 | |
| Quentin Colombet | de0e062 | 2013-10-11 18:29:42 +0000 | [diff] [blame] | 76 | /// Hidden option to stress test load slicing, i.e., when this option |
| 77 | /// is enabled, load slicing bypasses most of its profitability guards. |
| 78 | static cl::opt<bool> |
| 79 | StressLoadSlicing("combiner-stress-load-slicing", cl::Hidden, |
| 80 | cl::desc("Bypass the profitability model of load " |
| 81 | "slicing"), |
| 82 | cl::init(false)); |
| 83 | |
| Jim Laskey | 6549d22 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 84 | //------------------------------ DAGCombiner ---------------------------------// |
| 85 | |
| Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 86 | class DAGCombiner { |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 87 | SelectionDAG &DAG; |
| Dan Gohman | 619ef48 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 88 | const TargetLowering &TLI; |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 89 | CombineLevel Level; |
| Bill Wendling | 026e5d7 | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 90 | CodeGenOpt::Level OptLevel; |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 91 | bool LegalOperations; |
| 92 | bool LegalTypes; |
| Quentin Colombet | de0e062 | 2013-10-11 18:29:42 +0000 | [diff] [blame] | 93 | bool ForCodeSize; |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 94 | |
| 95 | // Worklist of all of the nodes that need to be simplified. |
| James Molloy | 67b6b11 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 96 | // |
| 97 | // This has the semantics that when adding to the worklist, |
| 98 | // the item added must be next to be processed. It should |
| 99 | // also only appear once. The naive approach to this takes |
| 100 | // linear time. |
| 101 | // |
| 102 | // To reduce the insert/remove time to logarithmic, we use |
| 103 | // a set and a vector to maintain our worklist. |
| 104 | // |
| 105 | // The set contains the items on the worklist, but does not |
| 106 | // maintain the order they should be visited. |
| 107 | // |
| 108 | // The vector maintains the order nodes should be visited, but may |
| 109 | // contain duplicate or removed nodes. When choosing a node to |
| 110 | // visit, we pop off the order stack until we find an item that is |
| 111 | // also in the contents set. All operations are O(log N). |
| 112 | SmallPtrSet<SDNode*, 64> WorkListContents; |
| Benjamin Kramer | e1e549d | 2012-03-10 00:23:58 +0000 | [diff] [blame] | 113 | SmallVector<SDNode*, 64> WorkListOrder; |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 114 | |
| Jim Laskey | dcb2b83 | 2006-10-16 20:52:31 +0000 | [diff] [blame] | 115 | // AA - Used for DAG load/store alias analysis. |
| 116 | AliasAnalysis &AA; |
| 117 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 118 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
| 119 | /// the instruction to the work lists because they might get more simplified |
| 120 | /// now. |
| 121 | /// |
| 122 | void AddUsersToWorkList(SDNode *N) { |
| 123 | for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); |
| Nate Begeman | 2504fe2 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 124 | UI != UE; ++UI) |
| Dan Gohman | 91e5dcb | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 125 | AddToWorkList(*UI); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| Dan Gohman | 5c6d0c3 | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 128 | /// visit - call the node-specific routine that knows how to fold each |
| 129 | /// particular type of node. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 130 | SDValue visit(SDNode *N); |
| Dan Gohman | 5c6d0c3 | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 131 | |
| Chris Lattner | bc1c85b | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 132 | public: |
| James Molloy | 920ae8c | 2012-02-16 09:48:07 +0000 | [diff] [blame] | 133 | /// AddToWorkList - Add to the work list making sure its instance is at the |
| James Molloy | 67b6b11 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 134 | /// back (next to be processed.) |
| Chris Lattner | fbcd62d | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 135 | void AddToWorkList(SDNode *N) { |
| James Molloy | 67b6b11 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 136 | WorkListContents.insert(N); |
| 137 | WorkListOrder.push_back(N); |
| Chris Lattner | fbcd62d | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 138 | } |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 139 | |
| Chris Lattner | b2b9d6f | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 140 | /// removeFromWorkList - remove all instances of N from the worklist. |
| 141 | /// |
| 142 | void removeFromWorkList(SDNode *N) { |
| James Molloy | 67b6b11 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 143 | WorkListContents.erase(N); |
| Chris Lattner | e260ed8 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 144 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 145 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 146 | SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo, |
| Evan Cheng | fd81c73 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 147 | bool AddTo = true); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 148 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 149 | SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) { |
| Jim Laskey | dcf983c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 150 | return CombineTo(N, &Res, 1, AddTo); |
| Chris Lattner | bc1c85b | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 151 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 152 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 153 | SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1, |
| Evan Cheng | fd81c73 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 154 | bool AddTo = true) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 155 | SDValue To[] = { Res0, Res1 }; |
| Jim Laskey | dcf983c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 156 | return CombineTo(N, To, 2, AddTo); |
| Chris Lattner | bc1c85b | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 157 | } |
| Dan Gohman | e58ab79 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 158 | |
| 159 | void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 160 | |
| 161 | private: |
| 162 | |
| Chris Lattner | 375e1a7 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 163 | /// SimplifyDemandedBits - Check the specified integer node value to see if |
| Chris Lattner | 232024e | 2006-03-01 19:55:35 +0000 | [diff] [blame] | 164 | /// it can be simplified or if things it uses can be simplified by bit |
| Chris Lattner | 375e1a7 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 165 | /// propagation. If so, return true. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 166 | bool SimplifyDemandedBits(SDValue Op) { |
| Dan Gohman | 1d459e4 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 167 | unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits(); |
| 168 | APInt Demanded = APInt::getAllOnesValue(BitWidth); |
| Dan Gohman | ae2b6fb | 2008-02-27 00:25:32 +0000 | [diff] [blame] | 169 | return SimplifyDemandedBits(Op, Demanded); |
| 170 | } |
| 171 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 172 | bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded); |
| Chris Lattner | 04c7370 | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 173 | |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 174 | bool CombineToPreIndexedLoadStore(SDNode *N); |
| 175 | bool CombineToPostIndexedLoadStore(SDNode *N); |
| Quentin Colombet | de0e062 | 2013-10-11 18:29:42 +0000 | [diff] [blame] | 176 | bool SliceUpLoad(SDNode *N); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 177 | |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 178 | void ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad); |
| 179 | SDValue PromoteOperand(SDValue Op, EVT PVT, bool &Replace); |
| 180 | SDValue SExtPromoteOperand(SDValue Op, EVT PVT); |
| 181 | SDValue ZExtPromoteOperand(SDValue Op, EVT PVT); |
| Evan Cheng | af56fac | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 182 | SDValue PromoteIntBinOp(SDValue Op); |
| Evan Cheng | f1223bd | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 183 | SDValue PromoteIntShiftOp(SDValue Op); |
| Evan Cheng | e19aa5c | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 184 | SDValue PromoteExtend(SDValue Op); |
| 185 | bool PromoteLoad(SDValue Op); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 186 | |
| Craig Topper | e0b7118 | 2013-07-13 07:43:40 +0000 | [diff] [blame] | 187 | void ExtendSetCCUses(const SmallVectorImpl<SDNode *> &SetCCs, |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 188 | SDValue Trunc, SDValue ExtLoad, SDLoc DL, |
| Nick Lewycky | 6d677cf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 189 | ISD::NodeType ExtType); |
| 190 | |
| Dan Gohman | 5c6d0c3 | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 191 | /// combine - call the node-specific routine that knows how to fold each |
| 192 | /// particular type of node. If that doesn't do anything, try the |
| 193 | /// target-specific DAG combines. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 194 | SDValue combine(SDNode *N); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 195 | |
| 196 | // Visitation implementation - Implement dag node combining for different |
| 197 | // node types. The semantics are as follows: |
| 198 | // Return Value: |
| Evan Cheng | 5e7658c | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 199 | // SDValue.getNode() == 0 - No change was made |
| 200 | // SDValue.getNode() == N - N was replaced, is dead and has been handled. |
| 201 | // otherwise - N should be replaced by the returned Operand. |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 202 | // |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 203 | SDValue visitTokenFactor(SDNode *N); |
| 204 | SDValue visitMERGE_VALUES(SDNode *N); |
| 205 | SDValue visitADD(SDNode *N); |
| 206 | SDValue visitSUB(SDNode *N); |
| 207 | SDValue visitADDC(SDNode *N); |
| Craig Topper | 43a1bd6 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 208 | SDValue visitSUBC(SDNode *N); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 209 | SDValue visitADDE(SDNode *N); |
| Craig Topper | 43a1bd6 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 210 | SDValue visitSUBE(SDNode *N); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 211 | SDValue visitMUL(SDNode *N); |
| 212 | SDValue visitSDIV(SDNode *N); |
| 213 | SDValue visitUDIV(SDNode *N); |
| 214 | SDValue visitSREM(SDNode *N); |
| 215 | SDValue visitUREM(SDNode *N); |
| 216 | SDValue visitMULHU(SDNode *N); |
| 217 | SDValue visitMULHS(SDNode *N); |
| 218 | SDValue visitSMUL_LOHI(SDNode *N); |
| 219 | SDValue visitUMUL_LOHI(SDNode *N); |
| Benjamin Kramer | 2fd48f2 | 2011-05-21 18:31:55 +0000 | [diff] [blame] | 220 | SDValue visitSMULO(SDNode *N); |
| 221 | SDValue visitUMULO(SDNode *N); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 222 | SDValue visitSDIVREM(SDNode *N); |
| 223 | SDValue visitUDIVREM(SDNode *N); |
| 224 | SDValue visitAND(SDNode *N); |
| 225 | SDValue visitOR(SDNode *N); |
| 226 | SDValue visitXOR(SDNode *N); |
| 227 | SDValue SimplifyVBinOp(SDNode *N); |
| Craig Topper | 8238461 | 2012-09-11 01:45:21 +0000 | [diff] [blame] | 228 | SDValue SimplifyVUnaryOp(SDNode *N); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 229 | SDValue visitSHL(SDNode *N); |
| 230 | SDValue visitSRA(SDNode *N); |
| 231 | SDValue visitSRL(SDNode *N); |
| 232 | SDValue visitCTLZ(SDNode *N); |
| Chandler Carruth | 637cc6a | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 233 | SDValue visitCTLZ_ZERO_UNDEF(SDNode *N); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 234 | SDValue visitCTTZ(SDNode *N); |
| Chandler Carruth | 637cc6a | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 235 | SDValue visitCTTZ_ZERO_UNDEF(SDNode *N); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 236 | SDValue visitCTPOP(SDNode *N); |
| 237 | SDValue visitSELECT(SDNode *N); |
| Benjamin Kramer | d56ffc7 | 2013-04-26 09:19:19 +0000 | [diff] [blame] | 238 | SDValue visitVSELECT(SDNode *N); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 239 | SDValue visitSELECT_CC(SDNode *N); |
| 240 | SDValue visitSETCC(SDNode *N); |
| 241 | SDValue visitSIGN_EXTEND(SDNode *N); |
| 242 | SDValue visitZERO_EXTEND(SDNode *N); |
| 243 | SDValue visitANY_EXTEND(SDNode *N); |
| 244 | SDValue visitSIGN_EXTEND_INREG(SDNode *N); |
| 245 | SDValue visitTRUNCATE(SDNode *N); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 246 | SDValue visitBITCAST(SDNode *N); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 247 | SDValue visitBUILD_PAIR(SDNode *N); |
| 248 | SDValue visitFADD(SDNode *N); |
| 249 | SDValue visitFSUB(SDNode *N); |
| 250 | SDValue visitFMUL(SDNode *N); |
| Owen Anderson | 41b0665 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 251 | SDValue visitFMA(SDNode *N); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 252 | SDValue visitFDIV(SDNode *N); |
| 253 | SDValue visitFREM(SDNode *N); |
| 254 | SDValue visitFCOPYSIGN(SDNode *N); |
| 255 | SDValue visitSINT_TO_FP(SDNode *N); |
| 256 | SDValue visitUINT_TO_FP(SDNode *N); |
| 257 | SDValue visitFP_TO_SINT(SDNode *N); |
| 258 | SDValue visitFP_TO_UINT(SDNode *N); |
| 259 | SDValue visitFP_ROUND(SDNode *N); |
| 260 | SDValue visitFP_ROUND_INREG(SDNode *N); |
| 261 | SDValue visitFP_EXTEND(SDNode *N); |
| 262 | SDValue visitFNEG(SDNode *N); |
| 263 | SDValue visitFABS(SDNode *N); |
| Owen Anderson | a40319b | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 264 | SDValue visitFCEIL(SDNode *N); |
| 265 | SDValue visitFTRUNC(SDNode *N); |
| 266 | SDValue visitFFLOOR(SDNode *N); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 267 | SDValue visitBRCOND(SDNode *N); |
| 268 | SDValue visitBR_CC(SDNode *N); |
| 269 | SDValue visitLOAD(SDNode *N); |
| 270 | SDValue visitSTORE(SDNode *N); |
| 271 | SDValue visitINSERT_VECTOR_ELT(SDNode *N); |
| 272 | SDValue visitEXTRACT_VECTOR_ELT(SDNode *N); |
| 273 | SDValue visitBUILD_VECTOR(SDNode *N); |
| 274 | SDValue visitCONCAT_VECTORS(SDNode *N); |
| Bruno Cardoso Lopes | 6cb23f6 | 2011-09-20 23:19:33 +0000 | [diff] [blame] | 275 | SDValue visitEXTRACT_SUBVECTOR(SDNode *N); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 276 | SDValue visitVECTOR_SHUFFLE(SDNode *N); |
| Manman Ren | 413a6cb | 2014-01-31 01:10:35 +0000 | [diff] [blame] | 277 | SDValue visitINSERT_SUBVECTOR(SDNode *N); |
| Chris Lattner | e260ed8 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 278 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 279 | SDValue XformToShuffleWithZero(SDNode *N); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 280 | SDValue ReassociateOps(unsigned Opc, SDLoc DL, SDValue LHS, SDValue RHS); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 281 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 282 | SDValue visitShiftByConstant(SDNode *N, unsigned Amt); |
| Chris Lattner | 7c709a5 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 283 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 284 | bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS); |
| 285 | SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 286 | SDValue SimplifySelect(SDLoc DL, SDValue N0, SDValue N1, SDValue N2); |
| 287 | SDValue SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1, SDValue N2, |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 288 | SDValue N3, ISD::CondCode CC, |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 289 | bool NotExtCompare = false); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 290 | SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond, |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 291 | SDLoc DL, bool foldBooleans = true); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 292 | SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp, |
| Chris Lattner | 31e9edc | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 293 | unsigned HiOp); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 294 | SDValue CombineConsecutiveLoads(SDNode *N, EVT VT); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 295 | SDValue ConstantFoldBITCASTofBUILD_VECTOR(SDNode *, EVT); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 296 | SDValue BuildSDIV(SDNode *N); |
| 297 | SDValue BuildUDIV(SDNode *N); |
| Evan Cheng | 4c0bd96 | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 298 | SDValue MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1, |
| 299 | bool DemandHighBits = true); |
| 300 | SDValue MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1); |
| Richard Sandiford | 95c864d | 2014-01-08 15:40:47 +0000 | [diff] [blame] | 301 | SDNode *MatchRotatePosNeg(SDValue Shifted, SDValue Pos, SDValue Neg, |
| 302 | SDValue InnerPos, SDValue InnerNeg, |
| 303 | unsigned PosOpcode, unsigned NegOpcode, |
| 304 | SDLoc DL); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 305 | SDNode *MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 306 | SDValue ReduceLoadWidth(SDNode *N); |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 307 | SDValue ReduceLoadOpStoreWidth(SDNode *N); |
| Evan Cheng | d42641c | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 308 | SDValue TransformFPLoadStorePair(SDNode *N); |
| Michael Liao | 6d106b7 | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 309 | SDValue reduceBuildVecExtToExtBuildVec(SDNode *N); |
| Michael Liao | 5922979 | 2012-10-24 04:14:18 +0000 | [diff] [blame] | 310 | SDValue reduceBuildVecConvertToConvertBuildVec(SDNode *N); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 311 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 312 | SDValue GetDemandedBits(SDValue V, const APInt &Mask); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 313 | |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 314 | /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes, |
| 315 | /// looking for aliasing nodes and adding them to the Aliases vector. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 316 | void GatherAllAliases(SDNode *N, SDValue OriginalChain, |
| Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 317 | SmallVectorImpl<SDValue> &Aliases); |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 318 | |
| Jim Laskey | a15b0eb | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 319 | /// isAlias - Return true if there is any possibility that the two addresses |
| 320 | /// overlap. |
| Richard Sandiford | 981fdeb | 2013-10-28 12:00:00 +0000 | [diff] [blame] | 321 | bool isAlias(SDValue Ptr1, int64_t Size1, bool IsVolatile1, |
| Jim Laskey | a15b0eb | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 322 | const Value *SrcValue1, int SrcValueOffset1, |
| Nate Begeman | 879d8f1 | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 323 | unsigned SrcValueAlign1, |
| Dan Gohman | a94cc6d | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 324 | const MDNode *TBAAInfo1, |
| Richard Sandiford | 981fdeb | 2013-10-28 12:00:00 +0000 | [diff] [blame] | 325 | SDValue Ptr2, int64_t Size2, bool IsVolatile2, |
| Nate Begeman | 879d8f1 | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 326 | const Value *SrcValue2, int SrcValueOffset2, |
| Dan Gohman | a94cc6d | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 327 | unsigned SrcValueAlign2, |
| 328 | const MDNode *TBAAInfo2) const; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 329 | |
| Nadav Rotem | 307d767 | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 330 | /// isAlias - Return true if there is any possibility that the two addresses |
| 331 | /// overlap. |
| 332 | bool isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1); |
| 333 | |
| Jim Laskey | 08edf33 | 2006-10-11 13:47:09 +0000 | [diff] [blame] | 334 | /// FindAliasInfo - Extracts the relevant alias information from the memory |
| 335 | /// node. Returns true if the operand was a load. |
| 336 | bool FindAliasInfo(SDNode *N, |
| Richard Sandiford | 981fdeb | 2013-10-28 12:00:00 +0000 | [diff] [blame] | 337 | SDValue &Ptr, int64_t &Size, bool &IsVolatile, |
| Nate Begeman | 879d8f1 | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 338 | const Value *&SrcValue, int &SrcValueOffset, |
| Dan Gohman | a94cc6d | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 339 | unsigned &SrcValueAlignment, |
| 340 | const MDNode *&TBAAInfo) const; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 341 | |
| Jim Laskey | d07be23 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 342 | /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 343 | /// looking for a better chain (aliasing node.) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 344 | SDValue FindBetterChain(SDNode *N, SDValue Chain); |
| Duncan Sands | 4182603 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 345 | |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 346 | /// Merge consecutive store operations into a wide store. |
| 347 | /// This optimization uses wide integers or vectors when possible. |
| 348 | /// \return True if some memory operations were changed. |
| 349 | bool MergeConsecutiveStores(StoreSDNode *N); |
| 350 | |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 351 | public: |
| Bill Wendling | 026e5d7 | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 352 | DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL) |
| Quentin Colombet | de0e062 | 2013-10-11 18:29:42 +0000 | [diff] [blame] | 353 | : DAG(D), TLI(D.getTargetLoweringInfo()), Level(BeforeLegalizeTypes), |
| 354 | OptLevel(OL), LegalOperations(false), LegalTypes(false), AA(A) { |
| 355 | AttributeSet FnAttrs = |
| 356 | DAG.getMachineFunction().getFunction()->getAttributes(); |
| 357 | ForCodeSize = |
| 358 | FnAttrs.hasAttribute(AttributeSet::FunctionIndex, |
| 359 | Attribute::OptimizeForSize) || |
| 360 | FnAttrs.hasAttribute(AttributeSet::FunctionIndex, Attribute::MinSize); |
| 361 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 362 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 363 | /// Run - runs the dag combiner on all nodes in the work list |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 364 | void Run(CombineLevel AtLevel); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 365 | |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 366 | SelectionDAG &getDAG() const { return DAG; } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 367 | |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 368 | /// getShiftAmountTy - Returns a type large enough to hold any valid |
| 369 | /// shift amount - before type legalization these can be huge. |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 370 | EVT getShiftAmountTy(EVT LHSTy) { |
| Elena Demikhovsky | 6769c50 | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 371 | assert(LHSTy.isInteger() && "Shift amount is not an integer type!"); |
| 372 | if (LHSTy.isVector()) |
| 373 | return LHSTy; |
| Jack Carter | d4e9615 | 2013-10-17 01:34:33 +0000 | [diff] [blame] | 374 | return LegalTypes ? TLI.getScalarShiftAmountTy(LHSTy) |
| 375 | : TLI.getPointerTy(); |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 376 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 377 | |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 378 | /// isTypeLegal - This method returns true if we are running before type |
| 379 | /// legalization or if the specified VT is legal. |
| 380 | bool isTypeLegal(const EVT &VT) { |
| 381 | if (!LegalTypes) return true; |
| 382 | return TLI.isTypeLegal(VT); |
| 383 | } |
| Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 384 | |
| 385 | /// getSetCCResultType - Convenience wrapper around |
| 386 | /// TargetLowering::getSetCCResultType |
| 387 | EVT getSetCCResultType(EVT VT) const { |
| 388 | return TLI.getSetCCResultType(*DAG.getContext(), VT); |
| 389 | } |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 390 | }; |
| 391 | } |
| 392 | |
| Chris Lattner | b2b9d6f | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 393 | |
| 394 | namespace { |
| 395 | /// WorkListRemover - This class is a DAGUpdateListener that removes any deleted |
| 396 | /// nodes from the worklist. |
| Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 397 | class WorkListRemover : public SelectionDAG::DAGUpdateListener { |
| Chris Lattner | b2b9d6f | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 398 | DAGCombiner &DC; |
| 399 | public: |
| Jakob Stoklund Olesen | beb9469 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 400 | explicit WorkListRemover(DAGCombiner &dc) |
| 401 | : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {} |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 402 | |
| Duncan Sands | bf17080 | 2008-06-11 11:42:12 +0000 | [diff] [blame] | 403 | virtual void NodeDeleted(SDNode *N, SDNode *E) { |
| Chris Lattner | b2b9d6f | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 404 | DC.removeFromWorkList(N); |
| 405 | } |
| Chris Lattner | b2b9d6f | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 406 | }; |
| 407 | } |
| 408 | |
| Chris Lattner | bc1c85b | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 409 | //===----------------------------------------------------------------------===// |
| 410 | // TargetLowering::DAGCombinerInfo implementation |
| 411 | //===----------------------------------------------------------------------===// |
| 412 | |
| 413 | void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) { |
| 414 | ((DAGCombiner*)DC)->AddToWorkList(N); |
| 415 | } |
| 416 | |
| Cameron Zwarich | 8c7bbc0 | 2011-04-02 02:40:26 +0000 | [diff] [blame] | 417 | void TargetLowering::DAGCombinerInfo::RemoveFromWorklist(SDNode *N) { |
| 418 | ((DAGCombiner*)DC)->removeFromWorkList(N); |
| 419 | } |
| 420 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 421 | SDValue TargetLowering::DAGCombinerInfo:: |
| Evan Cheng | fd81c73 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 422 | CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) { |
| 423 | return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo); |
| Chris Lattner | bc1c85b | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 426 | SDValue TargetLowering::DAGCombinerInfo:: |
| Evan Cheng | fd81c73 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 427 | CombineTo(SDNode *N, SDValue Res, bool AddTo) { |
| 428 | return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo); |
| Chris Lattner | bc1c85b | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 432 | SDValue TargetLowering::DAGCombinerInfo:: |
| Evan Cheng | fd81c73 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 433 | CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) { |
| 434 | return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo); |
| Chris Lattner | bc1c85b | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| Dan Gohman | e58ab79 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 437 | void TargetLowering::DAGCombinerInfo:: |
| 438 | CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) { |
| 439 | return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO); |
| 440 | } |
| Chris Lattner | bc1c85b | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 441 | |
| Chris Lattner | bc1c85b | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 442 | //===----------------------------------------------------------------------===// |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 443 | // Helper Functions |
| 444 | //===----------------------------------------------------------------------===// |
| 445 | |
| 446 | /// isNegatibleForFree - Return 1 if we can compute the negated form of the |
| 447 | /// specified expression for the same cost as the expression itself, or 2 if we |
| 448 | /// can compute the negated form more cheaply than the expression itself. |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 449 | static char isNegatibleForFree(SDValue Op, bool LegalOperations, |
| Owen Anderson | 2ee7c4d | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 450 | const TargetLowering &TLI, |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 451 | const TargetOptions *Options, |
| Chris Lattner | e7c1401 | 2008-02-26 07:04:54 +0000 | [diff] [blame] | 452 | unsigned Depth = 0) { |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 453 | // fneg is removable even if it has multiple uses. |
| 454 | if (Op.getOpcode() == ISD::FNEG) return 2; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 455 | |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 456 | // Don't allow anything with multiple uses. |
| 457 | if (!Op.hasOneUse()) return 0; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 458 | |
| Chris Lattner | 4698083 | 2007-05-25 02:19:06 +0000 | [diff] [blame] | 459 | // Don't recurse exponentially. |
| 460 | if (Depth > 6) return 0; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 461 | |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 462 | switch (Op.getOpcode()) { |
| 463 | default: return false; |
| 464 | case ISD::ConstantFP: |
| Chris Lattner | e7c1401 | 2008-02-26 07:04:54 +0000 | [diff] [blame] | 465 | // Don't invert constant FP values after legalize. The negated constant |
| 466 | // isn't necessarily legal. |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 467 | return LegalOperations ? 0 : 1; |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 468 | case ISD::FADD: |
| 469 | // FIXME: determine better conditions for this xform. |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 470 | if (!Options->UnsafeFPMath) return 0; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 471 | |
| Owen Anderson | 2ee7c4d | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 472 | // After operation legalization, it might not be legal to create new FSUBs. |
| 473 | if (LegalOperations && |
| 474 | !TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType())) |
| 475 | return 0; |
| 476 | |
| Craig Topper | 03f3977 | 2012-09-09 22:58:45 +0000 | [diff] [blame] | 477 | // fold (fneg (fadd A, B)) -> (fsub (fneg A), B) |
| Owen Anderson | 2ee7c4d | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 478 | if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, |
| 479 | Options, Depth + 1)) |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 480 | return V; |
| Bill Wendling | 6fbf549 | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 481 | // fold (fneg (fadd A, B)) -> (fsub (fneg B), A) |
| Owen Anderson | 2ee7c4d | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 482 | return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options, |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 483 | Depth + 1); |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 484 | case ISD::FSUB: |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 485 | // We can't turn -(A-B) into B-A when we honor signed zeros. |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 486 | if (!Options->UnsafeFPMath) return 0; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 487 | |
| Bill Wendling | 6fbf549 | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 488 | // fold (fneg (fsub A, B)) -> (fsub B, A) |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 489 | return 1; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 490 | |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 491 | case ISD::FMUL: |
| 492 | case ISD::FDIV: |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 493 | if (Options->HonorSignDependentRoundingFPMath()) return 0; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 494 | |
| Bill Wendling | 6fbf549 | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 495 | // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y)) |
| Owen Anderson | 2ee7c4d | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 496 | if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, |
| 497 | Options, Depth + 1)) |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 498 | return V; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 499 | |
| Owen Anderson | 2ee7c4d | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 500 | return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options, |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 501 | Depth + 1); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 502 | |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 503 | case ISD::FP_EXTEND: |
| 504 | case ISD::FP_ROUND: |
| 505 | case ISD::FSIN: |
| Owen Anderson | 2ee7c4d | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 506 | return isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, Options, |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 507 | Depth + 1); |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
| 511 | /// GetNegatedExpression - If isNegatibleForFree returns true, this function |
| 512 | /// returns the newly negated expression. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 513 | static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG, |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 514 | bool LegalOperations, unsigned Depth = 0) { |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 515 | // fneg is removable even if it has multiple uses. |
| 516 | if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 517 | |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 518 | // Don't allow anything with multiple uses. |
| 519 | assert(Op.hasOneUse() && "Unknown reuse!"); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 520 | |
| Chris Lattner | 4698083 | 2007-05-25 02:19:06 +0000 | [diff] [blame] | 521 | assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree"); |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 522 | switch (Op.getOpcode()) { |
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 523 | default: llvm_unreachable("Unknown code"); |
| Dale Johannesen | 446b900 | 2007-08-31 23:34:27 +0000 | [diff] [blame] | 524 | case ISD::ConstantFP: { |
| 525 | APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF(); |
| 526 | V.changeSign(); |
| 527 | return DAG.getConstantFP(V, Op.getValueType()); |
| 528 | } |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 529 | case ISD::FADD: |
| 530 | // FIXME: determine better conditions for this xform. |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 531 | assert(DAG.getTarget().Options.UnsafeFPMath); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 532 | |
| Bill Wendling | 6fbf549 | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 533 | // fold (fneg (fadd A, B)) -> (fsub (fneg A), B) |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 534 | if (isNegatibleForFree(Op.getOperand(0), LegalOperations, |
| Owen Anderson | 2ee7c4d | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 535 | DAG.getTargetLoweringInfo(), |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 536 | &DAG.getTarget().Options, Depth+1)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 537 | return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(), |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 538 | GetNegatedExpression(Op.getOperand(0), DAG, |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 539 | LegalOperations, Depth+1), |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 540 | Op.getOperand(1)); |
| Bill Wendling | 6fbf549 | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 541 | // fold (fneg (fadd A, B)) -> (fsub (fneg B), A) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 542 | return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(), |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 543 | GetNegatedExpression(Op.getOperand(1), DAG, |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 544 | LegalOperations, Depth+1), |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 545 | Op.getOperand(0)); |
| 546 | case ISD::FSUB: |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 547 | // We can't turn -(A-B) into B-A when we honor signed zeros. |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 548 | assert(DAG.getTarget().Options.UnsafeFPMath); |
| Dan Gohman | 9a70823 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 549 | |
| Bill Wendling | 6fbf549 | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 550 | // fold (fneg (fsub 0, B)) -> B |
| Dan Gohman | 9a70823 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 551 | if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0))) |
| Dale Johannesen | 446b900 | 2007-08-31 23:34:27 +0000 | [diff] [blame] | 552 | if (N0CFP->getValueAPF().isZero()) |
| Dan Gohman | 9a70823 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 553 | return Op.getOperand(1); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 554 | |
| Bill Wendling | 6fbf549 | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 555 | // fold (fneg (fsub A, B)) -> (fsub B, A) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 556 | return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(), |
| Bill Wendling | f6d0aff | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 557 | Op.getOperand(1), Op.getOperand(0)); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 558 | |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 559 | case ISD::FMUL: |
| 560 | case ISD::FDIV: |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 561 | assert(!DAG.getTarget().Options.HonorSignDependentRoundingFPMath()); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 562 | |
| Bill Wendling | 6fbf549 | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 563 | // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 564 | if (isNegatibleForFree(Op.getOperand(0), LegalOperations, |
| Owen Anderson | 2ee7c4d | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 565 | DAG.getTargetLoweringInfo(), |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 566 | &DAG.getTarget().Options, Depth+1)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 567 | return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(), |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 568 | GetNegatedExpression(Op.getOperand(0), DAG, |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 569 | LegalOperations, Depth+1), |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 570 | Op.getOperand(1)); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 571 | |
| Bill Wendling | 6fbf549 | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 572 | // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 573 | return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(), |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 574 | Op.getOperand(0), |
| Chris Lattner | e7c1401 | 2008-02-26 07:04:54 +0000 | [diff] [blame] | 575 | GetNegatedExpression(Op.getOperand(1), DAG, |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 576 | LegalOperations, Depth+1)); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 577 | |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 578 | case ISD::FP_EXTEND: |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 579 | case ISD::FSIN: |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 580 | return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(), |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 581 | GetNegatedExpression(Op.getOperand(0), DAG, |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 582 | LegalOperations, Depth+1)); |
| Chris Lattner | 72733e5 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 583 | case ISD::FP_ROUND: |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 584 | return DAG.getNode(ISD::FP_ROUND, SDLoc(Op), Op.getValueType(), |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 585 | GetNegatedExpression(Op.getOperand(0), DAG, |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 586 | LegalOperations, Depth+1), |
| Chris Lattner | 72733e5 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 587 | Op.getOperand(1)); |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 588 | } |
| 589 | } |
| Chris Lattner | bc1c85b | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 590 | |
| 591 | |
| Nate Begeman | 2504fe2 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 592 | // isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc |
| 593 | // that selects between the values 1 and 0, making it equivalent to a setcc. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 594 | // Also, set the incoming LHS, RHS, and CC references to the appropriate |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 595 | // nodes based on the type of node we are checking. This simplifies life a |
| 596 | // bit for the callers. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 597 | static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS, |
| 598 | SDValue &CC) { |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 599 | if (N.getOpcode() == ISD::SETCC) { |
| 600 | LHS = N.getOperand(0); |
| 601 | RHS = N.getOperand(1); |
| 602 | CC = N.getOperand(2); |
| Nate Begeman | 2504fe2 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 603 | return true; |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 604 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 605 | if (N.getOpcode() == ISD::SELECT_CC && |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 606 | N.getOperand(2).getOpcode() == ISD::Constant && |
| 607 | N.getOperand(3).getOpcode() == ISD::Constant && |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 608 | cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 && |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 609 | cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) { |
| 610 | LHS = N.getOperand(0); |
| 611 | RHS = N.getOperand(1); |
| 612 | CC = N.getOperand(4); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 613 | return true; |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 614 | } |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 615 | return false; |
| 616 | } |
| 617 | |
| Nate Begeman | 2cc2c9a | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 618 | // isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only |
| 619 | // one use. If this is true, it allows the users to invert the operation for |
| 620 | // free when it is profitable to do so. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 621 | static bool isOneUseSetCC(SDValue N) { |
| 622 | SDValue N0, N1, N2; |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 623 | if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse()) |
| Nate Begeman | 2504fe2 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 624 | return true; |
| 625 | return false; |
| 626 | } |
| 627 | |
| Juergen Ributzka | 6840282 | 2014-01-13 21:49:25 +0000 | [diff] [blame] | 628 | // \brief Returns the SDNode if it is a constant BuildVector or constant int. |
| 629 | static SDNode *isConstantBuildVectorOrConstantInt(SDValue N) { |
| 630 | if (isa<ConstantSDNode>(N)) |
| 631 | return N.getNode(); |
| 632 | BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N); |
| 633 | if(BV && BV->isConstant()) |
| 634 | return BV; |
| 635 | return NULL; |
| 636 | } |
| 637 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 638 | SDValue DAGCombiner::ReassociateOps(unsigned Opc, SDLoc DL, |
| Bill Wendling | f6d0aff | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 639 | SDValue N0, SDValue N1) { |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 640 | EVT VT = N0.getValueType(); |
| Juergen Ributzka | 6840282 | 2014-01-13 21:49:25 +0000 | [diff] [blame] | 641 | if (N0.getOpcode() == Opc) { |
| 642 | if (SDNode *L = isConstantBuildVectorOrConstantInt(N0.getOperand(1))) { |
| 643 | if (SDNode *R = isConstantBuildVectorOrConstantInt(N1)) { |
| 644 | // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2)) |
| 645 | SDValue OpNode = DAG.FoldConstantArithmetic(Opc, VT, L, R); |
| 646 | if (!OpNode.getNode()) |
| 647 | return SDValue(); |
| 648 | return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode); |
| Juergen Ributzka | 7384405 | 2014-01-13 20:51:35 +0000 | [diff] [blame] | 649 | } |
| Juergen Ributzka | 6840282 | 2014-01-13 21:49:25 +0000 | [diff] [blame] | 650 | if (N0.hasOneUse()) { |
| 651 | // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one |
| 652 | // use |
| 653 | SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT, N0.getOperand(0), N1); |
| 654 | if (!OpNode.getNode()) |
| 655 | return SDValue(); |
| 656 | AddToWorkList(OpNode.getNode()); |
| 657 | return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1)); |
| Juergen Ributzka | 7384405 | 2014-01-13 20:51:35 +0000 | [diff] [blame] | 658 | } |
| 659 | } |
| Nate Begeman | 22e251a | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 660 | } |
| Bill Wendling | f6d0aff | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 661 | |
| Juergen Ributzka | 6840282 | 2014-01-13 21:49:25 +0000 | [diff] [blame] | 662 | if (N1.getOpcode() == Opc) { |
| 663 | if (SDNode *R = isConstantBuildVectorOrConstantInt(N1.getOperand(1))) { |
| 664 | if (SDNode *L = isConstantBuildVectorOrConstantInt(N0)) { |
| 665 | // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2)) |
| 666 | SDValue OpNode = DAG.FoldConstantArithmetic(Opc, VT, R, L); |
| 667 | if (!OpNode.getNode()) |
| 668 | return SDValue(); |
| 669 | return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode); |
| 670 | } |
| 671 | if (N1.hasOneUse()) { |
| 672 | // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one |
| 673 | // use |
| 674 | SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT, N1.getOperand(0), N0); |
| 675 | if (!OpNode.getNode()) |
| 676 | return SDValue(); |
| 677 | AddToWorkList(OpNode.getNode()); |
| 678 | return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1)); |
| 679 | } |
| Nate Begeman | 22e251a | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 680 | } |
| 681 | } |
| Bill Wendling | f6d0aff | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 682 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 683 | return SDValue(); |
| Nate Begeman | 22e251a | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 686 | SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo, |
| 687 | bool AddTo) { |
| Chris Lattner | b2b9d6f | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 688 | assert(N->getNumValues() == NumTo && "Broken CombineTo call!"); |
| 689 | ++NodesCombined; |
| David Greene | fe5c352 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 690 | DEBUG(dbgs() << "\nReplacing.1 "; |
| Chris Lattner | 4dc3edd | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 691 | N->dump(&DAG); |
| David Greene | fe5c352 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 692 | dbgs() << "\nWith: "; |
| Chris Lattner | 4dc3edd | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 693 | To[0].getNode()->dump(&DAG); |
| David Greene | fe5c352 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 694 | dbgs() << " and " << NumTo-1 << " other values\n"; |
| Chris Lattner | 4dc3edd | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 695 | for (unsigned i = 0, e = NumTo; i != e; ++i) |
| Jakob Stoklund Olesen | 32042f9 | 2009-12-03 05:15:35 +0000 | [diff] [blame] | 696 | assert((!To[i].getNode() || |
| 697 | N->getValueType(i) == To[i].getValueType()) && |
| Dan Gohman | 7e6b932 | 2009-01-21 15:17:51 +0000 | [diff] [blame] | 698 | "Cannot combine value to value of different type!")); |
| Chris Lattner | b2b9d6f | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 699 | WorkListRemover DeadNodes(*this); |
| Jakob Stoklund Olesen | beb9469 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 700 | DAG.ReplaceAllUsesWith(N, To); |
| Chris Lattner | b2b9d6f | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 701 | if (AddTo) { |
| 702 | // Push the new nodes and any users onto the worklist |
| 703 | for (unsigned i = 0, e = NumTo; i != e; ++i) { |
| Chris Lattner | 4147f08 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 704 | if (To[i].getNode()) { |
| 705 | AddToWorkList(To[i].getNode()); |
| 706 | AddUsersToWorkList(To[i].getNode()); |
| 707 | } |
| Chris Lattner | b2b9d6f | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 708 | } |
| 709 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 710 | |
| Dan Gohman | cd0b1bf | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 711 | // Finally, if the node is now dead, remove it from the graph. The node |
| 712 | // may not be dead if the replacement process recursively simplified to |
| 713 | // something else needing this node. |
| 714 | if (N->use_empty()) { |
| 715 | // Nodes can be reintroduced into the worklist. Make sure we do not |
| 716 | // process a node that has been replaced. |
| 717 | removeFromWorkList(N); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 718 | |
| Dan Gohman | cd0b1bf | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 719 | // Finally, since the node is now dead, remove it from the graph. |
| 720 | DAG.DeleteNode(N); |
| 721 | } |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 722 | return SDValue(N, 0); |
| Chris Lattner | b2b9d6f | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 725 | void DAGCombiner:: |
| 726 | CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) { |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 727 | // Replace all uses. If any nodes become isomorphic to other nodes and |
| Chris Lattner | b2b9d6f | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 728 | // are deleted, make sure to remove them from our worklist. |
| 729 | WorkListRemover DeadNodes(*this); |
| Jakob Stoklund Olesen | beb9469 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 730 | DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New); |
| Dan Gohman | e58ab79 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 731 | |
| Chris Lattner | b2b9d6f | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 732 | // Push the new node and any (possibly new) users onto the worklist. |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 733 | AddToWorkList(TLO.New.getNode()); |
| 734 | AddUsersToWorkList(TLO.New.getNode()); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 735 | |
| Chris Lattner | b2b9d6f | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 736 | // Finally, if the node is now dead, remove it from the graph. The node |
| 737 | // may not be dead if the replacement process recursively simplified to |
| 738 | // something else needing this node. |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 739 | if (TLO.Old.getNode()->use_empty()) { |
| 740 | removeFromWorkList(TLO.Old.getNode()); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 741 | |
| Chris Lattner | b2b9d6f | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 742 | // If the operands of this node are only used by the node, they will now |
| 743 | // be dead. Make sure to visit them first to delete dead nodes early. |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 744 | for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i) |
| 745 | if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse()) |
| 746 | AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode()); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 747 | |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 748 | DAG.DeleteNode(TLO.Old.getNode()); |
| Chris Lattner | b2b9d6f | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 749 | } |
| Dan Gohman | e58ab79 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | /// SimplifyDemandedBits - Check the specified integer node value to see if |
| 753 | /// it can be simplified or if things it uses can be simplified by bit |
| 754 | /// propagation. If so, return true. |
| 755 | bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) { |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 756 | TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations); |
| Dan Gohman | e58ab79 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 757 | APInt KnownZero, KnownOne; |
| 758 | if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO)) |
| 759 | return false; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 760 | |
| Dan Gohman | e58ab79 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 761 | // Revisit the node. |
| 762 | AddToWorkList(Op.getNode()); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 763 | |
| Dan Gohman | e58ab79 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 764 | // Replace the old value with the new one. |
| 765 | ++NodesCombined; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 766 | DEBUG(dbgs() << "\nReplacing.2 "; |
| Chris Lattner | 4dc3edd | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 767 | TLO.Old.getNode()->dump(&DAG); |
| David Greene | fe5c352 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 768 | dbgs() << "\nWith: "; |
| Chris Lattner | 4dc3edd | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 769 | TLO.New.getNode()->dump(&DAG); |
| David Greene | fe5c352 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 770 | dbgs() << '\n'); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 771 | |
| Dan Gohman | e58ab79 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 772 | CommitTargetLoweringOpt(TLO); |
| Chris Lattner | b2b9d6f | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 773 | return true; |
| 774 | } |
| 775 | |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 776 | void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 777 | SDLoc dl(Load); |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 778 | EVT VT = Load->getValueType(0); |
| 779 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0)); |
| Evan Cheng | e19aa5c | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 780 | |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 781 | DEBUG(dbgs() << "\nReplacing.9 "; |
| 782 | Load->dump(&DAG); |
| 783 | dbgs() << "\nWith: "; |
| 784 | Trunc.getNode()->dump(&DAG); |
| 785 | dbgs() << '\n'); |
| 786 | WorkListRemover DeadNodes(*this); |
| Jakob Stoklund Olesen | beb9469 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 787 | DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc); |
| 788 | DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1)); |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 789 | removeFromWorkList(Load); |
| 790 | DAG.DeleteNode(Load); |
| Evan Cheng | e813690 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 791 | AddToWorkList(Trunc.getNode()); |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) { |
| 795 | Replace = false; |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 796 | SDLoc dl(Op); |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 797 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) { |
| Evan Cheng | e813690 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 798 | EVT MemVT = LD->getMemoryVT(); |
| 799 | ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD) |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 800 | ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD |
| Eric Christopher | d9e8eac | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 801 | : ISD::EXTLOAD) |
| Evan Cheng | e813690 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 802 | : LD->getExtensionType(); |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 803 | Replace = true; |
| Stuart Hastings | 81c4306 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 804 | return DAG.getExtLoad(ExtType, dl, PVT, |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 805 | LD->getChain(), LD->getBasePtr(), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 806 | MemVT, LD->getMemOperand()); |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| Evan Cheng | e19aa5c | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 809 | unsigned Opc = Op.getOpcode(); |
| Evan Cheng | b9ff130 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 810 | switch (Opc) { |
| 811 | default: break; |
| 812 | case ISD::AssertSext: |
| Evan Cheng | e19aa5c | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 813 | return DAG.getNode(ISD::AssertSext, dl, PVT, |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 814 | SExtPromoteOperand(Op.getOperand(0), PVT), |
| Evan Cheng | e19aa5c | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 815 | Op.getOperand(1)); |
| Evan Cheng | b9ff130 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 816 | case ISD::AssertZext: |
| Evan Cheng | e19aa5c | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 817 | return DAG.getNode(ISD::AssertZext, dl, PVT, |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 818 | ZExtPromoteOperand(Op.getOperand(0), PVT), |
| Evan Cheng | e19aa5c | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 819 | Op.getOperand(1)); |
| Evan Cheng | b9ff130 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 820 | case ISD::Constant: { |
| 821 | unsigned ExtOpc = |
| Evan Cheng | e19aa5c | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 822 | Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; |
| Evan Cheng | b9ff130 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 823 | return DAG.getNode(ExtOpc, dl, PVT, Op); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 824 | } |
| Evan Cheng | b9ff130 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT)) |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 828 | return SDValue(); |
| Evan Cheng | b9ff130 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 829 | return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op); |
| Evan Cheng | af56fac | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 832 | SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) { |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 833 | if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT)) |
| 834 | return SDValue(); |
| 835 | EVT OldVT = Op.getValueType(); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 836 | SDLoc dl(Op); |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 837 | bool Replace = false; |
| 838 | SDValue NewOp = PromoteOperand(Op, PVT, Replace); |
| 839 | if (NewOp.getNode() == 0) |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 840 | return SDValue(); |
| Evan Cheng | e813690 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 841 | AddToWorkList(NewOp.getNode()); |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 842 | |
| 843 | if (Replace) |
| 844 | ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode()); |
| 845 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp, |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 846 | DAG.getValueType(OldVT)); |
| 847 | } |
| 848 | |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 849 | SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) { |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 850 | EVT OldVT = Op.getValueType(); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 851 | SDLoc dl(Op); |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 852 | bool Replace = false; |
| 853 | SDValue NewOp = PromoteOperand(Op, PVT, Replace); |
| 854 | if (NewOp.getNode() == 0) |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 855 | return SDValue(); |
| Evan Cheng | e813690 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 856 | AddToWorkList(NewOp.getNode()); |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 857 | |
| 858 | if (Replace) |
| 859 | ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode()); |
| 860 | return DAG.getZeroExtendInReg(NewOp, dl, OldVT); |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 861 | } |
| 862 | |
| Evan Cheng | af56fac | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 863 | /// PromoteIntBinOp - Promote the specified integer binary operation if the |
| 864 | /// target indicates it is beneficial. e.g. On x86, it's usually better to |
| 865 | /// promote i16 operations to i32 since i16 instructions are longer. |
| 866 | SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) { |
| 867 | if (!LegalOperations) |
| 868 | return SDValue(); |
| 869 | |
| 870 | EVT VT = Op.getValueType(); |
| 871 | if (VT.isVector() || !VT.isInteger()) |
| 872 | return SDValue(); |
| 873 | |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 874 | // If operation type is 'undesirable', e.g. i16 on x86, consider |
| 875 | // promoting it. |
| 876 | unsigned Opc = Op.getOpcode(); |
| 877 | if (TLI.isTypeDesirableForOp(Opc, VT)) |
| 878 | return SDValue(); |
| 879 | |
| Evan Cheng | af56fac | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 880 | EVT PVT = VT; |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 881 | // Consult target whether it is a good idea to promote this operation and |
| 882 | // what's the right type to promote it to. |
| 883 | if (TLI.IsDesirableToPromoteOp(Op, PVT)) { |
| Evan Cheng | af56fac | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 884 | assert(PVT != VT && "Don't know what type to promote to!"); |
| 885 | |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 886 | bool Replace0 = false; |
| 887 | SDValue N0 = Op.getOperand(0); |
| 888 | SDValue NN0 = PromoteOperand(N0, PVT, Replace0); |
| 889 | if (NN0.getNode() == 0) |
| Evan Cheng | f1223bd | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 890 | return SDValue(); |
| 891 | |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 892 | bool Replace1 = false; |
| 893 | SDValue N1 = Op.getOperand(1); |
| Evan Cheng | 02947a4 | 2010-05-10 19:03:57 +0000 | [diff] [blame] | 894 | SDValue NN1; |
| 895 | if (N0 == N1) |
| 896 | NN1 = NN0; |
| 897 | else { |
| 898 | NN1 = PromoteOperand(N1, PVT, Replace1); |
| 899 | if (NN1.getNode() == 0) |
| 900 | return SDValue(); |
| 901 | } |
| Evan Cheng | f1223bd | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 902 | |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 903 | AddToWorkList(NN0.getNode()); |
| Evan Cheng | 02947a4 | 2010-05-10 19:03:57 +0000 | [diff] [blame] | 904 | if (NN1.getNode()) |
| 905 | AddToWorkList(NN1.getNode()); |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 906 | |
| 907 | if (Replace0) |
| 908 | ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode()); |
| 909 | if (Replace1) |
| 910 | ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode()); |
| Evan Cheng | f1223bd | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 911 | |
| Evan Cheng | e813690 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 912 | DEBUG(dbgs() << "\nPromoting "; |
| 913 | Op.getNode()->dump(&DAG)); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 914 | SDLoc dl(Op); |
| Evan Cheng | f1223bd | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 915 | return DAG.getNode(ISD::TRUNCATE, dl, VT, |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 916 | DAG.getNode(Opc, dl, PVT, NN0, NN1)); |
| Evan Cheng | f1223bd | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 917 | } |
| 918 | return SDValue(); |
| 919 | } |
| 920 | |
| 921 | /// PromoteIntShiftOp - Promote the specified integer shift operation if the |
| 922 | /// target indicates it is beneficial. e.g. On x86, it's usually better to |
| 923 | /// promote i16 operations to i32 since i16 instructions are longer. |
| 924 | SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) { |
| 925 | if (!LegalOperations) |
| 926 | return SDValue(); |
| 927 | |
| 928 | EVT VT = Op.getValueType(); |
| 929 | if (VT.isVector() || !VT.isInteger()) |
| 930 | return SDValue(); |
| 931 | |
| 932 | // If operation type is 'undesirable', e.g. i16 on x86, consider |
| 933 | // promoting it. |
| 934 | unsigned Opc = Op.getOpcode(); |
| 935 | if (TLI.isTypeDesirableForOp(Opc, VT)) |
| 936 | return SDValue(); |
| 937 | |
| 938 | EVT PVT = VT; |
| 939 | // Consult target whether it is a good idea to promote this operation and |
| 940 | // what's the right type to promote it to. |
| 941 | if (TLI.IsDesirableToPromoteOp(Op, PVT)) { |
| 942 | assert(PVT != VT && "Don't know what type to promote to!"); |
| 943 | |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 944 | bool Replace = false; |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 945 | SDValue N0 = Op.getOperand(0); |
| 946 | if (Opc == ISD::SRA) |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 947 | N0 = SExtPromoteOperand(Op.getOperand(0), PVT); |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 948 | else if (Opc == ISD::SRL) |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 949 | N0 = ZExtPromoteOperand(Op.getOperand(0), PVT); |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 950 | else |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 951 | N0 = PromoteOperand(N0, PVT, Replace); |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 952 | if (N0.getNode() == 0) |
| 953 | return SDValue(); |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 954 | |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 955 | AddToWorkList(N0.getNode()); |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 956 | if (Replace) |
| 957 | ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode()); |
| Evan Cheng | af56fac | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 958 | |
| Evan Cheng | e813690 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 959 | DEBUG(dbgs() << "\nPromoting "; |
| 960 | Op.getNode()->dump(&DAG)); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 961 | SDLoc dl(Op); |
| Evan Cheng | af56fac | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 962 | return DAG.getNode(ISD::TRUNCATE, dl, VT, |
| Evan Cheng | f1223bd | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 963 | DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1))); |
| Evan Cheng | af56fac | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 964 | } |
| 965 | return SDValue(); |
| 966 | } |
| 967 | |
| Evan Cheng | e19aa5c | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 968 | SDValue DAGCombiner::PromoteExtend(SDValue Op) { |
| 969 | if (!LegalOperations) |
| 970 | return SDValue(); |
| 971 | |
| 972 | EVT VT = Op.getValueType(); |
| 973 | if (VT.isVector() || !VT.isInteger()) |
| 974 | return SDValue(); |
| 975 | |
| 976 | // If operation type is 'undesirable', e.g. i16 on x86, consider |
| 977 | // promoting it. |
| 978 | unsigned Opc = Op.getOpcode(); |
| 979 | if (TLI.isTypeDesirableForOp(Opc, VT)) |
| 980 | return SDValue(); |
| 981 | |
| 982 | EVT PVT = VT; |
| 983 | // Consult target whether it is a good idea to promote this operation and |
| 984 | // what's the right type to promote it to. |
| 985 | if (TLI.IsDesirableToPromoteOp(Op, PVT)) { |
| 986 | assert(PVT != VT && "Don't know what type to promote to!"); |
| 987 | // fold (aext (aext x)) -> (aext x) |
| 988 | // fold (aext (zext x)) -> (zext x) |
| 989 | // fold (aext (sext x)) -> (sext x) |
| Evan Cheng | e813690 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 990 | DEBUG(dbgs() << "\nPromoting "; |
| 991 | Op.getNode()->dump(&DAG)); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 992 | return DAG.getNode(Op.getOpcode(), SDLoc(Op), VT, Op.getOperand(0)); |
| Evan Cheng | e19aa5c | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 993 | } |
| 994 | return SDValue(); |
| 995 | } |
| 996 | |
| 997 | bool DAGCombiner::PromoteLoad(SDValue Op) { |
| 998 | if (!LegalOperations) |
| 999 | return false; |
| 1000 | |
| 1001 | EVT VT = Op.getValueType(); |
| 1002 | if (VT.isVector() || !VT.isInteger()) |
| 1003 | return false; |
| 1004 | |
| 1005 | // If operation type is 'undesirable', e.g. i16 on x86, consider |
| 1006 | // promoting it. |
| 1007 | unsigned Opc = Op.getOpcode(); |
| 1008 | if (TLI.isTypeDesirableForOp(Opc, VT)) |
| 1009 | return false; |
| 1010 | |
| 1011 | EVT PVT = VT; |
| 1012 | // Consult target whether it is a good idea to promote this operation and |
| 1013 | // what's the right type to promote it to. |
| 1014 | if (TLI.IsDesirableToPromoteOp(Op, PVT)) { |
| 1015 | assert(PVT != VT && "Don't know what type to promote to!"); |
| 1016 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1017 | SDLoc dl(Op); |
| Evan Cheng | e19aa5c | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 1018 | SDNode *N = Op.getNode(); |
| 1019 | LoadSDNode *LD = cast<LoadSDNode>(N); |
| Evan Cheng | e813690 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 1020 | EVT MemVT = LD->getMemoryVT(); |
| 1021 | ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD) |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1022 | ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD |
| Eric Christopher | d9e8eac | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 1023 | : ISD::EXTLOAD) |
| Evan Cheng | e813690 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 1024 | : LD->getExtensionType(); |
| Stuart Hastings | 81c4306 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 1025 | SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT, |
| Evan Cheng | e19aa5c | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 1026 | LD->getChain(), LD->getBasePtr(), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 1027 | MemVT, LD->getMemOperand()); |
| Evan Cheng | e19aa5c | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 1028 | SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD); |
| 1029 | |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 1030 | DEBUG(dbgs() << "\nPromoting "; |
| Evan Cheng | e19aa5c | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 1031 | N->dump(&DAG); |
| Evan Cheng | 0abb54d | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 1032 | dbgs() << "\nTo: "; |
| Evan Cheng | e19aa5c | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 1033 | Result.getNode()->dump(&DAG); |
| 1034 | dbgs() << '\n'); |
| 1035 | WorkListRemover DeadNodes(*this); |
| Jakob Stoklund Olesen | beb9469 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 1036 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result); |
| 1037 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1)); |
| Evan Cheng | e19aa5c | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 1038 | removeFromWorkList(N); |
| 1039 | DAG.DeleteNode(N); |
| Evan Cheng | e813690 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 1040 | AddToWorkList(Result.getNode()); |
| Evan Cheng | e19aa5c | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 1041 | return true; |
| 1042 | } |
| 1043 | return false; |
| 1044 | } |
| 1045 | |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 1046 | |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 1047 | //===----------------------------------------------------------------------===// |
| 1048 | // Main DAG Combiner implementation |
| 1049 | //===----------------------------------------------------------------------===// |
| 1050 | |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 1051 | void DAGCombiner::Run(CombineLevel AtLevel) { |
| 1052 | // set the instance variables, so that the various visit routines may use it. |
| 1053 | Level = AtLevel; |
| Eli Friedman | 9d448e4 | 2011-11-12 00:35:34 +0000 | [diff] [blame] | 1054 | LegalOperations = Level >= AfterLegalizeVectorOps; |
| 1055 | LegalTypes = Level >= AfterLegalizeTypes; |
| Nate Begeman | 2504fe2 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 1056 | |
| Evan Cheng | 5e7658c | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1057 | // Add all the dag nodes to the worklist. |
| Evan Cheng | 5e7658c | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1058 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
| 1059 | E = DAG.allnodes_end(); I != E; ++I) |
| James Molloy | 67b6b11 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 1060 | AddToWorkList(I); |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 1061 | |
| Evan Cheng | 5e7658c | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1062 | // Create a dummy node (which is not added to allnodes), that adds a reference |
| 1063 | // to the root node, preventing it from being deleted, and tracking any |
| 1064 | // changes of the root. |
| 1065 | HandleSDNode Dummy(DAG.getRoot()); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1066 | |
| Jim Laskey | e7d2c24 | 2006-10-17 19:33:52 +0000 | [diff] [blame] | 1067 | // The root of the dag may dangle to deleted nodes until the dag combiner is |
| 1068 | // done. Set it to null to avoid confusion. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1069 | DAG.setRoot(SDValue()); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1070 | |
| James Molloy | 67b6b11 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 1071 | // while the worklist isn't empty, find a node and |
| Evan Cheng | 5e7658c | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1072 | // try and combine it. |
| James Molloy | 67b6b11 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 1073 | while (!WorkListContents.empty()) { |
| 1074 | SDNode *N; |
| Jack Carter | d4e9615 | 2013-10-17 01:34:33 +0000 | [diff] [blame] | 1075 | // The WorkListOrder holds the SDNodes in order, but it may contain |
| 1076 | // duplicates. |
| James Molloy | 67b6b11 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 1077 | // In order to avoid a linear scan, we use a set (O(log N)) to hold what the |
| 1078 | // worklist *should* contain, and check the node we want to visit is should |
| 1079 | // actually be visited. |
| 1080 | do { |
| Benjamin Kramer | e1e549d | 2012-03-10 00:23:58 +0000 | [diff] [blame] | 1081 | N = WorkListOrder.pop_back_val(); |
| James Molloy | 67b6b11 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 1082 | } while (!WorkListContents.erase(N)); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1083 | |
| Evan Cheng | 5e7658c | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1084 | // If N has no uses, it is dead. Make sure to revisit all N's operands once |
| 1085 | // N is deleted from the DAG, since they too may now be dead or may have a |
| 1086 | // reduced number of uses, allowing other xforms. |
| 1087 | if (N->use_empty() && N != &Dummy) { |
| 1088 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 1089 | AddToWorkList(N->getOperand(i).getNode()); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1090 | |
| Evan Cheng | 5e7658c | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1091 | DAG.DeleteNode(N); |
| 1092 | continue; |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1093 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1094 | |
| Evan Cheng | 5e7658c | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1095 | SDValue RV = combine(N); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1096 | |
| Evan Cheng | 5e7658c | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1097 | if (RV.getNode() == 0) |
| 1098 | continue; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1099 | |
| Evan Cheng | 5e7658c | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1100 | ++NodesCombined; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1101 | |
| Evan Cheng | 5e7658c | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1102 | // If we get back the same node we passed in, rather than a new node or |
| 1103 | // zero, we know that the node must have defined multiple values and |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1104 | // CombineTo was used. Since CombineTo takes care of the worklist |
| Evan Cheng | 5e7658c | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1105 | // mechanics for us, we have no work to do in this case. |
| 1106 | if (RV.getNode() == N) |
| 1107 | continue; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1108 | |
| Evan Cheng | 5e7658c | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1109 | assert(N->getOpcode() != ISD::DELETED_NODE && |
| 1110 | RV.getNode()->getOpcode() != ISD::DELETED_NODE && |
| 1111 | "Node was deleted but visit returned new node!"); |
| Chris Lattner | 8f872d2 | 2006-05-27 00:43:02 +0000 | [diff] [blame] | 1112 | |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1113 | DEBUG(dbgs() << "\nReplacing.3 "; |
| Chris Lattner | 4dc3edd | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1114 | N->dump(&DAG); |
| David Greene | fe5c352 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 1115 | dbgs() << "\nWith: "; |
| Chris Lattner | 4dc3edd | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1116 | RV.getNode()->dump(&DAG); |
| David Greene | fe5c352 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 1117 | dbgs() << '\n'); |
| Eric Christopher | d6300d2 | 2011-07-14 01:12:15 +0000 | [diff] [blame] | 1118 | |
| Devang Patel | efec771 | 2011-05-23 22:04:42 +0000 | [diff] [blame] | 1119 | // Transfer debug value. |
| 1120 | DAG.TransferDbgValues(SDValue(N, 0), RV); |
| Evan Cheng | 5e7658c | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1121 | WorkListRemover DeadNodes(*this); |
| 1122 | if (N->getNumValues() == RV.getNode()->getNumValues()) |
| Jakob Stoklund Olesen | beb9469 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 1123 | DAG.ReplaceAllUsesWith(N, RV.getNode()); |
| Evan Cheng | 5e7658c | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1124 | else { |
| 1125 | assert(N->getValueType(0) == RV.getValueType() && |
| 1126 | N->getNumValues() == 1 && "Type mismatch"); |
| 1127 | SDValue OpV = RV; |
| Jakob Stoklund Olesen | beb9469 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 1128 | DAG.ReplaceAllUsesWith(N, &OpV); |
| Evan Cheng | 5e7658c | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1129 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1130 | |
| Evan Cheng | 5e7658c | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1131 | // Push the new node and any users onto the worklist |
| 1132 | AddToWorkList(RV.getNode()); |
| 1133 | AddUsersToWorkList(RV.getNode()); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1134 | |
| Evan Cheng | 5e7658c | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1135 | // Add any uses of the old node to the worklist in case this node is the |
| 1136 | // last one that uses them. They may become dead after this node is |
| 1137 | // deleted. |
| 1138 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 1139 | AddToWorkList(N->getOperand(i).getNode()); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1140 | |
| Dan Gohman | cd0b1bf | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 1141 | // Finally, if the node is now dead, remove it from the graph. The node |
| 1142 | // may not be dead if the replacement process recursively simplified to |
| 1143 | // something else needing this node. |
| 1144 | if (N->use_empty()) { |
| 1145 | // Nodes can be reintroduced into the worklist. Make sure we do not |
| 1146 | // process a node that has been replaced. |
| 1147 | removeFromWorkList(N); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1148 | |
| Dan Gohman | cd0b1bf | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 1149 | // Finally, since the node is now dead, remove it from the graph. |
| 1150 | DAG.DeleteNode(N); |
| 1151 | } |
| Evan Cheng | 5e7658c | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1152 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1153 | |
| Chris Lattner | 06f1d0f | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 1154 | // If the root changed (e.g. it was a dead load, update the root). |
| 1155 | DAG.setRoot(Dummy.getValue()); |
| Hal Finkel | e0cf639 | 2012-04-16 03:33:22 +0000 | [diff] [blame] | 1156 | DAG.RemoveDeadNodes(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1159 | SDValue DAGCombiner::visit(SDNode *N) { |
| Evan Cheng | f100557 | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1160 | switch (N->getOpcode()) { |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1161 | default: break; |
| Nate Begeman | e8f78d1 | 2005-09-01 00:33:32 +0000 | [diff] [blame] | 1162 | case ISD::TokenFactor: return visitTokenFactor(N); |
| Chris Lattner | ee322b4 | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1163 | case ISD::MERGE_VALUES: return visitMERGE_VALUES(N); |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1164 | case ISD::ADD: return visitADD(N); |
| 1165 | case ISD::SUB: return visitSUB(N); |
| Chris Lattner | e2e13ca | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1166 | case ISD::ADDC: return visitADDC(N); |
| Craig Topper | 43a1bd6 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1167 | case ISD::SUBC: return visitSUBC(N); |
| Chris Lattner | e2e13ca | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1168 | case ISD::ADDE: return visitADDE(N); |
| Craig Topper | 43a1bd6 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1169 | case ISD::SUBE: return visitSUBE(N); |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1170 | case ISD::MUL: return visitMUL(N); |
| 1171 | case ISD::SDIV: return visitSDIV(N); |
| 1172 | case ISD::UDIV: return visitUDIV(N); |
| 1173 | case ISD::SREM: return visitSREM(N); |
| 1174 | case ISD::UREM: return visitUREM(N); |
| 1175 | case ISD::MULHU: return visitMULHU(N); |
| 1176 | case ISD::MULHS: return visitMULHS(N); |
| Dan Gohman | 5c6d0c3 | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1177 | case ISD::SMUL_LOHI: return visitSMUL_LOHI(N); |
| 1178 | case ISD::UMUL_LOHI: return visitUMUL_LOHI(N); |
| Benjamin Kramer | 2fd48f2 | 2011-05-21 18:31:55 +0000 | [diff] [blame] | 1179 | case ISD::SMULO: return visitSMULO(N); |
| 1180 | case ISD::UMULO: return visitUMULO(N); |
| Dan Gohman | 5c6d0c3 | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1181 | case ISD::SDIVREM: return visitSDIVREM(N); |
| 1182 | case ISD::UDIVREM: return visitUDIVREM(N); |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1183 | case ISD::AND: return visitAND(N); |
| 1184 | case ISD::OR: return visitOR(N); |
| 1185 | case ISD::XOR: return visitXOR(N); |
| 1186 | case ISD::SHL: return visitSHL(N); |
| 1187 | case ISD::SRA: return visitSRA(N); |
| 1188 | case ISD::SRL: return visitSRL(N); |
| 1189 | case ISD::CTLZ: return visitCTLZ(N); |
| Chandler Carruth | 637cc6a | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 1190 | case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N); |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1191 | case ISD::CTTZ: return visitCTTZ(N); |
| Chandler Carruth | 637cc6a | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 1192 | case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N); |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1193 | case ISD::CTPOP: return visitCTPOP(N); |
| Nate Begeman | 24a7eca | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1194 | case ISD::SELECT: return visitSELECT(N); |
| Benjamin Kramer | d56ffc7 | 2013-04-26 09:19:19 +0000 | [diff] [blame] | 1195 | case ISD::VSELECT: return visitVSELECT(N); |
| Nate Begeman | 24a7eca | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1196 | case ISD::SELECT_CC: return visitSELECT_CC(N); |
| 1197 | case ISD::SETCC: return visitSETCC(N); |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1198 | case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N); |
| 1199 | case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N); |
| Chris Lattner | 812646a | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 1200 | case ISD::ANY_EXTEND: return visitANY_EXTEND(N); |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1201 | case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N); |
| 1202 | case ISD::TRUNCATE: return visitTRUNCATE(N); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1203 | case ISD::BITCAST: return visitBITCAST(N); |
| Evan Cheng | b980f6f | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 1204 | case ISD::BUILD_PAIR: return visitBUILD_PAIR(N); |
| Chris Lattner | 6f3b577 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1205 | case ISD::FADD: return visitFADD(N); |
| 1206 | case ISD::FSUB: return visitFSUB(N); |
| 1207 | case ISD::FMUL: return visitFMUL(N); |
| Owen Anderson | 41b0665 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 1208 | case ISD::FMA: return visitFMA(N); |
| Chris Lattner | 6f3b577 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1209 | case ISD::FDIV: return visitFDIV(N); |
| 1210 | case ISD::FREM: return visitFREM(N); |
| Chris Lattner | 3bc4050 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 1211 | case ISD::FCOPYSIGN: return visitFCOPYSIGN(N); |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1212 | case ISD::SINT_TO_FP: return visitSINT_TO_FP(N); |
| 1213 | case ISD::UINT_TO_FP: return visitUINT_TO_FP(N); |
| 1214 | case ISD::FP_TO_SINT: return visitFP_TO_SINT(N); |
| 1215 | case ISD::FP_TO_UINT: return visitFP_TO_UINT(N); |
| 1216 | case ISD::FP_ROUND: return visitFP_ROUND(N); |
| 1217 | case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N); |
| 1218 | case ISD::FP_EXTEND: return visitFP_EXTEND(N); |
| 1219 | case ISD::FNEG: return visitFNEG(N); |
| 1220 | case ISD::FABS: return visitFABS(N); |
| Owen Anderson | a40319b | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 1221 | case ISD::FFLOOR: return visitFFLOOR(N); |
| 1222 | case ISD::FCEIL: return visitFCEIL(N); |
| 1223 | case ISD::FTRUNC: return visitFTRUNC(N); |
| Nate Begeman | c760f80 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1224 | case ISD::BRCOND: return visitBRCOND(N); |
| Nate Begeman | c760f80 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1225 | case ISD::BR_CC: return visitBR_CC(N); |
| Chris Lattner | e260ed8 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 1226 | case ISD::LOAD: return visitLOAD(N); |
| Chris Lattner | 04c7370 | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 1227 | case ISD::STORE: return visitSTORE(N); |
| Chris Lattner | 5336a59 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 1228 | case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N); |
| Evan Cheng | 0de312d | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 1229 | case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N); |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1230 | case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N); |
| 1231 | case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N); |
| Bruno Cardoso Lopes | 6cb23f6 | 2011-09-20 23:19:33 +0000 | [diff] [blame] | 1232 | case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N); |
| Chris Lattner | a46dfe8 | 2006-03-28 22:11:53 +0000 | [diff] [blame] | 1233 | case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N); |
| Manman Ren | 413a6cb | 2014-01-31 01:10:35 +0000 | [diff] [blame] | 1234 | case ISD::INSERT_SUBVECTOR: return visitINSERT_SUBVECTOR(N); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1235 | } |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1236 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1239 | SDValue DAGCombiner::combine(SDNode *N) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1240 | SDValue RV = visit(N); |
| Dan Gohman | 5c6d0c3 | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1241 | |
| 1242 | // If nothing happened, try a target-specific DAG combine. |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1243 | if (RV.getNode() == 0) { |
| Dan Gohman | 5c6d0c3 | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1244 | assert(N->getOpcode() != ISD::DELETED_NODE && |
| 1245 | "Node was deleted but visit returned NULL!"); |
| 1246 | |
| 1247 | if (N->getOpcode() >= ISD::BUILTIN_OP_END || |
| 1248 | TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) { |
| 1249 | |
| 1250 | // Expose the DAG combiner to the target combiner impls. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1251 | TargetLowering::DAGCombinerInfo |
| Nadav Rotem | b1dd524 | 2012-12-27 06:47:41 +0000 | [diff] [blame] | 1252 | DagCombineInfo(DAG, Level, false, this); |
| Dan Gohman | 5c6d0c3 | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1253 | |
| 1254 | RV = TLI.PerformDAGCombine(N, DagCombineInfo); |
| 1255 | } |
| 1256 | } |
| 1257 | |
| Evan Cheng | f100557 | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1258 | // If nothing happened still, try promoting the operation. |
| 1259 | if (RV.getNode() == 0) { |
| 1260 | switch (N->getOpcode()) { |
| 1261 | default: break; |
| 1262 | case ISD::ADD: |
| 1263 | case ISD::SUB: |
| 1264 | case ISD::MUL: |
| 1265 | case ISD::AND: |
| 1266 | case ISD::OR: |
| 1267 | case ISD::XOR: |
| 1268 | RV = PromoteIntBinOp(SDValue(N, 0)); |
| 1269 | break; |
| 1270 | case ISD::SHL: |
| 1271 | case ISD::SRA: |
| 1272 | case ISD::SRL: |
| 1273 | RV = PromoteIntShiftOp(SDValue(N, 0)); |
| 1274 | break; |
| 1275 | case ISD::SIGN_EXTEND: |
| 1276 | case ISD::ZERO_EXTEND: |
| 1277 | case ISD::ANY_EXTEND: |
| 1278 | RV = PromoteExtend(SDValue(N, 0)); |
| 1279 | break; |
| 1280 | case ISD::LOAD: |
| 1281 | if (PromoteLoad(SDValue(N, 0))) |
| 1282 | RV = SDValue(N, 0); |
| 1283 | break; |
| 1284 | } |
| 1285 | } |
| 1286 | |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1287 | // If N is a commutative binary node, try commuting it to enable more |
| Evan Cheng | 31604a6 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 1288 | // sdisel CSE. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1289 | if (RV.getNode() == 0 && |
| Evan Cheng | 31604a6 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 1290 | SelectionDAG::isCommutativeBinOp(N->getOpcode()) && |
| 1291 | N->getNumValues() == 1) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1292 | SDValue N0 = N->getOperand(0); |
| 1293 | SDValue N1 = N->getOperand(1); |
| Bill Wendling | 9c9a3b6 | 2009-01-30 01:13:16 +0000 | [diff] [blame] | 1294 | |
| Evan Cheng | 31604a6 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 1295 | // Constant operands are canonicalized to RHS. |
| 1296 | if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1297 | SDValue Ops[] = { N1, N0 }; |
| Evan Cheng | 31604a6 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 1298 | SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(), |
| 1299 | Ops, 2); |
| Evan Cheng | fe7610f | 2008-03-24 23:55:16 +0000 | [diff] [blame] | 1300 | if (CSENode) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1301 | return SDValue(CSENode, 0); |
| Evan Cheng | 31604a6 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 1302 | } |
| 1303 | } |
| 1304 | |
| Dan Gohman | 5c6d0c3 | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1305 | return RV; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1306 | } |
| Dan Gohman | 5c6d0c3 | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1307 | |
| Chris Lattner | 5ab6d8b | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1308 | /// getInputChainForNode - Given a node, return its input chain if it has one, |
| 1309 | /// otherwise return a null sd operand. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1310 | static SDValue getInputChainForNode(SDNode *N) { |
| Chris Lattner | 5ab6d8b | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1311 | if (unsigned NumOps = N->getNumOperands()) { |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1312 | if (N->getOperand(0).getValueType() == MVT::Other) |
| Chris Lattner | 5ab6d8b | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1313 | return N->getOperand(0); |
| Stephen Lin | 8e8424e | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 1314 | if (N->getOperand(NumOps-1).getValueType() == MVT::Other) |
| Chris Lattner | 5ab6d8b | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1315 | return N->getOperand(NumOps-1); |
| 1316 | for (unsigned i = 1; i < NumOps-1; ++i) |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1317 | if (N->getOperand(i).getValueType() == MVT::Other) |
| Chris Lattner | 5ab6d8b | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1318 | return N->getOperand(i); |
| 1319 | } |
| Bill Wendling | 9c9a3b6 | 2009-01-30 01:13:16 +0000 | [diff] [blame] | 1320 | return SDValue(); |
| Chris Lattner | 5ab6d8b | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1323 | SDValue DAGCombiner::visitTokenFactor(SDNode *N) { |
| Chris Lattner | 5ab6d8b | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1324 | // If N has two operands, where one has an input chain equal to the other, |
| 1325 | // the 'other' chain is redundant. |
| 1326 | if (N->getNumOperands() == 2) { |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1327 | if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1)) |
| Chris Lattner | 5ab6d8b | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1328 | return N->getOperand(0); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1329 | if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0)) |
| Chris Lattner | 5ab6d8b | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1330 | return N->getOperand(1); |
| 1331 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1332 | |
| Chris Lattner | 48fb92f | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 1333 | SmallVector<SDNode *, 8> TFs; // List of token factors to visit. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1334 | SmallVector<SDValue, 8> Ops; // Ops for replacing token factor. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1335 | SmallPtrSet<SDNode*, 16> SeenOps; |
| Chris Lattner | 48fb92f | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 1336 | bool Changed = false; // If we should replace this token factor. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1337 | |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1338 | // Start out with this token factor. |
| Jim Laskey | d07be23 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 1339 | TFs.push_back(N); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1340 | |
| Jim Laskey | 0463e08 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 1341 | // Iterate through token factors. The TFs grows when new token factors are |
| Jim Laskey | 6549d22 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 1342 | // encountered. |
| 1343 | for (unsigned i = 0; i < TFs.size(); ++i) { |
| 1344 | SDNode *TF = TFs[i]; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1345 | |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1346 | // Check each of the operands. |
| 1347 | for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1348 | SDValue Op = TF->getOperand(i); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1349 | |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1350 | switch (Op.getOpcode()) { |
| 1351 | case ISD::EntryToken: |
| Jim Laskey | 6549d22 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 1352 | // Entry tokens don't need to be added to the list. They are |
| 1353 | // rededundant. |
| 1354 | Changed = true; |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1355 | break; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1356 | |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1357 | case ISD::TokenFactor: |
| Nate Begeman | 879d8f1 | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 1358 | if (Op.hasOneUse() && |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1359 | std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) { |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1360 | // Queue up for processing. |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1361 | TFs.push_back(Op.getNode()); |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1362 | // Clean up in case the token factor is removed. |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1363 | AddToWorkList(Op.getNode()); |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1364 | Changed = true; |
| 1365 | break; |
| Jim Laskey | d07be23 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 1366 | } |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1367 | // Fall thru |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1368 | |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1369 | default: |
| Chris Lattner | 48fb92f | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 1370 | // Only add if it isn't already in the list. |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1371 | if (SeenOps.insert(Op.getNode())) |
| Jim Laskey | 6549d22 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 1372 | Ops.push_back(Op); |
| Chris Lattner | 48fb92f | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 1373 | else |
| 1374 | Changed = true; |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1375 | break; |
| Jim Laskey | d07be23 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 1376 | } |
| 1377 | } |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1378 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1379 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1380 | SDValue Result; |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1381 | |
| 1382 | // If we've change things around then replace token factor. |
| 1383 | if (Changed) { |
| Dan Gohman | 70de4cb | 2008-01-29 13:02:09 +0000 | [diff] [blame] | 1384 | if (Ops.empty()) { |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1385 | // The entry token is the only possible outcome. |
| 1386 | Result = DAG.getEntryNode(); |
| 1387 | } else { |
| 1388 | // New and improved token factor. |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1389 | Result = DAG.getNode(ISD::TokenFactor, SDLoc(N), |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1390 | MVT::Other, &Ops[0], Ops.size()); |
| Nate Begeman | 02b23c6 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1391 | } |
| Bill Wendling | 9c9a3b6 | 2009-01-30 01:13:16 +0000 | [diff] [blame] | 1392 | |
| Jim Laskey | dcf983c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 1393 | // Don't add users to work list. |
| 1394 | return CombineTo(N, Result, false); |
| Nate Begeman | 02b23c6 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1395 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1396 | |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1397 | return Result; |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| Chris Lattner | ee322b4 | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1400 | /// MERGE_VALUES can always be eliminated. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1401 | SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) { |
| Chris Lattner | ee322b4 | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1402 | WorkListRemover DeadNodes(*this); |
| Dan Gohman | 9d26c85 | 2009-08-10 23:43:19 +0000 | [diff] [blame] | 1403 | // Replacing results may cause a different MERGE_VALUES to suddenly |
| 1404 | // be CSE'd with N, and carry its uses with it. Iterate until no |
| 1405 | // uses remain, to ensure that the node can be safely deleted. |
| Pete Cooper | fe5b84b | 2012-06-20 19:35:43 +0000 | [diff] [blame] | 1406 | // First add the users of this node to the work list so that they |
| 1407 | // can be tried again once they have new operands. |
| 1408 | AddUsersToWorkList(N); |
| Dan Gohman | 9d26c85 | 2009-08-10 23:43:19 +0000 | [diff] [blame] | 1409 | do { |
| 1410 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| Jakob Stoklund Olesen | beb9469 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 1411 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i)); |
| Dan Gohman | 9d26c85 | 2009-08-10 23:43:19 +0000 | [diff] [blame] | 1412 | } while (!N->use_empty()); |
| Chris Lattner | ee322b4 | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1413 | removeFromWorkList(N); |
| 1414 | DAG.DeleteNode(N); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1415 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| Chris Lattner | ee322b4 | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
| Evan Cheng | 9201100 | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1418 | static |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1419 | SDValue combineShlAddConstant(SDLoc DL, SDValue N0, SDValue N1, |
| Bill Wendling | cdd9613 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1420 | SelectionDAG &DAG) { |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1421 | EVT VT = N0.getValueType(); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1422 | SDValue N00 = N0.getOperand(0); |
| 1423 | SDValue N01 = N0.getOperand(1); |
| Evan Cheng | 9201100 | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1424 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01); |
| Bill Wendling | cdd9613 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1425 | |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1426 | if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() && |
| Evan Cheng | 9201100 | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1427 | isa<ConstantSDNode>(N00.getOperand(1))) { |
| Bill Wendling | cdd9613 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1428 | // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), ) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1429 | N0 = DAG.getNode(ISD::ADD, SDLoc(N0), VT, |
| 1430 | DAG.getNode(ISD::SHL, SDLoc(N00), VT, |
| Bill Wendling | cdd9613 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1431 | N00.getOperand(0), N01), |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1432 | DAG.getNode(ISD::SHL, SDLoc(N01), VT, |
| Bill Wendling | cdd9613 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1433 | N00.getOperand(1), N01)); |
| 1434 | return DAG.getNode(ISD::ADD, DL, VT, N0, N1); |
| Evan Cheng | 9201100 | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1435 | } |
| Bill Wendling | cdd9613 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1436 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1437 | return SDValue(); |
| Evan Cheng | 9201100 | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1440 | SDValue DAGCombiner::visitADD(SDNode *N) { |
| 1441 | SDValue N0 = N->getOperand(0); |
| 1442 | SDValue N1 = N->getOperand(1); |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1443 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1444 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1445 | EVT VT = N0.getValueType(); |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1446 | |
| 1447 | // fold vector ops |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1448 | if (VT.isVector()) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1449 | SDValue FoldedVOp = SimplifyVBinOp(N); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1450 | if (FoldedVOp.getNode()) return FoldedVOp; |
| Craig Topper | d8005db | 2012-12-10 08:12:29 +0000 | [diff] [blame] | 1451 | |
| 1452 | // fold (add x, 0) -> x, vector edition |
| 1453 | if (ISD::isBuildVectorAllZeros(N1.getNode())) |
| 1454 | return N0; |
| 1455 | if (ISD::isBuildVectorAllZeros(N0.getNode())) |
| 1456 | return N1; |
| Dan Gohman | 80f9f07 | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 1457 | } |
| Bill Wendling | 0864a75 | 2008-12-10 22:36:00 +0000 | [diff] [blame] | 1458 | |
| Dan Gohman | 06563a8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1459 | // fold (add x, undef) -> undef |
| Dan Gohman | adb3d37 | 2007-07-10 15:19:29 +0000 | [diff] [blame] | 1460 | if (N0.getOpcode() == ISD::UNDEF) |
| 1461 | return N0; |
| 1462 | if (N1.getOpcode() == ISD::UNDEF) |
| Dan Gohman | 06563a8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1463 | return N1; |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1464 | // fold (add c1, c2) -> c1+c2 |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1465 | if (N0C && N1C) |
| Bill Wendling | dea9130 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1466 | return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C); |
| Nate Begeman | 2cc2c9a | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1467 | // canonicalize constant to RHS |
| Nate Begeman | 418c6e4 | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 1468 | if (N0C && !N1C) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1469 | return DAG.getNode(ISD::ADD, SDLoc(N), VT, N1, N0); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1470 | // fold (add x, 0) -> x |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1471 | if (N1C && N1C->isNullValue()) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1472 | return N0; |
| Dan Gohman | 2fe6bee | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1473 | // fold (add Sym, c) -> Sym+c |
| 1474 | if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0)) |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 1475 | if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C && |
| Dan Gohman | 2fe6bee | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1476 | GA->getOpcode() == ISD::GlobalAddress) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1477 | return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT, |
| Dan Gohman | 2fe6bee | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1478 | GA->getOffset() + |
| 1479 | (uint64_t)N1C->getSExtValue()); |
| Chris Lattner | 3470b5d | 2006-01-12 20:22:43 +0000 | [diff] [blame] | 1480 | // fold ((c1-A)+c2) -> (c1+c2)-A |
| 1481 | if (N1C && N0.getOpcode() == ISD::SUB) |
| 1482 | if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0))) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1483 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1484 | DAG.getConstant(N1C->getAPIntValue()+ |
| 1485 | N0C->getAPIntValue(), VT), |
| Chris Lattner | 3470b5d | 2006-01-12 20:22:43 +0000 | [diff] [blame] | 1486 | N0.getOperand(1)); |
| Nate Begeman | 22e251a | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1487 | // reassociate add |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1488 | SDValue RADD = ReassociateOps(ISD::ADD, SDLoc(N), N0, N1); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1489 | if (RADD.getNode() != 0) |
| Nate Begeman | 22e251a | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1490 | return RADD; |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1491 | // fold ((0-A) + B) -> B-A |
| 1492 | if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) && |
| 1493 | cast<ConstantSDNode>(N0.getOperand(0))->isNullValue()) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1494 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1, N0.getOperand(1)); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1495 | // fold (A + (0-B)) -> A-B |
| 1496 | if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) && |
| 1497 | cast<ConstantSDNode>(N1.getOperand(0))->isNullValue()) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1498 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1.getOperand(1)); |
| Chris Lattner | 6f3b577 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1499 | // fold (A+(B-A)) -> B |
| 1500 | if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1)) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1501 | return N1.getOperand(0); |
| Dale Johannesen | 73bc0ba | 2008-11-27 00:43:21 +0000 | [diff] [blame] | 1502 | // fold ((B-A)+A) -> B |
| 1503 | if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1)) |
| 1504 | return N0.getOperand(0); |
| Dale Johannesen | 8c76670 | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1505 | // fold (A+(B-(A+C))) to (B-C) |
| 1506 | if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD && |
| Bill Wendling | c442348 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1507 | N0 == N1.getOperand(1).getOperand(0)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1508 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0), |
| Dale Johannesen | 8c76670 | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1509 | N1.getOperand(1).getOperand(1)); |
| Dale Johannesen | 8c76670 | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1510 | // fold (A+(B-(C+A))) to (B-C) |
| 1511 | if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD && |
| Bill Wendling | c442348 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1512 | N0 == N1.getOperand(1).getOperand(1)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1513 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0), |
| Dale Johannesen | 8c76670 | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1514 | N1.getOperand(1).getOperand(0)); |
| Dale Johannesen | ee573fc | 2008-12-23 23:47:22 +0000 | [diff] [blame] | 1515 | // fold (A+((B-A)+or-C)) to (B+or-C) |
| Dale Johannesen | 54bdec2 | 2008-12-02 18:40:40 +0000 | [diff] [blame] | 1516 | if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) && |
| 1517 | N1.getOperand(0).getOpcode() == ISD::SUB && |
| Bill Wendling | c442348 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1518 | N0 == N1.getOperand(0).getOperand(1)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1519 | return DAG.getNode(N1.getOpcode(), SDLoc(N), VT, |
| Bill Wendling | c442348 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1520 | N1.getOperand(0).getOperand(0), N1.getOperand(1)); |
| Dale Johannesen | 54bdec2 | 2008-12-02 18:40:40 +0000 | [diff] [blame] | 1521 | |
| Dale Johannesen | 8c76670 | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1522 | // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant |
| 1523 | if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) { |
| 1524 | SDValue N00 = N0.getOperand(0); |
| 1525 | SDValue N01 = N0.getOperand(1); |
| 1526 | SDValue N10 = N1.getOperand(0); |
| 1527 | SDValue N11 = N1.getOperand(1); |
| Bill Wendling | c442348 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1528 | |
| 1529 | if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1530 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, |
| 1531 | DAG.getNode(ISD::ADD, SDLoc(N0), VT, N00, N10), |
| 1532 | DAG.getNode(ISD::ADD, SDLoc(N1), VT, N01, N11)); |
| Dale Johannesen | 8c76670 | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1533 | } |
| Chris Lattner | d8c2a48d | 2006-03-13 06:51:27 +0000 | [diff] [blame] | 1534 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1535 | if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0))) |
| 1536 | return SDValue(N, 0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1537 | |
| Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1538 | // fold (a+b) -> (a|b) iff a and b share no bits. |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1539 | if (VT.isInteger() && !VT.isVector()) { |
| Dan Gohman | d0ff91d | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 1540 | APInt LHSZero, LHSOne; |
| 1541 | APInt RHSZero, RHSOne; |
| Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1542 | DAG.ComputeMaskedBits(N0, LHSZero, LHSOne); |
| Bill Wendling | c442348 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1543 | |
| Dan Gohman | d0ff91d | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 1544 | if (LHSZero.getBoolValue()) { |
| Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1545 | DAG.ComputeMaskedBits(N1, RHSZero, RHSOne); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1546 | |
| Chris Lattner | d8c2a48d | 2006-03-13 06:51:27 +0000 | [diff] [blame] | 1547 | // If all possibly-set bits on the LHS are clear on the RHS, return an OR. |
| 1548 | // If all possibly-set bits on the RHS are clear on the LHS, return an OR. |
| Owen Anderson | 60a4678 | 2014-01-31 00:51:43 +0000 | [diff] [blame] | 1549 | if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero){ |
| 1550 | if (!LegalOperations || TLI.isOperationLegal(ISD::OR, VT)) |
| 1551 | return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1); |
| 1552 | } |
| Chris Lattner | d8c2a48d | 2006-03-13 06:51:27 +0000 | [diff] [blame] | 1553 | } |
| 1554 | } |
| Evan Cheng | eb99bd7 | 2006-11-06 08:14:30 +0000 | [diff] [blame] | 1555 | |
| Evan Cheng | 9201100 | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1556 | // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), ) |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1557 | if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1558 | SDValue Result = combineShlAddConstant(SDLoc(N), N0, N1, DAG); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1559 | if (Result.getNode()) return Result; |
| Evan Cheng | 9201100 | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1560 | } |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1561 | if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1562 | SDValue Result = combineShlAddConstant(SDLoc(N), N1, N0, DAG); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1563 | if (Result.getNode()) return Result; |
| Evan Cheng | 9201100 | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
| Dan Gohman | 954f490 | 2010-01-19 23:30:49 +0000 | [diff] [blame] | 1566 | // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n)) |
| 1567 | if (N1.getOpcode() == ISD::SHL && |
| 1568 | N1.getOperand(0).getOpcode() == ISD::SUB) |
| 1569 | if (ConstantSDNode *C = |
| 1570 | dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0))) |
| 1571 | if (C->getAPIntValue() == 0) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1572 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, |
| 1573 | DAG.getNode(ISD::SHL, SDLoc(N), VT, |
| Dan Gohman | 954f490 | 2010-01-19 23:30:49 +0000 | [diff] [blame] | 1574 | N1.getOperand(0).getOperand(1), |
| 1575 | N1.getOperand(1))); |
| 1576 | if (N0.getOpcode() == ISD::SHL && |
| 1577 | N0.getOperand(0).getOpcode() == ISD::SUB) |
| 1578 | if (ConstantSDNode *C = |
| 1579 | dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0))) |
| 1580 | if (C->getAPIntValue() == 0) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1581 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1, |
| 1582 | DAG.getNode(ISD::SHL, SDLoc(N), VT, |
| Dan Gohman | 954f490 | 2010-01-19 23:30:49 +0000 | [diff] [blame] | 1583 | N0.getOperand(0).getOperand(1), |
| 1584 | N0.getOperand(1))); |
| 1585 | |
| Owen Anderson | 5e65dfb | 2010-09-21 20:42:50 +0000 | [diff] [blame] | 1586 | if (N1.getOpcode() == ISD::AND) { |
| 1587 | SDValue AndOp0 = N1.getOperand(0); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1588 | ConstantSDNode *AndOp1 = dyn_cast<ConstantSDNode>(N1->getOperand(1)); |
| Owen Anderson | 5e65dfb | 2010-09-21 20:42:50 +0000 | [diff] [blame] | 1589 | unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0); |
| 1590 | unsigned DestBits = VT.getScalarType().getSizeInBits(); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1591 | |
| Owen Anderson | 5e65dfb | 2010-09-21 20:42:50 +0000 | [diff] [blame] | 1592 | // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x)) |
| 1593 | // and similar xforms where the inner op is either ~0 or 0. |
| 1594 | if (NumSignBits == DestBits && AndOp1 && AndOp1->isOne()) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1595 | SDLoc DL(N); |
| Owen Anderson | 5e65dfb | 2010-09-21 20:42:50 +0000 | [diff] [blame] | 1596 | return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), AndOp0); |
| 1597 | } |
| 1598 | } |
| 1599 | |
| Benjamin Kramer | 1f4dfbb | 2010-12-22 23:17:45 +0000 | [diff] [blame] | 1600 | // add (sext i1), X -> sub X, (zext i1) |
| 1601 | if (N0.getOpcode() == ISD::SIGN_EXTEND && |
| 1602 | N0.getOperand(0).getValueType() == MVT::i1 && |
| 1603 | !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1604 | SDLoc DL(N); |
| Benjamin Kramer | 1f4dfbb | 2010-12-22 23:17:45 +0000 | [diff] [blame] | 1605 | SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)); |
| 1606 | return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt); |
| 1607 | } |
| 1608 | |
| Evan Cheng | f100557 | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1609 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1612 | SDValue DAGCombiner::visitADDC(SDNode *N) { |
| 1613 | SDValue N0 = N->getOperand(0); |
| 1614 | SDValue N1 = N->getOperand(1); |
| Chris Lattner | e2e13ca | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1615 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1616 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1617 | EVT VT = N0.getValueType(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1618 | |
| Chris Lattner | e2e13ca | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1619 | // If the flag result is dead, turn this into an ADD. |
| Craig Topper | 0515cd4 | 2012-01-07 18:31:09 +0000 | [diff] [blame] | 1620 | if (!N->hasAnyUseOfValue(1)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1621 | return CombineTo(N, DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, N1), |
| Dale Johannesen | 5234d37 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 1622 | DAG.getNode(ISD::CARRY_FALSE, |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1623 | SDLoc(N), MVT::Glue)); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1624 | |
| Chris Lattner | e2e13ca | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1625 | // canonicalize constant to RHS. |
| Dan Gohman | b4e2637 | 2008-06-23 15:29:14 +0000 | [diff] [blame] | 1626 | if (N0C && !N1C) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1627 | return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N1, N0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1628 | |
| Chris Lattner | 4720666 | 2007-03-04 20:40:38 +0000 | [diff] [blame] | 1629 | // fold (addc x, 0) -> x + no carry out |
| 1630 | if (N1C && N1C->isNullValue()) |
| Dale Johannesen | 5234d37 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 1631 | return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1632 | SDLoc(N), MVT::Glue)); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1633 | |
| Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1634 | // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits. |
| Dan Gohman | d0ff91d | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 1635 | APInt LHSZero, LHSOne; |
| 1636 | APInt RHSZero, RHSOne; |
| Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1637 | DAG.ComputeMaskedBits(N0, LHSZero, LHSOne); |
| Bill Wendling | 6127757 | 2009-01-30 02:38:00 +0000 | [diff] [blame] | 1638 | |
| Dan Gohman | d0ff91d | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 1639 | if (LHSZero.getBoolValue()) { |
| Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1640 | DAG.ComputeMaskedBits(N1, RHSZero, RHSOne); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1641 | |
| Chris Lattner | 4720666 | 2007-03-04 20:40:38 +0000 | [diff] [blame] | 1642 | // If all possibly-set bits on the LHS are clear on the RHS, return an OR. |
| 1643 | // If all possibly-set bits on the RHS are clear on the LHS, return an OR. |
| Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1644 | if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1645 | return CombineTo(N, DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1), |
| Dale Johannesen | 5234d37 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 1646 | DAG.getNode(ISD::CARRY_FALSE, |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1647 | SDLoc(N), MVT::Glue)); |
| Chris Lattner | 4720666 | 2007-03-04 20:40:38 +0000 | [diff] [blame] | 1648 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1649 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1650 | return SDValue(); |
| Chris Lattner | e2e13ca | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1653 | SDValue DAGCombiner::visitADDE(SDNode *N) { |
| 1654 | SDValue N0 = N->getOperand(0); |
| 1655 | SDValue N1 = N->getOperand(1); |
| 1656 | SDValue CarryIn = N->getOperand(2); |
| Chris Lattner | e2e13ca | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1657 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1658 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1659 | |
| Chris Lattner | e2e13ca | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1660 | // canonicalize constant to RHS |
| Dan Gohman | b4e2637 | 2008-06-23 15:29:14 +0000 | [diff] [blame] | 1661 | if (N0C && !N1C) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1662 | return DAG.getNode(ISD::ADDE, SDLoc(N), N->getVTList(), |
| Bill Wendling | 6127757 | 2009-01-30 02:38:00 +0000 | [diff] [blame] | 1663 | N1, N0, CarryIn); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1664 | |
| Chris Lattner | 4720666 | 2007-03-04 20:40:38 +0000 | [diff] [blame] | 1665 | // fold (adde x, y, false) -> (addc x, y) |
| Dale Johannesen | 5234d37 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 1666 | if (CarryIn.getOpcode() == ISD::CARRY_FALSE) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1667 | return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N0, N1); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1668 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1669 | return SDValue(); |
| Chris Lattner | e2e13ca | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
| Eric Christopher | e5ca1e0 | 2011-02-16 04:50:12 +0000 | [diff] [blame] | 1672 | // Since it may not be valid to emit a fold to zero for vector initializers |
| 1673 | // check if we can before folding. |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1674 | static SDValue tryFoldToZero(SDLoc DL, const TargetLowering &TLI, EVT VT, |
| Hal Finkel | 6c29bd9 | 2013-07-09 17:02:45 +0000 | [diff] [blame] | 1675 | SelectionDAG &DAG, |
| 1676 | bool LegalOperations, bool LegalTypes) { |
| Stephen Lin | 8e8424e | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 1677 | if (!VT.isVector()) |
| Eric Christopher | e5ca1e0 | 2011-02-16 04:50:12 +0000 | [diff] [blame] | 1678 | return DAG.getConstant(0, VT); |
| Daniel Sanders | b021c6f | 2013-11-25 11:14:43 +0000 | [diff] [blame] | 1679 | if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) |
| 1680 | return DAG.getConstant(0, VT); |
| Eric Christopher | e5ca1e0 | 2011-02-16 04:50:12 +0000 | [diff] [blame] | 1681 | return SDValue(); |
| 1682 | } |
| 1683 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1684 | SDValue DAGCombiner::visitSUB(SDNode *N) { |
| 1685 | SDValue N0 = N->getOperand(0); |
| 1686 | SDValue N1 = N->getOperand(1); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1687 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode()); |
| 1688 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); |
| Eric Christopher | d6300d2 | 2011-07-14 01:12:15 +0000 | [diff] [blame] | 1689 | ConstantSDNode *N1C1 = N1.getOpcode() != ISD::ADD ? 0 : |
| 1690 | dyn_cast<ConstantSDNode>(N1.getOperand(1).getNode()); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1691 | EVT VT = N0.getValueType(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1692 | |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1693 | // fold vector ops |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1694 | if (VT.isVector()) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1695 | SDValue FoldedVOp = SimplifyVBinOp(N); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1696 | if (FoldedVOp.getNode()) return FoldedVOp; |
| Craig Topper | d8005db | 2012-12-10 08:12:29 +0000 | [diff] [blame] | 1697 | |
| 1698 | // fold (sub x, 0) -> x, vector edition |
| 1699 | if (ISD::isBuildVectorAllZeros(N1.getNode())) |
| 1700 | return N0; |
| Dan Gohman | 80f9f07 | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 1701 | } |
| Bill Wendling | 0864a75 | 2008-12-10 22:36:00 +0000 | [diff] [blame] | 1702 | |
| Chris Lattner | eeb2bda | 2005-10-17 01:07:11 +0000 | [diff] [blame] | 1703 | // fold (sub x, x) -> 0 |
| Eric Christopher | ef72141 | 2011-02-16 01:10:03 +0000 | [diff] [blame] | 1704 | // FIXME: Refactor this and xor and other similar operations together. |
| Eric Christopher | e5ca1e0 | 2011-02-16 04:50:12 +0000 | [diff] [blame] | 1705 | if (N0 == N1) |
| Hal Finkel | 6c29bd9 | 2013-07-09 17:02:45 +0000 | [diff] [blame] | 1706 | return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1707 | // fold (sub c1, c2) -> c1-c2 |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1708 | if (N0C && N1C) |
| Bill Wendling | dea9130 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1709 | return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C); |
| Chris Lattner | c38fb8e | 2005-10-11 06:07:15 +0000 | [diff] [blame] | 1710 | // fold (sub x, c) -> (add x, -c) |
| 1711 | if (N1C) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1712 | return DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1713 | DAG.getConstant(-N1C->getAPIntValue(), VT)); |
| Evan Cheng | 88b65bc | 2010-01-18 21:38:44 +0000 | [diff] [blame] | 1714 | // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) |
| 1715 | if (N0C && N0C->isAllOnesValue()) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1716 | return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0); |
| Benjamin Kramer | 65bb14d | 2011-01-29 12:34:05 +0000 | [diff] [blame] | 1717 | // fold A-(A-B) -> B |
| 1718 | if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0)) |
| 1719 | return N1.getOperand(1); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1720 | // fold (A+B)-A -> B |
| Chris Lattner | 6f3b577 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1721 | if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1722 | return N0.getOperand(1); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1723 | // fold (A+B)-B -> A |
| Chris Lattner | 6f3b577 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1724 | if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1) |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1725 | return N0.getOperand(0); |
| Eric Christopher | d6300d2 | 2011-07-14 01:12:15 +0000 | [diff] [blame] | 1726 | // fold C2-(A+C1) -> (C2-C1)-A |
| 1727 | if (N1.getOpcode() == ISD::ADD && N0C && N1C1) { |
| Nadav Rotem | 841c9a8 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 1728 | SDValue NewC = DAG.getConstant(N0C->getAPIntValue() - N1C1->getAPIntValue(), |
| 1729 | VT); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1730 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, NewC, |
| Bill Wendling | d163405 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 1731 | N1.getOperand(0)); |
| Eric Christopher | d6300d2 | 2011-07-14 01:12:15 +0000 | [diff] [blame] | 1732 | } |
| Dale Johannesen | ee573fc | 2008-12-23 23:47:22 +0000 | [diff] [blame] | 1733 | // fold ((A+(B+or-C))-B) -> A+or-C |
| Dale Johannesen | f51dcef | 2008-12-16 22:13:49 +0000 | [diff] [blame] | 1734 | if (N0.getOpcode() == ISD::ADD && |
| Dale Johannesen | acc84e5 | 2008-12-23 23:01:27 +0000 | [diff] [blame] | 1735 | (N0.getOperand(1).getOpcode() == ISD::SUB || |
| 1736 | N0.getOperand(1).getOpcode() == ISD::ADD) && |
| Dale Johannesen | f51dcef | 2008-12-16 22:13:49 +0000 | [diff] [blame] | 1737 | N0.getOperand(1).getOperand(0) == N1) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1738 | return DAG.getNode(N0.getOperand(1).getOpcode(), SDLoc(N), VT, |
| Bill Wendling | 48ff08e | 2009-01-30 02:42:10 +0000 | [diff] [blame] | 1739 | N0.getOperand(0), N0.getOperand(1).getOperand(1)); |
| Dale Johannesen | acc84e5 | 2008-12-23 23:01:27 +0000 | [diff] [blame] | 1740 | // fold ((A+(C+B))-B) -> A+C |
| 1741 | if (N0.getOpcode() == ISD::ADD && |
| 1742 | N0.getOperand(1).getOpcode() == ISD::ADD && |
| 1743 | N0.getOperand(1).getOperand(1) == N1) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1744 | return DAG.getNode(ISD::ADD, SDLoc(N), VT, |
| Bill Wendling | 48ff08e | 2009-01-30 02:42:10 +0000 | [diff] [blame] | 1745 | N0.getOperand(0), N0.getOperand(1).getOperand(0)); |
| Dale Johannesen | d2a4685 | 2008-12-23 01:59:54 +0000 | [diff] [blame] | 1746 | // fold ((A-(B-C))-C) -> A-B |
| 1747 | if (N0.getOpcode() == ISD::SUB && |
| 1748 | N0.getOperand(1).getOpcode() == ISD::SUB && |
| 1749 | N0.getOperand(1).getOperand(1) == N1) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1750 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, |
| Bill Wendling | 48ff08e | 2009-01-30 02:42:10 +0000 | [diff] [blame] | 1751 | N0.getOperand(0), N0.getOperand(1).getOperand(0)); |
| Bill Wendling | 48ff08e | 2009-01-30 02:42:10 +0000 | [diff] [blame] | 1752 | |
| Dan Gohman | 06563a8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1753 | // If either operand of a sub is undef, the result is undef |
| Dan Gohman | adb3d37 | 2007-07-10 15:19:29 +0000 | [diff] [blame] | 1754 | if (N0.getOpcode() == ISD::UNDEF) |
| 1755 | return N0; |
| 1756 | if (N1.getOpcode() == ISD::UNDEF) |
| 1757 | return N1; |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1758 | |
| Dan Gohman | 2fe6bee | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1759 | // If the relocation model supports it, consider symbol offsets. |
| 1760 | if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0)) |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 1761 | if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) { |
| Dan Gohman | 2fe6bee | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1762 | // fold (sub Sym, c) -> Sym-c |
| 1763 | if (N1C && GA->getOpcode() == ISD::GlobalAddress) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1764 | return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT, |
| Dan Gohman | 2fe6bee | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1765 | GA->getOffset() - |
| 1766 | (uint64_t)N1C->getSExtValue()); |
| 1767 | // fold (sub Sym+c1, Sym+c2) -> c1-c2 |
| 1768 | if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1)) |
| 1769 | if (GA->getGlobal() == GB->getGlobal()) |
| 1770 | return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(), |
| 1771 | VT); |
| 1772 | } |
| 1773 | |
| Evan Cheng | f100557 | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1774 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
| Craig Topper | 43a1bd6 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1777 | SDValue DAGCombiner::visitSUBC(SDNode *N) { |
| 1778 | SDValue N0 = N->getOperand(0); |
| 1779 | SDValue N1 = N->getOperand(1); |
| 1780 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1781 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 1782 | EVT VT = N0.getValueType(); |
| 1783 | |
| 1784 | // If the flag result is dead, turn this into an SUB. |
| Craig Topper | 0515cd4 | 2012-01-07 18:31:09 +0000 | [diff] [blame] | 1785 | if (!N->hasAnyUseOfValue(1)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1786 | return CombineTo(N, DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1), |
| 1787 | DAG.getNode(ISD::CARRY_FALSE, SDLoc(N), |
| Craig Topper | 43a1bd6 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1788 | MVT::Glue)); |
| 1789 | |
| 1790 | // fold (subc x, x) -> 0 + no borrow |
| 1791 | if (N0 == N1) |
| 1792 | return CombineTo(N, DAG.getConstant(0, VT), |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1793 | DAG.getNode(ISD::CARRY_FALSE, SDLoc(N), |
| Craig Topper | 43a1bd6 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1794 | MVT::Glue)); |
| 1795 | |
| 1796 | // fold (subc x, 0) -> x + no borrow |
| 1797 | if (N1C && N1C->isNullValue()) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1798 | return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, SDLoc(N), |
| Craig Topper | 43a1bd6 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1799 | MVT::Glue)); |
| 1800 | |
| 1801 | // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow |
| 1802 | if (N0C && N0C->isAllOnesValue()) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1803 | return CombineTo(N, DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0), |
| 1804 | DAG.getNode(ISD::CARRY_FALSE, SDLoc(N), |
| Craig Topper | 43a1bd6 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1805 | MVT::Glue)); |
| 1806 | |
| 1807 | return SDValue(); |
| 1808 | } |
| 1809 | |
| 1810 | SDValue DAGCombiner::visitSUBE(SDNode *N) { |
| 1811 | SDValue N0 = N->getOperand(0); |
| 1812 | SDValue N1 = N->getOperand(1); |
| 1813 | SDValue CarryIn = N->getOperand(2); |
| 1814 | |
| 1815 | // fold (sube x, y, false) -> (subc x, y) |
| 1816 | if (CarryIn.getOpcode() == ISD::CARRY_FALSE) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1817 | return DAG.getNode(ISD::SUBC, SDLoc(N), N->getVTList(), N0, N1); |
| Craig Topper | 43a1bd6 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1818 | |
| 1819 | return SDValue(); |
| 1820 | } |
| 1821 | |
| Jack Carter | d4e9615 | 2013-10-17 01:34:33 +0000 | [diff] [blame] | 1822 | /// isConstantSplatVector - Returns true if N is a BUILD_VECTOR node whose |
| 1823 | /// elements are all the same constant or undefined. |
| Elena Demikhovsky | 6769c50 | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1824 | static bool isConstantSplatVector(SDNode *N, APInt& SplatValue) { |
| 1825 | BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(N); |
| 1826 | if (!C) |
| 1827 | return false; |
| 1828 | |
| 1829 | APInt SplatUndef; |
| 1830 | unsigned SplatBitSize; |
| 1831 | bool HasAnyUndefs; |
| 1832 | EVT EltVT = N->getValueType(0).getVectorElementType(); |
| 1833 | return (C->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, |
| 1834 | HasAnyUndefs) && |
| 1835 | EltVT.getSizeInBits() >= SplatBitSize); |
| 1836 | } |
| 1837 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1838 | SDValue DAGCombiner::visitMUL(SDNode *N) { |
| 1839 | SDValue N0 = N->getOperand(0); |
| 1840 | SDValue N1 = N->getOperand(1); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1841 | EVT VT = N0.getValueType(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1842 | |
| Dan Gohman | 06563a8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1843 | // fold (mul x, undef) -> 0 |
| Dan Gohman | fa91282 | 2007-07-10 14:20:37 +0000 | [diff] [blame] | 1844 | if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) |
| Dan Gohman | 06563a8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1845 | return DAG.getConstant(0, VT); |
| Elena Demikhovsky | 6769c50 | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1846 | |
| 1847 | bool N0IsConst = false; |
| 1848 | bool N1IsConst = false; |
| 1849 | APInt ConstValue0, ConstValue1; |
| 1850 | // fold vector ops |
| 1851 | if (VT.isVector()) { |
| 1852 | SDValue FoldedVOp = SimplifyVBinOp(N); |
| 1853 | if (FoldedVOp.getNode()) return FoldedVOp; |
| 1854 | |
| 1855 | N0IsConst = isConstantSplatVector(N0.getNode(), ConstValue0); |
| 1856 | N1IsConst = isConstantSplatVector(N1.getNode(), ConstValue1); |
| 1857 | } else { |
| 1858 | N0IsConst = dyn_cast<ConstantSDNode>(N0) != 0; |
| Jack Carter | d4e9615 | 2013-10-17 01:34:33 +0000 | [diff] [blame] | 1859 | ConstValue0 = N0IsConst ? (dyn_cast<ConstantSDNode>(N0))->getAPIntValue() |
| 1860 | : APInt(); |
| Elena Demikhovsky | 6769c50 | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1861 | N1IsConst = dyn_cast<ConstantSDNode>(N1) != 0; |
| Jack Carter | d4e9615 | 2013-10-17 01:34:33 +0000 | [diff] [blame] | 1862 | ConstValue1 = N1IsConst ? (dyn_cast<ConstantSDNode>(N1))->getAPIntValue() |
| 1863 | : APInt(); |
| Elena Demikhovsky | 6769c50 | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1866 | // fold (mul c1, c2) -> c1*c2 |
| Elena Demikhovsky | 6769c50 | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1867 | if (N0IsConst && N1IsConst) |
| 1868 | return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0.getNode(), N1.getNode()); |
| 1869 | |
| Nate Begeman | 2cc2c9a | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1870 | // canonicalize constant to RHS |
| Elena Demikhovsky | 6769c50 | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1871 | if (N0IsConst && !N1IsConst) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1872 | return DAG.getNode(ISD::MUL, SDLoc(N), VT, N1, N0); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1873 | // fold (mul x, 0) -> 0 |
| Elena Demikhovsky | 6769c50 | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1874 | if (N1IsConst && ConstValue1 == 0) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1875 | return N1; |
| Benjamin Kramer | d443e4a | 2013-09-19 13:28:20 +0000 | [diff] [blame] | 1876 | // We require a splat of the entire scalar bit width for non-contiguous |
| 1877 | // bit patterns. |
| 1878 | bool IsFullSplat = |
| 1879 | ConstValue1.getBitWidth() == VT.getScalarType().getSizeInBits(); |
| Elena Demikhovsky | 6769c50 | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1880 | // fold (mul x, 1) -> x |
| Benjamin Kramer | d443e4a | 2013-09-19 13:28:20 +0000 | [diff] [blame] | 1881 | if (N1IsConst && ConstValue1 == 1 && IsFullSplat) |
| Elena Demikhovsky | 6769c50 | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1882 | return N0; |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1883 | // fold (mul x, -1) -> 0-x |
| Elena Demikhovsky | 6769c50 | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1884 | if (N1IsConst && ConstValue1.isAllOnesValue()) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1885 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, |
| Bill Wendling | 091f92f | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1886 | DAG.getConstant(0, VT), N0); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1887 | // fold (mul x, (1 << c)) -> x << c |
| Benjamin Kramer | d443e4a | 2013-09-19 13:28:20 +0000 | [diff] [blame] | 1888 | if (N1IsConst && ConstValue1.isPowerOf2() && IsFullSplat) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1889 | return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0, |
| Elena Demikhovsky | 6769c50 | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1890 | DAG.getConstant(ConstValue1.logBase2(), |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1891 | getShiftAmountTy(N0.getValueType()))); |
| Chris Lattner | a70878d | 2005-10-30 06:41:49 +0000 | [diff] [blame] | 1892 | // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c |
| Benjamin Kramer | d443e4a | 2013-09-19 13:28:20 +0000 | [diff] [blame] | 1893 | if (N1IsConst && (-ConstValue1).isPowerOf2() && IsFullSplat) { |
| Elena Demikhovsky | 6769c50 | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1894 | unsigned Log2Val = (-ConstValue1).logBase2(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1895 | // FIXME: If the input is something that is easily negated (e.g. a |
| Chris Lattner | a70878d | 2005-10-30 06:41:49 +0000 | [diff] [blame] | 1896 | // single-use add), we should put the negate there. |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1897 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, |
| Bill Wendling | 091f92f | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1898 | DAG.getConstant(0, VT), |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1899 | DAG.getNode(ISD::SHL, SDLoc(N), VT, N0, |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1900 | DAG.getConstant(Log2Val, |
| 1901 | getShiftAmountTy(N0.getValueType())))); |
| Chris Lattner | 4249b9a | 2009-03-09 20:22:18 +0000 | [diff] [blame] | 1902 | } |
| Elena Demikhovsky | 6769c50 | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1903 | |
| 1904 | APInt Val; |
| Chris Lattner | 324871e | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1905 | // (mul (shl X, c1), c2) -> (mul X, c2 << c1) |
| Stephen Lin | cfe7f35 | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 1906 | if (N1IsConst && N0.getOpcode() == ISD::SHL && |
| Elena Demikhovsky | 6769c50 | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1907 | (isConstantSplatVector(N0.getOperand(1).getNode(), Val) || |
| 1908 | isa<ConstantSDNode>(N0.getOperand(1)))) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1909 | SDValue C3 = DAG.getNode(ISD::SHL, SDLoc(N), VT, |
| Bill Wendling | 091f92f | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1910 | N1, N0.getOperand(1)); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1911 | AddToWorkList(C3.getNode()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1912 | return DAG.getNode(ISD::MUL, SDLoc(N), VT, |
| Bill Wendling | 091f92f | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1913 | N0.getOperand(0), C3); |
| Chris Lattner | 324871e | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1914 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1915 | |
| Chris Lattner | 324871e | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1916 | // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one |
| 1917 | // use. |
| 1918 | { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1919 | SDValue Sh(0,0), Y(0,0); |
| Chris Lattner | 324871e | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1920 | // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)). |
| Stephen Lin | cfe7f35 | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 1921 | if (N0.getOpcode() == ISD::SHL && |
| Elena Demikhovsky | 6769c50 | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1922 | (isConstantSplatVector(N0.getOperand(1).getNode(), Val) || |
| 1923 | isa<ConstantSDNode>(N0.getOperand(1))) && |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1924 | N0.getNode()->hasOneUse()) { |
| Chris Lattner | 324871e | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1925 | Sh = N0; Y = N1; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1926 | } else if (N1.getOpcode() == ISD::SHL && |
| Gabor Greif | e12264b | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 1927 | isa<ConstantSDNode>(N1.getOperand(1)) && |
| 1928 | N1.getNode()->hasOneUse()) { |
| Chris Lattner | 324871e | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1929 | Sh = N1; Y = N0; |
| 1930 | } |
| Bill Wendling | b48dcf6 | 2009-01-30 02:49:26 +0000 | [diff] [blame] | 1931 | |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1932 | if (Sh.getNode()) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1933 | SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT, |
| Bill Wendling | 091f92f | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1934 | Sh.getOperand(0), Y); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1935 | return DAG.getNode(ISD::SHL, SDLoc(N), VT, |
| Bill Wendling | 091f92f | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1936 | Mul, Sh.getOperand(1)); |
| Chris Lattner | 324871e | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1937 | } |
| 1938 | } |
| Bill Wendling | b48dcf6 | 2009-01-30 02:49:26 +0000 | [diff] [blame] | 1939 | |
| Chris Lattner | f29f520 | 2006-03-04 23:33:26 +0000 | [diff] [blame] | 1940 | // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2) |
| Elena Demikhovsky | 6769c50 | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1941 | if (N1IsConst && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() && |
| 1942 | (isConstantSplatVector(N0.getOperand(1).getNode(), Val) || |
| 1943 | isa<ConstantSDNode>(N0.getOperand(1)))) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1944 | return DAG.getNode(ISD::ADD, SDLoc(N), VT, |
| 1945 | DAG.getNode(ISD::MUL, SDLoc(N0), VT, |
| Bill Wendling | 091f92f | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1946 | N0.getOperand(0), N1), |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1947 | DAG.getNode(ISD::MUL, SDLoc(N1), VT, |
| Bill Wendling | 091f92f | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1948 | N0.getOperand(1), N1)); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1949 | |
| Nate Begeman | 22e251a | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1950 | // reassociate mul |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1951 | SDValue RMUL = ReassociateOps(ISD::MUL, SDLoc(N), N0, N1); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1952 | if (RMUL.getNode() != 0) |
| Nate Begeman | 22e251a | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1953 | return RMUL; |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1954 | |
| Evan Cheng | f100557 | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1955 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1958 | SDValue DAGCombiner::visitSDIV(SDNode *N) { |
| 1959 | SDValue N0 = N->getOperand(0); |
| 1960 | SDValue N1 = N->getOperand(1); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1961 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode()); |
| 1962 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1963 | EVT VT = N->getValueType(0); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1964 | |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1965 | // fold vector ops |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1966 | if (VT.isVector()) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1967 | SDValue FoldedVOp = SimplifyVBinOp(N); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1968 | if (FoldedVOp.getNode()) return FoldedVOp; |
| Dan Gohman | 80f9f07 | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 1969 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1970 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1971 | // fold (sdiv c1, c2) -> c1/c2 |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1972 | if (N0C && N1C && !N1C->isNullValue()) |
| Bill Wendling | dea9130 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1973 | return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C); |
| Nate Begeman | 4dd3831 | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1974 | // fold (sdiv X, 1) -> X |
| Eli Friedman | e9e356a | 2011-10-27 02:06:39 +0000 | [diff] [blame] | 1975 | if (N1C && N1C->getAPIntValue() == 1LL) |
| Nate Begeman | 4dd3831 | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1976 | return N0; |
| 1977 | // fold (sdiv X, -1) -> 0-X |
| 1978 | if (N1C && N1C->isAllOnesValue()) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1979 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, |
| Bill Wendling | 5b663e7 | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1980 | DAG.getConstant(0, VT), N0); |
| Chris Lattner | 5bcd0dd8 | 2005-10-07 06:10:46 +0000 | [diff] [blame] | 1981 | // If we know the sign bits of both operands are zero, strength reduce to a |
| 1982 | // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2 |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1983 | if (!VT.isVector()) { |
| Dan Gohman | 1f372ed | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 1984 | if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1985 | return DAG.getNode(ISD::UDIV, SDLoc(N), N1.getValueType(), |
| Bill Wendling | 5b663e7 | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1986 | N0, N1); |
| Chris Lattner | 2ee91f4 | 2008-01-27 23:32:17 +0000 | [diff] [blame] | 1987 | } |
| Nate Begeman | 57b3567 | 2006-02-17 07:26:20 +0000 | [diff] [blame] | 1988 | // fold (sdiv X, pow2) -> simple ops after legalize |
| Eli Friedman | f9081a8 | 2011-12-07 03:55:52 +0000 | [diff] [blame] | 1989 | if (N1C && !N1C->isNullValue() && |
| Eli Friedman | e9e356a | 2011-10-27 02:06:39 +0000 | [diff] [blame] | 1990 | (N1C->getAPIntValue().isPowerOf2() || |
| 1991 | (-N1C->getAPIntValue()).isPowerOf2())) { |
| Nate Begeman | 4dd3831 | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1992 | // If dividing by powers of two is cheap, then don't perform the following |
| 1993 | // fold. |
| 1994 | if (TLI.isPow2DivCheap()) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1995 | return SDValue(); |
| Bill Wendling | 5b663e7 | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1996 | |
| Eli Friedman | e9e356a | 2011-10-27 02:06:39 +0000 | [diff] [blame] | 1997 | unsigned lg2 = N1C->getAPIntValue().countTrailingZeros(); |
| Bill Wendling | 5b663e7 | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1998 | |
| Chris Lattner | 471627c | 2006-02-16 08:02:36 +0000 | [diff] [blame] | 1999 | // Splat the sign bit into the register |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2000 | SDValue SGN = DAG.getNode(ISD::SRA, SDLoc(N), VT, N0, |
| Bill Wendling | 5b663e7 | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 2001 | DAG.getConstant(VT.getSizeInBits()-1, |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2002 | getShiftAmountTy(N0.getValueType()))); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2003 | AddToWorkList(SGN.getNode()); |
| Bill Wendling | 5b663e7 | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 2004 | |
| Chris Lattner | 471627c | 2006-02-16 08:02:36 +0000 | [diff] [blame] | 2005 | // Add (N0 < 0) ? abs2 - 1 : 0; |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2006 | SDValue SRL = DAG.getNode(ISD::SRL, SDLoc(N), VT, SGN, |
| Bill Wendling | 5b663e7 | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 2007 | DAG.getConstant(VT.getSizeInBits() - lg2, |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2008 | getShiftAmountTy(SGN.getValueType()))); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2009 | SDValue ADD = DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, SRL); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2010 | AddToWorkList(SRL.getNode()); |
| 2011 | AddToWorkList(ADD.getNode()); // Divide by pow2 |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2012 | SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), VT, ADD, |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2013 | DAG.getConstant(lg2, getShiftAmountTy(ADD.getValueType()))); |
| Bill Wendling | 5b663e7 | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 2014 | |
| Nate Begeman | 4dd3831 | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 2015 | // If we're dividing by a positive value, we're done. Otherwise, we must |
| 2016 | // negate the result. |
| Eli Friedman | e9e356a | 2011-10-27 02:06:39 +0000 | [diff] [blame] | 2017 | if (N1C->getAPIntValue().isNonNegative()) |
| Nate Begeman | 4dd3831 | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 2018 | return SRA; |
| Bill Wendling | 5b663e7 | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 2019 | |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2020 | AddToWorkList(SRA.getNode()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2021 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, |
| Bill Wendling | 5b663e7 | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 2022 | DAG.getConstant(0, VT), SRA); |
| Nate Begeman | 4dd3831 | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 2023 | } |
| Bill Wendling | 5b663e7 | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 2024 | |
| Nate Begeman | c6f067a | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 2025 | // if integer divide is expensive and we satisfy the requirements, emit an |
| 2026 | // alternate sequence. |
| Eli Friedman | e9e356a | 2011-10-27 02:06:39 +0000 | [diff] [blame] | 2027 | if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2028 | SDValue Op = BuildSDIV(N); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2029 | if (Op.getNode()) return Op; |
| Nate Begeman | c6f067a | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 2030 | } |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2031 | |
| Dan Gohman | 06563a8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2032 | // undef / X -> 0 |
| 2033 | if (N0.getOpcode() == ISD::UNDEF) |
| 2034 | return DAG.getConstant(0, VT); |
| 2035 | // X / undef -> undef |
| 2036 | if (N1.getOpcode() == ISD::UNDEF) |
| 2037 | return N1; |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2038 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2039 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2042 | SDValue DAGCombiner::visitUDIV(SDNode *N) { |
| 2043 | SDValue N0 = N->getOperand(0); |
| 2044 | SDValue N1 = N->getOperand(1); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2045 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode()); |
| 2046 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2047 | EVT VT = N->getValueType(0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2048 | |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2049 | // fold vector ops |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2050 | if (VT.isVector()) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2051 | SDValue FoldedVOp = SimplifyVBinOp(N); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2052 | if (FoldedVOp.getNode()) return FoldedVOp; |
| Dan Gohman | 80f9f07 | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 2053 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2054 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2055 | // fold (udiv c1, c2) -> c1/c2 |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2056 | if (N0C && N1C && !N1C->isNullValue()) |
| Bill Wendling | dea9130 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 2057 | return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2058 | // fold (udiv x, (1 << c)) -> x >>u c |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2059 | if (N1C && N1C->getAPIntValue().isPowerOf2()) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2060 | return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2061 | DAG.getConstant(N1C->getAPIntValue().logBase2(), |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2062 | getShiftAmountTy(N0.getValueType()))); |
| Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2063 | // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2 |
| Nate Begeman | 25d178b | 2006-02-05 07:20:23 +0000 | [diff] [blame] | 2064 | if (N1.getOpcode() == ISD::SHL) { |
| 2065 | if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) { |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2066 | if (SHC->getAPIntValue().isPowerOf2()) { |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2067 | EVT ADDVT = N1.getOperand(1).getValueType(); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2068 | SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N), ADDVT, |
| Bill Wendling | aff3e03 | 2009-01-30 02:55:25 +0000 | [diff] [blame] | 2069 | N1.getOperand(1), |
| 2070 | DAG.getConstant(SHC->getAPIntValue() |
| 2071 | .logBase2(), |
| 2072 | ADDVT)); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2073 | AddToWorkList(Add.getNode()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2074 | return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, Add); |
| Nate Begeman | 25d178b | 2006-02-05 07:20:23 +0000 | [diff] [blame] | 2075 | } |
| 2076 | } |
| 2077 | } |
| Nate Begeman | c6f067a | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 2078 | // fold (udiv x, c) -> alternate |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2079 | if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2080 | SDValue Op = BuildUDIV(N); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2081 | if (Op.getNode()) return Op; |
| Chris Lattner | 9faa5b7 | 2005-10-22 18:50:15 +0000 | [diff] [blame] | 2082 | } |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2083 | |
| Dan Gohman | 06563a8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2084 | // undef / X -> 0 |
| 2085 | if (N0.getOpcode() == ISD::UNDEF) |
| 2086 | return DAG.getConstant(0, VT); |
| 2087 | // X / undef -> undef |
| 2088 | if (N1.getOpcode() == ISD::UNDEF) |
| 2089 | return N1; |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2090 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2091 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2094 | SDValue DAGCombiner::visitSREM(SDNode *N) { |
| 2095 | SDValue N0 = N->getOperand(0); |
| 2096 | SDValue N1 = N->getOperand(1); |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2097 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 2098 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2099 | EVT VT = N->getValueType(0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2100 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2101 | // fold (srem c1, c2) -> c1%c2 |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2102 | if (N0C && N1C && !N1C->isNullValue()) |
| Bill Wendling | dea9130 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 2103 | return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C); |
| Nate Begeman | 6828ed9 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 2104 | // If we know the sign bits of both operands are zero, strength reduce to a |
| 2105 | // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15 |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2106 | if (!VT.isVector()) { |
| Dan Gohman | 1f372ed | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2107 | if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2108 | return DAG.getNode(ISD::UREM, SDLoc(N), VT, N0, N1); |
| Chris Lattner | d0496d0 | 2008-01-27 23:21:58 +0000 | [diff] [blame] | 2109 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2110 | |
| Dan Gohman | 9a69341 | 2007-11-26 23:46:11 +0000 | [diff] [blame] | 2111 | // If X/C can be simplified by the division-by-constant logic, lower |
| 2112 | // X%C to the equivalent of X-X/C*C. |
| Chris Lattner | d0620d2 | 2006-10-12 20:58:32 +0000 | [diff] [blame] | 2113 | if (N1C && !N1C->isNullValue()) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2114 | SDValue Div = DAG.getNode(ISD::SDIV, SDLoc(N), VT, N0, N1); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2115 | AddToWorkList(Div.getNode()); |
| 2116 | SDValue OptimizedDiv = combine(Div.getNode()); |
| 2117 | if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2118 | SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT, |
| Bill Wendling | d033af0 | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 2119 | OptimizedDiv, N1); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2120 | SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2121 | AddToWorkList(Mul.getNode()); |
| Dan Gohman | 9a69341 | 2007-11-26 23:46:11 +0000 | [diff] [blame] | 2122 | return Sub; |
| 2123 | } |
| Chris Lattner | d0620d2 | 2006-10-12 20:58:32 +0000 | [diff] [blame] | 2124 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2125 | |
| Dan Gohman | 06563a8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2126 | // undef % X -> 0 |
| 2127 | if (N0.getOpcode() == ISD::UNDEF) |
| 2128 | return DAG.getConstant(0, VT); |
| 2129 | // X % undef -> undef |
| 2130 | if (N1.getOpcode() == ISD::UNDEF) |
| 2131 | return N1; |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2132 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2133 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2134 | } |
| 2135 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2136 | SDValue DAGCombiner::visitUREM(SDNode *N) { |
| 2137 | SDValue N0 = N->getOperand(0); |
| 2138 | SDValue N1 = N->getOperand(1); |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2139 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 2140 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2141 | EVT VT = N->getValueType(0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2142 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2143 | // fold (urem c1, c2) -> c1%c2 |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2144 | if (N0C && N1C && !N1C->isNullValue()) |
| Bill Wendling | dea9130 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 2145 | return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C); |
| Nate Begeman | 6828ed9 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 2146 | // fold (urem x, pow2) -> (and x, pow2-1) |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2147 | if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2()) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2148 | return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2149 | DAG.getConstant(N1C->getAPIntValue()-1,VT)); |
| Nate Begeman | c89fdf1 | 2006-02-05 07:36:48 +0000 | [diff] [blame] | 2150 | // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1)) |
| 2151 | if (N1.getOpcode() == ISD::SHL) { |
| 2152 | if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) { |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2153 | if (SHC->getAPIntValue().isPowerOf2()) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2154 | SDValue Add = |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2155 | DAG.getNode(ISD::ADD, SDLoc(N), VT, N1, |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2156 | DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2157 | VT)); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2158 | AddToWorkList(Add.getNode()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2159 | return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, Add); |
| Nate Begeman | c89fdf1 | 2006-02-05 07:36:48 +0000 | [diff] [blame] | 2160 | } |
| 2161 | } |
| 2162 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2163 | |
| Dan Gohman | 9a69341 | 2007-11-26 23:46:11 +0000 | [diff] [blame] | 2164 | // If X/C can be simplified by the division-by-constant logic, lower |
| 2165 | // X%C to the equivalent of X-X/C*C. |
| Chris Lattner | d0620d2 | 2006-10-12 20:58:32 +0000 | [diff] [blame] | 2166 | if (N1C && !N1C->isNullValue()) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2167 | SDValue Div = DAG.getNode(ISD::UDIV, SDLoc(N), VT, N0, N1); |
| Dan Gohman | 1df80f6 | 2008-09-08 16:59:01 +0000 | [diff] [blame] | 2168 | AddToWorkList(Div.getNode()); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2169 | SDValue OptimizedDiv = combine(Div.getNode()); |
| 2170 | if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2171 | SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT, |
| Bill Wendling | d033af0 | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 2172 | OptimizedDiv, N1); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2173 | SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2174 | AddToWorkList(Mul.getNode()); |
| Dan Gohman | 9a69341 | 2007-11-26 23:46:11 +0000 | [diff] [blame] | 2175 | return Sub; |
| 2176 | } |
| Chris Lattner | d0620d2 | 2006-10-12 20:58:32 +0000 | [diff] [blame] | 2177 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2178 | |
| Dan Gohman | 06563a8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2179 | // undef % X -> 0 |
| 2180 | if (N0.getOpcode() == ISD::UNDEF) |
| 2181 | return DAG.getConstant(0, VT); |
| 2182 | // X % undef -> undef |
| 2183 | if (N1.getOpcode() == ISD::UNDEF) |
| 2184 | return N1; |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2185 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2186 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2189 | SDValue DAGCombiner::visitMULHS(SDNode *N) { |
| 2190 | SDValue N0 = N->getOperand(0); |
| 2191 | SDValue N1 = N->getOperand(1); |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2192 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2193 | EVT VT = N->getValueType(0); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2194 | SDLoc DL(N); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2195 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2196 | // fold (mulhs x, 0) -> 0 |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2197 | if (N1C && N1C->isNullValue()) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2198 | return N1; |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2199 | // fold (mulhs x, 1) -> (sra x, size(x)-1) |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2200 | if (N1C && N1C->getAPIntValue() == 1) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2201 | return DAG.getNode(ISD::SRA, SDLoc(N), N0.getValueType(), N0, |
| Bill Wendling | faed065 | 2009-01-30 03:00:18 +0000 | [diff] [blame] | 2202 | DAG.getConstant(N0.getValueType().getSizeInBits() - 1, |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2203 | getShiftAmountTy(N0.getValueType()))); |
| Dan Gohman | 06563a8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2204 | // fold (mulhs x, undef) -> 0 |
| Dan Gohman | fa91282 | 2007-07-10 14:20:37 +0000 | [diff] [blame] | 2205 | if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) |
| Dan Gohman | 06563a8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2206 | return DAG.getConstant(0, VT); |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2207 | |
| Chris Lattner | 10bd29f | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 2208 | // If the type twice as wide is legal, transform the mulhs to a wider multiply |
| 2209 | // plus a shift. |
| 2210 | if (VT.isSimple() && !VT.isVector()) { |
| 2211 | MVT Simple = VT.getSimpleVT(); |
| 2212 | unsigned SimpleSize = Simple.getSizeInBits(); |
| 2213 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2); |
| 2214 | if (TLI.isOperationLegal(ISD::MUL, NewVT)) { |
| 2215 | N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0); |
| 2216 | N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1); |
| 2217 | N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1); |
| Chris Lattner | b86dcee | 2010-12-15 05:51:39 +0000 | [diff] [blame] | 2218 | N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1, |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2219 | DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType()))); |
| Chris Lattner | 10bd29f | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 2220 | return DAG.getNode(ISD::TRUNCATE, DL, VT, N1); |
| 2221 | } |
| 2222 | } |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2223 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2224 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2225 | } |
| 2226 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2227 | SDValue DAGCombiner::visitMULHU(SDNode *N) { |
| 2228 | SDValue N0 = N->getOperand(0); |
| 2229 | SDValue N1 = N->getOperand(1); |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2230 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2231 | EVT VT = N->getValueType(0); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2232 | SDLoc DL(N); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2233 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2234 | // fold (mulhu x, 0) -> 0 |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2235 | if (N1C && N1C->isNullValue()) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2236 | return N1; |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2237 | // fold (mulhu x, 1) -> 0 |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2238 | if (N1C && N1C->getAPIntValue() == 1) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2239 | return DAG.getConstant(0, N0.getValueType()); |
| Dan Gohman | 06563a8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2240 | // fold (mulhu x, undef) -> 0 |
| Dan Gohman | fa91282 | 2007-07-10 14:20:37 +0000 | [diff] [blame] | 2241 | if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) |
| Dan Gohman | 06563a8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2242 | return DAG.getConstant(0, VT); |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2243 | |
| Chris Lattner | 10bd29f | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 2244 | // If the type twice as wide is legal, transform the mulhu to a wider multiply |
| 2245 | // plus a shift. |
| 2246 | if (VT.isSimple() && !VT.isVector()) { |
| 2247 | MVT Simple = VT.getSimpleVT(); |
| 2248 | unsigned SimpleSize = Simple.getSizeInBits(); |
| 2249 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2); |
| 2250 | if (TLI.isOperationLegal(ISD::MUL, NewVT)) { |
| 2251 | N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0); |
| 2252 | N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1); |
| 2253 | N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1); |
| 2254 | N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1, |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2255 | DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType()))); |
| Chris Lattner | 10bd29f | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 2256 | return DAG.getNode(ISD::TRUNCATE, DL, VT, N1); |
| 2257 | } |
| 2258 | } |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2259 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2260 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2261 | } |
| 2262 | |
| Dan Gohman | 5c6d0c3 | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2263 | /// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that |
| 2264 | /// compute two values. LoOp and HiOp give the opcodes for the two computations |
| 2265 | /// that are being performed. Return true if a simplification was made. |
| 2266 | /// |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2267 | SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp, |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2268 | unsigned HiOp) { |
| Dan Gohman | 5c6d0c3 | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2269 | // If the high half is not needed, just compute the low half. |
| Evan Cheng | ece4c68 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2270 | bool HiExists = N->hasAnyUseOfValue(1); |
| 2271 | if (!HiExists && |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2272 | (!LegalOperations || |
| Owen Anderson | fb00d5b | 2014-01-20 18:41:34 +0000 | [diff] [blame] | 2273 | TLI.isOperationLegalOrCustom(LoOp, N->getValueType(0)))) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2274 | SDValue Res = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0), |
| Bill Wendling | 9b3407e | 2009-01-30 03:08:40 +0000 | [diff] [blame] | 2275 | N->op_begin(), N->getNumOperands()); |
| Chris Lattner | 31e9edc | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 2276 | return CombineTo(N, Res, Res); |
| Dan Gohman | 5c6d0c3 | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | // If the low half is not needed, just compute the high half. |
| Evan Cheng | ece4c68 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2280 | bool LoExists = N->hasAnyUseOfValue(0); |
| 2281 | if (!LoExists && |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2282 | (!LegalOperations || |
| Dan Gohman | 5c6d0c3 | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2283 | TLI.isOperationLegal(HiOp, N->getValueType(1)))) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2284 | SDValue Res = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1), |
| Bill Wendling | 9b3407e | 2009-01-30 03:08:40 +0000 | [diff] [blame] | 2285 | N->op_begin(), N->getNumOperands()); |
| Chris Lattner | 31e9edc | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 2286 | return CombineTo(N, Res, Res); |
| Dan Gohman | 5c6d0c3 | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
| Evan Cheng | ece4c68 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2289 | // If both halves are used, return as it is. |
| 2290 | if (LoExists && HiExists) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2291 | return SDValue(); |
| Evan Cheng | ece4c68 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2292 | |
| 2293 | // If the two computed results can be simplified separately, separate them. |
| Evan Cheng | ece4c68 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2294 | if (LoExists) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2295 | SDValue Lo = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0), |
| Bill Wendling | 9b3407e | 2009-01-30 03:08:40 +0000 | [diff] [blame] | 2296 | N->op_begin(), N->getNumOperands()); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2297 | AddToWorkList(Lo.getNode()); |
| 2298 | SDValue LoOpt = combine(Lo.getNode()); |
| 2299 | if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() && |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2300 | (!LegalOperations || |
| Duncan Sands | 8651e9c | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 2301 | TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType()))) |
| Chris Lattner | 31e9edc | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 2302 | return CombineTo(N, LoOpt, LoOpt); |
| Dan Gohman | 5c6d0c3 | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2303 | } |
| 2304 | |
| Evan Cheng | ece4c68 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2305 | if (HiExists) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2306 | SDValue Hi = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1), |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2307 | N->op_begin(), N->getNumOperands()); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2308 | AddToWorkList(Hi.getNode()); |
| 2309 | SDValue HiOpt = combine(Hi.getNode()); |
| 2310 | if (HiOpt.getNode() && HiOpt != Hi && |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2311 | (!LegalOperations || |
| Duncan Sands | 8651e9c | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 2312 | TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType()))) |
| Chris Lattner | 31e9edc | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 2313 | return CombineTo(N, HiOpt, HiOpt); |
| Evan Cheng | ece4c68 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2314 | } |
| Bill Wendling | 9b3407e | 2009-01-30 03:08:40 +0000 | [diff] [blame] | 2315 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2316 | return SDValue(); |
| Dan Gohman | 5c6d0c3 | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2319 | SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) { |
| 2320 | SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2321 | if (Res.getNode()) return Res; |
| Dan Gohman | 5c6d0c3 | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2322 | |
| Chris Lattner | 15090e1 | 2010-12-15 06:04:19 +0000 | [diff] [blame] | 2323 | EVT VT = N->getValueType(0); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2324 | SDLoc DL(N); |
| Chris Lattner | 15090e1 | 2010-12-15 06:04:19 +0000 | [diff] [blame] | 2325 | |
| 2326 | // If the type twice as wide is legal, transform the mulhu to a wider multiply |
| 2327 | // plus a shift. |
| 2328 | if (VT.isSimple() && !VT.isVector()) { |
| 2329 | MVT Simple = VT.getSimpleVT(); |
| 2330 | unsigned SimpleSize = Simple.getSizeInBits(); |
| 2331 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2); |
| 2332 | if (TLI.isOperationLegal(ISD::MUL, NewVT)) { |
| 2333 | SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0)); |
| 2334 | SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1)); |
| 2335 | Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi); |
| 2336 | // Compute the high part as N1. |
| 2337 | Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo, |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2338 | DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType()))); |
| Chris Lattner | 15090e1 | 2010-12-15 06:04:19 +0000 | [diff] [blame] | 2339 | Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi); |
| 2340 | // Compute the low part as N0. |
| 2341 | Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo); |
| 2342 | return CombineTo(N, Lo, Hi); |
| 2343 | } |
| 2344 | } |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2345 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2346 | return SDValue(); |
| Dan Gohman | 5c6d0c3 | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2349 | SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) { |
| 2350 | SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2351 | if (Res.getNode()) return Res; |
| Dan Gohman | 5c6d0c3 | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2352 | |
| Chris Lattner | 15090e1 | 2010-12-15 06:04:19 +0000 | [diff] [blame] | 2353 | EVT VT = N->getValueType(0); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2354 | SDLoc DL(N); |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2355 | |
| Chris Lattner | 15090e1 | 2010-12-15 06:04:19 +0000 | [diff] [blame] | 2356 | // If the type twice as wide is legal, transform the mulhu to a wider multiply |
| 2357 | // plus a shift. |
| 2358 | if (VT.isSimple() && !VT.isVector()) { |
| 2359 | MVT Simple = VT.getSimpleVT(); |
| 2360 | unsigned SimpleSize = Simple.getSizeInBits(); |
| 2361 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2); |
| 2362 | if (TLI.isOperationLegal(ISD::MUL, NewVT)) { |
| 2363 | SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0)); |
| 2364 | SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1)); |
| 2365 | Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi); |
| 2366 | // Compute the high part as N1. |
| 2367 | Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo, |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2368 | DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType()))); |
| Chris Lattner | 15090e1 | 2010-12-15 06:04:19 +0000 | [diff] [blame] | 2369 | Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi); |
| 2370 | // Compute the low part as N0. |
| 2371 | Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo); |
| 2372 | return CombineTo(N, Lo, Hi); |
| 2373 | } |
| 2374 | } |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2375 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2376 | return SDValue(); |
| Dan Gohman | 5c6d0c3 | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
| Benjamin Kramer | 2fd48f2 | 2011-05-21 18:31:55 +0000 | [diff] [blame] | 2379 | SDValue DAGCombiner::visitSMULO(SDNode *N) { |
| 2380 | // (smulo x, 2) -> (saddo x, x) |
| 2381 | if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1))) |
| 2382 | if (C2->getAPIntValue() == 2) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2383 | return DAG.getNode(ISD::SADDO, SDLoc(N), N->getVTList(), |
| Benjamin Kramer | 2fd48f2 | 2011-05-21 18:31:55 +0000 | [diff] [blame] | 2384 | N->getOperand(0), N->getOperand(0)); |
| 2385 | |
| 2386 | return SDValue(); |
| 2387 | } |
| 2388 | |
| 2389 | SDValue DAGCombiner::visitUMULO(SDNode *N) { |
| 2390 | // (umulo x, 2) -> (uaddo x, x) |
| 2391 | if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1))) |
| 2392 | if (C2->getAPIntValue() == 2) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2393 | return DAG.getNode(ISD::UADDO, SDLoc(N), N->getVTList(), |
| Benjamin Kramer | 2fd48f2 | 2011-05-21 18:31:55 +0000 | [diff] [blame] | 2394 | N->getOperand(0), N->getOperand(0)); |
| 2395 | |
| 2396 | return SDValue(); |
| 2397 | } |
| 2398 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2399 | SDValue DAGCombiner::visitSDIVREM(SDNode *N) { |
| 2400 | SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2401 | if (Res.getNode()) return Res; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2402 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2403 | return SDValue(); |
| Dan Gohman | 5c6d0c3 | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2404 | } |
| 2405 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2406 | SDValue DAGCombiner::visitUDIVREM(SDNode *N) { |
| 2407 | SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2408 | if (Res.getNode()) return Res; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2409 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2410 | return SDValue(); |
| Dan Gohman | 5c6d0c3 | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
| Chris Lattner | 8d6fc20 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2413 | /// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with |
| 2414 | /// two operands of the same opcode, try to simplify it. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2415 | SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) { |
| 2416 | SDValue N0 = N->getOperand(0), N1 = N->getOperand(1); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2417 | EVT VT = N0.getValueType(); |
| Chris Lattner | 8d6fc20 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2418 | assert(N0.getOpcode() == N1.getOpcode() && "Bad input!"); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2419 | |
| Dan Gohman | dd5286d | 2010-01-14 03:08:49 +0000 | [diff] [blame] | 2420 | // Bail early if none of these transforms apply. |
| 2421 | if (N0.getNode()->getNumOperands() == 0) return SDValue(); |
| 2422 | |
| Chris Lattner | 002ee91 | 2006-05-05 06:31:05 +0000 | [diff] [blame] | 2423 | // For each of OP in AND/OR/XOR: |
| 2424 | // fold (OP (zext x), (zext y)) -> (zext (OP x, y)) |
| 2425 | // fold (OP (sext x), (sext y)) -> (sext (OP x, y)) |
| 2426 | // fold (OP (aext x), (aext y)) -> (aext (OP x, y)) |
| Dan Gohman | 600f62b | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 2427 | // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free) |
| Nate Begeman | 9655f84 | 2009-12-03 07:11:29 +0000 | [diff] [blame] | 2428 | // |
| 2429 | // do not sink logical op inside of a vector extend, since it may combine |
| 2430 | // into a vsetcc. |
| Evan Cheng | 166a4e6 | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2431 | EVT Op0VT = N0.getOperand(0).getValueType(); |
| 2432 | if ((N0.getOpcode() == ISD::ZERO_EXTEND || |
| Dan Gohman | ad3e549 | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 2433 | N0.getOpcode() == ISD::SIGN_EXTEND || |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 2434 | // Avoid infinite looping with PromoteIntBinOp. |
| 2435 | (N0.getOpcode() == ISD::ANY_EXTEND && |
| 2436 | (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) || |
| Dan Gohman | 600f62b | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 2437 | (N0.getOpcode() == ISD::TRUNCATE && |
| 2438 | (!TLI.isZExtFree(VT, Op0VT) || |
| 2439 | !TLI.isTruncateFree(Op0VT, VT)) && |
| 2440 | TLI.isTypeLegal(Op0VT))) && |
| Nate Begeman | 9655f84 | 2009-12-03 07:11:29 +0000 | [diff] [blame] | 2441 | !VT.isVector() && |
| Evan Cheng | 166a4e6 | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2442 | Op0VT == N1.getOperand(0).getValueType() && |
| 2443 | (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2444 | SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0), |
| Bill Wendling | 781db7a | 2009-01-30 19:25:47 +0000 | [diff] [blame] | 2445 | N0.getOperand(0).getValueType(), |
| 2446 | N0.getOperand(0), N1.getOperand(0)); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2447 | AddToWorkList(ORNode.getNode()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2448 | return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, ORNode); |
| Chris Lattner | 8d6fc20 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2449 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2450 | |
| Chris Lattner | 5ac4293 | 2006-05-05 06:10:43 +0000 | [diff] [blame] | 2451 | // For each of OP in SHL/SRL/SRA/AND... |
| 2452 | // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z) |
| 2453 | // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z) |
| 2454 | // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z) |
| Chris Lattner | 8d6fc20 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2455 | if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL || |
| Chris Lattner | 5ac4293 | 2006-05-05 06:10:43 +0000 | [diff] [blame] | 2456 | N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) && |
| Chris Lattner | 8d6fc20 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2457 | N0.getOperand(1) == N1.getOperand(1)) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2458 | SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0), |
| Bill Wendling | 781db7a | 2009-01-30 19:25:47 +0000 | [diff] [blame] | 2459 | N0.getOperand(0).getValueType(), |
| 2460 | N0.getOperand(0), N1.getOperand(0)); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2461 | AddToWorkList(ORNode.getNode()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2462 | return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, |
| Bill Wendling | 781db7a | 2009-01-30 19:25:47 +0000 | [diff] [blame] | 2463 | ORNode, N0.getOperand(1)); |
| Chris Lattner | 8d6fc20 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2464 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2465 | |
| Nadav Rotem | b078350 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2466 | // Simplify xor/and/or (bitcast(A), bitcast(B)) -> bitcast(op (A,B)) |
| 2467 | // Only perform this optimization after type legalization and before |
| 2468 | // LegalizeVectorOprs. LegalizeVectorOprs promotes vector operations by |
| 2469 | // adding bitcasts. For example (xor v4i32) is promoted to (v2i64), and |
| 2470 | // we don't want to undo this promotion. |
| 2471 | // We also handle SCALAR_TO_VECTOR because xor/or/and operations are cheaper |
| 2472 | // on scalars. |
| Nadav Rotem | 841c9a8 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 2473 | if ((N0.getOpcode() == ISD::BITCAST || |
| 2474 | N0.getOpcode() == ISD::SCALAR_TO_VECTOR) && |
| 2475 | Level == AfterLegalizeTypes) { |
| Nadav Rotem | b078350 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2476 | SDValue In0 = N0.getOperand(0); |
| 2477 | SDValue In1 = N1.getOperand(0); |
| 2478 | EVT In0Ty = In0.getValueType(); |
| 2479 | EVT In1Ty = In1.getValueType(); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2480 | SDLoc DL(N); |
| Nadav Rotem | 841c9a8 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 2481 | // If both incoming values are integers, and the original types are the |
| 2482 | // same. |
| Nadav Rotem | b078350 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2483 | if (In0Ty.isInteger() && In1Ty.isInteger() && In0Ty == In1Ty) { |
| Nadav Rotem | 841c9a8 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 2484 | SDValue Op = DAG.getNode(N->getOpcode(), DL, In0Ty, In0, In1); |
| 2485 | SDValue BC = DAG.getNode(N0.getOpcode(), DL, VT, Op); |
| Nadav Rotem | b078350 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2486 | AddToWorkList(Op.getNode()); |
| 2487 | return BC; |
| 2488 | } |
| 2489 | } |
| 2490 | |
| 2491 | // Xor/and/or are indifferent to the swizzle operation (shuffle of one value). |
| 2492 | // Simplify xor/and/or (shuff(A), shuff(B)) -> shuff(op (A,B)) |
| 2493 | // If both shuffles use the same mask, and both shuffle within a single |
| 2494 | // vector, then it is worthwhile to move the swizzle after the operation. |
| 2495 | // The type-legalizer generates this pattern when loading illegal |
| 2496 | // vector types from memory. In many cases this allows additional shuffle |
| 2497 | // optimizations. |
| Craig Topper | 9c3da31 | 2012-04-09 07:19:09 +0000 | [diff] [blame] | 2498 | if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG && |
| 2499 | N0.getOperand(1).getOpcode() == ISD::UNDEF && |
| 2500 | N1.getOperand(1).getOpcode() == ISD::UNDEF) { |
| Nadav Rotem | b078350 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2501 | ShuffleVectorSDNode *SVN0 = cast<ShuffleVectorSDNode>(N0); |
| 2502 | ShuffleVectorSDNode *SVN1 = cast<ShuffleVectorSDNode>(N1); |
| Craig Topper | 9c3da31 | 2012-04-09 07:19:09 +0000 | [diff] [blame] | 2503 | |
| 2504 | assert(N0.getOperand(0).getValueType() == N1.getOperand(1).getValueType() && |
| 2505 | "Inputs to shuffles are not the same type"); |
| Nadav Rotem | b078350 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2506 | |
| 2507 | unsigned NumElts = VT.getVectorNumElements(); |
| Nadav Rotem | b078350 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2508 | |
| 2509 | // Check that both shuffles use the same mask. The masks are known to be of |
| 2510 | // the same length because the result vector type is the same. |
| 2511 | bool SameMask = true; |
| 2512 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2513 | int Idx0 = SVN0->getMaskElt(i); |
| 2514 | int Idx1 = SVN1->getMaskElt(i); |
| 2515 | if (Idx0 != Idx1) { |
| 2516 | SameMask = false; |
| 2517 | break; |
| 2518 | } |
| 2519 | } |
| 2520 | |
| Craig Topper | 9c3da31 | 2012-04-09 07:19:09 +0000 | [diff] [blame] | 2521 | if (SameMask) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2522 | SDValue Op = DAG.getNode(N->getOpcode(), SDLoc(N), VT, |
| Craig Topper | 9c3da31 | 2012-04-09 07:19:09 +0000 | [diff] [blame] | 2523 | N0.getOperand(0), N1.getOperand(0)); |
| Nadav Rotem | b078350 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2524 | AddToWorkList(Op.getNode()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2525 | return DAG.getVectorShuffle(VT, SDLoc(N), Op, |
| Craig Topper | 9c3da31 | 2012-04-09 07:19:09 +0000 | [diff] [blame] | 2526 | DAG.getUNDEF(VT), &SVN0->getMask()[0]); |
| Nadav Rotem | b078350 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2527 | } |
| 2528 | } |
| Craig Topper | 9c3da31 | 2012-04-09 07:19:09 +0000 | [diff] [blame] | 2529 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2530 | return SDValue(); |
| Chris Lattner | 8d6fc20 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2531 | } |
| 2532 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2533 | SDValue DAGCombiner::visitAND(SDNode *N) { |
| 2534 | SDValue N0 = N->getOperand(0); |
| 2535 | SDValue N1 = N->getOperand(1); |
| 2536 | SDValue LL, LR, RL, RR, CC0, CC1; |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2537 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 2538 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2539 | EVT VT = N1.getValueType(); |
| Dan Gohman | e14c408 | 2010-03-04 00:23:16 +0000 | [diff] [blame] | 2540 | unsigned BitWidth = VT.getScalarType().getSizeInBits(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2541 | |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2542 | // fold vector ops |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2543 | if (VT.isVector()) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2544 | SDValue FoldedVOp = SimplifyVBinOp(N); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2545 | if (FoldedVOp.getNode()) return FoldedVOp; |
| Craig Topper | a183ddb | 2012-12-08 22:49:19 +0000 | [diff] [blame] | 2546 | |
| 2547 | // fold (and x, 0) -> 0, vector edition |
| 2548 | if (ISD::isBuildVectorAllZeros(N0.getNode())) |
| 2549 | return N0; |
| 2550 | if (ISD::isBuildVectorAllZeros(N1.getNode())) |
| 2551 | return N1; |
| 2552 | |
| 2553 | // fold (and x, -1) -> x, vector edition |
| 2554 | if (ISD::isBuildVectorAllOnes(N0.getNode())) |
| 2555 | return N1; |
| 2556 | if (ISD::isBuildVectorAllOnes(N1.getNode())) |
| 2557 | return N0; |
| Dan Gohman | 80f9f07 | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 2558 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2559 | |
| Dan Gohman | 06563a8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2560 | // fold (and x, undef) -> 0 |
| Dan Gohman | fa91282 | 2007-07-10 14:20:37 +0000 | [diff] [blame] | 2561 | if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) |
| Dan Gohman | 06563a8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2562 | return DAG.getConstant(0, VT); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2563 | // fold (and c1, c2) -> c1&c2 |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2564 | if (N0C && N1C) |
| Bill Wendling | dea9130 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 2565 | return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C); |
| Nate Begeman | 2cc2c9a | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 2566 | // canonicalize constant to RHS |
| Nate Begeman | 418c6e4 | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 2567 | if (N0C && !N1C) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2568 | return DAG.getNode(ISD::AND, SDLoc(N), VT, N1, N0); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2569 | // fold (and x, -1) -> x |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2570 | if (N1C && N1C->isAllOnesValue()) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2571 | return N0; |
| 2572 | // if (and x, c) is known to be zero, return 0 |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2573 | if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0), |
| Dan Gohman | 1f372ed | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2574 | APInt::getAllOnesValue(BitWidth))) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2575 | return DAG.getConstant(0, VT); |
| Nate Begeman | 22e251a | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 2576 | // reassociate and |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2577 | SDValue RAND = ReassociateOps(ISD::AND, SDLoc(N), N0, N1); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2578 | if (RAND.getNode() != 0) |
| Nate Begeman | 22e251a | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 2579 | return RAND; |
| Bill Wendling | af13d82 | 2010-03-03 00:35:56 +0000 | [diff] [blame] | 2580 | // fold (and (or x, C), D) -> D if (C & D) == D |
| Nate Begeman | ee06528 | 2005-11-02 18:42:59 +0000 | [diff] [blame] | 2581 | if (N1C && N0.getOpcode() == ISD::OR) |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2582 | if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2583 | if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue()) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2584 | return N1; |
| Chris Lattner | 49beaf4 | 2006-02-02 07:17:31 +0000 | [diff] [blame] | 2585 | // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits. |
| 2586 | if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2587 | SDValue N0Op0 = N0.getOperand(0); |
| Dan Gohman | 1f372ed | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2588 | APInt Mask = ~N1C->getAPIntValue(); |
| Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 2589 | Mask = Mask.trunc(N0Op0.getValueSizeInBits()); |
| Dan Gohman | 1f372ed | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2590 | if (DAG.MaskedValueIsZero(N0Op0, Mask)) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2591 | SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), |
| Bill Wendling | 8617191 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2592 | N0.getValueType(), N0Op0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2593 | |
| Chris Lattner | 0db2f2c | 2006-03-01 21:47:21 +0000 | [diff] [blame] | 2594 | // Replace uses of the AND with uses of the Zero extend node. |
| 2595 | CombineTo(N, Zext); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2596 | |
| Chris Lattner | 49beaf4 | 2006-02-02 07:17:31 +0000 | [diff] [blame] | 2597 | // We actually want to replace all uses of the any_extend with the |
| 2598 | // zero_extend, to avoid duplicating things. This will later cause this |
| 2599 | // AND to be folded. |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2600 | CombineTo(N0.getNode(), Zext); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2601 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| Chris Lattner | 49beaf4 | 2006-02-02 07:17:31 +0000 | [diff] [blame] | 2602 | } |
| 2603 | } |
| Stephen Lin | cfe7f35 | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 2604 | // similarly fold (and (X (load ([non_ext|any_ext|zero_ext] V))), c) -> |
| James Molloy | 862fe49 | 2012-02-20 12:02:38 +0000 | [diff] [blame] | 2605 | // (X (load ([non_ext|zero_ext] V))) if 'and' only clears top bits which must |
| 2606 | // already be zero by virtue of the width of the base type of the load. |
| 2607 | // |
| 2608 | // the 'X' node here can either be nothing or an extract_vector_elt to catch |
| 2609 | // more cases. |
| 2610 | if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT && |
| 2611 | N0.getOperand(0).getOpcode() == ISD::LOAD) || |
| 2612 | N0.getOpcode() == ISD::LOAD) { |
| 2613 | LoadSDNode *Load = cast<LoadSDNode>( (N0.getOpcode() == ISD::LOAD) ? |
| 2614 | N0 : N0.getOperand(0) ); |
| 2615 | |
| 2616 | // Get the constant (if applicable) the zero'th operand is being ANDed with. |
| 2617 | // This can be a pure constant or a vector splat, in which case we treat the |
| 2618 | // vector as a scalar and use the splat value. |
| 2619 | APInt Constant = APInt::getNullValue(1); |
| 2620 | if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) { |
| 2621 | Constant = C->getAPIntValue(); |
| 2622 | } else if (BuildVectorSDNode *Vector = dyn_cast<BuildVectorSDNode>(N1)) { |
| 2623 | APInt SplatValue, SplatUndef; |
| 2624 | unsigned SplatBitSize; |
| 2625 | bool HasAnyUndefs; |
| 2626 | bool IsSplat = Vector->isConstantSplat(SplatValue, SplatUndef, |
| 2627 | SplatBitSize, HasAnyUndefs); |
| 2628 | if (IsSplat) { |
| 2629 | // Undef bits can contribute to a possible optimisation if set, so |
| 2630 | // set them. |
| 2631 | SplatValue |= SplatUndef; |
| 2632 | |
| 2633 | // The splat value may be something like "0x00FFFFFF", which means 0 for |
| 2634 | // the first vector value and FF for the rest, repeating. We need a mask |
| 2635 | // that will apply equally to all members of the vector, so AND all the |
| 2636 | // lanes of the constant together. |
| 2637 | EVT VT = Vector->getValueType(0); |
| 2638 | unsigned BitWidth = VT.getVectorElementType().getSizeInBits(); |
| Silviu Baranga | 3f40d87 | 2012-09-05 08:57:21 +0000 | [diff] [blame] | 2639 | |
| 2640 | // If the splat value has been compressed to a bitlength lower |
| 2641 | // than the size of the vector lane, we need to re-expand it to |
| 2642 | // the lane size. |
| 2643 | if (BitWidth > SplatBitSize) |
| 2644 | for (SplatValue = SplatValue.zextOrTrunc(BitWidth); |
| 2645 | SplatBitSize < BitWidth; |
| 2646 | SplatBitSize = SplatBitSize * 2) |
| 2647 | SplatValue |= SplatValue.shl(SplatBitSize); |
| 2648 | |
| James Molloy | 862fe49 | 2012-02-20 12:02:38 +0000 | [diff] [blame] | 2649 | Constant = APInt::getAllOnesValue(BitWidth); |
| Silviu Baranga | 3f40d87 | 2012-09-05 08:57:21 +0000 | [diff] [blame] | 2650 | for (unsigned i = 0, n = SplatBitSize/BitWidth; i < n; ++i) |
| James Molloy | 862fe49 | 2012-02-20 12:02:38 +0000 | [diff] [blame] | 2651 | Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth); |
| 2652 | } |
| 2653 | } |
| 2654 | |
| 2655 | // If we want to change an EXTLOAD to a ZEXTLOAD, ensure a ZEXTLOAD is |
| 2656 | // actually legal and isn't going to get expanded, else this is a false |
| 2657 | // optimisation. |
| 2658 | bool CanZextLoadProfitably = TLI.isLoadExtLegal(ISD::ZEXTLOAD, |
| 2659 | Load->getMemoryVT()); |
| 2660 | |
| 2661 | // Resize the constant to the same size as the original memory access before |
| 2662 | // extension. If it is still the AllOnesValue then this AND is completely |
| 2663 | // unneeded. |
| 2664 | Constant = |
| 2665 | Constant.zextOrTrunc(Load->getMemoryVT().getScalarType().getSizeInBits()); |
| 2666 | |
| 2667 | bool B; |
| 2668 | switch (Load->getExtensionType()) { |
| 2669 | default: B = false; break; |
| 2670 | case ISD::EXTLOAD: B = CanZextLoadProfitably; break; |
| 2671 | case ISD::ZEXTLOAD: |
| 2672 | case ISD::NON_EXTLOAD: B = true; break; |
| 2673 | } |
| 2674 | |
| 2675 | if (B && Constant.isAllOnesValue()) { |
| 2676 | // If the load type was an EXTLOAD, convert to ZEXTLOAD in order to |
| 2677 | // preserve semantics once we get rid of the AND. |
| 2678 | SDValue NewLoad(Load, 0); |
| 2679 | if (Load->getExtensionType() == ISD::EXTLOAD) { |
| 2680 | NewLoad = DAG.getLoad(Load->getAddressingMode(), ISD::ZEXTLOAD, |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2681 | Load->getValueType(0), SDLoc(Load), |
| James Molloy | 862fe49 | 2012-02-20 12:02:38 +0000 | [diff] [blame] | 2682 | Load->getChain(), Load->getBasePtr(), |
| 2683 | Load->getOffset(), Load->getMemoryVT(), |
| 2684 | Load->getMemOperand()); |
| 2685 | // Replace uses of the EXTLOAD with the new ZEXTLOAD. |
| Hal Finkel | 8a31138 | 2012-06-20 15:42:48 +0000 | [diff] [blame] | 2686 | if (Load->getNumValues() == 3) { |
| 2687 | // PRE/POST_INC loads have 3 values. |
| 2688 | SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1), |
| 2689 | NewLoad.getValue(2) }; |
| 2690 | CombineTo(Load, To, 3, true); |
| 2691 | } else { |
| 2692 | CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1)); |
| 2693 | } |
| James Molloy | 862fe49 | 2012-02-20 12:02:38 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
| 2696 | // Fold the AND away, taking care not to fold to the old load node if we |
| 2697 | // replaced it. |
| 2698 | CombineTo(N, (N0.getNode() == Load) ? NewLoad : N0); |
| 2699 | |
| 2700 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| 2701 | } |
| 2702 | } |
| Nate Begeman | 049b748 | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2703 | // fold (and (setcc x), (setcc y)) -> (setcc (and x, y)) |
| 2704 | if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){ |
| 2705 | ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get(); |
| 2706 | ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2707 | |
| Nate Begeman | 049b748 | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2708 | if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 && |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2709 | LL.getValueType().isInteger()) { |
| Bill Wendling | 8617191 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2710 | // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0) |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2711 | if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2712 | SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0), |
| Bill Wendling | 8617191 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2713 | LR.getValueType(), LL, RL); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2714 | AddToWorkList(ORNode.getNode()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2715 | return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1); |
| Nate Begeman | 049b748 | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2716 | } |
| Bill Wendling | 8617191 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2717 | // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1) |
| Nate Begeman | 049b748 | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2718 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2719 | SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(N0), |
| Bill Wendling | 8617191 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2720 | LR.getValueType(), LL, RL); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2721 | AddToWorkList(ANDNode.getNode()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2722 | return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1); |
| Nate Begeman | 049b748 | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2723 | } |
| Bill Wendling | 8617191 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2724 | // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1) |
| Nate Begeman | 049b748 | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2725 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2726 | SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0), |
| Bill Wendling | 8617191 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2727 | LR.getValueType(), LL, RL); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2728 | AddToWorkList(ORNode.getNode()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2729 | return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1); |
| Nate Begeman | 049b748 | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2730 | } |
| 2731 | } |
| Jim Grosbach | 327ccc7 | 2013-08-13 21:30:58 +0000 | [diff] [blame] | 2732 | // Simplify (and (setne X, 0), (setne X, -1)) -> (setuge (add X, 1), 2) |
| 2733 | if (LL == RL && isa<ConstantSDNode>(LR) && isa<ConstantSDNode>(RR) && |
| 2734 | Op0 == Op1 && LL.getValueType().isInteger() && |
| 2735 | Op0 == ISD::SETNE && ((cast<ConstantSDNode>(LR)->isNullValue() && |
| 2736 | cast<ConstantSDNode>(RR)->isAllOnesValue()) || |
| 2737 | (cast<ConstantSDNode>(LR)->isAllOnesValue() && |
| 2738 | cast<ConstantSDNode>(RR)->isNullValue()))) { |
| 2739 | SDValue ADDNode = DAG.getNode(ISD::ADD, SDLoc(N0), LL.getValueType(), |
| 2740 | LL, DAG.getConstant(1, LL.getValueType())); |
| 2741 | AddToWorkList(ADDNode.getNode()); |
| 2742 | return DAG.getSetCC(SDLoc(N), VT, ADDNode, |
| 2743 | DAG.getConstant(2, LL.getValueType()), ISD::SETUGE); |
| 2744 | } |
| Nate Begeman | 049b748 | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2745 | // canonicalize equivalent to ll == rl |
| 2746 | if (LL == RR && LR == RL) { |
| 2747 | Op1 = ISD::getSetCCSwappedOperands(Op1); |
| 2748 | std::swap(RL, RR); |
| 2749 | } |
| 2750 | if (LL == RL && LR == RR) { |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2751 | bool isInteger = LL.getValueType().isInteger(); |
| Nate Begeman | 049b748 | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2752 | ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger); |
| Chris Lattner | 5fa1040 | 2008-10-28 07:11:07 +0000 | [diff] [blame] | 2753 | if (Result != ISD::SETCC_INVALID && |
| Patrik Hagglund | ffd057a | 2012-12-19 10:19:55 +0000 | [diff] [blame] | 2754 | (!LegalOperations || |
| Owen Anderson | cc06899 | 2013-02-14 09:07:33 +0000 | [diff] [blame] | 2755 | (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) && |
| 2756 | TLI.isOperationLegal(ISD::SETCC, |
| Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 2757 | getSetCCResultType(N0.getSimpleValueType()))))) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2758 | return DAG.getSetCC(SDLoc(N), N0.getValueType(), |
| Bill Wendling | 8617191 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2759 | LL, LR, Result); |
| Nate Begeman | 049b748 | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2760 | } |
| 2761 | } |
| Chris Lattner | 8d6fc20 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2762 | |
| Bill Wendling | 8617191 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2763 | // Simplify: (and (op x...), (op y...)) -> (op (and x, y)) |
| Chris Lattner | 8d6fc20 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2764 | if (N0.getOpcode() == N1.getOpcode()) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2765 | SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2766 | if (Tmp.getNode()) return Tmp; |
| Nate Begeman | 049b748 | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2767 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2768 | |
| Nate Begeman | dc7bba9 | 2006-02-03 22:24:05 +0000 | [diff] [blame] | 2769 | // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1) |
| 2770 | // fold (and (sra)) -> (and (srl)) when possible. |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2771 | if (!VT.isVector() && |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2772 | SimplifyDemandedBits(SDValue(N, 0))) |
| 2773 | return SDValue(N, 0); |
| Evan Cheng | 166a4e6 | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2774 | |
| Nate Begeman | 02b23c6 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 2775 | // fold (zext_inreg (extload x)) -> (zextload x) |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2776 | if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) { |
| Evan Cheng | e71fe34d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2777 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
| Dan Gohman | 08c0a95 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 2778 | EVT MemVT = LN0->getMemoryVT(); |
| Nate Begeman | 8e022b3 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 2779 | // If we zero all the possible extended bits, then we can turn this into |
| 2780 | // a zextload if we are running before legalize or the operation is legal. |
| Dan Gohman | e14c408 | 2010-03-04 00:23:16 +0000 | [diff] [blame] | 2781 | unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits(); |
| Dan Gohman | 1f372ed | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2782 | if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth, |
| Dan Gohman | e14c408 | 2010-03-04 00:23:16 +0000 | [diff] [blame] | 2783 | BitWidth - MemVT.getScalarType().getSizeInBits())) && |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2784 | ((!LegalOperations && !LN0->isVolatile()) || |
| Dan Gohman | 08c0a95 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 2785 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2786 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT, |
| Bill Wendling | 8617191 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2787 | LN0->getChain(), LN0->getBasePtr(), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 2788 | MemVT, LN0->getMemOperand()); |
| Chris Lattner | fbcd62d | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 2789 | AddToWorkList(N); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2790 | CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2791 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| Nate Begeman | 02b23c6 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 2792 | } |
| 2793 | } |
| Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2794 | // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2795 | if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) && |
| Evan Cheng | 8a1d09d | 2007-03-07 08:07:03 +0000 | [diff] [blame] | 2796 | N0.hasOneUse()) { |
| Evan Cheng | e71fe34d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2797 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
| Dan Gohman | 08c0a95 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 2798 | EVT MemVT = LN0->getMemoryVT(); |
| Nate Begeman | 8e022b3 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 2799 | // If we zero all the possible extended bits, then we can turn this into |
| 2800 | // a zextload if we are running before legalize or the operation is legal. |
| Dan Gohman | e14c408 | 2010-03-04 00:23:16 +0000 | [diff] [blame] | 2801 | unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits(); |
| Dan Gohman | 1f372ed | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2802 | if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth, |
| Dan Gohman | e14c408 | 2010-03-04 00:23:16 +0000 | [diff] [blame] | 2803 | BitWidth - MemVT.getScalarType().getSizeInBits())) && |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2804 | ((!LegalOperations && !LN0->isVolatile()) || |
| Dan Gohman | 08c0a95 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 2805 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2806 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT, |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 2807 | LN0->getChain(), LN0->getBasePtr(), |
| 2808 | MemVT, LN0->getMemOperand()); |
| Chris Lattner | fbcd62d | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 2809 | AddToWorkList(N); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2810 | CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2811 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| Nate Begeman | 02b23c6 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 2812 | } |
| 2813 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2814 | |
| Chris Lattner | f0032b3 | 2006-02-28 06:49:37 +0000 | [diff] [blame] | 2815 | // fold (and (load x), 255) -> (zextload x, i8) |
| 2816 | // fold (and (extload x, i16), 255) -> (zextload x, i8) |
| Evan Cheng | 166a4e6 | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2817 | // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8) |
| 2818 | if (N1C && (N0.getOpcode() == ISD::LOAD || |
| 2819 | (N0.getOpcode() == ISD::ANY_EXTEND && |
| 2820 | N0.getOperand(0).getOpcode() == ISD::LOAD))) { |
| 2821 | bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND; |
| 2822 | LoadSDNode *LN0 = HasAnyExt |
| 2823 | ? cast<LoadSDNode>(N0.getOperand(0)) |
| 2824 | : cast<LoadSDNode>(N0); |
| Evan Cheng | e71fe34d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2825 | if (LN0->getExtensionType() != ISD::SEXTLOAD && |
| Tim Northover | 6823900 | 2013-07-02 09:58:53 +0000 | [diff] [blame] | 2826 | LN0->isUnindexed() && N0.hasOneUse() && SDValue(LN0, 0).hasOneUse()) { |
| Duncan Sands | 93b6609 | 2008-06-09 11:32:28 +0000 | [diff] [blame] | 2827 | uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits(); |
| Evan Cheng | 166a4e6 | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2828 | if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){ |
| 2829 | EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits); |
| 2830 | EVT LoadedVT = LN0->getMemoryVT(); |
| Duncan Sands | 93b6609 | 2008-06-09 11:32:28 +0000 | [diff] [blame] | 2831 | |
| Evan Cheng | 166a4e6 | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2832 | if (ExtVT == LoadedVT && |
| 2833 | (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) { |
| Chris Lattner | 88de384 | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2834 | EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2835 | |
| 2836 | SDValue NewLoad = |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2837 | DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy, |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 2838 | LN0->getChain(), LN0->getBasePtr(), ExtVT, |
| 2839 | LN0->getMemOperand()); |
| Chris Lattner | 88de384 | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2840 | AddToWorkList(N); |
| 2841 | CombineTo(LN0, NewLoad, NewLoad.getValue(1)); |
| 2842 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| 2843 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2844 | |
| Chris Lattner | 88de384 | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2845 | // Do not change the width of a volatile load. |
| 2846 | // Do not generate loads of non-round integer types since these can |
| 2847 | // be expensive (and would be wrong if the type is not byte sized). |
| 2848 | if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() && |
| 2849 | (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) { |
| 2850 | EVT PtrType = LN0->getOperand(1).getValueType(); |
| Bill Wendling | 8617191 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2851 | |
| Chris Lattner | 88de384 | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2852 | unsigned Alignment = LN0->getAlignment(); |
| 2853 | SDValue NewPtr = LN0->getBasePtr(); |
| 2854 | |
| 2855 | // For big endian targets, we need to add an offset to the pointer |
| 2856 | // to load the correct bytes. For little endian systems, we merely |
| 2857 | // need to read fewer bytes from the same pointer. |
| 2858 | if (TLI.isBigEndian()) { |
| Evan Cheng | 166a4e6 | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2859 | unsigned LVTStoreBytes = LoadedVT.getStoreSize(); |
| 2860 | unsigned EVTStoreBytes = ExtVT.getStoreSize(); |
| 2861 | unsigned PtrOff = LVTStoreBytes - EVTStoreBytes; |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2862 | NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0), PtrType, |
| Chris Lattner | 88de384 | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2863 | NewPtr, DAG.getConstant(PtrOff, PtrType)); |
| 2864 | Alignment = MinAlign(Alignment, PtrOff); |
| Evan Cheng | 166a4e6 | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2865 | } |
| Chris Lattner | 88de384 | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2866 | |
| 2867 | AddToWorkList(NewPtr.getNode()); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2868 | |
| Chris Lattner | 88de384 | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2869 | EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT; |
| 2870 | SDValue Load = |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2871 | DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy, |
| Chris Lattner | 88de384 | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2872 | LN0->getChain(), NewPtr, |
| Chris Lattner | 3d178ed | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 2873 | LN0->getPointerInfo(), |
| David Greene | 39c6d01 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2874 | ExtVT, LN0->isVolatile(), LN0->isNonTemporal(), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 2875 | Alignment, LN0->getTBAAInfo()); |
| Chris Lattner | 88de384 | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2876 | AddToWorkList(N); |
| 2877 | CombineTo(LN0, Load, Load.getValue(1)); |
| 2878 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| Duncan Sands | 1826ded | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 2879 | } |
| Evan Cheng | e71fe34d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2880 | } |
| Chris Lattner | bdbc447 | 2006-02-28 06:35:35 +0000 | [diff] [blame] | 2881 | } |
| 2882 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2883 | |
| Evan Cheng | e6a3b03 | 2012-07-17 18:54:11 +0000 | [diff] [blame] | 2884 | if (N0.getOpcode() == ISD::ADD && N1.getOpcode() == ISD::SRL && |
| 2885 | VT.getSizeInBits() <= 64) { |
| 2886 | if (ConstantSDNode *ADDI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
| 2887 | APInt ADDC = ADDI->getAPIntValue(); |
| 2888 | if (!TLI.isLegalAddImmediate(ADDC.getSExtValue())) { |
| 2889 | // Look for (and (add x, c1), (lshr y, c2)). If C1 wasn't a legal |
| 2890 | // immediate for an add, but it is legal if its top c2 bits are set, |
| 2891 | // transform the ADD so the immediate doesn't need to be materialized |
| 2892 | // in a register. |
| 2893 | if (ConstantSDNode *SRLI = dyn_cast<ConstantSDNode>(N1.getOperand(1))) { |
| 2894 | APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(), |
| 2895 | SRLI->getZExtValue()); |
| 2896 | if (DAG.MaskedValueIsZero(N0.getOperand(1), Mask)) { |
| 2897 | ADDC |= Mask; |
| 2898 | if (TLI.isLegalAddImmediate(ADDC.getSExtValue())) { |
| 2899 | SDValue NewAdd = |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2900 | DAG.getNode(ISD::ADD, SDLoc(N0), VT, |
| Evan Cheng | e6a3b03 | 2012-07-17 18:54:11 +0000 | [diff] [blame] | 2901 | N0.getOperand(0), DAG.getConstant(ADDC, VT)); |
| 2902 | CombineTo(N0.getNode(), NewAdd); |
| 2903 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| 2904 | } |
| 2905 | } |
| 2906 | } |
| 2907 | } |
| 2908 | } |
| 2909 | } |
| Evan Cheng | e6a3b03 | 2012-07-17 18:54:11 +0000 | [diff] [blame] | 2910 | |
| Tim Northover | 819bfb5 | 2013-08-27 13:46:45 +0000 | [diff] [blame] | 2911 | // fold (and (or (srl N, 8), (shl N, 8)), 0xffff) -> (srl (bswap N), const) |
| 2912 | if (N1C && N1C->getAPIntValue() == 0xffff && N0.getOpcode() == ISD::OR) { |
| 2913 | SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0), |
| 2914 | N0.getOperand(1), false); |
| 2915 | if (BSwap.getNode()) |
| 2916 | return BSwap; |
| 2917 | } |
| 2918 | |
| Evan Cheng | f100557 | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 2919 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2920 | } |
| 2921 | |
| Evan Cheng | 4c0bd96 | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 2922 | /// MatchBSwapHWord - Match (a >> 8) | (a << 8) as (bswap a) >> 16 |
| 2923 | /// |
| 2924 | SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1, |
| 2925 | bool DemandHighBits) { |
| 2926 | if (!LegalOperations) |
| 2927 | return SDValue(); |
| 2928 | |
| 2929 | EVT VT = N->getValueType(0); |
| 2930 | if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16) |
| 2931 | return SDValue(); |
| 2932 | if (!TLI.isOperationLegal(ISD::BSWAP, VT)) |
| 2933 | return SDValue(); |
| 2934 | |
| 2935 | // Recognize (and (shl a, 8), 0xff), (and (srl a, 8), 0xff00) |
| 2936 | bool LookPassAnd0 = false; |
| 2937 | bool LookPassAnd1 = false; |
| 2938 | if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL) |
| 2939 | std::swap(N0, N1); |
| 2940 | if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL) |
| 2941 | std::swap(N0, N1); |
| 2942 | if (N0.getOpcode() == ISD::AND) { |
| 2943 | if (!N0.getNode()->hasOneUse()) |
| 2944 | return SDValue(); |
| 2945 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 2946 | if (!N01C || N01C->getZExtValue() != 0xFF00) |
| 2947 | return SDValue(); |
| 2948 | N0 = N0.getOperand(0); |
| 2949 | LookPassAnd0 = true; |
| 2950 | } |
| 2951 | |
| 2952 | if (N1.getOpcode() == ISD::AND) { |
| 2953 | if (!N1.getNode()->hasOneUse()) |
| 2954 | return SDValue(); |
| 2955 | ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); |
| 2956 | if (!N11C || N11C->getZExtValue() != 0xFF) |
| 2957 | return SDValue(); |
| 2958 | N1 = N1.getOperand(0); |
| 2959 | LookPassAnd1 = true; |
| 2960 | } |
| 2961 | |
| 2962 | if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL) |
| 2963 | std::swap(N0, N1); |
| 2964 | if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL) |
| 2965 | return SDValue(); |
| 2966 | if (!N0.getNode()->hasOneUse() || |
| 2967 | !N1.getNode()->hasOneUse()) |
| 2968 | return SDValue(); |
| 2969 | |
| 2970 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 2971 | ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); |
| 2972 | if (!N01C || !N11C) |
| 2973 | return SDValue(); |
| 2974 | if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8) |
| 2975 | return SDValue(); |
| 2976 | |
| 2977 | // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8) |
| 2978 | SDValue N00 = N0->getOperand(0); |
| 2979 | if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) { |
| 2980 | if (!N00.getNode()->hasOneUse()) |
| 2981 | return SDValue(); |
| 2982 | ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1)); |
| 2983 | if (!N001C || N001C->getZExtValue() != 0xFF) |
| 2984 | return SDValue(); |
| 2985 | N00 = N00.getOperand(0); |
| 2986 | LookPassAnd0 = true; |
| 2987 | } |
| 2988 | |
| 2989 | SDValue N10 = N1->getOperand(0); |
| 2990 | if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) { |
| 2991 | if (!N10.getNode()->hasOneUse()) |
| 2992 | return SDValue(); |
| 2993 | ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1)); |
| 2994 | if (!N101C || N101C->getZExtValue() != 0xFF00) |
| 2995 | return SDValue(); |
| 2996 | N10 = N10.getOperand(0); |
| 2997 | LookPassAnd1 = true; |
| 2998 | } |
| 2999 | |
| 3000 | if (N00 != N10) |
| 3001 | return SDValue(); |
| 3002 | |
| Tim Northover | 819bfb5 | 2013-08-27 13:46:45 +0000 | [diff] [blame] | 3003 | // Make sure everything beyond the low halfword gets set to zero since the SRL |
| 3004 | // 16 will clear the top bits. |
| Evan Cheng | 4c0bd96 | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 3005 | unsigned OpSizeInBits = VT.getSizeInBits(); |
| Tim Northover | 819bfb5 | 2013-08-27 13:46:45 +0000 | [diff] [blame] | 3006 | if (DemandHighBits && OpSizeInBits > 16) { |
| 3007 | // If the left-shift isn't masked out then the only way this is a bswap is |
| 3008 | // if all bits beyond the low 8 are 0. In that case the entire pattern |
| 3009 | // reduces to a left shift anyway: leave it for other parts of the combiner. |
| 3010 | if (!LookPassAnd0) |
| 3011 | return SDValue(); |
| 3012 | |
| 3013 | // However, if the right shift isn't masked out then it might be because |
| 3014 | // it's not needed. See if we can spot that too. |
| 3015 | if (!LookPassAnd1 && |
| 3016 | !DAG.MaskedValueIsZero( |
| 3017 | N10, APInt::getHighBitsSet(OpSizeInBits, OpSizeInBits - 16))) |
| 3018 | return SDValue(); |
| 3019 | } |
| Eric Christopher | d6300d2 | 2011-07-14 01:12:15 +0000 | [diff] [blame] | 3020 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3021 | SDValue Res = DAG.getNode(ISD::BSWAP, SDLoc(N), VT, N00); |
| Evan Cheng | 4c0bd96 | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 3022 | if (OpSizeInBits > 16) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3023 | Res = DAG.getNode(ISD::SRL, SDLoc(N), VT, Res, |
| Evan Cheng | 4c0bd96 | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 3024 | DAG.getConstant(OpSizeInBits-16, getShiftAmountTy(VT))); |
| 3025 | return Res; |
| 3026 | } |
| 3027 | |
| 3028 | /// isBSwapHWordElement - Return true if the specified node is an element |
| 3029 | /// that makes up a 32-bit packed halfword byteswap. i.e. |
| 3030 | /// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8) |
| Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 3031 | static bool isBSwapHWordElement(SDValue N, SmallVectorImpl<SDNode *> &Parts) { |
| Evan Cheng | 4c0bd96 | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 3032 | if (!N.getNode()->hasOneUse()) |
| 3033 | return false; |
| 3034 | |
| 3035 | unsigned Opc = N.getOpcode(); |
| 3036 | if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL) |
| 3037 | return false; |
| 3038 | |
| 3039 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1)); |
| 3040 | if (!N1C) |
| 3041 | return false; |
| 3042 | |
| 3043 | unsigned Num; |
| 3044 | switch (N1C->getZExtValue()) { |
| 3045 | default: |
| 3046 | return false; |
| 3047 | case 0xFF: Num = 0; break; |
| 3048 | case 0xFF00: Num = 1; break; |
| 3049 | case 0xFF0000: Num = 2; break; |
| 3050 | case 0xFF000000: Num = 3; break; |
| 3051 | } |
| 3052 | |
| 3053 | // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00). |
| 3054 | SDValue N0 = N.getOperand(0); |
| 3055 | if (Opc == ISD::AND) { |
| 3056 | if (Num == 0 || Num == 2) { |
| 3057 | // (x >> 8) & 0xff |
| 3058 | // (x >> 8) & 0xff0000 |
| 3059 | if (N0.getOpcode() != ISD::SRL) |
| 3060 | return false; |
| 3061 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 3062 | if (!C || C->getZExtValue() != 8) |
| 3063 | return false; |
| 3064 | } else { |
| 3065 | // (x << 8) & 0xff00 |
| 3066 | // (x << 8) & 0xff000000 |
| 3067 | if (N0.getOpcode() != ISD::SHL) |
| 3068 | return false; |
| 3069 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 3070 | if (!C || C->getZExtValue() != 8) |
| 3071 | return false; |
| 3072 | } |
| 3073 | } else if (Opc == ISD::SHL) { |
| 3074 | // (x & 0xff) << 8 |
| 3075 | // (x & 0xff0000) << 8 |
| 3076 | if (Num != 0 && Num != 2) |
| 3077 | return false; |
| 3078 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1)); |
| 3079 | if (!C || C->getZExtValue() != 8) |
| 3080 | return false; |
| 3081 | } else { // Opc == ISD::SRL |
| 3082 | // (x & 0xff00) >> 8 |
| 3083 | // (x & 0xff000000) >> 8 |
| 3084 | if (Num != 1 && Num != 3) |
| 3085 | return false; |
| 3086 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1)); |
| 3087 | if (!C || C->getZExtValue() != 8) |
| 3088 | return false; |
| 3089 | } |
| 3090 | |
| 3091 | if (Parts[Num]) |
| 3092 | return false; |
| 3093 | |
| 3094 | Parts[Num] = N0.getOperand(0).getNode(); |
| 3095 | return true; |
| 3096 | } |
| 3097 | |
| 3098 | /// MatchBSwapHWord - Match a 32-bit packed halfword bswap. That is |
| 3099 | /// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8) |
| 3100 | /// => (rotl (bswap x), 16) |
| 3101 | SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) { |
| 3102 | if (!LegalOperations) |
| 3103 | return SDValue(); |
| 3104 | |
| 3105 | EVT VT = N->getValueType(0); |
| 3106 | if (VT != MVT::i32) |
| 3107 | return SDValue(); |
| 3108 | if (!TLI.isOperationLegal(ISD::BSWAP, VT)) |
| 3109 | return SDValue(); |
| 3110 | |
| 3111 | SmallVector<SDNode*,4> Parts(4, (SDNode*)0); |
| 3112 | // Look for either |
| 3113 | // (or (or (and), (and)), (or (and), (and))) |
| 3114 | // (or (or (or (and), (and)), (and)), (and)) |
| 3115 | if (N0.getOpcode() != ISD::OR) |
| 3116 | return SDValue(); |
| 3117 | SDValue N00 = N0.getOperand(0); |
| 3118 | SDValue N01 = N0.getOperand(1); |
| 3119 | |
| Evan Cheng | bf0baa9 | 2012-12-13 01:34:32 +0000 | [diff] [blame] | 3120 | if (N1.getOpcode() == ISD::OR && |
| 3121 | N00.getNumOperands() == 2 && N01.getNumOperands() == 2) { |
| Evan Cheng | 4c0bd96 | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 3122 | // (or (or (and), (and)), (or (and), (and))) |
| 3123 | SDValue N000 = N00.getOperand(0); |
| 3124 | if (!isBSwapHWordElement(N000, Parts)) |
| 3125 | return SDValue(); |
| 3126 | |
| 3127 | SDValue N001 = N00.getOperand(1); |
| 3128 | if (!isBSwapHWordElement(N001, Parts)) |
| 3129 | return SDValue(); |
| 3130 | SDValue N010 = N01.getOperand(0); |
| 3131 | if (!isBSwapHWordElement(N010, Parts)) |
| 3132 | return SDValue(); |
| 3133 | SDValue N011 = N01.getOperand(1); |
| 3134 | if (!isBSwapHWordElement(N011, Parts)) |
| 3135 | return SDValue(); |
| 3136 | } else { |
| 3137 | // (or (or (or (and), (and)), (and)), (and)) |
| 3138 | if (!isBSwapHWordElement(N1, Parts)) |
| 3139 | return SDValue(); |
| 3140 | if (!isBSwapHWordElement(N01, Parts)) |
| 3141 | return SDValue(); |
| 3142 | if (N00.getOpcode() != ISD::OR) |
| 3143 | return SDValue(); |
| 3144 | SDValue N000 = N00.getOperand(0); |
| 3145 | if (!isBSwapHWordElement(N000, Parts)) |
| 3146 | return SDValue(); |
| 3147 | SDValue N001 = N00.getOperand(1); |
| 3148 | if (!isBSwapHWordElement(N001, Parts)) |
| 3149 | return SDValue(); |
| 3150 | } |
| 3151 | |
| 3152 | // Make sure the parts are all coming from the same node. |
| 3153 | if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3]) |
| 3154 | return SDValue(); |
| 3155 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3156 | SDValue BSwap = DAG.getNode(ISD::BSWAP, SDLoc(N), VT, |
| Evan Cheng | 4c0bd96 | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 3157 | SDValue(Parts[0],0)); |
| 3158 | |
| Kay Tiong Khoo | 9195a5b | 2013-09-23 18:43:51 +0000 | [diff] [blame] | 3159 | // Result of the bswap should be rotated by 16. If it's not legal, then |
| Evan Cheng | 4c0bd96 | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 3160 | // do (x << 16) | (x >> 16). |
| 3161 | SDValue ShAmt = DAG.getConstant(16, getShiftAmountTy(VT)); |
| 3162 | if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3163 | return DAG.getNode(ISD::ROTL, SDLoc(N), VT, BSwap, ShAmt); |
| Craig Topper | 5f9791f | 2012-09-29 07:18:53 +0000 | [diff] [blame] | 3164 | if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3165 | return DAG.getNode(ISD::ROTR, SDLoc(N), VT, BSwap, ShAmt); |
| 3166 | return DAG.getNode(ISD::OR, SDLoc(N), VT, |
| 3167 | DAG.getNode(ISD::SHL, SDLoc(N), VT, BSwap, ShAmt), |
| 3168 | DAG.getNode(ISD::SRL, SDLoc(N), VT, BSwap, ShAmt)); |
| Evan Cheng | 4c0bd96 | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 3169 | } |
| 3170 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3171 | SDValue DAGCombiner::visitOR(SDNode *N) { |
| 3172 | SDValue N0 = N->getOperand(0); |
| 3173 | SDValue N1 = N->getOperand(1); |
| 3174 | SDValue LL, LR, RL, RR, CC0, CC1; |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3175 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 3176 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3177 | EVT VT = N1.getValueType(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3178 | |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3179 | // fold vector ops |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3180 | if (VT.isVector()) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3181 | SDValue FoldedVOp = SimplifyVBinOp(N); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3182 | if (FoldedVOp.getNode()) return FoldedVOp; |
| Craig Topper | a183ddb | 2012-12-08 22:49:19 +0000 | [diff] [blame] | 3183 | |
| 3184 | // fold (or x, 0) -> x, vector edition |
| 3185 | if (ISD::isBuildVectorAllZeros(N0.getNode())) |
| 3186 | return N1; |
| 3187 | if (ISD::isBuildVectorAllZeros(N1.getNode())) |
| 3188 | return N0; |
| 3189 | |
| 3190 | // fold (or x, -1) -> -1, vector edition |
| 3191 | if (ISD::isBuildVectorAllOnes(N0.getNode())) |
| 3192 | return N0; |
| 3193 | if (ISD::isBuildVectorAllOnes(N1.getNode())) |
| 3194 | return N1; |
| Dan Gohman | 80f9f07 | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 3195 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3196 | |
| Dan Gohman | 06563a8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 3197 | // fold (or x, undef) -> -1 |
| Bob Wilson | 269a89f | 2010-06-28 23:40:25 +0000 | [diff] [blame] | 3198 | if (!LegalOperations && |
| 3199 | (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)) { |
| Nate Begeman | 9655f84 | 2009-12-03 07:11:29 +0000 | [diff] [blame] | 3200 | EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT; |
| 3201 | return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT); |
| 3202 | } |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3203 | // fold (or c1, c2) -> c1|c2 |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3204 | if (N0C && N1C) |
| Bill Wendling | dea9130 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 3205 | return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C); |
| Nate Begeman | 2cc2c9a | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 3206 | // canonicalize constant to RHS |
| Nate Begeman | 418c6e4 | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 3207 | if (N0C && !N1C) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3208 | return DAG.getNode(ISD::OR, SDLoc(N), VT, N1, N0); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3209 | // fold (or x, 0) -> x |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3210 | if (N1C && N1C->isNullValue()) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3211 | return N0; |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3212 | // fold (or x, -1) -> -1 |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3213 | if (N1C && N1C->isAllOnesValue()) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3214 | return N1; |
| Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3215 | // fold (or x, c) -> c iff (x & ~c) == 0 |
| Dan Gohman | 1f372ed | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 3216 | if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue())) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3217 | return N1; |
| Evan Cheng | 4c0bd96 | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 3218 | |
| 3219 | // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16) |
| 3220 | SDValue BSwap = MatchBSwapHWord(N, N0, N1); |
| 3221 | if (BSwap.getNode() != 0) |
| 3222 | return BSwap; |
| 3223 | BSwap = MatchBSwapHWordLow(N, N0, N1); |
| 3224 | if (BSwap.getNode() != 0) |
| 3225 | return BSwap; |
| 3226 | |
| Nate Begeman | 22e251a | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 3227 | // reassociate or |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3228 | SDValue ROR = ReassociateOps(ISD::OR, SDLoc(N), N0, N1); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3229 | if (ROR.getNode() != 0) |
| Nate Begeman | 22e251a | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 3230 | return ROR; |
| 3231 | // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2) |
| Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3232 | // iff (c1 & c2) == 0. |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3233 | if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() && |
| Chris Lattner | d8c5c06 | 2005-10-27 05:06:38 +0000 | [diff] [blame] | 3234 | isa<ConstantSDNode>(N0.getOperand(1))) { |
| Chris Lattner | d8c5c06 | 2005-10-27 05:06:38 +0000 | [diff] [blame] | 3235 | ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1)); |
| Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 3236 | if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0) { |
| 3237 | SDValue COR = DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1); |
| 3238 | if (!COR.getNode()) |
| 3239 | return SDValue(); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3240 | return DAG.getNode(ISD::AND, SDLoc(N), VT, |
| 3241 | DAG.getNode(ISD::OR, SDLoc(N0), VT, |
| Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 3242 | N0.getOperand(0), N1), COR); |
| 3243 | } |
| Nate Begeman | 85c1cc4 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 3244 | } |
| Nate Begeman | 049b748 | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3245 | // fold (or (setcc x), (setcc y)) -> (setcc (or x, y)) |
| 3246 | if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){ |
| 3247 | ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get(); |
| 3248 | ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3249 | |
| Nate Begeman | 049b748 | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3250 | if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 && |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3251 | LL.getValueType().isInteger()) { |
| Bill Wendling | f29b6e1 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3252 | // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0) |
| 3253 | // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0) |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3254 | if (cast<ConstantSDNode>(LR)->isNullValue() && |
| Nate Begeman | 049b748 | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3255 | (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3256 | SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(LR), |
| Bill Wendling | f29b6e1 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3257 | LR.getValueType(), LL, RL); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3258 | AddToWorkList(ORNode.getNode()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3259 | return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1); |
| Nate Begeman | 049b748 | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3260 | } |
| Bill Wendling | f29b6e1 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3261 | // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1) |
| 3262 | // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1) |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3263 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && |
| Nate Begeman | 049b748 | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3264 | (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3265 | SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(LR), |
| Bill Wendling | f29b6e1 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3266 | LR.getValueType(), LL, RL); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3267 | AddToWorkList(ANDNode.getNode()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3268 | return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1); |
| Nate Begeman | 049b748 | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3269 | } |
| 3270 | } |
| 3271 | // canonicalize equivalent to ll == rl |
| 3272 | if (LL == RR && LR == RL) { |
| 3273 | Op1 = ISD::getSetCCSwappedOperands(Op1); |
| 3274 | std::swap(RL, RR); |
| 3275 | } |
| 3276 | if (LL == RL && LR == RR) { |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3277 | bool isInteger = LL.getValueType().isInteger(); |
| Nate Begeman | 049b748 | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3278 | ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger); |
| Chris Lattner | 5fa1040 | 2008-10-28 07:11:07 +0000 | [diff] [blame] | 3279 | if (Result != ISD::SETCC_INVALID && |
| Patrik Hagglund | ffd057a | 2012-12-19 10:19:55 +0000 | [diff] [blame] | 3280 | (!LegalOperations || |
| Owen Anderson | cc06899 | 2013-02-14 09:07:33 +0000 | [diff] [blame] | 3281 | (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) && |
| 3282 | TLI.isOperationLegal(ISD::SETCC, |
| Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 3283 | getSetCCResultType(N0.getValueType()))))) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3284 | return DAG.getSetCC(SDLoc(N), N0.getValueType(), |
| Bill Wendling | f29b6e1 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3285 | LL, LR, Result); |
| Nate Begeman | 049b748 | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3286 | } |
| 3287 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3288 | |
| Bill Wendling | f29b6e1 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3289 | // Simplify: (or (op x...), (op y...)) -> (op (or x, y)) |
| Chris Lattner | 8d6fc20 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 3290 | if (N0.getOpcode() == N1.getOpcode()) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3291 | SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3292 | if (Tmp.getNode()) return Tmp; |
| Nate Begeman | 049b748 | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3293 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3294 | |
| Bill Wendling | f29b6e1 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3295 | // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible. |
| Chris Lattner | 46d710e | 2006-09-14 21:11:37 +0000 | [diff] [blame] | 3296 | if (N0.getOpcode() == ISD::AND && |
| 3297 | N1.getOpcode() == ISD::AND && |
| 3298 | N0.getOperand(1).getOpcode() == ISD::Constant && |
| 3299 | N1.getOperand(1).getOpcode() == ISD::Constant && |
| 3300 | // Don't increase # computations. |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3301 | (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) { |
| Chris Lattner | 46d710e | 2006-09-14 21:11:37 +0000 | [diff] [blame] | 3302 | // We can only do this xform if we know that bits from X that are set in C2 |
| 3303 | // but not in C1 are already zero. Likewise for Y. |
| Dan Gohman | 1f372ed | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 3304 | const APInt &LHSMask = |
| 3305 | cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); |
| 3306 | const APInt &RHSMask = |
| 3307 | cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3308 | |
| Dan Gohman | 309d3d5 | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 3309 | if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) && |
| 3310 | DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3311 | SDValue X = DAG.getNode(ISD::OR, SDLoc(N0), VT, |
| Bill Wendling | f29b6e1 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3312 | N0.getOperand(0), N1.getOperand(0)); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3313 | return DAG.getNode(ISD::AND, SDLoc(N), VT, X, |
| Bill Wendling | f29b6e1 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3314 | DAG.getConstant(LHSMask | RHSMask, VT)); |
| Chris Lattner | 46d710e | 2006-09-14 21:11:37 +0000 | [diff] [blame] | 3315 | } |
| 3316 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3317 | |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3318 | // See if this is some rotate idiom. |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3319 | if (SDNode *Rot = MatchRotate(N0, N1, SDLoc(N))) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3320 | return SDValue(Rot, 0); |
| Chris Lattner | 8d6fc20 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 3321 | |
| Dan Gohman | 600f62b | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 3322 | // Simplify the operands using demanded-bits information. |
| 3323 | if (!VT.isVector() && |
| 3324 | SimplifyDemandedBits(SDValue(N, 0))) |
| 3325 | return SDValue(N, 0); |
| 3326 | |
| Evan Cheng | f100557 | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 3327 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3328 | } |
| 3329 | |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3330 | /// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3331 | static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) { |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3332 | if (Op.getOpcode() == ISD::AND) { |
| Reid Spencer | de46e48 | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 3333 | if (isa<ConstantSDNode>(Op.getOperand(1))) { |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3334 | Mask = Op.getOperand(1); |
| 3335 | Op = Op.getOperand(0); |
| 3336 | } else { |
| 3337 | return false; |
| 3338 | } |
| 3339 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3340 | |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3341 | if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) { |
| 3342 | Shift = Op; |
| 3343 | return true; |
| 3344 | } |
| Bill Wendling | f29b6e1 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3345 | |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3346 | return false; |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3347 | } |
| 3348 | |
| Richard Sandiford | 15cfc1c | 2014-01-09 10:56:42 +0000 | [diff] [blame] | 3349 | // Return true if we can prove that, whenever Neg and Pos are both in the |
| 3350 | // range [0, OpSize), Neg == (Pos == 0 ? 0 : OpSize - Pos). This means that |
| Richard Sandiford | 0f264db | 2014-01-09 10:49:40 +0000 | [diff] [blame] | 3351 | // for two opposing shifts shift1 and shift2 and a value X with OpBits bits: |
| 3352 | // |
| 3353 | // (or (shift1 X, Neg), (shift2 X, Pos)) |
| 3354 | // |
| 3355 | // reduces to a rotate in direction shift2 by Pos and a rotate in direction |
| Richard Sandiford | 15cfc1c | 2014-01-09 10:56:42 +0000 | [diff] [blame] | 3356 | // shift1 by Neg. The range [0, OpSize) means that we only need to consider |
| 3357 | // shift amounts with defined behavior. |
| Richard Sandiford | 0f264db | 2014-01-09 10:49:40 +0000 | [diff] [blame] | 3358 | static bool matchRotateSub(SDValue Pos, SDValue Neg, unsigned OpSize) { |
| Richard Sandiford | 15cfc1c | 2014-01-09 10:56:42 +0000 | [diff] [blame] | 3359 | // If OpSize is a power of 2 then: |
| 3360 | // |
| 3361 | // (a) (Pos == 0 ? 0 : OpSize - Pos) == (OpSize - Pos) & (OpSize - 1) |
| 3362 | // (b) Neg == Neg & (OpSize - 1) whenever Neg is in [0, OpSize). |
| 3363 | // |
| 3364 | // So if OpSize is a power of 2 and Neg is (and Neg', OpSize-1), we check |
| 3365 | // for the stronger condition: |
| 3366 | // |
| 3367 | // Neg & (OpSize - 1) == (OpSize - Pos) & (OpSize - 1) [A] |
| 3368 | // |
| 3369 | // for all Neg and Pos. Since Neg & (OpSize - 1) == Neg' & (OpSize - 1) |
| 3370 | // we can just replace Neg with Neg' for the rest of the function. |
| 3371 | // |
| 3372 | // In other cases we check for the even stronger condition: |
| 3373 | // |
| 3374 | // Neg == OpSize - Pos [B] |
| 3375 | // |
| 3376 | // for all Neg and Pos. Note that the (or ...) then invokes undefined |
| 3377 | // behavior if Pos == 0 (and consequently Neg == OpSize). |
| 3378 | // |
| 3379 | // We could actually use [A] whenever OpSize is a power of 2, but the |
| 3380 | // only extra cases that it would match are those uninteresting ones |
| 3381 | // where Neg and Pos are never in range at the same time. E.g. for |
| 3382 | // OpSize == 32, using [A] would allow a Neg of the form (sub 64, Pos) |
| 3383 | // as well as (sub 32, Pos), but: |
| 3384 | // |
| 3385 | // (or (shift1 X, (sub 64, Pos)), (shift2 X, Pos)) |
| 3386 | // |
| 3387 | // always invokes undefined behavior for 32-bit X. |
| 3388 | // |
| 3389 | // Below, Mask == OpSize - 1 when using [A] and is all-ones otherwise. |
| 3390 | unsigned LoBits = 0; |
| 3391 | if (Neg.getOpcode() == ISD::AND && |
| 3392 | isPowerOf2_64(OpSize) && |
| 3393 | Neg.getOperand(1).getOpcode() == ISD::Constant && |
| 3394 | cast<ConstantSDNode>(Neg.getOperand(1))->getAPIntValue() == OpSize - 1) { |
| 3395 | Neg = Neg.getOperand(0); |
| 3396 | LoBits = Log2_64(OpSize); |
| 3397 | } |
| 3398 | |
| Richard Sandiford | 0f264db | 2014-01-09 10:49:40 +0000 | [diff] [blame] | 3399 | // Check whether Neg has the form (sub NegC, NegOp1) for some NegC and NegOp1. |
| 3400 | if (Neg.getOpcode() != ISD::SUB) |
| 3401 | return 0; |
| 3402 | ConstantSDNode *NegC = dyn_cast<ConstantSDNode>(Neg.getOperand(0)); |
| 3403 | if (!NegC) |
| 3404 | return 0; |
| 3405 | SDValue NegOp1 = Neg.getOperand(1); |
| 3406 | |
| Richard Sandiford | 15cfc1c | 2014-01-09 10:56:42 +0000 | [diff] [blame] | 3407 | // The condition we need is now: |
| 3408 | // |
| 3409 | // (NegC - NegOp1) & Mask == (OpSize - Pos) & Mask |
| 3410 | // |
| 3411 | // If NegOp1 == Pos then we need: |
| 3412 | // |
| 3413 | // OpSize & Mask == NegC & Mask |
| 3414 | // |
| 3415 | // (because "x & Mask" is a truncation and distributes through subtraction). |
| 3416 | APInt Width; |
| Richard Sandiford | 0f264db | 2014-01-09 10:49:40 +0000 | [diff] [blame] | 3417 | if (Pos == NegOp1) |
| Richard Sandiford | 15cfc1c | 2014-01-09 10:56:42 +0000 | [diff] [blame] | 3418 | Width = NegC->getAPIntValue(); |
| Richard Sandiford | 0f264db | 2014-01-09 10:49:40 +0000 | [diff] [blame] | 3419 | // Check for cases where Pos has the form (add NegOp1, PosC) for some PosC. |
| 3420 | // Then the condition we want to prove becomes: |
| Richard Sandiford | 0f264db | 2014-01-09 10:49:40 +0000 | [diff] [blame] | 3421 | // |
| Richard Sandiford | 15cfc1c | 2014-01-09 10:56:42 +0000 | [diff] [blame] | 3422 | // (NegC - NegOp1) & Mask == (OpSize - (NegOp1 + PosC)) & Mask |
| 3423 | // |
| 3424 | // which, again because "x & Mask" is a truncation, becomes: |
| 3425 | // |
| 3426 | // NegC & Mask == (OpSize - PosC) & Mask |
| 3427 | // OpSize & Mask == (NegC + PosC) & Mask |
| 3428 | else if (Pos.getOpcode() == ISD::ADD && |
| 3429 | Pos.getOperand(0) == NegOp1 && |
| 3430 | Pos.getOperand(1).getOpcode() == ISD::Constant) |
| 3431 | Width = (cast<ConstantSDNode>(Pos.getOperand(1))->getAPIntValue() + |
| 3432 | NegC->getAPIntValue()); |
| 3433 | else |
| 3434 | return false; |
| Richard Sandiford | 0f264db | 2014-01-09 10:49:40 +0000 | [diff] [blame] | 3435 | |
| Richard Sandiford | 15cfc1c | 2014-01-09 10:56:42 +0000 | [diff] [blame] | 3436 | // Now we just need to check that OpSize & Mask == Width & Mask. |
| 3437 | if (LoBits) |
| 3438 | return Width.getLoBits(LoBits) == 0; |
| 3439 | return Width == OpSize; |
| Richard Sandiford | 0f264db | 2014-01-09 10:49:40 +0000 | [diff] [blame] | 3440 | } |
| 3441 | |
| Richard Sandiford | 95c864d | 2014-01-08 15:40:47 +0000 | [diff] [blame] | 3442 | // A subroutine of MatchRotate used once we have found an OR of two opposite |
| 3443 | // shifts of Shifted. If Neg == <operand size> - Pos then the OR reduces |
| 3444 | // to both (PosOpcode Shifted, Pos) and (NegOpcode Shifted, Neg), with the |
| 3445 | // former being preferred if supported. InnerPos and InnerNeg are Pos and |
| 3446 | // Neg with outer conversions stripped away. |
| 3447 | SDNode *DAGCombiner::MatchRotatePosNeg(SDValue Shifted, SDValue Pos, |
| 3448 | SDValue Neg, SDValue InnerPos, |
| 3449 | SDValue InnerNeg, unsigned PosOpcode, |
| 3450 | unsigned NegOpcode, SDLoc DL) { |
| Richard Sandiford | 95c864d | 2014-01-08 15:40:47 +0000 | [diff] [blame] | 3451 | // fold (or (shl x, (*ext y)), |
| 3452 | // (srl x, (*ext (sub 32, y)))) -> |
| 3453 | // (rotl x, y) or (rotr x, (sub 32, y)) |
| 3454 | // |
| 3455 | // fold (or (shl x, (*ext (sub 32, y))), |
| 3456 | // (srl x, (*ext y))) -> |
| 3457 | // (rotr x, y) or (rotl x, (sub 32, y)) |
| 3458 | EVT VT = Shifted.getValueType(); |
| Richard Sandiford | 0f264db | 2014-01-09 10:49:40 +0000 | [diff] [blame] | 3459 | if (matchRotateSub(InnerPos, InnerNeg, VT.getSizeInBits())) { |
| Richard Sandiford | 95c864d | 2014-01-08 15:40:47 +0000 | [diff] [blame] | 3460 | bool HasPos = TLI.isOperationLegalOrCustom(PosOpcode, VT); |
| 3461 | return DAG.getNode(HasPos ? PosOpcode : NegOpcode, DL, VT, Shifted, |
| 3462 | HasPos ? Pos : Neg).getNode(); |
| 3463 | } |
| 3464 | |
| 3465 | // fold (or (shl (*ext x), (*ext y)), |
| 3466 | // (srl (*ext x), (*ext (sub 32, y)))) -> |
| 3467 | // (*ext (rotl x, y)) or (*ext (rotr x, (sub 32, y))) |
| 3468 | // |
| 3469 | // fold (or (shl (*ext x), (*ext (sub 32, y))), |
| 3470 | // (srl (*ext x), (*ext y))) -> |
| 3471 | // (*ext (rotr x, y)) or (*ext (rotl x, (sub 32, y))) |
| 3472 | if (Shifted.getOpcode() == ISD::ZERO_EXTEND || |
| 3473 | Shifted.getOpcode() == ISD::ANY_EXTEND) { |
| 3474 | SDValue InnerShifted = Shifted.getOperand(0); |
| 3475 | EVT InnerVT = InnerShifted.getValueType(); |
| 3476 | bool HasPosInner = TLI.isOperationLegalOrCustom(PosOpcode, InnerVT); |
| 3477 | if (HasPosInner || TLI.isOperationLegalOrCustom(NegOpcode, InnerVT)) { |
| Richard Sandiford | 0f264db | 2014-01-09 10:49:40 +0000 | [diff] [blame] | 3478 | if (matchRotateSub(InnerPos, InnerNeg, InnerVT.getSizeInBits())) { |
| Richard Sandiford | 95c864d | 2014-01-08 15:40:47 +0000 | [diff] [blame] | 3479 | SDValue V = DAG.getNode(HasPosInner ? PosOpcode : NegOpcode, DL, |
| 3480 | InnerVT, InnerShifted, HasPosInner ? Pos : Neg); |
| 3481 | return DAG.getNode(Shifted.getOpcode(), DL, VT, V).getNode(); |
| 3482 | } |
| 3483 | } |
| 3484 | } |
| 3485 | |
| 3486 | return 0; |
| 3487 | } |
| 3488 | |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3489 | // MatchRotate - Handle an 'or' of two operands. If this is one of the many |
| 3490 | // idioms for rotate, and if the target supports rotation instructions, generate |
| 3491 | // a rot[lr]. |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3492 | SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL) { |
| Duncan Sands | 8651e9c | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 3493 | // Must be a legal type. Expanded 'n promoted things won't work with rotates. |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3494 | EVT VT = LHS.getValueType(); |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3495 | if (!TLI.isTypeLegal(VT)) return 0; |
| 3496 | |
| 3497 | // The target must have at least one rotate flavor. |
| Dan Gohman | 4aa1846 | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 3498 | bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT); |
| 3499 | bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT); |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3500 | if (!HasROTL && !HasROTR) return 0; |
| Duncan Sands | 8651e9c | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 3501 | |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3502 | // Match "(X shl/srl V1) & V2" where V2 may not be present. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3503 | SDValue LHSShift; // The shift. |
| 3504 | SDValue LHSMask; // AND value if any. |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3505 | if (!MatchRotateHalf(LHS, LHSShift, LHSMask)) |
| 3506 | return 0; // Not part of a rotate. |
| 3507 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3508 | SDValue RHSShift; // The shift. |
| 3509 | SDValue RHSMask; // AND value if any. |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3510 | if (!MatchRotateHalf(RHS, RHSShift, RHSMask)) |
| 3511 | return 0; // Not part of a rotate. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3512 | |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3513 | if (LHSShift.getOperand(0) != RHSShift.getOperand(0)) |
| 3514 | return 0; // Not shifting the same value. |
| 3515 | |
| 3516 | if (LHSShift.getOpcode() == RHSShift.getOpcode()) |
| 3517 | return 0; // Shifts must disagree. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3518 | |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3519 | // Canonicalize shl to left side in a shl/srl pair. |
| 3520 | if (RHSShift.getOpcode() == ISD::SHL) { |
| 3521 | std::swap(LHS, RHS); |
| 3522 | std::swap(LHSShift, RHSShift); |
| 3523 | std::swap(LHSMask , RHSMask ); |
| 3524 | } |
| 3525 | |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3526 | unsigned OpSizeInBits = VT.getSizeInBits(); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3527 | SDValue LHSShiftArg = LHSShift.getOperand(0); |
| 3528 | SDValue LHSShiftAmt = LHSShift.getOperand(1); |
| Kai Nacke | d09bb46 | 2013-09-19 23:00:28 +0000 | [diff] [blame] | 3529 | SDValue RHSShiftArg = RHSShift.getOperand(0); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3530 | SDValue RHSShiftAmt = RHSShift.getOperand(1); |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3531 | |
| 3532 | // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1) |
| 3533 | // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2) |
| Scott Michel | 16627a5 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3534 | if (LHSShiftAmt.getOpcode() == ISD::Constant && |
| 3535 | RHSShiftAmt.getOpcode() == ISD::Constant) { |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3536 | uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue(); |
| 3537 | uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue(); |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3538 | if ((LShVal + RShVal) != OpSizeInBits) |
| 3539 | return 0; |
| 3540 | |
| Craig Topper | 65161fa | 2012-09-29 06:54:22 +0000 | [diff] [blame] | 3541 | SDValue Rot = DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT, |
| 3542 | LHSShiftArg, HasROTL ? LHSShiftAmt : RHSShiftAmt); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3543 | |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3544 | // If there is an AND of either shifted operand, apply it to the result. |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3545 | if (LHSMask.getNode() || RHSMask.getNode()) { |
| Dan Gohman | e1c4f99 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 3546 | APInt Mask = APInt::getAllOnesValue(OpSizeInBits); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3547 | |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3548 | if (LHSMask.getNode()) { |
| Dan Gohman | e1c4f99 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 3549 | APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal); |
| 3550 | Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits; |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3551 | } |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3552 | if (RHSMask.getNode()) { |
| Dan Gohman | e1c4f99 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 3553 | APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal); |
| 3554 | Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits; |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3555 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3556 | |
| Bill Wendling | 35972a9 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3557 | Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT)); |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3558 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3559 | |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3560 | return Rot.getNode(); |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3561 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3562 | |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3563 | // If there is a mask here, and we have a variable shift, we can't be sure |
| 3564 | // that we're masking out the right stuff. |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3565 | if (LHSMask.getNode() || RHSMask.getNode()) |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3566 | return 0; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3567 | |
| Benjamin Kramer | 64bdb29 | 2013-09-24 14:21:28 +0000 | [diff] [blame] | 3568 | // If the shift amount is sign/zext/any-extended just peel it off. |
| 3569 | SDValue LExtOp0 = LHSShiftAmt; |
| 3570 | SDValue RExtOp0 = RHSShiftAmt; |
| Craig Topper | 5f9791f | 2012-09-29 07:18:53 +0000 | [diff] [blame] | 3571 | if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND || |
| 3572 | LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND || |
| 3573 | LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND || |
| 3574 | LHSShiftAmt.getOpcode() == ISD::TRUNCATE) && |
| 3575 | (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND || |
| 3576 | RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND || |
| 3577 | RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND || |
| 3578 | RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) { |
| Benjamin Kramer | 64bdb29 | 2013-09-24 14:21:28 +0000 | [diff] [blame] | 3579 | LExtOp0 = LHSShiftAmt.getOperand(0); |
| 3580 | RExtOp0 = RHSShiftAmt.getOperand(0); |
| 3581 | } |
| 3582 | |
| Richard Sandiford | 95c864d | 2014-01-08 15:40:47 +0000 | [diff] [blame] | 3583 | SDNode *TryL = MatchRotatePosNeg(LHSShiftArg, LHSShiftAmt, RHSShiftAmt, |
| 3584 | LExtOp0, RExtOp0, ISD::ROTL, ISD::ROTR, DL); |
| 3585 | if (TryL) |
| 3586 | return TryL; |
| 3587 | |
| 3588 | SDNode *TryR = MatchRotatePosNeg(RHSShiftArg, RHSShiftAmt, LHSShiftAmt, |
| 3589 | RExtOp0, LExtOp0, ISD::ROTR, ISD::ROTL, DL); |
| 3590 | if (TryR) |
| 3591 | return TryR; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3592 | |
| Chris Lattner | 97614c8 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3593 | return 0; |
| 3594 | } |
| 3595 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3596 | SDValue DAGCombiner::visitXOR(SDNode *N) { |
| 3597 | SDValue N0 = N->getOperand(0); |
| 3598 | SDValue N1 = N->getOperand(1); |
| 3599 | SDValue LHS, RHS, CC; |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3600 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 3601 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3602 | EVT VT = N0.getValueType(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3603 | |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3604 | // fold vector ops |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3605 | if (VT.isVector()) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3606 | SDValue FoldedVOp = SimplifyVBinOp(N); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3607 | if (FoldedVOp.getNode()) return FoldedVOp; |
| Craig Topper | a183ddb | 2012-12-08 22:49:19 +0000 | [diff] [blame] | 3608 | |
| 3609 | // fold (xor x, 0) -> x, vector edition |
| 3610 | if (ISD::isBuildVectorAllZeros(N0.getNode())) |
| 3611 | return N1; |
| 3612 | if (ISD::isBuildVectorAllZeros(N1.getNode())) |
| 3613 | return N0; |
| Dan Gohman | 80f9f07 | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 3614 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3615 | |
| Evan Cheng | df1690d | 2008-03-25 20:08:07 +0000 | [diff] [blame] | 3616 | // fold (xor undef, undef) -> 0. This is a common idiom (misuse). |
| 3617 | if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF) |
| 3618 | return DAG.getConstant(0, VT); |
| Dan Gohman | 06563a8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 3619 | // fold (xor x, undef) -> undef |
| Dan Gohman | adb3d37 | 2007-07-10 15:19:29 +0000 | [diff] [blame] | 3620 | if (N0.getOpcode() == ISD::UNDEF) |
| 3621 | return N0; |
| 3622 | if (N1.getOpcode() == ISD::UNDEF) |
| Dan Gohman | 06563a8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 3623 | return N1; |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3624 | // fold (xor c1, c2) -> c1^c2 |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3625 | if (N0C && N1C) |
| Bill Wendling | dea9130 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 3626 | return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C); |
| Nate Begeman | 2cc2c9a | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 3627 | // canonicalize constant to RHS |
| Nate Begeman | 418c6e4 | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 3628 | if (N0C && !N1C) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3629 | return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3630 | // fold (xor x, 0) -> x |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3631 | if (N1C && N1C->isNullValue()) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3632 | return N0; |
| Nate Begeman | 22e251a | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 3633 | // reassociate xor |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3634 | SDValue RXOR = ReassociateOps(ISD::XOR, SDLoc(N), N0, N1); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3635 | if (RXOR.getNode() != 0) |
| Nate Begeman | 22e251a | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 3636 | return RXOR; |
| Bill Wendling | 49a5ce8 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3637 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3638 | // fold !(x cc y) -> (x !cc y) |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3639 | if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) { |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3640 | bool isInt = LHS.getValueType().isInteger(); |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3641 | ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(), |
| 3642 | isInt); |
| Bill Wendling | 49a5ce8 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3643 | |
| Patrik Hagglund | ffd057a | 2012-12-19 10:19:55 +0000 | [diff] [blame] | 3644 | if (!LegalOperations || |
| 3645 | TLI.isCondCodeLegal(NotCC, LHS.getSimpleValueType())) { |
| Bill Wendling | 49a5ce8 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3646 | switch (N0.getOpcode()) { |
| 3647 | default: |
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3648 | llvm_unreachable("Unhandled SetCC Equivalent!"); |
| Bill Wendling | 49a5ce8 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3649 | case ISD::SETCC: |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3650 | return DAG.getSetCC(SDLoc(N), VT, LHS, RHS, NotCC); |
| Bill Wendling | 49a5ce8 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3651 | case ISD::SELECT_CC: |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3652 | return DAG.getSelectCC(SDLoc(N), LHS, RHS, N0.getOperand(2), |
| Bill Wendling | 49a5ce8 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3653 | N0.getOperand(3), NotCC); |
| 3654 | } |
| 3655 | } |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3656 | } |
| Bill Wendling | 49a5ce8 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3657 | |
| Chris Lattner | 58c227b | 2007-09-10 21:39:07 +0000 | [diff] [blame] | 3658 | // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y))) |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3659 | if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND && |
| Gabor Greif | e12264b | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 3660 | N0.getNode()->hasOneUse() && |
| 3661 | isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){ |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3662 | SDValue V = N0.getOperand(0); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3663 | V = DAG.getNode(ISD::XOR, SDLoc(N0), V.getValueType(), V, |
| Duncan Sands | 56ab90d | 2007-10-10 09:54:50 +0000 | [diff] [blame] | 3664 | DAG.getConstant(1, V.getValueType())); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3665 | AddToWorkList(V.getNode()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3666 | return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, V); |
| Chris Lattner | 58c227b | 2007-09-10 21:39:07 +0000 | [diff] [blame] | 3667 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3668 | |
| Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3669 | // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3670 | if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 && |
| Nate Begeman | 2cc2c9a | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 3671 | (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3672 | SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1); |
| Nate Begeman | 2cc2c9a | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 3673 | if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) { |
| 3674 | unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND; |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3675 | LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS |
| 3676 | RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3677 | AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3678 | return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3679 | } |
| 3680 | } |
| Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3681 | // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3682 | if (N1C && N1C->isAllOnesValue() && |
| Nate Begeman | 2cc2c9a | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 3683 | (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3684 | SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1); |
| Nate Begeman | 2cc2c9a | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 3685 | if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) { |
| 3686 | unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND; |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3687 | LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS |
| 3688 | RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3689 | AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3690 | return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3691 | } |
| 3692 | } |
| David Majnemer | 386ab7f | 2013-05-08 06:44:42 +0000 | [diff] [blame] | 3693 | // fold (xor (and x, y), y) -> (and (not x), y) |
| 3694 | if (N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() && |
| Benjamin Kramer | bb1dd73 | 2013-11-17 10:40:03 +0000 | [diff] [blame] | 3695 | N0->getOperand(1) == N1) { |
| David Majnemer | 386ab7f | 2013-05-08 06:44:42 +0000 | [diff] [blame] | 3696 | SDValue X = N0->getOperand(0); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3697 | SDValue NotX = DAG.getNOT(SDLoc(X), X, VT); |
| David Majnemer | 386ab7f | 2013-05-08 06:44:42 +0000 | [diff] [blame] | 3698 | AddToWorkList(NotX.getNode()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3699 | return DAG.getNode(ISD::AND, SDLoc(N), VT, NotX, N1); |
| David Majnemer | 386ab7f | 2013-05-08 06:44:42 +0000 | [diff] [blame] | 3700 | } |
| Bill Wendling | 35972a9 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3701 | // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2)) |
| Nate Begeman | 85c1cc4 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 3702 | if (N1C && N0.getOpcode() == ISD::XOR) { |
| 3703 | ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0)); |
| 3704 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 3705 | if (N00C) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3706 | return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(1), |
| Bill Wendling | 35972a9 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3707 | DAG.getConstant(N1C->getAPIntValue() ^ |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3708 | N00C->getAPIntValue(), VT)); |
| Nate Begeman | 85c1cc4 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 3709 | if (N01C) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3710 | return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(0), |
| Bill Wendling | 35972a9 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3711 | DAG.getConstant(N1C->getAPIntValue() ^ |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3712 | N01C->getAPIntValue(), VT)); |
| Nate Begeman | 85c1cc4 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 3713 | } |
| 3714 | // fold (xor x, x) -> 0 |
| Eric Christopher | e5ca1e0 | 2011-02-16 04:50:12 +0000 | [diff] [blame] | 3715 | if (N0 == N1) |
| Hal Finkel | 6c29bd9 | 2013-07-09 17:02:45 +0000 | [diff] [blame] | 3716 | return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3717 | |
| Chris Lattner | 8d6fc20 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 3718 | // Simplify: xor (op x...), (op y...) -> (op (xor x, y)) |
| 3719 | if (N0.getOpcode() == N1.getOpcode()) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3720 | SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3721 | if (Tmp.getNode()) return Tmp; |
| Nate Begeman | 049b748 | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3722 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3723 | |
| Chris Lattner | 098c01e | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 3724 | // Simplify the expression using non-local knowledge. |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3725 | if (!VT.isVector() && |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3726 | SimplifyDemandedBits(SDValue(N, 0))) |
| 3727 | return SDValue(N, 0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3728 | |
| Evan Cheng | f100557 | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 3729 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3730 | } |
| 3731 | |
| Chris Lattner | 7c709a5 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3732 | /// visitShiftByConstant - Handle transforms common to the three shifts, when |
| 3733 | /// the shift amount is a constant. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3734 | SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) { |
| Juergen Ributzka | fa0eba6 | 2014-02-06 04:09:06 +0000 | [diff] [blame] | 3735 | assert(isa<ConstantSDNode>(N->getOperand(1)) && |
| 3736 | "Expected an ConstantSDNode operand."); |
| 3737 | // We can't and shouldn't fold opaque constants. |
| 3738 | if (cast<ConstantSDNode>(N->getOperand(1))->isOpaque()) |
| 3739 | return SDValue(); |
| 3740 | |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3741 | SDNode *LHS = N->getOperand(0).getNode(); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3742 | if (!LHS->hasOneUse()) return SDValue(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3743 | |
| Chris Lattner | 7c709a5 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3744 | // We want to pull some binops through shifts, so that we have (and (shift)) |
| 3745 | // instead of (shift (and)), likewise for add, or, xor, etc. This sort of |
| 3746 | // thing happens with address calculations, so it's important to canonicalize |
| 3747 | // it. |
| 3748 | bool HighBitSet = false; // Can we transform this if the high bit is set? |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3749 | |
| Chris Lattner | 7c709a5 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3750 | switch (LHS->getOpcode()) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3751 | default: return SDValue(); |
| Chris Lattner | 7c709a5 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3752 | case ISD::OR: |
| 3753 | case ISD::XOR: |
| 3754 | HighBitSet = false; // We can only transform sra if the high bit is clear. |
| 3755 | break; |
| 3756 | case ISD::AND: |
| 3757 | HighBitSet = true; // We can only transform sra if the high bit is set. |
| 3758 | break; |
| 3759 | case ISD::ADD: |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3760 | if (N->getOpcode() != ISD::SHL) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3761 | return SDValue(); // only shl(add) not sr[al](add). |
| Chris Lattner | 7c709a5 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3762 | HighBitSet = false; // We can only transform sra if the high bit is clear. |
| 3763 | break; |
| 3764 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3765 | |
| Juergen Ributzka | fa0eba6 | 2014-02-06 04:09:06 +0000 | [diff] [blame] | 3766 | // We require the RHS of the binop to be a constant and not opaque as well. |
| Chris Lattner | 7c709a5 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3767 | ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1)); |
| Juergen Ributzka | fa0eba6 | 2014-02-06 04:09:06 +0000 | [diff] [blame] | 3768 | if (!BinOpCst || BinOpCst->isOpaque()) return SDValue(); |
| Bill Wendling | d51e3ff | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3769 | |
| 3770 | // FIXME: disable this unless the input to the binop is a shift by a constant. |
| 3771 | // If it is not a shift, it pessimizes some common cases like: |
| Chris Lattner | eedaf92 | 2007-12-06 07:47:55 +0000 | [diff] [blame] | 3772 | // |
| Bill Wendling | d51e3ff | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3773 | // void foo(int *X, int i) { X[i & 1235] = 1; } |
| 3774 | // int bar(int *X, int i) { return X[i & 255]; } |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3775 | SDNode *BinOpLHSVal = LHS->getOperand(0).getNode(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3776 | if ((BinOpLHSVal->getOpcode() != ISD::SHL && |
| Chris Lattner | eedaf92 | 2007-12-06 07:47:55 +0000 | [diff] [blame] | 3777 | BinOpLHSVal->getOpcode() != ISD::SRA && |
| 3778 | BinOpLHSVal->getOpcode() != ISD::SRL) || |
| 3779 | !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1))) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3780 | return SDValue(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3781 | |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3782 | EVT VT = N->getValueType(0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3783 | |
| Bill Wendling | d51e3ff | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3784 | // If this is a signed shift right, and the high bit is modified by the |
| 3785 | // logical operation, do not perform the transformation. The highBitSet |
| 3786 | // boolean indicates the value of the high bit of the constant which would |
| 3787 | // cause it to be modified for this operation. |
| Chris Lattner | 7c709a5 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3788 | if (N->getOpcode() == ISD::SRA) { |
| Dan Gohman | e1c4f99 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 3789 | bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative(); |
| 3790 | if (BinOpRHSSignSet != HighBitSet) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3791 | return SDValue(); |
| Chris Lattner | 7c709a5 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3792 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3793 | |
| Chris Lattner | 7c709a5 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3794 | // Fold the constants, shifting the binop RHS by the shift amount. |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3795 | SDValue NewRHS = DAG.getNode(N->getOpcode(), SDLoc(LHS->getOperand(1)), |
| Bill Wendling | d51e3ff | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3796 | N->getValueType(0), |
| 3797 | LHS->getOperand(1), N->getOperand(1)); |
| Juergen Ributzka | fa0eba6 | 2014-02-06 04:09:06 +0000 | [diff] [blame] | 3798 | assert(isa<ConstantSDNode>(NewRHS) && "Folding was not successful!"); |
| Chris Lattner | 7c709a5 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3799 | |
| 3800 | // Create the new shift. |
| Eric Christopher | d9e8eac | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 3801 | SDValue NewShift = DAG.getNode(N->getOpcode(), |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3802 | SDLoc(LHS->getOperand(0)), |
| Bill Wendling | d51e3ff | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3803 | VT, LHS->getOperand(0), N->getOperand(1)); |
| Chris Lattner | 7c709a5 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3804 | |
| 3805 | // Create the new binop. |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3806 | return DAG.getNode(LHS->getOpcode(), SDLoc(N), VT, NewShift, NewRHS); |
| Chris Lattner | 7c709a5 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3807 | } |
| 3808 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3809 | SDValue DAGCombiner::visitSHL(SDNode *N) { |
| 3810 | SDValue N0 = N->getOperand(0); |
| 3811 | SDValue N1 = N->getOperand(1); |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3812 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 3813 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3814 | EVT VT = N0.getValueType(); |
| Dan Gohman | 1d459e4 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3815 | unsigned OpSizeInBits = VT.getScalarType().getSizeInBits(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3816 | |
| Daniel Sanders | a1840d2 | 2013-11-11 17:23:41 +0000 | [diff] [blame] | 3817 | // fold vector ops |
| 3818 | if (VT.isVector()) { |
| 3819 | SDValue FoldedVOp = SimplifyVBinOp(N); |
| 3820 | if (FoldedVOp.getNode()) return FoldedVOp; |
| Quentin Colombet | 4db08df | 2014-02-21 23:42:41 +0000 | [diff] [blame] | 3821 | |
| 3822 | BuildVectorSDNode *N1CV = dyn_cast<BuildVectorSDNode>(N1); |
| 3823 | // If setcc produces all-one true value then: |
| 3824 | // (shl (and (setcc) N01CV) N1CV) -> (and (setcc) N01CV<<N1CV) |
| 3825 | if (N1CV && N1CV->isConstant() && |
| 3826 | TLI.getBooleanContents(true) == |
| 3827 | TargetLowering::ZeroOrNegativeOneBooleanContent && |
| 3828 | N0.getOpcode() == ISD::AND) { |
| 3829 | SDValue N00 = N0->getOperand(0); |
| 3830 | SDValue N01 = N0->getOperand(1); |
| 3831 | BuildVectorSDNode *N01CV = dyn_cast<BuildVectorSDNode>(N01); |
| 3832 | |
| 3833 | if (N01CV && N01CV->isConstant() && N00.getOpcode() == ISD::SETCC) { |
| 3834 | SDValue C = DAG.FoldConstantArithmetic(ISD::SHL, VT, N01CV, N1CV); |
| 3835 | if (C.getNode()) |
| 3836 | return DAG.getNode(ISD::AND, SDLoc(N), VT, N00, C); |
| 3837 | } |
| 3838 | } |
| Daniel Sanders | a1840d2 | 2013-11-11 17:23:41 +0000 | [diff] [blame] | 3839 | } |
| 3840 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3841 | // fold (shl c1, c2) -> c1<<c2 |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3842 | if (N0C && N1C) |
| Bill Wendling | dea9130 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 3843 | return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3844 | // fold (shl 0, x) -> 0 |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3845 | if (N0C && N0C->isNullValue()) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3846 | return N0; |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3847 | // fold (shl x, c >= size(x)) -> undef |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3848 | if (N1C && N1C->getZExtValue() >= OpSizeInBits) |
| Dale Johannesen | 8493575 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3849 | return DAG.getUNDEF(VT); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3850 | // fold (shl x, 0) -> x |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3851 | if (N1C && N1C->isNullValue()) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3852 | return N0; |
| Chad Rosier | 818e116 | 2011-06-14 22:29:10 +0000 | [diff] [blame] | 3853 | // fold (shl undef, x) -> 0 |
| 3854 | if (N0.getOpcode() == ISD::UNDEF) |
| 3855 | return DAG.getConstant(0, VT); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3856 | // if (shl x, c) is known to be zero, return 0 |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3857 | if (DAG.MaskedValueIsZero(SDValue(N, 0), |
| Dan Gohman | 1d459e4 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3858 | APInt::getAllOnesValue(OpSizeInBits))) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3859 | return DAG.getConstant(0, VT); |
| Duncan Sands | 3ed7688 | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3860 | // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))). |
| Evan Cheng | cfb7f3a | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3861 | if (N1.getOpcode() == ISD::TRUNCATE && |
| Evan Cheng | 13beeeb1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3862 | N1.getOperand(0).getOpcode() == ISD::AND && |
| 3863 | N1.hasOneUse() && N1.getOperand(0).hasOneUse()) { |
| Evan Cheng | cfb7f3a | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3864 | SDValue N101 = N1.getOperand(0).getOperand(1); |
| Evan Cheng | 13beeeb1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3865 | if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) { |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3866 | EVT TruncVT = N1.getValueType(); |
| Evan Cheng | 13beeeb1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3867 | SDValue N100 = N1.getOperand(0).getOperand(0); |
| Duncan Sands | 3ed7688 | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3868 | APInt TruncC = N101C->getAPIntValue(); |
| Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 3869 | TruncC = TruncC.trunc(TruncVT.getSizeInBits()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3870 | return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0, |
| 3871 | DAG.getNode(ISD::AND, SDLoc(N), TruncVT, |
| Bill Wendling | a6c75ff | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 3872 | DAG.getNode(ISD::TRUNCATE, |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3873 | SDLoc(N), |
| Bill Wendling | a6c75ff | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 3874 | TruncVT, N100), |
| Dan Gohman | fb58faf | 2009-01-27 20:39:34 +0000 | [diff] [blame] | 3875 | DAG.getConstant(TruncC, TruncVT))); |
| Evan Cheng | cfb7f3a | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3876 | } |
| 3877 | } |
| 3878 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3879 | if (N1C && SimplifyDemandedBits(SDValue(N, 0))) |
| 3880 | return SDValue(N, 0); |
| Bill Wendling | d51e3ff | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3881 | |
| 3882 | // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2)) |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3883 | if (N1C && N0.getOpcode() == ISD::SHL && |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3884 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3885 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue(); |
| 3886 | uint64_t c2 = N1C->getZExtValue(); |
| Dale Johannesen | a94e36b | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 3887 | if (c1 + c2 >= OpSizeInBits) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3888 | return DAG.getConstant(0, VT); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3889 | return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0), |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3890 | DAG.getConstant(c1 + c2, N1.getValueType())); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3891 | } |
| Dale Johannesen | a94e36b | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 3892 | |
| 3893 | // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2))) |
| 3894 | // For this to be valid, the second form must not preserve any of the bits |
| 3895 | // that are shifted out by the inner shift in the first form. This means |
| 3896 | // the outer shift size must be >= the number of bits added by the ext. |
| 3897 | // As a corollary, we don't care what kind of ext it is. |
| 3898 | if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND || |
| 3899 | N0.getOpcode() == ISD::ANY_EXTEND || |
| 3900 | N0.getOpcode() == ISD::SIGN_EXTEND) && |
| 3901 | N0.getOperand(0).getOpcode() == ISD::SHL && |
| 3902 | isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) { |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 3903 | uint64_t c1 = |
| Dale Johannesen | a94e36b | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 3904 | cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue(); |
| 3905 | uint64_t c2 = N1C->getZExtValue(); |
| 3906 | EVT InnerShiftVT = N0.getOperand(0).getValueType(); |
| 3907 | uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits(); |
| 3908 | if (c2 >= OpSizeInBits - InnerShiftSize) { |
| 3909 | if (c1 + c2 >= OpSizeInBits) |
| 3910 | return DAG.getConstant(0, VT); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3911 | return DAG.getNode(ISD::SHL, SDLoc(N0), VT, |
| 3912 | DAG.getNode(N0.getOpcode(), SDLoc(N0), VT, |
| Dale Johannesen | a94e36b | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 3913 | N0.getOperand(0)->getOperand(0)), |
| 3914 | DAG.getConstant(c1 + c2, N1.getValueType())); |
| 3915 | } |
| 3916 | } |
| 3917 | |
| Andrea Di Biagio | 56ce9c4 | 2013-09-27 11:37:05 +0000 | [diff] [blame] | 3918 | // fold (shl (zext (srl x, C)), C) -> (zext (shl (srl x, C), C)) |
| 3919 | // Only fold this if the inner zext has no other uses to avoid increasing |
| 3920 | // the total number of instructions. |
| 3921 | if (N1C && N0.getOpcode() == ISD::ZERO_EXTEND && N0.hasOneUse() && |
| 3922 | N0.getOperand(0).getOpcode() == ISD::SRL && |
| 3923 | isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) { |
| 3924 | uint64_t c1 = |
| 3925 | cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue(); |
| 3926 | if (c1 < VT.getSizeInBits()) { |
| 3927 | uint64_t c2 = N1C->getZExtValue(); |
| 3928 | if (c1 == c2) { |
| 3929 | SDValue NewOp0 = N0.getOperand(0); |
| 3930 | EVT CountVT = NewOp0.getOperand(1).getValueType(); |
| 3931 | SDValue NewSHL = DAG.getNode(ISD::SHL, SDLoc(N), NewOp0.getValueType(), |
| 3932 | NewOp0, DAG.getConstant(c2, CountVT)); |
| Andrea Di Biagio | 561badf | 2013-10-17 11:02:58 +0000 | [diff] [blame] | 3933 | AddToWorkList(NewSHL.getNode()); |
| Andrea Di Biagio | 56ce9c4 | 2013-09-27 11:37:05 +0000 | [diff] [blame] | 3934 | return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N0), VT, NewSHL); |
| 3935 | } |
| 3936 | } |
| 3937 | } |
| 3938 | |
| Eli Friedman | 1877ac9 | 2011-06-09 22:14:44 +0000 | [diff] [blame] | 3939 | // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or |
| 3940 | // (and (srl x, (sub c1, c2), MASK) |
| Chandler Carruth | e041a30 | 2012-01-05 11:05:55 +0000 | [diff] [blame] | 3941 | // Only fold this if the inner shift has no other uses -- if it does, folding |
| 3942 | // this will increase the total number of instructions. |
| 3943 | if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse() && |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3944 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3945 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue(); |
| Evan Cheng | a7bb55e | 2009-07-21 05:40:15 +0000 | [diff] [blame] | 3946 | if (c1 < VT.getSizeInBits()) { |
| 3947 | uint64_t c2 = N1C->getZExtValue(); |
| Eli Friedman | 1877ac9 | 2011-06-09 22:14:44 +0000 | [diff] [blame] | 3948 | APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(), |
| 3949 | VT.getSizeInBits() - c1); |
| 3950 | SDValue Shift; |
| 3951 | if (c2 > c1) { |
| 3952 | Mask = Mask.shl(c2-c1); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3953 | Shift = DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0), |
| Eli Friedman | 1877ac9 | 2011-06-09 22:14:44 +0000 | [diff] [blame] | 3954 | DAG.getConstant(c2-c1, N1.getValueType())); |
| 3955 | } else { |
| 3956 | Mask = Mask.lshr(c1-c2); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3957 | Shift = DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0), |
| Eli Friedman | 1877ac9 | 2011-06-09 22:14:44 +0000 | [diff] [blame] | 3958 | DAG.getConstant(c1-c2, N1.getValueType())); |
| 3959 | } |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3960 | return DAG.getNode(ISD::AND, SDLoc(N0), VT, Shift, |
| Eli Friedman | 1877ac9 | 2011-06-09 22:14:44 +0000 | [diff] [blame] | 3961 | DAG.getConstant(Mask, VT)); |
| Evan Cheng | a7bb55e | 2009-07-21 05:40:15 +0000 | [diff] [blame] | 3962 | } |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3963 | } |
| Bill Wendling | d51e3ff | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3964 | // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1)) |
| Dan Gohman | 5758e1e | 2009-08-06 09:18:59 +0000 | [diff] [blame] | 3965 | if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) { |
| 3966 | SDValue HiBitsMask = |
| 3967 | DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(), |
| 3968 | VT.getSizeInBits() - |
| 3969 | N1C->getZExtValue()), |
| 3970 | VT); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3971 | return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0), |
| Dan Gohman | 5758e1e | 2009-08-06 09:18:59 +0000 | [diff] [blame] | 3972 | HiBitsMask); |
| 3973 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3974 | |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 3975 | if (N1C) { |
| 3976 | SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue()); |
| 3977 | if (NewSHL.getNode()) |
| 3978 | return NewSHL; |
| 3979 | } |
| 3980 | |
| Evan Cheng | f100557 | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 3981 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3982 | } |
| 3983 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3984 | SDValue DAGCombiner::visitSRA(SDNode *N) { |
| 3985 | SDValue N0 = N->getOperand(0); |
| 3986 | SDValue N1 = N->getOperand(1); |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3987 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 3988 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3989 | EVT VT = N0.getValueType(); |
| Dan Gohman | 1d459e4 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3990 | unsigned OpSizeInBits = VT.getScalarType().getSizeInBits(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3991 | |
| Daniel Sanders | a1840d2 | 2013-11-11 17:23:41 +0000 | [diff] [blame] | 3992 | // fold vector ops |
| 3993 | if (VT.isVector()) { |
| 3994 | SDValue FoldedVOp = SimplifyVBinOp(N); |
| 3995 | if (FoldedVOp.getNode()) return FoldedVOp; |
| 3996 | } |
| 3997 | |
| Bill Wendling | d51e3ff | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3998 | // fold (sra c1, c2) -> (sra c1, c2) |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3999 | if (N0C && N1C) |
| Bill Wendling | dea9130 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 4000 | return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4001 | // fold (sra 0, x) -> 0 |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 4002 | if (N0C && N0C->isNullValue()) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 4003 | return N0; |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4004 | // fold (sra -1, x) -> -1 |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 4005 | if (N0C && N0C->isAllOnesValue()) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 4006 | return N0; |
| Bill Wendling | d51e3ff | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 4007 | // fold (sra x, (setge c, size(x))) -> undef |
| Dan Gohman | 1d459e4 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 4008 | if (N1C && N1C->getZExtValue() >= OpSizeInBits) |
| Dale Johannesen | 8493575 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 4009 | return DAG.getUNDEF(VT); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4010 | // fold (sra x, 0) -> x |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 4011 | if (N1C && N1C->isNullValue()) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 4012 | return N0; |
| Nate Begeman | fb5dbad | 2006-02-17 19:54:08 +0000 | [diff] [blame] | 4013 | // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports |
| 4014 | // sext_inreg. |
| 4015 | if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) { |
| Dan Gohman | 1d459e4 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 4016 | unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue(); |
| Dan Gohman | 6bd3ef8 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 4017 | EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits); |
| 4018 | if (VT.isVector()) |
| 4019 | ExtVT = EVT::getVectorVT(*DAG.getContext(), |
| 4020 | ExtVT, VT.getVectorNumElements()); |
| 4021 | if ((!LegalOperations || |
| 4022 | TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT))) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4023 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, |
| Dan Gohman | 6bd3ef8 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 4024 | N0.getOperand(0), DAG.getValueType(ExtVT)); |
| Nate Begeman | fb5dbad | 2006-02-17 19:54:08 +0000 | [diff] [blame] | 4025 | } |
| Duncan Sands | 8651e9c | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 4026 | |
| Bill Wendling | d51e3ff | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 4027 | // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2)) |
| Chris Lattner | 0f8a727 | 2006-02-28 06:23:04 +0000 | [diff] [blame] | 4028 | if (N1C && N0.getOpcode() == ISD::SRA) { |
| 4029 | if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4030 | unsigned Sum = N1C->getZExtValue() + C1->getZExtValue(); |
| Dan Gohman | 1d459e4 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 4031 | if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1; |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4032 | return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0.getOperand(0), |
| Chris Lattner | 0f8a727 | 2006-02-28 06:23:04 +0000 | [diff] [blame] | 4033 | DAG.getConstant(Sum, N1C->getValueType(0))); |
| 4034 | } |
| 4035 | } |
| Christopher Lamb | 8fe9109 | 2008-03-19 08:30:06 +0000 | [diff] [blame] | 4036 | |
| Bill Wendling | d51e3ff | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 4037 | // fold (sra (shl X, m), (sub result_size, n)) |
| 4038 | // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4039 | // result_size - n != m. |
| 4040 | // If truncate is free for the target sext(shl) is likely to result in better |
| Christopher Lamb | 3e9f497 | 2008-03-20 04:31:39 +0000 | [diff] [blame] | 4041 | // code. |
| Christopher Lamb | 8fe9109 | 2008-03-19 08:30:06 +0000 | [diff] [blame] | 4042 | if (N0.getOpcode() == ISD::SHL) { |
| 4043 | // Get the two constanst of the shifts, CN0 = m, CN = n. |
| 4044 | const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 4045 | if (N01C && N1C) { |
| Christopher Lamb | 3e9f497 | 2008-03-20 04:31:39 +0000 | [diff] [blame] | 4046 | // Determine what the truncate's result bitsize and type would be. |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4047 | EVT TruncVT = |
| Eric Christopher | d9e8eac | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 4048 | EVT::getIntegerVT(*DAG.getContext(), |
| 4049 | OpSizeInBits - N1C->getZExtValue()); |
| Christopher Lamb | 3e9f497 | 2008-03-20 04:31:39 +0000 | [diff] [blame] | 4050 | // Determine the residual right-shift amount. |
| Torok Edwin | be6a9a1 | 2009-05-23 17:29:48 +0000 | [diff] [blame] | 4051 | signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue(); |
| Duncan Sands | 8651e9c | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 4052 | |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4053 | // If the shift is not a no-op (in which case this should be just a sign |
| 4054 | // extend already), the truncated to type is legal, sign_extend is legal |
| Dan Gohman | 4a61882 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 4055 | // on that type, and the truncate to that type is both legal and free, |
| Christopher Lamb | 3e9f497 | 2008-03-20 04:31:39 +0000 | [diff] [blame] | 4056 | // perform the transform. |
| Torok Edwin | be6a9a1 | 2009-05-23 17:29:48 +0000 | [diff] [blame] | 4057 | if ((ShiftAmt > 0) && |
| Dan Gohman | 4aa1846 | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 4058 | TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) && |
| 4059 | TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) && |
| Evan Cheng | 7a3e750 | 2008-03-20 02:18:41 +0000 | [diff] [blame] | 4060 | TLI.isTruncateFree(VT, TruncVT)) { |
| Christopher Lamb | 3e9f497 | 2008-03-20 04:31:39 +0000 | [diff] [blame] | 4061 | |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 4062 | SDValue Amt = DAG.getConstant(ShiftAmt, |
| 4063 | getShiftAmountTy(N0.getOperand(0).getValueType())); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4064 | SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0), VT, |
| Bill Wendling | d51e3ff | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 4065 | N0.getOperand(0), Amt); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4066 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), TruncVT, |
| Bill Wendling | d51e3ff | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 4067 | Shift); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4068 | return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), |
| Bill Wendling | d51e3ff | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 4069 | N->getValueType(0), Trunc); |
| Christopher Lamb | 8fe9109 | 2008-03-19 08:30:06 +0000 | [diff] [blame] | 4070 | } |
| 4071 | } |
| 4072 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4073 | |
| Duncan Sands | 3ed7688 | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 4074 | // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))). |
| Evan Cheng | cfb7f3a | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 4075 | if (N1.getOpcode() == ISD::TRUNCATE && |
| Evan Cheng | 13beeeb1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 4076 | N1.getOperand(0).getOpcode() == ISD::AND && |
| 4077 | N1.hasOneUse() && N1.getOperand(0).hasOneUse()) { |
| Evan Cheng | cfb7f3a | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 4078 | SDValue N101 = N1.getOperand(0).getOperand(1); |
| Evan Cheng | 13beeeb1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 4079 | if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) { |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4080 | EVT TruncVT = N1.getValueType(); |
| Evan Cheng | 13beeeb1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 4081 | SDValue N100 = N1.getOperand(0).getOperand(0); |
| Duncan Sands | 3ed7688 | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 4082 | APInt TruncC = N101C->getAPIntValue(); |
| Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 4083 | TruncC = TruncC.trunc(TruncVT.getScalarType().getSizeInBits()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4084 | return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0, |
| 4085 | DAG.getNode(ISD::AND, SDLoc(N), |
| Bill Wendling | d51e3ff | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 4086 | TruncVT, |
| Bill Wendling | 3b585af | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4087 | DAG.getNode(ISD::TRUNCATE, |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4088 | SDLoc(N), |
| Bill Wendling | 3b585af | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4089 | TruncVT, N100), |
| Dan Gohman | fb58faf | 2009-01-27 20:39:34 +0000 | [diff] [blame] | 4090 | DAG.getConstant(TruncC, TruncVT))); |
| Evan Cheng | cfb7f3a | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 4091 | } |
| 4092 | } |
| 4093 | |
| Benjamin Kramer | 946e152 | 2011-01-30 16:38:43 +0000 | [diff] [blame] | 4094 | // fold (sra (trunc (sr x, c1)), c2) -> (trunc (sra x, c1+c2)) |
| 4095 | // if c1 is equal to the number of bits the trunc removes |
| 4096 | if (N0.getOpcode() == ISD::TRUNCATE && |
| 4097 | (N0.getOperand(0).getOpcode() == ISD::SRL || |
| 4098 | N0.getOperand(0).getOpcode() == ISD::SRA) && |
| 4099 | N0.getOperand(0).hasOneUse() && |
| 4100 | N0.getOperand(0).getOperand(1).hasOneUse() && |
| 4101 | N1C && isa<ConstantSDNode>(N0.getOperand(0).getOperand(1))) { |
| 4102 | EVT LargeVT = N0.getOperand(0).getValueType(); |
| 4103 | ConstantSDNode *LargeShiftAmt = |
| 4104 | cast<ConstantSDNode>(N0.getOperand(0).getOperand(1)); |
| 4105 | |
| 4106 | if (LargeVT.getScalarType().getSizeInBits() - OpSizeInBits == |
| 4107 | LargeShiftAmt->getZExtValue()) { |
| 4108 | SDValue Amt = |
| 4109 | DAG.getConstant(LargeShiftAmt->getZExtValue() + N1C->getZExtValue(), |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 4110 | getShiftAmountTy(N0.getOperand(0).getOperand(0).getValueType())); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4111 | SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), LargeVT, |
| Benjamin Kramer | 946e152 | 2011-01-30 16:38:43 +0000 | [diff] [blame] | 4112 | N0.getOperand(0).getOperand(0), Amt); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4113 | return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, SRA); |
| Benjamin Kramer | 946e152 | 2011-01-30 16:38:43 +0000 | [diff] [blame] | 4114 | } |
| 4115 | } |
| 4116 | |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4117 | // Simplify, based on bits shifted out of the LHS. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4118 | if (N1C && SimplifyDemandedBits(SDValue(N, 0))) |
| 4119 | return SDValue(N, 0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4120 | |
| 4121 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4122 | // If the sign bit is known to be zero, switch this to a SRL. |
| Dan Gohman | 1f372ed | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 4123 | if (DAG.SignBitIsZero(N0)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4124 | return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, N1); |
| Chris Lattner | 7c709a5 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 4125 | |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 4126 | if (N1C) { |
| 4127 | SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue()); |
| 4128 | if (NewSRA.getNode()) |
| 4129 | return NewSRA; |
| 4130 | } |
| 4131 | |
| Evan Cheng | f100557 | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 4132 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4133 | } |
| 4134 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4135 | SDValue DAGCombiner::visitSRL(SDNode *N) { |
| 4136 | SDValue N0 = N->getOperand(0); |
| 4137 | SDValue N1 = N->getOperand(1); |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 4138 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 4139 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4140 | EVT VT = N0.getValueType(); |
| Dan Gohman | 1d459e4 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 4141 | unsigned OpSizeInBits = VT.getScalarType().getSizeInBits(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4142 | |
| Daniel Sanders | a1840d2 | 2013-11-11 17:23:41 +0000 | [diff] [blame] | 4143 | // fold vector ops |
| 4144 | if (VT.isVector()) { |
| 4145 | SDValue FoldedVOp = SimplifyVBinOp(N); |
| 4146 | if (FoldedVOp.getNode()) return FoldedVOp; |
| 4147 | } |
| 4148 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4149 | // fold (srl c1, c2) -> c1 >>u c2 |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 4150 | if (N0C && N1C) |
| Bill Wendling | dea9130 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 4151 | return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4152 | // fold (srl 0, x) -> 0 |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 4153 | if (N0C && N0C->isNullValue()) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 4154 | return N0; |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4155 | // fold (srl x, c >= size(x)) -> undef |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4156 | if (N1C && N1C->getZExtValue() >= OpSizeInBits) |
| Dale Johannesen | 8493575 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 4157 | return DAG.getUNDEF(VT); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4158 | // fold (srl x, 0) -> x |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 4159 | if (N1C && N1C->isNullValue()) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 4160 | return N0; |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4161 | // if (srl x, c) is known to be zero, return 0 |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4162 | if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0), |
| Dan Gohman | 1f372ed | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 4163 | APInt::getAllOnesValue(OpSizeInBits))) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 4164 | return DAG.getConstant(0, VT); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4165 | |
| Bill Wendling | d51e3ff | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 4166 | // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2)) |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4167 | if (N1C && N0.getOpcode() == ISD::SRL && |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4168 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4169 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue(); |
| 4170 | uint64_t c2 = N1C->getZExtValue(); |
| Dale Johannesen | a94e36b | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 4171 | if (c1 + c2 >= OpSizeInBits) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 4172 | return DAG.getConstant(0, VT); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4173 | return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0), |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 4174 | DAG.getConstant(c1 + c2, N1.getValueType())); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4175 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4176 | |
| Dale Johannesen | cd538af | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 4177 | // fold (srl (trunc (srl x, c1)), c2) -> 0 or (trunc (srl x, (add c1, c2))) |
| Dale Johannesen | cd538af | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 4178 | if (N1C && N0.getOpcode() == ISD::TRUNCATE && |
| 4179 | N0.getOperand(0).getOpcode() == ISD::SRL && |
| Dale Johannesen | 0a291a3 | 2010-12-20 20:10:50 +0000 | [diff] [blame] | 4180 | isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) { |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 4181 | uint64_t c1 = |
| Dale Johannesen | cd538af | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 4182 | cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue(); |
| 4183 | uint64_t c2 = N1C->getZExtValue(); |
| Dale Johannesen | a94e36b | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 4184 | EVT InnerShiftVT = N0.getOperand(0).getValueType(); |
| 4185 | EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType(); |
| Dale Johannesen | cd538af | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 4186 | uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits(); |
| Dale Johannesen | 0a291a3 | 2010-12-20 20:10:50 +0000 | [diff] [blame] | 4187 | // This is only valid if the OpSizeInBits + c1 = size of inner shift. |
| Dale Johannesen | cd538af | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 4188 | if (c1 + OpSizeInBits == InnerShiftSize) { |
| 4189 | if (c1 + c2 >= InnerShiftSize) |
| 4190 | return DAG.getConstant(0, VT); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4191 | return DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT, |
| 4192 | DAG.getNode(ISD::SRL, SDLoc(N0), InnerShiftVT, |
| Dale Johannesen | cd538af | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 4193 | N0.getOperand(0)->getOperand(0), |
| Dale Johannesen | a94e36b | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 4194 | DAG.getConstant(c1 + c2, ShiftCountVT))); |
| Dale Johannesen | cd538af | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 4195 | } |
| 4196 | } |
| 4197 | |
| Chris Lattner | f9b2e3c | 2010-04-15 05:28:43 +0000 | [diff] [blame] | 4198 | // fold (srl (shl x, c), c) -> (and x, cst2) |
| 4199 | if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 && |
| 4200 | N0.getValueSizeInBits() <= 64) { |
| 4201 | uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits(); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4202 | return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0), |
| Chris Lattner | f9b2e3c | 2010-04-15 05:28:43 +0000 | [diff] [blame] | 4203 | DAG.getConstant(~0ULL >> ShAmt, VT)); |
| 4204 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4205 | |
| Michael Liao | 62ebfd8 | 2013-06-21 18:45:27 +0000 | [diff] [blame] | 4206 | // fold (srl (anyextend x), c) -> (and (anyextend (srl x, c)), mask) |
| Chris Lattner | 57f8c5a | 2006-05-05 22:53:17 +0000 | [diff] [blame] | 4207 | if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) { |
| 4208 | // Shifting in all undef bits? |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4209 | EVT SmallVT = N0.getOperand(0).getValueType(); |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4210 | if (N1C->getZExtValue() >= SmallVT.getSizeInBits()) |
| Dale Johannesen | 8493575 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 4211 | return DAG.getUNDEF(VT); |
| Chris Lattner | 57f8c5a | 2006-05-05 22:53:17 +0000 | [diff] [blame] | 4212 | |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 4213 | if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) { |
| Owen Anderson | a519284 | 2011-04-14 17:30:49 +0000 | [diff] [blame] | 4214 | uint64_t ShiftAmt = N1C->getZExtValue(); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4215 | SDValue SmallShift = DAG.getNode(ISD::SRL, SDLoc(N0), SmallVT, |
| Owen Anderson | a519284 | 2011-04-14 17:30:49 +0000 | [diff] [blame] | 4216 | N0.getOperand(0), |
| 4217 | DAG.getConstant(ShiftAmt, getShiftAmountTy(SmallVT))); |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 4218 | AddToWorkList(SmallShift.getNode()); |
| Michael Liao | 62ebfd8 | 2013-06-21 18:45:27 +0000 | [diff] [blame] | 4219 | APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits()).lshr(ShiftAmt); |
| 4220 | return DAG.getNode(ISD::AND, SDLoc(N), VT, |
| 4221 | DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, SmallShift), |
| 4222 | DAG.getConstant(Mask, VT)); |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 4223 | } |
| Chris Lattner | 57f8c5a | 2006-05-05 22:53:17 +0000 | [diff] [blame] | 4224 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4225 | |
| Chris Lattner | 2e33fb4 | 2006-10-12 20:23:19 +0000 | [diff] [blame] | 4226 | // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign |
| 4227 | // bit, which is unmodified by sra. |
| Bill Wendling | d51e3ff | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 4228 | if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) { |
| Chris Lattner | 2e33fb4 | 2006-10-12 20:23:19 +0000 | [diff] [blame] | 4229 | if (N0.getOpcode() == ISD::SRA) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4230 | return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0), N1); |
| Chris Lattner | 2e33fb4 | 2006-10-12 20:23:19 +0000 | [diff] [blame] | 4231 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4232 | |
| Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 4233 | // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit). |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4234 | if (N1C && N0.getOpcode() == ISD::CTLZ && |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4235 | N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) { |
| Dan Gohman | d0ff91d | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 4236 | APInt KnownZero, KnownOne; |
| Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 4237 | DAG.ComputeMaskedBits(N0.getOperand(0), KnownZero, KnownOne); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4238 | |
| Chris Lattner | 4993249 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 4239 | // If any of the input bits are KnownOne, then the input couldn't be all |
| 4240 | // zeros, thus the result of the srl will always be zero. |
| Dan Gohman | d0ff91d | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 4241 | if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4242 | |
| Chris Lattner | 4993249 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 4243 | // If all of the bits input the to ctlz node are known to be zero, then |
| 4244 | // the result of the ctlz is "32" and the result of the shift is one. |
| Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 4245 | APInt UnknownBits = ~KnownZero; |
| Chris Lattner | 4993249 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 4246 | if (UnknownBits == 0) return DAG.getConstant(1, VT); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4247 | |
| Chris Lattner | 4993249 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 4248 | // Otherwise, check to see if there is exactly one bit input to the ctlz. |
| Bill Wendling | d51e3ff | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 4249 | if ((UnknownBits & (UnknownBits - 1)) == 0) { |
| Chris Lattner | 4993249 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 4250 | // Okay, we know that only that the single bit specified by UnknownBits |
| Bill Wendling | d51e3ff | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 4251 | // could be set on input to the CTLZ node. If this bit is set, the SRL |
| 4252 | // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair |
| 4253 | // to an SRL/XOR pair, which is likely to simplify more. |
| Dan Gohman | d0ff91d | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 4254 | unsigned ShAmt = UnknownBits.countTrailingZeros(); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4255 | SDValue Op = N0.getOperand(0); |
| Bill Wendling | d51e3ff | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 4256 | |
| Chris Lattner | 4993249 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 4257 | if (ShAmt) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4258 | Op = DAG.getNode(ISD::SRL, SDLoc(N0), VT, Op, |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 4259 | DAG.getConstant(ShAmt, getShiftAmountTy(Op.getValueType()))); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4260 | AddToWorkList(Op.getNode()); |
| Chris Lattner | 4993249 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 4261 | } |
| Bill Wendling | d51e3ff | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 4262 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4263 | return DAG.getNode(ISD::XOR, SDLoc(N), VT, |
| Bill Wendling | d51e3ff | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 4264 | Op, DAG.getConstant(1, VT)); |
| Chris Lattner | 4993249 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 4265 | } |
| 4266 | } |
| Evan Cheng | cfb7f3a | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 4267 | |
| Duncan Sands | 3ed7688 | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 4268 | // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))). |
| Evan Cheng | cfb7f3a | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 4269 | if (N1.getOpcode() == ISD::TRUNCATE && |
| Evan Cheng | 13beeeb1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 4270 | N1.getOperand(0).getOpcode() == ISD::AND && |
| 4271 | N1.hasOneUse() && N1.getOperand(0).hasOneUse()) { |
| Evan Cheng | cfb7f3a | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 4272 | SDValue N101 = N1.getOperand(0).getOperand(1); |
| Evan Cheng | 13beeeb1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 4273 | if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) { |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4274 | EVT TruncVT = N1.getValueType(); |
| Evan Cheng | 13beeeb1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 4275 | SDValue N100 = N1.getOperand(0).getOperand(0); |
| Duncan Sands | 3ed7688 | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 4276 | APInt TruncC = N101C->getAPIntValue(); |
| Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 4277 | TruncC = TruncC.trunc(TruncVT.getSizeInBits()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4278 | return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, |
| 4279 | DAG.getNode(ISD::AND, SDLoc(N), |
| Bill Wendling | d51e3ff | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 4280 | TruncVT, |
| Bill Wendling | 3b585af | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4281 | DAG.getNode(ISD::TRUNCATE, |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4282 | SDLoc(N), |
| Bill Wendling | 3b585af | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4283 | TruncVT, N100), |
| Dan Gohman | fb58faf | 2009-01-27 20:39:34 +0000 | [diff] [blame] | 4284 | DAG.getConstant(TruncC, TruncVT))); |
| Evan Cheng | cfb7f3a | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 4285 | } |
| 4286 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4287 | |
| Chris Lattner | f03c90b | 2007-04-18 03:06:49 +0000 | [diff] [blame] | 4288 | // fold operands of srl based on knowledge that the low bits are not |
| 4289 | // demanded. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4290 | if (N1C && SimplifyDemandedBits(SDValue(N, 0))) |
| 4291 | return SDValue(N, 0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4292 | |
| Evan Cheng | b175de6 | 2009-12-18 21:31:31 +0000 | [diff] [blame] | 4293 | if (N1C) { |
| 4294 | SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue()); |
| 4295 | if (NewSRL.getNode()) |
| 4296 | return NewSRL; |
| 4297 | } |
| 4298 | |
| Dan Gohman | 600f62b | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 4299 | // Attempt to convert a srl of a load into a narrower zero-extending load. |
| 4300 | SDValue NarrowLoad = ReduceLoadWidth(N); |
| 4301 | if (NarrowLoad.getNode()) |
| 4302 | return NarrowLoad; |
| 4303 | |
| Evan Cheng | b175de6 | 2009-12-18 21:31:31 +0000 | [diff] [blame] | 4304 | // Here is a common situation. We want to optimize: |
| 4305 | // |
| 4306 | // %a = ... |
| 4307 | // %b = and i32 %a, 2 |
| 4308 | // %c = srl i32 %b, 1 |
| 4309 | // brcond i32 %c ... |
| 4310 | // |
| 4311 | // into |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4312 | // |
| Evan Cheng | b175de6 | 2009-12-18 21:31:31 +0000 | [diff] [blame] | 4313 | // %a = ... |
| 4314 | // %b = and %a, 2 |
| 4315 | // %c = setcc eq %b, 0 |
| 4316 | // brcond %c ... |
| 4317 | // |
| 4318 | // However when after the source operand of SRL is optimized into AND, the SRL |
| 4319 | // itself may not be optimized further. Look for it and add the BRCOND into |
| 4320 | // the worklist. |
| Evan Cheng | 166a4e6 | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 4321 | if (N->hasOneUse()) { |
| 4322 | SDNode *Use = *N->use_begin(); |
| 4323 | if (Use->getOpcode() == ISD::BRCOND) |
| 4324 | AddToWorkList(Use); |
| 4325 | else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) { |
| 4326 | // Also look pass the truncate. |
| 4327 | Use = *Use->use_begin(); |
| 4328 | if (Use->getOpcode() == ISD::BRCOND) |
| 4329 | AddToWorkList(Use); |
| 4330 | } |
| 4331 | } |
| Evan Cheng | b175de6 | 2009-12-18 21:31:31 +0000 | [diff] [blame] | 4332 | |
| Evan Cheng | f100557 | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 4333 | return SDValue(); |
| Evan Cheng | e19aa5c | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 4334 | } |
| 4335 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4336 | SDValue DAGCombiner::visitCTLZ(SDNode *N) { |
| 4337 | SDValue N0 = N->getOperand(0); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4338 | EVT VT = N->getValueType(0); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4339 | |
| 4340 | // fold (ctlz c1) -> c2 |
| Chris Lattner | 7e7bcf3 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 4341 | if (isa<ConstantSDNode>(N0)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4342 | return DAG.getNode(ISD::CTLZ, SDLoc(N), VT, N0); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4343 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4344 | } |
| 4345 | |
| Chandler Carruth | 637cc6a | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 4346 | SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) { |
| 4347 | SDValue N0 = N->getOperand(0); |
| 4348 | EVT VT = N->getValueType(0); |
| 4349 | |
| 4350 | // fold (ctlz_zero_undef c1) -> c2 |
| 4351 | if (isa<ConstantSDNode>(N0)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4352 | return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SDLoc(N), VT, N0); |
| Chandler Carruth | 637cc6a | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 4353 | return SDValue(); |
| 4354 | } |
| 4355 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4356 | SDValue DAGCombiner::visitCTTZ(SDNode *N) { |
| 4357 | SDValue N0 = N->getOperand(0); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4358 | EVT VT = N->getValueType(0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4359 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4360 | // fold (cttz c1) -> c2 |
| Chris Lattner | 7e7bcf3 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 4361 | if (isa<ConstantSDNode>(N0)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4362 | return DAG.getNode(ISD::CTTZ, SDLoc(N), VT, N0); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4363 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4364 | } |
| 4365 | |
| Chandler Carruth | 637cc6a | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 4366 | SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) { |
| 4367 | SDValue N0 = N->getOperand(0); |
| 4368 | EVT VT = N->getValueType(0); |
| 4369 | |
| 4370 | // fold (cttz_zero_undef c1) -> c2 |
| 4371 | if (isa<ConstantSDNode>(N0)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4372 | return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, SDLoc(N), VT, N0); |
| Chandler Carruth | 637cc6a | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 4373 | return SDValue(); |
| 4374 | } |
| 4375 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4376 | SDValue DAGCombiner::visitCTPOP(SDNode *N) { |
| 4377 | SDValue N0 = N->getOperand(0); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4378 | EVT VT = N->getValueType(0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4379 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4380 | // fold (ctpop c1) -> c2 |
| Chris Lattner | 7e7bcf3 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 4381 | if (isa<ConstantSDNode>(N0)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4382 | return DAG.getNode(ISD::CTPOP, SDLoc(N), VT, N0); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4383 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4384 | } |
| 4385 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4386 | SDValue DAGCombiner::visitSELECT(SDNode *N) { |
| 4387 | SDValue N0 = N->getOperand(0); |
| 4388 | SDValue N1 = N->getOperand(1); |
| 4389 | SDValue N2 = N->getOperand(2); |
| Nate Begeman | 24a7eca | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4390 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 4391 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 4392 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4393 | EVT VT = N->getValueType(0); |
| 4394 | EVT VT0 = N0.getValueType(); |
| Nate Begeman | c760f80 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 4395 | |
| Bill Wendling | b6b6f46 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4396 | // fold (select C, X, X) -> X |
| Nate Begeman | 24a7eca | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4397 | if (N1 == N2) |
| 4398 | return N1; |
| Bill Wendling | b6b6f46 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4399 | // fold (select true, X, Y) -> X |
| Nate Begeman | 24a7eca | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4400 | if (N0C && !N0C->isNullValue()) |
| 4401 | return N1; |
| Bill Wendling | b6b6f46 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4402 | // fold (select false, X, Y) -> Y |
| Nate Begeman | 24a7eca | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4403 | if (N0C && N0C->isNullValue()) |
| 4404 | return N2; |
| Bill Wendling | b6b6f46 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4405 | // fold (select C, 1, X) -> (or C, X) |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4406 | if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4407 | return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2); |
| Bill Wendling | b6b6f46 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4408 | // fold (select C, 0, 1) -> (xor C, 1) |
| Bob Wilson | c2dc7ee | 2009-01-22 22:05:48 +0000 | [diff] [blame] | 4409 | if (VT.isInteger() && |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4410 | (VT0 == MVT::i1 || |
| Bob Wilson | c2dc7ee | 2009-01-22 22:05:48 +0000 | [diff] [blame] | 4411 | (VT0.isInteger() && |
| Nadav Rotem | 841c9a8 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 4412 | TLI.getBooleanContents(false) == |
| 4413 | TargetLowering::ZeroOrOneBooleanContent)) && |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 4414 | N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) { |
| Bill Wendling | b6b6f46 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4415 | SDValue XORNode; |
| Evan Cheng | f5a23ab | 2007-08-18 05:57:05 +0000 | [diff] [blame] | 4416 | if (VT == VT0) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4417 | return DAG.getNode(ISD::XOR, SDLoc(N), VT0, |
| Bill Wendling | b6b6f46 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4418 | N0, DAG.getConstant(1, VT0)); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4419 | XORNode = DAG.getNode(ISD::XOR, SDLoc(N0), VT0, |
| Bill Wendling | b6b6f46 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4420 | N0, DAG.getConstant(1, VT0)); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4421 | AddToWorkList(XORNode.getNode()); |
| Duncan Sands | 11dd424 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4422 | if (VT.bitsGT(VT0)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4423 | return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, XORNode); |
| 4424 | return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, XORNode); |
| Evan Cheng | f5a23ab | 2007-08-18 05:57:05 +0000 | [diff] [blame] | 4425 | } |
| Bill Wendling | b6b6f46 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4426 | // fold (select C, 0, X) -> (and (not C), X) |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4427 | if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4428 | SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT); |
| Bob Wilson | c589005 | 2009-01-22 17:39:32 +0000 | [diff] [blame] | 4429 | AddToWorkList(NOTNode.getNode()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4430 | return DAG.getNode(ISD::AND, SDLoc(N), VT, NOTNode, N2); |
| Nate Begeman | 24a7eca | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4431 | } |
| Bill Wendling | b6b6f46 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4432 | // fold (select C, X, 1) -> (or (not C), X) |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4433 | if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4434 | SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT); |
| Bob Wilson | c589005 | 2009-01-22 17:39:32 +0000 | [diff] [blame] | 4435 | AddToWorkList(NOTNode.getNode()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4436 | return DAG.getNode(ISD::OR, SDLoc(N), VT, NOTNode, N1); |
| Nate Begeman | 24a7eca | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4437 | } |
| Bill Wendling | b6b6f46 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4438 | // fold (select C, X, 0) -> (and C, X) |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4439 | if (VT == MVT::i1 && N2C && N2C->isNullValue()) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4440 | return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1); |
| Bill Wendling | b6b6f46 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4441 | // fold (select X, X, Y) -> (or X, Y) |
| 4442 | // fold (select X, 1, Y) -> (or X, Y) |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4443 | if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1))) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4444 | return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2); |
| Bill Wendling | b6b6f46 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4445 | // fold (select X, Y, X) -> (and X, Y) |
| 4446 | // fold (select X, Y, 0) -> (and X, Y) |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4447 | if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0))) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4448 | return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4449 | |
| Chris Lattner | 6c14c35 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 4450 | // If we can fold this based on the true/false value, do so. |
| 4451 | if (SimplifySelectOps(N, N1, N2)) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4452 | return SDValue(N, 0); // Don't revisit N. |
| Duncan Sands | 8651e9c | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 4453 | |
| Nate Begeman | c760f80 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 4454 | // fold selects based on a setcc into other things, such as min/max/abs |
| Anton Korobeynikov | 035eaac | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 4455 | if (N0.getOpcode() == ISD::SETCC) { |
| Nate Begeman | 7e7f439 | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4456 | // FIXME: |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4457 | // Check against MVT::Other for SELECT_CC, which is a workaround for targets |
| Nate Begeman | 7e7f439 | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4458 | // having to say they don't support SELECT_CC on every type the DAG knows |
| 4459 | // about, since there is no way to mark an opcode illegal at all value types |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4460 | if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) && |
| Dan Gohman | 3f32384 | 2009-08-02 16:19:38 +0000 | [diff] [blame] | 4461 | TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4462 | return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, |
| Bill Wendling | b6b6f46 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4463 | N0.getOperand(0), N0.getOperand(1), |
| Nate Begeman | 7e7f439 | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4464 | N1, N2, N0.getOperand(2)); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4465 | return SimplifySelect(SDLoc(N), N0, N1, N2); |
| Anton Korobeynikov | 035eaac | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 4466 | } |
| Bill Wendling | b6b6f46 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4467 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4468 | return SDValue(); |
| Nate Begeman | 24a7eca | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4469 | } |
| 4470 | |
| Tom Stellard | 9cbd2c5 | 2013-11-22 00:39:23 +0000 | [diff] [blame] | 4471 | static |
| 4472 | std::pair<SDValue, SDValue> SplitVSETCC(const SDNode *N, SelectionDAG &DAG) { |
| 4473 | SDLoc DL(N); |
| 4474 | EVT LoVT, HiVT; |
| 4475 | llvm::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0)); |
| 4476 | |
| 4477 | // Split the inputs. |
| 4478 | SDValue Lo, Hi, LL, LH, RL, RH; |
| 4479 | llvm::tie(LL, LH) = DAG.SplitVectorOperand(N, 0); |
| 4480 | llvm::tie(RL, RH) = DAG.SplitVectorOperand(N, 1); |
| 4481 | |
| 4482 | Lo = DAG.getNode(N->getOpcode(), DL, LoVT, LL, RL, N->getOperand(2)); |
| 4483 | Hi = DAG.getNode(N->getOpcode(), DL, HiVT, LH, RH, N->getOperand(2)); |
| 4484 | |
| 4485 | return std::make_pair(Lo, Hi); |
| 4486 | } |
| 4487 | |
| Benjamin Kramer | d56ffc7 | 2013-04-26 09:19:19 +0000 | [diff] [blame] | 4488 | SDValue DAGCombiner::visitVSELECT(SDNode *N) { |
| 4489 | SDValue N0 = N->getOperand(0); |
| 4490 | SDValue N1 = N->getOperand(1); |
| 4491 | SDValue N2 = N->getOperand(2); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4492 | SDLoc DL(N); |
| Benjamin Kramer | d56ffc7 | 2013-04-26 09:19:19 +0000 | [diff] [blame] | 4493 | |
| 4494 | // Canonicalize integer abs. |
| 4495 | // vselect (setg[te] X, 0), X, -X -> |
| 4496 | // vselect (setgt X, -1), X, -X -> |
| 4497 | // vselect (setl[te] X, 0), -X, X -> |
| 4498 | // Y = sra (X, size(X)-1); xor (add (X, Y), Y) |
| 4499 | if (N0.getOpcode() == ISD::SETCC) { |
| 4500 | SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1); |
| 4501 | ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get(); |
| 4502 | bool isAbs = false; |
| 4503 | bool RHSIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode()); |
| 4504 | |
| 4505 | if (((RHSIsAllZeros && (CC == ISD::SETGT || CC == ISD::SETGE)) || |
| 4506 | (ISD::isBuildVectorAllOnes(RHS.getNode()) && CC == ISD::SETGT)) && |
| 4507 | N1 == LHS && N2.getOpcode() == ISD::SUB && N1 == N2.getOperand(1)) |
| 4508 | isAbs = ISD::isBuildVectorAllZeros(N2.getOperand(0).getNode()); |
| 4509 | else if ((RHSIsAllZeros && (CC == ISD::SETLT || CC == ISD::SETLE)) && |
| 4510 | N2 == LHS && N1.getOpcode() == ISD::SUB && N2 == N1.getOperand(1)) |
| 4511 | isAbs = ISD::isBuildVectorAllZeros(N1.getOperand(0).getNode()); |
| 4512 | |
| 4513 | if (isAbs) { |
| 4514 | EVT VT = LHS.getValueType(); |
| 4515 | SDValue Shift = DAG.getNode( |
| 4516 | ISD::SRA, DL, VT, LHS, |
| 4517 | DAG.getConstant(VT.getScalarType().getSizeInBits() - 1, VT)); |
| 4518 | SDValue Add = DAG.getNode(ISD::ADD, DL, VT, LHS, Shift); |
| 4519 | AddToWorkList(Shift.getNode()); |
| 4520 | AddToWorkList(Add.getNode()); |
| 4521 | return DAG.getNode(ISD::XOR, DL, VT, Add, Shift); |
| 4522 | } |
| 4523 | } |
| 4524 | |
| Tom Stellard | 9cbd2c5 | 2013-11-22 00:39:23 +0000 | [diff] [blame] | 4525 | // If the VSELECT result requires splitting and the mask is provided by a |
| 4526 | // SETCC, then split both nodes and its operands before legalization. This |
| 4527 | // prevents the type legalizer from unrolling SETCC into scalar comparisons |
| 4528 | // and enables future optimizations (e.g. min/max pattern matching on X86). |
| 4529 | if (N0.getOpcode() == ISD::SETCC) { |
| 4530 | EVT VT = N->getValueType(0); |
| Juergen Ributzka | 34c652d | 2013-11-13 01:57:54 +0000 | [diff] [blame] | 4531 | |
| Tom Stellard | 9cbd2c5 | 2013-11-22 00:39:23 +0000 | [diff] [blame] | 4532 | // Check if any splitting is required. |
| 4533 | if (TLI.getTypeAction(*DAG.getContext(), VT) != |
| 4534 | TargetLowering::TypeSplitVector) |
| 4535 | return SDValue(); |
| Juergen Ributzka | 34c652d | 2013-11-13 01:57:54 +0000 | [diff] [blame] | 4536 | |
| Tom Stellard | 9cbd2c5 | 2013-11-22 00:39:23 +0000 | [diff] [blame] | 4537 | SDValue Lo, Hi, CCLo, CCHi, LL, LH, RL, RH; |
| 4538 | llvm::tie(CCLo, CCHi) = SplitVSETCC(N0.getNode(), DAG); |
| 4539 | llvm::tie(LL, LH) = DAG.SplitVectorOperand(N, 1); |
| 4540 | llvm::tie(RL, RH) = DAG.SplitVectorOperand(N, 2); |
| Juergen Ributzka | 34c652d | 2013-11-13 01:57:54 +0000 | [diff] [blame] | 4541 | |
| Tom Stellard | 9cbd2c5 | 2013-11-22 00:39:23 +0000 | [diff] [blame] | 4542 | Lo = DAG.getNode(N->getOpcode(), DL, LL.getValueType(), CCLo, LL, RL); |
| 4543 | Hi = DAG.getNode(N->getOpcode(), DL, LH.getValueType(), CCHi, LH, RH); |
| Juergen Ributzka | 34c652d | 2013-11-13 01:57:54 +0000 | [diff] [blame] | 4544 | |
| Tom Stellard | 9cbd2c5 | 2013-11-22 00:39:23 +0000 | [diff] [blame] | 4545 | // Add the new VSELECT nodes to the work list in case they need to be split |
| 4546 | // again. |
| 4547 | AddToWorkList(Lo.getNode()); |
| 4548 | AddToWorkList(Hi.getNode()); |
| 4549 | |
| 4550 | return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Lo, Hi); |
| Juergen Ributzka | 34c652d | 2013-11-13 01:57:54 +0000 | [diff] [blame] | 4551 | } |
| 4552 | |
| Andrea Di Biagio | 23df4e4 | 2014-01-08 18:33:04 +0000 | [diff] [blame] | 4553 | // Fold (vselect (build_vector all_ones), N1, N2) -> N1 |
| 4554 | if (ISD::isBuildVectorAllOnes(N0.getNode())) |
| 4555 | return N1; |
| 4556 | // Fold (vselect (build_vector all_zeros), N1, N2) -> N2 |
| 4557 | if (ISD::isBuildVectorAllZeros(N0.getNode())) |
| 4558 | return N2; |
| 4559 | |
| Benjamin Kramer | d56ffc7 | 2013-04-26 09:19:19 +0000 | [diff] [blame] | 4560 | return SDValue(); |
| 4561 | } |
| 4562 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4563 | SDValue DAGCombiner::visitSELECT_CC(SDNode *N) { |
| 4564 | SDValue N0 = N->getOperand(0); |
| 4565 | SDValue N1 = N->getOperand(1); |
| 4566 | SDValue N2 = N->getOperand(2); |
| 4567 | SDValue N3 = N->getOperand(3); |
| 4568 | SDValue N4 = N->getOperand(4); |
| Nate Begeman | c760f80 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 4569 | ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4570 | |
| Nate Begeman | c760f80 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 4571 | // fold select_cc lhs, rhs, x, x, cc -> x |
| 4572 | if (N2 == N3) |
| 4573 | return N2; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4574 | |
| Chris Lattner | 8b68dec | 2006-09-20 06:19:26 +0000 | [diff] [blame] | 4575 | // Determine if the condition we're dealing with is constant |
| Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 4576 | SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()), |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4577 | N0, N1, CC, SDLoc(N), false); |
| Stephen Lin | 605207f | 2013-06-15 04:03:33 +0000 | [diff] [blame] | 4578 | if (SCC.getNode()) { |
| 4579 | AddToWorkList(SCC.getNode()); |
| Chris Lattner | 8b68dec | 2006-09-20 06:19:26 +0000 | [diff] [blame] | 4580 | |
| Stephen Lin | 605207f | 2013-06-15 04:03:33 +0000 | [diff] [blame] | 4581 | if (ConstantSDNode *SCCC = dyn_cast<ConstantSDNode>(SCC.getNode())) { |
| 4582 | if (!SCCC->isNullValue()) |
| 4583 | return N2; // cond always true -> true val |
| 4584 | else |
| 4585 | return N3; // cond always false -> false val |
| 4586 | } |
| 4587 | |
| 4588 | // Fold to a simpler select_cc |
| 4589 | if (SCC.getOpcode() == ISD::SETCC) |
| 4590 | return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N2.getValueType(), |
| 4591 | SCC.getOperand(0), SCC.getOperand(1), N2, N3, |
| 4592 | SCC.getOperand(2)); |
| Chris Lattner | 8b68dec | 2006-09-20 06:19:26 +0000 | [diff] [blame] | 4593 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4594 | |
| Chris Lattner | 6c14c35 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 4595 | // If we can fold this based on the true/false value, do so. |
| 4596 | if (SimplifySelectOps(N, N2, N3)) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4597 | return SDValue(N, 0); // Don't revisit N. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4598 | |
| Nate Begeman | c760f80 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 4599 | // fold select_cc into other things, such as min/max/abs |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4600 | return SimplifySelectCC(SDLoc(N), N0, N1, N2, N3, CC); |
| Nate Begeman | 24a7eca | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4601 | } |
| 4602 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4603 | SDValue DAGCombiner::visitSETCC(SDNode *N) { |
| Nate Begeman | 24a7eca | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4604 | return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1), |
| Dale Johannesen | f1163e9 | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 4605 | cast<CondCodeSDNode>(N->getOperand(2))->get(), |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4606 | SDLoc(N)); |
| Nate Begeman | 24a7eca | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4607 | } |
| 4608 | |
| Andrea Di Biagio | f09a357 | 2014-01-27 18:45:30 +0000 | [diff] [blame] | 4609 | // tryToFoldExtendOfConstant - Try to fold a sext/zext/aext |
| 4610 | // dag node into a ConstantSDNode or a build_vector of constants. |
| 4611 | // This function is called by the DAGCombiner when visiting sext/zext/aext |
| 4612 | // dag nodes (see for example method DAGCombiner::visitSIGN_EXTEND). |
| 4613 | // Vector extends are not folded if operations are legal; this is to |
| 4614 | // avoid introducing illegal build_vector dag nodes. |
| Andrea Di Biagio | b6d39af | 2014-01-28 12:53:56 +0000 | [diff] [blame] | 4615 | static SDNode *tryToFoldExtendOfConstant(SDNode *N, const TargetLowering &TLI, |
| 4616 | SelectionDAG &DAG, bool LegalTypes, |
| Andrea Di Biagio | f09a357 | 2014-01-27 18:45:30 +0000 | [diff] [blame] | 4617 | bool LegalOperations) { |
| 4618 | unsigned Opcode = N->getOpcode(); |
| 4619 | SDValue N0 = N->getOperand(0); |
| 4620 | EVT VT = N->getValueType(0); |
| 4621 | |
| 4622 | assert((Opcode == ISD::SIGN_EXTEND || Opcode == ISD::ZERO_EXTEND || |
| 4623 | Opcode == ISD::ANY_EXTEND) && "Expected EXTEND dag node in input!"); |
| 4624 | |
| 4625 | // fold (sext c1) -> c1 |
| 4626 | // fold (zext c1) -> c1 |
| 4627 | // fold (aext c1) -> c1 |
| 4628 | if (isa<ConstantSDNode>(N0)) |
| 4629 | return DAG.getNode(Opcode, SDLoc(N), VT, N0).getNode(); |
| 4630 | |
| 4631 | // fold (sext (build_vector AllConstants) -> (build_vector AllConstants) |
| 4632 | // fold (zext (build_vector AllConstants) -> (build_vector AllConstants) |
| 4633 | // fold (aext (build_vector AllConstants) -> (build_vector AllConstants) |
| Andrea Di Biagio | b6d39af | 2014-01-28 12:53:56 +0000 | [diff] [blame] | 4634 | EVT SVT = VT.getScalarType(); |
| 4635 | if (!(VT.isVector() && |
| 4636 | (!LegalTypes || (!LegalOperations && TLI.isTypeLegal(SVT))) && |
| Andrea Di Biagio | f09a357 | 2014-01-27 18:45:30 +0000 | [diff] [blame] | 4637 | ISD::isBuildVectorOfConstantSDNodes(N0.getNode()))) |
| 4638 | return 0; |
| 4639 | |
| 4640 | // We can fold this node into a build_vector. |
| Andrea Di Biagio | b6d39af | 2014-01-28 12:53:56 +0000 | [diff] [blame] | 4641 | unsigned VTBits = SVT.getSizeInBits(); |
| Andrea Di Biagio | f09a357 | 2014-01-27 18:45:30 +0000 | [diff] [blame] | 4642 | unsigned EVTBits = N0->getValueType(0).getScalarType().getSizeInBits(); |
| 4643 | unsigned ShAmt = VTBits - EVTBits; |
| 4644 | SmallVector<SDValue, 8> Elts; |
| 4645 | unsigned NumElts = N0->getNumOperands(); |
| 4646 | SDLoc DL(N); |
| 4647 | |
| 4648 | for (unsigned i=0; i != NumElts; ++i) { |
| 4649 | SDValue Op = N0->getOperand(i); |
| 4650 | if (Op->getOpcode() == ISD::UNDEF) { |
| Andrea Di Biagio | b6d39af | 2014-01-28 12:53:56 +0000 | [diff] [blame] | 4651 | Elts.push_back(DAG.getUNDEF(SVT)); |
| Andrea Di Biagio | f09a357 | 2014-01-27 18:45:30 +0000 | [diff] [blame] | 4652 | continue; |
| 4653 | } |
| 4654 | |
| 4655 | ConstantSDNode *CurrentND = cast<ConstantSDNode>(Op); |
| 4656 | const APInt &C = APInt(VTBits, CurrentND->getAPIntValue().getZExtValue()); |
| 4657 | if (Opcode == ISD::SIGN_EXTEND) |
| 4658 | Elts.push_back(DAG.getConstant(C.shl(ShAmt).ashr(ShAmt).getZExtValue(), |
| Andrea Di Biagio | b6d39af | 2014-01-28 12:53:56 +0000 | [diff] [blame] | 4659 | SVT)); |
| Andrea Di Biagio | f09a357 | 2014-01-27 18:45:30 +0000 | [diff] [blame] | 4660 | else |
| 4661 | Elts.push_back(DAG.getConstant(C.shl(ShAmt).lshr(ShAmt).getZExtValue(), |
| Andrea Di Biagio | b6d39af | 2014-01-28 12:53:56 +0000 | [diff] [blame] | 4662 | SVT)); |
| Andrea Di Biagio | f09a357 | 2014-01-27 18:45:30 +0000 | [diff] [blame] | 4663 | } |
| 4664 | |
| 4665 | return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], NumElts).getNode(); |
| 4666 | } |
| 4667 | |
| Evan Cheng | e106e2f | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4668 | // ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this: |
| Dan Gohman | 0e8d199 | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4669 | // "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))" |
| Evan Cheng | e106e2f | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4670 | // transformation. Returns true if extension are possible and the above |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4671 | // mentioned transformation is profitable. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4672 | static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0, |
| Evan Cheng | e106e2f | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4673 | unsigned ExtOpc, |
| Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 4674 | SmallVectorImpl<SDNode *> &ExtendNodes, |
| Dan Gohman | 619ef48 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 4675 | const TargetLowering &TLI) { |
| Evan Cheng | e106e2f | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4676 | bool HasCopyToRegUses = false; |
| 4677 | bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType()); |
| Gabor Greif | e12264b | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 4678 | for (SDNode::use_iterator UI = N0.getNode()->use_begin(), |
| 4679 | UE = N0.getNode()->use_end(); |
| Evan Cheng | e106e2f | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4680 | UI != UE; ++UI) { |
| Dan Gohman | 91e5dcb | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 4681 | SDNode *User = *UI; |
| Evan Cheng | e106e2f | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4682 | if (User == N) |
| 4683 | continue; |
| Dan Gohman | 0e8d199 | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4684 | if (UI.getUse().getResNo() != N0.getResNo()) |
| 4685 | continue; |
| Evan Cheng | e106e2f | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4686 | // FIXME: Only extend SETCC N, N and SETCC N, c for now. |
| Dan Gohman | 0e8d199 | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4687 | if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) { |
| Evan Cheng | e106e2f | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4688 | ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get(); |
| 4689 | if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC)) |
| 4690 | // Sign bits will be lost after a zext. |
| 4691 | return false; |
| 4692 | bool Add = false; |
| 4693 | for (unsigned i = 0; i != 2; ++i) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4694 | SDValue UseOp = User->getOperand(i); |
| Evan Cheng | e106e2f | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4695 | if (UseOp == N0) |
| 4696 | continue; |
| 4697 | if (!isa<ConstantSDNode>(UseOp)) |
| 4698 | return false; |
| 4699 | Add = true; |
| 4700 | } |
| 4701 | if (Add) |
| 4702 | ExtendNodes.push_back(User); |
| Dan Gohman | 0e8d199 | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4703 | continue; |
| Evan Cheng | e106e2f | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4704 | } |
| Dan Gohman | 0e8d199 | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4705 | // If truncates aren't free and there are users we can't |
| 4706 | // extend, it isn't worthwhile. |
| 4707 | if (!isTruncFree) |
| 4708 | return false; |
| 4709 | // Remember if this value is live-out. |
| 4710 | if (User->getOpcode() == ISD::CopyToReg) |
| 4711 | HasCopyToRegUses = true; |
| Evan Cheng | e106e2f | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4712 | } |
| 4713 | |
| 4714 | if (HasCopyToRegUses) { |
| 4715 | bool BothLiveOut = false; |
| 4716 | for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); |
| 4717 | UI != UE; ++UI) { |
| Dan Gohman | 0e8d199 | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4718 | SDUse &Use = UI.getUse(); |
| 4719 | if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) { |
| 4720 | BothLiveOut = true; |
| 4721 | break; |
| Evan Cheng | e106e2f | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4722 | } |
| 4723 | } |
| 4724 | if (BothLiveOut) |
| 4725 | // Both unextended and extended values are live out. There had better be |
| Bob Wilson | f9b96c4 | 2010-11-28 06:51:19 +0000 | [diff] [blame] | 4726 | // a good reason for the transformation. |
| Evan Cheng | e106e2f | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4727 | return ExtendNodes.size(); |
| 4728 | } |
| 4729 | return true; |
| 4730 | } |
| 4731 | |
| Craig Topper | e0b7118 | 2013-07-13 07:43:40 +0000 | [diff] [blame] | 4732 | void DAGCombiner::ExtendSetCCUses(const SmallVectorImpl<SDNode *> &SetCCs, |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4733 | SDValue Trunc, SDValue ExtLoad, SDLoc DL, |
| Nick Lewycky | 6d677cf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4734 | ISD::NodeType ExtType) { |
| 4735 | // Extend SetCC uses if necessary. |
| 4736 | for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) { |
| 4737 | SDNode *SetCC = SetCCs[i]; |
| 4738 | SmallVector<SDValue, 4> Ops; |
| 4739 | |
| 4740 | for (unsigned j = 0; j != 2; ++j) { |
| 4741 | SDValue SOp = SetCC->getOperand(j); |
| 4742 | if (SOp == Trunc) |
| 4743 | Ops.push_back(ExtLoad); |
| 4744 | else |
| 4745 | Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp)); |
| 4746 | } |
| 4747 | |
| 4748 | Ops.push_back(SetCC->getOperand(2)); |
| 4749 | CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0), |
| 4750 | &Ops[0], Ops.size())); |
| 4751 | } |
| 4752 | } |
| 4753 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4754 | SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) { |
| 4755 | SDValue N0 = N->getOperand(0); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4756 | EVT VT = N->getValueType(0); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4757 | |
| Andrea Di Biagio | b6d39af | 2014-01-28 12:53:56 +0000 | [diff] [blame] | 4758 | if (SDNode *Res = tryToFoldExtendOfConstant(N, TLI, DAG, LegalTypes, |
| 4759 | LegalOperations)) |
| Andrea Di Biagio | f09a357 | 2014-01-27 18:45:30 +0000 | [diff] [blame] | 4760 | return SDValue(Res, 0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4761 | |
| Nadav Rotem | 9450fcf | 2013-01-20 08:35:56 +0000 | [diff] [blame] | 4762 | // fold (sext (sext x)) -> (sext x) |
| 4763 | // fold (sext (aext x)) -> (sext x) |
| 4764 | if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4765 | return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, |
| Nadav Rotem | 9450fcf | 2013-01-20 08:35:56 +0000 | [diff] [blame] | 4766 | N0.getOperand(0)); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4767 | |
| Chris Lattner | fce448f | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 4768 | if (N0.getOpcode() == ISD::TRUNCATE) { |
| Dan Gohman | c1a4e21 | 2008-05-20 20:56:33 +0000 | [diff] [blame] | 4769 | // fold (sext (truncate (load x))) -> (sext (smaller load x)) |
| 4770 | // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n))) |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4771 | SDValue NarrowLoad = ReduceLoadWidth(N0.getNode()); |
| 4772 | if (NarrowLoad.getNode()) { |
| Dale Johannesen | ff384ad | 2010-05-25 17:50:03 +0000 | [diff] [blame] | 4773 | SDNode* oye = N0.getNode()->getOperand(0).getNode(); |
| 4774 | if (NarrowLoad.getNode() != N0.getNode()) { |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4775 | CombineTo(N0.getNode(), NarrowLoad); |
| Dale Johannesen | ff384ad | 2010-05-25 17:50:03 +0000 | [diff] [blame] | 4776 | // CombineTo deleted the truncate, if needed, but not what's under it. |
| 4777 | AddToWorkList(oye); |
| 4778 | } |
| Dan Gohman | be36f5c | 2009-04-27 02:00:55 +0000 | [diff] [blame] | 4779 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| Evan Cheng | a824e79 | 2007-03-23 02:16:52 +0000 | [diff] [blame] | 4780 | } |
| Evan Cheng | 464dc9b | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4781 | |
| Dan Gohman | c1a4e21 | 2008-05-20 20:56:33 +0000 | [diff] [blame] | 4782 | // See if the value being truncated is already sign extended. If so, just |
| 4783 | // eliminate the trunc/sext pair. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4784 | SDValue Op = N0.getOperand(0); |
| Dan Gohman | 6bd3ef8 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 4785 | unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits(); |
| 4786 | unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits(); |
| 4787 | unsigned DestBits = VT.getScalarType().getSizeInBits(); |
| Dan Gohman | 309d3d5 | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 4788 | unsigned NumSignBits = DAG.ComputeNumSignBits(Op); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4789 | |
| Chris Lattner | fce448f | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 4790 | if (OpBits == DestBits) { |
| 4791 | // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign |
| 4792 | // bits, it is already ready. |
| 4793 | if (NumSignBits > DestBits-MidBits) |
| 4794 | return Op; |
| 4795 | } else if (OpBits < DestBits) { |
| 4796 | // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign |
| 4797 | // bits, just sext from i32. |
| 4798 | if (NumSignBits > OpBits-MidBits) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4799 | return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, Op); |
| Chris Lattner | fce448f | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 4800 | } else { |
| 4801 | // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign |
| 4802 | // bits, just truncate to i32. |
| 4803 | if (NumSignBits > OpBits-MidBits) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4804 | return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op); |
| Chris Lattner | a31f0a6 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 4805 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4806 | |
| Chris Lattner | fce448f | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 4807 | // fold (sext (truncate x)) -> (sextinreg x). |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4808 | if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, |
| 4809 | N0.getValueType())) { |
| Dan Gohman | 6bd3ef8 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 4810 | if (OpBits < DestBits) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4811 | Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N0), VT, Op); |
| Dan Gohman | 6bd3ef8 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 4812 | else if (OpBits > DestBits) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4813 | Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT, Op); |
| 4814 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, Op, |
| Dan Gohman | 6bd3ef8 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 4815 | DAG.getValueType(N0.getValueType())); |
| Chris Lattner | fce448f | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 4816 | } |
| Chris Lattner | a31f0a6 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 4817 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4818 | |
| Evan Cheng | bce7c47 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 4819 | // fold (sext (load x)) -> (sext (truncate (sextload x))) |
| Nadav Rotem | 502f1b9 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 4820 | // None of the supported targets knows how to perform load and sign extend |
| Nadav Rotem | b009130 | 2011-02-27 07:40:43 +0000 | [diff] [blame] | 4821 | // on vectors in one instruction. We only perform this transformation on |
| 4822 | // scalars. |
| Nadav Rotem | 502f1b9 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 4823 | if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() && |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4824 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
| Evan Cheng | 07d53b1 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 4825 | TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) { |
| Evan Cheng | e106e2f | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4826 | bool DoXform = true; |
| 4827 | SmallVector<SDNode*, 4> SetCCs; |
| 4828 | if (!N0.hasOneUse()) |
| 4829 | DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI); |
| 4830 | if (DoXform) { |
| 4831 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4832 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT, |
| Dan Gohman | 0e8d199 | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4833 | LN0->getChain(), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 4834 | LN0->getBasePtr(), N0.getValueType(), |
| 4835 | LN0->getMemOperand()); |
| Evan Cheng | e106e2f | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4836 | CombineTo(N, ExtLoad); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4837 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), |
| Bill Wendling | c409318 | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4838 | N0.getValueType(), ExtLoad); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4839 | CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1)); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4840 | ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N), |
| Nick Lewycky | 6d677cf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4841 | ISD::SIGN_EXTEND); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4842 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| Evan Cheng | e106e2f | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4843 | } |
| Nate Begeman | 8caf81d | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 4844 | } |
| Chris Lattner | 7dac108 | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 4845 | |
| 4846 | // fold (sext (sextload x)) -> (sext (truncate (sextload x))) |
| 4847 | // fold (sext ( extload x)) -> (sext (truncate (sextload x))) |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4848 | if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) && |
| 4849 | ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) { |
| Evan Cheng | e71fe34d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4850 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
| Dan Gohman | 08c0a95 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 4851 | EVT MemVT = LN0->getMemoryVT(); |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4852 | if ((!LegalOperations && !LN0->isVolatile()) || |
| Dan Gohman | 08c0a95 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 4853 | TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4854 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT, |
| Bill Wendling | c409318 | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4855 | LN0->getChain(), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 4856 | LN0->getBasePtr(), MemVT, |
| 4857 | LN0->getMemOperand()); |
| Jim Laskey | 26df19a | 2006-12-15 21:38:30 +0000 | [diff] [blame] | 4858 | CombineTo(N, ExtLoad); |
| Gabor Greif | e12264b | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 4859 | CombineTo(N0.getNode(), |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4860 | DAG.getNode(ISD::TRUNCATE, SDLoc(N0), |
| Bill Wendling | c409318 | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4861 | N0.getValueType(), ExtLoad), |
| Jim Laskey | 26df19a | 2006-12-15 21:38:30 +0000 | [diff] [blame] | 4862 | ExtLoad.getValue(1)); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4863 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| Jim Laskey | 26df19a | 2006-12-15 21:38:30 +0000 | [diff] [blame] | 4864 | } |
| Chris Lattner | 7dac108 | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 4865 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4866 | |
| Nick Lewycky | 6d677cf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4867 | // fold (sext (and/or/xor (load x), cst)) -> |
| 4868 | // (and/or/xor (sextload x), (sext cst)) |
| 4869 | if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR || |
| 4870 | N0.getOpcode() == ISD::XOR) && |
| 4871 | isa<LoadSDNode>(N0.getOperand(0)) && |
| 4872 | N0.getOperand(1).getOpcode() == ISD::Constant && |
| 4873 | TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()) && |
| 4874 | (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) { |
| 4875 | LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0)); |
| 4876 | if (LN0->getExtensionType() != ISD::ZEXTLOAD) { |
| 4877 | bool DoXform = true; |
| 4878 | SmallVector<SDNode*, 4> SetCCs; |
| 4879 | if (!N0.hasOneUse()) |
| 4880 | DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::SIGN_EXTEND, |
| 4881 | SetCCs, TLI); |
| 4882 | if (DoXform) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4883 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(LN0), VT, |
| Nick Lewycky | 6d677cf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4884 | LN0->getChain(), LN0->getBasePtr(), |
| Nick Lewycky | 6d677cf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4885 | LN0->getMemoryVT(), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 4886 | LN0->getMemOperand()); |
| Nick Lewycky | 6d677cf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4887 | APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); |
| 4888 | Mask = Mask.sext(VT.getSizeInBits()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4889 | SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT, |
| Nick Lewycky | 6d677cf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4890 | ExtLoad, DAG.getConstant(Mask, VT)); |
| 4891 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4892 | SDLoc(N0.getOperand(0)), |
| Nick Lewycky | 6d677cf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4893 | N0.getOperand(0).getValueType(), ExtLoad); |
| 4894 | CombineTo(N, And); |
| 4895 | CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1)); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4896 | ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N), |
| Nick Lewycky | 6d677cf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4897 | ISD::SIGN_EXTEND); |
| 4898 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| 4899 | } |
| 4900 | } |
| 4901 | } |
| 4902 | |
| Chris Lattner | 65786b0 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 4903 | if (N0.getOpcode() == ISD::SETCC) { |
| Chris Lattner | 4ac6073 | 2009-07-08 00:31:33 +0000 | [diff] [blame] | 4904 | // sext(setcc) -> sext_in_reg(vsetcc) for vectors. |
| Dan Gohman | e82c25e | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 4905 | // Only do this before legalize for now. |
| Owen Anderson | 2d4cca3 | 2013-04-23 18:09:28 +0000 | [diff] [blame] | 4906 | if (VT.isVector() && !LegalOperations && |
| Stephen Lin | cfe7f35 | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 4907 | TLI.getBooleanContents(true) == |
| Owen Anderson | 2d4cca3 | 2013-04-23 18:09:28 +0000 | [diff] [blame] | 4908 | TargetLowering::ZeroOrNegativeOneBooleanContent) { |
| Dan Gohman | e82c25e | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 4909 | EVT N0VT = N0.getOperand(0).getValueType(); |
| Nadav Rotem | 9d376b6 | 2012-04-11 08:26:11 +0000 | [diff] [blame] | 4910 | // On some architectures (such as SSE/NEON/etc) the SETCC result type is |
| 4911 | // of the same size as the compared operands. Only optimize sext(setcc()) |
| 4912 | // if this is the case. |
| Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 4913 | EVT SVT = getSetCCResultType(N0VT); |
| Nadav Rotem | 9d376b6 | 2012-04-11 08:26:11 +0000 | [diff] [blame] | 4914 | |
| 4915 | // We know that the # elements of the results is the same as the |
| 4916 | // # elements of the compare (and the # elements of the compare result |
| 4917 | // for that matter). Check to see that they are the same size. If so, |
| 4918 | // we know that the element size of the sext'd result matches the |
| 4919 | // element size of the compare operands. |
| 4920 | if (VT.getSizeInBits() == SVT.getSizeInBits()) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4921 | return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0), |
| Duncan Sands | 41b4a6b | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 4922 | N0.getOperand(1), |
| 4923 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
| Matt Arsenault | 0412623 | 2013-05-17 21:43:43 +0000 | [diff] [blame] | 4924 | |
| Dan Gohman | e82c25e | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 4925 | // If the desired elements are smaller or larger than the source |
| 4926 | // elements we can use a matching integer vector type and then |
| 4927 | // truncate/sign extend |
| Matt Arsenault | 0412623 | 2013-05-17 21:43:43 +0000 | [diff] [blame] | 4928 | EVT MatchingVectorType = N0VT.changeVectorElementTypeToInteger(); |
| Craig Topper | 5f9791f | 2012-09-29 07:18:53 +0000 | [diff] [blame] | 4929 | if (SVT == MatchingVectorType) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4930 | SDValue VsetCC = DAG.getSetCC(SDLoc(N), MatchingVectorType, |
| Craig Topper | 5f9791f | 2012-09-29 07:18:53 +0000 | [diff] [blame] | 4931 | N0.getOperand(0), N0.getOperand(1), |
| 4932 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4933 | return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT); |
| Dan Gohman | e82c25e | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 4934 | } |
| Chris Lattner | 4ac6073 | 2009-07-08 00:31:33 +0000 | [diff] [blame] | 4935 | } |
| Dan Gohman | e82c25e | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 4936 | |
| Matt Arsenault | 5f2a92a | 2014-01-27 21:41:54 +0000 | [diff] [blame] | 4937 | // sext(setcc x, y, cc) -> (select (setcc x, y, cc), -1, 0) |
| Dan Gohman | 5544b0c | 2010-04-24 01:17:30 +0000 | [diff] [blame] | 4938 | unsigned ElementWidth = VT.getScalarType().getSizeInBits(); |
| Dan Gohman | 5758e1e | 2009-08-06 09:18:59 +0000 | [diff] [blame] | 4939 | SDValue NegOne = |
| Dan Gohman | 5544b0c | 2010-04-24 01:17:30 +0000 | [diff] [blame] | 4940 | DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4941 | SDValue SCC = |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4942 | SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1), |
| Dan Gohman | 5758e1e | 2009-08-06 09:18:59 +0000 | [diff] [blame] | 4943 | NegOne, DAG.getConstant(0, VT), |
| Chris Lattner | a083ffc | 2007-04-11 06:50:51 +0000 | [diff] [blame] | 4944 | cast<CondCodeSDNode>(N0.getOperand(2))->get(), true); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4945 | if (SCC.getNode()) return SCC; |
| Matt Arsenault | 5f2a92a | 2014-01-27 21:41:54 +0000 | [diff] [blame] | 4946 | |
| 4947 | if (!VT.isVector()) { |
| 4948 | EVT SetCCVT = getSetCCResultType(N0.getOperand(0).getValueType()); |
| 4949 | if (!LegalOperations || TLI.isOperationLegal(ISD::SETCC, SetCCVT)) { |
| 4950 | SDLoc DL(N); |
| 4951 | ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get(); |
| 4952 | SDValue SetCC = DAG.getSetCC(DL, |
| 4953 | SetCCVT, |
| 4954 | N0.getOperand(0), N0.getOperand(1), CC); |
| 4955 | EVT SelectVT = getSetCCResultType(VT); |
| 4956 | return DAG.getSelect(DL, VT, |
| 4957 | DAG.getSExtOrTrunc(SetCC, DL, SelectVT), |
| 4958 | NegOne, DAG.getConstant(0, VT)); |
| 4959 | |
| 4960 | } |
| Matt Arsenault | d2f0332 | 2013-06-14 22:04:37 +0000 | [diff] [blame] | 4961 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4962 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4963 | |
| Dan Gohman | 3eb10f7 | 2008-04-28 16:58:24 +0000 | [diff] [blame] | 4964 | // fold (sext x) -> (zext x) if the sign bit is known zero. |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4965 | if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) && |
| Dan Gohman | c968c1f | 2008-04-28 18:47:17 +0000 | [diff] [blame] | 4966 | DAG.SignBitIsZero(N0)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4967 | return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4968 | |
| Evan Cheng | f100557 | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 4969 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4970 | } |
| 4971 | |
| Rafael Espindola | 8f62b32 | 2012-04-09 16:06:03 +0000 | [diff] [blame] | 4972 | // isTruncateOf - If N is a truncate of some other value, return true, record |
| 4973 | // the value being truncated in Op and which of Op's bits are zero in KnownZero. |
| 4974 | // This function computes KnownZero to avoid a duplicated call to |
| 4975 | // ComputeMaskedBits in the caller. |
| 4976 | static bool isTruncateOf(SelectionDAG &DAG, SDValue N, SDValue &Op, |
| 4977 | APInt &KnownZero) { |
| 4978 | APInt KnownOne; |
| 4979 | if (N->getOpcode() == ISD::TRUNCATE) { |
| 4980 | Op = N->getOperand(0); |
| 4981 | DAG.ComputeMaskedBits(Op, KnownZero, KnownOne); |
| 4982 | return true; |
| 4983 | } |
| 4984 | |
| 4985 | if (N->getOpcode() != ISD::SETCC || N->getValueType(0) != MVT::i1 || |
| 4986 | cast<CondCodeSDNode>(N->getOperand(2))->get() != ISD::SETNE) |
| 4987 | return false; |
| 4988 | |
| 4989 | SDValue Op0 = N->getOperand(0); |
| 4990 | SDValue Op1 = N->getOperand(1); |
| 4991 | assert(Op0.getValueType() == Op1.getValueType()); |
| 4992 | |
| 4993 | ConstantSDNode *COp0 = dyn_cast<ConstantSDNode>(Op0); |
| 4994 | ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1); |
| Rafael Espindola | 1d9672b | 2012-04-10 00:16:22 +0000 | [diff] [blame] | 4995 | if (COp0 && COp0->isNullValue()) |
| Rafael Espindola | 8f62b32 | 2012-04-09 16:06:03 +0000 | [diff] [blame] | 4996 | Op = Op1; |
| Rafael Espindola | 1d9672b | 2012-04-10 00:16:22 +0000 | [diff] [blame] | 4997 | else if (COp1 && COp1->isNullValue()) |
| Rafael Espindola | 8f62b32 | 2012-04-09 16:06:03 +0000 | [diff] [blame] | 4998 | Op = Op0; |
| 4999 | else |
| 5000 | return false; |
| 5001 | |
| 5002 | DAG.ComputeMaskedBits(Op, KnownZero, KnownOne); |
| 5003 | |
| 5004 | if (!(KnownZero | APInt(Op.getValueSizeInBits(), 1)).isAllOnesValue()) |
| 5005 | return false; |
| 5006 | |
| 5007 | return true; |
| 5008 | } |
| 5009 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5010 | SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) { |
| 5011 | SDValue N0 = N->getOperand(0); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5012 | EVT VT = N->getValueType(0); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5013 | |
| Andrea Di Biagio | b6d39af | 2014-01-28 12:53:56 +0000 | [diff] [blame] | 5014 | if (SDNode *Res = tryToFoldExtendOfConstant(N, TLI, DAG, LegalTypes, |
| 5015 | LegalOperations)) |
| Andrea Di Biagio | f09a357 | 2014-01-27 18:45:30 +0000 | [diff] [blame] | 5016 | return SDValue(Res, 0); |
| 5017 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5018 | // fold (zext (zext x)) -> (zext x) |
| Chris Lattner | 7e7bcf3 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 5019 | // fold (zext (aext x)) -> (zext x) |
| 5020 | if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5021 | return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, |
| Bill Wendling | c409318 | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 5022 | N0.getOperand(0)); |
| Chris Lattner | a31f0a6 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 5023 | |
| Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 5024 | // fold (zext (truncate x)) -> (zext x) or |
| 5025 | // (zext (truncate x)) -> (truncate x) |
| 5026 | // This is valid when the truncated bits of x are already zero. |
| 5027 | // FIXME: We should extend this to work for vectors too. |
| Rafael Espindola | 8f62b32 | 2012-04-09 16:06:03 +0000 | [diff] [blame] | 5028 | SDValue Op; |
| 5029 | APInt KnownZero; |
| 5030 | if (!VT.isVector() && isTruncateOf(DAG, N0, Op, KnownZero)) { |
| 5031 | APInt TruncatedBits = |
| 5032 | (Op.getValueSizeInBits() == N0.getValueSizeInBits()) ? |
| 5033 | APInt(Op.getValueSizeInBits(), 0) : |
| 5034 | APInt::getBitsSet(Op.getValueSizeInBits(), |
| 5035 | N0.getValueSizeInBits(), |
| 5036 | std::min(Op.getValueSizeInBits(), |
| 5037 | VT.getSizeInBits())); |
| Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 5038 | if (TruncatedBits == (KnownZero & TruncatedBits)) { |
| Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 5039 | if (VT.bitsGT(Op.getValueType())) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5040 | return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, Op); |
| Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 5041 | if (VT.bitsLT(Op.getValueType())) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5042 | return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op); |
| Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 5043 | |
| 5044 | return Op; |
| 5045 | } |
| 5046 | } |
| 5047 | |
| Evan Cheng | 464dc9b | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5048 | // fold (zext (truncate (load x))) -> (zext (smaller load x)) |
| 5049 | // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n))) |
| Dale Johannesen | 4bbd2ee | 2007-03-30 21:38:07 +0000 | [diff] [blame] | 5050 | if (N0.getOpcode() == ISD::TRUNCATE) { |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5051 | SDValue NarrowLoad = ReduceLoadWidth(N0.getNode()); |
| 5052 | if (NarrowLoad.getNode()) { |
| Dale Johannesen | ff384ad | 2010-05-25 17:50:03 +0000 | [diff] [blame] | 5053 | SDNode* oye = N0.getNode()->getOperand(0).getNode(); |
| 5054 | if (NarrowLoad.getNode() != N0.getNode()) { |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5055 | CombineTo(N0.getNode(), NarrowLoad); |
| Dale Johannesen | ff384ad | 2010-05-25 17:50:03 +0000 | [diff] [blame] | 5056 | // CombineTo deleted the truncate, if needed, but not what's under it. |
| 5057 | AddToWorkList(oye); |
| 5058 | } |
| Eli Friedman | 55b0acd | 2011-04-16 23:25:34 +0000 | [diff] [blame] | 5059 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| Evan Cheng | a824e79 | 2007-03-23 02:16:52 +0000 | [diff] [blame] | 5060 | } |
| Evan Cheng | 464dc9b | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5061 | } |
| 5062 | |
| Chris Lattner | a31f0a6 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 5063 | // fold (zext (truncate x)) -> (and x, mask) |
| 5064 | if (N0.getOpcode() == ISD::TRUNCATE && |
| Dan Gohman | 600f62b | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 5065 | (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) { |
| Dan Gohman | 68fb004 | 2010-11-03 01:47:46 +0000 | [diff] [blame] | 5066 | |
| 5067 | // fold (zext (truncate (load x))) -> (zext (smaller load x)) |
| 5068 | // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n))) |
| 5069 | SDValue NarrowLoad = ReduceLoadWidth(N0.getNode()); |
| 5070 | if (NarrowLoad.getNode()) { |
| 5071 | SDNode* oye = N0.getNode()->getOperand(0).getNode(); |
| 5072 | if (NarrowLoad.getNode() != N0.getNode()) { |
| 5073 | CombineTo(N0.getNode(), NarrowLoad); |
| 5074 | // CombineTo deleted the truncate, if needed, but not what's under it. |
| 5075 | AddToWorkList(oye); |
| 5076 | } |
| 5077 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| 5078 | } |
| 5079 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5080 | SDValue Op = N0.getOperand(0); |
| Duncan Sands | 11dd424 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 5081 | if (Op.getValueType().bitsLT(VT)) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5082 | Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, Op); |
| Elena Demikhovsky | 8d7e56c | 2012-04-22 09:39:03 +0000 | [diff] [blame] | 5083 | AddToWorkList(Op.getNode()); |
| Duncan Sands | 11dd424 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 5084 | } else if (Op.getValueType().bitsGT(VT)) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5085 | Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op); |
| Elena Demikhovsky | 8d7e56c | 2012-04-22 09:39:03 +0000 | [diff] [blame] | 5086 | AddToWorkList(Op.getNode()); |
| Chris Lattner | a31f0a6 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 5087 | } |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5088 | return DAG.getZeroExtendInReg(Op, SDLoc(N), |
| Dan Gohman | 1d459e4 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 5089 | N0.getValueType().getScalarType()); |
| Chris Lattner | a31f0a6 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 5090 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5091 | |
| Dan Gohman | ad3e549 | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 5092 | // Fold (zext (and (trunc x), cst)) -> (and x, cst), |
| 5093 | // if either of the casts is not free. |
| Chris Lattner | 8d8a3bf | 2006-09-21 06:14:31 +0000 | [diff] [blame] | 5094 | if (N0.getOpcode() == ISD::AND && |
| 5095 | N0.getOperand(0).getOpcode() == ISD::TRUNCATE && |
| Dan Gohman | ad3e549 | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 5096 | N0.getOperand(1).getOpcode() == ISD::Constant && |
| 5097 | (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(), |
| 5098 | N0.getValueType()) || |
| 5099 | !TLI.isZExtFree(N0.getValueType(), VT))) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5100 | SDValue X = N0.getOperand(0).getOperand(0); |
| Duncan Sands | 11dd424 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 5101 | if (X.getValueType().bitsLT(VT)) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5102 | X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(X), VT, X); |
| Duncan Sands | 11dd424 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 5103 | } else if (X.getValueType().bitsGT(VT)) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5104 | X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X); |
| Chris Lattner | 8d8a3bf | 2006-09-21 06:14:31 +0000 | [diff] [blame] | 5105 | } |
| Dan Gohman | e1c4f99 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 5106 | APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); |
| Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 5107 | Mask = Mask.zext(VT.getSizeInBits()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5108 | return DAG.getNode(ISD::AND, SDLoc(N), VT, |
| Bill Wendling | c409318 | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 5109 | X, DAG.getConstant(Mask, VT)); |
| Chris Lattner | 8d8a3bf | 2006-09-21 06:14:31 +0000 | [diff] [blame] | 5110 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5111 | |
| Evan Cheng | bce7c47 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 5112 | // fold (zext (load x)) -> (zext (truncate (zextload x))) |
| Nadav Rotem | 25f2ac9 | 2011-02-20 12:37:50 +0000 | [diff] [blame] | 5113 | // None of the supported targets knows how to perform load and vector_zext |
| Nadav Rotem | b009130 | 2011-02-27 07:40:43 +0000 | [diff] [blame] | 5114 | // on vectors in one instruction. We only perform this transformation on |
| 5115 | // scalars. |
| Nadav Rotem | 25f2ac9 | 2011-02-20 12:37:50 +0000 | [diff] [blame] | 5116 | if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() && |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5117 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
| Evan Cheng | 07d53b1 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 5118 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) { |
| Evan Cheng | e106e2f | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 5119 | bool DoXform = true; |
| 5120 | SmallVector<SDNode*, 4> SetCCs; |
| 5121 | if (!N0.hasOneUse()) |
| 5122 | DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI); |
| 5123 | if (DoXform) { |
| 5124 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5125 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT, |
| Bill Wendling | c409318 | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 5126 | LN0->getChain(), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 5127 | LN0->getBasePtr(), N0.getValueType(), |
| 5128 | LN0->getMemOperand()); |
| Evan Cheng | e106e2f | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 5129 | CombineTo(N, ExtLoad); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5130 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), |
| Bill Wendling | c409318 | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 5131 | N0.getValueType(), ExtLoad); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5132 | CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1)); |
| Bill Wendling | c409318 | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 5133 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5134 | ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N), |
| Nick Lewycky | 6d677cf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 5135 | ISD::ZERO_EXTEND); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5136 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| Evan Cheng | e106e2f | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 5137 | } |
| Evan Cheng | bce7c47 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 5138 | } |
| Chris Lattner | 7dac108 | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 5139 | |
| Nick Lewycky | 6d677cf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 5140 | // fold (zext (and/or/xor (load x), cst)) -> |
| 5141 | // (and/or/xor (zextload x), (zext cst)) |
| 5142 | if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR || |
| 5143 | N0.getOpcode() == ISD::XOR) && |
| 5144 | isa<LoadSDNode>(N0.getOperand(0)) && |
| 5145 | N0.getOperand(1).getOpcode() == ISD::Constant && |
| 5146 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()) && |
| 5147 | (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) { |
| 5148 | LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0)); |
| 5149 | if (LN0->getExtensionType() != ISD::SEXTLOAD) { |
| 5150 | bool DoXform = true; |
| 5151 | SmallVector<SDNode*, 4> SetCCs; |
| 5152 | if (!N0.hasOneUse()) |
| 5153 | DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::ZERO_EXTEND, |
| 5154 | SetCCs, TLI); |
| 5155 | if (DoXform) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5156 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), VT, |
| Nick Lewycky | 6d677cf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 5157 | LN0->getChain(), LN0->getBasePtr(), |
| Nick Lewycky | 6d677cf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 5158 | LN0->getMemoryVT(), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 5159 | LN0->getMemOperand()); |
| Nick Lewycky | 6d677cf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 5160 | APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); |
| 5161 | Mask = Mask.zext(VT.getSizeInBits()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5162 | SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT, |
| Nick Lewycky | 6d677cf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 5163 | ExtLoad, DAG.getConstant(Mask, VT)); |
| 5164 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5165 | SDLoc(N0.getOperand(0)), |
| Nick Lewycky | 6d677cf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 5166 | N0.getOperand(0).getValueType(), ExtLoad); |
| 5167 | CombineTo(N, And); |
| 5168 | CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1)); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5169 | ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N), |
| Nick Lewycky | 6d677cf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 5170 | ISD::ZERO_EXTEND); |
| 5171 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| 5172 | } |
| 5173 | } |
| 5174 | } |
| 5175 | |
| Chris Lattner | 7dac108 | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 5176 | // fold (zext (zextload x)) -> (zext (truncate (zextload x))) |
| 5177 | // fold (zext ( extload x)) -> (zext (truncate (zextload x))) |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5178 | if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) && |
| 5179 | ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) { |
| Evan Cheng | e71fe34d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5180 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
| Dan Gohman | 08c0a95 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 5181 | EVT MemVT = LN0->getMemoryVT(); |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5182 | if ((!LegalOperations && !LN0->isVolatile()) || |
| Dan Gohman | 08c0a95 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 5183 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5184 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT, |
| Bill Wendling | c409318 | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 5185 | LN0->getChain(), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 5186 | LN0->getBasePtr(), MemVT, |
| 5187 | LN0->getMemOperand()); |
| Duncan Sands | 8651e9c | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 5188 | CombineTo(N, ExtLoad); |
| Gabor Greif | e12264b | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 5189 | CombineTo(N0.getNode(), |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5190 | DAG.getNode(ISD::TRUNCATE, SDLoc(N0), N0.getValueType(), |
| Bill Wendling | c409318 | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 5191 | ExtLoad), |
| Duncan Sands | 8651e9c | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 5192 | ExtLoad.getValue(1)); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5193 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| Duncan Sands | 8651e9c | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 5194 | } |
| Chris Lattner | 7dac108 | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 5195 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5196 | |
| Chris Lattner | 65786b0 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 5197 | if (N0.getOpcode() == ISD::SETCC) { |
| Kevin Qin | ede9ce1 | 2013-12-30 02:05:13 +0000 | [diff] [blame] | 5198 | if (!LegalOperations && VT.isVector() && |
| 5199 | N0.getValueType().getVectorElementType() == MVT::i1) { |
| Elena Demikhovsky | 9d56f1e | 2014-01-22 12:26:19 +0000 | [diff] [blame] | 5200 | EVT N0VT = N0.getOperand(0).getValueType(); |
| 5201 | if (getSetCCResultType(N0VT) == N0.getValueType()) |
| 5202 | return SDValue(); |
| 5203 | |
| Evan Cheng | abd0ad5 | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 5204 | // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors. |
| 5205 | // Only do this before legalize for now. |
| Evan Cheng | abd0ad5 | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 5206 | EVT EltVT = VT.getVectorElementType(); |
| 5207 | SmallVector<SDValue,8> OneOps(VT.getVectorNumElements(), |
| 5208 | DAG.getConstant(1, EltVT)); |
| Dan Gohman | 4298df6 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 5209 | if (VT.getSizeInBits() == N0VT.getSizeInBits()) |
| Evan Cheng | abd0ad5 | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 5210 | // We know that the # elements of the results is the same as the |
| 5211 | // # elements of the compare (and the # elements of the compare result |
| 5212 | // for that matter). Check to see that they are the same size. If so, |
| 5213 | // we know that the element size of the sext'd result matches the |
| 5214 | // element size of the compare operands. |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5215 | return DAG.getNode(ISD::AND, SDLoc(N), VT, |
| 5216 | DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0), |
| Evan Cheng | abd0ad5 | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 5217 | N0.getOperand(1), |
| 5218 | cast<CondCodeSDNode>(N0.getOperand(2))->get()), |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5219 | DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, |
| Evan Cheng | abd0ad5 | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 5220 | &OneOps[0], OneOps.size())); |
| Dan Gohman | 4298df6 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 5221 | |
| 5222 | // If the desired elements are smaller or larger than the source |
| 5223 | // elements we can use a matching integer vector type and then |
| 5224 | // truncate/sign extend |
| 5225 | EVT MatchingElementType = |
| 5226 | EVT::getIntegerVT(*DAG.getContext(), |
| 5227 | N0VT.getScalarType().getSizeInBits()); |
| 5228 | EVT MatchingVectorType = |
| 5229 | EVT::getVectorVT(*DAG.getContext(), MatchingElementType, |
| 5230 | N0VT.getVectorNumElements()); |
| 5231 | SDValue VsetCC = |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5232 | DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0), |
| Dan Gohman | 4298df6 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 5233 | N0.getOperand(1), |
| 5234 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5235 | return DAG.getNode(ISD::AND, SDLoc(N), VT, |
| 5236 | DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT), |
| 5237 | DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, |
| Dan Gohman | 4298df6 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 5238 | &OneOps[0], OneOps.size())); |
| Evan Cheng | abd0ad5 | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 5239 | } |
| 5240 | |
| 5241 | // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5242 | SDValue SCC = |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5243 | SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1), |
| Chris Lattner | 65786b0 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 5244 | DAG.getConstant(1, VT), DAG.getConstant(0, VT), |
| Chris Lattner | a083ffc | 2007-04-11 06:50:51 +0000 | [diff] [blame] | 5245 | cast<CondCodeSDNode>(N0.getOperand(2))->get(), true); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5246 | if (SCC.getNode()) return SCC; |
| Chris Lattner | 65786b0 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 5247 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5248 | |
| Evan Cheng | 852c486 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 5249 | // (zext (shl (zext x), cst)) -> (shl (zext x), cst) |
| Evan Cheng | ca7c690 | 2009-12-15 00:41:36 +0000 | [diff] [blame] | 5250 | if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) && |
| Evan Cheng | 852c486 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 5251 | isa<ConstantSDNode>(N0.getOperand(1)) && |
| Evan Cheng | ca7c690 | 2009-12-15 00:41:36 +0000 | [diff] [blame] | 5252 | N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND && |
| 5253 | N0.hasOneUse()) { |
| Chris Lattner | e95d195 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 5254 | SDValue ShAmt = N0.getOperand(1); |
| 5255 | unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue(); |
| Evan Cheng | 852c486 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 5256 | if (N0.getOpcode() == ISD::SHL) { |
| Chris Lattner | e95d195 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 5257 | SDValue InnerZExt = N0.getOperand(0); |
| Evan Cheng | 852c486 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 5258 | // If the original shl may be shifting out bits, do not perform this |
| 5259 | // transformation. |
| Chris Lattner | e95d195 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 5260 | unsigned KnownZeroBits = InnerZExt.getValueType().getSizeInBits() - |
| 5261 | InnerZExt.getOperand(0).getValueType().getSizeInBits(); |
| 5262 | if (ShAmtVal > KnownZeroBits) |
| Evan Cheng | 852c486 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 5263 | return SDValue(); |
| 5264 | } |
| Chris Lattner | e95d195 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 5265 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5266 | SDLoc DL(N); |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5267 | |
| 5268 | // Ensure that the shift amount is wide enough for the shifted value. |
| Chris Lattner | e95d195 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 5269 | if (VT.getSizeInBits() >= 256) |
| 5270 | ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt); |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5271 | |
| Chris Lattner | e95d195 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 5272 | return DAG.getNode(N0.getOpcode(), DL, VT, |
| 5273 | DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)), |
| 5274 | ShAmt); |
| Evan Cheng | ca7c690 | 2009-12-15 00:41:36 +0000 | [diff] [blame] | 5275 | } |
| 5276 | |
| Evan Cheng | f100557 | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 5277 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5278 | } |
| 5279 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5280 | SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) { |
| 5281 | SDValue N0 = N->getOperand(0); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5282 | EVT VT = N->getValueType(0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5283 | |
| Andrea Di Biagio | b6d39af | 2014-01-28 12:53:56 +0000 | [diff] [blame] | 5284 | if (SDNode *Res = tryToFoldExtendOfConstant(N, TLI, DAG, LegalTypes, |
| 5285 | LegalOperations)) |
| Andrea Di Biagio | f09a357 | 2014-01-27 18:45:30 +0000 | [diff] [blame] | 5286 | return SDValue(Res, 0); |
| 5287 | |
| Chris Lattner | 812646a | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 5288 | // fold (aext (aext x)) -> (aext x) |
| 5289 | // fold (aext (zext x)) -> (zext x) |
| 5290 | // fold (aext (sext x)) -> (sext x) |
| 5291 | if (N0.getOpcode() == ISD::ANY_EXTEND || |
| 5292 | N0.getOpcode() == ISD::ZERO_EXTEND || |
| 5293 | N0.getOpcode() == ISD::SIGN_EXTEND) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5294 | return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, N0.getOperand(0)); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5295 | |
| Evan Cheng | 464dc9b | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5296 | // fold (aext (truncate (load x))) -> (aext (smaller load x)) |
| 5297 | // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n))) |
| 5298 | if (N0.getOpcode() == ISD::TRUNCATE) { |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5299 | SDValue NarrowLoad = ReduceLoadWidth(N0.getNode()); |
| 5300 | if (NarrowLoad.getNode()) { |
| Dale Johannesen | 60fe2cd | 2010-05-25 18:47:23 +0000 | [diff] [blame] | 5301 | SDNode* oye = N0.getNode()->getOperand(0).getNode(); |
| 5302 | if (NarrowLoad.getNode() != N0.getNode()) { |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5303 | CombineTo(N0.getNode(), NarrowLoad); |
| Dale Johannesen | 60fe2cd | 2010-05-25 18:47:23 +0000 | [diff] [blame] | 5304 | // CombineTo deleted the truncate, if needed, but not what's under it. |
| 5305 | AddToWorkList(oye); |
| 5306 | } |
| Eli Friedman | 55b0acd | 2011-04-16 23:25:34 +0000 | [diff] [blame] | 5307 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| Evan Cheng | a824e79 | 2007-03-23 02:16:52 +0000 | [diff] [blame] | 5308 | } |
| Evan Cheng | 464dc9b | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5309 | } |
| 5310 | |
| Chris Lattner | 8746e2c | 2006-09-20 06:29:17 +0000 | [diff] [blame] | 5311 | // fold (aext (truncate x)) |
| 5312 | if (N0.getOpcode() == ISD::TRUNCATE) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5313 | SDValue TruncOp = N0.getOperand(0); |
| Chris Lattner | 8746e2c | 2006-09-20 06:29:17 +0000 | [diff] [blame] | 5314 | if (TruncOp.getValueType() == VT) |
| Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 5315 | return TruncOp; // x iff x size == zext size. |
| Duncan Sands | 11dd424 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 5316 | if (TruncOp.getValueType().bitsGT(VT)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5317 | return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, TruncOp); |
| 5318 | return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, TruncOp); |
| Chris Lattner | 8746e2c | 2006-09-20 06:29:17 +0000 | [diff] [blame] | 5319 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5320 | |
| Dan Gohman | ad3e549 | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 5321 | // Fold (aext (and (trunc x), cst)) -> (and x, cst) |
| 5322 | // if the trunc is not free. |
| Chris Lattner | 082db3f | 2006-09-21 06:40:43 +0000 | [diff] [blame] | 5323 | if (N0.getOpcode() == ISD::AND && |
| 5324 | N0.getOperand(0).getOpcode() == ISD::TRUNCATE && |
| Dan Gohman | ad3e549 | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 5325 | N0.getOperand(1).getOpcode() == ISD::Constant && |
| 5326 | !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(), |
| 5327 | N0.getValueType())) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5328 | SDValue X = N0.getOperand(0).getOperand(0); |
| Duncan Sands | 11dd424 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 5329 | if (X.getValueType().bitsLT(VT)) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5330 | X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, X); |
| Duncan Sands | 11dd424 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 5331 | } else if (X.getValueType().bitsGT(VT)) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5332 | X = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, X); |
| Chris Lattner | 082db3f | 2006-09-21 06:40:43 +0000 | [diff] [blame] | 5333 | } |
| Dan Gohman | e1c4f99 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 5334 | APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); |
| Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 5335 | Mask = Mask.zext(VT.getSizeInBits()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5336 | return DAG.getNode(ISD::AND, SDLoc(N), VT, |
| Bill Wendling | 9b3dc8d | 2009-01-30 22:27:33 +0000 | [diff] [blame] | 5337 | X, DAG.getConstant(Mask, VT)); |
| Chris Lattner | 082db3f | 2006-09-21 06:40:43 +0000 | [diff] [blame] | 5338 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5339 | |
| Chris Lattner | 812646a | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 5340 | // fold (aext (load x)) -> (aext (truncate (extload x))) |
| Nadav Rotem | 502f1b9 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 5341 | // None of the supported targets knows how to perform load and any_ext |
| Nadav Rotem | b009130 | 2011-02-27 07:40:43 +0000 | [diff] [blame] | 5342 | // on vectors in one instruction. We only perform this transformation on |
| 5343 | // scalars. |
| Nadav Rotem | 502f1b9 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 5344 | if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() && |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5345 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
| Evan Cheng | 07d53b1 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 5346 | TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) { |
| Dan Gohman | 0e8d199 | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 5347 | bool DoXform = true; |
| 5348 | SmallVector<SDNode*, 4> SetCCs; |
| 5349 | if (!N0.hasOneUse()) |
| 5350 | DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI); |
| 5351 | if (DoXform) { |
| 5352 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5353 | SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT, |
| Dan Gohman | 0e8d199 | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 5354 | LN0->getChain(), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 5355 | LN0->getBasePtr(), N0.getValueType(), |
| 5356 | LN0->getMemOperand()); |
| Dan Gohman | 0e8d199 | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 5357 | CombineTo(N, ExtLoad); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5358 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), |
| Dan Gohman | 0e8d199 | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 5359 | N0.getValueType(), ExtLoad); |
| 5360 | CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1)); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5361 | ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N), |
| Nick Lewycky | 6d677cf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 5362 | ISD::ANY_EXTEND); |
| Dan Gohman | 0e8d199 | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 5363 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| 5364 | } |
| Chris Lattner | 812646a | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 5365 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5366 | |
| Chris Lattner | 812646a | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 5367 | // fold (aext (zextload x)) -> (aext (truncate (zextload x))) |
| 5368 | // fold (aext (sextload x)) -> (aext (truncate (sextload x))) |
| 5369 | // fold (aext ( extload x)) -> (aext (truncate (extload x))) |
| Evan Cheng | 8a1d09d | 2007-03-07 08:07:03 +0000 | [diff] [blame] | 5370 | if (N0.getOpcode() == ISD::LOAD && |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5371 | !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) && |
| Evan Cheng | e71fe34d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5372 | N0.hasOneUse()) { |
| 5373 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
| Dan Gohman | 08c0a95 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 5374 | EVT MemVT = LN0->getMemoryVT(); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5375 | SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), SDLoc(N), |
| Stuart Hastings | 81c4306 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 5376 | VT, LN0->getChain(), LN0->getBasePtr(), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 5377 | MemVT, LN0->getMemOperand()); |
| Chris Lattner | 812646a | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 5378 | CombineTo(N, ExtLoad); |
| Evan Cheng | 894be33 | 2008-08-29 23:20:46 +0000 | [diff] [blame] | 5379 | CombineTo(N0.getNode(), |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5380 | DAG.getNode(ISD::TRUNCATE, SDLoc(N0), |
| Bill Wendling | 9b3dc8d | 2009-01-30 22:27:33 +0000 | [diff] [blame] | 5381 | N0.getValueType(), ExtLoad), |
| Chris Lattner | 812646a | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 5382 | ExtLoad.getValue(1)); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5383 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| Chris Lattner | 812646a | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 5384 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5385 | |
| Chris Lattner | 65786b0 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 5386 | if (N0.getOpcode() == ISD::SETCC) { |
| Evan Cheng | abd0ad5 | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 5387 | // aext(setcc) -> sext_in_reg(vsetcc) for vectors. |
| 5388 | // Only do this before legalize for now. |
| 5389 | if (VT.isVector() && !LegalOperations) { |
| 5390 | EVT N0VT = N0.getOperand(0).getValueType(); |
| 5391 | // We know that the # elements of the results is the same as the |
| 5392 | // # elements of the compare (and the # elements of the compare result |
| 5393 | // for that matter). Check to see that they are the same size. If so, |
| 5394 | // we know that the element size of the sext'd result matches the |
| 5395 | // element size of the compare operands. |
| 5396 | if (VT.getSizeInBits() == N0VT.getSizeInBits()) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5397 | return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0), |
| Duncan Sands | 41b4a6b | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 5398 | N0.getOperand(1), |
| 5399 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
| Evan Cheng | abd0ad5 | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 5400 | // If the desired elements are smaller or larger than the source |
| 5401 | // elements we can use a matching integer vector type and then |
| 5402 | // truncate/sign extend |
| 5403 | else { |
| Duncan Sands | 41b4a6b | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 5404 | EVT MatchingElementType = |
| 5405 | EVT::getIntegerVT(*DAG.getContext(), |
| 5406 | N0VT.getScalarType().getSizeInBits()); |
| 5407 | EVT MatchingVectorType = |
| 5408 | EVT::getVectorVT(*DAG.getContext(), MatchingElementType, |
| 5409 | N0VT.getVectorNumElements()); |
| 5410 | SDValue VsetCC = |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5411 | DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0), |
| Duncan Sands | 41b4a6b | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 5412 | N0.getOperand(1), |
| 5413 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5414 | return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT); |
| Evan Cheng | abd0ad5 | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 5415 | } |
| 5416 | } |
| 5417 | |
| 5418 | // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5419 | SDValue SCC = |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5420 | SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1), |
| Chris Lattner | a083ffc | 2007-04-11 06:50:51 +0000 | [diff] [blame] | 5421 | DAG.getConstant(1, VT), DAG.getConstant(0, VT), |
| Chris Lattner | 18e4ac4 | 2007-04-11 16:51:53 +0000 | [diff] [blame] | 5422 | cast<CondCodeSDNode>(N0.getOperand(2))->get(), true); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5423 | if (SCC.getNode()) |
| Chris Lattner | c5f85d3 | 2007-04-11 06:43:25 +0000 | [diff] [blame] | 5424 | return SCC; |
| Chris Lattner | 65786b0 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 5425 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5426 | |
| Evan Cheng | f100557 | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 5427 | return SDValue(); |
| Chris Lattner | 812646a | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 5428 | } |
| 5429 | |
| Chris Lattner | 5e6fe05 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 5430 | /// GetDemandedBits - See if the specified operand can be simplified with the |
| 5431 | /// knowledge that only the bits specified by Mask are used. If so, return the |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5432 | /// simpler operand, otherwise return a null SDValue. |
| 5433 | SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) { |
| Chris Lattner | 5e6fe05 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 5434 | switch (V.getOpcode()) { |
| 5435 | default: break; |
| Lang Hames | b85fcd0 | 2011-11-08 18:56:23 +0000 | [diff] [blame] | 5436 | case ISD::Constant: { |
| 5437 | const ConstantSDNode *CV = cast<ConstantSDNode>(V.getNode()); |
| 5438 | assert(CV != 0 && "Const value should be ConstSDNode."); |
| 5439 | const APInt &CVal = CV->getAPIntValue(); |
| 5440 | APInt NewVal = CVal & Mask; |
| Stephen Lin | 8e8424e | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 5441 | if (NewVal != CVal) |
| Lang Hames | b85fcd0 | 2011-11-08 18:56:23 +0000 | [diff] [blame] | 5442 | return DAG.getConstant(NewVal, V.getValueType()); |
| Lang Hames | b85fcd0 | 2011-11-08 18:56:23 +0000 | [diff] [blame] | 5443 | break; |
| 5444 | } |
| Chris Lattner | 5e6fe05 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 5445 | case ISD::OR: |
| 5446 | case ISD::XOR: |
| 5447 | // If the LHS or RHS don't contribute bits to the or, drop them. |
| 5448 | if (DAG.MaskedValueIsZero(V.getOperand(0), Mask)) |
| 5449 | return V.getOperand(1); |
| 5450 | if (DAG.MaskedValueIsZero(V.getOperand(1), Mask)) |
| 5451 | return V.getOperand(0); |
| 5452 | break; |
| Chris Lattner | f47e306 | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 5453 | case ISD::SRL: |
| 5454 | // Only look at single-use SRLs. |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5455 | if (!V.getNode()->hasOneUse()) |
| Chris Lattner | f47e306 | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 5456 | break; |
| 5457 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) { |
| 5458 | // See if we can recursively simplify the LHS. |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5459 | unsigned Amt = RHSC->getZExtValue(); |
| Bill Wendling | 7bfa43b | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5460 | |
| Dan Gohman | b9fa1d2 | 2009-01-03 19:22:06 +0000 | [diff] [blame] | 5461 | // Watch out for shift count overflow though. |
| 5462 | if (Amt >= Mask.getBitWidth()) break; |
| Dan Gohman | 1f372ed | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 5463 | APInt NewMask = Mask << Amt; |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5464 | SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask); |
| Bill Wendling | 7bfa43b | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5465 | if (SimplifyLHS.getNode()) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5466 | return DAG.getNode(ISD::SRL, SDLoc(V), V.getValueType(), |
| Chris Lattner | f47e306 | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 5467 | SimplifyLHS, V.getOperand(1)); |
| Chris Lattner | f47e306 | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 5468 | } |
| Chris Lattner | 5e6fe05 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 5469 | } |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5470 | return SDValue(); |
| Chris Lattner | 5e6fe05 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 5471 | } |
| 5472 | |
| Evan Cheng | 464dc9b | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5473 | /// ReduceLoadWidth - If the result of a wider load is shifted to right of N |
| 5474 | /// bits and then truncated to a narrower type and where N is a multiple |
| 5475 | /// of number of bits of the narrower type, transform it to a narrower load |
| 5476 | /// from address + N / num of bits of new type. If the result is to be |
| 5477 | /// extended, also fold the extension to form a extending load. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5478 | SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) { |
| Evan Cheng | 464dc9b | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5479 | unsigned Opc = N->getOpcode(); |
| Dan Gohman | 600f62b | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 5480 | |
| Evan Cheng | 464dc9b | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5481 | ISD::LoadExtType ExtType = ISD::NON_EXTLOAD; |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5482 | SDValue N0 = N->getOperand(0); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5483 | EVT VT = N->getValueType(0); |
| 5484 | EVT ExtVT = VT; |
| Evan Cheng | 464dc9b | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5485 | |
| Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 5486 | // This transformation isn't valid for vector loads. |
| 5487 | if (VT.isVector()) |
| 5488 | return SDValue(); |
| 5489 | |
| Dan Gohman | 6bd3ef8 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 5490 | // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then |
| Evan Cheng | a883b58 | 2007-03-23 22:13:36 +0000 | [diff] [blame] | 5491 | // extended to VT. |
| Evan Cheng | 464dc9b | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5492 | if (Opc == ISD::SIGN_EXTEND_INREG) { |
| 5493 | ExtType = ISD::SEXTLOAD; |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5494 | ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT(); |
| Dan Gohman | 600f62b | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 5495 | } else if (Opc == ISD::SRL) { |
| Chris Lattner | 2a7ff99 | 2010-12-21 18:05:22 +0000 | [diff] [blame] | 5496 | // Another special-case: SRL is basically zero-extending a narrower value. |
| Dan Gohman | 600f62b | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 5497 | ExtType = ISD::ZEXTLOAD; |
| 5498 | N0 = SDValue(N, 0); |
| 5499 | ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 5500 | if (!N01) return SDValue(); |
| 5501 | ExtVT = EVT::getIntegerVT(*DAG.getContext(), |
| 5502 | VT.getSizeInBits() - N01->getZExtValue()); |
| Evan Cheng | 464dc9b | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5503 | } |
| Richard Osborne | 272e084 | 2011-01-31 17:41:44 +0000 | [diff] [blame] | 5504 | if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT)) |
| 5505 | return SDValue(); |
| Evan Cheng | 464dc9b | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5506 | |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5507 | unsigned EVTBits = ExtVT.getSizeInBits(); |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5508 | |
| Chris Lattner | 9a499e9 | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5509 | // Do not generate loads of non-round integer types since these can |
| 5510 | // be expensive (and would be wrong if the type is not byte sized). |
| 5511 | if (!ExtVT.isRound()) |
| 5512 | return SDValue(); |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5513 | |
| Evan Cheng | 464dc9b | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5514 | unsigned ShAmt = 0; |
| Chris Lattner | 9a499e9 | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5515 | if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) { |
| Evan Cheng | 464dc9b | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5516 | if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5517 | ShAmt = N01->getZExtValue(); |
| Evan Cheng | 464dc9b | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5518 | // Is the shift amount a multiple of size of VT? |
| 5519 | if ((ShAmt & (EVTBits-1)) == 0) { |
| 5520 | N0 = N0.getOperand(0); |
| Eli Friedman | 1e008c1 | 2009-08-19 08:46:10 +0000 | [diff] [blame] | 5521 | // Is the load width a multiple of size of VT? |
| 5522 | if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5523 | return SDValue(); |
| Evan Cheng | 464dc9b | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5524 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5525 | |
| Chris Lattner | cafc1e6 | 2010-12-22 08:02:57 +0000 | [diff] [blame] | 5526 | // At this point, we must have a load or else we can't do the transform. |
| 5527 | if (!isa<LoadSDNode>(N0)) return SDValue(); |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5528 | |
| Chandler Carruth | b27041c | 2012-12-11 00:36:57 +0000 | [diff] [blame] | 5529 | // Because a SRL must be assumed to *need* to zero-extend the high bits |
| 5530 | // (as opposed to anyext the high bits), we can't combine the zextload |
| 5531 | // lowering of SRL and an sextload. |
| 5532 | if (cast<LoadSDNode>(N0)->getExtensionType() == ISD::SEXTLOAD) |
| 5533 | return SDValue(); |
| 5534 | |
| Chris Lattner | a205055 | 2010-10-01 05:36:09 +0000 | [diff] [blame] | 5535 | // If the shift amount is larger than the input type then we're not |
| 5536 | // accessing any of the loaded bytes. If the load was a zextload/extload |
| 5537 | // then the result of the shift+trunc is zero/undef (handled elsewhere). |
| Chris Lattner | cafc1e6 | 2010-12-22 08:02:57 +0000 | [diff] [blame] | 5538 | if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits()) |
| Chris Lattner | a205055 | 2010-10-01 05:36:09 +0000 | [diff] [blame] | 5539 | return SDValue(); |
| Evan Cheng | 464dc9b | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5540 | } |
| 5541 | } |
| 5542 | |
| Dan Gohman | 68fb004 | 2010-11-03 01:47:46 +0000 | [diff] [blame] | 5543 | // If the load is shifted left (and the result isn't shifted back right), |
| 5544 | // we can fold the truncate through the shift. |
| 5545 | unsigned ShLeftAmt = 0; |
| 5546 | if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() && |
| Chris Lattner | 222374d | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5547 | ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) { |
| Dan Gohman | 68fb004 | 2010-11-03 01:47:46 +0000 | [diff] [blame] | 5548 | if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
| 5549 | ShLeftAmt = N01->getZExtValue(); |
| 5550 | N0 = N0.getOperand(0); |
| 5551 | } |
| 5552 | } |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5553 | |
| Chris Lattner | 222374d | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5554 | // If we haven't found a load, we can't narrow it. Don't transform one with |
| 5555 | // multiple uses, this would require adding a new load. |
| Bill Schmidt | d006c69 | 2013-01-14 22:04:38 +0000 | [diff] [blame] | 5556 | if (!isa<LoadSDNode>(N0) || !N0.hasOneUse()) |
| 5557 | return SDValue(); |
| 5558 | |
| 5559 | // Don't change the width of a volatile load. |
| 5560 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
| 5561 | if (LN0->isVolatile()) |
| Chris Lattner | 222374d | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5562 | return SDValue(); |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5563 | |
| Chris Lattner | 9a499e9 | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5564 | // Verify that we are actually reducing a load width here. |
| Bill Schmidt | d006c69 | 2013-01-14 22:04:38 +0000 | [diff] [blame] | 5565 | if (LN0->getMemoryVT().getSizeInBits() < EVTBits) |
| Chris Lattner | 222374d | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5566 | return SDValue(); |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5567 | |
| Bill Schmidt | d006c69 | 2013-01-14 22:04:38 +0000 | [diff] [blame] | 5568 | // For the transform to be legal, the load must produce only two values |
| 5569 | // (the value loaded and the chain). Don't transform a pre-increment |
| Stephen Lin | cfe7f35 | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 5570 | // load, for example, which produces an extra value. Otherwise the |
| Bill Schmidt | d006c69 | 2013-01-14 22:04:38 +0000 | [diff] [blame] | 5571 | // transformation is not equivalent, and the downstream logic to replace |
| 5572 | // uses gets things wrong. |
| 5573 | if (LN0->getNumValues() > 2) |
| 5574 | return SDValue(); |
| 5575 | |
| Benjamin Kramer | c7332b2 | 2013-07-06 14:05:09 +0000 | [diff] [blame] | 5576 | // If the load that we're shrinking is an extload and we're not just |
| 5577 | // discarding the extension we can't simply shrink the load. Bail. |
| 5578 | // TODO: It would be possible to merge the extensions in some cases. |
| 5579 | if (LN0->getExtensionType() != ISD::NON_EXTLOAD && |
| 5580 | LN0->getMemoryVT().getSizeInBits() < ExtVT.getSizeInBits() + ShAmt) |
| 5581 | return SDValue(); |
| 5582 | |
| Chris Lattner | 222374d | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5583 | EVT PtrType = N0.getOperand(1).getValueType(); |
| Bill Wendling | 7bfa43b | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5584 | |
| Evan Cheng | 4c6f917 | 2012-06-26 01:19:33 +0000 | [diff] [blame] | 5585 | if (PtrType == MVT::Untyped || PtrType.isExtended()) |
| 5586 | // It's not possible to generate a constant of extended or untyped type. |
| 5587 | return SDValue(); |
| 5588 | |
| Chris Lattner | 222374d | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5589 | // For big endian targets, we need to adjust the offset to the pointer to |
| 5590 | // load the correct bytes. |
| 5591 | if (TLI.isBigEndian()) { |
| 5592 | unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits(); |
| 5593 | unsigned EVTStoreBits = ExtVT.getStoreSizeInBits(); |
| 5594 | ShAmt = LVTStoreBits - EVTStoreBits - ShAmt; |
| Evan Cheng | 464dc9b | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5595 | } |
| 5596 | |
| Chris Lattner | 222374d | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5597 | uint64_t PtrOff = ShAmt / 8; |
| 5598 | unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5599 | SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0), |
| Chris Lattner | 222374d | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5600 | PtrType, LN0->getBasePtr(), |
| 5601 | DAG.getConstant(PtrOff, PtrType)); |
| 5602 | AddToWorkList(NewPtr.getNode()); |
| 5603 | |
| Chris Lattner | 9a499e9 | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5604 | SDValue Load; |
| 5605 | if (ExtType == ISD::NON_EXTLOAD) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5606 | Load = DAG.getLoad(VT, SDLoc(N0), LN0->getChain(), NewPtr, |
| Chris Lattner | 9a499e9 | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5607 | LN0->getPointerInfo().getWithOffset(PtrOff), |
| Pete Cooper | 82cd9e8 | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 5608 | LN0->isVolatile(), LN0->isNonTemporal(), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 5609 | LN0->isInvariant(), NewAlign, LN0->getTBAAInfo()); |
| Chris Lattner | 9a499e9 | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5610 | else |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5611 | Load = DAG.getExtLoad(ExtType, SDLoc(N0), VT, LN0->getChain(),NewPtr, |
| Chris Lattner | 9a499e9 | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5612 | LN0->getPointerInfo().getWithOffset(PtrOff), |
| 5613 | ExtVT, LN0->isVolatile(), LN0->isNonTemporal(), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 5614 | NewAlign, LN0->getTBAAInfo()); |
| Chris Lattner | 222374d | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5615 | |
| 5616 | // Replace the old load's chain with the new load's chain. |
| 5617 | WorkListRemover DeadNodes(*this); |
| Jakob Stoklund Olesen | beb9469 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 5618 | DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1)); |
| Chris Lattner | 222374d | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5619 | |
| 5620 | // Shift the result left, if we've swallowed a left shift. |
| 5621 | SDValue Result = Load; |
| 5622 | if (ShLeftAmt != 0) { |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5623 | EVT ShImmTy = getShiftAmountTy(Result.getValueType()); |
| Chris Lattner | 222374d | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5624 | if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt)) |
| 5625 | ShImmTy = VT; |
| Paul Redmond | 288604e | 2013-02-12 15:21:21 +0000 | [diff] [blame] | 5626 | // If the shift amount is as large as the result size (but, presumably, |
| 5627 | // no larger than the source) then the useful bits of the result are |
| 5628 | // zero; we can't simply return the shortened shift, because the result |
| 5629 | // of that operation is undefined. |
| 5630 | if (ShLeftAmt >= VT.getSizeInBits()) |
| 5631 | Result = DAG.getConstant(0, VT); |
| 5632 | else |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5633 | Result = DAG.getNode(ISD::SHL, SDLoc(N0), VT, |
| Paul Redmond | 288604e | 2013-02-12 15:21:21 +0000 | [diff] [blame] | 5634 | Result, DAG.getConstant(ShLeftAmt, ShImmTy)); |
| Chris Lattner | 222374d | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5635 | } |
| 5636 | |
| 5637 | // Return the new loaded value. |
| 5638 | return Result; |
| Evan Cheng | 464dc9b | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5639 | } |
| 5640 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5641 | SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) { |
| 5642 | SDValue N0 = N->getOperand(0); |
| 5643 | SDValue N1 = N->getOperand(1); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5644 | EVT VT = N->getValueType(0); |
| 5645 | EVT EVT = cast<VTSDNode>(N1)->getVT(); |
| Dan Gohman | 1d459e4 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 5646 | unsigned VTBits = VT.getScalarType().getSizeInBits(); |
| Dan Gohman | 6bd3ef8 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 5647 | unsigned EVTBits = EVT.getScalarType().getSizeInBits(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5648 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5649 | // fold (sext_in_reg c1) -> c1 |
| Chris Lattner | 29062da | 2006-05-08 20:59:41 +0000 | [diff] [blame] | 5650 | if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5651 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, N0, N1); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5652 | |
| Chris Lattner | 2a4d7b8 | 2006-05-06 22:43:44 +0000 | [diff] [blame] | 5653 | // If the input is already sign extended, just drop the extension. |
| Dan Gohman | 1d459e4 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 5654 | if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1) |
| Chris Lattner | 1ecb2a2 | 2006-05-06 09:30:03 +0000 | [diff] [blame] | 5655 | return N0; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5656 | |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 5657 | // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2 |
| 5658 | if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG && |
| Stephen Lin | 8e8424e | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 5659 | EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5660 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, |
| Bill Wendling | 7bfa43b | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5661 | N0.getOperand(0), N1); |
| Chris Lattner | 446e1ef | 2006-05-08 21:18:59 +0000 | [diff] [blame] | 5662 | |
| Dan Gohman | 345d63c | 2008-07-31 00:50:31 +0000 | [diff] [blame] | 5663 | // fold (sext_in_reg (sext x)) -> (sext x) |
| 5664 | // fold (sext_in_reg (aext x)) -> (sext x) |
| 5665 | // if x is small enough. |
| 5666 | if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) { |
| 5667 | SDValue N00 = N0.getOperand(0); |
| Evan Cheng | f037f87 | 2010-04-16 22:26:19 +0000 | [diff] [blame] | 5668 | if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits && |
| 5669 | (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT))) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5670 | return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N00, N1); |
| Dan Gohman | 345d63c | 2008-07-31 00:50:31 +0000 | [diff] [blame] | 5671 | } |
| 5672 | |
| Chris Lattner | 9ad5915 | 2007-04-17 19:03:21 +0000 | [diff] [blame] | 5673 | // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero. |
| Dan Gohman | 1f372ed | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 5674 | if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits))) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5675 | return DAG.getZeroExtendInReg(N0, SDLoc(N), EVT); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5676 | |
| Chris Lattner | 9ad5915 | 2007-04-17 19:03:21 +0000 | [diff] [blame] | 5677 | // fold operands of sext_in_reg based on knowledge that the top bits are not |
| 5678 | // demanded. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5679 | if (SimplifyDemandedBits(SDValue(N, 0))) |
| 5680 | return SDValue(N, 0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5681 | |
| Evan Cheng | 464dc9b | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5682 | // fold (sext_in_reg (load x)) -> (smaller sextload x) |
| 5683 | // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits)) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5684 | SDValue NarrowLoad = ReduceLoadWidth(N); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5685 | if (NarrowLoad.getNode()) |
| Evan Cheng | 464dc9b | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5686 | return NarrowLoad; |
| 5687 | |
| Bill Wendling | 7bfa43b | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5688 | // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24) |
| Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 5689 | // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible. |
| Chris Lattner | 446e1ef | 2006-05-08 21:18:59 +0000 | [diff] [blame] | 5690 | // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above. |
| 5691 | if (N0.getOpcode() == ISD::SRL) { |
| 5692 | if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1))) |
| Dan Gohman | 1d459e4 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 5693 | if (ShAmt->getZExtValue()+EVTBits <= VTBits) { |
| Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 5694 | // We can turn this into an SRA iff the input to the SRL is already sign |
| Chris Lattner | 446e1ef | 2006-05-08 21:18:59 +0000 | [diff] [blame] | 5695 | // extended enough. |
| Dan Gohman | 309d3d5 | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 5696 | unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0)); |
| Dan Gohman | 1d459e4 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 5697 | if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5698 | return DAG.getNode(ISD::SRA, SDLoc(N), VT, |
| Bill Wendling | 7bfa43b | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5699 | N0.getOperand(0), N0.getOperand(1)); |
| Chris Lattner | 446e1ef | 2006-05-08 21:18:59 +0000 | [diff] [blame] | 5700 | } |
| 5701 | } |
| Evan Cheng | 464dc9b | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5702 | |
| Nate Begeman | 02b23c6 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 5703 | // fold (sext_inreg (extload x)) -> (sextload x) |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5704 | if (ISD::isEXTLoad(N0.getNode()) && |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5705 | ISD::isUNINDEXEDLoad(N0.getNode()) && |
| Dan Gohman | 47a7d6f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 5706 | EVT == cast<LoadSDNode>(N0)->getMemoryVT() && |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5707 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
| Evan Cheng | 07d53b1 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 5708 | TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) { |
| Evan Cheng | e71fe34d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5709 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5710 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT, |
| Bill Wendling | 7bfa43b | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5711 | LN0->getChain(), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 5712 | LN0->getBasePtr(), EVT, |
| 5713 | LN0->getMemOperand()); |
| Chris Lattner | d39c60f | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 5714 | CombineTo(N, ExtLoad); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5715 | CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); |
| Elena Demikhovsky | 14a4af0 | 2012-12-19 07:50:20 +0000 | [diff] [blame] | 5716 | AddToWorkList(ExtLoad.getNode()); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5717 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| Nate Begeman | 02b23c6 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 5718 | } |
| Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 5719 | // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5720 | if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) && |
| Evan Cheng | 8a1d09d | 2007-03-07 08:07:03 +0000 | [diff] [blame] | 5721 | N0.hasOneUse() && |
| Dan Gohman | 47a7d6f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 5722 | EVT == cast<LoadSDNode>(N0)->getMemoryVT() && |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5723 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
| Evan Cheng | 07d53b1 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 5724 | TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) { |
| Evan Cheng | e71fe34d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5725 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5726 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT, |
| Bill Wendling | 7bfa43b | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5727 | LN0->getChain(), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 5728 | LN0->getBasePtr(), EVT, |
| 5729 | LN0->getMemOperand()); |
| Chris Lattner | d39c60f | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 5730 | CombineTo(N, ExtLoad); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5731 | CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5732 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| Nate Begeman | 02b23c6 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 5733 | } |
| Evan Cheng | 4c0bd96 | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 5734 | |
| 5735 | // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16)) |
| 5736 | if (EVTBits <= 16 && N0.getOpcode() == ISD::OR) { |
| 5737 | SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0), |
| 5738 | N0.getOperand(1), false); |
| 5739 | if (BSwap.getNode() != 0) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5740 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, |
| Evan Cheng | 4c0bd96 | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 5741 | BSwap, N1); |
| 5742 | } |
| 5743 | |
| Andrea Di Biagio | 46dcddb | 2013-12-27 20:20:28 +0000 | [diff] [blame] | 5744 | // Fold a sext_inreg of a build_vector of ConstantSDNodes or undefs |
| 5745 | // into a build_vector. |
| 5746 | if (ISD::isBuildVectorOfConstantSDNodes(N0.getNode())) { |
| 5747 | SmallVector<SDValue, 8> Elts; |
| 5748 | unsigned NumElts = N0->getNumOperands(); |
| 5749 | unsigned ShAmt = VTBits - EVTBits; |
| 5750 | |
| 5751 | for (unsigned i = 0; i != NumElts; ++i) { |
| 5752 | SDValue Op = N0->getOperand(i); |
| 5753 | if (Op->getOpcode() == ISD::UNDEF) { |
| 5754 | Elts.push_back(Op); |
| 5755 | continue; |
| 5756 | } |
| 5757 | |
| 5758 | ConstantSDNode *CurrentND = cast<ConstantSDNode>(Op); |
| Kevin Qin | 5cd73c9 | 2014-01-06 02:26:10 +0000 | [diff] [blame] | 5759 | const APInt &C = APInt(VTBits, CurrentND->getAPIntValue().getZExtValue()); |
| 5760 | Elts.push_back(DAG.getConstant(C.shl(ShAmt).ashr(ShAmt).getZExtValue(), |
| Andrea Di Biagio | 46dcddb | 2013-12-27 20:20:28 +0000 | [diff] [blame] | 5761 | Op.getValueType())); |
| 5762 | } |
| 5763 | |
| 5764 | return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, &Elts[0], NumElts); |
| 5765 | } |
| 5766 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5767 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5768 | } |
| 5769 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5770 | SDValue DAGCombiner::visitTRUNCATE(SDNode *N) { |
| 5771 | SDValue N0 = N->getOperand(0); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5772 | EVT VT = N->getValueType(0); |
| Nadav Rotem | 5399f4d | 2012-02-03 13:18:25 +0000 | [diff] [blame] | 5773 | bool isLE = TLI.isLittleEndian(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5774 | |
| 5775 | // noop truncate |
| 5776 | if (N0.getValueType() == N->getValueType(0)) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 5777 | return N0; |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5778 | // fold (truncate c1) -> c1 |
| Chris Lattner | 7e7bcf3 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 5779 | if (isa<ConstantSDNode>(N0)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5780 | return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5781 | // fold (truncate (truncate x)) -> (truncate x) |
| 5782 | if (N0.getOpcode() == ISD::TRUNCATE) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5783 | return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0)); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5784 | // fold (truncate (ext x)) -> (ext x) or (truncate x) or x |
| Chris Lattner | 6855d62 | 2010-04-07 18:13:33 +0000 | [diff] [blame] | 5785 | if (N0.getOpcode() == ISD::ZERO_EXTEND || |
| 5786 | N0.getOpcode() == ISD::SIGN_EXTEND || |
| Chris Lattner | 907e392 | 2006-05-05 22:56:26 +0000 | [diff] [blame] | 5787 | N0.getOpcode() == ISD::ANY_EXTEND) { |
| Duncan Sands | 11dd424 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 5788 | if (N0.getOperand(0).getValueType().bitsLT(VT)) |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5789 | // if the source is smaller than the dest, we still need an extend |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5790 | return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, |
| Bill Wendling | 4e0a615 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5791 | N0.getOperand(0)); |
| Craig Topper | 5f9791f | 2012-09-29 07:18:53 +0000 | [diff] [blame] | 5792 | if (N0.getOperand(0).getValueType().bitsGT(VT)) |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5793 | // if the source is larger than the dest, than we just need the truncate |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5794 | return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0)); |
| Craig Topper | 5f9791f | 2012-09-29 07:18:53 +0000 | [diff] [blame] | 5795 | // if the source and dest are the same type, we can drop both the extend |
| 5796 | // and the truncate. |
| 5797 | return N0.getOperand(0); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5798 | } |
| Evan Cheng | d63baea | 2007-03-21 20:14:05 +0000 | [diff] [blame] | 5799 | |
| Nadav Rotem | 4f4546b | 2012-02-05 11:39:23 +0000 | [diff] [blame] | 5800 | // Fold extract-and-trunc into a narrow extract. For example: |
| 5801 | // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1) |
| 5802 | // i32 y = TRUNCATE(i64 x) |
| 5803 | // -- becomes -- |
| 5804 | // v16i8 b = BITCAST (v2i64 val) |
| 5805 | // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8) |
| 5806 | // |
| 5807 | // Note: We only run this optimization after type legalization (which often |
| Nadav Rotem | 5399f4d | 2012-02-03 13:18:25 +0000 | [diff] [blame] | 5808 | // creates this pattern) and before operation legalization after which |
| 5809 | // we need to be more careful about the vector instructions that we generate. |
| 5810 | if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT && |
| 5811 | LegalTypes && !LegalOperations && N0->hasOneUse()) { |
| 5812 | |
| 5813 | EVT VecTy = N0.getOperand(0).getValueType(); |
| 5814 | EVT ExTy = N0.getValueType(); |
| 5815 | EVT TrTy = N->getValueType(0); |
| 5816 | |
| 5817 | unsigned NumElem = VecTy.getVectorNumElements(); |
| 5818 | unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits(); |
| 5819 | |
| 5820 | EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, SizeRatio * NumElem); |
| 5821 | assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size"); |
| 5822 | |
| 5823 | SDValue EltNo = N0->getOperand(1); |
| 5824 | if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) { |
| 5825 | int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); |
| Tom Stellard | d42c594 | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 5826 | EVT IndexTy = TLI.getVectorIdxTy(); |
| Nadav Rotem | 5399f4d | 2012-02-03 13:18:25 +0000 | [diff] [blame] | 5827 | int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1)); |
| 5828 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5829 | SDValue V = DAG.getNode(ISD::BITCAST, SDLoc(N), |
| Nadav Rotem | 5399f4d | 2012-02-03 13:18:25 +0000 | [diff] [blame] | 5830 | NVT, N0.getOperand(0)); |
| 5831 | |
| 5832 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5833 | SDLoc(N), TrTy, V, |
| Jim Grosbach | 92f6adc | 2012-05-08 20:56:07 +0000 | [diff] [blame] | 5834 | DAG.getConstant(Index, IndexTy)); |
| Nadav Rotem | 5399f4d | 2012-02-03 13:18:25 +0000 | [diff] [blame] | 5835 | } |
| 5836 | } |
| 5837 | |
| Arnold Schwaighofer | 3f9568e | 2013-02-20 21:33:32 +0000 | [diff] [blame] | 5838 | // Fold a series of buildvector, bitcast, and truncate if possible. |
| 5839 | // For example fold |
| 5840 | // (2xi32 trunc (bitcast ((4xi32)buildvector x, x, y, y) 2xi64)) to |
| 5841 | // (2xi32 (buildvector x, y)). |
| 5842 | if (Level == AfterLegalizeVectorOps && VT.isVector() && |
| 5843 | N0.getOpcode() == ISD::BITCAST && N0.hasOneUse() && |
| 5844 | N0.getOperand(0).getOpcode() == ISD::BUILD_VECTOR && |
| 5845 | N0.getOperand(0).hasOneUse()) { |
| 5846 | |
| 5847 | SDValue BuildVect = N0.getOperand(0); |
| 5848 | EVT BuildVectEltTy = BuildVect.getValueType().getVectorElementType(); |
| 5849 | EVT TruncVecEltTy = VT.getVectorElementType(); |
| 5850 | |
| 5851 | // Check that the element types match. |
| 5852 | if (BuildVectEltTy == TruncVecEltTy) { |
| 5853 | // Now we only need to compute the offset of the truncated elements. |
| 5854 | unsigned BuildVecNumElts = BuildVect.getNumOperands(); |
| 5855 | unsigned TruncVecNumElts = VT.getVectorNumElements(); |
| 5856 | unsigned TruncEltOffset = BuildVecNumElts / TruncVecNumElts; |
| 5857 | |
| 5858 | assert((BuildVecNumElts % TruncVecNumElts) == 0 && |
| 5859 | "Invalid number of elements"); |
| 5860 | |
| 5861 | SmallVector<SDValue, 8> Opnds; |
| 5862 | for (unsigned i = 0, e = BuildVecNumElts; i != e; i += TruncEltOffset) |
| 5863 | Opnds.push_back(BuildVect.getOperand(i)); |
| 5864 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5865 | return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, &Opnds[0], |
| Arnold Schwaighofer | 3f9568e | 2013-02-20 21:33:32 +0000 | [diff] [blame] | 5866 | Opnds.size()); |
| 5867 | } |
| 5868 | } |
| 5869 | |
| Chris Lattner | 5e6fe05 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 5870 | // See if we can simplify the input to this truncate through knowledge that |
| Nadav Rotem | 502f1b9 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 5871 | // only the low bits are being used. |
| 5872 | // For example "trunc (or (shl x, 8), y)" // -> trunc y |
| Nadav Rotem | b009130 | 2011-02-27 07:40:43 +0000 | [diff] [blame] | 5873 | // Currently we only perform this optimization on scalars because vectors |
| Nadav Rotem | 502f1b9 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 5874 | // may have different active low bits. |
| 5875 | if (!VT.isVector()) { |
| 5876 | SDValue Shorter = |
| 5877 | GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(), |
| 5878 | VT.getSizeInBits())); |
| 5879 | if (Shorter.getNode()) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5880 | return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Shorter); |
| Nadav Rotem | 502f1b9 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 5881 | } |
| Nate Begeman | 8caf81d | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 5882 | // fold (truncate (load x)) -> (smaller load x) |
| Evan Cheng | d63baea | 2007-03-21 20:14:05 +0000 | [diff] [blame] | 5883 | // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits)) |
| Dan Gohman | 600f62b | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 5884 | if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) { |
| 5885 | SDValue Reduced = ReduceLoadWidth(N); |
| 5886 | if (Reduced.getNode()) |
| 5887 | return Reduced; |
| Richard Sandiford | d109363 | 2013-12-11 11:37:27 +0000 | [diff] [blame] | 5888 | // Handle the case where the load remains an extending load even |
| 5889 | // after truncation. |
| 5890 | if (N0.hasOneUse() && ISD::isUNINDEXEDLoad(N0.getNode())) { |
| 5891 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
| 5892 | if (!LN0->isVolatile() && |
| 5893 | LN0->getMemoryVT().getStoreSizeInBits() < VT.getSizeInBits()) { |
| 5894 | SDValue NewLoad = DAG.getExtLoad(LN0->getExtensionType(), SDLoc(LN0), |
| 5895 | VT, LN0->getChain(), LN0->getBasePtr(), |
| 5896 | LN0->getMemoryVT(), |
| 5897 | LN0->getMemOperand()); |
| 5898 | DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLoad.getValue(1)); |
| 5899 | return NewLoad; |
| 5900 | } |
| 5901 | } |
| Dan Gohman | 600f62b | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 5902 | } |
| Michael Liao | 3ac8201 | 2012-10-17 23:45:54 +0000 | [diff] [blame] | 5903 | // fold (trunc (concat ... x ...)) -> (concat ..., (trunc x), ...)), |
| 5904 | // where ... are all 'undef'. |
| 5905 | if (N0.getOpcode() == ISD::CONCAT_VECTORS && !LegalTypes) { |
| 5906 | SmallVector<EVT, 8> VTs; |
| 5907 | SDValue V; |
| 5908 | unsigned Idx = 0; |
| 5909 | unsigned NumDefs = 0; |
| 5910 | |
| 5911 | for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) { |
| 5912 | SDValue X = N0.getOperand(i); |
| 5913 | if (X.getOpcode() != ISD::UNDEF) { |
| 5914 | V = X; |
| 5915 | Idx = i; |
| 5916 | NumDefs++; |
| 5917 | } |
| 5918 | // Stop if more than one members are non-undef. |
| 5919 | if (NumDefs > 1) |
| 5920 | break; |
| 5921 | VTs.push_back(EVT::getVectorVT(*DAG.getContext(), |
| 5922 | VT.getVectorElementType(), |
| 5923 | X.getValueType().getVectorNumElements())); |
| 5924 | } |
| 5925 | |
| 5926 | if (NumDefs == 0) |
| 5927 | return DAG.getUNDEF(VT); |
| 5928 | |
| 5929 | if (NumDefs == 1) { |
| 5930 | assert(V.getNode() && "The single defined operand is empty!"); |
| 5931 | SmallVector<SDValue, 8> Opnds; |
| 5932 | for (unsigned i = 0, e = VTs.size(); i != e; ++i) { |
| 5933 | if (i != Idx) { |
| 5934 | Opnds.push_back(DAG.getUNDEF(VTs[i])); |
| 5935 | continue; |
| 5936 | } |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5937 | SDValue NV = DAG.getNode(ISD::TRUNCATE, SDLoc(V), VTs[i], V); |
| Michael Liao | 3ac8201 | 2012-10-17 23:45:54 +0000 | [diff] [blame] | 5938 | AddToWorkList(NV.getNode()); |
| 5939 | Opnds.push_back(NV); |
| 5940 | } |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5941 | return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, |
| Michael Liao | 3ac8201 | 2012-10-17 23:45:54 +0000 | [diff] [blame] | 5942 | &Opnds[0], Opnds.size()); |
| 5943 | } |
| 5944 | } |
| Dan Gohman | 600f62b | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 5945 | |
| 5946 | // Simplify the operands using demanded-bits information. |
| 5947 | if (!VT.isVector() && |
| 5948 | SimplifyDemandedBits(SDValue(N, 0))) |
| 5949 | return SDValue(N, 0); |
| 5950 | |
| Evan Cheng | f1bd5fc | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 5951 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5952 | } |
| 5953 | |
| Evan Cheng | b980f6f | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5954 | static SDNode *getBuildPairElt(SDNode *N, unsigned i) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5955 | SDValue Elt = N->getOperand(i); |
| Evan Cheng | b980f6f | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5956 | if (Elt.getOpcode() != ISD::MERGE_VALUES) |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5957 | return Elt.getNode(); |
| 5958 | return Elt.getOperand(Elt.getResNo()).getNode(); |
| Evan Cheng | b980f6f | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5959 | } |
| 5960 | |
| 5961 | /// CombineConsecutiveLoads - build_pair (load, load) -> load |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5962 | /// if load locations are consecutive. |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5963 | SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) { |
| Evan Cheng | b980f6f | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5964 | assert(N->getOpcode() == ISD::BUILD_PAIR); |
| 5965 | |
| Nate Begeman | 624690c | 2009-06-05 21:37:30 +0000 | [diff] [blame] | 5966 | LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0)); |
| 5967 | LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1)); |
| Chris Lattner | f72c3c0 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 5968 | if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() || |
| 5969 | LD1->getPointerInfo().getAddrSpace() != |
| 5970 | LD2->getPointerInfo().getAddrSpace()) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5971 | return SDValue(); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5972 | EVT LD1VT = LD1->getValueType(0); |
| Bill Wendling | 4e0a615 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5973 | |
| Evan Cheng | b980f6f | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5974 | if (ISD::isNON_EXTLoad(LD2) && |
| 5975 | LD2->hasOneUse() && |
| Duncan Sands | 8651e9c | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 5976 | // If both are volatile this would reduce the number of volatile loads. |
| 5977 | // If one is volatile it might be ok, but play conservative and bail out. |
| Nate Begeman | 624690c | 2009-06-05 21:37:30 +0000 | [diff] [blame] | 5978 | !LD1->isVolatile() && |
| 5979 | !LD2->isVolatile() && |
| Evan Cheng | f5938d5 | 2009-12-09 01:36:00 +0000 | [diff] [blame] | 5980 | DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) { |
| Nate Begeman | 624690c | 2009-06-05 21:37:30 +0000 | [diff] [blame] | 5981 | unsigned Align = LD1->getAlignment(); |
| Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 5982 | unsigned NewAlign = TLI.getDataLayout()-> |
| Owen Anderson | 117c9e8 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5983 | getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext())); |
| Bill Wendling | 4e0a615 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5984 | |
| Duncan Sands | 8651e9c | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 5985 | if (NewAlign <= Align && |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5986 | (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5987 | return DAG.getLoad(VT, SDLoc(N), LD1->getChain(), |
| Chris Lattner | f72c3c0 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 5988 | LD1->getBasePtr(), LD1->getPointerInfo(), |
| Pete Cooper | 82cd9e8 | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 5989 | false, false, false, Align); |
| Evan Cheng | b980f6f | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5990 | } |
| Bill Wendling | 4e0a615 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5991 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5992 | return SDValue(); |
| Evan Cheng | b980f6f | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5993 | } |
| 5994 | |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5995 | SDValue DAGCombiner::visitBITCAST(SDNode *N) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5996 | SDValue N0 = N->getOperand(0); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5997 | EVT VT = N->getValueType(0); |
| Chris Lattner | a187460 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 5998 | |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5999 | // If the input is a BUILD_VECTOR with all constant elements, fold this now. |
| 6000 | // Only do this before legalize, since afterward the target may be depending |
| 6001 | // on the bitconvert. |
| 6002 | // First check to see if this is all constant. |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6003 | if (!LegalTypes && |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6004 | N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() && |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6005 | VT.isVector()) { |
| Juergen Ributzka | 7384405 | 2014-01-13 20:51:35 +0000 | [diff] [blame] | 6006 | bool isSimple = cast<BuildVectorSDNode>(N0)->isConstant(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6007 | |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6008 | EVT DestEltVT = N->getValueType(0).getVectorElementType(); |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6009 | assert(!DestEltVT.isVector() && |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6010 | "Element type of vector ValueType must not be vector!"); |
| Bill Wendling | 4e0a615 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 6011 | if (isSimple) |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6012 | return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT); |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6013 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6014 | |
| Dan Gohman | 921ddd6 | 2008-09-05 01:58:21 +0000 | [diff] [blame] | 6015 | // If the input is a constant, let getNode fold it. |
| Chris Lattner | a187460 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 6016 | if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6017 | SDValue Res = DAG.getNode(ISD::BITCAST, SDLoc(N), VT, N0); |
| Dan Gohman | 733a64d | 2009-08-10 23:15:10 +0000 | [diff] [blame] | 6018 | if (Res.getNode() != N) { |
| 6019 | if (!LegalOperations || |
| 6020 | TLI.isOperationLegal(Res.getNode()->getOpcode(), VT)) |
| 6021 | return Res; |
| 6022 | |
| 6023 | // Folding it resulted in an illegal node, and it's too late to |
| 6024 | // do that. Clean up the old node and forego the transformation. |
| 6025 | // Ideally this won't happen very often, because instcombine |
| 6026 | // and the earlier dagcombine runs (where illegal nodes are |
| 6027 | // permitted) should have folded most of them already. |
| 6028 | DAG.DeleteNode(Res.getNode()); |
| 6029 | } |
| Chris Lattner | a187460 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 6030 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6031 | |
| Bill Wendling | 4e0a615 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 6032 | // (conv (conv x, t1), t2) -> (conv x, t2) |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6033 | if (N0.getOpcode() == ISD::BITCAST) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6034 | return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, |
| Bill Wendling | 4e0a615 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 6035 | N0.getOperand(0)); |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6036 | |
| Chris Lattner | 54560f6 | 2005-12-23 05:44:41 +0000 | [diff] [blame] | 6037 | // fold (conv (load x)) -> (load (conv*)x) |
| Evan Cheng | 0de312d | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 6038 | // If the resultant load doesn't need a higher alignment than the original! |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6039 | if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() && |
| Duncan Sands | 8651e9c | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 6040 | // Do not change the width of a volatile load. |
| 6041 | !cast<LoadSDNode>(N0)->isVolatile() && |
| Matt Arsenault | c5559bb | 2013-11-15 04:42:23 +0000 | [diff] [blame] | 6042 | (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)) && |
| 6043 | TLI.isLoadBitCastBeneficial(N0.getValueType(), VT)) { |
| Evan Cheng | e71fe34d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 6044 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
| Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 6045 | unsigned Align = TLI.getDataLayout()-> |
| Owen Anderson | 117c9e8 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6046 | getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext())); |
| Evan Cheng | a4cf58a | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 6047 | unsigned OrigAlign = LN0->getAlignment(); |
| Bill Wendling | 4e0a615 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 6048 | |
| Evan Cheng | a4cf58a | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 6049 | if (Align <= OrigAlign) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6050 | SDValue Load = DAG.getLoad(VT, SDLoc(N), LN0->getChain(), |
| Chris Lattner | f72c3c0 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 6051 | LN0->getBasePtr(), LN0->getPointerInfo(), |
| David Greene | 39c6d01 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6052 | LN0->isVolatile(), LN0->isNonTemporal(), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 6053 | LN0->isInvariant(), OrigAlign, |
| 6054 | LN0->getTBAAInfo()); |
| Evan Cheng | a4cf58a | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 6055 | AddToWorkList(N); |
| Gabor Greif | e12264b | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 6056 | CombineTo(N0.getNode(), |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6057 | DAG.getNode(ISD::BITCAST, SDLoc(N0), |
| Bill Wendling | 4e0a615 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 6058 | N0.getValueType(), Load), |
| Evan Cheng | a4cf58a | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 6059 | Load.getValue(1)); |
| 6060 | return Load; |
| 6061 | } |
| Chris Lattner | 54560f6 | 2005-12-23 05:44:41 +0000 | [diff] [blame] | 6062 | } |
| Duncan Sands | 8651e9c | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 6063 | |
| Bill Wendling | 4e0a615 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 6064 | // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit) |
| 6065 | // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit)) |
| Chris Lattner | 888560d | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 6066 | // This often reduces constant pool loads. |
| Tom Stellard | c54731a | 2013-07-23 23:55:03 +0000 | [diff] [blame] | 6067 | if (((N0.getOpcode() == ISD::FNEG && !TLI.isFNegFree(N0.getValueType())) || |
| 6068 | (N0.getOpcode() == ISD::FABS && !TLI.isFAbsFree(N0.getValueType()))) && |
| Nadav Rotem | 24a822a | 2012-09-13 14:54:28 +0000 | [diff] [blame] | 6069 | N0.getNode()->hasOneUse() && VT.isInteger() && |
| 6070 | !VT.isVector() && !N0.getValueType().isVector()) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6071 | SDValue NewConv = DAG.getNode(ISD::BITCAST, SDLoc(N0), VT, |
| Bill Wendling | 4e0a615 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 6072 | N0.getOperand(0)); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6073 | AddToWorkList(NewConv.getNode()); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6074 | |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6075 | APInt SignBit = APInt::getSignBit(VT.getSizeInBits()); |
| Chris Lattner | 888560d | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 6076 | if (N0.getOpcode() == ISD::FNEG) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6077 | return DAG.getNode(ISD::XOR, SDLoc(N), VT, |
| Bill Wendling | 4e0a615 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 6078 | NewConv, DAG.getConstant(SignBit, VT)); |
| Chris Lattner | 888560d | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 6079 | assert(N0.getOpcode() == ISD::FABS); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6080 | return DAG.getNode(ISD::AND, SDLoc(N), VT, |
| Bill Wendling | 4e0a615 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 6081 | NewConv, DAG.getConstant(~SignBit, VT)); |
| Chris Lattner | 888560d | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 6082 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6083 | |
| Bill Wendling | 4e0a615 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 6084 | // fold (bitconvert (fcopysign cst, x)) -> |
| 6085 | // (or (and (bitconvert x), sign), (and cst, (not sign))) |
| 6086 | // Note that we don't handle (copysign x, cst) because this can always be |
| 6087 | // folded to an fneg or fabs. |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6088 | if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() && |
| Chris Lattner | 2ee91f4 | 2008-01-27 23:32:17 +0000 | [diff] [blame] | 6089 | isa<ConstantFPSDNode>(N0.getOperand(0)) && |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6090 | VT.isInteger() && !VT.isVector()) { |
| 6091 | unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits(); |
| Owen Anderson | 117c9e8 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6092 | EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth); |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6093 | if (isTypeLegal(IntXVT)) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6094 | SDValue X = DAG.getNode(ISD::BITCAST, SDLoc(N0), |
| Bill Wendling | 4e0a615 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 6095 | IntXVT, N0.getOperand(1)); |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6096 | AddToWorkList(X.getNode()); |
| Chris Lattner | 888560d | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 6097 | |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6098 | // If X has a different width than the result/lhs, sext it or truncate it. |
| 6099 | unsigned VTWidth = VT.getSizeInBits(); |
| 6100 | if (OrigXWidth < VTWidth) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6101 | X = DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, X); |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6102 | AddToWorkList(X.getNode()); |
| 6103 | } else if (OrigXWidth > VTWidth) { |
| 6104 | // To get the sign bit in the right place, we have to shift it right |
| 6105 | // before truncating. |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6106 | X = DAG.getNode(ISD::SRL, SDLoc(X), |
| Bill Wendling | 4e0a615 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 6107 | X.getValueType(), X, |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6108 | DAG.getConstant(OrigXWidth-VTWidth, X.getValueType())); |
| 6109 | AddToWorkList(X.getNode()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6110 | X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X); |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6111 | AddToWorkList(X.getNode()); |
| 6112 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6113 | |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6114 | APInt SignBit = APInt::getSignBit(VT.getSizeInBits()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6115 | X = DAG.getNode(ISD::AND, SDLoc(X), VT, |
| Bill Wendling | 4e0a615 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 6116 | X, DAG.getConstant(SignBit, VT)); |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6117 | AddToWorkList(X.getNode()); |
| Chris Lattner | 888560d | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 6118 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6119 | SDValue Cst = DAG.getNode(ISD::BITCAST, SDLoc(N0), |
| Bill Wendling | 4e0a615 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 6120 | VT, N0.getOperand(0)); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6121 | Cst = DAG.getNode(ISD::AND, SDLoc(Cst), VT, |
| Bill Wendling | 4e0a615 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 6122 | Cst, DAG.getConstant(~SignBit, VT)); |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6123 | AddToWorkList(Cst.getNode()); |
| Chris Lattner | 888560d | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 6124 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6125 | return DAG.getNode(ISD::OR, SDLoc(N), VT, X, Cst); |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6126 | } |
| Chris Lattner | 888560d | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 6127 | } |
| Evan Cheng | b980f6f | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 6128 | |
| Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 6129 | // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive. |
| Evan Cheng | b980f6f | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 6130 | if (N0.getOpcode() == ISD::BUILD_PAIR) { |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6131 | SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT); |
| 6132 | if (CombineLD.getNode()) |
| Evan Cheng | b980f6f | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 6133 | return CombineLD; |
| 6134 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6135 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6136 | return SDValue(); |
| Chris Lattner | a187460 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 6137 | } |
| 6138 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6139 | SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) { |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6140 | EVT VT = N->getValueType(0); |
| Evan Cheng | b980f6f | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 6141 | return CombineConsecutiveLoads(N, VT); |
| 6142 | } |
| 6143 | |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6144 | /// ConstantFoldBITCASTofBUILD_VECTOR - We know that BV is a build_vector |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6145 | /// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6146 | /// destination element value type. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6147 | SDValue DAGCombiner:: |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6148 | ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) { |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6149 | EVT SrcEltVT = BV->getValueType(0).getVectorElementType(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6150 | |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6151 | // If this is already the right type, we're done. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6152 | if (SrcEltVT == DstEltVT) return SDValue(BV, 0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6153 | |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6154 | unsigned SrcBitSize = SrcEltVT.getSizeInBits(); |
| 6155 | unsigned DstBitSize = DstEltVT.getSizeInBits(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6156 | |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6157 | // If this is a conversion of N elements of one type to N elements of another |
| 6158 | // type, convert each element. This handles FP<->INT cases. |
| 6159 | if (SrcBitSize == DstBitSize) { |
| Nate Begeman | 317b969 | 2010-07-27 18:02:18 +0000 | [diff] [blame] | 6160 | EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, |
| 6161 | BV->getValueType(0).getVectorNumElements()); |
| 6162 | |
| 6163 | // Due to the FP element handling below calling this routine recursively, |
| 6164 | // we can end up with a scalar-to-vector node here. |
| 6165 | if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6166 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT, |
| 6167 | DAG.getNode(ISD::BITCAST, SDLoc(BV), |
| Nate Begeman | 317b969 | 2010-07-27 18:02:18 +0000 | [diff] [blame] | 6168 | DstEltVT, BV->getOperand(0))); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6169 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6170 | SmallVector<SDValue, 8> Ops; |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6171 | for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) { |
| Bob Wilson | 59dbbb2 | 2009-04-13 22:05:19 +0000 | [diff] [blame] | 6172 | SDValue Op = BV->getOperand(i); |
| 6173 | // If the vector element type is not legal, the BUILD_VECTOR operands |
| 6174 | // are promoted and implicitly truncated. Make that explicit here. |
| Bob Wilson | da188eb | 2009-04-20 17:27:09 +0000 | [diff] [blame] | 6175 | if (Op.getValueType() != SrcEltVT) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6176 | Op = DAG.getNode(ISD::TRUNCATE, SDLoc(BV), SrcEltVT, Op); |
| 6177 | Ops.push_back(DAG.getNode(ISD::BITCAST, SDLoc(BV), |
| Bob Wilson | 59dbbb2 | 2009-04-13 22:05:19 +0000 | [diff] [blame] | 6178 | DstEltVT, Op)); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6179 | AddToWorkList(Ops.back().getNode()); |
| Chris Lattner | 098c01e | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 6180 | } |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6181 | return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT, |
| Evan Cheng | a49de9d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 6182 | &Ops[0], Ops.size()); |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6183 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6184 | |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6185 | // Otherwise, we're growing or shrinking the elements. To avoid having to |
| 6186 | // handle annoying details of growing/shrinking FP values, we convert them to |
| 6187 | // int first. |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6188 | if (SrcEltVT.isFloatingPoint()) { |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6189 | // Convert the input float vector to a int vector where the elements are the |
| 6190 | // same sizes. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6191 | assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!"); |
| Owen Anderson | 117c9e8 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6192 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits()); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6193 | BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode(); |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6194 | SrcEltVT = IntVT; |
| 6195 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6196 | |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6197 | // Now we know the input is an integer vector. If the output is a FP type, |
| 6198 | // convert to integer first, then to FP of the right size. |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6199 | if (DstEltVT.isFloatingPoint()) { |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6200 | assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!"); |
| Owen Anderson | 117c9e8 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6201 | EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits()); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6202 | SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6203 | |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6204 | // Next, convert to FP elements of the same size. |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6205 | return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT); |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6206 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6207 | |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6208 | // Okay, we know the src/dst types are both integers of differing types. |
| 6209 | // Handling growing first. |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6210 | assert(SrcEltVT.isInteger() && DstEltVT.isInteger()); |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6211 | if (SrcBitSize < DstBitSize) { |
| 6212 | unsigned NumInputsPerOutput = DstBitSize/SrcBitSize; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6213 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6214 | SmallVector<SDValue, 8> Ops; |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6215 | for (unsigned i = 0, e = BV->getNumOperands(); i != e; |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6216 | i += NumInputsPerOutput) { |
| 6217 | bool isLE = TLI.isLittleEndian(); |
| Dan Gohman | e1c4f99 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 6218 | APInt NewBits = APInt(DstBitSize, 0); |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6219 | bool EltIsUndef = true; |
| 6220 | for (unsigned j = 0; j != NumInputsPerOutput; ++j) { |
| 6221 | // Shift the previously computed bits over. |
| 6222 | NewBits <<= SrcBitSize; |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6223 | SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j)); |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6224 | if (Op.getOpcode() == ISD::UNDEF) continue; |
| 6225 | EltIsUndef = false; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6226 | |
| Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 6227 | NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue(). |
| Dan Gohman | ecd40a3 | 2010-04-12 02:24:01 +0000 | [diff] [blame] | 6228 | zextOrTrunc(SrcBitSize).zext(DstBitSize); |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6229 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6230 | |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6231 | if (EltIsUndef) |
| Dale Johannesen | 8493575 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 6232 | Ops.push_back(DAG.getUNDEF(DstEltVT)); |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6233 | else |
| 6234 | Ops.push_back(DAG.getConstant(NewBits, DstEltVT)); |
| 6235 | } |
| 6236 | |
| Owen Anderson | 117c9e8 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6237 | EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6238 | return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT, |
| Evan Cheng | a49de9d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 6239 | &Ops[0], Ops.size()); |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6240 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6241 | |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6242 | // Finally, this must be the case where we are shrinking elements: each input |
| 6243 | // turns into multiple outputs. |
| Evan Cheng | 6200c22 | 2008-02-18 23:04:32 +0000 | [diff] [blame] | 6244 | bool isS2V = ISD::isScalarToVector(BV); |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6245 | unsigned NumOutputsPerInput = SrcBitSize/DstBitSize; |
| Owen Anderson | 117c9e8 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 6246 | EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, |
| 6247 | NumOutputsPerInput*BV->getNumOperands()); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6248 | SmallVector<SDValue, 8> Ops; |
| Bill Wendling | cb9be5d | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 6249 | |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6250 | for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) { |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6251 | if (BV->getOperand(i).getOpcode() == ISD::UNDEF) { |
| 6252 | for (unsigned j = 0; j != NumOutputsPerInput; ++j) |
| Dale Johannesen | 8493575 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 6253 | Ops.push_back(DAG.getUNDEF(DstEltVT)); |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6254 | continue; |
| 6255 | } |
| Bill Wendling | cb9be5d | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 6256 | |
| Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 6257 | APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))-> |
| 6258 | getAPIntValue().zextOrTrunc(SrcBitSize); |
| Bill Wendling | cb9be5d | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 6259 | |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6260 | for (unsigned j = 0; j != NumOutputsPerInput; ++j) { |
| Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 6261 | APInt ThisVal = OpVal.trunc(DstBitSize); |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6262 | Ops.push_back(DAG.getConstant(ThisVal, DstEltVT)); |
| Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 6263 | if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal) |
| Evan Cheng | 6200c22 | 2008-02-18 23:04:32 +0000 | [diff] [blame] | 6264 | // Simply turn this into a SCALAR_TO_VECTOR of the new type. |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6265 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT, |
| Bill Wendling | cb9be5d | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 6266 | Ops[0]); |
| Dan Gohman | e1c4f99 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 6267 | OpVal = OpVal.lshr(DstBitSize); |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6268 | } |
| 6269 | |
| 6270 | // For big endian targets, swap the order of the pieces of each element. |
| Duncan Sands | 7377f5f | 2008-02-11 10:37:04 +0000 | [diff] [blame] | 6271 | if (TLI.isBigEndian()) |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6272 | std::reverse(Ops.end()-NumOutputsPerInput, Ops.end()); |
| 6273 | } |
| Bill Wendling | cb9be5d | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 6274 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6275 | return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT, |
| Evan Cheng | a49de9d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 6276 | &Ops[0], Ops.size()); |
| Chris Lattner | e4e64b6 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 6277 | } |
| 6278 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6279 | SDValue DAGCombiner::visitFADD(SDNode *N) { |
| 6280 | SDValue N0 = N->getOperand(0); |
| 6281 | SDValue N1 = N->getOperand(1); |
| Nate Begeman | 418c6e4 | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 6282 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 6283 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6284 | EVT VT = N->getValueType(0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6285 | |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6286 | // fold vector ops |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6287 | if (VT.isVector()) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6288 | SDValue FoldedVOp = SimplifyVBinOp(N); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6289 | if (FoldedVOp.getNode()) return FoldedVOp; |
| Dan Gohman | 80f9f07 | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 6290 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6291 | |
| Lang Hames | a33db65 | 2012-06-14 20:37:15 +0000 | [diff] [blame] | 6292 | // fold (fadd c1, c2) -> c1 + c2 |
| Ulrich Weigand | 3abb343 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6293 | if (N0CFP && N1CFP) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6294 | return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N1); |
| Nate Begeman | 418c6e4 | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 6295 | // canonicalize constant to RHS |
| 6296 | if (N0CFP && !N1CFP) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6297 | return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N0); |
| Bill Wendling | cb9be5d | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 6298 | // fold (fadd A, 0) -> A |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6299 | if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && |
| 6300 | N1CFP->getValueAPF().isZero()) |
| Dan Gohman | 1f3411d | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 6301 | return N0; |
| Bill Wendling | cb9be5d | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 6302 | // fold (fadd A, (fneg B)) -> (fsub A, B) |
| Owen Anderson | 2ee7c4d | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6303 | if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) && |
| Nadav Rotem | 841c9a8 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 6304 | isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options) == 2) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6305 | return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0, |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6306 | GetNegatedExpression(N1, DAG, LegalOperations)); |
| Bill Wendling | cb9be5d | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 6307 | // fold (fadd (fneg A), B) -> (fsub B, A) |
| Owen Anderson | 2ee7c4d | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6308 | if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) && |
| Nadav Rotem | 841c9a8 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 6309 | isNegatibleForFree(N0, LegalOperations, TLI, &DAG.getTarget().Options) == 2) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6310 | return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N1, |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6311 | GetNegatedExpression(N0, DAG, LegalOperations)); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6312 | |
| Chris Lattner | 0199fd6 | 2007-01-08 23:04:05 +0000 | [diff] [blame] | 6313 | // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2)) |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6314 | if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && |
| 6315 | N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() && |
| 6316 | isa<ConstantFPSDNode>(N0.getOperand(1))) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6317 | return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0.getOperand(0), |
| 6318 | DAG.getNode(ISD::FADD, SDLoc(N), VT, |
| Bill Wendling | a6c75ff | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 6319 | N0.getOperand(1), N1)); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6320 | |
| Shuxin Yang | 93b1f12 | 2013-03-25 22:52:29 +0000 | [diff] [blame] | 6321 | // No FP constant should be created after legalization as Instruction |
| 6322 | // Selection pass has hard time in dealing with FP constant. |
| 6323 | // |
| 6324 | // We don't need test this condition for transformation like following, as |
| 6325 | // the DAG being transformed implies it is legal to take FP constant as |
| 6326 | // operand. |
| Stephen Lin | cfe7f35 | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 6327 | // |
| Shuxin Yang | 93b1f12 | 2013-03-25 22:52:29 +0000 | [diff] [blame] | 6328 | // (fadd (fmul c, x), x) -> (fmul c+1, x) |
| Stephen Lin | cfe7f35 | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 6329 | // |
| Shuxin Yang | 93b1f12 | 2013-03-25 22:52:29 +0000 | [diff] [blame] | 6330 | bool AllowNewFpConst = (Level < AfterLegalizeDAG); |
| 6331 | |
| Owen Anderson | b351c8d | 2012-11-01 02:00:53 +0000 | [diff] [blame] | 6332 | // If allow, fold (fadd (fneg x), x) -> 0.0 |
| Shuxin Yang | 93b1f12 | 2013-03-25 22:52:29 +0000 | [diff] [blame] | 6333 | if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath && |
| Stephen Lin | 8e8424e | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6334 | N0.getOpcode() == ISD::FNEG && N0.getOperand(0) == N1) |
| Owen Anderson | b351c8d | 2012-11-01 02:00:53 +0000 | [diff] [blame] | 6335 | return DAG.getConstantFP(0.0, VT); |
| Owen Anderson | b351c8d | 2012-11-01 02:00:53 +0000 | [diff] [blame] | 6336 | |
| 6337 | // If allow, fold (fadd x, (fneg x)) -> 0.0 |
| Shuxin Yang | 93b1f12 | 2013-03-25 22:52:29 +0000 | [diff] [blame] | 6338 | if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath && |
| Stephen Lin | 8e8424e | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6339 | N1.getOpcode() == ISD::FNEG && N1.getOperand(0) == N0) |
| Owen Anderson | b351c8d | 2012-11-01 02:00:53 +0000 | [diff] [blame] | 6340 | return DAG.getConstantFP(0.0, VT); |
| Owen Anderson | b351c8d | 2012-11-01 02:00:53 +0000 | [diff] [blame] | 6341 | |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6342 | // In unsafe math mode, we can fold chains of FADD's of the same value |
| 6343 | // into multiplications. This transform is not safe in general because |
| 6344 | // we are reducing the number of rounding steps. |
| 6345 | if (DAG.getTarget().Options.UnsafeFPMath && |
| 6346 | TLI.isOperationLegalOrCustom(ISD::FMUL, VT) && |
| 6347 | !N0CFP && !N1CFP) { |
| 6348 | if (N0.getOpcode() == ISD::FMUL) { |
| 6349 | ConstantFPSDNode *CFP00 = dyn_cast<ConstantFPSDNode>(N0.getOperand(0)); |
| 6350 | ConstantFPSDNode *CFP01 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1)); |
| 6351 | |
| Stephen Lin | e31f2d2 | 2013-06-14 18:17:35 +0000 | [diff] [blame] | 6352 | // (fadd (fmul c, x), x) -> (fmul x, c+1) |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6353 | if (CFP00 && !CFP01 && N0.getOperand(1) == N1) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6354 | SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT, |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6355 | SDValue(CFP00, 0), |
| 6356 | DAG.getConstantFP(1.0, VT)); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6357 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6358 | N1, NewCFP); |
| 6359 | } |
| 6360 | |
| Stephen Lin | e31f2d2 | 2013-06-14 18:17:35 +0000 | [diff] [blame] | 6361 | // (fadd (fmul x, c), x) -> (fmul x, c+1) |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6362 | if (CFP01 && !CFP00 && N0.getOperand(0) == N1) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6363 | SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT, |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6364 | SDValue(CFP01, 0), |
| 6365 | DAG.getConstantFP(1.0, VT)); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6366 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6367 | N1, NewCFP); |
| 6368 | } |
| 6369 | |
| Stephen Lin | e31f2d2 | 2013-06-14 18:17:35 +0000 | [diff] [blame] | 6370 | // (fadd (fmul c, x), (fadd x, x)) -> (fmul x, c+2) |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6371 | if (CFP00 && !CFP01 && N1.getOpcode() == ISD::FADD && |
| 6372 | N1.getOperand(0) == N1.getOperand(1) && |
| 6373 | N0.getOperand(1) == N1.getOperand(0)) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6374 | SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT, |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6375 | SDValue(CFP00, 0), |
| 6376 | DAG.getConstantFP(2.0, VT)); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6377 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6378 | N0.getOperand(1), NewCFP); |
| 6379 | } |
| 6380 | |
| Stephen Lin | e31f2d2 | 2013-06-14 18:17:35 +0000 | [diff] [blame] | 6381 | // (fadd (fmul x, c), (fadd x, x)) -> (fmul x, c+2) |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6382 | if (CFP01 && !CFP00 && N1.getOpcode() == ISD::FADD && |
| 6383 | N1.getOperand(0) == N1.getOperand(1) && |
| 6384 | N0.getOperand(0) == N1.getOperand(0)) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6385 | SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT, |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6386 | SDValue(CFP01, 0), |
| 6387 | DAG.getConstantFP(2.0, VT)); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6388 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6389 | N0.getOperand(0), NewCFP); |
| 6390 | } |
| 6391 | } |
| 6392 | |
| 6393 | if (N1.getOpcode() == ISD::FMUL) { |
| 6394 | ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0)); |
| 6395 | ConstantFPSDNode *CFP11 = dyn_cast<ConstantFPSDNode>(N1.getOperand(1)); |
| 6396 | |
| Stephen Lin | e31f2d2 | 2013-06-14 18:17:35 +0000 | [diff] [blame] | 6397 | // (fadd x, (fmul c, x)) -> (fmul x, c+1) |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6398 | if (CFP10 && !CFP11 && N1.getOperand(1) == N0) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6399 | SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT, |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6400 | SDValue(CFP10, 0), |
| 6401 | DAG.getConstantFP(1.0, VT)); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6402 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6403 | N0, NewCFP); |
| 6404 | } |
| 6405 | |
| Stephen Lin | e31f2d2 | 2013-06-14 18:17:35 +0000 | [diff] [blame] | 6406 | // (fadd x, (fmul x, c)) -> (fmul x, c+1) |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6407 | if (CFP11 && !CFP10 && N1.getOperand(0) == N0) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6408 | SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT, |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6409 | SDValue(CFP11, 0), |
| 6410 | DAG.getConstantFP(1.0, VT)); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6411 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6412 | N0, NewCFP); |
| 6413 | } |
| 6414 | |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6415 | |
| Stephen Lin | e31f2d2 | 2013-06-14 18:17:35 +0000 | [diff] [blame] | 6416 | // (fadd (fadd x, x), (fmul c, x)) -> (fmul x, c+2) |
| 6417 | if (CFP10 && !CFP11 && N0.getOpcode() == ISD::FADD && |
| 6418 | N0.getOperand(0) == N0.getOperand(1) && |
| 6419 | N1.getOperand(1) == N0.getOperand(0)) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6420 | SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT, |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6421 | SDValue(CFP10, 0), |
| 6422 | DAG.getConstantFP(2.0, VT)); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6423 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
| Stephen Lin | e31f2d2 | 2013-06-14 18:17:35 +0000 | [diff] [blame] | 6424 | N1.getOperand(1), NewCFP); |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6425 | } |
| 6426 | |
| Stephen Lin | e31f2d2 | 2013-06-14 18:17:35 +0000 | [diff] [blame] | 6427 | // (fadd (fadd x, x), (fmul x, c)) -> (fmul x, c+2) |
| 6428 | if (CFP11 && !CFP10 && N0.getOpcode() == ISD::FADD && |
| 6429 | N0.getOperand(0) == N0.getOperand(1) && |
| 6430 | N1.getOperand(0) == N0.getOperand(0)) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6431 | SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT, |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6432 | SDValue(CFP11, 0), |
| 6433 | DAG.getConstantFP(2.0, VT)); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6434 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
| Stephen Lin | e31f2d2 | 2013-06-14 18:17:35 +0000 | [diff] [blame] | 6435 | N1.getOperand(0), NewCFP); |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6436 | } |
| 6437 | } |
| 6438 | |
| Shuxin Yang | 93b1f12 | 2013-03-25 22:52:29 +0000 | [diff] [blame] | 6439 | if (N0.getOpcode() == ISD::FADD && AllowNewFpConst) { |
| Shuxin Yang | cadd8a0 | 2013-02-02 00:22:03 +0000 | [diff] [blame] | 6440 | ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N0.getOperand(0)); |
| Stephen Lin | 4e69d01 | 2013-06-14 21:33:58 +0000 | [diff] [blame] | 6441 | // (fadd (fadd x, x), x) -> (fmul x, 3.0) |
| Shuxin Yang | cadd8a0 | 2013-02-02 00:22:03 +0000 | [diff] [blame] | 6442 | if (!CFP && N0.getOperand(0) == N0.getOperand(1) && |
| Stephen Lin | 8e8424e | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6443 | (N0.getOperand(0) == N1)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6444 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
| Shuxin Yang | cadd8a0 | 2013-02-02 00:22:03 +0000 | [diff] [blame] | 6445 | N1, DAG.getConstantFP(3.0, VT)); |
| Shuxin Yang | cadd8a0 | 2013-02-02 00:22:03 +0000 | [diff] [blame] | 6446 | } |
| 6447 | |
| Shuxin Yang | 93b1f12 | 2013-03-25 22:52:29 +0000 | [diff] [blame] | 6448 | if (N1.getOpcode() == ISD::FADD && AllowNewFpConst) { |
| Shuxin Yang | cadd8a0 | 2013-02-02 00:22:03 +0000 | [diff] [blame] | 6449 | ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0)); |
| Stephen Lin | 4e69d01 | 2013-06-14 21:33:58 +0000 | [diff] [blame] | 6450 | // (fadd x, (fadd x, x)) -> (fmul x, 3.0) |
| Shuxin Yang | cadd8a0 | 2013-02-02 00:22:03 +0000 | [diff] [blame] | 6451 | if (!CFP10 && N1.getOperand(0) == N1.getOperand(1) && |
| Stephen Lin | 8e8424e | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6452 | N1.getOperand(0) == N0) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6453 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
| Shuxin Yang | cadd8a0 | 2013-02-02 00:22:03 +0000 | [diff] [blame] | 6454 | N0, DAG.getConstantFP(3.0, VT)); |
| Shuxin Yang | cadd8a0 | 2013-02-02 00:22:03 +0000 | [diff] [blame] | 6455 | } |
| 6456 | |
| Stephen Lin | 4e69d01 | 2013-06-14 21:33:58 +0000 | [diff] [blame] | 6457 | // (fadd (fadd x, x), (fadd x, x)) -> (fmul x, 4.0) |
| Shuxin Yang | 93b1f12 | 2013-03-25 22:52:29 +0000 | [diff] [blame] | 6458 | if (AllowNewFpConst && |
| 6459 | N0.getOpcode() == ISD::FADD && N1.getOpcode() == ISD::FADD && |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6460 | N0.getOperand(0) == N0.getOperand(1) && |
| 6461 | N1.getOperand(0) == N1.getOperand(1) && |
| Stephen Lin | 8e8424e | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6462 | N0.getOperand(0) == N1.getOperand(0)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6463 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6464 | N0.getOperand(0), |
| 6465 | DAG.getConstantFP(4.0, VT)); |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6466 | } |
| 6467 | |
| Lang Hames | 39fb1d0 | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6468 | // FADD -> FMA combines: |
| Lang Hames | b8650f1 | 2012-06-22 01:09:09 +0000 | [diff] [blame] | 6469 | if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast || |
| Lang Hames | 39fb1d0 | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6470 | DAG.getTarget().Options.UnsafeFPMath) && |
| Stephen Lin | 73de7bf | 2013-07-09 18:16:56 +0000 | [diff] [blame] | 6471 | DAG.getTarget().getTargetLowering()->isFMAFasterThanFMulAndFAdd(VT) && |
| 6472 | (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT))) { |
| Lang Hames | 39fb1d0 | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6473 | |
| 6474 | // fold (fadd (fmul x, y), z) -> (fma x, y, z) |
| Stephen Lin | 8e8424e | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6475 | if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse()) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6476 | return DAG.getNode(ISD::FMA, SDLoc(N), VT, |
| Lang Hames | 39fb1d0 | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6477 | N0.getOperand(0), N0.getOperand(1), N1); |
| Owen Anderson | cc61f87 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6478 | |
| Michael Liao | ec385012 | 2012-09-01 04:09:16 +0000 | [diff] [blame] | 6479 | // fold (fadd x, (fmul y, z)) -> (fma y, z, x) |
| Lang Hames | 39fb1d0 | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6480 | // Note: Commutes FADD operands. |
| Stephen Lin | 8e8424e | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6481 | if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse()) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6482 | return DAG.getNode(ISD::FMA, SDLoc(N), VT, |
| Lang Hames | 39fb1d0 | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6483 | N1.getOperand(0), N1.getOperand(1), N0); |
| Lang Hames | 39fb1d0 | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6484 | } |
| 6485 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6486 | return SDValue(); |
| Chris Lattner | 6f3b577 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6487 | } |
| 6488 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6489 | SDValue DAGCombiner::visitFSUB(SDNode *N) { |
| 6490 | SDValue N0 = N->getOperand(0); |
| 6491 | SDValue N1 = N->getOperand(1); |
| Nate Begeman | 418c6e4 | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 6492 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 6493 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6494 | EVT VT = N->getValueType(0); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6495 | SDLoc dl(N); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6496 | |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6497 | // fold vector ops |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6498 | if (VT.isVector()) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6499 | SDValue FoldedVOp = SimplifyVBinOp(N); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6500 | if (FoldedVOp.getNode()) return FoldedVOp; |
| Dan Gohman | 80f9f07 | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 6501 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6502 | |
| Nate Begeman | 418c6e4 | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 6503 | // fold (fsub c1, c2) -> c1-c2 |
| Ulrich Weigand | 3abb343 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6504 | if (N0CFP && N1CFP) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6505 | return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0, N1); |
| Bill Wendling | cb9be5d | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 6506 | // fold (fsub A, 0) -> A |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6507 | if (DAG.getTarget().Options.UnsafeFPMath && |
| 6508 | N1CFP && N1CFP->getValueAPF().isZero()) |
| Dan Gohman | 1275e28 | 2009-01-23 19:10:37 +0000 | [diff] [blame] | 6509 | return N0; |
| Bill Wendling | cb9be5d | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 6510 | // fold (fsub 0, B) -> -B |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6511 | if (DAG.getTarget().Options.UnsafeFPMath && |
| 6512 | N0CFP && N0CFP->getValueAPF().isZero()) { |
| Owen Anderson | 2ee7c4d | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6513 | if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options)) |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6514 | return GetNegatedExpression(N1, DAG, LegalOperations); |
| Dan Gohman | 1f3411d | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 6515 | if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT)) |
| Elena Demikhovsky | 3cb3b00 | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 6516 | return DAG.getNode(ISD::FNEG, dl, VT, N1); |
| Dan Gohman | 9a70823 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 6517 | } |
| Bill Wendling | cb9be5d | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 6518 | // fold (fsub A, (fneg B)) -> (fadd A, B) |
| Owen Anderson | 2ee7c4d | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6519 | if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options)) |
| Elena Demikhovsky | 3cb3b00 | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 6520 | return DAG.getNode(ISD::FADD, dl, VT, N0, |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6521 | GetNegatedExpression(N1, DAG, LegalOperations)); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6522 | |
| Bill Wendling | df170db | 2012-03-15 05:12:00 +0000 | [diff] [blame] | 6523 | // If 'unsafe math' is enabled, fold |
| Owen Anderson | ab63d84 | 2012-05-07 20:51:25 +0000 | [diff] [blame] | 6524 | // (fsub x, x) -> 0.0 & |
| Bill Wendling | df170db | 2012-03-15 05:12:00 +0000 | [diff] [blame] | 6525 | // (fsub x, (fadd x, y)) -> (fneg y) & |
| 6526 | // (fsub x, (fadd y, x)) -> (fneg y) |
| 6527 | if (DAG.getTarget().Options.UnsafeFPMath) { |
| Owen Anderson | ab63d84 | 2012-05-07 20:51:25 +0000 | [diff] [blame] | 6528 | if (N0 == N1) |
| 6529 | return DAG.getConstantFP(0.0f, VT); |
| 6530 | |
| Bill Wendling | df170db | 2012-03-15 05:12:00 +0000 | [diff] [blame] | 6531 | if (N1.getOpcode() == ISD::FADD) { |
| 6532 | SDValue N10 = N1->getOperand(0); |
| 6533 | SDValue N11 = N1->getOperand(1); |
| 6534 | |
| 6535 | if (N10 == N0 && isNegatibleForFree(N11, LegalOperations, TLI, |
| 6536 | &DAG.getTarget().Options)) |
| 6537 | return GetNegatedExpression(N11, DAG, LegalOperations); |
| Stephen Lin | 1094750 | 2013-07-10 20:47:39 +0000 | [diff] [blame] | 6538 | |
| Stephen Lin | 8e8424e | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6539 | if (N11 == N0 && isNegatibleForFree(N10, LegalOperations, TLI, |
| 6540 | &DAG.getTarget().Options)) |
| Bill Wendling | df170db | 2012-03-15 05:12:00 +0000 | [diff] [blame] | 6541 | return GetNegatedExpression(N10, DAG, LegalOperations); |
| 6542 | } |
| 6543 | } |
| 6544 | |
| Lang Hames | 39fb1d0 | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6545 | // FSUB -> FMA combines: |
| Lang Hames | b8650f1 | 2012-06-22 01:09:09 +0000 | [diff] [blame] | 6546 | if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast || |
| Lang Hames | 39fb1d0 | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6547 | DAG.getTarget().Options.UnsafeFPMath) && |
| Stephen Lin | 73de7bf | 2013-07-09 18:16:56 +0000 | [diff] [blame] | 6548 | DAG.getTarget().getTargetLowering()->isFMAFasterThanFMulAndFAdd(VT) && |
| 6549 | (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT))) { |
| Lang Hames | 39fb1d0 | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6550 | |
| 6551 | // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z)) |
| Stephen Lin | 8e8424e | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6552 | if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse()) |
| Elena Demikhovsky | 3cb3b00 | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 6553 | return DAG.getNode(ISD::FMA, dl, VT, |
| Lang Hames | 39fb1d0 | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6554 | N0.getOperand(0), N0.getOperand(1), |
| Elena Demikhovsky | 3cb3b00 | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 6555 | DAG.getNode(ISD::FNEG, dl, VT, N1)); |
| Lang Hames | 39fb1d0 | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6556 | |
| 6557 | // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x) |
| 6558 | // Note: Commutes FSUB operands. |
| Stephen Lin | 1094750 | 2013-07-10 20:47:39 +0000 | [diff] [blame] | 6559 | if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse()) |
| Elena Demikhovsky | 3cb3b00 | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 6560 | return DAG.getNode(ISD::FMA, dl, VT, |
| 6561 | DAG.getNode(ISD::FNEG, dl, VT, |
| Lang Hames | 39fb1d0 | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6562 | N1.getOperand(0)), |
| 6563 | N1.getOperand(1), N0); |
| Elena Demikhovsky | 3cb3b00 | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 6564 | |
| Stephen Lin | 8e8424e | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6565 | // fold (fsub (fneg (fmul, x, y)), z) -> (fma (fneg x), y, (fneg z)) |
| Stephen Lin | cfe7f35 | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 6566 | if (N0.getOpcode() == ISD::FNEG && |
| Elena Demikhovsky | 3cb3b00 | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 6567 | N0.getOperand(0).getOpcode() == ISD::FMUL && |
| 6568 | N0->hasOneUse() && N0.getOperand(0).hasOneUse()) { |
| 6569 | SDValue N00 = N0.getOperand(0).getOperand(0); |
| 6570 | SDValue N01 = N0.getOperand(0).getOperand(1); |
| 6571 | return DAG.getNode(ISD::FMA, dl, VT, |
| 6572 | DAG.getNode(ISD::FNEG, dl, VT, N00), N01, |
| 6573 | DAG.getNode(ISD::FNEG, dl, VT, N1)); |
| 6574 | } |
| Lang Hames | 39fb1d0 | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6575 | } |
| 6576 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6577 | return SDValue(); |
| Chris Lattner | 6f3b577 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6578 | } |
| 6579 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6580 | SDValue DAGCombiner::visitFMUL(SDNode *N) { |
| 6581 | SDValue N0 = N->getOperand(0); |
| 6582 | SDValue N1 = N->getOperand(1); |
| Nate Begeman | ec48a1b | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 6583 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 6584 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6585 | EVT VT = N->getValueType(0); |
| Owen Anderson | 2ee7c4d | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6586 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| Chris Lattner | 6f3b577 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6587 | |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6588 | // fold vector ops |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6589 | if (VT.isVector()) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6590 | SDValue FoldedVOp = SimplifyVBinOp(N); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6591 | if (FoldedVOp.getNode()) return FoldedVOp; |
| Dan Gohman | 80f9f07 | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 6592 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6593 | |
| Nate Begeman | ec48a1b | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 6594 | // fold (fmul c1, c2) -> c1*c2 |
| Ulrich Weigand | 3abb343 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6595 | if (N0CFP && N1CFP) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6596 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0, N1); |
| Nate Begeman | ec48a1b | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 6597 | // canonicalize constant to RHS |
| Nate Begeman | 418c6e4 | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 6598 | if (N0CFP && !N1CFP) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6599 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N1, N0); |
| Bill Wendling | 3dc5d24 | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 6600 | // fold (fmul A, 0) -> 0 |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6601 | if (DAG.getTarget().Options.UnsafeFPMath && |
| 6602 | N1CFP && N1CFP->getValueAPF().isZero()) |
| Dan Gohman | 1f3411d | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 6603 | return N1; |
| Dan Gohman | 7b6b5dd | 2009-06-04 17:12:12 +0000 | [diff] [blame] | 6604 | // fold (fmul A, 0) -> 0, vector edition. |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6605 | if (DAG.getTarget().Options.UnsafeFPMath && |
| 6606 | ISD::isBuildVectorAllZeros(N1.getNode())) |
| Dan Gohman | 7b6b5dd | 2009-06-04 17:12:12 +0000 | [diff] [blame] | 6607 | return N1; |
| Owen Anderson | b5f167c | 2012-05-02 21:32:35 +0000 | [diff] [blame] | 6608 | // fold (fmul A, 1.0) -> A |
| 6609 | if (N1CFP && N1CFP->isExactlyValue(1.0)) |
| 6610 | return N0; |
| Nate Begeman | ec48a1b | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 6611 | // fold (fmul X, 2.0) -> (fadd X, X) |
| 6612 | if (N1CFP && N1CFP->isExactlyValue(+2.0)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6613 | return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N0); |
| Dan Gohman | b717091 | 2009-08-10 16:50:32 +0000 | [diff] [blame] | 6614 | // fold (fmul X, -1.0) -> (fneg X) |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 6615 | if (N1CFP && N1CFP->isExactlyValue(-1.0)) |
| Dan Gohman | 1f3411d | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 6616 | if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6617 | return DAG.getNode(ISD::FNEG, SDLoc(N), VT, N0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6618 | |
| Bill Wendling | 3dc5d24 | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 6619 | // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y) |
| Owen Anderson | 2ee7c4d | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6620 | if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI, |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6621 | &DAG.getTarget().Options)) { |
| Stephen Lin | cfe7f35 | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 6622 | if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI, |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6623 | &DAG.getTarget().Options)) { |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 6624 | // Both can be negated for free, check to see if at least one is cheaper |
| 6625 | // negated. |
| 6626 | if (LHSNeg == 2 || RHSNeg == 2) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6627 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6628 | GetNegatedExpression(N0, DAG, LegalOperations), |
| 6629 | GetNegatedExpression(N1, DAG, LegalOperations)); |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 6630 | } |
| 6631 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6632 | |
| Chris Lattner | 0199fd6 | 2007-01-08 23:04:05 +0000 | [diff] [blame] | 6633 | // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2)) |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6634 | if (DAG.getTarget().Options.UnsafeFPMath && |
| 6635 | N1CFP && N0.getOpcode() == ISD::FMUL && |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6636 | N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1))) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6637 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0.getOperand(0), |
| 6638 | DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
| Dale Johannesen | 400dc2e | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 6639 | N0.getOperand(1), N1)); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6640 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6641 | return SDValue(); |
| Chris Lattner | 6f3b577 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6642 | } |
| 6643 | |
| Owen Anderson | 41b0665 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 6644 | SDValue DAGCombiner::visitFMA(SDNode *N) { |
| 6645 | SDValue N0 = N->getOperand(0); |
| 6646 | SDValue N1 = N->getOperand(1); |
| 6647 | SDValue N2 = N->getOperand(2); |
| 6648 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 6649 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
| 6650 | EVT VT = N->getValueType(0); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6651 | SDLoc dl(N); |
| Owen Anderson | 41b0665 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 6652 | |
| Owen Anderson | b351c8d | 2012-11-01 02:00:53 +0000 | [diff] [blame] | 6653 | if (DAG.getTarget().Options.UnsafeFPMath) { |
| 6654 | if (N0CFP && N0CFP->isZero()) |
| 6655 | return N2; |
| 6656 | if (N1CFP && N1CFP->isZero()) |
| 6657 | return N2; |
| 6658 | } |
| Owen Anderson | 41b0665 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 6659 | if (N0CFP && N0CFP->isExactlyValue(1.0)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6660 | return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N2); |
| Owen Anderson | 41b0665 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 6661 | if (N1CFP && N1CFP->isExactlyValue(1.0)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6662 | return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N2); |
| Owen Anderson | 41b0665 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 6663 | |
| Owen Anderson | c7aaf52 | 2012-05-30 18:50:39 +0000 | [diff] [blame] | 6664 | // Canonicalize (fma c, x, y) -> (fma x, c, y) |
| Owen Anderson | 0eda3e1 | 2012-05-30 18:54:50 +0000 | [diff] [blame] | 6665 | if (N0CFP && !N1CFP) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6666 | return DAG.getNode(ISD::FMA, SDLoc(N), VT, N1, N0, N2); |
| Owen Anderson | c7aaf52 | 2012-05-30 18:50:39 +0000 | [diff] [blame] | 6667 | |
| Owen Anderson | 90e0eaf | 2012-09-01 06:04:27 +0000 | [diff] [blame] | 6668 | // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2) |
| 6669 | if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && |
| 6670 | N2.getOpcode() == ISD::FMUL && |
| 6671 | N0 == N2.getOperand(0) && |
| 6672 | N2.getOperand(1).getOpcode() == ISD::ConstantFP) { |
| 6673 | return DAG.getNode(ISD::FMUL, dl, VT, N0, |
| 6674 | DAG.getNode(ISD::FADD, dl, VT, N1, N2.getOperand(1))); |
| 6675 | } |
| 6676 | |
| 6677 | |
| 6678 | // (fma (fmul x, c1), c2, y) -> (fma x, c1*c2, y) |
| 6679 | if (DAG.getTarget().Options.UnsafeFPMath && |
| 6680 | N0.getOpcode() == ISD::FMUL && N1CFP && |
| 6681 | N0.getOperand(1).getOpcode() == ISD::ConstantFP) { |
| 6682 | return DAG.getNode(ISD::FMA, dl, VT, |
| 6683 | N0.getOperand(0), |
| 6684 | DAG.getNode(ISD::FMUL, dl, VT, N1, N0.getOperand(1)), |
| 6685 | N2); |
| 6686 | } |
| 6687 | |
| 6688 | // (fma x, 1, y) -> (fadd x, y) |
| 6689 | // (fma x, -1, y) -> (fadd (fneg x), y) |
| 6690 | if (N1CFP) { |
| 6691 | if (N1CFP->isExactlyValue(1.0)) |
| 6692 | return DAG.getNode(ISD::FADD, dl, VT, N0, N2); |
| 6693 | |
| 6694 | if (N1CFP->isExactlyValue(-1.0) && |
| 6695 | (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))) { |
| 6696 | SDValue RHSNeg = DAG.getNode(ISD::FNEG, dl, VT, N0); |
| 6697 | AddToWorkList(RHSNeg.getNode()); |
| 6698 | return DAG.getNode(ISD::FADD, dl, VT, N2, RHSNeg); |
| 6699 | } |
| 6700 | } |
| 6701 | |
| 6702 | // (fma x, c, x) -> (fmul x, (c+1)) |
| Stephen Lin | 8e8424e | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6703 | if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && N0 == N2) |
| 6704 | return DAG.getNode(ISD::FMUL, dl, VT, N0, |
| Owen Anderson | 90e0eaf | 2012-09-01 06:04:27 +0000 | [diff] [blame] | 6705 | DAG.getNode(ISD::FADD, dl, VT, |
| 6706 | N1, DAG.getConstantFP(1.0, VT))); |
| Owen Anderson | 90e0eaf | 2012-09-01 06:04:27 +0000 | [diff] [blame] | 6707 | |
| 6708 | // (fma x, c, (fneg x)) -> (fmul x, (c-1)) |
| 6709 | if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && |
| Stephen Lin | 8e8424e | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6710 | N2.getOpcode() == ISD::FNEG && N2.getOperand(0) == N0) |
| 6711 | return DAG.getNode(ISD::FMUL, dl, VT, N0, |
| Owen Anderson | 90e0eaf | 2012-09-01 06:04:27 +0000 | [diff] [blame] | 6712 | DAG.getNode(ISD::FADD, dl, VT, |
| 6713 | N1, DAG.getConstantFP(-1.0, VT))); |
| Owen Anderson | 90e0eaf | 2012-09-01 06:04:27 +0000 | [diff] [blame] | 6714 | |
| 6715 | |
| Owen Anderson | 41b0665 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 6716 | return SDValue(); |
| 6717 | } |
| 6718 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6719 | SDValue DAGCombiner::visitFDIV(SDNode *N) { |
| 6720 | SDValue N0 = N->getOperand(0); |
| 6721 | SDValue N1 = N->getOperand(1); |
| Nate Begeman | 569c439 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6722 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 6723 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6724 | EVT VT = N->getValueType(0); |
| Owen Anderson | 2ee7c4d | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6725 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| Chris Lattner | 6f3b577 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6726 | |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6727 | // fold vector ops |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6728 | if (VT.isVector()) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6729 | SDValue FoldedVOp = SimplifyVBinOp(N); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6730 | if (FoldedVOp.getNode()) return FoldedVOp; |
| Dan Gohman | 80f9f07 | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 6731 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6732 | |
| Nate Begeman | 569c439 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6733 | // fold (fdiv c1, c2) -> c1/c2 |
| Ulrich Weigand | 3abb343 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6734 | if (N0CFP && N1CFP) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6735 | return DAG.getNode(ISD::FDIV, SDLoc(N), VT, N0, N1); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6736 | |
| Duncan Sands | 2f1dc38 | 2012-04-08 18:08:12 +0000 | [diff] [blame] | 6737 | // fold (fdiv X, c2) -> fmul X, 1/c2 if losing precision is acceptable. |
| Ulrich Weigand | 3abb343 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6738 | if (N1CFP && DAG.getTarget().Options.UnsafeFPMath) { |
| Duncan Sands | 5f8397a | 2012-04-07 20:04:00 +0000 | [diff] [blame] | 6739 | // Compute the reciprocal 1.0 / c2. |
| 6740 | APFloat N1APF = N1CFP->getValueAPF(); |
| 6741 | APFloat Recip(N1APF.getSemantics(), 1); // 1.0 |
| 6742 | APFloat::opStatus st = Recip.divide(N1APF, APFloat::rmNearestTiesToEven); |
| Duncan Sands | 4f53074 | 2012-04-10 20:35:27 +0000 | [diff] [blame] | 6743 | // Only do the transform if the reciprocal is a legal fp immediate that |
| 6744 | // isn't too nasty (eg NaN, denormal, ...). |
| 6745 | if ((st == APFloat::opOK || st == APFloat::opInexact) && // Not too nasty |
| Anton Korobeynikov | 4d1220d | 2012-04-10 13:22:49 +0000 | [diff] [blame] | 6746 | (!LegalOperations || |
| 6747 | // FIXME: custom lowering of ConstantFP might fail (see e.g. ARM |
| 6748 | // backend)... we should handle this gracefully after Legalize. |
| 6749 | // TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT) || |
| 6750 | TLI.isOperationLegal(llvm::ISD::ConstantFP, VT) || |
| 6751 | TLI.isFPImmLegal(Recip, VT))) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6752 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0, |
| Duncan Sands | 5f8397a | 2012-04-07 20:04:00 +0000 | [diff] [blame] | 6753 | DAG.getConstantFP(Recip, VT)); |
| 6754 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6755 | |
| Bill Wendling | 3dc5d24 | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 6756 | // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y) |
| Owen Anderson | 2ee7c4d | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6757 | if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI, |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6758 | &DAG.getTarget().Options)) { |
| Owen Anderson | 2ee7c4d | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6759 | if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI, |
| Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6760 | &DAG.getTarget().Options)) { |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 6761 | // Both can be negated for free, check to see if at least one is cheaper |
| 6762 | // negated. |
| 6763 | if (LHSNeg == 2 || RHSNeg == 2) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6764 | return DAG.getNode(ISD::FDIV, SDLoc(N), VT, |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6765 | GetNegatedExpression(N0, DAG, LegalOperations), |
| 6766 | GetNegatedExpression(N1, DAG, LegalOperations)); |
| Chris Lattner | e49c974 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 6767 | } |
| 6768 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6769 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6770 | return SDValue(); |
| Chris Lattner | 6f3b577 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6771 | } |
| 6772 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6773 | SDValue DAGCombiner::visitFREM(SDNode *N) { |
| 6774 | SDValue N0 = N->getOperand(0); |
| 6775 | SDValue N1 = N->getOperand(1); |
| Nate Begeman | 569c439 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6776 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 6777 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6778 | EVT VT = N->getValueType(0); |
| Chris Lattner | 6f3b577 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6779 | |
| Nate Begeman | 569c439 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6780 | // fold (frem c1, c2) -> fmod(c1,c2) |
| Ulrich Weigand | 3abb343 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6781 | if (N0CFP && N1CFP) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6782 | return DAG.getNode(ISD::FREM, SDLoc(N), VT, N0, N1); |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6783 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6784 | return SDValue(); |
| Chris Lattner | 6f3b577 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6785 | } |
| 6786 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6787 | SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) { |
| 6788 | SDValue N0 = N->getOperand(0); |
| 6789 | SDValue N1 = N->getOperand(1); |
| Chris Lattner | 3bc4050 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6790 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 6791 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6792 | EVT VT = N->getValueType(0); |
| Chris Lattner | 3bc4050 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6793 | |
| Ulrich Weigand | 3abb343 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6794 | if (N0CFP && N1CFP) // Constant fold |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6795 | return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, N0, N1); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6796 | |
| Chris Lattner | 3bc4050 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6797 | if (N1CFP) { |
| Dale Johannesen | b6d2bec | 2007-08-26 01:18:27 +0000 | [diff] [blame] | 6798 | const APFloat& V = N1CFP->getValueAPF(); |
| Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 6799 | // copysign(x, c1) -> fabs(x) iff ispos(c1) |
| 6800 | // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1) |
| Dan Gohman | 1f3411d | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 6801 | if (!V.isNegative()) { |
| 6802 | if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6803 | return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0); |
| Dan Gohman | 1f3411d | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 6804 | } else { |
| 6805 | if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6806 | return DAG.getNode(ISD::FNEG, SDLoc(N), VT, |
| 6807 | DAG.getNode(ISD::FABS, SDLoc(N0), VT, N0)); |
| Dan Gohman | 1f3411d | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 6808 | } |
| Chris Lattner | 3bc4050 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6809 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6810 | |
| Chris Lattner | 3bc4050 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6811 | // copysign(fabs(x), y) -> copysign(x, y) |
| 6812 | // copysign(fneg(x), y) -> copysign(x, y) |
| 6813 | // copysign(copysign(x,z), y) -> copysign(x, y) |
| 6814 | if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG || |
| 6815 | N0.getOpcode() == ISD::FCOPYSIGN) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6816 | return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, |
| Bill Wendling | 0bd2974 | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6817 | N0.getOperand(0), N1); |
| Chris Lattner | 3bc4050 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6818 | |
| 6819 | // copysign(x, abs(y)) -> abs(x) |
| 6820 | if (N1.getOpcode() == ISD::FABS) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6821 | return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6822 | |
| Chris Lattner | 3bc4050 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6823 | // copysign(x, copysign(y,z)) -> copysign(x, z) |
| 6824 | if (N1.getOpcode() == ISD::FCOPYSIGN) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6825 | return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, |
| Bill Wendling | 0bd2974 | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6826 | N0, N1.getOperand(1)); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6827 | |
| Chris Lattner | 3bc4050 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6828 | // copysign(x, fp_extend(y)) -> copysign(x, y) |
| 6829 | // copysign(x, fp_round(y)) -> copysign(x, y) |
| 6830 | if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6831 | return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, |
| Bill Wendling | 0bd2974 | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6832 | N0, N1.getOperand(0)); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6833 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6834 | return SDValue(); |
| Chris Lattner | 3bc4050 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6835 | } |
| 6836 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6837 | SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) { |
| 6838 | SDValue N0 = N->getOperand(0); |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 6839 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6840 | EVT VT = N->getValueType(0); |
| 6841 | EVT OpVT = N0.getValueType(); |
| Chris Lattner | b1e66ce | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 6842 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6843 | // fold (sint_to_fp c1) -> c1fp |
| Ulrich Weigand | 3abb343 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6844 | if (N0C && |
| Stuart Hastings | 6b4007d | 2011-03-02 19:36:30 +0000 | [diff] [blame] | 6845 | // ...but only if the target supports immediate floating-point values |
| Eli Friedman | 9d448e4 | 2011-11-12 00:35:34 +0000 | [diff] [blame] | 6846 | (!LegalOperations || |
| Evan Cheng | 4c0bd96 | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 6847 | TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6848 | return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6849 | |
| Chris Lattner | b1e66ce | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 6850 | // If the input is a legal type, and SINT_TO_FP is not legal on this target, |
| 6851 | // but UINT_TO_FP is legal on this target, try to convert. |
| Dan Gohman | 4aa1846 | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 6852 | if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) && |
| 6853 | TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) { |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6854 | // If the sign bit is known to be zero, we can change this to UINT_TO_FP. |
| Chris Lattner | b1e66ce | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 6855 | if (DAG.SignBitIsZero(N0)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6856 | return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0); |
| Chris Lattner | b1e66ce | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 6857 | } |
| Bill Wendling | 0bd2974 | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6858 | |
| Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 6859 | // The next optimizations are desirable only if SELECT_CC can be lowered. |
| Nadav Rotem | 9056076 | 2012-07-23 07:59:50 +0000 | [diff] [blame] | 6860 | // Check against MVT::Other for SELECT_CC, which is a workaround for targets |
| 6861 | // having to say they don't support SELECT_CC on every type the DAG knows |
| 6862 | // about, since there is no way to mark an opcode illegal at all value types |
| 6863 | // (See also visitSELECT) |
| 6864 | if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) { |
| 6865 | // fold (sint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc) |
| 6866 | if (N0.getOpcode() == ISD::SETCC && N0.getValueType() == MVT::i1 && |
| 6867 | !VT.isVector() && |
| 6868 | (!LegalOperations || |
| 6869 | TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) { |
| 6870 | SDValue Ops[] = |
| 6871 | { N0.getOperand(0), N0.getOperand(1), |
| 6872 | DAG.getConstantFP(-1.0, VT) , DAG.getConstantFP(0.0, VT), |
| 6873 | N0.getOperand(2) }; |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6874 | return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5); |
| Nadav Rotem | 9056076 | 2012-07-23 07:59:50 +0000 | [diff] [blame] | 6875 | } |
| Owen Anderson | d4b841f | 2012-07-09 20:31:12 +0000 | [diff] [blame] | 6876 | |
| Nadav Rotem | 9056076 | 2012-07-23 07:59:50 +0000 | [diff] [blame] | 6877 | // fold (sint_to_fp (zext (setcc x, y, cc))) -> |
| 6878 | // (select_cc x, y, 1.0, 0.0,, cc) |
| 6879 | if (N0.getOpcode() == ISD::ZERO_EXTEND && |
| 6880 | N0.getOperand(0).getOpcode() == ISD::SETCC &&!VT.isVector() && |
| 6881 | (!LegalOperations || |
| 6882 | TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) { |
| 6883 | SDValue Ops[] = |
| 6884 | { N0.getOperand(0).getOperand(0), N0.getOperand(0).getOperand(1), |
| 6885 | DAG.getConstantFP(1.0, VT) , DAG.getConstantFP(0.0, VT), |
| 6886 | N0.getOperand(0).getOperand(2) }; |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6887 | return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5); |
| Nadav Rotem | 9056076 | 2012-07-23 07:59:50 +0000 | [diff] [blame] | 6888 | } |
| Owen Anderson | d4b841f | 2012-07-09 20:31:12 +0000 | [diff] [blame] | 6889 | } |
| 6890 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6891 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6892 | } |
| 6893 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6894 | SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) { |
| 6895 | SDValue N0 = N->getOperand(0); |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 6896 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6897 | EVT VT = N->getValueType(0); |
| 6898 | EVT OpVT = N0.getValueType(); |
| Nate Begeman | 569c439 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6899 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6900 | // fold (uint_to_fp c1) -> c1fp |
| Ulrich Weigand | 3abb343 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6901 | if (N0C && |
| Stuart Hastings | 6b4007d | 2011-03-02 19:36:30 +0000 | [diff] [blame] | 6902 | // ...but only if the target supports immediate floating-point values |
| Eli Friedman | 9d448e4 | 2011-11-12 00:35:34 +0000 | [diff] [blame] | 6903 | (!LegalOperations || |
| Evan Cheng | 4c0bd96 | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 6904 | TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6905 | return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6906 | |
| Chris Lattner | b1e66ce | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 6907 | // If the input is a legal type, and UINT_TO_FP is not legal on this target, |
| 6908 | // but SINT_TO_FP is legal on this target, try to convert. |
| Dan Gohman | 4aa1846 | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 6909 | if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) && |
| 6910 | TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) { |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6911 | // If the sign bit is known to be zero, we can change this to SINT_TO_FP. |
| Chris Lattner | b1e66ce | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 6912 | if (DAG.SignBitIsZero(N0)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6913 | return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0); |
| Chris Lattner | b1e66ce | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 6914 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6915 | |
| Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 6916 | // The next optimizations are desirable only if SELECT_CC can be lowered. |
| Nadav Rotem | 9056076 | 2012-07-23 07:59:50 +0000 | [diff] [blame] | 6917 | // Check against MVT::Other for SELECT_CC, which is a workaround for targets |
| 6918 | // having to say they don't support SELECT_CC on every type the DAG knows |
| 6919 | // about, since there is no way to mark an opcode illegal at all value types |
| 6920 | // (See also visitSELECT) |
| 6921 | if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) { |
| 6922 | // fold (uint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc) |
| Owen Anderson | d4b841f | 2012-07-09 20:31:12 +0000 | [diff] [blame] | 6923 | |
| Nadav Rotem | 9056076 | 2012-07-23 07:59:50 +0000 | [diff] [blame] | 6924 | if (N0.getOpcode() == ISD::SETCC && !VT.isVector() && |
| 6925 | (!LegalOperations || |
| 6926 | TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) { |
| 6927 | SDValue Ops[] = |
| 6928 | { N0.getOperand(0), N0.getOperand(1), |
| 6929 | DAG.getConstantFP(1.0, VT), DAG.getConstantFP(0.0, VT), |
| 6930 | N0.getOperand(2) }; |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6931 | return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5); |
| Nadav Rotem | 9056076 | 2012-07-23 07:59:50 +0000 | [diff] [blame] | 6932 | } |
| 6933 | } |
| Owen Anderson | d4b841f | 2012-07-09 20:31:12 +0000 | [diff] [blame] | 6934 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6935 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6936 | } |
| 6937 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6938 | SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) { |
| 6939 | SDValue N0 = N->getOperand(0); |
| Nate Begeman | 569c439 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6940 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6941 | EVT VT = N->getValueType(0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6942 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6943 | // fold (fp_to_sint c1fp) -> c1 |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 6944 | if (N0CFP) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6945 | return DAG.getNode(ISD::FP_TO_SINT, SDLoc(N), VT, N0); |
| Bill Wendling | 0bd2974 | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6946 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6947 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6948 | } |
| 6949 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6950 | SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) { |
| 6951 | SDValue N0 = N->getOperand(0); |
| Nate Begeman | 569c439 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6952 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6953 | EVT VT = N->getValueType(0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6954 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6955 | // fold (fp_to_uint c1fp) -> c1 |
| Ulrich Weigand | 3abb343 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6956 | if (N0CFP) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6957 | return DAG.getNode(ISD::FP_TO_UINT, SDLoc(N), VT, N0); |
| Bill Wendling | 0bd2974 | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6958 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6959 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6960 | } |
| 6961 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6962 | SDValue DAGCombiner::visitFP_ROUND(SDNode *N) { |
| 6963 | SDValue N0 = N->getOperand(0); |
| 6964 | SDValue N1 = N->getOperand(1); |
| Nate Begeman | 569c439 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6965 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6966 | EVT VT = N->getValueType(0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6967 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6968 | // fold (fp_round c1fp) -> c1fp |
| Ulrich Weigand | 3abb343 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6969 | if (N0CFP) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6970 | return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0, N1); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6971 | |
| Chris Lattner | 8bb6cb7 | 2006-03-13 06:26:26 +0000 | [diff] [blame] | 6972 | // fold (fp_round (fp_extend x)) -> x |
| 6973 | if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType()) |
| 6974 | return N0.getOperand(0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6975 | |
| Chris Lattner | 0feb1b0 | 2008-01-24 06:45:35 +0000 | [diff] [blame] | 6976 | // fold (fp_round (fp_round x)) -> (fp_round x) |
| 6977 | if (N0.getOpcode() == ISD::FP_ROUND) { |
| 6978 | // This is a value preserving truncation if both round's are. |
| 6979 | bool IsTrunc = N->getConstantOperandVal(1) == 1 && |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6980 | N0.getNode()->getConstantOperandVal(1) == 1; |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6981 | return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0.getOperand(0), |
| Chris Lattner | 0feb1b0 | 2008-01-24 06:45:35 +0000 | [diff] [blame] | 6982 | DAG.getIntPtrConstant(IsTrunc)); |
| 6983 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6984 | |
| Chris Lattner | 8bb6cb7 | 2006-03-13 06:26:26 +0000 | [diff] [blame] | 6985 | // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y) |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6986 | if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6987 | SDValue Tmp = DAG.getNode(ISD::FP_ROUND, SDLoc(N0), VT, |
| Bill Wendling | 0bd2974 | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6988 | N0.getOperand(0), N1); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6989 | AddToWorkList(Tmp.getNode()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6990 | return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, |
| Bill Wendling | 0bd2974 | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6991 | Tmp, N0.getOperand(1)); |
| Chris Lattner | 8bb6cb7 | 2006-03-13 06:26:26 +0000 | [diff] [blame] | 6992 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6993 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6994 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6995 | } |
| 6996 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6997 | SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) { |
| 6998 | SDValue N0 = N->getOperand(0); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6999 | EVT VT = N->getValueType(0); |
| 7000 | EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT(); |
| Nate Begeman | 7cea6ef | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 7001 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7002 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 7003 | // fold (fp_round_inreg c1fp) -> c1fp |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 7004 | if (N0CFP && isTypeLegal(EVT)) { |
| Dan Gohman | ec270fb | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 7005 | SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7006 | return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Round); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 7007 | } |
| Bill Wendling | 0bd2974 | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 7008 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7009 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 7010 | } |
| 7011 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7012 | SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) { |
| 7013 | SDValue N0 = N->getOperand(0); |
| Nate Begeman | 569c439 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 7014 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7015 | EVT VT = N->getValueType(0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7016 | |
| Chris Lattner | 5919b48 | 2007-12-29 06:55:23 +0000 | [diff] [blame] | 7017 | // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7018 | if (N->hasOneUse() && |
| Dan Gohman | 8e4ac9b | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 7019 | N->use_begin()->getOpcode() == ISD::FP_ROUND) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7020 | return SDValue(); |
| Chris Lattner | 72733e5 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 7021 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 7022 | // fold (fp_extend c1fp) -> c1fp |
| Ulrich Weigand | 3abb343 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 7023 | if (N0CFP) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7024 | return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, N0); |
| Chris Lattner | 72733e5 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 7025 | |
| 7026 | // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the |
| 7027 | // value of X. |
| Gabor Greif | e12264b | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 7028 | if (N0.getOpcode() == ISD::FP_ROUND |
| 7029 | && N0.getNode()->getConstantOperandVal(1) == 1) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7030 | SDValue In = N0.getOperand(0); |
| Chris Lattner | 72733e5 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 7031 | if (In.getValueType() == VT) return In; |
| Duncan Sands | 11dd424 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 7032 | if (VT.bitsLT(In.getValueType())) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7033 | return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, |
| Bill Wendling | 0bd2974 | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 7034 | In, N0.getOperand(1)); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7035 | return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, In); |
| Chris Lattner | 72733e5 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 7036 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7037 | |
| Chris Lattner | 72733e5 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 7038 | // fold (fpext (load x)) -> (fpext (fptrunc (extload x))) |
| Hal Finkel | dbc7a8a | 2013-10-04 22:18:12 +0000 | [diff] [blame] | 7039 | if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() && |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 7040 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
| Evan Cheng | 07d53b1 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 7041 | TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) { |
| Evan Cheng | e71fe34d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 7042 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7043 | SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT, |
| Bill Wendling | 0bd2974 | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 7044 | LN0->getChain(), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 7045 | LN0->getBasePtr(), N0.getValueType(), |
| 7046 | LN0->getMemOperand()); |
| Chris Lattner | 3d26577 | 2006-05-05 21:34:35 +0000 | [diff] [blame] | 7047 | CombineTo(N, ExtLoad); |
| Bill Wendling | 0bd2974 | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 7048 | CombineTo(N0.getNode(), |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7049 | DAG.getNode(ISD::FP_ROUND, SDLoc(N0), |
| Bill Wendling | 0bd2974 | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 7050 | N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)), |
| Chris Lattner | 3d26577 | 2006-05-05 21:34:35 +0000 | [diff] [blame] | 7051 | ExtLoad.getValue(1)); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7052 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| Chris Lattner | 3d26577 | 2006-05-05 21:34:35 +0000 | [diff] [blame] | 7053 | } |
| Duncan Sands | 8651e9c | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 7054 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7055 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 7056 | } |
| 7057 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7058 | SDValue DAGCombiner::visitFNEG(SDNode *N) { |
| 7059 | SDValue N0 = N->getOperand(0); |
| Anton Korobeynikov | a6faf60 | 2009-10-20 21:37:45 +0000 | [diff] [blame] | 7060 | EVT VT = N->getValueType(0); |
| Nate Begeman | 569c439 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 7061 | |
| Craig Topper | 8238461 | 2012-09-11 01:45:21 +0000 | [diff] [blame] | 7062 | if (VT.isVector()) { |
| 7063 | SDValue FoldedVOp = SimplifyVUnaryOp(N); |
| 7064 | if (FoldedVOp.getNode()) return FoldedVOp; |
| Craig Topper | 03f3977 | 2012-09-09 22:58:45 +0000 | [diff] [blame] | 7065 | } |
| 7066 | |
| Owen Anderson | 2ee7c4d | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 7067 | if (isNegatibleForFree(N0, LegalOperations, DAG.getTargetLoweringInfo(), |
| 7068 | &DAG.getTarget().Options)) |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 7069 | return GetNegatedExpression(N0, DAG, LegalOperations); |
| Dan Gohman | 9a70823 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 7070 | |
| Chris Lattner | 888560d | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 7071 | // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading |
| 7072 | // constant pool values. |
| Owen Anderson | 98f2c0c | 2012-04-02 22:10:29 +0000 | [diff] [blame] | 7073 | if (!TLI.isFNegFree(VT) && N0.getOpcode() == ISD::BITCAST && |
| Anton Korobeynikov | a6faf60 | 2009-10-20 21:37:45 +0000 | [diff] [blame] | 7074 | !VT.isVector() && |
| 7075 | N0.getNode()->hasOneUse() && |
| 7076 | N0.getOperand(0).getValueType().isInteger()) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7077 | SDValue Int = N0.getOperand(0); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7078 | EVT IntVT = Int.getValueType(); |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 7079 | if (IntVT.isInteger() && !IntVT.isVector()) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7080 | Int = DAG.getNode(ISD::XOR, SDLoc(N0), IntVT, Int, |
| Duncan Sands | 3ed7688 | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 7081 | DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT)); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7082 | AddToWorkList(Int.getNode()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7083 | return DAG.getNode(ISD::BITCAST, SDLoc(N), |
| Anton Korobeynikov | a6faf60 | 2009-10-20 21:37:45 +0000 | [diff] [blame] | 7084 | VT, Int); |
| Chris Lattner | 888560d | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 7085 | } |
| 7086 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7087 | |
| Owen Anderson | 90e0eaf | 2012-09-01 06:04:27 +0000 | [diff] [blame] | 7088 | // (fneg (fmul c, x)) -> (fmul -c, x) |
| 7089 | if (N0.getOpcode() == ISD::FMUL) { |
| 7090 | ConstantFPSDNode *CFP1 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1)); |
| Stephen Lin | 8e8424e | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 7091 | if (CFP1) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7092 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
| Owen Anderson | 90e0eaf | 2012-09-01 06:04:27 +0000 | [diff] [blame] | 7093 | N0.getOperand(0), |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7094 | DAG.getNode(ISD::FNEG, SDLoc(N), VT, |
| Owen Anderson | 90e0eaf | 2012-09-01 06:04:27 +0000 | [diff] [blame] | 7095 | N0.getOperand(1))); |
| Owen Anderson | 90e0eaf | 2012-09-01 06:04:27 +0000 | [diff] [blame] | 7096 | } |
| 7097 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7098 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 7099 | } |
| 7100 | |
| Owen Anderson | a40319b | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 7101 | SDValue DAGCombiner::visitFCEIL(SDNode *N) { |
| 7102 | SDValue N0 = N->getOperand(0); |
| 7103 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 7104 | EVT VT = N->getValueType(0); |
| 7105 | |
| 7106 | // fold (fceil c1) -> fceil(c1) |
| Ulrich Weigand | 3abb343 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 7107 | if (N0CFP) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7108 | return DAG.getNode(ISD::FCEIL, SDLoc(N), VT, N0); |
| Owen Anderson | a40319b | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 7109 | |
| 7110 | return SDValue(); |
| 7111 | } |
| 7112 | |
| 7113 | SDValue DAGCombiner::visitFTRUNC(SDNode *N) { |
| 7114 | SDValue N0 = N->getOperand(0); |
| 7115 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 7116 | EVT VT = N->getValueType(0); |
| 7117 | |
| 7118 | // fold (ftrunc c1) -> ftrunc(c1) |
| Ulrich Weigand | 3abb343 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 7119 | if (N0CFP) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7120 | return DAG.getNode(ISD::FTRUNC, SDLoc(N), VT, N0); |
| Owen Anderson | a40319b | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 7121 | |
| 7122 | return SDValue(); |
| 7123 | } |
| 7124 | |
| 7125 | SDValue DAGCombiner::visitFFLOOR(SDNode *N) { |
| 7126 | SDValue N0 = N->getOperand(0); |
| 7127 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 7128 | EVT VT = N->getValueType(0); |
| 7129 | |
| 7130 | // fold (ffloor c1) -> ffloor(c1) |
| Ulrich Weigand | 3abb343 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 7131 | if (N0CFP) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7132 | return DAG.getNode(ISD::FFLOOR, SDLoc(N), VT, N0); |
| Owen Anderson | a40319b | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 7133 | |
| 7134 | return SDValue(); |
| 7135 | } |
| 7136 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7137 | SDValue DAGCombiner::visitFABS(SDNode *N) { |
| 7138 | SDValue N0 = N->getOperand(0); |
| Nate Begeman | 569c439 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 7139 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7140 | EVT VT = N->getValueType(0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7141 | |
| Craig Topper | 8238461 | 2012-09-11 01:45:21 +0000 | [diff] [blame] | 7142 | if (VT.isVector()) { |
| 7143 | SDValue FoldedVOp = SimplifyVUnaryOp(N); |
| 7144 | if (FoldedVOp.getNode()) return FoldedVOp; |
| 7145 | } |
| 7146 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 7147 | // fold (fabs c1) -> fabs(c1) |
| Ulrich Weigand | 3abb343 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 7148 | if (N0CFP) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7149 | return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 7150 | // fold (fabs (fabs x)) -> (fabs x) |
| Chris Lattner | 3bc4050 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 7151 | if (N0.getOpcode() == ISD::FABS) |
| Nate Begeman | d23739d | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 7152 | return N->getOperand(0); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 7153 | // fold (fabs (fneg x)) -> (fabs x) |
| Chris Lattner | 3bc4050 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 7154 | // fold (fabs (fcopysign x, y)) -> (fabs x) |
| 7155 | if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7156 | return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0.getOperand(0)); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7157 | |
| Chris Lattner | 888560d | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 7158 | // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading |
| 7159 | // constant pool values. |
| Stephen Lin | cfe7f35 | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 7160 | if (!TLI.isFAbsFree(VT) && |
| Owen Anderson | 98f2c0c | 2012-04-02 22:10:29 +0000 | [diff] [blame] | 7161 | N0.getOpcode() == ISD::BITCAST && N0.getNode()->hasOneUse() && |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 7162 | N0.getOperand(0).getValueType().isInteger() && |
| 7163 | !N0.getOperand(0).getValueType().isVector()) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7164 | SDValue Int = N0.getOperand(0); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7165 | EVT IntVT = Int.getValueType(); |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 7166 | if (IntVT.isInteger() && !IntVT.isVector()) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7167 | Int = DAG.getNode(ISD::AND, SDLoc(N0), IntVT, Int, |
| Duncan Sands | 3ed7688 | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 7168 | DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT)); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7169 | AddToWorkList(Int.getNode()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7170 | return DAG.getNode(ISD::BITCAST, SDLoc(N), |
| Bill Wendling | 306bfc2 | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7171 | N->getValueType(0), Int); |
| Chris Lattner | 888560d | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 7172 | } |
| 7173 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7174 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7175 | return SDValue(); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 7176 | } |
| 7177 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7178 | SDValue DAGCombiner::visitBRCOND(SDNode *N) { |
| 7179 | SDValue Chain = N->getOperand(0); |
| 7180 | SDValue N1 = N->getOperand(1); |
| 7181 | SDValue N2 = N->getOperand(2); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7182 | |
| Dan Gohman | 82e8001 | 2009-11-17 00:47:23 +0000 | [diff] [blame] | 7183 | // If N is a constant we could fold this into a fallthrough or unconditional |
| 7184 | // branch. However that doesn't happen very often in normal code, because |
| 7185 | // Instcombine/SimplifyCFG should have handled the available opportunities. |
| 7186 | // If we did this folding here, it would be necessary to update the |
| 7187 | // MachineBasicBlock CFG, which is awkward. |
| 7188 | |
| Nate Begeman | 7e7f439 | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 7189 | // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal |
| 7190 | // on the target. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7191 | if (N1.getOpcode() == ISD::SETCC && |
| Tom Stellard | b1588fc | 2013-03-08 15:36:57 +0000 | [diff] [blame] | 7192 | TLI.isOperationLegalOrCustom(ISD::BR_CC, |
| 7193 | N1.getOperand(0).getValueType())) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7194 | return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other, |
| Bill Wendling | 306bfc2 | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7195 | Chain, N1.getOperand(2), |
| Nate Begeman | 7e7f439 | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 7196 | N1.getOperand(0), N1.getOperand(1), N2); |
| 7197 | } |
| Bill Wendling | 306bfc2 | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7198 | |
| Evan Cheng | c8d6cfd | 2010-10-04 22:41:01 +0000 | [diff] [blame] | 7199 | if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) || |
| 7200 | ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) && |
| 7201 | (N1.getOperand(0).hasOneUse() && |
| 7202 | N1.getOperand(0).getOpcode() == ISD::SRL))) { |
| 7203 | SDNode *Trunc = 0; |
| 7204 | if (N1.getOpcode() == ISD::TRUNCATE) { |
| 7205 | // Look pass the truncate. |
| 7206 | Trunc = N1.getNode(); |
| 7207 | N1 = N1.getOperand(0); |
| 7208 | } |
| Evan Cheng | 166a4e6 | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 7209 | |
| Bill Wendling | aa28be6 | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 7210 | // Match this pattern so that we can generate simpler code: |
| 7211 | // |
| 7212 | // %a = ... |
| 7213 | // %b = and i32 %a, 2 |
| 7214 | // %c = srl i32 %b, 1 |
| 7215 | // brcond i32 %c ... |
| 7216 | // |
| 7217 | // into |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7218 | // |
| Bill Wendling | aa28be6 | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 7219 | // %a = ... |
| Evan Cheng | 166a4e6 | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 7220 | // %b = and i32 %a, 2 |
| Bill Wendling | aa28be6 | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 7221 | // %c = setcc eq %b, 0 |
| 7222 | // brcond %c ... |
| 7223 | // |
| 7224 | // This applies only when the AND constant value has one bit set and the |
| 7225 | // SRL constant is equal to the log2 of the AND constant. The back-end is |
| 7226 | // smart enough to convert the result into a TEST/JMP sequence. |
| 7227 | SDValue Op0 = N1.getOperand(0); |
| 7228 | SDValue Op1 = N1.getOperand(1); |
| 7229 | |
| 7230 | if (Op0.getOpcode() == ISD::AND && |
| Bill Wendling | aa28be6 | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 7231 | Op1.getOpcode() == ISD::Constant) { |
| Bill Wendling | aa28be6 | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 7232 | SDValue AndOp1 = Op0.getOperand(1); |
| 7233 | |
| 7234 | if (AndOp1.getOpcode() == ISD::Constant) { |
| 7235 | const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue(); |
| 7236 | |
| 7237 | if (AndConst.isPowerOf2() && |
| 7238 | cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) { |
| 7239 | SDValue SetCC = |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7240 | DAG.getSetCC(SDLoc(N), |
| Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 7241 | getSetCCResultType(Op0.getValueType()), |
| Bill Wendling | aa28be6 | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 7242 | Op0, DAG.getConstant(0, Op0.getValueType()), |
| 7243 | ISD::SETNE); |
| 7244 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7245 | SDValue NewBRCond = DAG.getNode(ISD::BRCOND, SDLoc(N), |
| Evan Cheng | 166a4e6 | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 7246 | MVT::Other, Chain, SetCC, N2); |
| 7247 | // Don't add the new BRCond into the worklist or else SimplifySelectCC |
| 7248 | // will convert it back to (X & C1) >> C2. |
| 7249 | CombineTo(N, NewBRCond, false); |
| 7250 | // Truncate is dead. |
| 7251 | if (Trunc) { |
| 7252 | removeFromWorkList(Trunc); |
| 7253 | DAG.DeleteNode(Trunc); |
| 7254 | } |
| Bill Wendling | aa28be6 | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 7255 | // Replace the uses of SRL with SETCC |
| Evan Cheng | 228c31f | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 7256 | WorkListRemover DeadNodes(*this); |
| Jakob Stoklund Olesen | beb9469 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7257 | DAG.ReplaceAllUsesOfValueWith(N1, SetCC); |
| Bill Wendling | aa28be6 | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 7258 | removeFromWorkList(N1.getNode()); |
| 7259 | DAG.DeleteNode(N1.getNode()); |
| Evan Cheng | 166a4e6 | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 7260 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| Bill Wendling | aa28be6 | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 7261 | } |
| 7262 | } |
| 7263 | } |
| Evan Cheng | c8d6cfd | 2010-10-04 22:41:01 +0000 | [diff] [blame] | 7264 | |
| 7265 | if (Trunc) |
| 7266 | // Restore N1 if the above transformation doesn't match. |
| 7267 | N1 = N->getOperand(1); |
| Bill Wendling | aa28be6 | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 7268 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7269 | |
| Evan Cheng | 228c31f | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 7270 | // Transform br(xor(x, y)) -> br(x != y) |
| 7271 | // Transform br(xor(xor(x,y), 1)) -> br (x == y) |
| 7272 | if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) { |
| 7273 | SDNode *TheXor = N1.getNode(); |
| 7274 | SDValue Op0 = TheXor->getOperand(0); |
| 7275 | SDValue Op1 = TheXor->getOperand(1); |
| 7276 | if (Op0.getOpcode() == Op1.getOpcode()) { |
| 7277 | // Avoid missing important xor optimizations. |
| 7278 | SDValue Tmp = visitXOR(TheXor); |
| Evan Cheng | 5652a8d | 2013-01-09 20:56:40 +0000 | [diff] [blame] | 7279 | if (Tmp.getNode()) { |
| 7280 | if (Tmp.getNode() != TheXor) { |
| 7281 | DEBUG(dbgs() << "\nReplacing.8 "; |
| 7282 | TheXor->dump(&DAG); |
| 7283 | dbgs() << "\nWith: "; |
| 7284 | Tmp.getNode()->dump(&DAG); |
| 7285 | dbgs() << '\n'); |
| 7286 | WorkListRemover DeadNodes(*this); |
| 7287 | DAG.ReplaceAllUsesOfValueWith(N1, Tmp); |
| 7288 | removeFromWorkList(TheXor); |
| 7289 | DAG.DeleteNode(TheXor); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7290 | return DAG.getNode(ISD::BRCOND, SDLoc(N), |
| Evan Cheng | 5652a8d | 2013-01-09 20:56:40 +0000 | [diff] [blame] | 7291 | MVT::Other, Chain, Tmp, N2); |
| 7292 | } |
| 7293 | |
| Benjamin Kramer | 9335443 | 2013-03-30 21:28:18 +0000 | [diff] [blame] | 7294 | // visitXOR has changed XOR's operands or replaced the XOR completely, |
| 7295 | // bail out. |
| 7296 | return SDValue(N, 0); |
| Evan Cheng | 228c31f | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 7297 | } |
| 7298 | } |
| 7299 | |
| 7300 | if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) { |
| 7301 | bool Equal = false; |
| 7302 | if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0)) |
| 7303 | if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() && |
| 7304 | Op0.getOpcode() == ISD::XOR) { |
| 7305 | TheXor = Op0.getNode(); |
| 7306 | Equal = true; |
| 7307 | } |
| 7308 | |
| Evan Cheng | c8d6cfd | 2010-10-04 22:41:01 +0000 | [diff] [blame] | 7309 | EVT SetCCVT = N1.getValueType(); |
| Evan Cheng | 228c31f | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 7310 | if (LegalTypes) |
| Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 7311 | SetCCVT = getSetCCResultType(SetCCVT); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7312 | SDValue SetCC = DAG.getSetCC(SDLoc(TheXor), |
| Evan Cheng | 228c31f | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 7313 | SetCCVT, |
| 7314 | Op0, Op1, |
| 7315 | Equal ? ISD::SETEQ : ISD::SETNE); |
| 7316 | // Replace the uses of XOR with SETCC |
| 7317 | WorkListRemover DeadNodes(*this); |
| Jakob Stoklund Olesen | beb9469 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7318 | DAG.ReplaceAllUsesOfValueWith(N1, SetCC); |
| Evan Cheng | c8d6cfd | 2010-10-04 22:41:01 +0000 | [diff] [blame] | 7319 | removeFromWorkList(N1.getNode()); |
| 7320 | DAG.DeleteNode(N1.getNode()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7321 | return DAG.getNode(ISD::BRCOND, SDLoc(N), |
| Evan Cheng | 228c31f | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 7322 | MVT::Other, Chain, SetCC, N2); |
| 7323 | } |
| 7324 | } |
| Bill Wendling | aa28be6 | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 7325 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7326 | return SDValue(); |
| Nate Begeman | c760f80 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 7327 | } |
| 7328 | |
| Chris Lattner | a49e16f | 2005-10-05 06:47:48 +0000 | [diff] [blame] | 7329 | // Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB. |
| 7330 | // |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7331 | SDValue DAGCombiner::visitBR_CC(SDNode *N) { |
| Chris Lattner | a49e16f | 2005-10-05 06:47:48 +0000 | [diff] [blame] | 7332 | CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1)); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7333 | SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7334 | |
| Dan Gohman | 82e8001 | 2009-11-17 00:47:23 +0000 | [diff] [blame] | 7335 | // If N is a constant we could fold this into a fallthrough or unconditional |
| 7336 | // branch. However that doesn't happen very often in normal code, because |
| 7337 | // Instcombine/SimplifyCFG should have handled the available opportunities. |
| 7338 | // If we did this folding here, it would be necessary to update the |
| 7339 | // MachineBasicBlock CFG, which is awkward. |
| 7340 | |
| Duncan Sands | 93b6609 | 2008-06-09 11:32:28 +0000 | [diff] [blame] | 7341 | // Use SimplifySetCC to simplify SETCC's. |
| Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 7342 | SDValue Simp = SimplifySetCC(getSetCCResultType(CondLHS.getValueType()), |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7343 | CondLHS, CondRHS, CC->get(), SDLoc(N), |
| Dale Johannesen | f1163e9 | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 7344 | false); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7345 | if (Simp.getNode()) AddToWorkList(Simp.getNode()); |
| Chris Lattner | 6a1b2de | 2006-10-14 03:52:46 +0000 | [diff] [blame] | 7346 | |
| Nate Begeman | bd7df03 | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 7347 | // fold to a simpler setcc |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7348 | if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7349 | return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other, |
| Bill Wendling | 306bfc2 | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7350 | N->getOperand(0), Simp.getOperand(2), |
| 7351 | Simp.getOperand(0), Simp.getOperand(1), |
| 7352 | N->getOperand(4)); |
| 7353 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7354 | return SDValue(); |
| Nate Begeman | c760f80 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 7355 | } |
| 7356 | |
| Evan Cheng | fa83263 | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 7357 | /// canFoldInAddressingMode - Return true if 'Use' is a load or a store that |
| 7358 | /// uses N as its base pointer and that N may be folded in the load / store |
| Evan Cheng | 80893ce | 2012-03-06 23:33:32 +0000 | [diff] [blame] | 7359 | /// addressing mode. |
| Evan Cheng | fa83263 | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 7360 | static bool canFoldInAddressingMode(SDNode *N, SDNode *Use, |
| 7361 | SelectionDAG &DAG, |
| 7362 | const TargetLowering &TLI) { |
| 7363 | EVT VT; |
| 7364 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) { |
| 7365 | if (LD->isIndexed() || LD->getBasePtr().getNode() != N) |
| 7366 | return false; |
| 7367 | VT = Use->getValueType(0); |
| 7368 | } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) { |
| 7369 | if (ST->isIndexed() || ST->getBasePtr().getNode() != N) |
| 7370 | return false; |
| 7371 | VT = ST->getValue().getValueType(); |
| 7372 | } else |
| 7373 | return false; |
| 7374 | |
| Chandler Carruth | 95f83e0 | 2013-01-07 15:14:13 +0000 | [diff] [blame] | 7375 | TargetLowering::AddrMode AM; |
| Evan Cheng | fa83263 | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 7376 | if (N->getOpcode() == ISD::ADD) { |
| 7377 | ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1)); |
| 7378 | if (Offset) |
| Evan Cheng | 80893ce | 2012-03-06 23:33:32 +0000 | [diff] [blame] | 7379 | // [reg +/- imm] |
| Evan Cheng | fa83263 | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 7380 | AM.BaseOffs = Offset->getSExtValue(); |
| 7381 | else |
| Evan Cheng | 80893ce | 2012-03-06 23:33:32 +0000 | [diff] [blame] | 7382 | // [reg +/- reg] |
| 7383 | AM.Scale = 1; |
| Evan Cheng | fa83263 | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 7384 | } else if (N->getOpcode() == ISD::SUB) { |
| 7385 | ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1)); |
| 7386 | if (Offset) |
| Evan Cheng | 80893ce | 2012-03-06 23:33:32 +0000 | [diff] [blame] | 7387 | // [reg +/- imm] |
| Evan Cheng | fa83263 | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 7388 | AM.BaseOffs = -Offset->getSExtValue(); |
| 7389 | else |
| Evan Cheng | 80893ce | 2012-03-06 23:33:32 +0000 | [diff] [blame] | 7390 | // [reg +/- reg] |
| 7391 | AM.Scale = 1; |
| Evan Cheng | fa83263 | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 7392 | } else |
| 7393 | return false; |
| 7394 | |
| 7395 | return TLI.isLegalAddressingMode(AM, VT.getTypeForEVT(*DAG.getContext())); |
| 7396 | } |
| 7397 | |
| Duncan Sands | 075293f | 2008-06-15 20:12:31 +0000 | [diff] [blame] | 7398 | /// CombineToPreIndexedLoadStore - Try turning a load / store into a |
| 7399 | /// pre-indexed load / store when the base pointer is an add or subtract |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7400 | /// and it has other uses besides the load / store. After the |
| 7401 | /// transformation, the new indexed load / store has effectively folded |
| 7402 | /// the add / subtract in and all of its other uses are redirected to the |
| 7403 | /// new load / store. |
| 7404 | bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) { |
| Eli Friedman | 9d448e4 | 2011-11-12 00:35:34 +0000 | [diff] [blame] | 7405 | if (Level < AfterLegalizeDAG) |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7406 | return false; |
| 7407 | |
| 7408 | bool isLoad = true; |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7409 | SDValue Ptr; |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7410 | EVT VT; |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7411 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { |
| Chris Lattner | 1ea55cf | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 7412 | if (LD->isIndexed()) |
| Evan Cheng | 28cf427 | 2006-12-16 06:25:23 +0000 | [diff] [blame] | 7413 | return false; |
| Dan Gohman | 47a7d6f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 7414 | VT = LD->getMemoryVT(); |
| Evan Cheng | 8a1d09d | 2007-03-07 08:07:03 +0000 | [diff] [blame] | 7415 | if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) && |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7416 | !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT)) |
| 7417 | return false; |
| 7418 | Ptr = LD->getBasePtr(); |
| 7419 | } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { |
| Chris Lattner | 1ea55cf | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 7420 | if (ST->isIndexed()) |
| Evan Cheng | 28cf427 | 2006-12-16 06:25:23 +0000 | [diff] [blame] | 7421 | return false; |
| Dan Gohman | 47a7d6f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 7422 | VT = ST->getMemoryVT(); |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7423 | if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) && |
| 7424 | !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT)) |
| 7425 | return false; |
| 7426 | Ptr = ST->getBasePtr(); |
| 7427 | isLoad = false; |
| Bill Wendling | 306bfc2 | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7428 | } else { |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7429 | return false; |
| Bill Wendling | 306bfc2 | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7430 | } |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7431 | |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7432 | // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail |
| 7433 | // out. There is no reason to make this a preinc/predec. |
| 7434 | if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) || |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7435 | Ptr.getNode()->hasOneUse()) |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7436 | return false; |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7437 | |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7438 | // Ask the target to do addressing mode selection. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7439 | SDValue BasePtr; |
| 7440 | SDValue Offset; |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7441 | ISD::MemIndexedMode AM = ISD::UNINDEXED; |
| 7442 | if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG)) |
| 7443 | return false; |
| Hal Finkel | 2581905 | 2013-02-08 21:35:47 +0000 | [diff] [blame] | 7444 | |
| 7445 | // Backends without true r+i pre-indexed forms may need to pass a |
| 7446 | // constant base with a variable offset so that constant coercion |
| 7447 | // will work with the patterns in canonical form. |
| 7448 | bool Swapped = false; |
| 7449 | if (isa<ConstantSDNode>(BasePtr)) { |
| 7450 | std::swap(BasePtr, Offset); |
| 7451 | Swapped = true; |
| 7452 | } |
| 7453 | |
| Evan Cheng | 044a0a8 | 2007-05-03 23:52:19 +0000 | [diff] [blame] | 7454 | // Don't create a indexed load / store with zero offset. |
| 7455 | if (isa<ConstantSDNode>(Offset) && |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 7456 | cast<ConstantSDNode>(Offset)->isNullValue()) |
| Evan Cheng | 044a0a8 | 2007-05-03 23:52:19 +0000 | [diff] [blame] | 7457 | return false; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7458 | |
| Chris Lattner | a0a8003 | 2006-11-11 01:00:15 +0000 | [diff] [blame] | 7459 | // Try turning it into a pre-indexed load / store except when: |
| Evan Cheng | a4d187b | 2007-05-24 02:35:39 +0000 | [diff] [blame] | 7460 | // 1) The new base ptr is a frame index. |
| 7461 | // 2) If N is a store and the new base ptr is either the same as or is a |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7462 | // predecessor of the value being stored. |
| Evan Cheng | a4d187b | 2007-05-24 02:35:39 +0000 | [diff] [blame] | 7463 | // 3) Another use of old base ptr is a predecessor of N. If ptr is folded |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7464 | // that would create a cycle. |
| Evan Cheng | a4d187b | 2007-05-24 02:35:39 +0000 | [diff] [blame] | 7465 | // 4) All uses are load / store ops that use it as old base ptr. |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7466 | |
| Chris Lattner | a0a8003 | 2006-11-11 01:00:15 +0000 | [diff] [blame] | 7467 | // Check #1. Preinc'ing a frame index would require copying the stack pointer |
| 7468 | // (plus the implicit offset) to a register to preinc anyway. |
| Evan Cheng | cfc0513 | 2009-05-06 18:25:01 +0000 | [diff] [blame] | 7469 | if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr)) |
| Chris Lattner | a0a8003 | 2006-11-11 01:00:15 +0000 | [diff] [blame] | 7470 | return false; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7471 | |
| Chris Lattner | a0a8003 | 2006-11-11 01:00:15 +0000 | [diff] [blame] | 7472 | // Check #2. |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7473 | if (!isLoad) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7474 | SDValue Val = cast<StoreSDNode>(N)->getValue(); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7475 | if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode())) |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7476 | return false; |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7477 | } |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7478 | |
| Hal Finkel | 2581905 | 2013-02-08 21:35:47 +0000 | [diff] [blame] | 7479 | // If the offset is a constant, there may be other adds of constants that |
| 7480 | // can be folded with this one. We should do this to avoid having to keep |
| 7481 | // a copy of the original base pointer. |
| 7482 | SmallVector<SDNode *, 16> OtherUses; |
| 7483 | if (isa<ConstantSDNode>(Offset)) |
| 7484 | for (SDNode::use_iterator I = BasePtr.getNode()->use_begin(), |
| 7485 | E = BasePtr.getNode()->use_end(); I != E; ++I) { |
| 7486 | SDNode *Use = *I; |
| 7487 | if (Use == Ptr.getNode()) |
| 7488 | continue; |
| 7489 | |
| 7490 | if (Use->isPredecessorOf(N)) |
| 7491 | continue; |
| 7492 | |
| 7493 | if (Use->getOpcode() != ISD::ADD && Use->getOpcode() != ISD::SUB) { |
| 7494 | OtherUses.clear(); |
| 7495 | break; |
| 7496 | } |
| 7497 | |
| 7498 | SDValue Op0 = Use->getOperand(0), Op1 = Use->getOperand(1); |
| 7499 | if (Op1.getNode() == BasePtr.getNode()) |
| 7500 | std::swap(Op0, Op1); |
| 7501 | assert(Op0.getNode() == BasePtr.getNode() && |
| 7502 | "Use of ADD/SUB but not an operand"); |
| 7503 | |
| 7504 | if (!isa<ConstantSDNode>(Op1)) { |
| 7505 | OtherUses.clear(); |
| 7506 | break; |
| 7507 | } |
| 7508 | |
| 7509 | // FIXME: In some cases, we can be smarter about this. |
| 7510 | if (Op1.getValueType() != Offset.getValueType()) { |
| 7511 | OtherUses.clear(); |
| 7512 | break; |
| 7513 | } |
| 7514 | |
| 7515 | OtherUses.push_back(Use); |
| 7516 | } |
| 7517 | |
| 7518 | if (Swapped) |
| 7519 | std::swap(BasePtr, Offset); |
| 7520 | |
| Evan Cheng | a4d187b | 2007-05-24 02:35:39 +0000 | [diff] [blame] | 7521 | // Now check for #3 and #4. |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7522 | bool RealUse = false; |
| Lang Hames | 5a00499 | 2011-07-07 04:31:51 +0000 | [diff] [blame] | 7523 | |
| 7524 | // Caches for hasPredecessorHelper |
| 7525 | SmallPtrSet<const SDNode *, 32> Visited; |
| 7526 | SmallVector<const SDNode *, 16> Worklist; |
| 7527 | |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7528 | for (SDNode::use_iterator I = Ptr.getNode()->use_begin(), |
| 7529 | E = Ptr.getNode()->use_end(); I != E; ++I) { |
| Dan Gohman | 91e5dcb | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 7530 | SDNode *Use = *I; |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7531 | if (Use == N) |
| 7532 | continue; |
| Lang Hames | 5a00499 | 2011-07-07 04:31:51 +0000 | [diff] [blame] | 7533 | if (N->hasPredecessorHelper(Use, Visited, Worklist)) |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7534 | return false; |
| 7535 | |
| Evan Cheng | fa83263 | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 7536 | // If Ptr may be folded in addressing mode of other use, then it's |
| 7537 | // not profitable to do this transformation. |
| 7538 | if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI)) |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7539 | RealUse = true; |
| 7540 | } |
| Bill Wendling | 306bfc2 | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7541 | |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7542 | if (!RealUse) |
| 7543 | return false; |
| 7544 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7545 | SDValue Result; |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7546 | if (isLoad) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7547 | Result = DAG.getIndexedLoad(SDValue(N,0), SDLoc(N), |
| Bill Wendling | 306bfc2 | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7548 | BasePtr, Offset, AM); |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7549 | else |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7550 | Result = DAG.getIndexedStore(SDValue(N,0), SDLoc(N), |
| Bill Wendling | 306bfc2 | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7551 | BasePtr, Offset, AM); |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7552 | ++PreIndexedNodes; |
| 7553 | ++NodesCombined; |
| David Greene | fe5c352 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7554 | DEBUG(dbgs() << "\nReplacing.4 "; |
| Chris Lattner | 4dc3edd | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7555 | N->dump(&DAG); |
| David Greene | fe5c352 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7556 | dbgs() << "\nWith: "; |
| Chris Lattner | 4dc3edd | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7557 | Result.getNode()->dump(&DAG); |
| David Greene | fe5c352 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7558 | dbgs() << '\n'); |
| Chris Lattner | b2b9d6f | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 7559 | WorkListRemover DeadNodes(*this); |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7560 | if (isLoad) { |
| Jakob Stoklund Olesen | beb9469 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7561 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0)); |
| 7562 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2)); |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7563 | } else { |
| Jakob Stoklund Olesen | beb9469 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7564 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1)); |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7565 | } |
| 7566 | |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7567 | // Finally, since the node is now dead, remove it from the graph. |
| 7568 | DAG.DeleteNode(N); |
| 7569 | |
| Hal Finkel | 2581905 | 2013-02-08 21:35:47 +0000 | [diff] [blame] | 7570 | if (Swapped) |
| 7571 | std::swap(BasePtr, Offset); |
| 7572 | |
| 7573 | // Replace other uses of BasePtr that can be updated to use Ptr |
| 7574 | for (unsigned i = 0, e = OtherUses.size(); i != e; ++i) { |
| 7575 | unsigned OffsetIdx = 1; |
| 7576 | if (OtherUses[i]->getOperand(OffsetIdx).getNode() == BasePtr.getNode()) |
| 7577 | OffsetIdx = 0; |
| 7578 | assert(OtherUses[i]->getOperand(!OffsetIdx).getNode() == |
| 7579 | BasePtr.getNode() && "Expected BasePtr operand"); |
| 7580 | |
| Silviu Baranga | af7e8c3 | 2013-04-26 15:52:24 +0000 | [diff] [blame] | 7581 | // We need to replace ptr0 in the following expression: |
| 7582 | // x0 * offset0 + y0 * ptr0 = t0 |
| 7583 | // knowing that |
| 7584 | // x1 * offset1 + y1 * ptr0 = t1 (the indexed load/store) |
| Stephen Lin | cfe7f35 | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 7585 | // |
| Silviu Baranga | af7e8c3 | 2013-04-26 15:52:24 +0000 | [diff] [blame] | 7586 | // where x0, x1, y0 and y1 in {-1, 1} are given by the types of the |
| 7587 | // indexed load/store and the expresion that needs to be re-written. |
| 7588 | // |
| 7589 | // Therefore, we have: |
| 7590 | // t0 = (x0 * offset0 - x1 * y0 * y1 *offset1) + (y0 * y1) * t1 |
| Hal Finkel | 2581905 | 2013-02-08 21:35:47 +0000 | [diff] [blame] | 7591 | |
| 7592 | ConstantSDNode *CN = |
| 7593 | cast<ConstantSDNode>(OtherUses[i]->getOperand(OffsetIdx)); |
| Silviu Baranga | af7e8c3 | 2013-04-26 15:52:24 +0000 | [diff] [blame] | 7594 | int X0, X1, Y0, Y1; |
| 7595 | APInt Offset0 = CN->getAPIntValue(); |
| 7596 | APInt Offset1 = cast<ConstantSDNode>(Offset)->getAPIntValue(); |
| Hal Finkel | 2581905 | 2013-02-08 21:35:47 +0000 | [diff] [blame] | 7597 | |
| Silviu Baranga | af7e8c3 | 2013-04-26 15:52:24 +0000 | [diff] [blame] | 7598 | X0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 1) ? -1 : 1; |
| 7599 | Y0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 0) ? -1 : 1; |
| 7600 | X1 = (AM == ISD::PRE_DEC && !Swapped) ? -1 : 1; |
| 7601 | Y1 = (AM == ISD::PRE_DEC && Swapped) ? -1 : 1; |
| Hal Finkel | 2581905 | 2013-02-08 21:35:47 +0000 | [diff] [blame] | 7602 | |
| Silviu Baranga | af7e8c3 | 2013-04-26 15:52:24 +0000 | [diff] [blame] | 7603 | unsigned Opcode = (Y0 * Y1 < 0) ? ISD::SUB : ISD::ADD; |
| 7604 | |
| 7605 | APInt CNV = Offset0; |
| 7606 | if (X0 < 0) CNV = -CNV; |
| 7607 | if (X1 * Y0 * Y1 < 0) CNV = CNV + Offset1; |
| 7608 | else CNV = CNV - Offset1; |
| 7609 | |
| 7610 | // We can now generate the new expression. |
| 7611 | SDValue NewOp1 = DAG.getConstant(CNV, CN->getValueType(0)); |
| 7612 | SDValue NewOp2 = Result.getValue(isLoad ? 1 : 0); |
| 7613 | |
| 7614 | SDValue NewUse = DAG.getNode(Opcode, |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7615 | SDLoc(OtherUses[i]), |
| Hal Finkel | 2581905 | 2013-02-08 21:35:47 +0000 | [diff] [blame] | 7616 | OtherUses[i]->getValueType(0), NewOp1, NewOp2); |
| 7617 | DAG.ReplaceAllUsesOfValueWith(SDValue(OtherUses[i], 0), NewUse); |
| 7618 | removeFromWorkList(OtherUses[i]); |
| 7619 | DAG.DeleteNode(OtherUses[i]); |
| 7620 | } |
| 7621 | |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7622 | // Replace the uses of Ptr with uses of the updated base value. |
| Jakob Stoklund Olesen | beb9469 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7623 | DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0)); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7624 | removeFromWorkList(Ptr.getNode()); |
| 7625 | DAG.DeleteNode(Ptr.getNode()); |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7626 | |
| 7627 | return true; |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7628 | } |
| 7629 | |
| Duncan Sands | 075293f | 2008-06-15 20:12:31 +0000 | [diff] [blame] | 7630 | /// CombineToPostIndexedLoadStore - Try to combine a load / store with a |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7631 | /// add / sub of the base pointer node into a post-indexed load / store. |
| 7632 | /// The transformation folded the add / subtract into the new indexed |
| 7633 | /// load / store effectively and all of its uses are redirected to the |
| 7634 | /// new load / store. |
| 7635 | bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) { |
| Eli Friedman | 9d448e4 | 2011-11-12 00:35:34 +0000 | [diff] [blame] | 7636 | if (Level < AfterLegalizeDAG) |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7637 | return false; |
| 7638 | |
| 7639 | bool isLoad = true; |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7640 | SDValue Ptr; |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7641 | EVT VT; |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7642 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { |
| Chris Lattner | 1ea55cf | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 7643 | if (LD->isIndexed()) |
| Evan Cheng | 28cf427 | 2006-12-16 06:25:23 +0000 | [diff] [blame] | 7644 | return false; |
| Dan Gohman | 47a7d6f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 7645 | VT = LD->getMemoryVT(); |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7646 | if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) && |
| 7647 | !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT)) |
| 7648 | return false; |
| 7649 | Ptr = LD->getBasePtr(); |
| 7650 | } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { |
| Chris Lattner | 1ea55cf | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 7651 | if (ST->isIndexed()) |
| Evan Cheng | 28cf427 | 2006-12-16 06:25:23 +0000 | [diff] [blame] | 7652 | return false; |
| Dan Gohman | 47a7d6f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 7653 | VT = ST->getMemoryVT(); |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7654 | if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) && |
| 7655 | !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT)) |
| 7656 | return false; |
| 7657 | Ptr = ST->getBasePtr(); |
| 7658 | isLoad = false; |
| Bill Wendling | 306bfc2 | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7659 | } else { |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7660 | return false; |
| Bill Wendling | 306bfc2 | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7661 | } |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7662 | |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7663 | if (Ptr.getNode()->hasOneUse()) |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7664 | return false; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7665 | |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7666 | for (SDNode::use_iterator I = Ptr.getNode()->use_begin(), |
| 7667 | E = Ptr.getNode()->use_end(); I != E; ++I) { |
| Dan Gohman | 91e5dcb | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 7668 | SDNode *Op = *I; |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7669 | if (Op == N || |
| 7670 | (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB)) |
| 7671 | continue; |
| 7672 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7673 | SDValue BasePtr; |
| 7674 | SDValue Offset; |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7675 | ISD::MemIndexedMode AM = ISD::UNINDEXED; |
| 7676 | if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) { |
| Evan Cheng | 044a0a8 | 2007-05-03 23:52:19 +0000 | [diff] [blame] | 7677 | // Don't create a indexed load / store with zero offset. |
| 7678 | if (isa<ConstantSDNode>(Offset) && |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 7679 | cast<ConstantSDNode>(Offset)->isNullValue()) |
| Evan Cheng | 044a0a8 | 2007-05-03 23:52:19 +0000 | [diff] [blame] | 7680 | continue; |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7681 | |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7682 | // Try turning it into a post-indexed load / store except when |
| Evan Cheng | fa83263 | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 7683 | // 1) All uses are load / store ops that use it as base ptr (and |
| 7684 | // it may be folded as addressing mmode). |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7685 | // 2) Op must be independent of N, i.e. Op is neither a predecessor |
| 7686 | // nor a successor of N. Otherwise, if Op is folded that would |
| 7687 | // create a cycle. |
| 7688 | |
| Evan Cheng | cfc0513 | 2009-05-06 18:25:01 +0000 | [diff] [blame] | 7689 | if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr)) |
| 7690 | continue; |
| 7691 | |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7692 | // Check for #1. |
| 7693 | bool TryNext = false; |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7694 | for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(), |
| 7695 | EE = BasePtr.getNode()->use_end(); II != EE; ++II) { |
| Dan Gohman | 91e5dcb | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 7696 | SDNode *Use = *II; |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7697 | if (Use == Ptr.getNode()) |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7698 | continue; |
| 7699 | |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7700 | // If all the uses are load / store addresses, then don't do the |
| 7701 | // transformation. |
| 7702 | if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){ |
| 7703 | bool RealUse = false; |
| 7704 | for (SDNode::use_iterator III = Use->use_begin(), |
| 7705 | EEE = Use->use_end(); III != EEE; ++III) { |
| Dan Gohman | 91e5dcb | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 7706 | SDNode *UseUse = *III; |
| Stephen Lin | cfe7f35 | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 7707 | if (!canFoldInAddressingMode(Use, UseUse, DAG, TLI)) |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7708 | RealUse = true; |
| 7709 | } |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7710 | |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7711 | if (!RealUse) { |
| 7712 | TryNext = true; |
| 7713 | break; |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7714 | } |
| 7715 | } |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7716 | } |
| Bill Wendling | 306bfc2 | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7717 | |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7718 | if (TryNext) |
| 7719 | continue; |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7720 | |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7721 | // Check for #2 |
| Evan Cheng | 567d2e5 | 2008-03-04 00:41:45 +0000 | [diff] [blame] | 7722 | if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7723 | SDValue Result = isLoad |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7724 | ? DAG.getIndexedLoad(SDValue(N,0), SDLoc(N), |
| Bill Wendling | 306bfc2 | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7725 | BasePtr, Offset, AM) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7726 | : DAG.getIndexedStore(SDValue(N,0), SDLoc(N), |
| Bill Wendling | 306bfc2 | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7727 | BasePtr, Offset, AM); |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7728 | ++PostIndexedNodes; |
| 7729 | ++NodesCombined; |
| David Greene | fe5c352 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7730 | DEBUG(dbgs() << "\nReplacing.5 "; |
| Chris Lattner | 4dc3edd | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7731 | N->dump(&DAG); |
| David Greene | fe5c352 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7732 | dbgs() << "\nWith: "; |
| Chris Lattner | 4dc3edd | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7733 | Result.getNode()->dump(&DAG); |
| David Greene | fe5c352 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7734 | dbgs() << '\n'); |
| Chris Lattner | b2b9d6f | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 7735 | WorkListRemover DeadNodes(*this); |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7736 | if (isLoad) { |
| Jakob Stoklund Olesen | beb9469 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7737 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0)); |
| 7738 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2)); |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7739 | } else { |
| Jakob Stoklund Olesen | beb9469 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7740 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1)); |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7741 | } |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7742 | |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7743 | // Finally, since the node is now dead, remove it from the graph. |
| 7744 | DAG.DeleteNode(N); |
| 7745 | |
| 7746 | // Replace the uses of Use with uses of the updated base value. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7747 | DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0), |
| Jakob Stoklund Olesen | beb9469 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7748 | Result.getValue(isLoad ? 1 : 0)); |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7749 | removeFromWorkList(Op); |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7750 | DAG.DeleteNode(Op); |
| Chris Lattner | eabc15c | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7751 | return true; |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7752 | } |
| 7753 | } |
| 7754 | } |
| Bill Wendling | 306bfc2 | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7755 | |
| Chris Lattner | ffad216 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7756 | return false; |
| 7757 | } |
| 7758 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7759 | SDValue DAGCombiner::visitLOAD(SDNode *N) { |
| Evan Cheng | e71fe34d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 7760 | LoadSDNode *LD = cast<LoadSDNode>(N); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7761 | SDValue Chain = LD->getChain(); |
| 7762 | SDValue Ptr = LD->getBasePtr(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7763 | |
| Evan Cheng | a684cd2 | 2007-05-01 00:38:21 +0000 | [diff] [blame] | 7764 | // If load is not volatile and there are no uses of the loaded value (and |
| 7765 | // the updated indexed value in case of indexed loads), change uses of the |
| 7766 | // chain value into uses of the chain input (i.e. delete the dead load). |
| 7767 | if (!LD->isVolatile()) { |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7768 | if (N->getValueType(1) == MVT::Other) { |
| Evan Cheng | b68343c | 2007-05-01 08:53:39 +0000 | [diff] [blame] | 7769 | // Unindexed loads. |
| Craig Topper | 0515cd4 | 2012-01-07 18:31:09 +0000 | [diff] [blame] | 7770 | if (!N->hasAnyUseOfValue(0)) { |
| Evan Cheng | 7be1528 | 2008-01-16 23:11:54 +0000 | [diff] [blame] | 7771 | // It's not safe to use the two value CombineTo variant here. e.g. |
| 7772 | // v1, chain2 = load chain1, loc |
| 7773 | // v2, chain3 = load chain2, loc |
| 7774 | // v3 = add v2, c |
| Chris Lattner | e97fa8c | 2008-01-24 07:57:06 +0000 | [diff] [blame] | 7775 | // Now we replace use of chain2 with chain1. This makes the second load |
| 7776 | // isomorphic to the one we are deleting, and thus makes this load live. |
| David Greene | fe5c352 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7777 | DEBUG(dbgs() << "\nReplacing.6 "; |
| Chris Lattner | 4dc3edd | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7778 | N->dump(&DAG); |
| David Greene | fe5c352 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7779 | dbgs() << "\nWith chain: "; |
| Chris Lattner | 4dc3edd | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7780 | Chain.getNode()->dump(&DAG); |
| David Greene | fe5c352 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7781 | dbgs() << "\n"); |
| Chris Lattner | b2b9d6f | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 7782 | WorkListRemover DeadNodes(*this); |
| Jakob Stoklund Olesen | beb9469 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7783 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain); |
| Bill Wendling | 306bfc2 | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7784 | |
| Chris Lattner | e97fa8c | 2008-01-24 07:57:06 +0000 | [diff] [blame] | 7785 | if (N->use_empty()) { |
| 7786 | removeFromWorkList(N); |
| 7787 | DAG.DeleteNode(N); |
| 7788 | } |
| Bill Wendling | 306bfc2 | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7789 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7790 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| Evan Cheng | 7be1528 | 2008-01-16 23:11:54 +0000 | [diff] [blame] | 7791 | } |
| Evan Cheng | b68343c | 2007-05-01 08:53:39 +0000 | [diff] [blame] | 7792 | } else { |
| 7793 | // Indexed loads. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7794 | assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?"); |
| Craig Topper | 0515cd4 | 2012-01-07 18:31:09 +0000 | [diff] [blame] | 7795 | if (!N->hasAnyUseOfValue(0) && !N->hasAnyUseOfValue(1)) { |
| Dale Johannesen | 8493575 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 7796 | SDValue Undef = DAG.getUNDEF(N->getValueType(0)); |
| Evan Cheng | 228c31f | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 7797 | DEBUG(dbgs() << "\nReplacing.7 "; |
| Chris Lattner | 4dc3edd | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7798 | N->dump(&DAG); |
| David Greene | fe5c352 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7799 | dbgs() << "\nWith: "; |
| Chris Lattner | 4dc3edd | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7800 | Undef.getNode()->dump(&DAG); |
| David Greene | fe5c352 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7801 | dbgs() << " and 2 other values\n"); |
| Chris Lattner | b2b9d6f | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 7802 | WorkListRemover DeadNodes(*this); |
| Jakob Stoklund Olesen | beb9469 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7803 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7804 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), |
| Jakob Stoklund Olesen | beb9469 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7805 | DAG.getUNDEF(N->getValueType(1))); |
| 7806 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain); |
| Evan Cheng | 7be1528 | 2008-01-16 23:11:54 +0000 | [diff] [blame] | 7807 | removeFromWorkList(N); |
| Evan Cheng | 7be1528 | 2008-01-16 23:11:54 +0000 | [diff] [blame] | 7808 | DAG.DeleteNode(N); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7809 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
| Evan Cheng | a684cd2 | 2007-05-01 00:38:21 +0000 | [diff] [blame] | 7810 | } |
| Evan Cheng | a684cd2 | 2007-05-01 00:38:21 +0000 | [diff] [blame] | 7811 | } |
| 7812 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7813 | |
| Chris Lattner | e260ed8 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 7814 | // If this load is directly stored, replace the load value with the stored |
| 7815 | // value. |
| 7816 | // TODO: Handle store large -> read small portion. |
| Jim Laskey | 0f7c328 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 7817 | // TODO: Handle TRUNCSTORE/LOADEXT |
| Evan Cheng | adb9c03 | 2011-03-11 00:48:56 +0000 | [diff] [blame] | 7818 | if (ISD::isNormalLoad(N) && !LD->isVolatile()) { |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7819 | if (ISD::isNON_TRUNCStore(Chain.getNode())) { |
| Evan Cheng | ab51cf2 | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 7820 | StoreSDNode *PrevST = cast<StoreSDNode>(Chain); |
| 7821 | if (PrevST->getBasePtr() == Ptr && |
| 7822 | PrevST->getValue().getValueType() == N->getValueType(0)) |
| Jim Laskey | 0f7c328 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 7823 | return CombineTo(N, Chain.getOperand(1), Chain); |
| Evan Cheng | ab51cf2 | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 7824 | } |
| Jim Laskey | 0f7c328 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 7825 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7826 | |
| Evan Cheng | 43cd9e3 | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 7827 | // Try to infer better alignment information than the load already has. |
| 7828 | if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) { |
| Evan Cheng | 4a5b204 | 2011-11-28 22:37:34 +0000 | [diff] [blame] | 7829 | if (unsigned Align = DAG.InferPtrAlignment(Ptr)) { |
| Owen Anderson | de89ecf | 2013-02-05 19:24:39 +0000 | [diff] [blame] | 7830 | if (Align > LD->getMemOperand()->getBaseAlignment()) { |
| 7831 | SDValue NewLoad = |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7832 | DAG.getExtLoad(LD->getExtensionType(), SDLoc(N), |
| Evan Cheng | 4a5b204 | 2011-11-28 22:37:34 +0000 | [diff] [blame] | 7833 | LD->getValueType(0), |
| 7834 | Chain, Ptr, LD->getPointerInfo(), |
| 7835 | LD->getMemoryVT(), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 7836 | LD->isVolatile(), LD->isNonTemporal(), Align, |
| 7837 | LD->getTBAAInfo()); |
| Owen Anderson | de89ecf | 2013-02-05 19:24:39 +0000 | [diff] [blame] | 7838 | return CombineTo(N, NewLoad, SDValue(NewLoad.getNode(), 1), true); |
| 7839 | } |
| Evan Cheng | 43cd9e3 | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 7840 | } |
| 7841 | } |
| 7842 | |
| Hal Finkel | 5ef4dcc | 2013-08-29 03:29:55 +0000 | [diff] [blame] | 7843 | bool UseAA = CombinerAA.getNumOccurrences() > 0 ? CombinerAA : |
| 7844 | TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA(); |
| Hal Finkel | 9b2617a | 2014-01-25 17:32:39 +0000 | [diff] [blame] | 7845 | #ifndef NDEBUG |
| 7846 | if (CombinerAAOnlyFunc.getNumOccurrences() && |
| 7847 | CombinerAAOnlyFunc != DAG.getMachineFunction().getName()) |
| 7848 | UseAA = false; |
| 7849 | #endif |
| Hal Finkel | ccc18e1 | 2014-01-24 18:25:26 +0000 | [diff] [blame] | 7850 | if (UseAA && LD->isUnindexed()) { |
| Jim Laskey | d07be23 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7851 | // Walk up chain skipping non-aliasing memory nodes. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7852 | SDValue BetterChain = FindBetterChain(N, Chain); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7853 | |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 7854 | // If there is a better chain. |
| Jim Laskey | d07be23 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7855 | if (Chain != BetterChain) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7856 | SDValue ReplLoad; |
| Jim Laskey | 0f7c328 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 7857 | |
| Jim Laskey | d07be23 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7858 | // Replace the chain to void dependency. |
| Jim Laskey | 0f7c328 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 7859 | if (LD->getExtensionType() == ISD::NON_EXTLOAD) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7860 | ReplLoad = DAG.getLoad(N->getValueType(0), SDLoc(LD), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 7861 | BetterChain, Ptr, LD->getMemOperand()); |
| Jim Laskey | 0f7c328 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 7862 | } else { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7863 | ReplLoad = DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), |
| Stuart Hastings | 81c4306 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 7864 | LD->getValueType(0), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 7865 | BetterChain, Ptr, LD->getMemoryVT(), |
| 7866 | LD->getMemOperand()); |
| Jim Laskey | 0f7c328 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 7867 | } |
| Jim Laskey | d07be23 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7868 | |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 7869 | // Create token factor to keep old chain connected. |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7870 | SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N), |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7871 | MVT::Other, Chain, ReplLoad.getValue(1)); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7872 | |
| Nate Begeman | 879d8f1 | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 7873 | // Make sure the new and old chains are cleaned up. |
| 7874 | AddToWorkList(Token.getNode()); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7875 | |
| Jim Laskey | dcf983c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 7876 | // Replace uses with load result and token factor. Don't add users |
| 7877 | // to work list. |
| 7878 | return CombineTo(N, ReplLoad.getValue(0), Token, false); |
| Jim Laskey | d07be23 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7879 | } |
| 7880 | } |
| 7881 | |
| Evan Cheng | 357017f | 2006-11-03 03:06:21 +0000 | [diff] [blame] | 7882 | // Try transforming N to an indexed load. |
| Evan Cheng | 60c6846 | 2006-11-07 09:03:05 +0000 | [diff] [blame] | 7883 | if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N)) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7884 | return SDValue(N, 0); |
| Evan Cheng | 357017f | 2006-11-03 03:06:21 +0000 | [diff] [blame] | 7885 | |
| Quentin Colombet | de0e062 | 2013-10-11 18:29:42 +0000 | [diff] [blame] | 7886 | // Try to slice up N to more direct loads if the slices are mapped to |
| 7887 | // different register banks or pairing can take place. |
| 7888 | if (SliceUpLoad(N)) |
| 7889 | return SDValue(N, 0); |
| 7890 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7891 | return SDValue(); |
| Chris Lattner | e260ed8 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 7892 | } |
| 7893 | |
| Quentin Colombet | de0e062 | 2013-10-11 18:29:42 +0000 | [diff] [blame] | 7894 | namespace { |
| 7895 | /// \brief Helper structure used to slice a load in smaller loads. |
| 7896 | /// Basically a slice is obtained from the following sequence: |
| 7897 | /// Origin = load Ty1, Base |
| 7898 | /// Shift = srl Ty1 Origin, CstTy Amount |
| 7899 | /// Inst = trunc Shift to Ty2 |
| 7900 | /// |
| 7901 | /// Then, it will be rewriten into: |
| 7902 | /// Slice = load SliceTy, Base + SliceOffset |
| 7903 | /// [Inst = zext Slice to Ty2], only if SliceTy <> Ty2 |
| 7904 | /// |
| 7905 | /// SliceTy is deduced from the number of bits that are actually used to |
| 7906 | /// build Inst. |
| 7907 | struct LoadedSlice { |
| 7908 | /// \brief Helper structure used to compute the cost of a slice. |
| 7909 | struct Cost { |
| 7910 | /// Are we optimizing for code size. |
| 7911 | bool ForCodeSize; |
| 7912 | /// Various cost. |
| 7913 | unsigned Loads; |
| 7914 | unsigned Truncates; |
| 7915 | unsigned CrossRegisterBanksCopies; |
| 7916 | unsigned ZExts; |
| 7917 | unsigned Shift; |
| 7918 | |
| 7919 | Cost(bool ForCodeSize = false) |
| 7920 | : ForCodeSize(ForCodeSize), Loads(0), Truncates(0), |
| 7921 | CrossRegisterBanksCopies(0), ZExts(0), Shift(0) {} |
| 7922 | |
| 7923 | /// \brief Get the cost of one isolated slice. |
| 7924 | Cost(const LoadedSlice &LS, bool ForCodeSize = false) |
| 7925 | : ForCodeSize(ForCodeSize), Loads(1), Truncates(0), |
| 7926 | CrossRegisterBanksCopies(0), ZExts(0), Shift(0) { |
| 7927 | EVT TruncType = LS.Inst->getValueType(0); |
| 7928 | EVT LoadedType = LS.getLoadedType(); |
| 7929 | if (TruncType != LoadedType && |
| 7930 | !LS.DAG->getTargetLoweringInfo().isZExtFree(LoadedType, TruncType)) |
| 7931 | ZExts = 1; |
| 7932 | } |
| 7933 | |
| 7934 | /// \brief Account for slicing gain in the current cost. |
| 7935 | /// Slicing provide a few gains like removing a shift or a |
| 7936 | /// truncate. This method allows to grow the cost of the original |
| 7937 | /// load with the gain from this slice. |
| 7938 | void addSliceGain(const LoadedSlice &LS) { |
| 7939 | // Each slice saves a truncate. |
| 7940 | const TargetLowering &TLI = LS.DAG->getTargetLoweringInfo(); |
| 7941 | if (!TLI.isTruncateFree(LS.Inst->getValueType(0), |
| 7942 | LS.Inst->getOperand(0).getValueType())) |
| 7943 | ++Truncates; |
| 7944 | // If there is a shift amount, this slice gets rid of it. |
| 7945 | if (LS.Shift) |
| 7946 | ++Shift; |
| 7947 | // If this slice can merge a cross register bank copy, account for it. |
| 7948 | if (LS.canMergeExpensiveCrossRegisterBankCopy()) |
| 7949 | ++CrossRegisterBanksCopies; |
| 7950 | } |
| 7951 | |
| 7952 | Cost &operator+=(const Cost &RHS) { |
| 7953 | Loads += RHS.Loads; |
| 7954 | Truncates += RHS.Truncates; |
| 7955 | CrossRegisterBanksCopies += RHS.CrossRegisterBanksCopies; |
| 7956 | ZExts += RHS.ZExts; |
| 7957 | Shift += RHS.Shift; |
| 7958 | return *this; |
| 7959 | } |
| 7960 | |
| 7961 | bool operator==(const Cost &RHS) const { |
| 7962 | return Loads == RHS.Loads && Truncates == RHS.Truncates && |
| 7963 | CrossRegisterBanksCopies == RHS.CrossRegisterBanksCopies && |
| 7964 | ZExts == RHS.ZExts && Shift == RHS.Shift; |
| 7965 | } |
| 7966 | |
| 7967 | bool operator!=(const Cost &RHS) const { return !(*this == RHS); } |
| 7968 | |
| 7969 | bool operator<(const Cost &RHS) const { |
| 7970 | // Assume cross register banks copies are as expensive as loads. |
| 7971 | // FIXME: Do we want some more target hooks? |
| 7972 | unsigned ExpensiveOpsLHS = Loads + CrossRegisterBanksCopies; |
| 7973 | unsigned ExpensiveOpsRHS = RHS.Loads + RHS.CrossRegisterBanksCopies; |
| 7974 | // Unless we are optimizing for code size, consider the |
| 7975 | // expensive operation first. |
| 7976 | if (!ForCodeSize && ExpensiveOpsLHS != ExpensiveOpsRHS) |
| 7977 | return ExpensiveOpsLHS < ExpensiveOpsRHS; |
| 7978 | return (Truncates + ZExts + Shift + ExpensiveOpsLHS) < |
| 7979 | (RHS.Truncates + RHS.ZExts + RHS.Shift + ExpensiveOpsRHS); |
| 7980 | } |
| 7981 | |
| 7982 | bool operator>(const Cost &RHS) const { return RHS < *this; } |
| 7983 | |
| 7984 | bool operator<=(const Cost &RHS) const { return !(RHS < *this); } |
| 7985 | |
| 7986 | bool operator>=(const Cost &RHS) const { return !(*this < RHS); } |
| 7987 | }; |
| 7988 | // The last instruction that represent the slice. This should be a |
| 7989 | // truncate instruction. |
| 7990 | SDNode *Inst; |
| 7991 | // The original load instruction. |
| 7992 | LoadSDNode *Origin; |
| 7993 | // The right shift amount in bits from the original load. |
| 7994 | unsigned Shift; |
| 7995 | // The DAG from which Origin came from. |
| 7996 | // This is used to get some contextual information about legal types, etc. |
| 7997 | SelectionDAG *DAG; |
| 7998 | |
| 7999 | LoadedSlice(SDNode *Inst = NULL, LoadSDNode *Origin = NULL, |
| 8000 | unsigned Shift = 0, SelectionDAG *DAG = NULL) |
| 8001 | : Inst(Inst), Origin(Origin), Shift(Shift), DAG(DAG) {} |
| 8002 | |
| 8003 | LoadedSlice(const LoadedSlice &LS) |
| 8004 | : Inst(LS.Inst), Origin(LS.Origin), Shift(LS.Shift), DAG(LS.DAG) {} |
| 8005 | |
| 8006 | /// \brief Get the bits used in a chunk of bits \p BitWidth large. |
| 8007 | /// \return Result is \p BitWidth and has used bits set to 1 and |
| 8008 | /// not used bits set to 0. |
| 8009 | APInt getUsedBits() const { |
| 8010 | // Reproduce the trunc(lshr) sequence: |
| 8011 | // - Start from the truncated value. |
| 8012 | // - Zero extend to the desired bit width. |
| 8013 | // - Shift left. |
| 8014 | assert(Origin && "No original load to compare against."); |
| 8015 | unsigned BitWidth = Origin->getValueSizeInBits(0); |
| 8016 | assert(Inst && "This slice is not bound to an instruction"); |
| 8017 | assert(Inst->getValueSizeInBits(0) <= BitWidth && |
| 8018 | "Extracted slice is bigger than the whole type!"); |
| 8019 | APInt UsedBits(Inst->getValueSizeInBits(0), 0); |
| 8020 | UsedBits.setAllBits(); |
| 8021 | UsedBits = UsedBits.zext(BitWidth); |
| 8022 | UsedBits <<= Shift; |
| 8023 | return UsedBits; |
| 8024 | } |
| 8025 | |
| 8026 | /// \brief Get the size of the slice to be loaded in bytes. |
| 8027 | unsigned getLoadedSize() const { |
| 8028 | unsigned SliceSize = getUsedBits().countPopulation(); |
| 8029 | assert(!(SliceSize & 0x7) && "Size is not a multiple of a byte."); |
| 8030 | return SliceSize / 8; |
| 8031 | } |
| 8032 | |
| 8033 | /// \brief Get the type that will be loaded for this slice. |
| 8034 | /// Note: This may not be the final type for the slice. |
| 8035 | EVT getLoadedType() const { |
| 8036 | assert(DAG && "Missing context"); |
| 8037 | LLVMContext &Ctxt = *DAG->getContext(); |
| 8038 | return EVT::getIntegerVT(Ctxt, getLoadedSize() * 8); |
| 8039 | } |
| 8040 | |
| 8041 | /// \brief Get the alignment of the load used for this slice. |
| 8042 | unsigned getAlignment() const { |
| 8043 | unsigned Alignment = Origin->getAlignment(); |
| 8044 | unsigned Offset = getOffsetFromBase(); |
| 8045 | if (Offset != 0) |
| 8046 | Alignment = MinAlign(Alignment, Alignment + Offset); |
| 8047 | return Alignment; |
| 8048 | } |
| 8049 | |
| 8050 | /// \brief Check if this slice can be rewritten with legal operations. |
| 8051 | bool isLegal() const { |
| 8052 | // An invalid slice is not legal. |
| 8053 | if (!Origin || !Inst || !DAG) |
| 8054 | return false; |
| 8055 | |
| 8056 | // Offsets are for indexed load only, we do not handle that. |
| 8057 | if (Origin->getOffset().getOpcode() != ISD::UNDEF) |
| 8058 | return false; |
| 8059 | |
| 8060 | const TargetLowering &TLI = DAG->getTargetLoweringInfo(); |
| 8061 | |
| 8062 | // Check that the type is legal. |
| 8063 | EVT SliceType = getLoadedType(); |
| 8064 | if (!TLI.isTypeLegal(SliceType)) |
| 8065 | return false; |
| 8066 | |
| 8067 | // Check that the load is legal for this type. |
| 8068 | if (!TLI.isOperationLegal(ISD::LOAD, SliceType)) |
| 8069 | return false; |
| 8070 | |
| 8071 | // Check that the offset can be computed. |
| 8072 | // 1. Check its type. |
| 8073 | EVT PtrType = Origin->getBasePtr().getValueType(); |
| 8074 | if (PtrType == MVT::Untyped || PtrType.isExtended()) |
| 8075 | return false; |
| 8076 | |
| 8077 | // 2. Check that it fits in the immediate. |
| 8078 | if (!TLI.isLegalAddImmediate(getOffsetFromBase())) |
| 8079 | return false; |
| 8080 | |
| 8081 | // 3. Check that the computation is legal. |
| 8082 | if (!TLI.isOperationLegal(ISD::ADD, PtrType)) |
| 8083 | return false; |
| 8084 | |
| 8085 | // Check that the zext is legal if it needs one. |
| 8086 | EVT TruncateType = Inst->getValueType(0); |
| 8087 | if (TruncateType != SliceType && |
| 8088 | !TLI.isOperationLegal(ISD::ZERO_EXTEND, TruncateType)) |
| 8089 | return false; |
| 8090 | |
| 8091 | return true; |
| 8092 | } |
| 8093 | |
| 8094 | /// \brief Get the offset in bytes of this slice in the original chunk of |
| 8095 | /// bits. |
| 8096 | /// \pre DAG != NULL. |
| 8097 | uint64_t getOffsetFromBase() const { |
| 8098 | assert(DAG && "Missing context."); |
| 8099 | bool IsBigEndian = |
| 8100 | DAG->getTargetLoweringInfo().getDataLayout()->isBigEndian(); |
| 8101 | assert(!(Shift & 0x7) && "Shifts not aligned on Bytes are not supported."); |
| 8102 | uint64_t Offset = Shift / 8; |
| 8103 | unsigned TySizeInBytes = Origin->getValueSizeInBits(0) / 8; |
| 8104 | assert(!(Origin->getValueSizeInBits(0) & 0x7) && |
| 8105 | "The size of the original loaded type is not a multiple of a" |
| 8106 | " byte."); |
| 8107 | // If Offset is bigger than TySizeInBytes, it means we are loading all |
| 8108 | // zeros. This should have been optimized before in the process. |
| 8109 | assert(TySizeInBytes > Offset && |
| 8110 | "Invalid shift amount for given loaded size"); |
| 8111 | if (IsBigEndian) |
| 8112 | Offset = TySizeInBytes - Offset - getLoadedSize(); |
| 8113 | return Offset; |
| 8114 | } |
| 8115 | |
| 8116 | /// \brief Generate the sequence of instructions to load the slice |
| 8117 | /// represented by this object and redirect the uses of this slice to |
| 8118 | /// this new sequence of instructions. |
| 8119 | /// \pre this->Inst && this->Origin are valid Instructions and this |
| 8120 | /// object passed the legal check: LoadedSlice::isLegal returned true. |
| 8121 | /// \return The last instruction of the sequence used to load the slice. |
| 8122 | SDValue loadSlice() const { |
| 8123 | assert(Inst && Origin && "Unable to replace a non-existing slice."); |
| 8124 | const SDValue &OldBaseAddr = Origin->getBasePtr(); |
| 8125 | SDValue BaseAddr = OldBaseAddr; |
| 8126 | // Get the offset in that chunk of bytes w.r.t. the endianess. |
| 8127 | int64_t Offset = static_cast<int64_t>(getOffsetFromBase()); |
| 8128 | assert(Offset >= 0 && "Offset too big to fit in int64_t!"); |
| 8129 | if (Offset) { |
| 8130 | // BaseAddr = BaseAddr + Offset. |
| 8131 | EVT ArithType = BaseAddr.getValueType(); |
| 8132 | BaseAddr = DAG->getNode(ISD::ADD, SDLoc(Origin), ArithType, BaseAddr, |
| 8133 | DAG->getConstant(Offset, ArithType)); |
| 8134 | } |
| 8135 | |
| 8136 | // Create the type of the loaded slice according to its size. |
| 8137 | EVT SliceType = getLoadedType(); |
| 8138 | |
| 8139 | // Create the load for the slice. |
| 8140 | SDValue LastInst = DAG->getLoad( |
| 8141 | SliceType, SDLoc(Origin), Origin->getChain(), BaseAddr, |
| 8142 | Origin->getPointerInfo().getWithOffset(Offset), Origin->isVolatile(), |
| 8143 | Origin->isNonTemporal(), Origin->isInvariant(), getAlignment()); |
| 8144 | // If the final type is not the same as the loaded type, this means that |
| 8145 | // we have to pad with zero. Create a zero extend for that. |
| 8146 | EVT FinalType = Inst->getValueType(0); |
| 8147 | if (SliceType != FinalType) |
| 8148 | LastInst = |
| 8149 | DAG->getNode(ISD::ZERO_EXTEND, SDLoc(LastInst), FinalType, LastInst); |
| 8150 | return LastInst; |
| 8151 | } |
| 8152 | |
| 8153 | /// \brief Check if this slice can be merged with an expensive cross register |
| 8154 | /// bank copy. E.g., |
| 8155 | /// i = load i32 |
| 8156 | /// f = bitcast i32 i to float |
| 8157 | bool canMergeExpensiveCrossRegisterBankCopy() const { |
| 8158 | if (!Inst || !Inst->hasOneUse()) |
| 8159 | return false; |
| 8160 | SDNode *Use = *Inst->use_begin(); |
| 8161 | if (Use->getOpcode() != ISD::BITCAST) |
| 8162 | return false; |
| 8163 | assert(DAG && "Missing context"); |
| 8164 | const TargetLowering &TLI = DAG->getTargetLoweringInfo(); |
| 8165 | EVT ResVT = Use->getValueType(0); |
| 8166 | const TargetRegisterClass *ResRC = TLI.getRegClassFor(ResVT.getSimpleVT()); |
| 8167 | const TargetRegisterClass *ArgRC = |
| 8168 | TLI.getRegClassFor(Use->getOperand(0).getValueType().getSimpleVT()); |
| 8169 | if (ArgRC == ResRC || !TLI.isOperationLegal(ISD::LOAD, ResVT)) |
| 8170 | return false; |
| 8171 | |
| 8172 | // At this point, we know that we perform a cross-register-bank copy. |
| 8173 | // Check if it is expensive. |
| 8174 | const TargetRegisterInfo *TRI = TLI.getTargetMachine().getRegisterInfo(); |
| 8175 | // Assume bitcasts are cheap, unless both register classes do not |
| 8176 | // explicitly share a common sub class. |
| 8177 | if (!TRI || TRI->getCommonSubClass(ArgRC, ResRC)) |
| 8178 | return false; |
| 8179 | |
| 8180 | // Check if it will be merged with the load. |
| 8181 | // 1. Check the alignment constraint. |
| 8182 | unsigned RequiredAlignment = TLI.getDataLayout()->getABITypeAlignment( |
| 8183 | ResVT.getTypeForEVT(*DAG->getContext())); |
| 8184 | |
| 8185 | if (RequiredAlignment > getAlignment()) |
| 8186 | return false; |
| 8187 | |
| 8188 | // 2. Check that the load is a legal operation for that type. |
| 8189 | if (!TLI.isOperationLegal(ISD::LOAD, ResVT)) |
| 8190 | return false; |
| 8191 | |
| 8192 | // 3. Check that we do not have a zext in the way. |
| 8193 | if (Inst->getValueType(0) != getLoadedType()) |
| 8194 | return false; |
| 8195 | |
| 8196 | return true; |
| 8197 | } |
| 8198 | }; |
| 8199 | } |
| 8200 | |
| 8201 | /// \brief Sorts LoadedSlice according to their offset. |
| 8202 | struct LoadedSliceSorter { |
| 8203 | bool operator()(const LoadedSlice &LHS, const LoadedSlice &RHS) { |
| 8204 | assert(LHS.Origin == RHS.Origin && "Different bases not implemented."); |
| 8205 | return LHS.getOffsetFromBase() < RHS.getOffsetFromBase(); |
| 8206 | } |
| 8207 | }; |
| 8208 | |
| 8209 | /// \brief Check that all bits set in \p UsedBits form a dense region, i.e., |
| 8210 | /// \p UsedBits looks like 0..0 1..1 0..0. |
| 8211 | static bool areUsedBitsDense(const APInt &UsedBits) { |
| 8212 | // If all the bits are one, this is dense! |
| 8213 | if (UsedBits.isAllOnesValue()) |
| 8214 | return true; |
| 8215 | |
| 8216 | // Get rid of the unused bits on the right. |
| 8217 | APInt NarrowedUsedBits = UsedBits.lshr(UsedBits.countTrailingZeros()); |
| 8218 | // Get rid of the unused bits on the left. |
| 8219 | if (NarrowedUsedBits.countLeadingZeros()) |
| 8220 | NarrowedUsedBits = NarrowedUsedBits.trunc(NarrowedUsedBits.getActiveBits()); |
| 8221 | // Check that the chunk of bits is completely used. |
| 8222 | return NarrowedUsedBits.isAllOnesValue(); |
| 8223 | } |
| 8224 | |
| 8225 | /// \brief Check whether or not \p First and \p Second are next to each other |
| 8226 | /// in memory. This means that there is no hole between the bits loaded |
| 8227 | /// by \p First and the bits loaded by \p Second. |
| 8228 | static bool areSlicesNextToEachOther(const LoadedSlice &First, |
| 8229 | const LoadedSlice &Second) { |
| 8230 | assert(First.Origin == Second.Origin && First.Origin && |
| 8231 | "Unable to match different memory origins."); |
| 8232 | APInt UsedBits = First.getUsedBits(); |
| 8233 | assert((UsedBits & Second.getUsedBits()) == 0 && |
| 8234 | "Slices are not supposed to overlap."); |
| 8235 | UsedBits |= Second.getUsedBits(); |
| 8236 | return areUsedBitsDense(UsedBits); |
| 8237 | } |
| 8238 | |
| 8239 | /// \brief Adjust the \p GlobalLSCost according to the target |
| 8240 | /// paring capabilities and the layout of the slices. |
| 8241 | /// \pre \p GlobalLSCost should account for at least as many loads as |
| 8242 | /// there is in the slices in \p LoadedSlices. |
| 8243 | static void adjustCostForPairing(SmallVectorImpl<LoadedSlice> &LoadedSlices, |
| 8244 | LoadedSlice::Cost &GlobalLSCost) { |
| 8245 | unsigned NumberOfSlices = LoadedSlices.size(); |
| 8246 | // If there is less than 2 elements, no pairing is possible. |
| 8247 | if (NumberOfSlices < 2) |
| 8248 | return; |
| 8249 | |
| 8250 | // Sort the slices so that elements that are likely to be next to each |
| 8251 | // other in memory are next to each other in the list. |
| 8252 | std::sort(LoadedSlices.begin(), LoadedSlices.end(), LoadedSliceSorter()); |
| 8253 | const TargetLowering &TLI = LoadedSlices[0].DAG->getTargetLoweringInfo(); |
| 8254 | // First (resp. Second) is the first (resp. Second) potentially candidate |
| 8255 | // to be placed in a paired load. |
| 8256 | const LoadedSlice *First = NULL; |
| 8257 | const LoadedSlice *Second = NULL; |
| 8258 | for (unsigned CurrSlice = 0; CurrSlice < NumberOfSlices; ++CurrSlice, |
| 8259 | // Set the beginning of the pair. |
| 8260 | First = Second) { |
| 8261 | |
| 8262 | Second = &LoadedSlices[CurrSlice]; |
| 8263 | |
| 8264 | // If First is NULL, it means we start a new pair. |
| 8265 | // Get to the next slice. |
| 8266 | if (!First) |
| 8267 | continue; |
| 8268 | |
| 8269 | EVT LoadedType = First->getLoadedType(); |
| 8270 | |
| 8271 | // If the types of the slices are different, we cannot pair them. |
| 8272 | if (LoadedType != Second->getLoadedType()) |
| 8273 | continue; |
| 8274 | |
| 8275 | // Check if the target supplies paired loads for this type. |
| 8276 | unsigned RequiredAlignment = 0; |
| 8277 | if (!TLI.hasPairedLoad(LoadedType, RequiredAlignment)) { |
| 8278 | // move to the next pair, this type is hopeless. |
| 8279 | Second = NULL; |
| 8280 | continue; |
| 8281 | } |
| 8282 | // Check if we meet the alignment requirement. |
| 8283 | if (RequiredAlignment > First->getAlignment()) |
| 8284 | continue; |
| 8285 | |
| 8286 | // Check that both loads are next to each other in memory. |
| 8287 | if (!areSlicesNextToEachOther(*First, *Second)) |
| 8288 | continue; |
| 8289 | |
| 8290 | assert(GlobalLSCost.Loads > 0 && "We save more loads than we created!"); |
| 8291 | --GlobalLSCost.Loads; |
| 8292 | // Move to the next pair. |
| 8293 | Second = NULL; |
| 8294 | } |
| 8295 | } |
| 8296 | |
| 8297 | /// \brief Check the profitability of all involved LoadedSlice. |
| 8298 | /// Currently, it is considered profitable if there is exactly two |
| 8299 | /// involved slices (1) which are (2) next to each other in memory, and |
| 8300 | /// whose cost (\see LoadedSlice::Cost) is smaller than the original load (3). |
| 8301 | /// |
| 8302 | /// Note: The order of the elements in \p LoadedSlices may be modified, but not |
| 8303 | /// the elements themselves. |
| 8304 | /// |
| 8305 | /// FIXME: When the cost model will be mature enough, we can relax |
| 8306 | /// constraints (1) and (2). |
| 8307 | static bool isSlicingProfitable(SmallVectorImpl<LoadedSlice> &LoadedSlices, |
| 8308 | const APInt &UsedBits, bool ForCodeSize) { |
| 8309 | unsigned NumberOfSlices = LoadedSlices.size(); |
| 8310 | if (StressLoadSlicing) |
| 8311 | return NumberOfSlices > 1; |
| 8312 | |
| 8313 | // Check (1). |
| 8314 | if (NumberOfSlices != 2) |
| 8315 | return false; |
| 8316 | |
| 8317 | // Check (2). |
| 8318 | if (!areUsedBitsDense(UsedBits)) |
| 8319 | return false; |
| 8320 | |
| 8321 | // Check (3). |
| 8322 | LoadedSlice::Cost OrigCost(ForCodeSize), GlobalSlicingCost(ForCodeSize); |
| 8323 | // The original code has one big load. |
| 8324 | OrigCost.Loads = 1; |
| 8325 | for (unsigned CurrSlice = 0; CurrSlice < NumberOfSlices; ++CurrSlice) { |
| 8326 | const LoadedSlice &LS = LoadedSlices[CurrSlice]; |
| 8327 | // Accumulate the cost of all the slices. |
| 8328 | LoadedSlice::Cost SliceCost(LS, ForCodeSize); |
| 8329 | GlobalSlicingCost += SliceCost; |
| 8330 | |
| 8331 | // Account as cost in the original configuration the gain obtained |
| 8332 | // with the current slices. |
| 8333 | OrigCost.addSliceGain(LS); |
| 8334 | } |
| 8335 | |
| 8336 | // If the target supports paired load, adjust the cost accordingly. |
| 8337 | adjustCostForPairing(LoadedSlices, GlobalSlicingCost); |
| 8338 | return OrigCost > GlobalSlicingCost; |
| 8339 | } |
| 8340 | |
| 8341 | /// \brief If the given load, \p LI, is used only by trunc or trunc(lshr) |
| 8342 | /// operations, split it in the various pieces being extracted. |
| 8343 | /// |
| 8344 | /// This sort of thing is introduced by SROA. |
| 8345 | /// This slicing takes care not to insert overlapping loads. |
| 8346 | /// \pre LI is a simple load (i.e., not an atomic or volatile load). |
| 8347 | bool DAGCombiner::SliceUpLoad(SDNode *N) { |
| 8348 | if (Level < AfterLegalizeDAG) |
| 8349 | return false; |
| 8350 | |
| 8351 | LoadSDNode *LD = cast<LoadSDNode>(N); |
| 8352 | if (LD->isVolatile() || !ISD::isNormalLoad(LD) || |
| 8353 | !LD->getValueType(0).isInteger()) |
| 8354 | return false; |
| 8355 | |
| 8356 | // Keep track of already used bits to detect overlapping values. |
| 8357 | // In that case, we will just abort the transformation. |
| 8358 | APInt UsedBits(LD->getValueSizeInBits(0), 0); |
| 8359 | |
| 8360 | SmallVector<LoadedSlice, 4> LoadedSlices; |
| 8361 | |
| 8362 | // Check if this load is used as several smaller chunks of bits. |
| 8363 | // Basically, look for uses in trunc or trunc(lshr) and record a new chain |
| 8364 | // of computation for each trunc. |
| 8365 | for (SDNode::use_iterator UI = LD->use_begin(), UIEnd = LD->use_end(); |
| 8366 | UI != UIEnd; ++UI) { |
| 8367 | // Skip the uses of the chain. |
| 8368 | if (UI.getUse().getResNo() != 0) |
| 8369 | continue; |
| 8370 | |
| 8371 | SDNode *User = *UI; |
| 8372 | unsigned Shift = 0; |
| 8373 | |
| 8374 | // Check if this is a trunc(lshr). |
| 8375 | if (User->getOpcode() == ISD::SRL && User->hasOneUse() && |
| 8376 | isa<ConstantSDNode>(User->getOperand(1))) { |
| 8377 | Shift = cast<ConstantSDNode>(User->getOperand(1))->getZExtValue(); |
| 8378 | User = *User->use_begin(); |
| 8379 | } |
| 8380 | |
| 8381 | // At this point, User is a Truncate, iff we encountered, trunc or |
| 8382 | // trunc(lshr). |
| 8383 | if (User->getOpcode() != ISD::TRUNCATE) |
| 8384 | return false; |
| 8385 | |
| 8386 | // The width of the type must be a power of 2 and greater than 8-bits. |
| 8387 | // Otherwise the load cannot be represented in LLVM IR. |
| Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 8388 | // Moreover, if we shifted with a non-8-bits multiple, the slice |
| Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 8389 | // will be across several bytes. We do not support that. |
| Quentin Colombet | de0e062 | 2013-10-11 18:29:42 +0000 | [diff] [blame] | 8390 | unsigned Width = User->getValueSizeInBits(0); |
| 8391 | if (Width < 8 || !isPowerOf2_32(Width) || (Shift & 0x7)) |
| 8392 | return 0; |
| 8393 | |
| 8394 | // Build the slice for this chain of computations. |
| 8395 | LoadedSlice LS(User, LD, Shift, &DAG); |
| 8396 | APInt CurrentUsedBits = LS.getUsedBits(); |
| 8397 | |
| 8398 | // Check if this slice overlaps with another. |
| 8399 | if ((CurrentUsedBits & UsedBits) != 0) |
| 8400 | return false; |
| 8401 | // Update the bits used globally. |
| 8402 | UsedBits |= CurrentUsedBits; |
| 8403 | |
| 8404 | // Check if the new slice would be legal. |
| 8405 | if (!LS.isLegal()) |
| 8406 | return false; |
| 8407 | |
| 8408 | // Record the slice. |
| 8409 | LoadedSlices.push_back(LS); |
| 8410 | } |
| 8411 | |
| 8412 | // Abort slicing if it does not seem to be profitable. |
| 8413 | if (!isSlicingProfitable(LoadedSlices, UsedBits, ForCodeSize)) |
| 8414 | return false; |
| 8415 | |
| 8416 | ++SlicedLoads; |
| 8417 | |
| 8418 | // Rewrite each chain to use an independent load. |
| 8419 | // By construction, each chain can be represented by a unique load. |
| 8420 | |
| 8421 | // Prepare the argument for the new token factor for all the slices. |
| 8422 | SmallVector<SDValue, 8> ArgChains; |
| 8423 | for (SmallVectorImpl<LoadedSlice>::const_iterator |
| 8424 | LSIt = LoadedSlices.begin(), |
| 8425 | LSItEnd = LoadedSlices.end(); |
| 8426 | LSIt != LSItEnd; ++LSIt) { |
| 8427 | SDValue SliceInst = LSIt->loadSlice(); |
| 8428 | CombineTo(LSIt->Inst, SliceInst, true); |
| 8429 | if (SliceInst.getNode()->getOpcode() != ISD::LOAD) |
| 8430 | SliceInst = SliceInst.getOperand(0); |
| 8431 | assert(SliceInst->getOpcode() == ISD::LOAD && |
| 8432 | "It takes more than a zext to get to the loaded slice!!"); |
| 8433 | ArgChains.push_back(SliceInst.getValue(1)); |
| 8434 | } |
| 8435 | |
| 8436 | SDValue Chain = DAG.getNode(ISD::TokenFactor, SDLoc(LD), MVT::Other, |
| 8437 | &ArgChains[0], ArgChains.size()); |
| 8438 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain); |
| 8439 | return true; |
| 8440 | } |
| 8441 | |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8442 | /// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the |
| 8443 | /// load is having specific bytes cleared out. If so, return the byte size |
| 8444 | /// being masked out and the shift amount. |
| 8445 | static std::pair<unsigned, unsigned> |
| 8446 | CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) { |
| 8447 | std::pair<unsigned, unsigned> Result(0, 0); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8448 | |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8449 | // Check for the structure we're looking for. |
| 8450 | if (V->getOpcode() != ISD::AND || |
| 8451 | !isa<ConstantSDNode>(V->getOperand(1)) || |
| 8452 | !ISD::isNormalLoad(V->getOperand(0).getNode())) |
| 8453 | return Result; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8454 | |
| Chris Lattner | 3245afd | 2010-04-15 06:10:49 +0000 | [diff] [blame] | 8455 | // Check the chain and pointer. |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8456 | LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0)); |
| Chris Lattner | 3245afd | 2010-04-15 06:10:49 +0000 | [diff] [blame] | 8457 | if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer. |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8458 | |
| Chris Lattner | 3245afd | 2010-04-15 06:10:49 +0000 | [diff] [blame] | 8459 | // The store should be chained directly to the load or be an operand of a |
| 8460 | // tokenfactor. |
| 8461 | if (LD == Chain.getNode()) |
| 8462 | ; // ok. |
| 8463 | else if (Chain->getOpcode() != ISD::TokenFactor) |
| 8464 | return Result; // Fail. |
| 8465 | else { |
| 8466 | bool isOk = false; |
| 8467 | for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i) |
| 8468 | if (Chain->getOperand(i).getNode() == LD) { |
| 8469 | isOk = true; |
| 8470 | break; |
| 8471 | } |
| 8472 | if (!isOk) return Result; |
| 8473 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8474 | |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8475 | // This only handles simple types. |
| 8476 | if (V.getValueType() != MVT::i16 && |
| 8477 | V.getValueType() != MVT::i32 && |
| 8478 | V.getValueType() != MVT::i64) |
| 8479 | return Result; |
| 8480 | |
| 8481 | // Check the constant mask. Invert it so that the bits being masked out are |
| 8482 | // 0 and the bits being kept are 1. Use getSExtValue so that leading bits |
| 8483 | // follow the sign bit for uniformity. |
| 8484 | uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue(); |
| Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 8485 | unsigned NotMaskLZ = countLeadingZeros(NotMask); |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8486 | if (NotMaskLZ & 7) return Result; // Must be multiple of a byte. |
| Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 8487 | unsigned NotMaskTZ = countTrailingZeros(NotMask); |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8488 | if (NotMaskTZ & 7) return Result; // Must be multiple of a byte. |
| 8489 | if (NotMaskLZ == 64) return Result; // All zero mask. |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8490 | |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8491 | // See if we have a continuous run of bits. If so, we have 0*1+0* |
| 8492 | if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64) |
| 8493 | return Result; |
| 8494 | |
| 8495 | // Adjust NotMaskLZ down to be from the actual size of the int instead of i64. |
| 8496 | if (V.getValueType() != MVT::i64 && NotMaskLZ) |
| 8497 | NotMaskLZ -= 64-V.getValueSizeInBits(); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8498 | |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8499 | unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8; |
| 8500 | switch (MaskedBytes) { |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8501 | case 1: |
| 8502 | case 2: |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8503 | case 4: break; |
| 8504 | default: return Result; // All one mask, or 5-byte mask. |
| 8505 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8506 | |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8507 | // Verify that the first bit starts at a multiple of mask so that the access |
| 8508 | // is aligned the same as the access width. |
| 8509 | if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8510 | |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8511 | Result.first = MaskedBytes; |
| 8512 | Result.second = NotMaskTZ/8; |
| 8513 | return Result; |
| 8514 | } |
| 8515 | |
| 8516 | |
| 8517 | /// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that |
| 8518 | /// provides a value as specified by MaskInfo. If so, replace the specified |
| 8519 | /// store with a narrower store of truncated IVal. |
| 8520 | static SDNode * |
| 8521 | ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo, |
| 8522 | SDValue IVal, StoreSDNode *St, |
| 8523 | DAGCombiner *DC) { |
| 8524 | unsigned NumBytes = MaskInfo.first; |
| 8525 | unsigned ByteShift = MaskInfo.second; |
| 8526 | SelectionDAG &DAG = DC->getDAG(); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8527 | |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8528 | // Check to see if IVal is all zeros in the part being masked in by the 'or' |
| 8529 | // that uses this. If not, this is not a replacement. |
| 8530 | APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(), |
| 8531 | ByteShift*8, (ByteShift+NumBytes)*8); |
| 8532 | if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8533 | |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8534 | // Check that it is legal on the target to do this. It is legal if the new |
| 8535 | // VT we're shrinking to (i8/i16/i32) is legal or we're still before type |
| 8536 | // legalization. |
| 8537 | MVT VT = MVT::getIntegerVT(NumBytes*8); |
| 8538 | if (!DC->isTypeLegal(VT)) |
| 8539 | return 0; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8540 | |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8541 | // Okay, we can do this! Replace the 'St' store with a store of IVal that is |
| 8542 | // shifted by ByteShift and truncated down to NumBytes. |
| 8543 | if (ByteShift) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8544 | IVal = DAG.getNode(ISD::SRL, SDLoc(IVal), IVal.getValueType(), IVal, |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 8545 | DAG.getConstant(ByteShift*8, |
| 8546 | DC->getShiftAmountTy(IVal.getValueType()))); |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8547 | |
| 8548 | // Figure out the offset for the store and the alignment of the access. |
| 8549 | unsigned StOffset; |
| 8550 | unsigned NewAlign = St->getAlignment(); |
| 8551 | |
| 8552 | if (DAG.getTargetLoweringInfo().isLittleEndian()) |
| 8553 | StOffset = ByteShift; |
| 8554 | else |
| 8555 | StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8556 | |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8557 | SDValue Ptr = St->getBasePtr(); |
| 8558 | if (StOffset) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8559 | Ptr = DAG.getNode(ISD::ADD, SDLoc(IVal), Ptr.getValueType(), |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8560 | Ptr, DAG.getConstant(StOffset, Ptr.getValueType())); |
| 8561 | NewAlign = MinAlign(NewAlign, StOffset); |
| 8562 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8563 | |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8564 | // Truncate down to the new size. |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8565 | IVal = DAG.getNode(ISD::TRUNCATE, SDLoc(IVal), VT, IVal); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8566 | |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8567 | ++OpsNarrowed; |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8568 | return DAG.getStore(St->getChain(), SDLoc(St), IVal, Ptr, |
| Chris Lattner | 676c61d | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 8569 | St->getPointerInfo().getWithOffset(StOffset), |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8570 | false, false, NewAlign).getNode(); |
| 8571 | } |
| 8572 | |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8573 | |
| 8574 | /// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is |
| 8575 | /// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some |
| 8576 | /// of the loaded bits, try narrowing the load and store if it would end up |
| 8577 | /// being a win for performance or code size. |
| 8578 | SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) { |
| 8579 | StoreSDNode *ST = cast<StoreSDNode>(N); |
| Evan Cheng | 6673ff0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 8580 | if (ST->isVolatile()) |
| 8581 | return SDValue(); |
| 8582 | |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8583 | SDValue Chain = ST->getChain(); |
| 8584 | SDValue Value = ST->getValue(); |
| 8585 | SDValue Ptr = ST->getBasePtr(); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 8586 | EVT VT = Value.getValueType(); |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8587 | |
| 8588 | if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse()) |
| Evan Cheng | 6673ff0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 8589 | return SDValue(); |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8590 | |
| 8591 | unsigned Opc = Value.getOpcode(); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8592 | |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8593 | // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst |
| 8594 | // is a byte mask indicating a consecutive number of bytes, check to see if |
| 8595 | // Y is known to provide just those bytes. If so, we try to replace the |
| 8596 | // load + replace + store sequence with a single (narrower) store, which makes |
| 8597 | // the load dead. |
| 8598 | if (Opc == ISD::OR) { |
| 8599 | std::pair<unsigned, unsigned> MaskedLoad; |
| 8600 | MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain); |
| 8601 | if (MaskedLoad.first) |
| 8602 | if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad, |
| 8603 | Value.getOperand(1), ST,this)) |
| 8604 | return SDValue(NewST, 0); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8605 | |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8606 | // Or is commutative, so try swapping X and Y. |
| 8607 | MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain); |
| 8608 | if (MaskedLoad.first) |
| 8609 | if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad, |
| 8610 | Value.getOperand(0), ST,this)) |
| 8611 | return SDValue(NewST, 0); |
| 8612 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8613 | |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8614 | if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) || |
| 8615 | Value.getOperand(1).getOpcode() != ISD::Constant) |
| Evan Cheng | 6673ff0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 8616 | return SDValue(); |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8617 | |
| 8618 | SDValue N0 = Value.getOperand(0); |
| Dan Gohman | 3c9b5f3 | 2010-09-02 21:18:42 +0000 | [diff] [blame] | 8619 | if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() && |
| 8620 | Chain == SDValue(N0.getNode(), 1)) { |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8621 | LoadSDNode *LD = cast<LoadSDNode>(N0); |
| Chris Lattner | f72c3c0 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 8622 | if (LD->getBasePtr() != Ptr || |
| 8623 | LD->getPointerInfo().getAddrSpace() != |
| 8624 | ST->getPointerInfo().getAddrSpace()) |
| Evan Cheng | 6673ff0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 8625 | return SDValue(); |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8626 | |
| 8627 | // Find the type to narrow it the load / op / store to. |
| 8628 | SDValue N1 = Value.getOperand(1); |
| 8629 | unsigned BitWidth = N1.getValueSizeInBits(); |
| 8630 | APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue(); |
| 8631 | if (Opc == ISD::AND) |
| 8632 | Imm ^= APInt::getAllOnesValue(BitWidth); |
| Evan Cheng | 86cdb4b | 2009-05-28 23:52:18 +0000 | [diff] [blame] | 8633 | if (Imm == 0 || Imm.isAllOnesValue()) |
| 8634 | return SDValue(); |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8635 | unsigned ShAmt = Imm.countTrailingZeros(); |
| 8636 | unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1; |
| 8637 | unsigned NewBW = NextPowerOf2(MSB - ShAmt); |
| Owen Anderson | 117c9e8 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 8638 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW); |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8639 | while (NewBW < BitWidth && |
| Evan Cheng | 6673ff0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 8640 | !(TLI.isOperationLegalOrCustom(Opc, NewVT) && |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8641 | TLI.isNarrowingProfitable(VT, NewVT))) { |
| 8642 | NewBW = NextPowerOf2(NewBW); |
| Owen Anderson | 117c9e8 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 8643 | NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW); |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8644 | } |
| Evan Cheng | 6673ff0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 8645 | if (NewBW >= BitWidth) |
| 8646 | return SDValue(); |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8647 | |
| 8648 | // If the lsb changed does not start at the type bitwidth boundary, |
| 8649 | // start at the previous one. |
| 8650 | if (ShAmt % NewBW) |
| 8651 | ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW; |
| Manman Ren | 82751a1 | 2012-12-12 01:13:50 +0000 | [diff] [blame] | 8652 | APInt Mask = APInt::getBitsSet(BitWidth, ShAmt, |
| 8653 | std::min(BitWidth, ShAmt + NewBW)); |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8654 | if ((Imm & Mask) == Imm) { |
| 8655 | APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW); |
| 8656 | if (Opc == ISD::AND) |
| 8657 | NewImm ^= APInt::getAllOnesValue(NewBW); |
| 8658 | uint64_t PtrOff = ShAmt / 8; |
| 8659 | // For big endian targets, we need to adjust the offset to the pointer to |
| 8660 | // load the correct bytes. |
| 8661 | if (TLI.isBigEndian()) |
| Evan Cheng | 6673ff0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 8662 | PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff; |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8663 | |
| 8664 | unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff); |
| Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 8665 | Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext()); |
| Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 8666 | if (NewAlign < TLI.getDataLayout()->getABITypeAlignment(NewVTTy)) |
| Evan Cheng | 6673ff0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 8667 | return SDValue(); |
| 8668 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8669 | SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LD), |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8670 | Ptr.getValueType(), Ptr, |
| 8671 | DAG.getConstant(PtrOff, Ptr.getValueType())); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8672 | SDValue NewLD = DAG.getLoad(NewVT, SDLoc(N0), |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8673 | LD->getChain(), NewPtr, |
| Chris Lattner | f72c3c0 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 8674 | LD->getPointerInfo().getWithOffset(PtrOff), |
| David Greene | 39c6d01 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 8675 | LD->isVolatile(), LD->isNonTemporal(), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 8676 | LD->isInvariant(), NewAlign, |
| 8677 | LD->getTBAAInfo()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8678 | SDValue NewVal = DAG.getNode(Opc, SDLoc(Value), NewVT, NewLD, |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8679 | DAG.getConstant(NewImm, NewVT)); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8680 | SDValue NewST = DAG.getStore(Chain, SDLoc(N), |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8681 | NewVal, NewPtr, |
| Chris Lattner | f72c3c0 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 8682 | ST->getPointerInfo().getWithOffset(PtrOff), |
| David Greene | 39c6d01 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 8683 | false, false, NewAlign); |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8684 | |
| 8685 | AddToWorkList(NewPtr.getNode()); |
| 8686 | AddToWorkList(NewLD.getNode()); |
| 8687 | AddToWorkList(NewVal.getNode()); |
| 8688 | WorkListRemover DeadNodes(*this); |
| Jakob Stoklund Olesen | beb9469 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 8689 | DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1)); |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8690 | ++OpsNarrowed; |
| 8691 | return NewST; |
| 8692 | } |
| 8693 | } |
| 8694 | |
| Evan Cheng | 6673ff0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 8695 | return SDValue(); |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8696 | } |
| 8697 | |
| Evan Cheng | d42641c | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 8698 | /// TransformFPLoadStorePair - For a given floating point load / store pair, |
| 8699 | /// if the load value isn't used by any other operations, then consider |
| 8700 | /// transforming the pair to integer load / store operations if the target |
| 8701 | /// deems the transformation profitable. |
| 8702 | SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) { |
| 8703 | StoreSDNode *ST = cast<StoreSDNode>(N); |
| 8704 | SDValue Chain = ST->getChain(); |
| 8705 | SDValue Value = ST->getValue(); |
| 8706 | if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) && |
| 8707 | Value.hasOneUse() && |
| 8708 | Chain == SDValue(Value.getNode(), 1)) { |
| 8709 | LoadSDNode *LD = cast<LoadSDNode>(Value); |
| 8710 | EVT VT = LD->getMemoryVT(); |
| 8711 | if (!VT.isFloatingPoint() || |
| 8712 | VT != ST->getMemoryVT() || |
| 8713 | LD->isNonTemporal() || |
| 8714 | ST->isNonTemporal() || |
| 8715 | LD->getPointerInfo().getAddrSpace() != 0 || |
| 8716 | ST->getPointerInfo().getAddrSpace() != 0) |
| 8717 | return SDValue(); |
| 8718 | |
| 8719 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits()); |
| 8720 | if (!TLI.isOperationLegal(ISD::LOAD, IntVT) || |
| 8721 | !TLI.isOperationLegal(ISD::STORE, IntVT) || |
| 8722 | !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) || |
| 8723 | !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT)) |
| 8724 | return SDValue(); |
| 8725 | |
| 8726 | unsigned LDAlign = LD->getAlignment(); |
| 8727 | unsigned STAlign = ST->getAlignment(); |
| Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 8728 | Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext()); |
| Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 8729 | unsigned ABIAlign = TLI.getDataLayout()->getABITypeAlignment(IntVTTy); |
| Evan Cheng | d42641c | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 8730 | if (LDAlign < ABIAlign || STAlign < ABIAlign) |
| 8731 | return SDValue(); |
| 8732 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8733 | SDValue NewLD = DAG.getLoad(IntVT, SDLoc(Value), |
| Evan Cheng | d42641c | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 8734 | LD->getChain(), LD->getBasePtr(), |
| 8735 | LD->getPointerInfo(), |
| Pete Cooper | 82cd9e8 | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 8736 | false, false, false, LDAlign); |
| Evan Cheng | d42641c | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 8737 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8738 | SDValue NewST = DAG.getStore(NewLD.getValue(1), SDLoc(N), |
| Evan Cheng | d42641c | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 8739 | NewLD, ST->getBasePtr(), |
| 8740 | ST->getPointerInfo(), |
| 8741 | false, false, STAlign); |
| 8742 | |
| 8743 | AddToWorkList(NewLD.getNode()); |
| 8744 | AddToWorkList(NewST.getNode()); |
| 8745 | WorkListRemover DeadNodes(*this); |
| Jakob Stoklund Olesen | beb9469 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 8746 | DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1)); |
| Evan Cheng | d42641c | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 8747 | ++LdStFP2Int; |
| 8748 | return NewST; |
| 8749 | } |
| 8750 | |
| 8751 | return SDValue(); |
| 8752 | } |
| 8753 | |
| Arnold Schwaighofer | 6752366 | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8754 | /// Helper struct to parse and store a memory address as base + index + offset. |
| 8755 | /// We ignore sign extensions when it is safe to do so. |
| 8756 | /// The following two expressions are not equivalent. To differentiate we need |
| 8757 | /// to store whether there was a sign extension involved in the index |
| 8758 | /// computation. |
| 8759 | /// (load (i64 add (i64 copyfromreg %c) |
| 8760 | /// (i64 signextend (add (i8 load %index) |
| 8761 | /// (i8 1)))) |
| 8762 | /// vs |
| 8763 | /// |
| 8764 | /// (load (i64 add (i64 copyfromreg %c) |
| 8765 | /// (i64 signextend (i32 add (i32 signextend (i8 load %index)) |
| 8766 | /// (i32 1))))) |
| 8767 | struct BaseIndexOffset { |
| 8768 | SDValue Base; |
| 8769 | SDValue Index; |
| 8770 | int64_t Offset; |
| 8771 | bool IsIndexSignExt; |
| 8772 | |
| 8773 | BaseIndexOffset() : Offset(0), IsIndexSignExt(false) {} |
| 8774 | |
| 8775 | BaseIndexOffset(SDValue Base, SDValue Index, int64_t Offset, |
| 8776 | bool IsIndexSignExt) : |
| 8777 | Base(Base), Index(Index), Offset(Offset), IsIndexSignExt(IsIndexSignExt) {} |
| 8778 | |
| 8779 | bool equalBaseIndex(const BaseIndexOffset &Other) { |
| 8780 | return Other.Base == Base && Other.Index == Index && |
| 8781 | Other.IsIndexSignExt == IsIndexSignExt; |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8782 | } |
| 8783 | |
| Arnold Schwaighofer | 6752366 | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8784 | /// Parses tree in Ptr for base, index, offset addresses. |
| 8785 | static BaseIndexOffset match(SDValue Ptr) { |
| 8786 | bool IsIndexSignExt = false; |
| 8787 | |
| Juergen Ributzka | 3db39dc | 2013-08-21 21:53:38 +0000 | [diff] [blame] | 8788 | // We only can pattern match BASE + INDEX + OFFSET. If Ptr is not an ADD |
| 8789 | // instruction, then it could be just the BASE or everything else we don't |
| 8790 | // know how to handle. Just use Ptr as BASE and give up. |
| Arnold Schwaighofer | 6752366 | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8791 | if (Ptr->getOpcode() != ISD::ADD) |
| 8792 | return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt); |
| 8793 | |
| Juergen Ributzka | 3db39dc | 2013-08-21 21:53:38 +0000 | [diff] [blame] | 8794 | // We know that we have at least an ADD instruction. Try to pattern match |
| 8795 | // the simple case of BASE + OFFSET. |
| Arnold Schwaighofer | 6752366 | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8796 | if (isa<ConstantSDNode>(Ptr->getOperand(1))) { |
| 8797 | int64_t Offset = cast<ConstantSDNode>(Ptr->getOperand(1))->getSExtValue(); |
| 8798 | return BaseIndexOffset(Ptr->getOperand(0), SDValue(), Offset, |
| 8799 | IsIndexSignExt); |
| 8800 | } |
| 8801 | |
| Juergen Ributzka | 3db39dc | 2013-08-21 21:53:38 +0000 | [diff] [blame] | 8802 | // Inside a loop the current BASE pointer is calculated using an ADD and a |
| Juergen Ributzka | 11c52c6 | 2013-08-28 22:33:58 +0000 | [diff] [blame] | 8803 | // MUL instruction. In this case Ptr is the actual BASE pointer. |
| Juergen Ributzka | 3db39dc | 2013-08-21 21:53:38 +0000 | [diff] [blame] | 8804 | // (i64 add (i64 %array_ptr) |
| 8805 | // (i64 mul (i64 %induction_var) |
| 8806 | // (i64 %element_size))) |
| Juergen Ributzka | 11c52c6 | 2013-08-28 22:33:58 +0000 | [diff] [blame] | 8807 | if (Ptr->getOperand(1)->getOpcode() == ISD::MUL) |
| Juergen Ributzka | 3db39dc | 2013-08-21 21:53:38 +0000 | [diff] [blame] | 8808 | return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt); |
| Juergen Ributzka | 3db39dc | 2013-08-21 21:53:38 +0000 | [diff] [blame] | 8809 | |
| Arnold Schwaighofer | 6752366 | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8810 | // Look at Base + Index + Offset cases. |
| 8811 | SDValue Base = Ptr->getOperand(0); |
| 8812 | SDValue IndexOffset = Ptr->getOperand(1); |
| 8813 | |
| 8814 | // Skip signextends. |
| 8815 | if (IndexOffset->getOpcode() == ISD::SIGN_EXTEND) { |
| 8816 | IndexOffset = IndexOffset->getOperand(0); |
| 8817 | IsIndexSignExt = true; |
| 8818 | } |
| 8819 | |
| 8820 | // Either the case of Base + Index (no offset) or something else. |
| 8821 | if (IndexOffset->getOpcode() != ISD::ADD) |
| 8822 | return BaseIndexOffset(Base, IndexOffset, 0, IsIndexSignExt); |
| 8823 | |
| 8824 | // Now we have the case of Base + Index + offset. |
| 8825 | SDValue Index = IndexOffset->getOperand(0); |
| 8826 | SDValue Offset = IndexOffset->getOperand(1); |
| 8827 | |
| 8828 | if (!isa<ConstantSDNode>(Offset)) |
| 8829 | return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt); |
| 8830 | |
| 8831 | // Ignore signextends. |
| 8832 | if (Index->getOpcode() == ISD::SIGN_EXTEND) { |
| 8833 | Index = Index->getOperand(0); |
| 8834 | IsIndexSignExt = true; |
| 8835 | } else IsIndexSignExt = false; |
| 8836 | |
| 8837 | int64_t Off = cast<ConstantSDNode>(Offset)->getSExtValue(); |
| 8838 | return BaseIndexOffset(Base, Index, Off, IsIndexSignExt); |
| 8839 | } |
| 8840 | }; |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8841 | |
| 8842 | /// Holds a pointer to an LSBaseSDNode as well as information on where it |
| 8843 | /// is located in a sequence of memory operations connected by a chain. |
| 8844 | struct MemOpLink { |
| 8845 | MemOpLink (LSBaseSDNode *N, int64_t Offset, unsigned Seq): |
| 8846 | MemNode(N), OffsetFromBase(Offset), SequenceNum(Seq) { } |
| 8847 | // Ptr to the mem node. |
| 8848 | LSBaseSDNode *MemNode; |
| 8849 | // Offset from the base ptr. |
| 8850 | int64_t OffsetFromBase; |
| 8851 | // What is the sequence number of this mem node. |
| 8852 | // Lowest mem operand in the DAG starts at zero. |
| 8853 | unsigned SequenceNum; |
| 8854 | }; |
| 8855 | |
| 8856 | /// Sorts store nodes in a link according to their offset from a shared |
| 8857 | // base ptr. |
| 8858 | struct ConsecutiveMemoryChainSorter { |
| 8859 | bool operator()(MemOpLink LHS, MemOpLink RHS) { |
| Stepan Dyatkovskiy | 157bb42 | 2014-01-27 09:18:31 +0000 | [diff] [blame] | 8860 | return |
| 8861 | LHS.OffsetFromBase < RHS.OffsetFromBase || |
| 8862 | (LHS.OffsetFromBase == RHS.OffsetFromBase && |
| 8863 | LHS.SequenceNum > RHS.SequenceNum); |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8864 | } |
| 8865 | }; |
| 8866 | |
| 8867 | bool DAGCombiner::MergeConsecutiveStores(StoreSDNode* St) { |
| 8868 | EVT MemVT = St->getMemoryVT(); |
| 8869 | int64_t ElementSizeBytes = MemVT.getSizeInBits()/8; |
| Nadav Rotem | 495b1a4 | 2013-02-14 18:28:52 +0000 | [diff] [blame] | 8870 | bool NoVectors = DAG.getMachineFunction().getFunction()->getAttributes(). |
| 8871 | hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat); |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8872 | |
| 8873 | // Don't merge vectors into wider inputs. |
| 8874 | if (MemVT.isVector() || !MemVT.isSimple()) |
| 8875 | return false; |
| 8876 | |
| 8877 | // Perform an early exit check. Do not bother looking at stored values that |
| 8878 | // are not constants or loads. |
| 8879 | SDValue StoredVal = St->getValue(); |
| 8880 | bool IsLoadSrc = isa<LoadSDNode>(StoredVal); |
| 8881 | if (!isa<ConstantSDNode>(StoredVal) && !isa<ConstantFPSDNode>(StoredVal) && |
| 8882 | !IsLoadSrc) |
| 8883 | return false; |
| 8884 | |
| 8885 | // Only look at ends of store sequences. |
| 8886 | SDValue Chain = SDValue(St, 1); |
| 8887 | if (Chain->hasOneUse() && Chain->use_begin()->getOpcode() == ISD::STORE) |
| 8888 | return false; |
| 8889 | |
| Arnold Schwaighofer | 6752366 | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8890 | // This holds the base pointer, index, and the offset in bytes from the base |
| 8891 | // pointer. |
| 8892 | BaseIndexOffset BasePtr = BaseIndexOffset::match(St->getBasePtr()); |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8893 | |
| 8894 | // We must have a base and an offset. |
| Arnold Schwaighofer | 6752366 | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8895 | if (!BasePtr.Base.getNode()) |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8896 | return false; |
| 8897 | |
| 8898 | // Do not handle stores to undef base pointers. |
| Arnold Schwaighofer | 6752366 | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8899 | if (BasePtr.Base.getOpcode() == ISD::UNDEF) |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8900 | return false; |
| 8901 | |
| Nadav Rotem | 307d767 | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 8902 | // Save the LoadSDNodes that we find in the chain. |
| 8903 | // We need to make sure that these nodes do not interfere with |
| 8904 | // any of the store nodes. |
| 8905 | SmallVector<LSBaseSDNode*, 8> AliasLoadNodes; |
| 8906 | |
| 8907 | // Save the StoreSDNodes that we find in the chain. |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8908 | SmallVector<MemOpLink, 8> StoreNodes; |
| Nadav Rotem | 307d767 | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 8909 | |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8910 | // Walk up the chain and look for nodes with offsets from the same |
| 8911 | // base pointer. Stop when reaching an instruction with a different kind |
| 8912 | // or instruction which has a different base pointer. |
| 8913 | unsigned Seq = 0; |
| 8914 | StoreSDNode *Index = St; |
| 8915 | while (Index) { |
| 8916 | // If the chain has more than one use, then we can't reorder the mem ops. |
| 8917 | if (Index != St && !SDValue(Index, 1)->hasOneUse()) |
| 8918 | break; |
| 8919 | |
| 8920 | // Find the base pointer and offset for this memory node. |
| Arnold Schwaighofer | 6752366 | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8921 | BaseIndexOffset Ptr = BaseIndexOffset::match(Index->getBasePtr()); |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8922 | |
| 8923 | // Check that the base pointer is the same as the original one. |
| Arnold Schwaighofer | 6752366 | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8924 | if (!Ptr.equalBaseIndex(BasePtr)) |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8925 | break; |
| 8926 | |
| 8927 | // Check that the alignment is the same. |
| 8928 | if (Index->getAlignment() != St->getAlignment()) |
| 8929 | break; |
| 8930 | |
| 8931 | // The memory operands must not be volatile. |
| 8932 | if (Index->isVolatile() || Index->isIndexed()) |
| 8933 | break; |
| 8934 | |
| 8935 | // No truncation. |
| 8936 | if (StoreSDNode *St = dyn_cast<StoreSDNode>(Index)) |
| 8937 | if (St->isTruncatingStore()) |
| 8938 | break; |
| 8939 | |
| 8940 | // The stored memory type must be the same. |
| 8941 | if (Index->getMemoryVT() != MemVT) |
| 8942 | break; |
| 8943 | |
| 8944 | // We do not allow unaligned stores because we want to prevent overriding |
| 8945 | // stores. |
| 8946 | if (Index->getAlignment()*8 != MemVT.getSizeInBits()) |
| 8947 | break; |
| 8948 | |
| 8949 | // We found a potential memory operand to merge. |
| Arnold Schwaighofer | 6752366 | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8950 | StoreNodes.push_back(MemOpLink(Index, Ptr.Offset, Seq++)); |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8951 | |
| Nadav Rotem | 307d767 | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 8952 | // Find the next memory operand in the chain. If the next operand in the |
| 8953 | // chain is a store then move up and continue the scan with the next |
| 8954 | // memory operand. If the next operand is a load save it and use alias |
| 8955 | // information to check if it interferes with anything. |
| 8956 | SDNode *NextInChain = Index->getChain().getNode(); |
| 8957 | while (1) { |
| Nadav Rotem | ac450eb | 2012-12-06 17:34:13 +0000 | [diff] [blame] | 8958 | if (StoreSDNode *STn = dyn_cast<StoreSDNode>(NextInChain)) { |
| Nadav Rotem | 307d767 | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 8959 | // We found a store node. Use it for the next iteration. |
| Nadav Rotem | ac450eb | 2012-12-06 17:34:13 +0000 | [diff] [blame] | 8960 | Index = STn; |
| Nadav Rotem | 307d767 | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 8961 | break; |
| 8962 | } else if (LoadSDNode *Ldn = dyn_cast<LoadSDNode>(NextInChain)) { |
| Bill Wendling | 9200bb0 | 2013-11-25 18:05:22 +0000 | [diff] [blame] | 8963 | if (Ldn->isVolatile()) { |
| 8964 | Index = NULL; |
| 8965 | break; |
| 8966 | } |
| 8967 | |
| Nadav Rotem | 307d767 | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 8968 | // Save the load node for later. Continue the scan. |
| 8969 | AliasLoadNodes.push_back(Ldn); |
| 8970 | NextInChain = Ldn->getChain().getNode(); |
| 8971 | continue; |
| 8972 | } else { |
| 8973 | Index = NULL; |
| 8974 | break; |
| 8975 | } |
| 8976 | } |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8977 | } |
| 8978 | |
| 8979 | // Check if there is anything to merge. |
| 8980 | if (StoreNodes.size() < 2) |
| 8981 | return false; |
| 8982 | |
| 8983 | // Sort the memory operands according to their distance from the base pointer. |
| 8984 | std::sort(StoreNodes.begin(), StoreNodes.end(), |
| 8985 | ConsecutiveMemoryChainSorter()); |
| 8986 | |
| 8987 | // Scan the memory operations on the chain and find the first non-consecutive |
| 8988 | // store memory address. |
| 8989 | unsigned LastConsecutiveStore = 0; |
| 8990 | int64_t StartAddress = StoreNodes[0].OffsetFromBase; |
| Nadav Rotem | ac450eb | 2012-12-06 17:34:13 +0000 | [diff] [blame] | 8991 | for (unsigned i = 0, e = StoreNodes.size(); i < e; ++i) { |
| 8992 | |
| 8993 | // Check that the addresses are consecutive starting from the second |
| 8994 | // element in the list of stores. |
| 8995 | if (i > 0) { |
| 8996 | int64_t CurrAddress = StoreNodes[i].OffsetFromBase; |
| 8997 | if (CurrAddress - StartAddress != (ElementSizeBytes * i)) |
| 8998 | break; |
| 8999 | } |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9000 | |
| Nadav Rotem | 307d767 | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 9001 | bool Alias = false; |
| 9002 | // Check if this store interferes with any of the loads that we found. |
| 9003 | for (unsigned ld = 0, lde = AliasLoadNodes.size(); ld < lde; ++ld) |
| 9004 | if (isAlias(AliasLoadNodes[ld], StoreNodes[i].MemNode)) { |
| 9005 | Alias = true; |
| 9006 | break; |
| 9007 | } |
| Nadav Rotem | 307d767 | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 9008 | // We found a load that alias with this store. Stop the sequence. |
| 9009 | if (Alias) |
| 9010 | break; |
| 9011 | |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9012 | // Mark this node as useful. |
| 9013 | LastConsecutiveStore = i; |
| 9014 | } |
| 9015 | |
| 9016 | // The node with the lowest store address. |
| 9017 | LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode; |
| 9018 | |
| 9019 | // Store the constants into memory as one consecutive store. |
| 9020 | if (!IsLoadSrc) { |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9021 | unsigned LastLegalType = 0; |
| Nadav Rotem | b27777f | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 9022 | unsigned LastLegalVectorType = 0; |
| 9023 | bool NonZero = false; |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9024 | for (unsigned i=0; i<LastConsecutiveStore+1; ++i) { |
| 9025 | StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode); |
| 9026 | SDValue StoredVal = St->getValue(); |
| Nadav Rotem | b27777f | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 9027 | |
| 9028 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(StoredVal)) { |
| Benjamin Kramer | 62f7fb9 | 2012-10-05 18:19:44 +0000 | [diff] [blame] | 9029 | NonZero |= !C->isNullValue(); |
| Nadav Rotem | b27777f | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 9030 | } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(StoredVal)) { |
| Benjamin Kramer | 62f7fb9 | 2012-10-05 18:19:44 +0000 | [diff] [blame] | 9031 | NonZero |= !C->getConstantFPValue()->isNullValue(); |
| Nadav Rotem | b27777f | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 9032 | } else { |
| Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 9033 | // Non-constant. |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9034 | break; |
| Nadav Rotem | b27777f | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 9035 | } |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9036 | |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9037 | // Find a legal type for the constant store. |
| 9038 | unsigned StoreBW = (i+1) * ElementSizeBytes * 8; |
| 9039 | EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW); |
| 9040 | if (TLI.isTypeLegal(StoreTy)) |
| 9041 | LastLegalType = i+1; |
| Arnold Schwaighofer | d6c6e86 | 2013-04-02 15:58:51 +0000 | [diff] [blame] | 9042 | // Or check whether a truncstore is legal. |
| 9043 | else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) == |
| 9044 | TargetLowering::TypePromoteInteger) { |
| 9045 | EVT LegalizedStoredValueTy = |
| 9046 | TLI.getTypeToTransformTo(*DAG.getContext(), StoredVal.getValueType()); |
| 9047 | if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy)) |
| 9048 | LastLegalType = i+1; |
| 9049 | } |
| Nadav Rotem | b27777f | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 9050 | |
| 9051 | // Find a legal type for the vector store. |
| 9052 | EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1); |
| 9053 | if (TLI.isTypeLegal(Ty)) |
| 9054 | LastLegalVectorType = i + 1; |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9055 | } |
| 9056 | |
| Bob Wilson | 3365b80 | 2012-12-20 01:36:20 +0000 | [diff] [blame] | 9057 | // We only use vectors if the constant is known to be zero and the |
| 9058 | // function is not marked with the noimplicitfloat attribute. |
| Nadav Rotem | 495b1a4 | 2013-02-14 18:28:52 +0000 | [diff] [blame] | 9059 | if (NonZero || NoVectors) |
| Nadav Rotem | b27777f | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 9060 | LastLegalVectorType = 0; |
| 9061 | |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9062 | // Check if we found a legal integer type to store. |
| Nadav Rotem | b27777f | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 9063 | if (LastLegalType == 0 && LastLegalVectorType == 0) |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9064 | return false; |
| 9065 | |
| Nadav Rotem | 495b1a4 | 2013-02-14 18:28:52 +0000 | [diff] [blame] | 9066 | bool UseVector = (LastLegalVectorType > LastLegalType) && !NoVectors; |
| Nadav Rotem | b27777f | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 9067 | unsigned NumElem = UseVector ? LastLegalVectorType : LastLegalType; |
| 9068 | |
| 9069 | // Make sure we have something to merge. |
| 9070 | if (NumElem < 2) |
| 9071 | return false; |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9072 | |
| 9073 | unsigned EarliestNodeUsed = 0; |
| 9074 | for (unsigned i=0; i < NumElem; ++i) { |
| 9075 | // Find a chain for the new wide-store operand. Notice that some |
| 9076 | // of the store nodes that we found may not be selected for inclusion |
| 9077 | // in the wide store. The chain we use needs to be the chain of the |
| 9078 | // earliest store node which is *used* and replaced by the wide store. |
| 9079 | if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum) |
| 9080 | EarliestNodeUsed = i; |
| 9081 | } |
| 9082 | |
| 9083 | // The earliest Node in the DAG. |
| 9084 | LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode; |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9085 | SDLoc DL(StoreNodes[0].MemNode); |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9086 | |
| Nadav Rotem | b27777f | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 9087 | SDValue StoredVal; |
| 9088 | if (UseVector) { |
| 9089 | // Find a legal type for the vector store. |
| 9090 | EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem); |
| 9091 | assert(TLI.isTypeLegal(Ty) && "Illegal vector store"); |
| 9092 | StoredVal = DAG.getConstant(0, Ty); |
| 9093 | } else { |
| 9094 | unsigned StoreBW = NumElem * ElementSizeBytes * 8; |
| 9095 | APInt StoreInt(StoreBW, 0); |
| 9096 | |
| 9097 | // Construct a single integer constant which is made of the smaller |
| 9098 | // constant inputs. |
| 9099 | bool IsLE = TLI.isLittleEndian(); |
| 9100 | for (unsigned i = 0; i < NumElem ; ++i) { |
| 9101 | unsigned Idx = IsLE ?(NumElem - 1 - i) : i; |
| 9102 | StoreSDNode *St = cast<StoreSDNode>(StoreNodes[Idx].MemNode); |
| 9103 | SDValue Val = St->getValue(); |
| 9104 | StoreInt<<=ElementSizeBytes*8; |
| 9105 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) { |
| 9106 | StoreInt|=C->getAPIntValue().zext(StoreBW); |
| 9107 | } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Val)) { |
| 9108 | StoreInt|= C->getValueAPF().bitcastToAPInt().zext(StoreBW); |
| 9109 | } else { |
| 9110 | assert(false && "Invalid constant element type"); |
| 9111 | } |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9112 | } |
| Nadav Rotem | b27777f | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 9113 | |
| 9114 | // Create the new Load and Store operations. |
| 9115 | EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW); |
| 9116 | StoredVal = DAG.getConstant(StoreInt, StoreTy); |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9117 | } |
| 9118 | |
| Nadav Rotem | b27777f | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 9119 | SDValue NewStore = DAG.getStore(EarliestOp->getChain(), DL, StoredVal, |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9120 | FirstInChain->getBasePtr(), |
| 9121 | FirstInChain->getPointerInfo(), |
| 9122 | false, false, |
| 9123 | FirstInChain->getAlignment()); |
| 9124 | |
| 9125 | // Replace the first store with the new store |
| 9126 | CombineTo(EarliestOp, NewStore); |
| 9127 | // Erase all other stores. |
| 9128 | for (unsigned i = 0; i < NumElem ; ++i) { |
| 9129 | if (StoreNodes[i].MemNode == EarliestOp) |
| 9130 | continue; |
| 9131 | StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode); |
| Rafael Espindola | c79532d | 2012-11-14 05:08:56 +0000 | [diff] [blame] | 9132 | // ReplaceAllUsesWith will replace all uses that existed when it was |
| 9133 | // called, but graph optimizations may cause new ones to appear. For |
| 9134 | // example, the case in pr14333 looks like |
| 9135 | // |
| 9136 | // St's chain -> St -> another store -> X |
| 9137 | // |
| 9138 | // And the only difference from St to the other store is the chain. |
| 9139 | // When we change it's chain to be St's chain they become identical, |
| 9140 | // get CSEed and the net result is that X is now a use of St. |
| 9141 | // Since we know that St is redundant, just iterate. |
| 9142 | while (!St->use_empty()) |
| 9143 | DAG.ReplaceAllUsesWith(SDValue(St, 0), St->getChain()); |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9144 | removeFromWorkList(St); |
| 9145 | DAG.DeleteNode(St); |
| 9146 | } |
| 9147 | |
| 9148 | return true; |
| 9149 | } |
| 9150 | |
| 9151 | // Below we handle the case of multiple consecutive stores that |
| 9152 | // come from multiple consecutive loads. We merge them into a single |
| 9153 | // wide load and a single wide store. |
| 9154 | |
| 9155 | // Look for load nodes which are used by the stored values. |
| 9156 | SmallVector<MemOpLink, 8> LoadNodes; |
| 9157 | |
| 9158 | // Find acceptable loads. Loads need to have the same chain (token factor), |
| 9159 | // must not be zext, volatile, indexed, and they must be consecutive. |
| Arnold Schwaighofer | 6752366 | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 9160 | BaseIndexOffset LdBasePtr; |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9161 | for (unsigned i=0; i<LastConsecutiveStore+1; ++i) { |
| 9162 | StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode); |
| 9163 | LoadSDNode *Ld = dyn_cast<LoadSDNode>(St->getValue()); |
| 9164 | if (!Ld) break; |
| 9165 | |
| 9166 | // Loads must only have one use. |
| 9167 | if (!Ld->hasNUsesOfValue(1, 0)) |
| 9168 | break; |
| 9169 | |
| 9170 | // Check that the alignment is the same as the stores. |
| 9171 | if (Ld->getAlignment() != St->getAlignment()) |
| 9172 | break; |
| 9173 | |
| 9174 | // The memory operands must not be volatile. |
| 9175 | if (Ld->isVolatile() || Ld->isIndexed()) |
| 9176 | break; |
| 9177 | |
| 9178 | // We do not accept ext loads. |
| 9179 | if (Ld->getExtensionType() != ISD::NON_EXTLOAD) |
| 9180 | break; |
| 9181 | |
| 9182 | // The stored memory type must be the same. |
| 9183 | if (Ld->getMemoryVT() != MemVT) |
| 9184 | break; |
| 9185 | |
| Arnold Schwaighofer | 6752366 | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 9186 | BaseIndexOffset LdPtr = BaseIndexOffset::match(Ld->getBasePtr()); |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9187 | // If this is not the first ptr that we check. |
| Arnold Schwaighofer | 6752366 | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 9188 | if (LdBasePtr.Base.getNode()) { |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9189 | // The base ptr must be the same. |
| Arnold Schwaighofer | 6752366 | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 9190 | if (!LdPtr.equalBaseIndex(LdBasePtr)) |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9191 | break; |
| 9192 | } else { |
| 9193 | // Check that all other base pointers are the same as this one. |
| Arnold Schwaighofer | 6752366 | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 9194 | LdBasePtr = LdPtr; |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9195 | } |
| 9196 | |
| 9197 | // We found a potential memory operand to merge. |
| Arnold Schwaighofer | 6752366 | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 9198 | LoadNodes.push_back(MemOpLink(Ld, LdPtr.Offset, 0)); |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9199 | } |
| 9200 | |
| 9201 | if (LoadNodes.size() < 2) |
| 9202 | return false; |
| 9203 | |
| 9204 | // Scan the memory operations on the chain and find the first non-consecutive |
| 9205 | // load memory address. These variables hold the index in the store node |
| 9206 | // array. |
| 9207 | unsigned LastConsecutiveLoad = 0; |
| 9208 | // This variable refers to the size and not index in the array. |
| 9209 | unsigned LastLegalVectorType = 0; |
| 9210 | unsigned LastLegalIntegerType = 0; |
| 9211 | StartAddress = LoadNodes[0].OffsetFromBase; |
| Nadav Rotem | ac92066 | 2012-10-03 19:30:31 +0000 | [diff] [blame] | 9212 | SDValue FirstChain = LoadNodes[0].MemNode->getChain(); |
| 9213 | for (unsigned i = 1; i < LoadNodes.size(); ++i) { |
| 9214 | // All loads much share the same chain. |
| 9215 | if (LoadNodes[i].MemNode->getChain() != FirstChain) |
| 9216 | break; |
| Nadav Rotem | 495b1a4 | 2013-02-14 18:28:52 +0000 | [diff] [blame] | 9217 | |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9218 | int64_t CurrAddress = LoadNodes[i].OffsetFromBase; |
| 9219 | if (CurrAddress - StartAddress != (ElementSizeBytes * i)) |
| 9220 | break; |
| 9221 | LastConsecutiveLoad = i; |
| 9222 | |
| 9223 | // Find a legal type for the vector store. |
| 9224 | EVT StoreTy = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1); |
| 9225 | if (TLI.isTypeLegal(StoreTy)) |
| 9226 | LastLegalVectorType = i + 1; |
| 9227 | |
| 9228 | // Find a legal type for the integer store. |
| 9229 | unsigned StoreBW = (i+1) * ElementSizeBytes * 8; |
| 9230 | StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW); |
| 9231 | if (TLI.isTypeLegal(StoreTy)) |
| 9232 | LastLegalIntegerType = i + 1; |
| Arnold Schwaighofer | d6c6e86 | 2013-04-02 15:58:51 +0000 | [diff] [blame] | 9233 | // Or check whether a truncstore and extload is legal. |
| 9234 | else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) == |
| 9235 | TargetLowering::TypePromoteInteger) { |
| 9236 | EVT LegalizedStoredValueTy = |
| 9237 | TLI.getTypeToTransformTo(*DAG.getContext(), StoreTy); |
| 9238 | if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy) && |
| 9239 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, StoreTy) && |
| 9240 | TLI.isLoadExtLegal(ISD::SEXTLOAD, StoreTy) && |
| 9241 | TLI.isLoadExtLegal(ISD::EXTLOAD, StoreTy)) |
| 9242 | LastLegalIntegerType = i+1; |
| 9243 | } |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9244 | } |
| 9245 | |
| 9246 | // Only use vector types if the vector type is larger than the integer type. |
| 9247 | // If they are the same, use integers. |
| Nadav Rotem | 495b1a4 | 2013-02-14 18:28:52 +0000 | [diff] [blame] | 9248 | bool UseVectorTy = LastLegalVectorType > LastLegalIntegerType && !NoVectors; |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9249 | unsigned LastLegalType = std::max(LastLegalVectorType, LastLegalIntegerType); |
| 9250 | |
| 9251 | // We add +1 here because the LastXXX variables refer to location while |
| 9252 | // the NumElem refers to array/index size. |
| 9253 | unsigned NumElem = std::min(LastConsecutiveStore, LastConsecutiveLoad) + 1; |
| 9254 | NumElem = std::min(LastLegalType, NumElem); |
| 9255 | |
| 9256 | if (NumElem < 2) |
| 9257 | return false; |
| 9258 | |
| 9259 | // The earliest Node in the DAG. |
| 9260 | unsigned EarliestNodeUsed = 0; |
| 9261 | LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode; |
| 9262 | for (unsigned i=1; i<NumElem; ++i) { |
| 9263 | // Find a chain for the new wide-store operand. Notice that some |
| 9264 | // of the store nodes that we found may not be selected for inclusion |
| 9265 | // in the wide store. The chain we use needs to be the chain of the |
| 9266 | // earliest store node which is *used* and replaced by the wide store. |
| 9267 | if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum) |
| 9268 | EarliestNodeUsed = i; |
| 9269 | } |
| 9270 | |
| 9271 | // Find if it is better to use vectors or integers to load and store |
| 9272 | // to memory. |
| 9273 | EVT JointMemOpVT; |
| 9274 | if (UseVectorTy) { |
| 9275 | JointMemOpVT = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem); |
| 9276 | } else { |
| 9277 | unsigned StoreBW = NumElem * ElementSizeBytes * 8; |
| 9278 | JointMemOpVT = EVT::getIntegerVT(*DAG.getContext(), StoreBW); |
| 9279 | } |
| 9280 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9281 | SDLoc LoadDL(LoadNodes[0].MemNode); |
| 9282 | SDLoc StoreDL(StoreNodes[0].MemNode); |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9283 | |
| 9284 | LoadSDNode *FirstLoad = cast<LoadSDNode>(LoadNodes[0].MemNode); |
| 9285 | SDValue NewLoad = DAG.getLoad(JointMemOpVT, LoadDL, |
| 9286 | FirstLoad->getChain(), |
| 9287 | FirstLoad->getBasePtr(), |
| 9288 | FirstLoad->getPointerInfo(), |
| 9289 | false, false, false, |
| 9290 | FirstLoad->getAlignment()); |
| 9291 | |
| 9292 | SDValue NewStore = DAG.getStore(EarliestOp->getChain(), StoreDL, NewLoad, |
| 9293 | FirstInChain->getBasePtr(), |
| 9294 | FirstInChain->getPointerInfo(), false, false, |
| 9295 | FirstInChain->getAlignment()); |
| 9296 | |
| Nadav Rotem | ac92066 | 2012-10-03 19:30:31 +0000 | [diff] [blame] | 9297 | // Replace one of the loads with the new load. |
| 9298 | LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[0].MemNode); |
| 9299 | DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), |
| 9300 | SDValue(NewLoad.getNode(), 1)); |
| 9301 | |
| 9302 | // Remove the rest of the load chains. |
| 9303 | for (unsigned i = 1; i < NumElem ; ++i) { |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9304 | // Replace all chain users of the old load nodes with the chain of the new |
| 9305 | // load node. |
| 9306 | LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[i].MemNode); |
| Nadav Rotem | ac92066 | 2012-10-03 19:30:31 +0000 | [diff] [blame] | 9307 | DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), Ld->getChain()); |
| 9308 | } |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9309 | |
| Nadav Rotem | ac92066 | 2012-10-03 19:30:31 +0000 | [diff] [blame] | 9310 | // Replace the first store with the new store. |
| 9311 | CombineTo(EarliestOp, NewStore); |
| 9312 | // Erase all other stores. |
| 9313 | for (unsigned i = 0; i < NumElem ; ++i) { |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9314 | // Remove all Store nodes. |
| 9315 | if (StoreNodes[i].MemNode == EarliestOp) |
| 9316 | continue; |
| 9317 | StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode); |
| 9318 | DAG.ReplaceAllUsesOfValueWith(SDValue(St, 0), St->getChain()); |
| 9319 | removeFromWorkList(St); |
| 9320 | DAG.DeleteNode(St); |
| 9321 | } |
| 9322 | |
| 9323 | return true; |
| 9324 | } |
| 9325 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9326 | SDValue DAGCombiner::visitSTORE(SDNode *N) { |
| Evan Cheng | ab51cf2 | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 9327 | StoreSDNode *ST = cast<StoreSDNode>(N); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9328 | SDValue Chain = ST->getChain(); |
| 9329 | SDValue Value = ST->getValue(); |
| 9330 | SDValue Ptr = ST->getBasePtr(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9331 | |
| Evan Cheng | a4cf58a | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 9332 | // If this is a store of a bit convert, store the input value if the |
| Evan Cheng | f325c2a | 2007-05-09 21:49:47 +0000 | [diff] [blame] | 9333 | // resultant store does not need a higher alignment than the original. |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9334 | if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() && |
| Chris Lattner | 1ea55cf | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 9335 | ST->isUnindexed()) { |
| Dan Gohman | e7fe80f | 2009-02-20 23:29:13 +0000 | [diff] [blame] | 9336 | unsigned OrigAlign = ST->getAlignment(); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 9337 | EVT SVT = Value.getOperand(0).getValueType(); |
| Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 9338 | unsigned Align = TLI.getDataLayout()-> |
| Owen Anderson | 117c9e8 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 9339 | getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext())); |
| Duncan Sands | 8651e9c | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 9340 | if (Align <= OrigAlign && |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 9341 | ((!LegalOperations && !ST->isVolatile()) || |
| Dan Gohman | 4aa1846 | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 9342 | TLI.isOperationLegalOrCustom(ISD::STORE, SVT))) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9343 | return DAG.getStore(Chain, SDLoc(N), Value.getOperand(0), |
| Chris Lattner | 676c61d | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 9344 | Ptr, ST->getPointerInfo(), ST->isVolatile(), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 9345 | ST->isNonTemporal(), OrigAlign, |
| 9346 | ST->getTBAAInfo()); |
| Jim Laskey | d07be23 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 9347 | } |
| Owen Anderson | a519284 | 2011-04-14 17:30:49 +0000 | [diff] [blame] | 9348 | |
| Chris Lattner | 41c80e8 | 2011-04-09 02:32:02 +0000 | [diff] [blame] | 9349 | // Turn 'store undef, Ptr' -> nothing. |
| 9350 | if (Value.getOpcode() == ISD::UNDEF && ST->isUnindexed()) |
| 9351 | return Chain; |
| Duncan Sands | 8651e9c | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 9352 | |
| Nate Begeman | 8e20c76 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 9353 | // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' |
| Nate Begeman | 8e20c76 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 9354 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) { |
| Duncan Sands | 8651e9c | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 9355 | // NOTE: If the original store is volatile, this transform must not increase |
| 9356 | // the number of stores. For example, on x86-32 an f64 can be stored in one |
| 9357 | // processor operation but an i64 (which is not legal) requires two. So the |
| 9358 | // transform should not be done in this case. |
| Evan Cheng | 2183698 | 2006-12-11 17:25:19 +0000 | [diff] [blame] | 9359 | if (Value.getOpcode() != ISD::TargetConstantFP) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9360 | SDValue Tmp; |
| Craig Topper | d9c2783 | 2013-08-15 02:44:19 +0000 | [diff] [blame] | 9361 | switch (CFP->getSimpleValueType(0).SimpleTy) { |
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 9362 | default: llvm_unreachable("Unknown FP type"); |
| Pete Cooper | 5b61422 | 2012-06-21 18:00:39 +0000 | [diff] [blame] | 9363 | case MVT::f16: // We don't do this for these yet. |
| 9364 | case MVT::f80: |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 9365 | case MVT::f128: |
| 9366 | case MVT::ppcf128: |
| Dale Johannesen | af12b57 | 2007-09-18 18:36:59 +0000 | [diff] [blame] | 9367 | break; |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 9368 | case MVT::f32: |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 9369 | if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) || |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 9370 | TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) { |
| Dale Johannesen | 028084e | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 9371 | Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF(). |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 9372 | bitcastToAPInt().getZExtValue(), MVT::i32); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9373 | return DAG.getStore(Chain, SDLoc(N), Tmp, |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 9374 | Ptr, ST->getMemOperand()); |
| Chris Lattner | b7524b6 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 9375 | } |
| 9376 | break; |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 9377 | case MVT::f64: |
| Chris Lattner | 4041ab6 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 9378 | if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations && |
| Dan Gohman | 4aa1846 | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 9379 | !ST->isVolatile()) || |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 9380 | TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) { |
| Dale Johannesen | 54306fe | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 9381 | Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt(). |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 9382 | getZExtValue(), MVT::i64); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9383 | return DAG.getStore(Chain, SDLoc(N), Tmp, |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 9384 | Ptr, ST->getMemOperand()); |
| Chris Lattner | 41c80e8 | 2011-04-09 02:32:02 +0000 | [diff] [blame] | 9385 | } |
| Owen Anderson | a519284 | 2011-04-14 17:30:49 +0000 | [diff] [blame] | 9386 | |
| Chris Lattner | 41c80e8 | 2011-04-09 02:32:02 +0000 | [diff] [blame] | 9387 | if (!ST->isVolatile() && |
| 9388 | TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) { |
| Duncan Sands | 1826ded | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 9389 | // Many FP stores are not made apparent until after legalize, e.g. for |
| Chris Lattner | b7524b6 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 9390 | // argument passing. Since this is so common, custom legalize the |
| 9391 | // 64-bit integer store into two 32-bit stores. |
| Dale Johannesen | 54306fe | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 9392 | uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue(); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 9393 | SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32); |
| 9394 | SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32); |
| Duncan Sands | 7377f5f | 2008-02-11 10:37:04 +0000 | [diff] [blame] | 9395 | if (TLI.isBigEndian()) std::swap(Lo, Hi); |
| Chris Lattner | b7524b6 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 9396 | |
| Dan Gohman | 2af3063 | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 9397 | unsigned Alignment = ST->getAlignment(); |
| 9398 | bool isVolatile = ST->isVolatile(); |
| David Greene | 39c6d01 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 9399 | bool isNonTemporal = ST->isNonTemporal(); |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 9400 | const MDNode *TBAAInfo = ST->getTBAAInfo(); |
| Dan Gohman | 2af3063 | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 9401 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9402 | SDValue St0 = DAG.getStore(Chain, SDLoc(ST), Lo, |
| Chris Lattner | 676c61d | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 9403 | Ptr, ST->getPointerInfo(), |
| David Greene | 39c6d01 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 9404 | isVolatile, isNonTemporal, |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 9405 | ST->getAlignment(), TBAAInfo); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9406 | Ptr = DAG.getNode(ISD::ADD, SDLoc(N), Ptr.getValueType(), Ptr, |
| Chris Lattner | b7524b6 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 9407 | DAG.getConstant(4, Ptr.getValueType())); |
| Duncan Sands | 1826ded | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 9408 | Alignment = MinAlign(Alignment, 4U); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9409 | SDValue St1 = DAG.getStore(Chain, SDLoc(ST), Hi, |
| Chris Lattner | 676c61d | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 9410 | Ptr, ST->getPointerInfo().getWithOffset(4), |
| 9411 | isVolatile, isNonTemporal, |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 9412 | Alignment, TBAAInfo); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9413 | return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other, |
| Bill Wendling | 27d9dd4 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9414 | St0, St1); |
| Chris Lattner | b7524b6 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 9415 | } |
| Bill Wendling | 27d9dd4 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9416 | |
| Chris Lattner | b7524b6 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 9417 | break; |
| Evan Cheng | 2183698 | 2006-12-11 17:25:19 +0000 | [diff] [blame] | 9418 | } |
| Nate Begeman | 8e20c76 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 9419 | } |
| Nate Begeman | 8e20c76 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 9420 | } |
| 9421 | |
| Evan Cheng | 43cd9e3 | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 9422 | // Try to infer better alignment information than the store already has. |
| 9423 | if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) { |
| Evan Cheng | 4a5b204 | 2011-11-28 22:37:34 +0000 | [diff] [blame] | 9424 | if (unsigned Align = DAG.InferPtrAlignment(Ptr)) { |
| 9425 | if (Align > ST->getAlignment()) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9426 | return DAG.getTruncStore(Chain, SDLoc(N), Value, |
| Evan Cheng | 4a5b204 | 2011-11-28 22:37:34 +0000 | [diff] [blame] | 9427 | Ptr, ST->getPointerInfo(), ST->getMemoryVT(), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 9428 | ST->isVolatile(), ST->isNonTemporal(), Align, |
| 9429 | ST->getTBAAInfo()); |
| Evan Cheng | 43cd9e3 | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 9430 | } |
| 9431 | } |
| 9432 | |
| Evan Cheng | d42641c | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 9433 | // Try transforming a pair floating point load / store ops to integer |
| 9434 | // load / store ops. |
| 9435 | SDValue NewST = TransformFPLoadStorePair(N); |
| 9436 | if (NewST.getNode()) |
| 9437 | return NewST; |
| 9438 | |
| Hal Finkel | 5ef4dcc | 2013-08-29 03:29:55 +0000 | [diff] [blame] | 9439 | bool UseAA = CombinerAA.getNumOccurrences() > 0 ? CombinerAA : |
| 9440 | TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA(); |
| Hal Finkel | 9b2617a | 2014-01-25 17:32:39 +0000 | [diff] [blame] | 9441 | #ifndef NDEBUG |
| 9442 | if (CombinerAAOnlyFunc.getNumOccurrences() && |
| 9443 | CombinerAAOnlyFunc != DAG.getMachineFunction().getName()) |
| 9444 | UseAA = false; |
| 9445 | #endif |
| Hal Finkel | ccc18e1 | 2014-01-24 18:25:26 +0000 | [diff] [blame] | 9446 | if (UseAA && ST->isUnindexed()) { |
| Jim Laskey | d07be23 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 9447 | // Walk up chain skipping non-aliasing memory nodes. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9448 | SDValue BetterChain = FindBetterChain(N, Chain); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9449 | |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 9450 | // If there is a better chain. |
| Jim Laskey | d07be23 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 9451 | if (Chain != BetterChain) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9452 | SDValue ReplStore; |
| Nate Begeman | 879d8f1 | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 9453 | |
| 9454 | // Replace the chain to avoid dependency. |
| Jim Laskey | 3bf4f3b | 2006-10-14 12:14:27 +0000 | [diff] [blame] | 9455 | if (ST->isTruncatingStore()) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9456 | ReplStore = DAG.getTruncStore(BetterChain, SDLoc(N), Value, Ptr, |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 9457 | ST->getMemoryVT(), ST->getMemOperand()); |
| Jim Laskey | 3bf4f3b | 2006-10-14 12:14:27 +0000 | [diff] [blame] | 9458 | } else { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9459 | ReplStore = DAG.getStore(BetterChain, SDLoc(N), Value, Ptr, |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 9460 | ST->getMemOperand()); |
| Jim Laskey | 3bf4f3b | 2006-10-14 12:14:27 +0000 | [diff] [blame] | 9461 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9462 | |
| Jim Laskey | d07be23 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 9463 | // Create token to keep both nodes around. |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9464 | SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N), |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 9465 | MVT::Other, Chain, ReplStore); |
| Bill Wendling | 27d9dd4 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9466 | |
| Nate Begeman | 879d8f1 | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 9467 | // Make sure the new and old chains are cleaned up. |
| 9468 | AddToWorkList(Token.getNode()); |
| 9469 | |
| Jim Laskey | dcf983c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 9470 | // Don't add users to work list. |
| 9471 | return CombineTo(N, Token, false); |
| Jim Laskey | d07be23 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 9472 | } |
| Jim Laskey | 5d19d59 | 2006-09-21 16:28:59 +0000 | [diff] [blame] | 9473 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9474 | |
| Evan Cheng | 3315770 | 2006-11-05 09:31:14 +0000 | [diff] [blame] | 9475 | // Try transforming N to an indexed store. |
| Evan Cheng | 60c6846 | 2006-11-07 09:03:05 +0000 | [diff] [blame] | 9476 | if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N)) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9477 | return SDValue(N, 0); |
| Evan Cheng | 3315770 | 2006-11-05 09:31:14 +0000 | [diff] [blame] | 9478 | |
| Chris Lattner | 3f9c6a7 | 2007-12-29 06:26:16 +0000 | [diff] [blame] | 9479 | // FIXME: is there such a thing as a truncating indexed store? |
| Chris Lattner | 1ea55cf | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 9480 | if (ST->isTruncatingStore() && ST->isUnindexed() && |
| Nadav Rotem | d2d9bdb | 2011-06-15 11:19:12 +0000 | [diff] [blame] | 9481 | Value.getValueType().isInteger()) { |
| Chris Lattner | 5e6fe05 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 9482 | // See if we can simplify the input to this truncstore with knowledge that |
| 9483 | // only the low bits are being used. For example: |
| 9484 | // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8" |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9485 | SDValue Shorter = |
| Dan Gohman | 1f372ed | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 9486 | GetDemandedBits(Value, |
| Nadav Rotem | d2d9bdb | 2011-06-15 11:19:12 +0000 | [diff] [blame] | 9487 | APInt::getLowBitsSet( |
| 9488 | Value.getValueType().getScalarType().getSizeInBits(), |
| 9489 | ST->getMemoryVT().getScalarType().getSizeInBits())); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9490 | AddToWorkList(Value.getNode()); |
| 9491 | if (Shorter.getNode()) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9492 | return DAG.getTruncStore(Chain, SDLoc(N), Shorter, |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 9493 | Ptr, ST->getMemoryVT(), ST->getMemOperand()); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9494 | |
| Chris Lattner | f47e306 | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 9495 | // Otherwise, see if we can simplify the operation with |
| 9496 | // SimplifyDemandedBits, which only works if the value has a single use. |
| Dan Gohman | ae2b6fb | 2008-02-27 00:25:32 +0000 | [diff] [blame] | 9497 | if (SimplifyDemandedBits(Value, |
| Eric Christopher | d9e8eac | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 9498 | APInt::getLowBitsSet( |
| 9499 | Value.getValueType().getScalarType().getSizeInBits(), |
| 9500 | ST->getMemoryVT().getScalarType().getSizeInBits()))) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9501 | return SDValue(N, 0); |
| Chris Lattner | 5e6fe05 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 9502 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9503 | |
| Chris Lattner | 3f9c6a7 | 2007-12-29 06:26:16 +0000 | [diff] [blame] | 9504 | // If this is a load followed by a store to the same location, then the store |
| 9505 | // is dead/noop. |
| 9506 | if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) { |
| Dan Gohman | 47a7d6f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 9507 | if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() && |
| Chris Lattner | 1ea55cf | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 9508 | ST->isUnindexed() && !ST->isVolatile() && |
| Chris Lattner | 51b01bf | 2008-01-08 23:08:06 +0000 | [diff] [blame] | 9509 | // There can't be any side effects between the load and store, such as |
| 9510 | // a call or store. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9511 | Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) { |
| Chris Lattner | 3f9c6a7 | 2007-12-29 06:26:16 +0000 | [diff] [blame] | 9512 | // The store is dead, remove it. |
| 9513 | return Chain; |
| 9514 | } |
| 9515 | } |
| Duncan Sands | 8651e9c | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 9516 | |
| Chris Lattner | 1ea55cf | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 9517 | // If this is an FP_ROUND or TRUNC followed by a store, fold this into a |
| 9518 | // truncating store. We can do this even if this is already a truncstore. |
| 9519 | if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE) |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9520 | && Value.getNode()->hasOneUse() && ST->isUnindexed() && |
| Chris Lattner | 1ea55cf | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 9521 | TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(), |
| Dan Gohman | 47a7d6f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 9522 | ST->getMemoryVT())) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9523 | return DAG.getTruncStore(Chain, SDLoc(N), Value.getOperand(0), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 9524 | Ptr, ST->getMemoryVT(), ST->getMemOperand()); |
| Chris Lattner | 1ea55cf | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 9525 | } |
| Duncan Sands | 8651e9c | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 9526 | |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9527 | // Only perform this optimization before the types are legal, because we |
| Nadav Rotem | b27777f | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 9528 | // don't want to perform this optimization on every DAGCombine invocation. |
| Nadav Rotem | 1157e14 | 2012-12-02 17:14:09 +0000 | [diff] [blame] | 9529 | if (!LegalTypes) { |
| 9530 | bool EverChanged = false; |
| 9531 | |
| 9532 | do { |
| 9533 | // There can be multiple store sequences on the same chain. |
| 9534 | // Keep trying to merge store sequences until we are unable to do so |
| 9535 | // or until we merge the last store on the chain. |
| 9536 | bool Changed = MergeConsecutiveStores(ST); |
| 9537 | EverChanged |= Changed; |
| 9538 | if (!Changed) break; |
| 9539 | } while (ST->getOpcode() != ISD::DELETED_NODE); |
| 9540 | |
| 9541 | if (EverChanged) |
| 9542 | return SDValue(N, 0); |
| 9543 | } |
| Nadav Rotem | 7cbc12a | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9544 | |
| Evan Cheng | a9cda8a | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 9545 | return ReduceLoadOpStoreWidth(N); |
| Chris Lattner | 04c7370 | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 9546 | } |
| 9547 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9548 | SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) { |
| 9549 | SDValue InVec = N->getOperand(0); |
| 9550 | SDValue InVal = N->getOperand(1); |
| 9551 | SDValue EltNo = N->getOperand(2); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9552 | SDLoc dl(N); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9553 | |
| Bob Wilson | 4260395 | 2010-05-19 23:42:58 +0000 | [diff] [blame] | 9554 | // If the inserted element is an UNDEF, just use the input vector. |
| 9555 | if (InVal.getOpcode() == ISD::UNDEF) |
| 9556 | return InVec; |
| 9557 | |
| Nadav Rotem | db2f548 | 2011-02-12 14:40:33 +0000 | [diff] [blame] | 9558 | EVT VT = InVec.getValueType(); |
| 9559 | |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 9560 | // If we can't generate a legal BUILD_VECTOR, exit |
| Nadav Rotem | db2f548 | 2011-02-12 14:40:33 +0000 | [diff] [blame] | 9561 | if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) |
| 9562 | return SDValue(); |
| 9563 | |
| Eli Friedman | b7910b7 | 2011-09-09 21:04:06 +0000 | [diff] [blame] | 9564 | // Check that we know which element is being inserted |
| 9565 | if (!isa<ConstantSDNode>(EltNo)) |
| 9566 | return SDValue(); |
| 9567 | unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9568 | |
| Eli Friedman | b7910b7 | 2011-09-09 21:04:06 +0000 | [diff] [blame] | 9569 | // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially |
| 9570 | // be converted to a BUILD_VECTOR). Fill in the Ops vector with the |
| 9571 | // vector elements. |
| 9572 | SmallVector<SDValue, 8> Ops; |
| Quentin Colombet | 6bf4baa | 2013-07-30 00:24:09 +0000 | [diff] [blame] | 9573 | // Do not combine these two vectors if the output vector will not replace |
| 9574 | // the input vector. |
| 9575 | if (InVec.getOpcode() == ISD::BUILD_VECTOR && InVec.hasOneUse()) { |
| Eli Friedman | b7910b7 | 2011-09-09 21:04:06 +0000 | [diff] [blame] | 9576 | Ops.append(InVec.getNode()->op_begin(), |
| 9577 | InVec.getNode()->op_end()); |
| 9578 | } else if (InVec.getOpcode() == ISD::UNDEF) { |
| 9579 | unsigned NElts = VT.getVectorNumElements(); |
| 9580 | Ops.append(NElts, DAG.getUNDEF(InVal.getValueType())); |
| 9581 | } else { |
| 9582 | return SDValue(); |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 9583 | } |
| Eli Friedman | b7910b7 | 2011-09-09 21:04:06 +0000 | [diff] [blame] | 9584 | |
| 9585 | // Insert the element |
| 9586 | if (Elt < Ops.size()) { |
| 9587 | // All the operands of BUILD_VECTOR must have the same type; |
| 9588 | // we enforce that here. |
| 9589 | EVT OpVT = Ops[0].getValueType(); |
| 9590 | if (InVal.getValueType() != OpVT) |
| 9591 | InVal = OpVT.bitsGT(InVal.getValueType()) ? |
| 9592 | DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) : |
| 9593 | DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal); |
| 9594 | Ops[Elt] = InVal; |
| 9595 | } |
| 9596 | |
| 9597 | // Return the new vector |
| 9598 | return DAG.getNode(ISD::BUILD_VECTOR, dl, |
| 9599 | VT, &Ops[0], Ops.size()); |
| Chris Lattner | 5336a59 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 9600 | } |
| 9601 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9602 | SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) { |
| Mon P Wang | ca6d6de | 2009-01-17 00:07:25 +0000 | [diff] [blame] | 9603 | // (vextract (scalar_to_vector val, 0) -> val |
| 9604 | SDValue InVec = N->getOperand(0); |
| Nadav Rotem | fb6ddee | 2012-01-17 21:44:01 +0000 | [diff] [blame] | 9605 | EVT VT = InVec.getValueType(); |
| 9606 | EVT NVT = N->getValueType(0); |
| Mon P Wang | ca6d6de | 2009-01-17 00:07:25 +0000 | [diff] [blame] | 9607 | |
| Duncan Sands | 6be291a | 2011-05-09 08:03:33 +0000 | [diff] [blame] | 9608 | if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) { |
| 9609 | // Check if the result type doesn't match the inserted element type. A |
| 9610 | // SCALAR_TO_VECTOR may truncate the inserted element and the |
| 9611 | // EXTRACT_VECTOR_ELT may widen the extracted vector. |
| 9612 | SDValue InOp = InVec.getOperand(0); |
| Duncan Sands | 6be291a | 2011-05-09 08:03:33 +0000 | [diff] [blame] | 9613 | if (InOp.getValueType() != NVT) { |
| 9614 | assert(InOp.getValueType().isInteger() && NVT.isInteger()); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9615 | return DAG.getSExtOrTrunc(InOp, SDLoc(InVec), NVT); |
| Duncan Sands | 6be291a | 2011-05-09 08:03:33 +0000 | [diff] [blame] | 9616 | } |
| 9617 | return InOp; |
| 9618 | } |
| Evan Cheng | 1120279a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9619 | |
| Nadav Rotem | fb6ddee | 2012-01-17 21:44:01 +0000 | [diff] [blame] | 9620 | SDValue EltNo = N->getOperand(1); |
| 9621 | bool ConstEltNo = isa<ConstantSDNode>(EltNo); |
| 9622 | |
| 9623 | // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT. |
| 9624 | // We only perform this optimization before the op legalization phase because |
| Nadav Rotem | 841c9a8 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 9625 | // we may introduce new vector instructions which are not backed by TD |
| 9626 | // patterns. For example on AVX, extracting elements from a wide vector |
| 9627 | // without using extract_subvector. |
| Nadav Rotem | fb6ddee | 2012-01-17 21:44:01 +0000 | [diff] [blame] | 9628 | if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE |
| 9629 | && ConstEltNo && !LegalOperations) { |
| 9630 | int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); |
| 9631 | int NumElem = VT.getVectorNumElements(); |
| 9632 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(InVec); |
| 9633 | // Find the new index to extract from. |
| 9634 | int OrigElt = SVOp->getMaskElt(Elt); |
| 9635 | |
| 9636 | // Extracting an undef index is undef. |
| 9637 | if (OrigElt == -1) |
| 9638 | return DAG.getUNDEF(NVT); |
| 9639 | |
| 9640 | // Select the right vector half to extract from. |
| 9641 | if (OrigElt < NumElem) { |
| 9642 | InVec = InVec->getOperand(0); |
| 9643 | } else { |
| 9644 | InVec = InVec->getOperand(1); |
| 9645 | OrigElt -= NumElem; |
| 9646 | } |
| 9647 | |
| Tom Stellard | d42c594 | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 9648 | EVT IndexTy = TLI.getVectorIdxTy(); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9649 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N), NVT, |
| Jim Grosbach | 92f6adc | 2012-05-08 20:56:07 +0000 | [diff] [blame] | 9650 | InVec, DAG.getConstant(OrigElt, IndexTy)); |
| Nadav Rotem | fb6ddee | 2012-01-17 21:44:01 +0000 | [diff] [blame] | 9651 | } |
| 9652 | |
| Evan Cheng | 1120279a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9653 | // Perform only after legalization to ensure build_vector / vector_shuffle |
| 9654 | // optimizations have already been done. |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 9655 | if (!LegalOperations) return SDValue(); |
| Evan Cheng | 1120279a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9656 | |
| Mon P Wang | ca6d6de | 2009-01-17 00:07:25 +0000 | [diff] [blame] | 9657 | // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size) |
| 9658 | // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size) |
| 9659 | // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr) |
| Evan Cheng | 0de312d | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 9660 | |
| Nadav Rotem | fb6ddee | 2012-01-17 21:44:01 +0000 | [diff] [blame] | 9661 | if (ConstEltNo) { |
| Eric Christopher | fcc9e68 | 2010-11-03 09:36:40 +0000 | [diff] [blame] | 9662 | int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); |
| Evan Cheng | 0de312d | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 9663 | bool NewLoad = false; |
| Mon P Wang | b5eb720 | 2008-12-11 00:26:16 +0000 | [diff] [blame] | 9664 | bool BCNumEltsChanged = false; |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 9665 | EVT ExtVT = VT.getVectorElementType(); |
| 9666 | EVT LVT = ExtVT; |
| Bill Wendling | 27d9dd4 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9667 | |
| Evan Cheng | 7bf8309 | 2012-03-13 22:00:52 +0000 | [diff] [blame] | 9668 | // If the result of load has to be truncated, then it's not necessarily |
| 9669 | // profitable. |
| Evan Cheng | d5f8e57 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 9670 | if (NVT.bitsLT(LVT) && !TLI.isTruncateFree(LVT, NVT)) |
| Evan Cheng | 7bf8309 | 2012-03-13 22:00:52 +0000 | [diff] [blame] | 9671 | return SDValue(); |
| 9672 | |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9673 | if (InVec.getOpcode() == ISD::BITCAST) { |
| Eli Friedman | e96286c | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 9674 | // Don't duplicate a load with other uses. |
| 9675 | if (!InVec.hasOneUse()) |
| 9676 | return SDValue(); |
| 9677 | |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 9678 | EVT BCVT = InVec.getOperand(0).getValueType(); |
| 9679 | if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType())) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9680 | return SDValue(); |
| Mon P Wang | b5eb720 | 2008-12-11 00:26:16 +0000 | [diff] [blame] | 9681 | if (VT.getVectorNumElements() != BCVT.getVectorNumElements()) |
| 9682 | BCNumEltsChanged = true; |
| Evan Cheng | 1120279a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9683 | InVec = InVec.getOperand(0); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 9684 | ExtVT = BCVT.getVectorElementType(); |
| Evan Cheng | 1120279a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9685 | NewLoad = true; |
| 9686 | } |
| Evan Cheng | 0de312d | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 9687 | |
| Evan Cheng | 1120279a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9688 | LoadSDNode *LN0 = NULL; |
| Nate Begeman | 5f829d8 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 9689 | const ShuffleVectorSDNode *SVN = NULL; |
| Bill Wendling | 27d9dd4 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9690 | if (ISD::isNormalLoad(InVec.getNode())) { |
| Evan Cheng | 1120279a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9691 | LN0 = cast<LoadSDNode>(InVec); |
| Bill Wendling | 27d9dd4 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9692 | } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR && |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 9693 | InVec.getOperand(0).getValueType() == ExtVT && |
| Bill Wendling | 27d9dd4 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9694 | ISD::isNormalLoad(InVec.getOperand(0).getNode())) { |
| Eli Friedman | e96286c | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 9695 | // Don't duplicate a load with other uses. |
| 9696 | if (!InVec.hasOneUse()) |
| 9697 | return SDValue(); |
| 9698 | |
| Evan Cheng | 1120279a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9699 | LN0 = cast<LoadSDNode>(InVec.getOperand(0)); |
| Nate Begeman | 5f829d8 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 9700 | } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) { |
| Evan Cheng | 1120279a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9701 | // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1) |
| 9702 | // => |
| 9703 | // (load $addr+1*size) |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9704 | |
| Eli Friedman | e96286c | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 9705 | // Don't duplicate a load with other uses. |
| 9706 | if (!InVec.hasOneUse()) |
| 9707 | return SDValue(); |
| 9708 | |
| Mon P Wang | b5eb720 | 2008-12-11 00:26:16 +0000 | [diff] [blame] | 9709 | // If the bit convert changed the number of elements, it is unsafe |
| 9710 | // to examine the mask. |
| 9711 | if (BCNumEltsChanged) |
| 9712 | return SDValue(); |
| Nate Begeman | 5f829d8 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 9713 | |
| 9714 | // Select the input vector, guarding against out of range extract vector. |
| 9715 | unsigned NumElems = VT.getVectorNumElements(); |
| Eric Christopher | fcc9e68 | 2010-11-03 09:36:40 +0000 | [diff] [blame] | 9716 | int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt); |
| Nate Begeman | 5f829d8 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 9717 | InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1); |
| 9718 | |
| Eli Friedman | e96286c | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 9719 | if (InVec.getOpcode() == ISD::BITCAST) { |
| 9720 | // Don't duplicate a load with other uses. |
| 9721 | if (!InVec.hasOneUse()) |
| 9722 | return SDValue(); |
| 9723 | |
| Evan Cheng | 1120279a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9724 | InVec = InVec.getOperand(0); |
| Eli Friedman | e96286c | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 9725 | } |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9726 | if (ISD::isNormalLoad(InVec.getNode())) { |
| Evan Cheng | 1120279a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9727 | LN0 = cast<LoadSDNode>(InVec); |
| Ted Kremenek | d87bd77 | 2010-04-08 18:49:30 +0000 | [diff] [blame] | 9728 | Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems; |
| Evan Cheng | 0de312d | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 9729 | } |
| 9730 | } |
| Bill Wendling | 27d9dd4 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9731 | |
| Eli Friedman | e96286c | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 9732 | // Make sure we found a non-volatile load and the extractelement is |
| 9733 | // the only use. |
| Nadav Rotem | 8a7beb8 | 2011-05-11 14:40:50 +0000 | [diff] [blame] | 9734 | if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile()) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9735 | return SDValue(); |
| Evan Cheng | 1120279a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9736 | |
| Eric Christopher | c6418b1 | 2010-11-03 20:44:42 +0000 | [diff] [blame] | 9737 | // If Idx was -1 above, Elt is going to be -1, so just return undef. |
| 9738 | if (Elt == -1) |
| Eli Friedman | cbd3ba9 | 2011-07-25 22:25:42 +0000 | [diff] [blame] | 9739 | return DAG.getUNDEF(LVT); |
| Eric Christopher | c6418b1 | 2010-11-03 20:44:42 +0000 | [diff] [blame] | 9740 | |
| Evan Cheng | 1120279a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9741 | unsigned Align = LN0->getAlignment(); |
| 9742 | if (NewLoad) { |
| 9743 | // Check the resultant load doesn't need a higher alignment than the |
| 9744 | // original load. |
| Bill Wendling | 27d9dd4 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9745 | unsigned NewAlign = |
| Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 9746 | TLI.getDataLayout() |
| Eric Christopher | d9e8eac | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 9747 | ->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext())); |
| Bill Wendling | 27d9dd4 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9748 | |
| Dan Gohman | 4aa1846 | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 9749 | if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT)) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9750 | return SDValue(); |
| Bill Wendling | 27d9dd4 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9751 | |
| Evan Cheng | 1120279a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9752 | Align = NewAlign; |
| 9753 | } |
| 9754 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9755 | SDValue NewPtr = LN0->getBasePtr(); |
| Chris Lattner | f72c3c0 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 9756 | unsigned PtrOff = 0; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9757 | |
| Eric Christopher | c6418b1 | 2010-11-03 20:44:42 +0000 | [diff] [blame] | 9758 | if (Elt) { |
| Chris Lattner | f72c3c0 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 9759 | PtrOff = LVT.getSizeInBits() * Elt / 8; |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 9760 | EVT PtrType = NewPtr.getValueType(); |
| Evan Cheng | 1120279a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9761 | if (TLI.isBigEndian()) |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 9762 | PtrOff = VT.getSizeInBits() / 8 - PtrOff; |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9763 | NewPtr = DAG.getNode(ISD::ADD, SDLoc(N), PtrType, NewPtr, |
| Evan Cheng | 1120279a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9764 | DAG.getConstant(PtrOff, PtrType)); |
| 9765 | } |
| Bill Wendling | 27d9dd4 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9766 | |
| Eli Friedman | ff1eaa7 | 2011-11-16 23:50:22 +0000 | [diff] [blame] | 9767 | // The replacement we need to do here is a little tricky: we need to |
| 9768 | // replace an extractelement of a load with a load. |
| 9769 | // Use ReplaceAllUsesOfValuesWith to do the replacement. |
| Eli Friedman | e96286c | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 9770 | // Note that this replacement assumes that the extractvalue is the only |
| 9771 | // use of the load; that's okay because we don't want to perform this |
| 9772 | // transformation in other cases anyway. |
| Evan Cheng | 7bf8309 | 2012-03-13 22:00:52 +0000 | [diff] [blame] | 9773 | SDValue Load; |
| Evan Cheng | d5f8e57 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 9774 | SDValue Chain; |
| Evan Cheng | 7bf8309 | 2012-03-13 22:00:52 +0000 | [diff] [blame] | 9775 | if (NVT.bitsGT(LVT)) { |
| 9776 | // If the result type of vextract is wider than the load, then issue an |
| 9777 | // extending load instead. |
| 9778 | ISD::LoadExtType ExtType = TLI.isLoadExtLegal(ISD::ZEXTLOAD, LVT) |
| 9779 | ? ISD::ZEXTLOAD : ISD::EXTLOAD; |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9780 | Load = DAG.getExtLoad(ExtType, SDLoc(N), NVT, LN0->getChain(), |
| Evan Cheng | 7bf8309 | 2012-03-13 22:00:52 +0000 | [diff] [blame] | 9781 | NewPtr, LN0->getPointerInfo().getWithOffset(PtrOff), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 9782 | LVT, LN0->isVolatile(), LN0->isNonTemporal(), |
| 9783 | Align, LN0->getTBAAInfo()); |
| Evan Cheng | d5f8e57 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 9784 | Chain = Load.getValue(1); |
| 9785 | } else { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9786 | Load = DAG.getLoad(LVT, SDLoc(N), LN0->getChain(), NewPtr, |
| Evan Cheng | 7bf8309 | 2012-03-13 22:00:52 +0000 | [diff] [blame] | 9787 | LN0->getPointerInfo().getWithOffset(PtrOff), |
| Stephen Lin | cfe7f35 | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 9788 | LN0->isVolatile(), LN0->isNonTemporal(), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 9789 | LN0->isInvariant(), Align, LN0->getTBAAInfo()); |
| Evan Cheng | d5f8e57 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 9790 | Chain = Load.getValue(1); |
| 9791 | if (NVT.bitsLT(LVT)) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9792 | Load = DAG.getNode(ISD::TRUNCATE, SDLoc(N), NVT, Load); |
| Evan Cheng | d5f8e57 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 9793 | else |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9794 | Load = DAG.getNode(ISD::BITCAST, SDLoc(N), NVT, Load); |
| Evan Cheng | d5f8e57 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 9795 | } |
| Eli Friedman | ff1eaa7 | 2011-11-16 23:50:22 +0000 | [diff] [blame] | 9796 | WorkListRemover DeadNodes(*this); |
| 9797 | SDValue From[] = { SDValue(N, 0), SDValue(LN0,1) }; |
| Evan Cheng | d5f8e57 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 9798 | SDValue To[] = { Load, Chain }; |
| Jakob Stoklund Olesen | beb9469 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 9799 | DAG.ReplaceAllUsesOfValuesWith(From, To, 2); |
| Eli Friedman | ff1eaa7 | 2011-11-16 23:50:22 +0000 | [diff] [blame] | 9800 | // Since we're explcitly calling ReplaceAllUses, add the new node to the |
| 9801 | // worklist explicitly as well. |
| 9802 | AddToWorkList(Load.getNode()); |
| Craig Topper | aaeae98 | 2012-03-20 05:28:39 +0000 | [diff] [blame] | 9803 | AddUsersToWorkList(Load.getNode()); // Add users too |
| Eli Friedman | ff1eaa7 | 2011-11-16 23:50:22 +0000 | [diff] [blame] | 9804 | // Make sure to revisit this node to clean it up; it will usually be dead. |
| 9805 | AddToWorkList(N); |
| 9806 | return SDValue(N, 0); |
| Evan Cheng | 0de312d | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 9807 | } |
| Bill Wendling | 27d9dd4 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9808 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9809 | return SDValue(); |
| Evan Cheng | 0de312d | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 9810 | } |
| Evan Cheng | 0de312d | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 9811 | |
| Michael Liao | 6d106b7 | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9812 | // Simplify (build_vec (ext )) to (bitcast (build_vec )) |
| 9813 | SDValue DAGCombiner::reduceBuildVecExtToExtBuildVec(SDNode *N) { |
| 9814 | // We perform this optimization post type-legalization because |
| 9815 | // the type-legalizer often scalarizes integer-promoted vectors. |
| 9816 | // Performing this optimization before may create bit-casts which |
| 9817 | // will be type-legalized to complex code sequences. |
| 9818 | // We perform this optimization only before the operation legalizer because we |
| 9819 | // may introduce illegal operations. |
| 9820 | if (Level != AfterLegalizeVectorOps && Level != AfterLegalizeTypes) |
| 9821 | return SDValue(); |
| 9822 | |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9823 | unsigned NumInScalars = N->getNumOperands(); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9824 | SDLoc dl(N); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 9825 | EVT VT = N->getValueType(0); |
| Nadav Rotem | a62368c | 2012-07-15 08:38:23 +0000 | [diff] [blame] | 9826 | |
| Nadav Rotem | bf6568b | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9827 | // Check to see if this is a BUILD_VECTOR of a bunch of values |
| 9828 | // which come from any_extend or zero_extend nodes. If so, we can create |
| 9829 | // a new BUILD_VECTOR using bit-casts which may enable other BUILD_VECTOR |
| Nadav Rotem | f310361 | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 9830 | // optimizations. We do not handle sign-extend because we can't fill the sign |
| 9831 | // using shuffles. |
| Nadav Rotem | bf6568b | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9832 | EVT SourceType = MVT::Other; |
| Craig Topper | 02cb0fb | 2012-01-17 09:09:48 +0000 | [diff] [blame] | 9833 | bool AllAnyExt = true; |
| Nadav Rotem | a62368c | 2012-07-15 08:38:23 +0000 | [diff] [blame] | 9834 | |
| Craig Topper | 02cb0fb | 2012-01-17 09:09:48 +0000 | [diff] [blame] | 9835 | for (unsigned i = 0; i != NumInScalars; ++i) { |
| Nadav Rotem | bf6568b | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9836 | SDValue In = N->getOperand(i); |
| 9837 | // Ignore undef inputs. |
| 9838 | if (In.getOpcode() == ISD::UNDEF) continue; |
| 9839 | |
| 9840 | bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND; |
| 9841 | bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND; |
| 9842 | |
| Nadav Rotem | f310361 | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 9843 | // Abort if the element is not an extension. |
| Nadav Rotem | bf6568b | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9844 | if (!ZeroExt && !AnyExt) { |
| Nadav Rotem | f310361 | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 9845 | SourceType = MVT::Other; |
| Nadav Rotem | bf6568b | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9846 | break; |
| 9847 | } |
| 9848 | |
| 9849 | // The input is a ZeroExt or AnyExt. Check the original type. |
| 9850 | EVT InTy = In.getOperand(0).getValueType(); |
| 9851 | |
| 9852 | // Check that all of the widened source types are the same. |
| 9853 | if (SourceType == MVT::Other) |
| Nadav Rotem | f310361 | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 9854 | // First time. |
| Nadav Rotem | bf6568b | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9855 | SourceType = InTy; |
| 9856 | else if (InTy != SourceType) { |
| 9857 | // Multiple income types. Abort. |
| Nadav Rotem | f310361 | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 9858 | SourceType = MVT::Other; |
| Nadav Rotem | bf6568b | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9859 | break; |
| 9860 | } |
| 9861 | |
| 9862 | // Check if all of the extends are ANY_EXTENDs. |
| Craig Topper | 02cb0fb | 2012-01-17 09:09:48 +0000 | [diff] [blame] | 9863 | AllAnyExt &= AnyExt; |
| Nadav Rotem | bf6568b | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9864 | } |
| 9865 | |
| Nadav Rotem | f310361 | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 9866 | // In order to have valid types, all of the inputs must be extended from the |
| 9867 | // same source type and all of the inputs must be any or zero extend. |
| 9868 | // Scalar sizes must be a power of two. |
| Michael Liao | 6d106b7 | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9869 | EVT OutScalarTy = VT.getScalarType(); |
| Nadav Rotem | 34ca89a | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 9870 | bool ValidTypes = SourceType != MVT::Other && |
| Nadav Rotem | f310361 | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 9871 | isPowerOf2_32(OutScalarTy.getSizeInBits()) && |
| 9872 | isPowerOf2_32(SourceType.getSizeInBits()); |
| 9873 | |
| Nadav Rotem | 6fd1d32 | 2012-03-15 08:49:06 +0000 | [diff] [blame] | 9874 | // Create a new simpler BUILD_VECTOR sequence which other optimizations can |
| 9875 | // turn into a single shuffle instruction. |
| Michael Liao | 6d106b7 | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9876 | if (!ValidTypes) |
| 9877 | return SDValue(); |
| Nadav Rotem | bf6568b | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9878 | |
| Michael Liao | 6d106b7 | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9879 | bool isLE = TLI.isLittleEndian(); |
| 9880 | unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits(); |
| 9881 | assert(ElemRatio > 1 && "Invalid element size ratio"); |
| 9882 | SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType): |
| 9883 | DAG.getConstant(0, SourceType); |
| Nadav Rotem | bf6568b | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9884 | |
| Michael Liao | 6d106b7 | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9885 | unsigned NewBVElems = ElemRatio * VT.getVectorNumElements(); |
| 9886 | SmallVector<SDValue, 8> Ops(NewBVElems, Filler); |
| Nadav Rotem | bf6568b | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9887 | |
| Michael Liao | 6d106b7 | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9888 | // Populate the new build_vector |
| Jakub Staszak | a6addc2 | 2012-10-24 00:38:25 +0000 | [diff] [blame] | 9889 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| Michael Liao | 6d106b7 | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9890 | SDValue Cast = N->getOperand(i); |
| 9891 | assert((Cast.getOpcode() == ISD::ANY_EXTEND || |
| 9892 | Cast.getOpcode() == ISD::ZERO_EXTEND || |
| 9893 | Cast.getOpcode() == ISD::UNDEF) && "Invalid cast opcode"); |
| 9894 | SDValue In; |
| 9895 | if (Cast.getOpcode() == ISD::UNDEF) |
| 9896 | In = DAG.getUNDEF(SourceType); |
| 9897 | else |
| 9898 | In = Cast->getOperand(0); |
| 9899 | unsigned Index = isLE ? (i * ElemRatio) : |
| 9900 | (i * ElemRatio + (ElemRatio - 1)); |
| Nadav Rotem | bf6568b | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9901 | |
| Michael Liao | 6d106b7 | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9902 | assert(Index < Ops.size() && "Invalid index"); |
| 9903 | Ops[Index] = In; |
| Nadav Rotem | bf6568b | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9904 | } |
| Chris Lattner | 5336a59 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 9905 | |
| Michael Liao | 6d106b7 | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9906 | // The type of the new BUILD_VECTOR node. |
| 9907 | EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems); |
| 9908 | assert(VecVT.getSizeInBits() == VT.getSizeInBits() && |
| 9909 | "Invalid vector size"); |
| 9910 | // Check if the new vector type is legal. |
| 9911 | if (!isTypeLegal(VecVT)) return SDValue(); |
| 9912 | |
| 9913 | // Make the new BUILD_VECTOR. |
| 9914 | SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], Ops.size()); |
| 9915 | |
| 9916 | // The new BUILD_VECTOR node has the potential to be further optimized. |
| 9917 | AddToWorkList(BV.getNode()); |
| 9918 | // Bitcast to the desired type. |
| 9919 | return DAG.getNode(ISD::BITCAST, dl, VT, BV); |
| 9920 | } |
| 9921 | |
| Michael Liao | 5922979 | 2012-10-24 04:14:18 +0000 | [diff] [blame] | 9922 | SDValue DAGCombiner::reduceBuildVecConvertToConvertBuildVec(SDNode *N) { |
| 9923 | EVT VT = N->getValueType(0); |
| 9924 | |
| 9925 | unsigned NumInScalars = N->getNumOperands(); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9926 | SDLoc dl(N); |
| Michael Liao | 5922979 | 2012-10-24 04:14:18 +0000 | [diff] [blame] | 9927 | |
| 9928 | EVT SrcVT = MVT::Other; |
| 9929 | unsigned Opcode = ISD::DELETED_NODE; |
| 9930 | unsigned NumDefs = 0; |
| 9931 | |
| 9932 | for (unsigned i = 0; i != NumInScalars; ++i) { |
| 9933 | SDValue In = N->getOperand(i); |
| 9934 | unsigned Opc = In.getOpcode(); |
| 9935 | |
| 9936 | if (Opc == ISD::UNDEF) |
| 9937 | continue; |
| 9938 | |
| 9939 | // If all scalar values are floats and converted from integers. |
| 9940 | if (Opcode == ISD::DELETED_NODE && |
| 9941 | (Opc == ISD::UINT_TO_FP || Opc == ISD::SINT_TO_FP)) { |
| 9942 | Opcode = Opc; |
| Michael Liao | 5922979 | 2012-10-24 04:14:18 +0000 | [diff] [blame] | 9943 | } |
| Tom Stellard | 567f886 | 2013-01-02 22:13:01 +0000 | [diff] [blame] | 9944 | |
| Michael Liao | 5922979 | 2012-10-24 04:14:18 +0000 | [diff] [blame] | 9945 | if (Opc != Opcode) |
| 9946 | return SDValue(); |
| 9947 | |
| 9948 | EVT InVT = In.getOperand(0).getValueType(); |
| 9949 | |
| 9950 | // If all scalar values are typed differently, bail out. It's chosen to |
| 9951 | // simplify BUILD_VECTOR of integer types. |
| 9952 | if (SrcVT == MVT::Other) |
| 9953 | SrcVT = InVT; |
| 9954 | if (SrcVT != InVT) |
| 9955 | return SDValue(); |
| 9956 | NumDefs++; |
| 9957 | } |
| 9958 | |
| 9959 | // If the vector has just one element defined, it's not worth to fold it into |
| 9960 | // a vectorized one. |
| 9961 | if (NumDefs < 2) |
| 9962 | return SDValue(); |
| 9963 | |
| 9964 | assert((Opcode == ISD::UINT_TO_FP || Opcode == ISD::SINT_TO_FP) |
| 9965 | && "Should only handle conversion from integer to float."); |
| 9966 | assert(SrcVT != MVT::Other && "Cannot determine source type!"); |
| 9967 | |
| 9968 | EVT NVT = EVT::getVectorVT(*DAG.getContext(), SrcVT, NumInScalars); |
| Tom Stellard | 567f886 | 2013-01-02 22:13:01 +0000 | [diff] [blame] | 9969 | |
| 9970 | if (!TLI.isOperationLegalOrCustom(Opcode, NVT)) |
| 9971 | return SDValue(); |
| 9972 | |
| Michael Liao | 5922979 | 2012-10-24 04:14:18 +0000 | [diff] [blame] | 9973 | SmallVector<SDValue, 8> Opnds; |
| 9974 | for (unsigned i = 0; i != NumInScalars; ++i) { |
| 9975 | SDValue In = N->getOperand(i); |
| 9976 | |
| 9977 | if (In.getOpcode() == ISD::UNDEF) |
| 9978 | Opnds.push_back(DAG.getUNDEF(SrcVT)); |
| 9979 | else |
| 9980 | Opnds.push_back(In.getOperand(0)); |
| 9981 | } |
| 9982 | SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, |
| 9983 | &Opnds[0], Opnds.size()); |
| 9984 | AddToWorkList(BV.getNode()); |
| 9985 | |
| 9986 | return DAG.getNode(Opcode, dl, VT, BV); |
| 9987 | } |
| 9988 | |
| Michael Liao | 6d106b7 | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9989 | SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) { |
| 9990 | unsigned NumInScalars = N->getNumOperands(); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9991 | SDLoc dl(N); |
| Michael Liao | 6d106b7 | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9992 | EVT VT = N->getValueType(0); |
| 9993 | |
| 9994 | // A vector built entirely of undefs is undef. |
| 9995 | if (ISD::allOperandsUndef(N)) |
| 9996 | return DAG.getUNDEF(VT); |
| 9997 | |
| 9998 | SDValue V = reduceBuildVecExtToExtBuildVec(N); |
| 9999 | if (V.getNode()) |
| 10000 | return V; |
| 10001 | |
| Michael Liao | 5922979 | 2012-10-24 04:14:18 +0000 | [diff] [blame] | 10002 | V = reduceBuildVecConvertToConvertBuildVec(N); |
| 10003 | if (V.getNode()) |
| 10004 | return V; |
| 10005 | |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10006 | // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT |
| 10007 | // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from |
| 10008 | // at most two distinct vectors, turn this into a shuffle node. |
| Duncan Sands | 3fb2fc6 | 2012-03-19 15:35:44 +0000 | [diff] [blame] | 10009 | |
| 10010 | // May only combine to shuffle after legalize if shuffle is legal. |
| 10011 | if (LegalOperations && |
| 10012 | !TLI.isOperationLegalOrCustom(ISD::VECTOR_SHUFFLE, VT)) |
| 10013 | return SDValue(); |
| 10014 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10015 | SDValue VecIn1, VecIn2; |
| Chris Lattner | c999254 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 10016 | for (unsigned i = 0; i != NumInScalars; ++i) { |
| 10017 | // Ignore undef inputs. |
| 10018 | if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10019 | |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10020 | // If this input is something other than a EXTRACT_VECTOR_ELT with a |
| Chris Lattner | c999254 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 10021 | // constant index, bail out. |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10022 | if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT || |
| Chris Lattner | c999254 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 10023 | !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10024 | VecIn1 = VecIn2 = SDValue(0, 0); |
| Chris Lattner | c999254 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 10025 | break; |
| 10026 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10027 | |
| Nadav Rotem | 34ca89a | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 10028 | // We allow up to two distinct input vectors. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10029 | SDValue ExtractedFromVec = N->getOperand(i).getOperand(0); |
| Chris Lattner | c999254 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 10030 | if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2) |
| 10031 | continue; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10032 | |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10033 | if (VecIn1.getNode() == 0) { |
| Chris Lattner | c999254 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 10034 | VecIn1 = ExtractedFromVec; |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10035 | } else if (VecIn2.getNode() == 0) { |
| Chris Lattner | c999254 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 10036 | VecIn2 = ExtractedFromVec; |
| 10037 | } else { |
| 10038 | // Too many inputs. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10039 | VecIn1 = VecIn2 = SDValue(0, 0); |
| Chris Lattner | c999254 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 10040 | break; |
| 10041 | } |
| 10042 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10043 | |
| Nadav Rotem | 34ca89a | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 10044 | // If everything is good, we can make a shuffle operation. |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10045 | if (VecIn1.getNode()) { |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 10046 | SmallVector<int, 8> Mask; |
| Chris Lattner | c999254 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 10047 | for (unsigned i = 0; i != NumInScalars; ++i) { |
| 10048 | if (N->getOperand(i).getOpcode() == ISD::UNDEF) { |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 10049 | Mask.push_back(-1); |
| Chris Lattner | c999254 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 10050 | continue; |
| 10051 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10052 | |
| Rafael Espindola | b93db66 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 10053 | // If extracting from the first vector, just use the index directly. |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 10054 | SDValue Extract = N->getOperand(i); |
| Mon P Wang | 523c085 | 2009-03-17 06:33:10 +0000 | [diff] [blame] | 10055 | SDValue ExtVal = Extract.getOperand(1); |
| Chris Lattner | c999254 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 10056 | if (Extract.getOperand(0) == VecIn1) { |
| Nate Begeman | 5f829d8 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 10057 | unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue(); |
| 10058 | if (ExtIndex > VT.getVectorNumElements()) |
| 10059 | return SDValue(); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10060 | |
| Nate Begeman | 5f829d8 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 10061 | Mask.push_back(ExtIndex); |
| Chris Lattner | c999254 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 10062 | continue; |
| 10063 | } |
| 10064 | |
| 10065 | // Otherwise, use InIdx + VecSize |
| Mon P Wang | 523c085 | 2009-03-17 06:33:10 +0000 | [diff] [blame] | 10066 | unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue(); |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 10067 | Mask.push_back(Idx+NumInScalars); |
| Chris Lattner | c999254 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 10068 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10069 | |
| Nadav Rotem | 34ca89a | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 10070 | // We can't generate a shuffle node with mismatched input and output types. |
| 10071 | // Attempt to transform a single input vector to the correct type. |
| 10072 | if ((VT != VecIn1.getValueType())) { |
| 10073 | // We don't support shuffeling between TWO values of different types. |
| 10074 | if (VecIn2.getNode() != 0) |
| 10075 | return SDValue(); |
| 10076 | |
| 10077 | // We only support widening of vectors which are half the size of the |
| 10078 | // output registers. For example XMM->YMM widening on X86 with AVX. |
| 10079 | if (VecIn1.getValueType().getSizeInBits()*2 != VT.getSizeInBits()) |
| 10080 | return SDValue(); |
| 10081 | |
| James Molloy | 1e5c611 | 2012-09-10 14:01:21 +0000 | [diff] [blame] | 10082 | // If the input vector type has a different base type to the output |
| 10083 | // vector type, bail out. |
| 10084 | if (VecIn1.getValueType().getVectorElementType() != |
| 10085 | VT.getVectorElementType()) |
| 10086 | return SDValue(); |
| 10087 | |
| Stepan Dyatkovskiy | 99120e0 | 2012-08-22 09:33:55 +0000 | [diff] [blame] | 10088 | // Widen the input vector by adding undef values. |
| Michael Liao | 6d106b7 | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 10089 | VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, |
| Stepan Dyatkovskiy | 99120e0 | 2012-08-22 09:33:55 +0000 | [diff] [blame] | 10090 | VecIn1, DAG.getUNDEF(VecIn1.getValueType())); |
| Nadav Rotem | 34ca89a | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 10091 | } |
| 10092 | |
| 10093 | // If VecIn2 is unused then change it to undef. |
| 10094 | VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT); |
| 10095 | |
| Nadav Rotem | 841c9a8 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 10096 | // Check that we were able to transform all incoming values to the same |
| 10097 | // type. |
| Nadav Rotem | 0c65064 | 2012-02-13 12:42:26 +0000 | [diff] [blame] | 10098 | if (VecIn2.getValueType() != VecIn1.getValueType() || |
| 10099 | VecIn1.getValueType() != VT) |
| 10100 | return SDValue(); |
| 10101 | |
| Nadav Rotem | 34ca89a | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 10102 | // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes. |
| Nadav Rotem | 0c65064 | 2012-02-13 12:42:26 +0000 | [diff] [blame] | 10103 | if (!isTypeLegal(VT)) |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 10104 | return SDValue(); |
| 10105 | |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10106 | // Return the new VECTOR_SHUFFLE node. |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 10107 | SDValue Ops[2]; |
| Chris Lattner | c24a1d3 | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 10108 | Ops[0] = VecIn1; |
| Nadav Rotem | 34ca89a | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 10109 | Ops[1] = VecIn2; |
| Michael Liao | 6d106b7 | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 10110 | return DAG.getVectorShuffle(VT, dl, Ops[0], Ops[1], &Mask[0]); |
| Chris Lattner | c999254 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 10111 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10112 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10113 | return SDValue(); |
| Chris Lattner | c999254 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 10114 | } |
| 10115 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10116 | SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) { |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10117 | // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of |
| 10118 | // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector |
| 10119 | // inputs come from at most two distinct vectors, turn this into a shuffle |
| 10120 | // node. |
| 10121 | |
| 10122 | // If we only have one input vector, we don't need to do any concatenation. |
| Bill Wendling | 27d9dd4 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 10123 | if (N->getNumOperands() == 1) |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10124 | return N->getOperand(0); |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10125 | |
| Nadav Rotem | 0189210 | 2012-07-14 21:30:27 +0000 | [diff] [blame] | 10126 | // Check if all of the operands are undefs. |
| Nadav Rotem | d369d4b | 2013-10-25 06:41:18 +0000 | [diff] [blame] | 10127 | EVT VT = N->getValueType(0); |
| Nadav Rotem | a62368c | 2012-07-15 08:38:23 +0000 | [diff] [blame] | 10128 | if (ISD::allOperandsUndef(N)) |
| Nadav Rotem | d369d4b | 2013-10-25 06:41:18 +0000 | [diff] [blame] | 10129 | return DAG.getUNDEF(VT); |
| 10130 | |
| 10131 | // Optimize concat_vectors where one of the vectors is undef. |
| 10132 | if (N->getNumOperands() == 2 && |
| 10133 | N->getOperand(1)->getOpcode() == ISD::UNDEF) { |
| 10134 | SDValue In = N->getOperand(0); |
| Nadav Rotem | 6eee080 | 2013-12-10 01:13:59 +0000 | [diff] [blame] | 10135 | assert(In.getValueType().isVector() && "Must concat vectors"); |
| Nadav Rotem | d369d4b | 2013-10-25 06:41:18 +0000 | [diff] [blame] | 10136 | |
| 10137 | // Transform: concat_vectors(scalar, undef) -> scalar_to_vector(sclr). |
| 10138 | if (In->getOpcode() == ISD::BITCAST && |
| 10139 | !In->getOperand(0)->getValueType(0).isVector()) { |
| 10140 | SDValue Scalar = In->getOperand(0); |
| 10141 | EVT SclTy = Scalar->getValueType(0); |
| 10142 | |
| 10143 | if (!SclTy.isFloatingPoint() && !SclTy.isInteger()) |
| 10144 | return SDValue(); |
| 10145 | |
| 10146 | EVT NVT = EVT::getVectorVT(*DAG.getContext(), SclTy, |
| 10147 | VT.getSizeInBits() / SclTy.getSizeInBits()); |
| 10148 | if (!TLI.isTypeLegal(NVT) || !TLI.isTypeLegal(Scalar.getValueType())) |
| 10149 | return SDValue(); |
| 10150 | |
| 10151 | SDLoc dl = SDLoc(N); |
| 10152 | SDValue Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NVT, Scalar); |
| 10153 | return DAG.getNode(ISD::BITCAST, dl, VT, Res); |
| 10154 | } |
| 10155 | } |
| Nadav Rotem | 0189210 | 2012-07-14 21:30:27 +0000 | [diff] [blame] | 10156 | |
| Robert Lougher | 7d9084f | 2014-02-11 15:42:46 +0000 | [diff] [blame] | 10157 | // fold (concat_vectors (BUILD_VECTOR A, B, ...), (BUILD_VECTOR C, D, ...)) |
| 10158 | // -> (BUILD_VECTOR A, B, ..., C, D, ...) |
| 10159 | if (N->getNumOperands() == 2 && |
| 10160 | N->getOperand(0).getOpcode() == ISD::BUILD_VECTOR && |
| 10161 | N->getOperand(1).getOpcode() == ISD::BUILD_VECTOR) { |
| 10162 | EVT VT = N->getValueType(0); |
| 10163 | SDValue N0 = N->getOperand(0); |
| 10164 | SDValue N1 = N->getOperand(1); |
| 10165 | SmallVector<SDValue, 8> Opnds; |
| 10166 | unsigned BuildVecNumElts = N0.getNumOperands(); |
| 10167 | |
| 10168 | for (unsigned i = 0; i != BuildVecNumElts; ++i) |
| 10169 | Opnds.push_back(N0.getOperand(i)); |
| 10170 | for (unsigned i = 0; i != BuildVecNumElts; ++i) |
| 10171 | Opnds.push_back(N1.getOperand(i)); |
| 10172 | |
| 10173 | return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, &Opnds[0], |
| 10174 | Opnds.size()); |
| 10175 | } |
| 10176 | |
| Nadav Rotem | e5a2dda | 2013-05-01 19:18:51 +0000 | [diff] [blame] | 10177 | // Type legalization of vectors and DAG canonicalization of SHUFFLE_VECTOR |
| 10178 | // nodes often generate nop CONCAT_VECTOR nodes. |
| 10179 | // Scan the CONCAT_VECTOR operands and look for a CONCAT operations that |
| 10180 | // place the incoming vectors at the exact same location. |
| 10181 | SDValue SingleSource = SDValue(); |
| 10182 | unsigned PartNumElem = N->getOperand(0).getValueType().getVectorNumElements(); |
| 10183 | |
| 10184 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 10185 | SDValue Op = N->getOperand(i); |
| 10186 | |
| 10187 | if (Op.getOpcode() == ISD::UNDEF) |
| 10188 | continue; |
| 10189 | |
| 10190 | // Check if this is the identity extract: |
| 10191 | if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR) |
| 10192 | return SDValue(); |
| 10193 | |
| 10194 | // Find the single incoming vector for the extract_subvector. |
| 10195 | if (SingleSource.getNode()) { |
| 10196 | if (Op.getOperand(0) != SingleSource) |
| 10197 | return SDValue(); |
| 10198 | } else { |
| 10199 | SingleSource = Op.getOperand(0); |
| Michael Kuperstein | ac86875 | 2013-05-06 08:06:13 +0000 | [diff] [blame] | 10200 | |
| 10201 | // Check the source type is the same as the type of the result. |
| 10202 | // If not, this concat may extend the vector, so we can not |
| 10203 | // optimize it away. |
| 10204 | if (SingleSource.getValueType() != N->getValueType(0)) |
| 10205 | return SDValue(); |
| Nadav Rotem | e5a2dda | 2013-05-01 19:18:51 +0000 | [diff] [blame] | 10206 | } |
| 10207 | |
| 10208 | unsigned IdentityIndex = i * PartNumElem; |
| 10209 | ConstantSDNode *CS = dyn_cast<ConstantSDNode>(Op.getOperand(1)); |
| 10210 | // The extract index must be constant. |
| 10211 | if (!CS) |
| 10212 | return SDValue(); |
| Stephen Lin | cfe7f35 | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 10213 | |
| Nadav Rotem | e5a2dda | 2013-05-01 19:18:51 +0000 | [diff] [blame] | 10214 | // Check that we are reading from the identity index. |
| 10215 | if (CS->getZExtValue() != IdentityIndex) |
| 10216 | return SDValue(); |
| 10217 | } |
| 10218 | |
| 10219 | if (SingleSource.getNode()) |
| 10220 | return SingleSource; |
| Stephen Lin | cfe7f35 | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 10221 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10222 | return SDValue(); |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10223 | } |
| 10224 | |
| Bruno Cardoso Lopes | 6cb23f6 | 2011-09-20 23:19:33 +0000 | [diff] [blame] | 10225 | SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode* N) { |
| 10226 | EVT NVT = N->getValueType(0); |
| 10227 | SDValue V = N->getOperand(0); |
| 10228 | |
| Michael Liao | 7a442c80 | 2012-10-17 20:48:33 +0000 | [diff] [blame] | 10229 | if (V->getOpcode() == ISD::CONCAT_VECTORS) { |
| 10230 | // Combine: |
| 10231 | // (extract_subvec (concat V1, V2, ...), i) |
| 10232 | // Into: |
| 10233 | // Vi if possible |
| Jack Carter | d4e9615 | 2013-10-17 01:34:33 +0000 | [diff] [blame] | 10234 | // Only operand 0 is checked as 'concat' assumes all inputs of the same |
| 10235 | // type. |
| Michael Liao | 2c23580 | 2012-10-19 03:17:00 +0000 | [diff] [blame] | 10236 | if (V->getOperand(0).getValueType() != NVT) |
| 10237 | return SDValue(); |
| Michael Liao | 7a442c80 | 2012-10-17 20:48:33 +0000 | [diff] [blame] | 10238 | unsigned Idx = dyn_cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); |
| 10239 | unsigned NumElems = NVT.getVectorNumElements(); |
| 10240 | assert((Idx % NumElems) == 0 && |
| 10241 | "IDX in concat is not a multiple of the result vector length."); |
| 10242 | return V->getOperand(Idx / NumElems); |
| 10243 | } |
| 10244 | |
| Michael Liao | bb05a1d | 2013-03-25 23:47:35 +0000 | [diff] [blame] | 10245 | // Skip bitcasting |
| 10246 | if (V->getOpcode() == ISD::BITCAST) |
| 10247 | V = V.getOperand(0); |
| 10248 | |
| 10249 | if (V->getOpcode() == ISD::INSERT_SUBVECTOR) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10250 | SDLoc dl(N); |
| Michael Liao | bb05a1d | 2013-03-25 23:47:35 +0000 | [diff] [blame] | 10251 | // Handle only simple case where vector being inserted and vector |
| 10252 | // being extracted are of same type, and are half size of larger vectors. |
| 10253 | EVT BigVT = V->getOperand(0).getValueType(); |
| 10254 | EVT SmallVT = V->getOperand(1).getValueType(); |
| 10255 | if (!NVT.bitsEq(SmallVT) || NVT.getSizeInBits()*2 != BigVT.getSizeInBits()) |
| 10256 | return SDValue(); |
| 10257 | |
| 10258 | // Only handle cases where both indexes are constants with the same type. |
| 10259 | ConstantSDNode *ExtIdx = dyn_cast<ConstantSDNode>(N->getOperand(1)); |
| 10260 | ConstantSDNode *InsIdx = dyn_cast<ConstantSDNode>(V->getOperand(2)); |
| 10261 | |
| 10262 | if (InsIdx && ExtIdx && |
| 10263 | InsIdx->getValueType(0).getSizeInBits() <= 64 && |
| 10264 | ExtIdx->getValueType(0).getSizeInBits() <= 64) { |
| 10265 | // Combine: |
| 10266 | // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx) |
| 10267 | // Into: |
| 10268 | // indices are equal or bit offsets are equal => V1 |
| 10269 | // otherwise => (extract_subvec V1, ExtIdx) |
| 10270 | if (InsIdx->getZExtValue() * SmallVT.getScalarType().getSizeInBits() == |
| 10271 | ExtIdx->getZExtValue() * NVT.getScalarType().getSizeInBits()) |
| 10272 | return DAG.getNode(ISD::BITCAST, dl, NVT, V->getOperand(1)); |
| 10273 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT, |
| 10274 | DAG.getNode(ISD::BITCAST, dl, |
| 10275 | N->getOperand(0).getValueType(), |
| 10276 | V->getOperand(0)), N->getOperand(1)); |
| 10277 | } |
| 10278 | } |
| 10279 | |
| Bruno Cardoso Lopes | 6cb23f6 | 2011-09-20 23:19:33 +0000 | [diff] [blame] | 10280 | return SDValue(); |
| 10281 | } |
| 10282 | |
| Benjamin Kramer | bbae991 | 2013-04-09 17:41:43 +0000 | [diff] [blame] | 10283 | // Tries to turn a shuffle of two CONCAT_VECTORS into a single concat. |
| 10284 | static SDValue partitionShuffleOfConcats(SDNode *N, SelectionDAG &DAG) { |
| 10285 | EVT VT = N->getValueType(0); |
| 10286 | unsigned NumElts = VT.getVectorNumElements(); |
| 10287 | |
| 10288 | SDValue N0 = N->getOperand(0); |
| 10289 | SDValue N1 = N->getOperand(1); |
| 10290 | ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); |
| 10291 | |
| 10292 | SmallVector<SDValue, 4> Ops; |
| 10293 | EVT ConcatVT = N0.getOperand(0).getValueType(); |
| 10294 | unsigned NumElemsPerConcat = ConcatVT.getVectorNumElements(); |
| 10295 | unsigned NumConcats = NumElts / NumElemsPerConcat; |
| 10296 | |
| 10297 | // Look at every vector that's inserted. We're looking for exact |
| 10298 | // subvector-sized copies from a concatenated vector |
| 10299 | for (unsigned I = 0; I != NumConcats; ++I) { |
| 10300 | // Make sure we're dealing with a copy. |
| 10301 | unsigned Begin = I * NumElemsPerConcat; |
| Hao Liu | bc60196 | 2013-05-13 02:07:05 +0000 | [diff] [blame] | 10302 | bool AllUndef = true, NoUndef = true; |
| 10303 | for (unsigned J = Begin; J != Begin + NumElemsPerConcat; ++J) { |
| 10304 | if (SVN->getMaskElt(J) >= 0) |
| 10305 | AllUndef = false; |
| 10306 | else |
| 10307 | NoUndef = false; |
| Benjamin Kramer | bbae991 | 2013-04-09 17:41:43 +0000 | [diff] [blame] | 10308 | } |
| 10309 | |
| Hao Liu | bc60196 | 2013-05-13 02:07:05 +0000 | [diff] [blame] | 10310 | if (NoUndef) { |
| Hao Liu | bc60196 | 2013-05-13 02:07:05 +0000 | [diff] [blame] | 10311 | if (SVN->getMaskElt(Begin) % NumElemsPerConcat != 0) |
| 10312 | return SDValue(); |
| 10313 | |
| 10314 | for (unsigned J = 1; J != NumElemsPerConcat; ++J) |
| 10315 | if (SVN->getMaskElt(Begin + J - 1) + 1 != SVN->getMaskElt(Begin + J)) |
| 10316 | return SDValue(); |
| 10317 | |
| 10318 | unsigned FirstElt = SVN->getMaskElt(Begin) / NumElemsPerConcat; |
| 10319 | if (FirstElt < N0.getNumOperands()) |
| 10320 | Ops.push_back(N0.getOperand(FirstElt)); |
| 10321 | else |
| 10322 | Ops.push_back(N1.getOperand(FirstElt - N0.getNumOperands())); |
| 10323 | |
| 10324 | } else if (AllUndef) { |
| 10325 | Ops.push_back(DAG.getUNDEF(N0.getOperand(0).getValueType())); |
| 10326 | } else { // Mixed with general masks and undefs, can't do optimization. |
| 10327 | return SDValue(); |
| 10328 | } |
| Benjamin Kramer | bbae991 | 2013-04-09 17:41:43 +0000 | [diff] [blame] | 10329 | } |
| 10330 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10331 | return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, Ops.data(), |
| Benjamin Kramer | bbae991 | 2013-04-09 17:41:43 +0000 | [diff] [blame] | 10332 | Ops.size()); |
| 10333 | } |
| 10334 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10335 | SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) { |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 10336 | EVT VT = N->getValueType(0); |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 10337 | unsigned NumElts = VT.getVectorNumElements(); |
| Chris Lattner | 39dcf1a | 2006-03-31 22:16:43 +0000 | [diff] [blame] | 10338 | |
| Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 10339 | SDValue N0 = N->getOperand(0); |
| Craig Topper | 279c77b | 2012-01-04 08:07:43 +0000 | [diff] [blame] | 10340 | SDValue N1 = N->getOperand(1); |
| Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 10341 | |
| Craig Topper | 5894fe4 | 2012-04-09 05:16:56 +0000 | [diff] [blame] | 10342 | assert(N0.getValueType() == VT && "Vector shuffle must be normalized in DAG"); |
| Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 10343 | |
| Craig Topper | 279c77b | 2012-01-04 08:07:43 +0000 | [diff] [blame] | 10344 | // Canonicalize shuffle undef, undef -> undef |
| 10345 | if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF) |
| 10346 | return DAG.getUNDEF(VT); |
| 10347 | |
| 10348 | ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); |
| 10349 | |
| 10350 | // Canonicalize shuffle v, v -> v, undef |
| 10351 | if (N0 == N1) { |
| 10352 | SmallVector<int, 8> NewMask; |
| 10353 | for (unsigned i = 0; i != NumElts; ++i) { |
| 10354 | int Idx = SVN->getMaskElt(i); |
| 10355 | if (Idx >= (int)NumElts) Idx -= NumElts; |
| 10356 | NewMask.push_back(Idx); |
| 10357 | } |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10358 | return DAG.getVectorShuffle(VT, SDLoc(N), N0, DAG.getUNDEF(VT), |
| Craig Topper | 279c77b | 2012-01-04 08:07:43 +0000 | [diff] [blame] | 10359 | &NewMask[0]); |
| 10360 | } |
| 10361 | |
| 10362 | // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask. |
| 10363 | if (N0.getOpcode() == ISD::UNDEF) { |
| 10364 | SmallVector<int, 8> NewMask; |
| 10365 | for (unsigned i = 0; i != NumElts; ++i) { |
| 10366 | int Idx = SVN->getMaskElt(i); |
| Craig Topper | e3ad483 | 2012-04-09 05:55:33 +0000 | [diff] [blame] | 10367 | if (Idx >= 0) { |
| Craig Topper | 309dfef | 2013-08-08 07:38:55 +0000 | [diff] [blame] | 10368 | if (Idx >= (int)NumElts) |
| Craig Topper | e3ad483 | 2012-04-09 05:55:33 +0000 | [diff] [blame] | 10369 | Idx -= NumElts; |
| Craig Topper | 309dfef | 2013-08-08 07:38:55 +0000 | [diff] [blame] | 10370 | else |
| 10371 | Idx = -1; // remove reference to lhs |
| Craig Topper | e3ad483 | 2012-04-09 05:55:33 +0000 | [diff] [blame] | 10372 | } |
| 10373 | NewMask.push_back(Idx); |
| Craig Topper | 279c77b | 2012-01-04 08:07:43 +0000 | [diff] [blame] | 10374 | } |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10375 | return DAG.getVectorShuffle(VT, SDLoc(N), N1, DAG.getUNDEF(VT), |
| Craig Topper | 279c77b | 2012-01-04 08:07:43 +0000 | [diff] [blame] | 10376 | &NewMask[0]); |
| 10377 | } |
| 10378 | |
| 10379 | // Remove references to rhs if it is undef |
| 10380 | if (N1.getOpcode() == ISD::UNDEF) { |
| 10381 | bool Changed = false; |
| 10382 | SmallVector<int, 8> NewMask; |
| 10383 | for (unsigned i = 0; i != NumElts; ++i) { |
| 10384 | int Idx = SVN->getMaskElt(i); |
| 10385 | if (Idx >= (int)NumElts) { |
| 10386 | Idx = -1; |
| 10387 | Changed = true; |
| 10388 | } |
| 10389 | NewMask.push_back(Idx); |
| 10390 | } |
| 10391 | if (Changed) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10392 | return DAG.getVectorShuffle(VT, SDLoc(N), N0, N1, &NewMask[0]); |
| Craig Topper | 279c77b | 2012-01-04 08:07:43 +0000 | [diff] [blame] | 10393 | } |
| Evan Cheng | 8472e0c | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 10394 | |
| Bob Wilson | f63da12 | 2010-10-28 17:06:14 +0000 | [diff] [blame] | 10395 | // If it is a splat, check if the argument vector is another splat or a |
| 10396 | // build_vector with all scalar elements the same. |
| Bob Wilson | f63da12 | 2010-10-28 17:06:14 +0000 | [diff] [blame] | 10397 | if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) { |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10398 | SDNode *V = N0.getNode(); |
| Evan Cheng | 7c970b9 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 10399 | |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10400 | // If this is a bit convert that changes the element type of the vector but |
| Evan Cheng | f3ae00a | 2006-10-16 22:49:37 +0000 | [diff] [blame] | 10401 | // not the number of vector elements, look through it. Be careful not to |
| 10402 | // look though conversions that change things like v4f32 to v2f64. |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10403 | if (V->getOpcode() == ISD::BITCAST) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10404 | SDValue ConvInput = V->getOperand(0); |
| Evan Cheng | b8ff223 | 2008-07-22 20:42:56 +0000 | [diff] [blame] | 10405 | if (ConvInput.getValueType().isVector() && |
| 10406 | ConvInput.getValueType().getVectorNumElements() == NumElts) |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10407 | V = ConvInput.getNode(); |
| Evan Cheng | f3ae00a | 2006-10-16 22:49:37 +0000 | [diff] [blame] | 10408 | } |
| 10409 | |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10410 | if (V->getOpcode() == ISD::BUILD_VECTOR) { |
| Bob Wilson | f63da12 | 2010-10-28 17:06:14 +0000 | [diff] [blame] | 10411 | assert(V->getNumOperands() == NumElts && |
| 10412 | "BUILD_VECTOR has wrong number of operands"); |
| 10413 | SDValue Base; |
| 10414 | bool AllSame = true; |
| 10415 | for (unsigned i = 0; i != NumElts; ++i) { |
| 10416 | if (V->getOperand(i).getOpcode() != ISD::UNDEF) { |
| 10417 | Base = V->getOperand(i); |
| 10418 | break; |
| Evan Cheng | 7c970b9 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 10419 | } |
| Evan Cheng | 7c970b9 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 10420 | } |
| Bob Wilson | f63da12 | 2010-10-28 17:06:14 +0000 | [diff] [blame] | 10421 | // Splat of <u, u, u, u>, return <u, u, u, u> |
| 10422 | if (!Base.getNode()) |
| 10423 | return N0; |
| 10424 | for (unsigned i = 0; i != NumElts; ++i) { |
| 10425 | if (V->getOperand(i) != Base) { |
| 10426 | AllSame = false; |
| 10427 | break; |
| 10428 | } |
| 10429 | } |
| 10430 | // Splat of <x, x, x, x>, return <x, x, x, x> |
| 10431 | if (AllSame) |
| 10432 | return N0; |
| Evan Cheng | 7c970b9 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 10433 | } |
| 10434 | } |
| Nadav Rotem | b078350 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 10435 | |
| Benjamin Kramer | bbae991 | 2013-04-09 17:41:43 +0000 | [diff] [blame] | 10436 | if (N0.getOpcode() == ISD::CONCAT_VECTORS && |
| 10437 | Level < AfterLegalizeVectorOps && |
| 10438 | (N1.getOpcode() == ISD::UNDEF || |
| 10439 | (N1.getOpcode() == ISD::CONCAT_VECTORS && |
| 10440 | N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()))) { |
| 10441 | SDValue V = partitionShuffleOfConcats(N, DAG); |
| 10442 | |
| 10443 | if (V.getNode()) |
| 10444 | return V; |
| 10445 | } |
| 10446 | |
| Nadav Rotem | b078350 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 10447 | // If this shuffle node is simply a swizzle of another shuffle node, |
| Nadav Rotem | 71d07ae | 2012-04-07 21:19:08 +0000 | [diff] [blame] | 10448 | // and it reverses the swizzle of the previous shuffle then we can |
| 10449 | // optimize shuffle(shuffle(x, undef), undef) -> x. |
| Nadav Rotem | b078350 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 10450 | if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG && |
| 10451 | N1.getOpcode() == ISD::UNDEF) { |
| 10452 | |
| Nadav Rotem | b078350 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 10453 | ShuffleVectorSDNode *OtherSV = cast<ShuffleVectorSDNode>(N0); |
| 10454 | |
| Nadav Rotem | 71d07ae | 2012-04-07 21:19:08 +0000 | [diff] [blame] | 10455 | // Shuffle nodes can only reverse shuffles with a single non-undef value. |
| 10456 | if (N0.getOperand(1).getOpcode() != ISD::UNDEF) |
| 10457 | return SDValue(); |
| 10458 | |
| Craig Topper | 5894fe4 | 2012-04-09 05:16:56 +0000 | [diff] [blame] | 10459 | // The incoming shuffle must be of the same type as the result of the |
| 10460 | // current shuffle. |
| 10461 | assert(OtherSV->getOperand(0).getValueType() == VT && |
| 10462 | "Shuffle types don't match"); |
| Nadav Rotem | b078350 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 10463 | |
| 10464 | for (unsigned i = 0; i != NumElts; ++i) { |
| 10465 | int Idx = SVN->getMaskElt(i); |
| Craig Topper | 5894fe4 | 2012-04-09 05:16:56 +0000 | [diff] [blame] | 10466 | assert(Idx < (int)NumElts && "Index references undef operand"); |
| Nadav Rotem | b078350 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 10467 | // Next, this index comes from the first value, which is the incoming |
| 10468 | // shuffle. Adopt the incoming index. |
| 10469 | if (Idx >= 0) |
| 10470 | Idx = OtherSV->getMaskElt(Idx); |
| 10471 | |
| Nadav Rotem | 71d07ae | 2012-04-07 21:19:08 +0000 | [diff] [blame] | 10472 | // The combined shuffle must map each index to itself. |
| Craig Topper | 5894fe4 | 2012-04-09 05:16:56 +0000 | [diff] [blame] | 10473 | if (Idx >= 0 && (unsigned)Idx != i) |
| Nadav Rotem | 71d07ae | 2012-04-07 21:19:08 +0000 | [diff] [blame] | 10474 | return SDValue(); |
| Nadav Rotem | b078350 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 10475 | } |
| Nadav Rotem | 71d07ae | 2012-04-07 21:19:08 +0000 | [diff] [blame] | 10476 | |
| 10477 | return OtherSV->getOperand(0); |
| Nadav Rotem | b078350 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 10478 | } |
| 10479 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10480 | return SDValue(); |
| Chris Lattner | 39dcf1a | 2006-03-31 22:16:43 +0000 | [diff] [blame] | 10481 | } |
| 10482 | |
| Manman Ren | 413a6cb | 2014-01-31 01:10:35 +0000 | [diff] [blame] | 10483 | SDValue DAGCombiner::visitINSERT_SUBVECTOR(SDNode *N) { |
| 10484 | SDValue N0 = N->getOperand(0); |
| 10485 | SDValue N2 = N->getOperand(2); |
| 10486 | |
| 10487 | // If the input vector is a concatenation, and the insert replaces |
| 10488 | // one of the halves, we can optimize into a single concat_vectors. |
| 10489 | if (N0.getOpcode() == ISD::CONCAT_VECTORS && |
| 10490 | N0->getNumOperands() == 2 && N2.getOpcode() == ISD::Constant) { |
| 10491 | APInt InsIdx = cast<ConstantSDNode>(N2)->getAPIntValue(); |
| 10492 | EVT VT = N->getValueType(0); |
| 10493 | |
| 10494 | // Lower half: fold (insert_subvector (concat_vectors X, Y), Z) -> |
| 10495 | // (concat_vectors Z, Y) |
| 10496 | if (InsIdx == 0) |
| 10497 | return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, |
| 10498 | N->getOperand(1), N0.getOperand(1)); |
| 10499 | |
| 10500 | // Upper half: fold (insert_subvector (concat_vectors X, Y), Z) -> |
| 10501 | // (concat_vectors X, Z) |
| 10502 | if (InsIdx == VT.getVectorNumElements()/2) |
| 10503 | return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, |
| 10504 | N0.getOperand(0), N->getOperand(1)); |
| 10505 | } |
| 10506 | |
| 10507 | return SDValue(); |
| 10508 | } |
| 10509 | |
| Evan Cheng | a320abc | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10510 | /// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10511 | /// an AND to a vector_shuffle with the destination vector and a zero vector. |
| 10512 | /// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==> |
| Evan Cheng | a320abc | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10513 | /// vector_shuffle V, Zero, <0, 4, 2, 4> |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10514 | SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) { |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 10515 | EVT VT = N->getValueType(0); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10516 | SDLoc dl(N); |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10517 | SDValue LHS = N->getOperand(0); |
| 10518 | SDValue RHS = N->getOperand(1); |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10519 | if (N->getOpcode() == ISD::AND) { |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10520 | if (RHS.getOpcode() == ISD::BITCAST) |
| Evan Cheng | a320abc | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10521 | RHS = RHS.getOperand(0); |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10522 | if (RHS.getOpcode() == ISD::BUILD_VECTOR) { |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 10523 | SmallVector<int, 8> Indices; |
| 10524 | unsigned NumElts = RHS.getNumOperands(); |
| Evan Cheng | a320abc | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10525 | for (unsigned i = 0; i != NumElts; ++i) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10526 | SDValue Elt = RHS.getOperand(i); |
| Evan Cheng | a320abc | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10527 | if (!isa<ConstantSDNode>(Elt)) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10528 | return SDValue(); |
| Craig Topper | e5893f6 | 2012-04-09 05:59:53 +0000 | [diff] [blame] | 10529 | |
| 10530 | if (cast<ConstantSDNode>(Elt)->isAllOnesValue()) |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 10531 | Indices.push_back(i); |
| Evan Cheng | a320abc | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10532 | else if (cast<ConstantSDNode>(Elt)->isNullValue()) |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 10533 | Indices.push_back(NumElts); |
| Evan Cheng | a320abc | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10534 | else |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10535 | return SDValue(); |
| Evan Cheng | a320abc | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10536 | } |
| 10537 | |
| 10538 | // Let's see if the target supports this vector_shuffle. |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 10539 | EVT RVT = RHS.getValueType(); |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 10540 | if (!TLI.isVectorClearMaskLegal(Indices, RVT)) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10541 | return SDValue(); |
| Evan Cheng | a320abc | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10542 | |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10543 | // Return the new VECTOR_SHUFFLE node. |
| Dan Gohman | 08c0a95 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 10544 | EVT EltVT = RVT.getVectorElementType(); |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 10545 | SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(), |
| Dan Gohman | 08c0a95 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 10546 | DAG.getConstant(0, EltVT)); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10547 | SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 10548 | RVT, &ZeroOps[0], ZeroOps.size()); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10549 | LHS = DAG.getNode(ISD::BITCAST, dl, RVT, LHS); |
| Nate Begeman | 8d6d4b9 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 10550 | SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10551 | return DAG.getNode(ISD::BITCAST, dl, VT, Shuf); |
| Evan Cheng | a320abc | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10552 | } |
| 10553 | } |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10554 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10555 | return SDValue(); |
| Evan Cheng | a320abc | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10556 | } |
| 10557 | |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10558 | /// SimplifyVBinOp - Visit a binary vector operation, like ADD. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10559 | SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) { |
| Bob Wilson | 5408144 | 2010-12-17 23:06:49 +0000 | [diff] [blame] | 10560 | assert(N->getValueType(0).isVector() && |
| 10561 | "SimplifyVBinOp only works on vectors!"); |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10562 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10563 | SDValue LHS = N->getOperand(0); |
| 10564 | SDValue RHS = N->getOperand(1); |
| 10565 | SDValue Shuffle = XformToShuffleWithZero(N); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10566 | if (Shuffle.getNode()) return Shuffle; |
| Evan Cheng | a320abc | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10567 | |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10568 | // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold |
| Chris Lattner | 0442a18 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 10569 | // this operation. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10570 | if (LHS.getOpcode() == ISD::BUILD_VECTOR && |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10571 | RHS.getOpcode() == ISD::BUILD_VECTOR) { |
| Juergen Ributzka | 7384405 | 2014-01-13 20:51:35 +0000 | [diff] [blame] | 10572 | // Check if both vectors are constants. If not bail out. |
| Andrea Di Biagio | d7c03ec | 2014-01-15 19:51:32 +0000 | [diff] [blame] | 10573 | if (!(cast<BuildVectorSDNode>(LHS)->isConstant() && |
| 10574 | cast<BuildVectorSDNode>(RHS)->isConstant())) |
| Juergen Ributzka | 7384405 | 2014-01-13 20:51:35 +0000 | [diff] [blame] | 10575 | return SDValue(); |
| 10576 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10577 | SmallVector<SDValue, 8> Ops; |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10578 | for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10579 | SDValue LHSOp = LHS.getOperand(i); |
| 10580 | SDValue RHSOp = RHS.getOperand(i); |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10581 | |
| Evan Cheng | 64d2846 | 2006-05-31 06:08:35 +0000 | [diff] [blame] | 10582 | // Can't fold divide by zero. |
| Dan Gohman | a866514 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10583 | if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV || |
| 10584 | N->getOpcode() == ISD::FDIV) { |
| Evan Cheng | 64d2846 | 2006-05-31 06:08:35 +0000 | [diff] [blame] | 10585 | if ((RHSOp.getOpcode() == ISD::Constant && |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10586 | cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) || |
| Evan Cheng | 64d2846 | 2006-05-31 06:08:35 +0000 | [diff] [blame] | 10587 | (RHSOp.getOpcode() == ISD::ConstantFP && |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10588 | cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero())) |
| Evan Cheng | 64d2846 | 2006-05-31 06:08:35 +0000 | [diff] [blame] | 10589 | break; |
| 10590 | } |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10591 | |
| Bob Wilson | 5408144 | 2010-12-17 23:06:49 +0000 | [diff] [blame] | 10592 | EVT VT = LHSOp.getValueType(); |
| Bob Wilson | 6815619 | 2011-10-18 17:34:47 +0000 | [diff] [blame] | 10593 | EVT RVT = RHSOp.getValueType(); |
| 10594 | if (RVT != VT) { |
| 10595 | // Integer BUILD_VECTOR operands may have types larger than the element |
| 10596 | // size (e.g., when the element type is not legal). Prior to type |
| 10597 | // legalization, the types may not match between the two BUILD_VECTORS. |
| 10598 | // Truncate one of the operands to make them match. |
| 10599 | if (RVT.getSizeInBits() > VT.getSizeInBits()) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10600 | RHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, RHSOp); |
| Bob Wilson | 6815619 | 2011-10-18 17:34:47 +0000 | [diff] [blame] | 10601 | } else { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10602 | LHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), RVT, LHSOp); |
| Bob Wilson | 6815619 | 2011-10-18 17:34:47 +0000 | [diff] [blame] | 10603 | VT = RVT; |
| 10604 | } |
| 10605 | } |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10606 | SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(LHS), VT, |
| Evan Cheng | 48f0de9 | 2010-05-18 00:03:40 +0000 | [diff] [blame] | 10607 | LHSOp, RHSOp); |
| 10608 | if (FoldOp.getOpcode() != ISD::UNDEF && |
| 10609 | FoldOp.getOpcode() != ISD::Constant && |
| 10610 | FoldOp.getOpcode() != ISD::ConstantFP) |
| 10611 | break; |
| 10612 | Ops.push_back(FoldOp); |
| 10613 | AddToWorkList(FoldOp.getNode()); |
| Chris Lattner | 0442a18 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 10614 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10615 | |
| Bob Wilson | 5408144 | 2010-12-17 23:06:49 +0000 | [diff] [blame] | 10616 | if (Ops.size() == LHS.getNumOperands()) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10617 | return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), |
| Bob Wilson | 5408144 | 2010-12-17 23:06:49 +0000 | [diff] [blame] | 10618 | LHS.getValueType(), &Ops[0], Ops.size()); |
| Chris Lattner | 0442a18 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 10619 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10620 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10621 | return SDValue(); |
| Chris Lattner | 0442a18 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 10622 | } |
| 10623 | |
| Craig Topper | 8238461 | 2012-09-11 01:45:21 +0000 | [diff] [blame] | 10624 | /// SimplifyVUnaryOp - Visit a binary vector operation, like FABS/FNEG. |
| 10625 | SDValue DAGCombiner::SimplifyVUnaryOp(SDNode *N) { |
| Craig Topper | 8238461 | 2012-09-11 01:45:21 +0000 | [diff] [blame] | 10626 | assert(N->getValueType(0).isVector() && |
| 10627 | "SimplifyVUnaryOp only works on vectors!"); |
| 10628 | |
| 10629 | SDValue N0 = N->getOperand(0); |
| 10630 | |
| 10631 | if (N0.getOpcode() != ISD::BUILD_VECTOR) |
| 10632 | return SDValue(); |
| 10633 | |
| 10634 | // Operand is a BUILD_VECTOR node, see if we can constant fold it. |
| 10635 | SmallVector<SDValue, 8> Ops; |
| 10636 | for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) { |
| 10637 | SDValue Op = N0.getOperand(i); |
| 10638 | if (Op.getOpcode() != ISD::UNDEF && |
| 10639 | Op.getOpcode() != ISD::ConstantFP) |
| 10640 | break; |
| 10641 | EVT EltVT = Op.getValueType(); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10642 | SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(N0), EltVT, Op); |
| Craig Topper | 8238461 | 2012-09-11 01:45:21 +0000 | [diff] [blame] | 10643 | if (FoldOp.getOpcode() != ISD::UNDEF && |
| 10644 | FoldOp.getOpcode() != ISD::ConstantFP) |
| 10645 | break; |
| 10646 | Ops.push_back(FoldOp); |
| 10647 | AddToWorkList(FoldOp.getNode()); |
| 10648 | } |
| 10649 | |
| 10650 | if (Ops.size() != N0.getNumOperands()) |
| 10651 | return SDValue(); |
| 10652 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10653 | return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), |
| Craig Topper | 8238461 | 2012-09-11 01:45:21 +0000 | [diff] [blame] | 10654 | N0.getValueType(), &Ops[0], Ops.size()); |
| 10655 | } |
| 10656 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10657 | SDValue DAGCombiner::SimplifySelect(SDLoc DL, SDValue N0, |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10658 | SDValue N1, SDValue N2){ |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10659 | assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!"); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10660 | |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10661 | SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2, |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10662 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10663 | |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10664 | // If we got a simplified select_cc node back from SimplifySelectCC, then |
| 10665 | // break it down into a new SETCC node, and a new SELECT node, and then return |
| 10666 | // the SELECT node, since we were called with a SELECT node. |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10667 | if (SCC.getNode()) { |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10668 | // Check to see if we got a select_cc back (to turn into setcc/select). |
| 10669 | // Otherwise, just return whatever node we got back, like fabs. |
| 10670 | if (SCC.getOpcode() == ISD::SELECT_CC) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10671 | SDValue SETCC = DAG.getNode(ISD::SETCC, SDLoc(N0), |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10672 | N0.getValueType(), |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10673 | SCC.getOperand(0), SCC.getOperand(1), |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10674 | SCC.getOperand(4)); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10675 | AddToWorkList(SETCC.getNode()); |
| Matt Arsenault | d2f0332 | 2013-06-14 22:04:37 +0000 | [diff] [blame] | 10676 | return DAG.getSelect(SDLoc(SCC), SCC.getValueType(), |
| 10677 | SCC.getOperand(2), SCC.getOperand(3), SETCC); |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10678 | } |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10679 | |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10680 | return SCC; |
| 10681 | } |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10682 | return SDValue(); |
| Nate Begeman | c760f80 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 10683 | } |
| 10684 | |
| Chris Lattner | 6c14c35 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 10685 | /// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS |
| 10686 | /// are the two values being selected between, see if we can simplify the |
| Chris Lattner | 8f872d2 | 2006-05-27 00:43:02 +0000 | [diff] [blame] | 10687 | /// select. Callers of this should assume that TheSelect is deleted if this |
| 10688 | /// returns true. As such, they should return the appropriate thing (e.g. the |
| 10689 | /// node) back to the top-level of the DAG combiner loop to avoid it being |
| 10690 | /// looked at. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10691 | bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS, |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10692 | SDValue RHS) { |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10693 | |
| Nadav Rotem | a49a02a | 2011-02-11 19:57:47 +0000 | [diff] [blame] | 10694 | // Cannot simplify select with vector condition |
| 10695 | if (TheSelect->getOperand(0).getValueType().isVector()) return false; |
| 10696 | |
| Chris Lattner | 6c14c35 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 10697 | // If this is a select from two identical things, try to pull the operation |
| 10698 | // through the select. |
| Chris Lattner | 254c445 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 10699 | if (LHS.getOpcode() != RHS.getOpcode() || |
| 10700 | !LHS.hasOneUse() || !RHS.hasOneUse()) |
| 10701 | return false; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10702 | |
| Chris Lattner | 254c445 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 10703 | // If this is a load and the token chain is identical, replace the select |
| 10704 | // of two loads with a load through a select of the address to load from. |
| 10705 | // This triggers in things like "select bool X, 10.0, 123.0" after the FP |
| 10706 | // constants have been dropped into the constant pool. |
| 10707 | if (LHS.getOpcode() == ISD::LOAD) { |
| 10708 | LoadSDNode *LLD = cast<LoadSDNode>(LHS); |
| 10709 | LoadSDNode *RLD = cast<LoadSDNode>(RHS); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10710 | |
| Chris Lattner | 254c445 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 10711 | // Token chains must be identical. |
| 10712 | if (LHS.getOperand(0) != RHS.getOperand(0) || |
| Duncan Sands | 8651e9c | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 10713 | // Do not let this transformation reduce the number of volatile loads. |
| Chris Lattner | 254c445 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 10714 | LLD->isVolatile() || RLD->isVolatile() || |
| 10715 | // If this is an EXTLOAD, the VT's must match. |
| 10716 | LLD->getMemoryVT() != RLD->getMemoryVT() || |
| Duncan Sands | 12f3b3b | 2010-11-18 20:05:18 +0000 | [diff] [blame] | 10717 | // If this is an EXTLOAD, the kind of extension must match. |
| 10718 | (LLD->getExtensionType() != RLD->getExtensionType() && |
| 10719 | // The only exception is if one of the extensions is anyext. |
| 10720 | LLD->getExtensionType() != ISD::EXTLOAD && |
| 10721 | RLD->getExtensionType() != ISD::EXTLOAD) || |
| Dan Gohman | ba8735d | 2009-10-31 14:14:04 +0000 | [diff] [blame] | 10722 | // FIXME: this discards src value information. This is |
| 10723 | // over-conservative. It would be beneficial to be able to remember |
| Mon P Wang | ec57c81 | 2010-01-11 20:12:49 +0000 | [diff] [blame] | 10724 | // both potential memory locations. Since we are discarding |
| 10725 | // src value info, don't do the transformation if the memory |
| 10726 | // locations are not in the default address space. |
| Chris Lattner | 254c445 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 10727 | LLD->getPointerInfo().getAddrSpace() != 0 || |
| Pete Cooper | 10a3ae7 | 2013-02-12 03:14:50 +0000 | [diff] [blame] | 10728 | RLD->getPointerInfo().getAddrSpace() != 0 || |
| 10729 | !TLI.isOperationLegalOrCustom(TheSelect->getOpcode(), |
| 10730 | LLD->getBasePtr().getValueType())) |
| Chris Lattner | 254c445 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 10731 | return false; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10732 | |
| Chris Lattner | e326752 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 10733 | // Check that the select condition doesn't reach either load. If so, |
| 10734 | // folding this will induce a cycle into the DAG. If not, this is safe to |
| 10735 | // xform, so create a select of the addresses. |
| Chris Lattner | 254c445 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 10736 | SDValue Addr; |
| 10737 | if (TheSelect->getOpcode() == ISD::SELECT) { |
| Chris Lattner | e326752 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 10738 | SDNode *CondNode = TheSelect->getOperand(0).getNode(); |
| 10739 | if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) || |
| 10740 | (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode))) |
| 10741 | return false; |
| Nadav Rotem | d5f8859 | 2012-10-18 18:06:48 +0000 | [diff] [blame] | 10742 | // The loads must not depend on one another. |
| 10743 | if (LLD->isPredecessorOf(RLD) || |
| 10744 | RLD->isPredecessorOf(LLD)) |
| 10745 | return false; |
| Matt Arsenault | d2f0332 | 2013-06-14 22:04:37 +0000 | [diff] [blame] | 10746 | Addr = DAG.getSelect(SDLoc(TheSelect), |
| 10747 | LLD->getBasePtr().getValueType(), |
| 10748 | TheSelect->getOperand(0), LLD->getBasePtr(), |
| 10749 | RLD->getBasePtr()); |
| Chris Lattner | 254c445 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 10750 | } else { // Otherwise SELECT_CC |
| Chris Lattner | e326752 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 10751 | SDNode *CondLHS = TheSelect->getOperand(0).getNode(); |
| 10752 | SDNode *CondRHS = TheSelect->getOperand(1).getNode(); |
| 10753 | |
| 10754 | if ((LLD->hasAnyUseOfValue(1) && |
| 10755 | (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) || |
| Chris Lattner | 1cc25e8 | 2012-03-27 16:27:21 +0000 | [diff] [blame] | 10756 | (RLD->hasAnyUseOfValue(1) && |
| 10757 | (RLD->isPredecessorOf(CondLHS) || RLD->isPredecessorOf(CondRHS)))) |
| Chris Lattner | e326752 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 10758 | return false; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10759 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10760 | Addr = DAG.getNode(ISD::SELECT_CC, SDLoc(TheSelect), |
| Chris Lattner | e326752 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 10761 | LLD->getBasePtr().getValueType(), |
| 10762 | TheSelect->getOperand(0), |
| 10763 | TheSelect->getOperand(1), |
| 10764 | LLD->getBasePtr(), RLD->getBasePtr(), |
| 10765 | TheSelect->getOperand(4)); |
| Chris Lattner | 254c445 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 10766 | } |
| 10767 | |
| Chris Lattner | e326752 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 10768 | SDValue Load; |
| 10769 | if (LLD->getExtensionType() == ISD::NON_EXTLOAD) { |
| 10770 | Load = DAG.getLoad(TheSelect->getValueType(0), |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10771 | SDLoc(TheSelect), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 10772 | // FIXME: Discards pointer and TBAA info. |
| Chris Lattner | e326752 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 10773 | LLD->getChain(), Addr, MachinePointerInfo(), |
| 10774 | LLD->isVolatile(), LLD->isNonTemporal(), |
| Pete Cooper | 82cd9e8 | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 10775 | LLD->isInvariant(), LLD->getAlignment()); |
| Chris Lattner | e326752 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 10776 | } else { |
| Duncan Sands | c92331b | 2010-11-18 21:16:28 +0000 | [diff] [blame] | 10777 | Load = DAG.getExtLoad(LLD->getExtensionType() == ISD::EXTLOAD ? |
| 10778 | RLD->getExtensionType() : LLD->getExtensionType(), |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10779 | SDLoc(TheSelect), |
| Stuart Hastings | 81c4306 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 10780 | TheSelect->getValueType(0), |
| Richard Sandiford | 39c1ce4 | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 10781 | // FIXME: Discards pointer and TBAA info. |
| Chris Lattner | e326752 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 10782 | LLD->getChain(), Addr, MachinePointerInfo(), |
| 10783 | LLD->getMemoryVT(), LLD->isVolatile(), |
| 10784 | LLD->isNonTemporal(), LLD->getAlignment()); |
| Chris Lattner | 6c14c35 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 10785 | } |
| Chris Lattner | e326752 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 10786 | |
| 10787 | // Users of the select now use the result of the load. |
| 10788 | CombineTo(TheSelect, Load); |
| 10789 | |
| 10790 | // Users of the old loads now use the new load's chain. We know the |
| 10791 | // old-load value is dead now. |
| 10792 | CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1)); |
| 10793 | CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1)); |
| 10794 | return true; |
| Chris Lattner | 6c14c35 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 10795 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10796 | |
| Chris Lattner | 6c14c35 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 10797 | return false; |
| 10798 | } |
| 10799 | |
| Chris Lattner | 43d6377 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10800 | /// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3 |
| 10801 | /// where 'cond' is the comparison specified by CC. |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10802 | SDValue DAGCombiner::SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1, |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10803 | SDValue N2, SDValue N3, |
| 10804 | ISD::CondCode CC, bool NotExtCompare) { |
| Chris Lattner | 43d6377 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10805 | // (x ? y : y) -> y. |
| 10806 | if (N2 == N3) return N2; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10807 | |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 10808 | EVT VT = N2.getValueType(); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10809 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); |
| 10810 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode()); |
| 10811 | ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode()); |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10812 | |
| 10813 | // Determine if the condition we're dealing with is constant |
| Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 10814 | SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()), |
| Dale Johannesen | f1163e9 | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 10815 | N0, N1, CC, DL, false); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10816 | if (SCC.getNode()) AddToWorkList(SCC.getNode()); |
| 10817 | ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode()); |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10818 | |
| 10819 | // fold select_cc true, x, y -> x |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 10820 | if (SCCC && !SCCC->isNullValue()) |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10821 | return N2; |
| 10822 | // fold select_cc false, x, y -> y |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 10823 | if (SCCC && SCCC->isNullValue()) |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10824 | return N3; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10825 | |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10826 | // Check to see if we can simplify the select into an fabs node |
| 10827 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) { |
| 10828 | // Allow either -0.0 or 0.0 |
| Dale Johannesen | 2cfcf70 | 2007-08-25 22:10:57 +0000 | [diff] [blame] | 10829 | if (CFP->getValueAPF().isZero()) { |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10830 | // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs |
| 10831 | if ((CC == ISD::SETGE || CC == ISD::SETGT) && |
| 10832 | N0 == N2 && N3.getOpcode() == ISD::FNEG && |
| 10833 | N2 == N3.getOperand(0)) |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10834 | return DAG.getNode(ISD::FABS, DL, VT, N0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10835 | |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10836 | // select (setl[te] X, +/-0.0), fneg(X), X -> fabs |
| 10837 | if ((CC == ISD::SETLT || CC == ISD::SETLE) && |
| 10838 | N0 == N3 && N2.getOpcode() == ISD::FNEG && |
| 10839 | N2.getOperand(0) == N3) |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10840 | return DAG.getNode(ISD::FABS, DL, VT, N3); |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10841 | } |
| 10842 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10843 | |
| Chris Lattner | 43d6377 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10844 | // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)" |
| 10845 | // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0 |
| 10846 | // in it. This is a win when the constant is not otherwise available because |
| 10847 | // it replaces two constant pool loads with one. We only do this if the FP |
| 10848 | // type is known to be legal, because if it isn't, then we are before legalize |
| 10849 | // types an we want the other legalization to happen first (e.g. to avoid |
| Mon P Wang | c867156 | 2009-03-14 00:25:19 +0000 | [diff] [blame] | 10850 | // messing with soft float) and if the ConstantFP is not legal, because if |
| 10851 | // it is legal, we may not need to store the FP constant in a constant pool. |
| Chris Lattner | 43d6377 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10852 | if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2)) |
| 10853 | if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) { |
| 10854 | if (TLI.isTypeLegal(N2.getValueType()) && |
| Mon P Wang | c867156 | 2009-03-14 00:25:19 +0000 | [diff] [blame] | 10855 | (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) != |
| 10856 | TargetLowering::Legal) && |
| Chris Lattner | 43d6377 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10857 | // If both constants have multiple uses, then we won't need to do an |
| 10858 | // extra load, they are likely around in registers for other users. |
| 10859 | (TV->hasOneUse() || FV->hasOneUse())) { |
| 10860 | Constant *Elts[] = { |
| 10861 | const_cast<ConstantFP*>(FV->getConstantFPValue()), |
| 10862 | const_cast<ConstantFP*>(TV->getConstantFPValue()) |
| 10863 | }; |
| Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 10864 | Type *FPTy = Elts[0]->getType(); |
| Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 10865 | const DataLayout &TD = *TLI.getDataLayout(); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10866 | |
| Chris Lattner | 43d6377 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10867 | // Create a ConstantArray of the two constants. |
| Jay Foad | 83be361 | 2011-06-22 09:24:39 +0000 | [diff] [blame] | 10868 | Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts); |
| Chris Lattner | 43d6377 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10869 | SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(), |
| 10870 | TD.getPrefTypeAlignment(FPTy)); |
| Evan Cheng | 1fb8aed | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 10871 | unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); |
| Chris Lattner | 43d6377 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10872 | |
| 10873 | // Get the offsets to the 0 and 1 element of the array so that we can |
| 10874 | // select between them. |
| 10875 | SDValue Zero = DAG.getIntPtrConstant(0); |
| Duncan Sands | af9eaa8 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 10876 | unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType()); |
| Chris Lattner | 43d6377 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10877 | SDValue One = DAG.getIntPtrConstant(EltSize); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10878 | |
| Chris Lattner | 43d6377 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10879 | SDValue Cond = DAG.getSetCC(DL, |
| Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 10880 | getSetCCResultType(N0.getValueType()), |
| Chris Lattner | 43d6377 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10881 | N0, N1, CC); |
| Dan Gohman | e83e1b2 | 2011-09-22 23:01:29 +0000 | [diff] [blame] | 10882 | AddToWorkList(Cond.getNode()); |
| Matt Arsenault | d2f0332 | 2013-06-14 22:04:37 +0000 | [diff] [blame] | 10883 | SDValue CstOffset = DAG.getSelect(DL, Zero.getValueType(), |
| 10884 | Cond, One, Zero); |
| Dan Gohman | e83e1b2 | 2011-09-22 23:01:29 +0000 | [diff] [blame] | 10885 | AddToWorkList(CstOffset.getNode()); |
| Tom Stellard | 838e234 | 2013-08-26 15:06:10 +0000 | [diff] [blame] | 10886 | CPIdx = DAG.getNode(ISD::ADD, DL, CPIdx.getValueType(), CPIdx, |
| Chris Lattner | 43d6377 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10887 | CstOffset); |
| Dan Gohman | e83e1b2 | 2011-09-22 23:01:29 +0000 | [diff] [blame] | 10888 | AddToWorkList(CPIdx.getNode()); |
| Chris Lattner | 43d6377 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10889 | return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx, |
| Chris Lattner | a35499e | 2010-09-21 07:32:19 +0000 | [diff] [blame] | 10890 | MachinePointerInfo::getConstantPool(), false, |
| Pete Cooper | 82cd9e8 | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 10891 | false, false, Alignment); |
| Chris Lattner | 43d6377 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10892 | |
| 10893 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10894 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10895 | |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10896 | // Check to see if we can perform the "gzip trick", transforming |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10897 | // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A) |
| Chris Lattner | c8cd62d | 2006-09-20 06:41:35 +0000 | [diff] [blame] | 10898 | if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT && |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 10899 | (N1C->isNullValue() || // (a < 0) ? b : 0 |
| 10900 | (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0 |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 10901 | EVT XType = N0.getValueType(); |
| 10902 | EVT AType = N2.getValueType(); |
| Duncan Sands | 11dd424 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 10903 | if (XType.bitsGE(AType)) { |
| Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 10904 | // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a |
| Nate Begeman | 6828ed9 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 10905 | // single-bit constant. |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 10906 | if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) { |
| 10907 | unsigned ShCtV = N2C->getAPIntValue().logBase2(); |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 10908 | ShCtV = XType.getSizeInBits()-ShCtV-1; |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 10909 | SDValue ShCt = DAG.getConstant(ShCtV, |
| 10910 | getShiftAmountTy(N0.getValueType())); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10911 | SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0), |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10912 | XType, N0, ShCt); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10913 | AddToWorkList(Shift.getNode()); |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10914 | |
| Duncan Sands | 11dd424 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 10915 | if (XType.bitsGT(AType)) { |
| Bill Wendling | 3b585af | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 10916 | Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10917 | AddToWorkList(Shift.getNode()); |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10918 | } |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10919 | |
| 10920 | return DAG.getNode(ISD::AND, DL, AType, Shift, N2); |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10921 | } |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10922 | |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10923 | SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0), |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10924 | XType, N0, |
| 10925 | DAG.getConstant(XType.getSizeInBits()-1, |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 10926 | getShiftAmountTy(N0.getValueType()))); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10927 | AddToWorkList(Shift.getNode()); |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10928 | |
| Duncan Sands | 11dd424 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 10929 | if (XType.bitsGT(AType)) { |
| Bill Wendling | 3b585af | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 10930 | Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift); |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10931 | AddToWorkList(Shift.getNode()); |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10932 | } |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10933 | |
| 10934 | return DAG.getNode(ISD::AND, DL, AType, Shift, N2); |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10935 | } |
| 10936 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10937 | |
| Owen Anderson | 3231d13 | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 10938 | // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A) |
| 10939 | // where y is has a single bit set. |
| 10940 | // A plaintext description would be, we can turn the SELECT_CC into an AND |
| 10941 | // when the condition can be materialized as an all-ones register. Any |
| 10942 | // single bit-test can be materialized as an all-ones register with |
| 10943 | // shift-left and shift-right-arith. |
| 10944 | if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND && |
| 10945 | N0->getValueType(0) == VT && |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10946 | N1C && N1C->isNullValue() && |
| Owen Anderson | 3231d13 | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 10947 | N2C && N2C->isNullValue()) { |
| 10948 | SDValue AndLHS = N0->getOperand(0); |
| 10949 | ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1)); |
| 10950 | if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) { |
| 10951 | // Shift the tested bit over the sign bit. |
| 10952 | APInt AndMask = ConstAndRHS->getAPIntValue(); |
| 10953 | SDValue ShlAmt = |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 10954 | DAG.getConstant(AndMask.countLeadingZeros(), |
| 10955 | getShiftAmountTy(AndLHS.getValueType())); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10956 | SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(N0), VT, AndLHS, ShlAmt); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10957 | |
| Owen Anderson | 3231d13 | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 10958 | // Now arithmetic right shift it all the way over, so the result is either |
| 10959 | // all-ones, or zero. |
| 10960 | SDValue ShrAmt = |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 10961 | DAG.getConstant(AndMask.getBitWidth()-1, |
| 10962 | getShiftAmountTy(Shl.getValueType())); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10963 | SDValue Shr = DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, ShrAmt); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10964 | |
| Owen Anderson | 3231d13 | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 10965 | return DAG.getNode(ISD::AND, DL, VT, Shr, N3); |
| 10966 | } |
| 10967 | } |
| 10968 | |
| Nate Begeman | 6828ed9 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 10969 | // fold select C, 16, 0 -> shl C, 4 |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 10970 | if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() && |
| Duncan Sands | f2641e1 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 10971 | TLI.getBooleanContents(N0.getValueType().isVector()) == |
| 10972 | TargetLowering::ZeroOrOneBooleanContent) { |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10973 | |
| Chris Lattner | a083ffc | 2007-04-11 06:50:51 +0000 | [diff] [blame] | 10974 | // If the caller doesn't want us to simplify this into a zext of a compare, |
| 10975 | // don't do it. |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 10976 | if (NotExtCompare && N2C->getAPIntValue() == 1) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10977 | return SDValue(); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10978 | |
| Nate Begeman | 6828ed9 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 10979 | // Get a SetCC of the condition |
| Owen Anderson | 15fd6ac | 2012-11-03 00:17:26 +0000 | [diff] [blame] | 10980 | // NOTE: Don't create a SETCC if it's not legal on this target. |
| 10981 | if (!LegalOperations || |
| 10982 | TLI.isOperationLegal(ISD::SETCC, |
| Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 10983 | LegalTypes ? getSetCCResultType(N0.getValueType()) : MVT::i1)) { |
| Owen Anderson | 15fd6ac | 2012-11-03 00:17:26 +0000 | [diff] [blame] | 10984 | SDValue Temp, SCC; |
| 10985 | // cast from setcc result type to select result type |
| 10986 | if (LegalTypes) { |
| Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 10987 | SCC = DAG.getSetCC(DL, getSetCCResultType(N0.getValueType()), |
| Owen Anderson | 15fd6ac | 2012-11-03 00:17:26 +0000 | [diff] [blame] | 10988 | N0, N1, CC); |
| 10989 | if (N2.getValueType().bitsLT(SCC.getValueType())) |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10990 | Temp = DAG.getZeroExtendInReg(SCC, SDLoc(N2), |
| Owen Anderson | 15fd6ac | 2012-11-03 00:17:26 +0000 | [diff] [blame] | 10991 | N2.getValueType()); |
| 10992 | else |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10993 | Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2), |
| Owen Anderson | 15fd6ac | 2012-11-03 00:17:26 +0000 | [diff] [blame] | 10994 | N2.getValueType(), SCC); |
| 10995 | } else { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10996 | SCC = DAG.getSetCC(SDLoc(N0), MVT::i1, N0, N1, CC); |
| 10997 | Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2), |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10998 | N2.getValueType(), SCC); |
| Owen Anderson | 15fd6ac | 2012-11-03 00:17:26 +0000 | [diff] [blame] | 10999 | } |
| 11000 | |
| 11001 | AddToWorkList(SCC.getNode()); |
| 11002 | AddToWorkList(Temp.getNode()); |
| 11003 | |
| 11004 | if (N2C->getAPIntValue() == 1) |
| 11005 | return Temp; |
| 11006 | |
| 11007 | // shl setcc result by log2 n2c |
| Jack Carter | d4e9615 | 2013-10-17 01:34:33 +0000 | [diff] [blame] | 11008 | return DAG.getNode( |
| 11009 | ISD::SHL, DL, N2.getValueType(), Temp, |
| 11010 | DAG.getConstant(N2C->getAPIntValue().logBase2(), |
| 11011 | getShiftAmountTy(Temp.getValueType()))); |
| Nate Begeman | abac616 | 2006-02-18 02:40:58 +0000 | [diff] [blame] | 11012 | } |
| Nate Begeman | 6828ed9 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 11013 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11014 | |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 11015 | // Check to see if this is the equivalent of setcc |
| 11016 | // FIXME: Turn all of these into setcc if setcc if setcc is legal |
| 11017 | // otherwise, go ahead with the folds. |
| Dan Gohman | b72127a | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 11018 | if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) { |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 11019 | EVT XType = N0.getValueType(); |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 11020 | if (!LegalOperations || |
| Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 11021 | TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(XType))) { |
| 11022 | SDValue Res = DAG.getSetCC(DL, getSetCCResultType(XType), N0, N1, CC); |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 11023 | if (Res.getValueType() != VT) |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 11024 | Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res); |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 11025 | return Res; |
| 11026 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11027 | |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 11028 | // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X)))) |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11029 | if (N1C && N1C->isNullValue() && CC == ISD::SETEQ && |
| Duncan Sands | dc2dac1 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 11030 | (!LegalOperations || |
| Duncan Sands | b1bfff5 | 2008-06-14 17:48:34 +0000 | [diff] [blame] | 11031 | TLI.isOperationLegal(ISD::CTLZ, XType))) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11032 | SDValue Ctlz = DAG.getNode(ISD::CTLZ, SDLoc(N0), XType, N0); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11033 | return DAG.getNode(ISD::SRL, DL, XType, Ctlz, |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 11034 | DAG.getConstant(Log2_32(XType.getSizeInBits()), |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 11035 | getShiftAmountTy(Ctlz.getValueType()))); |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 11036 | } |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 11037 | // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1)) |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11038 | if (N1C && N1C->isNullValue() && CC == ISD::SETGT) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11039 | SDValue NegN0 = DAG.getNode(ISD::SUB, SDLoc(N0), |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 11040 | XType, DAG.getConstant(0, XType), N0); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11041 | SDValue NotN0 = DAG.getNOT(SDLoc(N0), N0, XType); |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 11042 | return DAG.getNode(ISD::SRL, DL, XType, |
| Bill Wendling | a6c75ff | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 11043 | DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0), |
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 11044 | DAG.getConstant(XType.getSizeInBits()-1, |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 11045 | getShiftAmountTy(XType))); |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 11046 | } |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 11047 | // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1)) |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 11048 | if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11049 | SDValue Sign = DAG.getNode(ISD::SRL, SDLoc(N0), XType, N0, |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 11050 | DAG.getConstant(XType.getSizeInBits()-1, |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 11051 | getShiftAmountTy(N0.getValueType()))); |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 11052 | return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType)); |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 11053 | } |
| 11054 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11055 | |
| Benjamin Kramer | 0ae3f08 | 2010-07-08 12:09:56 +0000 | [diff] [blame] | 11056 | // Check to see if this is an integer abs. |
| 11057 | // select_cc setg[te] X, 0, X, -X -> |
| 11058 | // select_cc setgt X, -1, X, -X -> |
| 11059 | // select_cc setl[te] X, 0, -X, X -> |
| 11060 | // select_cc setlt X, 1, -X, X -> |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 11061 | // Y = sra (X, size(X)-1); xor (add (X, Y), Y) |
| Benjamin Kramer | 0ae3f08 | 2010-07-08 12:09:56 +0000 | [diff] [blame] | 11062 | if (N1C) { |
| 11063 | ConstantSDNode *SubC = NULL; |
| 11064 | if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) || |
| 11065 | (N1C->isAllOnesValue() && CC == ISD::SETGT)) && |
| 11066 | N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) |
| 11067 | SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0)); |
| 11068 | else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) || |
| 11069 | (N1C->isOne() && CC == ISD::SETLT)) && |
| 11070 | N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) |
| 11071 | SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0)); |
| 11072 | |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 11073 | EVT XType = N0.getValueType(); |
| Benjamin Kramer | 0ae3f08 | 2010-07-08 12:09:56 +0000 | [diff] [blame] | 11074 | if (SubC && SubC->isNullValue() && XType.isInteger()) { |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11075 | SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0), XType, |
| Benjamin Kramer | 0ae3f08 | 2010-07-08 12:09:56 +0000 | [diff] [blame] | 11076 | N0, |
| 11077 | DAG.getConstant(XType.getSizeInBits()-1, |
| Owen Anderson | b2c80da | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 11078 | getShiftAmountTy(N0.getValueType()))); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11079 | SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N0), |
| Benjamin Kramer | 0ae3f08 | 2010-07-08 12:09:56 +0000 | [diff] [blame] | 11080 | XType, N0, Shift); |
| 11081 | AddToWorkList(Shift.getNode()); |
| 11082 | AddToWorkList(Add.getNode()); |
| 11083 | return DAG.getNode(ISD::XOR, DL, XType, Add, Shift); |
| Nate Begeman | 2042aa5 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 11084 | } |
| 11085 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11086 | |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11087 | return SDValue(); |
| Nate Begeman | c760f80 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 11088 | } |
| 11089 | |
| Evan Cheng | 92658d5 | 2007-02-08 22:13:59 +0000 | [diff] [blame] | 11090 | /// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC. |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 11091 | SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0, |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11092 | SDValue N1, ISD::CondCode Cond, |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11093 | SDLoc DL, bool foldBooleans) { |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11094 | TargetLowering::DAGCombinerInfo |
| Nadav Rotem | b1dd524 | 2012-12-27 06:47:41 +0000 | [diff] [blame] | 11095 | DagCombineInfo(DAG, Level, false, this); |
| Dale Johannesen | f1163e9 | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 11096 | return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL); |
| Nate Begeman | 24a7eca | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 11097 | } |
| 11098 | |
| Nate Begeman | c6f067a | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 11099 | /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant, |
| 11100 | /// return a DAG expression to select that will generate the same value by |
| 11101 | /// multiplying by a magic number. See: |
| 11102 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11103 | SDValue DAGCombiner::BuildSDIV(SDNode *N) { |
| Andrew Lenharth | 0e57b2c | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 11104 | std::vector<SDNode*> Built; |
| Richard Osborne | 561fac4 | 2011-11-07 17:09:05 +0000 | [diff] [blame] | 11105 | SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, &Built); |
| Andrew Lenharth | 1dc9ec5 | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 11106 | |
| Andrew Lenharth | 0e57b2c | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 11107 | for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end(); |
| Andrew Lenharth | 1dc9ec5 | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 11108 | ii != ee; ++ii) |
| 11109 | AddToWorkList(*ii); |
| 11110 | return S; |
| Nate Begeman | c6f067a | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 11111 | } |
| 11112 | |
| 11113 | /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant, |
| 11114 | /// return a DAG expression to select that will generate the same value by |
| 11115 | /// multiplying by a magic number. See: |
| 11116 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11117 | SDValue DAGCombiner::BuildUDIV(SDNode *N) { |
| Andrew Lenharth | 0e57b2c | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 11118 | std::vector<SDNode*> Built; |
| Richard Osborne | 561fac4 | 2011-11-07 17:09:05 +0000 | [diff] [blame] | 11119 | SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, &Built); |
| Nate Begeman | c6f067a | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 11120 | |
| Andrew Lenharth | 0e57b2c | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 11121 | for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end(); |
| Andrew Lenharth | 1dc9ec5 | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 11122 | ii != ee; ++ii) |
| 11123 | AddToWorkList(*ii); |
| 11124 | return S; |
| Nate Begeman | c6f067a | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 11125 | } |
| 11126 | |
| Nate Begeman | 18150d5 | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 11127 | /// FindBaseOffset - Return true if base is a frame index, which is known not |
| Eric Christopher | d9e8eac | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 11128 | // to alias with anything but itself. Provides base object and offset as |
| 11129 | // results. |
| Nate Begeman | 18150d5 | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 11130 | static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset, |
| Roman Divacky | 9338344 | 2012-09-05 22:15:49 +0000 | [diff] [blame] | 11131 | const GlobalValue *&GV, const void *&CV) { |
| Jim Laskey | 0463e08 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 11132 | // Assume it is a primitive operation. |
| Nate Begeman | 18150d5 | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 11133 | Base = Ptr; Offset = 0; GV = 0; CV = 0; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11134 | |
| Jim Laskey | 0463e08 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 11135 | // If it's an adding a simple constant then integrate the offset. |
| 11136 | if (Base.getOpcode() == ISD::ADD) { |
| 11137 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) { |
| 11138 | Base = Base.getOperand(0); |
| Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 11139 | Offset += C->getZExtValue(); |
| Jim Laskey | 0463e08 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 11140 | } |
| 11141 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 11142 | |
| Nate Begeman | 18150d5 | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 11143 | // Return the underlying GlobalValue, and update the Offset. Return false |
| 11144 | // for GlobalAddressSDNode since the same GlobalAddress may be represented |
| 11145 | // by multiple nodes with different offsets. |
| 11146 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) { |
| 11147 | GV = G->getGlobal(); |
| 11148 | Offset += G->getOffset(); |
| 11149 | return false; |
| 11150 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11151 | |
| Nate Begeman | 18150d5 | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 11152 | // Return the underlying Constant value, and update the Offset. Return false |
| 11153 | // for ConstantSDNodes since the same constant pool entry may be represented |
| 11154 | // by multiple nodes with different offsets. |
| 11155 | if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) { |
| Roman Divacky | 9338344 | 2012-09-05 22:15:49 +0000 | [diff] [blame] | 11156 | CV = C->isMachineConstantPoolEntry() ? (const void *)C->getMachineCPVal() |
| 11157 | : (const void *)C->getConstVal(); |
| Nate Begeman | 18150d5 | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 11158 | Offset += C->getOffset(); |
| 11159 | return false; |
| 11160 | } |
| Jim Laskey | 0463e08 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 11161 | // If it's any of the following then it can't alias with anything but itself. |
| Nate Begeman | 18150d5 | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 11162 | return isa<FrameIndexSDNode>(Base); |
| Jim Laskey | 0463e08 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 11163 | } |
| 11164 | |
| 11165 | /// isAlias - Return true if there is any possibility that the two addresses |
| 11166 | /// overlap. |
| Richard Sandiford | 981fdeb | 2013-10-28 12:00:00 +0000 | [diff] [blame] | 11167 | bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1, bool IsVolatile1, |
| Jim Laskey | a15b0eb | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 11168 | const Value *SrcValue1, int SrcValueOffset1, |
| Nate Begeman | 879d8f1 | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 11169 | unsigned SrcValueAlign1, |
| Dan Gohman | a94cc6d | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 11170 | const MDNode *TBAAInfo1, |
| Richard Sandiford | 981fdeb | 2013-10-28 12:00:00 +0000 | [diff] [blame] | 11171 | SDValue Ptr2, int64_t Size2, bool IsVolatile2, |
| Nate Begeman | 879d8f1 | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 11172 | const Value *SrcValue2, int SrcValueOffset2, |
| Dan Gohman | a94cc6d | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 11173 | unsigned SrcValueAlign2, |
| 11174 | const MDNode *TBAAInfo2) const { |
| Jim Laskey | 0463e08 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 11175 | // If they are the same then they must be aliases. |
| 11176 | if (Ptr1 == Ptr2) return true; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11177 | |
| Richard Sandiford | 981fdeb | 2013-10-28 12:00:00 +0000 | [diff] [blame] | 11178 | // If they are both volatile then they cannot be reordered. |
| 11179 | if (IsVolatile1 && IsVolatile2) return true; |
| 11180 | |
| Jim Laskey | 0463e08 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 11181 | // Gather base node and offset information. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11182 | SDValue Base1, Base2; |
| Jim Laskey | 0463e08 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 11183 | int64_t Offset1, Offset2; |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 11184 | const GlobalValue *GV1, *GV2; |
| Roman Divacky | 9338344 | 2012-09-05 22:15:49 +0000 | [diff] [blame] | 11185 | const void *CV1, *CV2; |
| Nate Begeman | 18150d5 | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 11186 | bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1); |
| 11187 | bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11188 | |
| Nate Begeman | 18150d5 | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 11189 | // If they have a same base address then check to see if they overlap. |
| 11190 | if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2))) |
| Bill Wendling | 31b5099 | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 11191 | return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11192 | |
| Owen Anderson | 272ff94 | 2010-09-20 20:39:59 +0000 | [diff] [blame] | 11193 | // It is possible for different frame indices to alias each other, mostly |
| 11194 | // when tail call optimization reuses return address slots for arguments. |
| 11195 | // To catch this case, look up the actual index of frame indices to compute |
| 11196 | // the real alias relationship. |
| 11197 | if (isFrameIndex1 && isFrameIndex2) { |
| 11198 | MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); |
| 11199 | Offset1 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex()); |
| 11200 | Offset2 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex()); |
| 11201 | return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1); |
| 11202 | } |
| 11203 | |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 11204 | // Otherwise, if we know what the bases are, and they aren't identical, then |
| Owen Anderson | 272ff94 | 2010-09-20 20:39:59 +0000 | [diff] [blame] | 11205 | // we know they cannot alias. |
| Nate Begeman | 18150d5 | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 11206 | if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2)) |
| 11207 | return false; |
| Jim Laskey | a15b0eb | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 11208 | |
| Nate Begeman | 879d8f1 | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 11209 | // If we know required SrcValue1 and SrcValue2 have relatively large alignment |
| 11210 | // compared to the size and offset of the access, we may be able to prove they |
| 11211 | // do not alias. This check is conservative for now to catch cases created by |
| 11212 | // splitting vector types. |
| 11213 | if ((SrcValueAlign1 == SrcValueAlign2) && |
| 11214 | (SrcValueOffset1 != SrcValueOffset2) && |
| 11215 | (Size1 == Size2) && (SrcValueAlign1 > Size1)) { |
| 11216 | int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1; |
| 11217 | int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 11218 | |
| Nate Begeman | 879d8f1 | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 11219 | // There is no overlap between these relatively aligned accesses of similar |
| 11220 | // size, return no alias. |
| 11221 | if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1) |
| 11222 | return false; |
| 11223 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 11224 | |
| Hal Finkel | 5ef4dcc | 2013-08-29 03:29:55 +0000 | [diff] [blame] | 11225 | bool UseAA = CombinerGlobalAA.getNumOccurrences() > 0 ? CombinerGlobalAA : |
| 11226 | TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA(); |
| Hal Finkel | 9b2617a | 2014-01-25 17:32:39 +0000 | [diff] [blame] | 11227 | #ifndef NDEBUG |
| 11228 | if (CombinerAAOnlyFunc.getNumOccurrences() && |
| 11229 | CombinerAAOnlyFunc != DAG.getMachineFunction().getName()) |
| 11230 | UseAA = false; |
| 11231 | #endif |
| Hal Finkel | 3165883 | 2013-09-15 02:19:49 +0000 | [diff] [blame] | 11232 | if (UseAA && SrcValue1 && SrcValue2) { |
| Jim Laskey | 55e4dca | 2006-10-18 19:08:31 +0000 | [diff] [blame] | 11233 | // Use alias analysis information. |
| Dan Gohman | 9625d81 | 2007-08-27 16:32:11 +0000 | [diff] [blame] | 11234 | int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2); |
| 11235 | int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset; |
| 11236 | int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11237 | AliasAnalysis::AliasResult AAResult = |
| Hal Finkel | dbebb52 | 2014-01-25 19:24:54 +0000 | [diff] [blame] | 11238 | AA.alias(AliasAnalysis::Location(SrcValue1, Overlap1, |
| 11239 | UseTBAA ? TBAAInfo1 : 0), |
| 11240 | AliasAnalysis::Location(SrcValue2, Overlap2, |
| 11241 | UseTBAA ? TBAAInfo2 : 0)); |
| Jim Laskey | 55e4dca | 2006-10-18 19:08:31 +0000 | [diff] [blame] | 11242 | if (AAResult == AliasAnalysis::NoAlias) |
| 11243 | return false; |
| 11244 | } |
| Jim Laskey | a15b0eb | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 11245 | |
| 11246 | // Otherwise we have to assume they alias. |
| 11247 | return true; |
| Jim Laskey | 0463e08 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 11248 | } |
| 11249 | |
| Nadav Rotem | 307d767 | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 11250 | bool DAGCombiner::isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1) { |
| 11251 | SDValue Ptr0, Ptr1; |
| 11252 | int64_t Size0, Size1; |
| Richard Sandiford | 981fdeb | 2013-10-28 12:00:00 +0000 | [diff] [blame] | 11253 | bool IsVolatile0, IsVolatile1; |
| Nadav Rotem | 307d767 | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 11254 | const Value *SrcValue0, *SrcValue1; |
| 11255 | int SrcValueOffset0, SrcValueOffset1; |
| 11256 | unsigned SrcValueAlign0, SrcValueAlign1; |
| 11257 | const MDNode *SrcTBAAInfo0, *SrcTBAAInfo1; |
| Richard Sandiford | 981fdeb | 2013-10-28 12:00:00 +0000 | [diff] [blame] | 11258 | FindAliasInfo(Op0, Ptr0, Size0, IsVolatile0, SrcValue0, SrcValueOffset0, |
| Nadav Rotem | 307d767 | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 11259 | SrcValueAlign0, SrcTBAAInfo0); |
| Richard Sandiford | 981fdeb | 2013-10-28 12:00:00 +0000 | [diff] [blame] | 11260 | FindAliasInfo(Op1, Ptr1, Size1, IsVolatile1, SrcValue1, SrcValueOffset1, |
| Nadav Rotem | 307d767 | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 11261 | SrcValueAlign1, SrcTBAAInfo1); |
| Richard Sandiford | 981fdeb | 2013-10-28 12:00:00 +0000 | [diff] [blame] | 11262 | return isAlias(Ptr0, Size0, IsVolatile0, SrcValue0, SrcValueOffset0, |
| Nadav Rotem | ac450eb | 2012-12-06 17:34:13 +0000 | [diff] [blame] | 11263 | SrcValueAlign0, SrcTBAAInfo0, |
| Richard Sandiford | 981fdeb | 2013-10-28 12:00:00 +0000 | [diff] [blame] | 11264 | Ptr1, Size1, IsVolatile1, SrcValue1, SrcValueOffset1, |
| Nadav Rotem | ac450eb | 2012-12-06 17:34:13 +0000 | [diff] [blame] | 11265 | SrcValueAlign1, SrcTBAAInfo1); |
| Nadav Rotem | 307d767 | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 11266 | } |
| 11267 | |
| Jim Laskey | 0463e08 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 11268 | /// FindAliasInfo - Extracts the relevant alias information from the memory |
| Richard Sandiford | 981fdeb | 2013-10-28 12:00:00 +0000 | [diff] [blame] | 11269 | /// node. Returns true if the operand was a nonvolatile load. |
| Jim Laskey | 08edf33 | 2006-10-11 13:47:09 +0000 | [diff] [blame] | 11270 | bool DAGCombiner::FindAliasInfo(SDNode *N, |
| Richard Sandiford | 981fdeb | 2013-10-28 12:00:00 +0000 | [diff] [blame] | 11271 | SDValue &Ptr, int64_t &Size, bool &IsVolatile, |
| Benjamin Kramer | 5a377e2 | 2012-01-15 11:50:43 +0000 | [diff] [blame] | 11272 | const Value *&SrcValue, |
| 11273 | int &SrcValueOffset, |
| 11274 | unsigned &SrcValueAlign, |
| 11275 | const MDNode *&TBAAInfo) const { |
| 11276 | LSBaseSDNode *LS = cast<LSBaseSDNode>(N); |
| 11277 | |
| 11278 | Ptr = LS->getBasePtr(); |
| 11279 | Size = LS->getMemoryVT().getSizeInBits() >> 3; |
| Richard Sandiford | 981fdeb | 2013-10-28 12:00:00 +0000 | [diff] [blame] | 11280 | IsVolatile = LS->isVolatile(); |
| Benjamin Kramer | 5a377e2 | 2012-01-15 11:50:43 +0000 | [diff] [blame] | 11281 | SrcValue = LS->getSrcValue(); |
| 11282 | SrcValueOffset = LS->getSrcValueOffset(); |
| 11283 | SrcValueAlign = LS->getOriginalAlignment(); |
| 11284 | TBAAInfo = LS->getTBAAInfo(); |
| Richard Sandiford | 981fdeb | 2013-10-28 12:00:00 +0000 | [diff] [blame] | 11285 | return isa<LoadSDNode>(LS) && !IsVolatile; |
| Jim Laskey | 0463e08 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 11286 | } |
| 11287 | |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 11288 | /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes, |
| 11289 | /// looking for aliasing nodes and adding them to the Aliases vector. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11290 | void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain, |
| Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 11291 | SmallVectorImpl<SDValue> &Aliases) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11292 | SmallVector<SDValue, 8> Chains; // List of chains to visit. |
| Nate Begeman | 879d8f1 | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 11293 | SmallPtrSet<SDNode *, 16> Visited; // Visited node set. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11294 | |
| Jim Laskey | d07be23 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 11295 | // Get alias information for node. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11296 | SDValue Ptr; |
| Nate Begeman | 879d8f1 | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 11297 | int64_t Size; |
| Richard Sandiford | 981fdeb | 2013-10-28 12:00:00 +0000 | [diff] [blame] | 11298 | bool IsVolatile; |
| Nate Begeman | 879d8f1 | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 11299 | const Value *SrcValue; |
| 11300 | int SrcValueOffset; |
| 11301 | unsigned SrcValueAlign; |
| Dan Gohman | a94cc6d | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 11302 | const MDNode *SrcTBAAInfo; |
| Richard Sandiford | 981fdeb | 2013-10-28 12:00:00 +0000 | [diff] [blame] | 11303 | bool IsLoad = FindAliasInfo(N, Ptr, Size, IsVolatile, SrcValue, |
| 11304 | SrcValueOffset, SrcValueAlign, SrcTBAAInfo); |
| Jim Laskey | d07be23 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 11305 | |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 11306 | // Starting off. |
| Jim Laskey | 6549d22 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 11307 | Chains.push_back(OriginalChain); |
| Nate Begeman | a3ed9ed | 2009-10-12 05:53:58 +0000 | [diff] [blame] | 11308 | unsigned Depth = 0; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 11309 | |
| Jim Laskey | 6549d22 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 11310 | // Look at each chain and determine if it is an alias. If so, add it to the |
| 11311 | // aliases list. If not, then continue up the chain looking for the next |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11312 | // candidate. |
| Jim Laskey | 6549d22 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 11313 | while (!Chains.empty()) { |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11314 | SDValue Chain = Chains.back(); |
| Jim Laskey | 6549d22 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 11315 | Chains.pop_back(); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 11316 | |
| 11317 | // For TokenFactor nodes, look at each operand and only continue up the |
| 11318 | // chain until we find two aliases. If we've seen two aliases, assume we'll |
| Nate Begeman | a3ed9ed | 2009-10-12 05:53:58 +0000 | [diff] [blame] | 11319 | // find more and revert to original chain since the xform is unlikely to be |
| 11320 | // profitable. |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 11321 | // |
| 11322 | // FIXME: The depth check could be made to return the last non-aliasing |
| Nate Begeman | a3ed9ed | 2009-10-12 05:53:58 +0000 | [diff] [blame] | 11323 | // chain we found before we hit a tokenfactor rather than the original |
| 11324 | // chain. |
| 11325 | if (Depth > 6 || Aliases.size() == 2) { |
| 11326 | Aliases.clear(); |
| 11327 | Aliases.push_back(OriginalChain); |
| Hal Finkel | 51a9838 | 2014-01-24 20:12:02 +0000 | [diff] [blame] | 11328 | return; |
| Nate Begeman | a3ed9ed | 2009-10-12 05:53:58 +0000 | [diff] [blame] | 11329 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11330 | |
| Nate Begeman | 879d8f1 | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 11331 | // Don't bother if we've been before. |
| 11332 | if (!Visited.insert(Chain.getNode())) |
| 11333 | continue; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11334 | |
| Jim Laskey | 6549d22 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 11335 | switch (Chain.getOpcode()) { |
| 11336 | case ISD::EntryToken: |
| 11337 | // Entry token is ideal chain operand, but handled in FindBetterChain. |
| 11338 | break; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11339 | |
| Jim Laskey | 6549d22 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 11340 | case ISD::LOAD: |
| 11341 | case ISD::STORE: { |
| 11342 | // Get alias information for Chain. |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11343 | SDValue OpPtr; |
| Nate Begeman | 879d8f1 | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 11344 | int64_t OpSize; |
| Richard Sandiford | 981fdeb | 2013-10-28 12:00:00 +0000 | [diff] [blame] | 11345 | bool OpIsVolatile; |
| Nate Begeman | 879d8f1 | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 11346 | const Value *OpSrcValue; |
| 11347 | int OpSrcValueOffset; |
| 11348 | unsigned OpSrcValueAlign; |
| Dan Gohman | a94cc6d | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 11349 | const MDNode *OpSrcTBAAInfo; |
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 11350 | bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize, |
| Richard Sandiford | 981fdeb | 2013-10-28 12:00:00 +0000 | [diff] [blame] | 11351 | OpIsVolatile, OpSrcValue, OpSrcValueOffset, |
| Dan Gohman | a94cc6d | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 11352 | OpSrcValueAlign, |
| 11353 | OpSrcTBAAInfo); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11354 | |
| Jim Laskey | 6549d22 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 11355 | // If chain is alias then stop here. |
| 11356 | if (!(IsLoad && IsOpLoad) && |
| Richard Sandiford | 981fdeb | 2013-10-28 12:00:00 +0000 | [diff] [blame] | 11357 | isAlias(Ptr, Size, IsVolatile, SrcValue, SrcValueOffset, |
| 11358 | SrcValueAlign, SrcTBAAInfo, |
| 11359 | OpPtr, OpSize, OpIsVolatile, OpSrcValue, OpSrcValueOffset, |
| Dan Gohman | a94cc6d | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 11360 | OpSrcValueAlign, OpSrcTBAAInfo)) { |
| Jim Laskey | 6549d22 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 11361 | Aliases.push_back(Chain); |
| 11362 | } else { |
| 11363 | // Look further up the chain. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11364 | Chains.push_back(Chain.getOperand(0)); |
| Nate Begeman | a3ed9ed | 2009-10-12 05:53:58 +0000 | [diff] [blame] | 11365 | ++Depth; |
| Jim Laskey | d07be23 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 11366 | } |
| Jim Laskey | 6549d22 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 11367 | break; |
| 11368 | } |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11369 | |
| Jim Laskey | 6549d22 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 11370 | case ISD::TokenFactor: |
| Nate Begeman | 879d8f1 | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 11371 | // We have to check each of the operands of the token factor for "small" |
| 11372 | // token factors, so we queue them up. Adding the operands to the queue |
| 11373 | // (stack) in reverse order maintains the original order and increases the |
| 11374 | // likelihood that getNode will find a matching token factor (CSE.) |
| 11375 | if (Chain.getNumOperands() > 16) { |
| 11376 | Aliases.push_back(Chain); |
| 11377 | break; |
| 11378 | } |
| Jim Laskey | 6549d22 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 11379 | for (unsigned n = Chain.getNumOperands(); n;) |
| 11380 | Chains.push_back(Chain.getOperand(--n)); |
| Nate Begeman | a3ed9ed | 2009-10-12 05:53:58 +0000 | [diff] [blame] | 11381 | ++Depth; |
| Jim Laskey | 6549d22 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 11382 | break; |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11383 | |
| Jim Laskey | 6549d22 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 11384 | default: |
| 11385 | // For all other instructions we will just have to take what we can get. |
| 11386 | Aliases.push_back(Chain); |
| 11387 | break; |
| Jim Laskey | d07be23 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 11388 | } |
| 11389 | } |
| Hal Finkel | 51a9838 | 2014-01-24 20:12:02 +0000 | [diff] [blame] | 11390 | |
| 11391 | // We need to be careful here to also search for aliases through the |
| 11392 | // value operand of a store, etc. Consider the following situation: |
| 11393 | // Token1 = ... |
| 11394 | // L1 = load Token1, %52 |
| 11395 | // S1 = store Token1, L1, %51 |
| 11396 | // L2 = load Token1, %52+8 |
| 11397 | // S2 = store Token1, L2, %51+8 |
| 11398 | // Token2 = Token(S1, S2) |
| 11399 | // L3 = load Token2, %53 |
| 11400 | // S3 = store Token2, L3, %52 |
| 11401 | // L4 = load Token2, %53+8 |
| 11402 | // S4 = store Token2, L4, %52+8 |
| 11403 | // If we search for aliases of S3 (which loads address %52), and we look |
| 11404 | // only through the chain, then we'll miss the trivial dependence on L1 |
| 11405 | // (which also loads from %52). We then might change all loads and |
| 11406 | // stores to use Token1 as their chain operand, which could result in |
| 11407 | // copying %53 into %52 before copying %52 into %51 (which should |
| 11408 | // happen first). |
| 11409 | // |
| 11410 | // The problem is, however, that searching for such data dependencies |
| 11411 | // can become expensive, and the cost is not directly related to the |
| 11412 | // chain depth. Instead, we'll rule out such configurations here by |
| 11413 | // insisting that we've visited all chain users (except for users |
| 11414 | // of the original chain, which is not necessary). When doing this, |
| 11415 | // we need to look through nodes we don't care about (otherwise, things |
| 11416 | // like register copies will interfere with trivial cases). |
| 11417 | |
| 11418 | SmallVector<const SDNode *, 16> Worklist; |
| 11419 | for (SmallPtrSet<SDNode *, 16>::iterator I = Visited.begin(), |
| 11420 | IE = Visited.end(); I != IE; ++I) |
| 11421 | if (*I != OriginalChain.getNode()) |
| 11422 | Worklist.push_back(*I); |
| 11423 | |
| 11424 | while (!Worklist.empty()) { |
| 11425 | const SDNode *M = Worklist.pop_back_val(); |
| 11426 | |
| 11427 | // We have already visited M, and want to make sure we've visited any uses |
| 11428 | // of M that we care about. For uses that we've not visisted, and don't |
| 11429 | // care about, queue them to the worklist. |
| 11430 | |
| 11431 | for (SDNode::use_iterator UI = M->use_begin(), |
| 11432 | UIE = M->use_end(); UI != UIE; ++UI) |
| 11433 | if (UI.getUse().getValueType() == MVT::Other && Visited.insert(*UI)) { |
| 11434 | if (isa<MemIntrinsicSDNode>(*UI) || isa<MemSDNode>(*UI)) { |
| 11435 | // We've not visited this use, and we care about it (it could have an |
| 11436 | // ordering dependency with the original node). |
| 11437 | Aliases.clear(); |
| 11438 | Aliases.push_back(OriginalChain); |
| 11439 | return; |
| 11440 | } |
| 11441 | |
| 11442 | // We've not visited this use, but we don't care about it. Mark it as |
| 11443 | // visited and enqueue it to the worklist. |
| 11444 | Worklist.push_back(*UI); |
| 11445 | } |
| 11446 | } |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 11447 | } |
| 11448 | |
| 11449 | /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking |
| 11450 | /// for a better chain (aliasing node.) |
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11451 | SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) { |
| 11452 | SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor. |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11453 | |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 11454 | // Accumulate all the aliases to this node. |
| 11455 | GatherAllAliases(N, OldChain, Aliases); |
| Scott Michel | cf0da6c | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11456 | |
| Dan Gohman | 4298df6 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 11457 | // If no operands then chain to entry token. |
| 11458 | if (Aliases.size() == 0) |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 11459 | return DAG.getEntryNode(); |
| Dan Gohman | 4298df6 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 11460 | |
| 11461 | // If a single operand then chain to it. We don't need to revisit it. |
| 11462 | if (Aliases.size() == 1) |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 11463 | return Aliases[0]; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 11464 | |
| Jim Laskey | 708d0db | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 11465 | // Construct a custom tailored token factor. |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11466 | return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other, |
| Nate Begeman | 879d8f1 | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 11467 | &Aliases[0], Aliases.size()); |
| Jim Laskey | d07be23 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 11468 | } |
| 11469 | |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 11470 | // SelectionDAG::Combine - This is the entry point for the file. |
| 11471 | // |
| Bill Wendling | 084669a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 11472 | void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA, |
| Bill Wendling | 026e5d7 | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 11473 | CodeGenOpt::Level OptLevel) { |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 11474 | /// run - This is the main entry point to this class. |
| 11475 | /// |
| Bill Wendling | 084669a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 11476 | DAGCombiner(*this, AA, OptLevel).Run(Level); |
| Nate Begeman | 21158fc | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 11477 | } |