Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 1 | //===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===// |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2 | // |
3 | // The LLVM Compiler Infrastructure | ||||
4 | // | ||||
Chris Lattner | 4ee451d | 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 | 1d4d414 | 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 | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 12 | // |
Dan Gohman | 4128700 | 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 | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 17 | //===----------------------------------------------------------------------===// |
18 | |||||
19 | #define DEBUG_TYPE "dagcombine" | ||||
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/SelectionDAG.h" |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallPtrSet.h" |
22 | #include "llvm/ADT/Statistic.h" | ||||
Chandler Carruth | d04a8d4 | 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 | 0b8c9a8 | 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 | d1aed7a | 2006-09-21 16:28:59 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 32 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 33 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | d04a8d4 | 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 | 83f743a | 2013-10-11 18:29:42 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetRegisterInfo.h" |
Hal Finkel | 253acef | 2013-08-29 03:29:55 +0000 | [diff] [blame] | 39 | #include "llvm/Target/TargetSubtargetInfo.h" |
Chris Lattner | a500fc6 | 2005-09-09 23:53:39 +0000 | [diff] [blame] | 40 | #include <algorithm> |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 41 | using namespace llvm; |
42 | |||||
Chris Lattner | cd3245a | 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 | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 46 | STATISTIC(OpsNarrowed , "Number of load/op/store narrowed"); |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 47 | STATISTIC(LdStFP2Int , "Number of fp load/store pairs transformed to int"); |
Quentin Colombet | 83f743a | 2013-10-11 18:29:42 +0000 | [diff] [blame] | 48 | STATISTIC(SlicedLoads, "Number of load sliced"); |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 49 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 50 | namespace { |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 51 | static cl::opt<bool> |
Owen Anderson | 0dcc814 | 2010-09-19 21:01:26 +0000 | [diff] [blame] | 52 | CombinerAA("combiner-alias-analysis", cl::Hidden, |
Jim Laskey | 26f7fa7 | 2006-10-17 19:33:52 +0000 | [diff] [blame] | 53 | cl::desc("Turn on alias analysis during testing")); |
Jim Laskey | 3ad175b | 2006-10-12 15:22:24 +0000 | [diff] [blame] | 54 | |
Jim Laskey | 07a2709 | 2006-10-18 19:08:31 +0000 | [diff] [blame] | 55 | static cl::opt<bool> |
56 | CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden, | ||||
57 | cl::desc("Include global information in alias analysis")); | ||||
58 | |||||
Quentin Colombet | 83f743a | 2013-10-11 18:29:42 +0000 | [diff] [blame] | 59 | /// Hidden option to stress test load slicing, i.e., when this option |
60 | /// is enabled, load slicing bypasses most of its profitability guards. | ||||
61 | static cl::opt<bool> | ||||
62 | StressLoadSlicing("combiner-stress-load-slicing", cl::Hidden, | ||||
63 | cl::desc("Bypass the profitability model of load " | ||||
64 | "slicing"), | ||||
65 | cl::init(false)); | ||||
66 | |||||
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 67 | //------------------------------ DAGCombiner ---------------------------------// |
68 | |||||
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 69 | class DAGCombiner { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 70 | SelectionDAG &DAG; |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 71 | const TargetLowering &TLI; |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 72 | CombineLevel Level; |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 73 | CodeGenOpt::Level OptLevel; |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 74 | bool LegalOperations; |
75 | bool LegalTypes; | ||||
Quentin Colombet | 83f743a | 2013-10-11 18:29:42 +0000 | [diff] [blame] | 76 | bool ForCodeSize; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 77 | |
78 | // Worklist of all of the nodes that need to be simplified. | ||||
James Molloy | 6660c05 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 79 | // |
80 | // This has the semantics that when adding to the worklist, | ||||
81 | // the item added must be next to be processed. It should | ||||
82 | // also only appear once. The naive approach to this takes | ||||
83 | // linear time. | ||||
84 | // | ||||
85 | // To reduce the insert/remove time to logarithmic, we use | ||||
86 | // a set and a vector to maintain our worklist. | ||||
87 | // | ||||
88 | // The set contains the items on the worklist, but does not | ||||
89 | // maintain the order they should be visited. | ||||
90 | // | ||||
91 | // The vector maintains the order nodes should be visited, but may | ||||
92 | // contain duplicate or removed nodes. When choosing a node to | ||||
93 | // visit, we pop off the order stack until we find an item that is | ||||
94 | // also in the contents set. All operations are O(log N). | ||||
95 | SmallPtrSet<SDNode*, 64> WorkListContents; | ||||
Benjamin Kramer | d5f7690 | 2012-03-10 00:23:58 +0000 | [diff] [blame] | 96 | SmallVector<SDNode*, 64> WorkListOrder; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 97 | |
Jim Laskey | c7c3f11 | 2006-10-16 20:52:31 +0000 | [diff] [blame] | 98 | // AA - Used for DAG load/store alias analysis. |
99 | AliasAnalysis &AA; | ||||
100 | |||||
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 101 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
102 | /// the instruction to the work lists because they might get more simplified | ||||
103 | /// now. | ||||
104 | /// | ||||
105 | void AddUsersToWorkList(SDNode *N) { | ||||
106 | for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); | ||||
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 107 | UI != UE; ++UI) |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 108 | AddToWorkList(*UI); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 109 | } |
110 | |||||
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 111 | /// visit - call the node-specific routine that knows how to fold each |
112 | /// particular type of node. | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 113 | SDValue visit(SDNode *N); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 115 | public: |
James Molloy | 6afa3f7 | 2012-02-16 09:48:07 +0000 | [diff] [blame] | 116 | /// AddToWorkList - Add to the work list making sure its instance is at the |
James Molloy | 6660c05 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 117 | /// back (next to be processed.) |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 118 | void AddToWorkList(SDNode *N) { |
James Molloy | 6660c05 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 119 | WorkListContents.insert(N); |
120 | WorkListOrder.push_back(N); | ||||
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 121 | } |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 122 | |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 123 | /// removeFromWorkList - remove all instances of N from the worklist. |
124 | /// | ||||
125 | void removeFromWorkList(SDNode *N) { | ||||
James Molloy | 6660c05 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 126 | WorkListContents.erase(N); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 127 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 128 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 129 | SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo, |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 130 | bool AddTo = true); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 131 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 132 | SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) { |
Jim Laskey | 274062c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 133 | return CombineTo(N, &Res, 1, AddTo); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 134 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 135 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 136 | SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1, |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 137 | bool AddTo = true) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 138 | SDValue To[] = { Res0, Res1 }; |
Jim Laskey | 274062c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 139 | return CombineTo(N, To, 2, AddTo); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 140 | } |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 141 | |
142 | void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 143 | |
144 | private: | ||||
145 | |||||
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 146 | /// SimplifyDemandedBits - Check the specified integer node value to see if |
Chris Lattner | b2742f4 | 2006-03-01 19:55:35 +0000 | [diff] [blame] | 147 | /// it can be simplified or if things it uses can be simplified by bit |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 148 | /// propagation. If so, return true. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 149 | bool SimplifyDemandedBits(SDValue Op) { |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 150 | unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits(); |
151 | APInt Demanded = APInt::getAllOnesValue(BitWidth); | ||||
Dan Gohman | 7b8d4a9 | 2008-02-27 00:25:32 +0000 | [diff] [blame] | 152 | return SimplifyDemandedBits(Op, Demanded); |
153 | } | ||||
154 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 155 | bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded); |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 156 | |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 157 | bool CombineToPreIndexedLoadStore(SDNode *N); |
158 | bool CombineToPostIndexedLoadStore(SDNode *N); | ||||
Quentin Colombet | 83f743a | 2013-10-11 18:29:42 +0000 | [diff] [blame] | 159 | bool SliceUpLoad(SDNode *N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 160 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 161 | void ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad); |
162 | SDValue PromoteOperand(SDValue Op, EVT PVT, bool &Replace); | ||||
163 | SDValue SExtPromoteOperand(SDValue Op, EVT PVT); | ||||
164 | SDValue ZExtPromoteOperand(SDValue Op, EVT PVT); | ||||
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 165 | SDValue PromoteIntBinOp(SDValue Op); |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 166 | SDValue PromoteIntShiftOp(SDValue Op); |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 167 | SDValue PromoteExtend(SDValue Op); |
168 | bool PromoteLoad(SDValue Op); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 169 | |
Craig Topper | 6c64fba | 2013-07-13 07:43:40 +0000 | [diff] [blame] | 170 | void ExtendSetCCUses(const SmallVectorImpl<SDNode *> &SetCCs, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 171 | SDValue Trunc, SDValue ExtLoad, SDLoc DL, |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 172 | ISD::NodeType ExtType); |
173 | |||||
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 174 | /// combine - call the node-specific routine that knows how to fold each |
175 | /// particular type of node. If that doesn't do anything, try the | ||||
176 | /// target-specific DAG combines. | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 177 | SDValue combine(SDNode *N); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 178 | |
179 | // Visitation implementation - Implement dag node combining for different | ||||
180 | // node types. The semantics are as follows: | ||||
181 | // Return Value: | ||||
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 182 | // SDValue.getNode() == 0 - No change was made |
183 | // SDValue.getNode() == N - N was replaced, is dead and has been handled. | ||||
184 | // otherwise - N should be replaced by the returned Operand. | ||||
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 185 | // |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 186 | SDValue visitTokenFactor(SDNode *N); |
187 | SDValue visitMERGE_VALUES(SDNode *N); | ||||
188 | SDValue visitADD(SDNode *N); | ||||
189 | SDValue visitSUB(SDNode *N); | ||||
190 | SDValue visitADDC(SDNode *N); | ||||
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 191 | SDValue visitSUBC(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 192 | SDValue visitADDE(SDNode *N); |
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 193 | SDValue visitSUBE(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 194 | SDValue visitMUL(SDNode *N); |
195 | SDValue visitSDIV(SDNode *N); | ||||
196 | SDValue visitUDIV(SDNode *N); | ||||
197 | SDValue visitSREM(SDNode *N); | ||||
198 | SDValue visitUREM(SDNode *N); | ||||
199 | SDValue visitMULHU(SDNode *N); | ||||
200 | SDValue visitMULHS(SDNode *N); | ||||
201 | SDValue visitSMUL_LOHI(SDNode *N); | ||||
202 | SDValue visitUMUL_LOHI(SDNode *N); | ||||
Benjamin Kramer | f55d26e | 2011-05-21 18:31:55 +0000 | [diff] [blame] | 203 | SDValue visitSMULO(SDNode *N); |
204 | SDValue visitUMULO(SDNode *N); | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 205 | SDValue visitSDIVREM(SDNode *N); |
206 | SDValue visitUDIVREM(SDNode *N); | ||||
207 | SDValue visitAND(SDNode *N); | ||||
208 | SDValue visitOR(SDNode *N); | ||||
209 | SDValue visitXOR(SDNode *N); | ||||
210 | SDValue SimplifyVBinOp(SDNode *N); | ||||
Craig Topper | dd201ff | 2012-09-11 01:45:21 +0000 | [diff] [blame] | 211 | SDValue SimplifyVUnaryOp(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 212 | SDValue visitSHL(SDNode *N); |
213 | SDValue visitSRA(SDNode *N); | ||||
214 | SDValue visitSRL(SDNode *N); | ||||
215 | SDValue visitCTLZ(SDNode *N); | ||||
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 216 | SDValue visitCTLZ_ZERO_UNDEF(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 217 | SDValue visitCTTZ(SDNode *N); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 218 | SDValue visitCTTZ_ZERO_UNDEF(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 219 | SDValue visitCTPOP(SDNode *N); |
220 | SDValue visitSELECT(SDNode *N); | ||||
Benjamin Kramer | 6242fda | 2013-04-26 09:19:19 +0000 | [diff] [blame] | 221 | SDValue visitVSELECT(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 222 | SDValue visitSELECT_CC(SDNode *N); |
223 | SDValue visitSETCC(SDNode *N); | ||||
224 | SDValue visitSIGN_EXTEND(SDNode *N); | ||||
225 | SDValue visitZERO_EXTEND(SDNode *N); | ||||
226 | SDValue visitANY_EXTEND(SDNode *N); | ||||
227 | SDValue visitSIGN_EXTEND_INREG(SDNode *N); | ||||
228 | SDValue visitTRUNCATE(SDNode *N); | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 229 | SDValue visitBITCAST(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 230 | SDValue visitBUILD_PAIR(SDNode *N); |
231 | SDValue visitFADD(SDNode *N); | ||||
232 | SDValue visitFSUB(SDNode *N); | ||||
233 | SDValue visitFMUL(SDNode *N); | ||||
Owen Anderson | 062c0a5 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 234 | SDValue visitFMA(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 235 | SDValue visitFDIV(SDNode *N); |
236 | SDValue visitFREM(SDNode *N); | ||||
237 | SDValue visitFCOPYSIGN(SDNode *N); | ||||
238 | SDValue visitSINT_TO_FP(SDNode *N); | ||||
239 | SDValue visitUINT_TO_FP(SDNode *N); | ||||
240 | SDValue visitFP_TO_SINT(SDNode *N); | ||||
241 | SDValue visitFP_TO_UINT(SDNode *N); | ||||
242 | SDValue visitFP_ROUND(SDNode *N); | ||||
243 | SDValue visitFP_ROUND_INREG(SDNode *N); | ||||
244 | SDValue visitFP_EXTEND(SDNode *N); | ||||
245 | SDValue visitFNEG(SDNode *N); | ||||
246 | SDValue visitFABS(SDNode *N); | ||||
Owen Anderson | 7c626d3 | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 247 | SDValue visitFCEIL(SDNode *N); |
248 | SDValue visitFTRUNC(SDNode *N); | ||||
249 | SDValue visitFFLOOR(SDNode *N); | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 250 | SDValue visitBRCOND(SDNode *N); |
251 | SDValue visitBR_CC(SDNode *N); | ||||
252 | SDValue visitLOAD(SDNode *N); | ||||
253 | SDValue visitSTORE(SDNode *N); | ||||
254 | SDValue visitINSERT_VECTOR_ELT(SDNode *N); | ||||
255 | SDValue visitEXTRACT_VECTOR_ELT(SDNode *N); | ||||
256 | SDValue visitBUILD_VECTOR(SDNode *N); | ||||
257 | SDValue visitCONCAT_VECTORS(SDNode *N); | ||||
Bruno Cardoso Lopes | e97190f | 2011-09-20 23:19:33 +0000 | [diff] [blame] | 258 | SDValue visitEXTRACT_SUBVECTOR(SDNode *N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 259 | SDValue visitVECTOR_SHUFFLE(SDNode *N); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 260 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 261 | SDValue XformToShuffleWithZero(SDNode *N); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 262 | SDValue ReassociateOps(unsigned Opc, SDLoc DL, SDValue LHS, SDValue RHS); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 263 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 264 | SDValue visitShiftByConstant(SDNode *N, unsigned Amt); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 265 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 266 | bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS); |
267 | SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 268 | SDValue SimplifySelect(SDLoc DL, SDValue N0, SDValue N1, SDValue N2); |
269 | SDValue SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1, SDValue N2, | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 270 | SDValue N3, ISD::CondCode CC, |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 271 | bool NotExtCompare = false); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 272 | SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 273 | SDLoc DL, bool foldBooleans = true); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 274 | SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp, |
Chris Lattner | 5eee427 | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 275 | unsigned HiOp); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 276 | SDValue CombineConsecutiveLoads(SDNode *N, EVT VT); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 277 | SDValue ConstantFoldBITCASTofBUILD_VECTOR(SDNode *, EVT); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 278 | SDValue BuildSDIV(SDNode *N); |
279 | SDValue BuildUDIV(SDNode *N); | ||||
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 280 | SDValue MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1, |
281 | bool DemandHighBits = true); | ||||
282 | SDValue MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 283 | SDNode *MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 284 | SDValue ReduceLoadWidth(SDNode *N); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 285 | SDValue ReduceLoadOpStoreWidth(SDNode *N); |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 286 | SDValue TransformFPLoadStorePair(SDNode *N); |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 287 | SDValue reduceBuildVecExtToExtBuildVec(SDNode *N); |
Michael Liao | 1a5cc71 | 2012-10-24 04:14:18 +0000 | [diff] [blame] | 288 | SDValue reduceBuildVecConvertToConvertBuildVec(SDNode *N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 289 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 290 | SDValue GetDemandedBits(SDValue V, const APInt &Mask); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 291 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 292 | /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes, |
293 | /// looking for aliasing nodes and adding them to the Aliases vector. | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 294 | void GatherAllAliases(SDNode *N, SDValue OriginalChain, |
Craig Topper | a0ec3f9 | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 295 | SmallVectorImpl<SDValue> &Aliases); |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 296 | |
Jim Laskey | 096c22e | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 297 | /// isAlias - Return true if there is any possibility that the two addresses |
298 | /// overlap. | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 299 | bool isAlias(SDValue Ptr1, int64_t Size1, |
Jim Laskey | 096c22e | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 300 | const Value *SrcValue1, int SrcValueOffset1, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 301 | unsigned SrcValueAlign1, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 302 | const MDNode *TBAAInfo1, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 303 | SDValue Ptr2, int64_t Size2, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 304 | const Value *SrcValue2, int SrcValueOffset2, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 305 | unsigned SrcValueAlign2, |
306 | const MDNode *TBAAInfo2) const; | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 307 | |
Nadav Rotem | 90e11dc | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 308 | /// isAlias - Return true if there is any possibility that the two addresses |
309 | /// overlap. | ||||
310 | bool isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1); | ||||
311 | |||||
Jim Laskey | 7ca56af | 2006-10-11 13:47:09 +0000 | [diff] [blame] | 312 | /// FindAliasInfo - Extracts the relevant alias information from the memory |
313 | /// node. Returns true if the operand was a load. | ||||
314 | bool FindAliasInfo(SDNode *N, | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 315 | SDValue &Ptr, int64_t &Size, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 316 | const Value *&SrcValue, int &SrcValueOffset, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 317 | unsigned &SrcValueAlignment, |
318 | const MDNode *&TBAAInfo) const; | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 319 | |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 320 | /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 321 | /// looking for a better chain (aliasing node.) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 322 | SDValue FindBetterChain(SDNode *N, SDValue Chain); |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 323 | |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 324 | /// Merge consecutive store operations into a wide store. |
325 | /// This optimization uses wide integers or vectors when possible. | ||||
326 | /// \return True if some memory operations were changed. | ||||
327 | bool MergeConsecutiveStores(StoreSDNode *N); | ||||
328 | |||||
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 329 | public: |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 330 | DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL) |
Quentin Colombet | 83f743a | 2013-10-11 18:29:42 +0000 | [diff] [blame] | 331 | : DAG(D), TLI(D.getTargetLoweringInfo()), Level(BeforeLegalizeTypes), |
332 | OptLevel(OL), LegalOperations(false), LegalTypes(false), AA(A) { | ||||
333 | AttributeSet FnAttrs = | ||||
334 | DAG.getMachineFunction().getFunction()->getAttributes(); | ||||
335 | ForCodeSize = | ||||
336 | FnAttrs.hasAttribute(AttributeSet::FunctionIndex, | ||||
337 | Attribute::OptimizeForSize) || | ||||
338 | FnAttrs.hasAttribute(AttributeSet::FunctionIndex, Attribute::MinSize); | ||||
339 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 340 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 341 | /// Run - runs the dag combiner on all nodes in the work list |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 342 | void Run(CombineLevel AtLevel); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 343 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 344 | SelectionDAG &getDAG() const { return DAG; } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 345 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 346 | /// getShiftAmountTy - Returns a type large enough to hold any valid |
347 | /// shift amount - before type legalization these can be huge. | ||||
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 348 | EVT getShiftAmountTy(EVT LHSTy) { |
Elena Demikhovsky | 87070fe | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 349 | assert(LHSTy.isInteger() && "Shift amount is not an integer type!"); |
350 | if (LHSTy.isVector()) | ||||
351 | return LHSTy; | ||||
352 | return LegalTypes ? TLI.getScalarShiftAmountTy(LHSTy) : TLI.getPointerTy(); | ||||
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 353 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 354 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 355 | /// isTypeLegal - This method returns true if we are running before type |
356 | /// legalization or if the specified VT is legal. | ||||
357 | bool isTypeLegal(const EVT &VT) { | ||||
358 | if (!LegalTypes) return true; | ||||
359 | return TLI.isTypeLegal(VT); | ||||
360 | } | ||||
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 361 | |
362 | /// getSetCCResultType - Convenience wrapper around | ||||
363 | /// TargetLowering::getSetCCResultType | ||||
364 | EVT getSetCCResultType(EVT VT) const { | ||||
365 | return TLI.getSetCCResultType(*DAG.getContext(), VT); | ||||
366 | } | ||||
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 367 | }; |
368 | } | ||||
369 | |||||
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 370 | |
371 | namespace { | ||||
372 | /// WorkListRemover - This class is a DAGUpdateListener that removes any deleted | ||||
373 | /// nodes from the worklist. | ||||
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 374 | class WorkListRemover : public SelectionDAG::DAGUpdateListener { |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 375 | DAGCombiner &DC; |
376 | public: | ||||
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 377 | explicit WorkListRemover(DAGCombiner &dc) |
378 | : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {} | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 379 | |
Duncan Sands | edfcf59 | 2008-06-11 11:42:12 +0000 | [diff] [blame] | 380 | virtual void NodeDeleted(SDNode *N, SDNode *E) { |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 381 | DC.removeFromWorkList(N); |
382 | } | ||||
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 383 | }; |
384 | } | ||||
385 | |||||
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 386 | //===----------------------------------------------------------------------===// |
387 | // TargetLowering::DAGCombinerInfo implementation | ||||
388 | //===----------------------------------------------------------------------===// | ||||
389 | |||||
390 | void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) { | ||||
391 | ((DAGCombiner*)DC)->AddToWorkList(N); | ||||
392 | } | ||||
393 | |||||
Cameron Zwarich | ed3caf9 | 2011-04-02 02:40:26 +0000 | [diff] [blame] | 394 | void TargetLowering::DAGCombinerInfo::RemoveFromWorklist(SDNode *N) { |
395 | ((DAGCombiner*)DC)->removeFromWorkList(N); | ||||
396 | } | ||||
397 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 398 | SDValue TargetLowering::DAGCombinerInfo:: |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 399 | CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) { |
400 | return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo); | ||||
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 401 | } |
402 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 403 | SDValue TargetLowering::DAGCombinerInfo:: |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 404 | CombineTo(SDNode *N, SDValue Res, bool AddTo) { |
405 | return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo); | ||||
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 406 | } |
407 | |||||
408 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 409 | SDValue TargetLowering::DAGCombinerInfo:: |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 410 | CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) { |
411 | return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo); | ||||
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 412 | } |
413 | |||||
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 414 | void TargetLowering::DAGCombinerInfo:: |
415 | CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) { | ||||
416 | return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO); | ||||
417 | } | ||||
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 418 | |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 419 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 420 | // Helper Functions |
421 | //===----------------------------------------------------------------------===// | ||||
422 | |||||
423 | /// isNegatibleForFree - Return 1 if we can compute the negated form of the | ||||
424 | /// specified expression for the same cost as the expression itself, or 2 if we | ||||
425 | /// can compute the negated form more cheaply than the expression itself. | ||||
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 426 | static char isNegatibleForFree(SDValue Op, bool LegalOperations, |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 427 | const TargetLowering &TLI, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 428 | const TargetOptions *Options, |
Chris Lattner | 0254e70 | 2008-02-26 07:04:54 +0000 | [diff] [blame] | 429 | unsigned Depth = 0) { |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 430 | // fneg is removable even if it has multiple uses. |
431 | if (Op.getOpcode() == ISD::FNEG) return 2; | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 432 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 433 | // Don't allow anything with multiple uses. |
434 | if (!Op.hasOneUse()) return 0; | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 435 | |
Chris Lattner | 3adf951 | 2007-05-25 02:19:06 +0000 | [diff] [blame] | 436 | // Don't recurse exponentially. |
437 | if (Depth > 6) return 0; | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 438 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 439 | switch (Op.getOpcode()) { |
440 | default: return false; | ||||
441 | case ISD::ConstantFP: | ||||
Chris Lattner | 0254e70 | 2008-02-26 07:04:54 +0000 | [diff] [blame] | 442 | // Don't invert constant FP values after legalize. The negated constant |
443 | // isn't necessarily legal. | ||||
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 444 | return LegalOperations ? 0 : 1; |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 445 | case ISD::FADD: |
446 | // FIXME: determine better conditions for this xform. | ||||
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 447 | if (!Options->UnsafeFPMath) return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 448 | |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 449 | // After operation legalization, it might not be legal to create new FSUBs. |
450 | if (LegalOperations && | ||||
451 | !TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType())) | ||||
452 | return 0; | ||||
453 | |||||
Craig Topper | 956342b | 2012-09-09 22:58:45 +0000 | [diff] [blame] | 454 | // fold (fneg (fadd A, B)) -> (fsub (fneg A), B) |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 455 | if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, |
456 | Options, Depth + 1)) | ||||
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 457 | return V; |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 458 | // fold (fneg (fadd A, B)) -> (fsub (fneg B), A) |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 459 | return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 460 | Depth + 1); |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 461 | case ISD::FSUB: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 462 | // We can't turn -(A-B) into B-A when we honor signed zeros. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 463 | if (!Options->UnsafeFPMath) return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 464 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 465 | // fold (fneg (fsub A, B)) -> (fsub B, A) |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 466 | return 1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 467 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 468 | case ISD::FMUL: |
469 | case ISD::FDIV: | ||||
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 470 | if (Options->HonorSignDependentRoundingFPMath()) return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 471 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 472 | // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y)) |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 473 | if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, |
474 | Options, Depth + 1)) | ||||
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 475 | return V; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 476 | |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 477 | return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 478 | Depth + 1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 479 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 480 | case ISD::FP_EXTEND: |
481 | case ISD::FP_ROUND: | ||||
482 | case ISD::FSIN: | ||||
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 483 | return isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, Options, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 484 | Depth + 1); |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 485 | } |
486 | } | ||||
487 | |||||
488 | /// GetNegatedExpression - If isNegatibleForFree returns true, this function | ||||
489 | /// returns the newly negated expression. | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 490 | static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 491 | bool LegalOperations, unsigned Depth = 0) { |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 492 | // fneg is removable even if it has multiple uses. |
493 | if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 494 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 495 | // Don't allow anything with multiple uses. |
496 | assert(Op.hasOneUse() && "Unknown reuse!"); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 497 | |
Chris Lattner | 3adf951 | 2007-05-25 02:19:06 +0000 | [diff] [blame] | 498 | assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree"); |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 499 | switch (Op.getOpcode()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 500 | default: llvm_unreachable("Unknown code"); |
Dale Johannesen | c4dd3c3 | 2007-08-31 23:34:27 +0000 | [diff] [blame] | 501 | case ISD::ConstantFP: { |
502 | APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF(); | ||||
503 | V.changeSign(); | ||||
504 | return DAG.getConstantFP(V, Op.getValueType()); | ||||
505 | } | ||||
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 506 | case ISD::FADD: |
507 | // FIXME: determine better conditions for this xform. | ||||
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 508 | assert(DAG.getTarget().Options.UnsafeFPMath); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 509 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 510 | // fold (fneg (fadd A, B)) -> (fsub (fneg A), B) |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 511 | if (isNegatibleForFree(Op.getOperand(0), LegalOperations, |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 512 | DAG.getTargetLoweringInfo(), |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 513 | &DAG.getTarget().Options, Depth+1)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 514 | return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 515 | GetNegatedExpression(Op.getOperand(0), DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 516 | LegalOperations, Depth+1), |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 517 | Op.getOperand(1)); |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 518 | // fold (fneg (fadd A, B)) -> (fsub (fneg B), A) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 519 | return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 520 | GetNegatedExpression(Op.getOperand(1), DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 521 | LegalOperations, Depth+1), |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 522 | Op.getOperand(0)); |
523 | case ISD::FSUB: | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 524 | // We can't turn -(A-B) into B-A when we honor signed zeros. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 525 | assert(DAG.getTarget().Options.UnsafeFPMath); |
Dan Gohman | 23ff182 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 526 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 527 | // fold (fneg (fsub 0, B)) -> B |
Dan Gohman | 23ff182 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 528 | if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0))) |
Dale Johannesen | c4dd3c3 | 2007-08-31 23:34:27 +0000 | [diff] [blame] | 529 | if (N0CFP->getValueAPF().isZero()) |
Dan Gohman | 23ff182 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 530 | return Op.getOperand(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 531 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 532 | // fold (fneg (fsub A, B)) -> (fsub B, A) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 533 | return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(), |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 534 | Op.getOperand(1), Op.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 535 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 536 | case ISD::FMUL: |
537 | case ISD::FDIV: | ||||
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 538 | assert(!DAG.getTarget().Options.HonorSignDependentRoundingFPMath()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 539 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 540 | // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 541 | if (isNegatibleForFree(Op.getOperand(0), LegalOperations, |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 542 | DAG.getTargetLoweringInfo(), |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 543 | &DAG.getTarget().Options, Depth+1)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 544 | return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 545 | GetNegatedExpression(Op.getOperand(0), DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 546 | LegalOperations, Depth+1), |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 547 | Op.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 548 | |
Bill Wendling | d34470c | 2009-01-30 23:10:18 +0000 | [diff] [blame] | 549 | // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 550 | return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(), |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 551 | Op.getOperand(0), |
Chris Lattner | 0254e70 | 2008-02-26 07:04:54 +0000 | [diff] [blame] | 552 | GetNegatedExpression(Op.getOperand(1), DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 553 | LegalOperations, Depth+1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 554 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 555 | case ISD::FP_EXTEND: |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 556 | case ISD::FSIN: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 557 | return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 558 | GetNegatedExpression(Op.getOperand(0), DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 559 | LegalOperations, Depth+1)); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 560 | case ISD::FP_ROUND: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 561 | return DAG.getNode(ISD::FP_ROUND, SDLoc(Op), Op.getValueType(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 562 | GetNegatedExpression(Op.getOperand(0), DAG, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 563 | LegalOperations, Depth+1), |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 564 | Op.getOperand(1)); |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 565 | } |
566 | } | ||||
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 567 | |
568 | |||||
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 569 | // isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc |
570 | // that selects between the values 1 and 0, making it equivalent to a setcc. | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 571 | // Also, set the incoming LHS, RHS, and CC references to the appropriate |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 572 | // nodes based on the type of node we are checking. This simplifies life a |
573 | // bit for the callers. | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 574 | static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS, |
575 | SDValue &CC) { | ||||
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 576 | if (N.getOpcode() == ISD::SETCC) { |
577 | LHS = N.getOperand(0); | ||||
578 | RHS = N.getOperand(1); | ||||
579 | CC = N.getOperand(2); | ||||
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 580 | return true; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 581 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 582 | if (N.getOpcode() == ISD::SELECT_CC && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 583 | N.getOperand(2).getOpcode() == ISD::Constant && |
584 | N.getOperand(3).getOpcode() == ISD::Constant && | ||||
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 585 | cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 && |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 586 | cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) { |
587 | LHS = N.getOperand(0); | ||||
588 | RHS = N.getOperand(1); | ||||
589 | CC = N.getOperand(4); | ||||
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 590 | return true; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 591 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 592 | return false; |
593 | } | ||||
594 | |||||
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 595 | // isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only |
596 | // one use. If this is true, it allows the users to invert the operation for | ||||
597 | // free when it is profitable to do so. | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 598 | static bool isOneUseSetCC(SDValue N) { |
599 | SDValue N0, N1, N2; | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 600 | if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse()) |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 601 | return true; |
602 | return false; | ||||
603 | } | ||||
604 | |||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 605 | SDValue DAGCombiner::ReassociateOps(unsigned Opc, SDLoc DL, |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 606 | SDValue N0, SDValue N1) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 607 | EVT VT = N0.getValueType(); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 608 | if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) { |
609 | if (isa<ConstantSDNode>(N1)) { | ||||
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 610 | // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2)) |
Bill Wendling | 6af7618 | 2009-01-30 20:50:00 +0000 | [diff] [blame] | 611 | SDValue OpNode = |
612 | DAG.FoldConstantArithmetic(Opc, VT, | ||||
613 | cast<ConstantSDNode>(N0.getOperand(1)), | ||||
614 | cast<ConstantSDNode>(N1)); | ||||
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 615 | return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode); |
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 616 | } |
617 | if (N0.hasOneUse()) { | ||||
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 618 | // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 619 | SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT, |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 620 | N0.getOperand(0), N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 621 | AddToWorkList(OpNode.getNode()); |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 622 | return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1)); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 623 | } |
624 | } | ||||
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 625 | |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 626 | if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) { |
627 | if (isa<ConstantSDNode>(N0)) { | ||||
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 628 | // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2)) |
Bill Wendling | 6af7618 | 2009-01-30 20:50:00 +0000 | [diff] [blame] | 629 | SDValue OpNode = |
630 | DAG.FoldConstantArithmetic(Opc, VT, | ||||
631 | cast<ConstantSDNode>(N1.getOperand(1)), | ||||
632 | cast<ConstantSDNode>(N0)); | ||||
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 633 | return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode); |
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 634 | } |
635 | if (N1.hasOneUse()) { | ||||
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 636 | // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 637 | SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT, |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 638 | N1.getOperand(0), N0); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 639 | AddToWorkList(OpNode.getNode()); |
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 640 | return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1)); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 641 | } |
642 | } | ||||
Bill Wendling | 35247c3 | 2009-01-30 00:45:56 +0000 | [diff] [blame] | 643 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 644 | return SDValue(); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 645 | } |
646 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 647 | SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo, |
648 | bool AddTo) { | ||||
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 649 | assert(N->getNumValues() == NumTo && "Broken CombineTo call!"); |
650 | ++NodesCombined; | ||||
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 651 | DEBUG(dbgs() << "\nReplacing.1 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 652 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 653 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 654 | To[0].getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 655 | dbgs() << " and " << NumTo-1 << " other values\n"; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 656 | for (unsigned i = 0, e = NumTo; i != e; ++i) |
Jakob Stoklund Olesen | 9f0d4e6 | 2009-12-03 05:15:35 +0000 | [diff] [blame] | 657 | assert((!To[i].getNode() || |
658 | N->getValueType(i) == To[i].getValueType()) && | ||||
Dan Gohman | 764fd0c | 2009-01-21 15:17:51 +0000 | [diff] [blame] | 659 | "Cannot combine value to value of different type!")); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 660 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 661 | DAG.ReplaceAllUsesWith(N, To); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 662 | if (AddTo) { |
663 | // Push the new nodes and any users onto the worklist | ||||
664 | for (unsigned i = 0, e = NumTo; i != e; ++i) { | ||||
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 665 | if (To[i].getNode()) { |
666 | AddToWorkList(To[i].getNode()); | ||||
667 | AddUsersToWorkList(To[i].getNode()); | ||||
668 | } | ||||
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 669 | } |
670 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 671 | |
Dan Gohman | dbe664a | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 672 | // Finally, if the node is now dead, remove it from the graph. The node |
673 | // may not be dead if the replacement process recursively simplified to | ||||
674 | // something else needing this node. | ||||
675 | if (N->use_empty()) { | ||||
676 | // Nodes can be reintroduced into the worklist. Make sure we do not | ||||
677 | // process a node that has been replaced. | ||||
678 | removeFromWorkList(N); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 679 | |
Dan Gohman | dbe664a | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 680 | // Finally, since the node is now dead, remove it from the graph. |
681 | DAG.DeleteNode(N); | ||||
682 | } | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 683 | return SDValue(N, 0); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 684 | } |
685 | |||||
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 686 | void DAGCombiner:: |
687 | CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) { | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 688 | // Replace all uses. If any nodes become isomorphic to other nodes and |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 689 | // are deleted, make sure to remove them from our worklist. |
690 | WorkListRemover DeadNodes(*this); | ||||
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 691 | DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New); |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 692 | |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 693 | // Push the new node and any (possibly new) users onto the worklist. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 694 | AddToWorkList(TLO.New.getNode()); |
695 | AddUsersToWorkList(TLO.New.getNode()); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 696 | |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 697 | // Finally, if the node is now dead, remove it from the graph. The node |
698 | // may not be dead if the replacement process recursively simplified to | ||||
699 | // something else needing this node. | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 700 | if (TLO.Old.getNode()->use_empty()) { |
701 | removeFromWorkList(TLO.Old.getNode()); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 702 | |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 703 | // If the operands of this node are only used by the node, they will now |
704 | // be dead. Make sure to visit them first to delete dead nodes early. | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 705 | for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i) |
706 | if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse()) | ||||
707 | AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode()); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 708 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 709 | DAG.DeleteNode(TLO.Old.getNode()); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 710 | } |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 711 | } |
712 | |||||
713 | /// SimplifyDemandedBits - Check the specified integer node value to see if | ||||
714 | /// it can be simplified or if things it uses can be simplified by bit | ||||
715 | /// propagation. If so, return true. | ||||
716 | bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) { | ||||
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 717 | TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations); |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 718 | APInt KnownZero, KnownOne; |
719 | if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO)) | ||||
720 | return false; | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 721 | |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 722 | // Revisit the node. |
723 | AddToWorkList(Op.getNode()); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 724 | |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 725 | // Replace the old value with the new one. |
726 | ++NodesCombined; | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 727 | DEBUG(dbgs() << "\nReplacing.2 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 728 | TLO.Old.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 729 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 730 | TLO.New.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 731 | dbgs() << '\n'); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 732 | |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 733 | CommitTargetLoweringOpt(TLO); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 734 | return true; |
735 | } | ||||
736 | |||||
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 737 | void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 738 | SDLoc dl(Load); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 739 | EVT VT = Load->getValueType(0); |
740 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0)); | ||||
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 741 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 742 | DEBUG(dbgs() << "\nReplacing.9 "; |
743 | Load->dump(&DAG); | ||||
744 | dbgs() << "\nWith: "; | ||||
745 | Trunc.getNode()->dump(&DAG); | ||||
746 | dbgs() << '\n'); | ||||
747 | WorkListRemover DeadNodes(*this); | ||||
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 748 | DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc); |
749 | DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1)); | ||||
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 750 | removeFromWorkList(Load); |
751 | DAG.DeleteNode(Load); | ||||
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 752 | AddToWorkList(Trunc.getNode()); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 753 | } |
754 | |||||
755 | SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) { | ||||
756 | Replace = false; | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 757 | SDLoc dl(Op); |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 758 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) { |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 759 | EVT MemVT = LD->getMemoryVT(); |
760 | ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD) | ||||
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 761 | ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 762 | : ISD::EXTLOAD) |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 763 | : LD->getExtensionType(); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 764 | Replace = true; |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 765 | return DAG.getExtLoad(ExtType, dl, PVT, |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 766 | LD->getChain(), LD->getBasePtr(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 767 | LD->getPointerInfo(), |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 768 | MemVT, LD->isVolatile(), |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 769 | LD->isNonTemporal(), LD->getAlignment()); |
770 | } | ||||
771 | |||||
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 772 | unsigned Opc = Op.getOpcode(); |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 773 | switch (Opc) { |
774 | default: break; | ||||
775 | case ISD::AssertSext: | ||||
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 776 | return DAG.getNode(ISD::AssertSext, dl, PVT, |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 777 | SExtPromoteOperand(Op.getOperand(0), PVT), |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 778 | Op.getOperand(1)); |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 779 | case ISD::AssertZext: |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 780 | return DAG.getNode(ISD::AssertZext, dl, PVT, |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 781 | ZExtPromoteOperand(Op.getOperand(0), PVT), |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 782 | Op.getOperand(1)); |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 783 | case ISD::Constant: { |
784 | unsigned ExtOpc = | ||||
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 785 | Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 786 | return DAG.getNode(ExtOpc, dl, PVT, Op); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 787 | } |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 788 | } |
789 | |||||
790 | if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT)) | ||||
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 791 | return SDValue(); |
Evan Cheng | caf7740 | 2010-04-23 19:10:30 +0000 | [diff] [blame] | 792 | return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op); |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 793 | } |
794 | |||||
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 795 | SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) { |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 796 | if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT)) |
797 | return SDValue(); | ||||
798 | EVT OldVT = Op.getValueType(); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 799 | SDLoc dl(Op); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 800 | bool Replace = false; |
801 | SDValue NewOp = PromoteOperand(Op, PVT, Replace); | ||||
802 | if (NewOp.getNode() == 0) | ||||
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 803 | return SDValue(); |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 804 | AddToWorkList(NewOp.getNode()); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 805 | |
806 | if (Replace) | ||||
807 | ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode()); | ||||
808 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp, | ||||
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 809 | DAG.getValueType(OldVT)); |
810 | } | ||||
811 | |||||
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 812 | SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) { |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 813 | EVT OldVT = Op.getValueType(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 814 | SDLoc dl(Op); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 815 | bool Replace = false; |
816 | SDValue NewOp = PromoteOperand(Op, PVT, Replace); | ||||
817 | if (NewOp.getNode() == 0) | ||||
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 818 | return SDValue(); |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 819 | AddToWorkList(NewOp.getNode()); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 820 | |
821 | if (Replace) | ||||
822 | ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode()); | ||||
823 | return DAG.getZeroExtendInReg(NewOp, dl, OldVT); | ||||
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 824 | } |
825 | |||||
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 826 | /// PromoteIntBinOp - Promote the specified integer binary operation if the |
827 | /// target indicates it is beneficial. e.g. On x86, it's usually better to | ||||
828 | /// promote i16 operations to i32 since i16 instructions are longer. | ||||
829 | SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) { | ||||
830 | if (!LegalOperations) | ||||
831 | return SDValue(); | ||||
832 | |||||
833 | EVT VT = Op.getValueType(); | ||||
834 | if (VT.isVector() || !VT.isInteger()) | ||||
835 | return SDValue(); | ||||
836 | |||||
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 837 | // If operation type is 'undesirable', e.g. i16 on x86, consider |
838 | // promoting it. | ||||
839 | unsigned Opc = Op.getOpcode(); | ||||
840 | if (TLI.isTypeDesirableForOp(Opc, VT)) | ||||
841 | return SDValue(); | ||||
842 | |||||
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 843 | EVT PVT = VT; |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 844 | // Consult target whether it is a good idea to promote this operation and |
845 | // what's the right type to promote it to. | ||||
846 | if (TLI.IsDesirableToPromoteOp(Op, PVT)) { | ||||
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 847 | assert(PVT != VT && "Don't know what type to promote to!"); |
848 | |||||
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 849 | bool Replace0 = false; |
850 | SDValue N0 = Op.getOperand(0); | ||||
851 | SDValue NN0 = PromoteOperand(N0, PVT, Replace0); | ||||
852 | if (NN0.getNode() == 0) | ||||
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 853 | return SDValue(); |
854 | |||||
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 855 | bool Replace1 = false; |
856 | SDValue N1 = Op.getOperand(1); | ||||
Evan Cheng | aad753b | 2010-05-10 19:03:57 +0000 | [diff] [blame] | 857 | SDValue NN1; |
858 | if (N0 == N1) | ||||
859 | NN1 = NN0; | ||||
860 | else { | ||||
861 | NN1 = PromoteOperand(N1, PVT, Replace1); | ||||
862 | if (NN1.getNode() == 0) | ||||
863 | return SDValue(); | ||||
864 | } | ||||
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 865 | |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 866 | AddToWorkList(NN0.getNode()); |
Evan Cheng | aad753b | 2010-05-10 19:03:57 +0000 | [diff] [blame] | 867 | if (NN1.getNode()) |
868 | AddToWorkList(NN1.getNode()); | ||||
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 869 | |
870 | if (Replace0) | ||||
871 | ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode()); | ||||
872 | if (Replace1) | ||||
873 | ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode()); | ||||
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 874 | |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 875 | DEBUG(dbgs() << "\nPromoting "; |
876 | Op.getNode()->dump(&DAG)); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 877 | SDLoc dl(Op); |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 878 | return DAG.getNode(ISD::TRUNCATE, dl, VT, |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 879 | DAG.getNode(Opc, dl, PVT, NN0, NN1)); |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 880 | } |
881 | return SDValue(); | ||||
882 | } | ||||
883 | |||||
884 | /// PromoteIntShiftOp - Promote the specified integer shift operation if the | ||||
885 | /// target indicates it is beneficial. e.g. On x86, it's usually better to | ||||
886 | /// promote i16 operations to i32 since i16 instructions are longer. | ||||
887 | SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) { | ||||
888 | if (!LegalOperations) | ||||
889 | return SDValue(); | ||||
890 | |||||
891 | EVT VT = Op.getValueType(); | ||||
892 | if (VT.isVector() || !VT.isInteger()) | ||||
893 | return SDValue(); | ||||
894 | |||||
895 | // If operation type is 'undesirable', e.g. i16 on x86, consider | ||||
896 | // promoting it. | ||||
897 | unsigned Opc = Op.getOpcode(); | ||||
898 | if (TLI.isTypeDesirableForOp(Opc, VT)) | ||||
899 | return SDValue(); | ||||
900 | |||||
901 | EVT PVT = VT; | ||||
902 | // Consult target whether it is a good idea to promote this operation and | ||||
903 | // what's the right type to promote it to. | ||||
904 | if (TLI.IsDesirableToPromoteOp(Op, PVT)) { | ||||
905 | assert(PVT != VT && "Don't know what type to promote to!"); | ||||
906 | |||||
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 907 | bool Replace = false; |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 908 | SDValue N0 = Op.getOperand(0); |
909 | if (Opc == ISD::SRA) | ||||
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 910 | N0 = SExtPromoteOperand(Op.getOperand(0), PVT); |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 911 | else if (Opc == ISD::SRL) |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 912 | N0 = ZExtPromoteOperand(Op.getOperand(0), PVT); |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 913 | else |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 914 | N0 = PromoteOperand(N0, PVT, Replace); |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 915 | if (N0.getNode() == 0) |
916 | return SDValue(); | ||||
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 917 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 918 | AddToWorkList(N0.getNode()); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 919 | if (Replace) |
920 | ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode()); | ||||
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 921 | |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 922 | DEBUG(dbgs() << "\nPromoting "; |
923 | Op.getNode()->dump(&DAG)); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 924 | SDLoc dl(Op); |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 925 | return DAG.getNode(ISD::TRUNCATE, dl, VT, |
Evan Cheng | 07c4e10 | 2010-04-22 20:19:46 +0000 | [diff] [blame] | 926 | DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1))); |
Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 927 | } |
928 | return SDValue(); | ||||
929 | } | ||||
930 | |||||
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 931 | SDValue DAGCombiner::PromoteExtend(SDValue Op) { |
932 | if (!LegalOperations) | ||||
933 | return SDValue(); | ||||
934 | |||||
935 | EVT VT = Op.getValueType(); | ||||
936 | if (VT.isVector() || !VT.isInteger()) | ||||
937 | return SDValue(); | ||||
938 | |||||
939 | // If operation type is 'undesirable', e.g. i16 on x86, consider | ||||
940 | // promoting it. | ||||
941 | unsigned Opc = Op.getOpcode(); | ||||
942 | if (TLI.isTypeDesirableForOp(Opc, VT)) | ||||
943 | return SDValue(); | ||||
944 | |||||
945 | EVT PVT = VT; | ||||
946 | // Consult target whether it is a good idea to promote this operation and | ||||
947 | // what's the right type to promote it to. | ||||
948 | if (TLI.IsDesirableToPromoteOp(Op, PVT)) { | ||||
949 | assert(PVT != VT && "Don't know what type to promote to!"); | ||||
950 | // fold (aext (aext x)) -> (aext x) | ||||
951 | // fold (aext (zext x)) -> (zext x) | ||||
952 | // fold (aext (sext x)) -> (sext x) | ||||
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 953 | DEBUG(dbgs() << "\nPromoting "; |
954 | Op.getNode()->dump(&DAG)); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 955 | return DAG.getNode(Op.getOpcode(), SDLoc(Op), VT, Op.getOperand(0)); |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 956 | } |
957 | return SDValue(); | ||||
958 | } | ||||
959 | |||||
960 | bool DAGCombiner::PromoteLoad(SDValue Op) { | ||||
961 | if (!LegalOperations) | ||||
962 | return false; | ||||
963 | |||||
964 | EVT VT = Op.getValueType(); | ||||
965 | if (VT.isVector() || !VT.isInteger()) | ||||
966 | return false; | ||||
967 | |||||
968 | // If operation type is 'undesirable', e.g. i16 on x86, consider | ||||
969 | // promoting it. | ||||
970 | unsigned Opc = Op.getOpcode(); | ||||
971 | if (TLI.isTypeDesirableForOp(Opc, VT)) | ||||
972 | return false; | ||||
973 | |||||
974 | EVT PVT = VT; | ||||
975 | // Consult target whether it is a good idea to promote this operation and | ||||
976 | // what's the right type to promote it to. | ||||
977 | if (TLI.IsDesirableToPromoteOp(Op, PVT)) { | ||||
978 | assert(PVT != VT && "Don't know what type to promote to!"); | ||||
979 | |||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 980 | SDLoc dl(Op); |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 981 | SDNode *N = Op.getNode(); |
982 | LoadSDNode *LD = cast<LoadSDNode>(N); | ||||
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 983 | EVT MemVT = LD->getMemoryVT(); |
984 | ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD) | ||||
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 985 | ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 986 | : ISD::EXTLOAD) |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 987 | : LD->getExtensionType(); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 988 | SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT, |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 989 | LD->getChain(), LD->getBasePtr(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 990 | LD->getPointerInfo(), |
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 991 | MemVT, LD->isVolatile(), |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 992 | LD->isNonTemporal(), LD->getAlignment()); |
993 | SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD); | ||||
994 | |||||
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 995 | DEBUG(dbgs() << "\nPromoting "; |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 996 | N->dump(&DAG); |
Evan Cheng | 95c57ea | 2010-04-24 04:43:44 +0000 | [diff] [blame] | 997 | dbgs() << "\nTo: "; |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 998 | Result.getNode()->dump(&DAG); |
999 | dbgs() << '\n'); | ||||
1000 | WorkListRemover DeadNodes(*this); | ||||
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 1001 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result); |
1002 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1)); | ||||
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 1003 | removeFromWorkList(N); |
1004 | DAG.DeleteNode(N); | ||||
Evan Cheng | ac7eae5 | 2010-04-27 19:48:13 +0000 | [diff] [blame] | 1005 | AddToWorkList(Result.getNode()); |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 1006 | return true; |
1007 | } | ||||
1008 | return false; | ||||
1009 | } | ||||
1010 | |||||
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 1011 | |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 1012 | //===----------------------------------------------------------------------===// |
1013 | // Main DAG Combiner implementation | ||||
1014 | //===----------------------------------------------------------------------===// | ||||
1015 | |||||
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 1016 | void DAGCombiner::Run(CombineLevel AtLevel) { |
1017 | // set the instance variables, so that the various visit routines may use it. | ||||
1018 | Level = AtLevel; | ||||
Eli Friedman | 5018524 | 2011-11-12 00:35:34 +0000 | [diff] [blame] | 1019 | LegalOperations = Level >= AfterLegalizeVectorOps; |
1020 | LegalTypes = Level >= AfterLegalizeTypes; | ||||
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 1021 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1022 | // Add all the dag nodes to the worklist. |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1023 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
1024 | E = DAG.allnodes_end(); I != E; ++I) | ||||
James Molloy | 6660c05 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 1025 | AddToWorkList(I); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 1026 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1027 | // Create a dummy node (which is not added to allnodes), that adds a reference |
1028 | // to the root node, preventing it from being deleted, and tracking any | ||||
1029 | // changes of the root. | ||||
1030 | HandleSDNode Dummy(DAG.getRoot()); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1031 | |
Jim Laskey | 26f7fa7 | 2006-10-17 19:33:52 +0000 | [diff] [blame] | 1032 | // The root of the dag may dangle to deleted nodes until the dag combiner is |
1033 | // done. Set it to null to avoid confusion. | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1034 | DAG.setRoot(SDValue()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1035 | |
James Molloy | 6660c05 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 1036 | // while the worklist isn't empty, find a node and |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1037 | // try and combine it. |
James Molloy | 6660c05 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 1038 | while (!WorkListContents.empty()) { |
1039 | SDNode *N; | ||||
1040 | // The WorkListOrder holds the SDNodes in order, but it may contain duplicates. | ||||
1041 | // In order to avoid a linear scan, we use a set (O(log N)) to hold what the | ||||
1042 | // worklist *should* contain, and check the node we want to visit is should | ||||
1043 | // actually be visited. | ||||
1044 | do { | ||||
Benjamin Kramer | d5f7690 | 2012-03-10 00:23:58 +0000 | [diff] [blame] | 1045 | N = WorkListOrder.pop_back_val(); |
James Molloy | 6660c05 | 2012-02-16 09:17:04 +0000 | [diff] [blame] | 1046 | } while (!WorkListContents.erase(N)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1047 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1048 | // If N has no uses, it is dead. Make sure to revisit all N's operands once |
1049 | // N is deleted from the DAG, since they too may now be dead or may have a | ||||
1050 | // reduced number of uses, allowing other xforms. | ||||
1051 | if (N->use_empty() && N != &Dummy) { | ||||
1052 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) | ||||
1053 | AddToWorkList(N->getOperand(i).getNode()); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1054 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1055 | DAG.DeleteNode(N); |
1056 | continue; | ||||
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1057 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1058 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1059 | SDValue RV = combine(N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1060 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1061 | if (RV.getNode() == 0) |
1062 | continue; | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1063 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1064 | ++NodesCombined; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1065 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1066 | // If we get back the same node we passed in, rather than a new node or |
1067 | // zero, we know that the node must have defined multiple values and | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1068 | // CombineTo was used. Since CombineTo takes care of the worklist |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1069 | // mechanics for us, we have no work to do in this case. |
1070 | if (RV.getNode() == N) | ||||
1071 | continue; | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1072 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1073 | assert(N->getOpcode() != ISD::DELETED_NODE && |
1074 | RV.getNode()->getOpcode() != ISD::DELETED_NODE && | ||||
1075 | "Node was deleted but visit returned new node!"); | ||||
Chris Lattner | 729c6d1 | 2006-05-27 00:43:02 +0000 | [diff] [blame] | 1076 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1077 | DEBUG(dbgs() << "\nReplacing.3 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1078 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 1079 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 1080 | RV.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 1081 | dbgs() << '\n'); |
Eric Christopher | 7332e6e | 2011-07-14 01:12:15 +0000 | [diff] [blame] | 1082 | |
Devang Patel | 9728ea2 | 2011-05-23 22:04:42 +0000 | [diff] [blame] | 1083 | // Transfer debug value. |
1084 | DAG.TransferDbgValues(SDValue(N, 0), RV); | ||||
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1085 | WorkListRemover DeadNodes(*this); |
1086 | if (N->getNumValues() == RV.getNode()->getNumValues()) | ||||
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 1087 | DAG.ReplaceAllUsesWith(N, RV.getNode()); |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1088 | else { |
1089 | assert(N->getValueType(0) == RV.getValueType() && | ||||
1090 | N->getNumValues() == 1 && "Type mismatch"); | ||||
1091 | SDValue OpV = RV; | ||||
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 1092 | DAG.ReplaceAllUsesWith(N, &OpV); |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1093 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1094 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1095 | // Push the new node and any users onto the worklist |
1096 | AddToWorkList(RV.getNode()); | ||||
1097 | AddUsersToWorkList(RV.getNode()); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1098 | |
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1099 | // Add any uses of the old node to the worklist in case this node is the |
1100 | // last one that uses them. They may become dead after this node is | ||||
1101 | // deleted. | ||||
1102 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) | ||||
1103 | AddToWorkList(N->getOperand(i).getNode()); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1104 | |
Dan Gohman | dbe664a | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 1105 | // Finally, if the node is now dead, remove it from the graph. The node |
1106 | // may not be dead if the replacement process recursively simplified to | ||||
1107 | // something else needing this node. | ||||
1108 | if (N->use_empty()) { | ||||
1109 | // Nodes can be reintroduced into the worklist. Make sure we do not | ||||
1110 | // process a node that has been replaced. | ||||
1111 | removeFromWorkList(N); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1112 | |
Dan Gohman | dbe664a | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 1113 | // Finally, since the node is now dead, remove it from the graph. |
1114 | DAG.DeleteNode(N); | ||||
1115 | } | ||||
Evan Cheng | 17a568b | 2008-08-29 22:21:44 +0000 | [diff] [blame] | 1116 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1117 | |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 1118 | // If the root changed (e.g. it was a dead load, update the root). |
1119 | DAG.setRoot(Dummy.getValue()); | ||||
Hal Finkel | 31490ba | 2012-04-16 03:33:22 +0000 | [diff] [blame] | 1120 | DAG.RemoveDeadNodes(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1121 | } |
1122 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1123 | SDValue DAGCombiner::visit(SDNode *N) { |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1124 | switch (N->getOpcode()) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1125 | default: break; |
Nate Begeman | 4942a96 | 2005-09-01 00:33:32 +0000 | [diff] [blame] | 1126 | case ISD::TokenFactor: return visitTokenFactor(N); |
Chris Lattner | fec42eb | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1127 | case ISD::MERGE_VALUES: return visitMERGE_VALUES(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1128 | case ISD::ADD: return visitADD(N); |
1129 | case ISD::SUB: return visitSUB(N); | ||||
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1130 | case ISD::ADDC: return visitADDC(N); |
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1131 | case ISD::SUBC: return visitSUBC(N); |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1132 | case ISD::ADDE: return visitADDE(N); |
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1133 | case ISD::SUBE: return visitSUBE(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1134 | case ISD::MUL: return visitMUL(N); |
1135 | case ISD::SDIV: return visitSDIV(N); | ||||
1136 | case ISD::UDIV: return visitUDIV(N); | ||||
1137 | case ISD::SREM: return visitSREM(N); | ||||
1138 | case ISD::UREM: return visitUREM(N); | ||||
1139 | case ISD::MULHU: return visitMULHU(N); | ||||
1140 | case ISD::MULHS: return visitMULHS(N); | ||||
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1141 | case ISD::SMUL_LOHI: return visitSMUL_LOHI(N); |
1142 | case ISD::UMUL_LOHI: return visitUMUL_LOHI(N); | ||||
Benjamin Kramer | f55d26e | 2011-05-21 18:31:55 +0000 | [diff] [blame] | 1143 | case ISD::SMULO: return visitSMULO(N); |
1144 | case ISD::UMULO: return visitUMULO(N); | ||||
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1145 | case ISD::SDIVREM: return visitSDIVREM(N); |
1146 | case ISD::UDIVREM: return visitUDIVREM(N); | ||||
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1147 | case ISD::AND: return visitAND(N); |
1148 | case ISD::OR: return visitOR(N); | ||||
1149 | case ISD::XOR: return visitXOR(N); | ||||
1150 | case ISD::SHL: return visitSHL(N); | ||||
1151 | case ISD::SRA: return visitSRA(N); | ||||
1152 | case ISD::SRL: return visitSRL(N); | ||||
1153 | case ISD::CTLZ: return visitCTLZ(N); | ||||
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 1154 | case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1155 | case ISD::CTTZ: return visitCTTZ(N); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 1156 | case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1157 | case ISD::CTPOP: return visitCTPOP(N); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1158 | case ISD::SELECT: return visitSELECT(N); |
Benjamin Kramer | 6242fda | 2013-04-26 09:19:19 +0000 | [diff] [blame] | 1159 | case ISD::VSELECT: return visitVSELECT(N); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1160 | case ISD::SELECT_CC: return visitSELECT_CC(N); |
1161 | case ISD::SETCC: return visitSETCC(N); | ||||
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1162 | case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N); |
1163 | case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N); | ||||
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 1164 | case ISD::ANY_EXTEND: return visitANY_EXTEND(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1165 | case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N); |
1166 | case ISD::TRUNCATE: return visitTRUNCATE(N); | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1167 | case ISD::BITCAST: return visitBITCAST(N); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 1168 | case ISD::BUILD_PAIR: return visitBUILD_PAIR(N); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1169 | case ISD::FADD: return visitFADD(N); |
1170 | case ISD::FSUB: return visitFSUB(N); | ||||
1171 | case ISD::FMUL: return visitFMUL(N); | ||||
Owen Anderson | 062c0a5 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 1172 | case ISD::FMA: return visitFMA(N); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1173 | case ISD::FDIV: return visitFDIV(N); |
1174 | case ISD::FREM: return visitFREM(N); | ||||
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 1175 | case ISD::FCOPYSIGN: return visitFCOPYSIGN(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1176 | case ISD::SINT_TO_FP: return visitSINT_TO_FP(N); |
1177 | case ISD::UINT_TO_FP: return visitUINT_TO_FP(N); | ||||
1178 | case ISD::FP_TO_SINT: return visitFP_TO_SINT(N); | ||||
1179 | case ISD::FP_TO_UINT: return visitFP_TO_UINT(N); | ||||
1180 | case ISD::FP_ROUND: return visitFP_ROUND(N); | ||||
1181 | case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N); | ||||
1182 | case ISD::FP_EXTEND: return visitFP_EXTEND(N); | ||||
1183 | case ISD::FNEG: return visitFNEG(N); | ||||
1184 | case ISD::FABS: return visitFABS(N); | ||||
Owen Anderson | 7c626d3 | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 1185 | case ISD::FFLOOR: return visitFFLOOR(N); |
1186 | case ISD::FCEIL: return visitFCEIL(N); | ||||
1187 | case ISD::FTRUNC: return visitFTRUNC(N); | ||||
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1188 | case ISD::BRCOND: return visitBRCOND(N); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1189 | case ISD::BR_CC: return visitBR_CC(N); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 1190 | case ISD::LOAD: return visitLOAD(N); |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 1191 | case ISD::STORE: return visitSTORE(N); |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 1192 | case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N); |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 1193 | case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1194 | case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N); |
1195 | case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N); | ||||
Bruno Cardoso Lopes | e97190f | 2011-09-20 23:19:33 +0000 | [diff] [blame] | 1196 | case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N); |
Chris Lattner | 66445d3 | 2006-03-28 22:11:53 +0000 | [diff] [blame] | 1197 | case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1198 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1199 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1200 | } |
1201 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1202 | SDValue DAGCombiner::combine(SDNode *N) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1203 | SDValue RV = visit(N); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1204 | |
1205 | // If nothing happened, try a target-specific DAG combine. | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1206 | if (RV.getNode() == 0) { |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1207 | assert(N->getOpcode() != ISD::DELETED_NODE && |
1208 | "Node was deleted but visit returned NULL!"); | ||||
1209 | |||||
1210 | if (N->getOpcode() >= ISD::BUILTIN_OP_END || | ||||
1211 | TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) { | ||||
1212 | |||||
1213 | // Expose the DAG combiner to the target combiner impls. | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1214 | TargetLowering::DAGCombinerInfo |
Nadav Rotem | 444b4bf | 2012-12-27 06:47:41 +0000 | [diff] [blame] | 1215 | DagCombineInfo(DAG, Level, false, this); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1216 | |
1217 | RV = TLI.PerformDAGCombine(N, DagCombineInfo); | ||||
1218 | } | ||||
1219 | } | ||||
1220 | |||||
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1221 | // If nothing happened still, try promoting the operation. |
1222 | if (RV.getNode() == 0) { | ||||
1223 | switch (N->getOpcode()) { | ||||
1224 | default: break; | ||||
1225 | case ISD::ADD: | ||||
1226 | case ISD::SUB: | ||||
1227 | case ISD::MUL: | ||||
1228 | case ISD::AND: | ||||
1229 | case ISD::OR: | ||||
1230 | case ISD::XOR: | ||||
1231 | RV = PromoteIntBinOp(SDValue(N, 0)); | ||||
1232 | break; | ||||
1233 | case ISD::SHL: | ||||
1234 | case ISD::SRA: | ||||
1235 | case ISD::SRL: | ||||
1236 | RV = PromoteIntShiftOp(SDValue(N, 0)); | ||||
1237 | break; | ||||
1238 | case ISD::SIGN_EXTEND: | ||||
1239 | case ISD::ZERO_EXTEND: | ||||
1240 | case ISD::ANY_EXTEND: | ||||
1241 | RV = PromoteExtend(SDValue(N, 0)); | ||||
1242 | break; | ||||
1243 | case ISD::LOAD: | ||||
1244 | if (PromoteLoad(SDValue(N, 0))) | ||||
1245 | RV = SDValue(N, 0); | ||||
1246 | break; | ||||
1247 | } | ||||
1248 | } | ||||
1249 | |||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1250 | // If N is a commutative binary node, try commuting it to enable more |
Evan Cheng | 08b1173 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 1251 | // sdisel CSE. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1252 | if (RV.getNode() == 0 && |
Evan Cheng | 08b1173 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 1253 | SelectionDAG::isCommutativeBinOp(N->getOpcode()) && |
1254 | N->getNumValues() == 1) { | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1255 | SDValue N0 = N->getOperand(0); |
1256 | SDValue N1 = N->getOperand(1); | ||||
Bill Wendling | 5c71acf | 2009-01-30 01:13:16 +0000 | [diff] [blame] | 1257 | |
Evan Cheng | 08b1173 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 1258 | // Constant operands are canonicalized to RHS. |
1259 | if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) { | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1260 | SDValue Ops[] = { N1, N0 }; |
Evan Cheng | 08b1173 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 1261 | SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(), |
1262 | Ops, 2); | ||||
Evan Cheng | ea10046 | 2008-03-24 23:55:16 +0000 | [diff] [blame] | 1263 | if (CSENode) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1264 | return SDValue(CSENode, 0); |
Evan Cheng | 08b1173 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 1265 | } |
1266 | } | ||||
1267 | |||||
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1268 | return RV; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1269 | } |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 1270 | |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1271 | /// getInputChainForNode - Given a node, return its input chain if it has one, |
1272 | /// otherwise return a null sd operand. | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1273 | static SDValue getInputChainForNode(SDNode *N) { |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1274 | if (unsigned NumOps = N->getNumOperands()) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1275 | if (N->getOperand(0).getValueType() == MVT::Other) |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1276 | return N->getOperand(0); |
Stephen Lin | b494015 | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 1277 | if (N->getOperand(NumOps-1).getValueType() == MVT::Other) |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1278 | return N->getOperand(NumOps-1); |
1279 | for (unsigned i = 1; i < NumOps-1; ++i) | ||||
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1280 | if (N->getOperand(i).getValueType() == MVT::Other) |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1281 | return N->getOperand(i); |
1282 | } | ||||
Bill Wendling | 5c71acf | 2009-01-30 01:13:16 +0000 | [diff] [blame] | 1283 | return SDValue(); |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1284 | } |
1285 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1286 | SDValue DAGCombiner::visitTokenFactor(SDNode *N) { |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1287 | // If N has two operands, where one has an input chain equal to the other, |
1288 | // the 'other' chain is redundant. | ||||
1289 | if (N->getNumOperands() == 2) { | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1290 | if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1)) |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1291 | return N->getOperand(0); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1292 | if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0)) |
Chris Lattner | 6270f68 | 2006-10-08 22:57:01 +0000 | [diff] [blame] | 1293 | return N->getOperand(1); |
1294 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1295 | |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 1296 | SmallVector<SDNode *, 8> TFs; // List of token factors to visit. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1297 | SmallVector<SDValue, 8> Ops; // Ops for replacing token factor. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1298 | SmallPtrSet<SDNode*, 16> SeenOps; |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 1299 | bool Changed = false; // If we should replace this token factor. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1300 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1301 | // Start out with this token factor. |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 1302 | TFs.push_back(N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1303 | |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 1304 | // Iterate through token factors. The TFs grows when new token factors are |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 1305 | // encountered. |
1306 | for (unsigned i = 0; i < TFs.size(); ++i) { | ||||
1307 | SDNode *TF = TFs[i]; | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1308 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1309 | // Check each of the operands. |
1310 | for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) { | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1311 | SDValue Op = TF->getOperand(i); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1312 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1313 | switch (Op.getOpcode()) { |
1314 | case ISD::EntryToken: | ||||
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 1315 | // Entry tokens don't need to be added to the list. They are |
1316 | // rededundant. | ||||
1317 | Changed = true; | ||||
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1318 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1319 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1320 | case ISD::TokenFactor: |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 1321 | if (Op.hasOneUse() && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1322 | std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) { |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1323 | // Queue up for processing. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1324 | TFs.push_back(Op.getNode()); |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1325 | // Clean up in case the token factor is removed. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1326 | AddToWorkList(Op.getNode()); |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1327 | Changed = true; |
1328 | break; | ||||
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 1329 | } |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1330 | // Fall thru |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1331 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1332 | default: |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 1333 | // Only add if it isn't already in the list. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1334 | if (SeenOps.insert(Op.getNode())) |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 1335 | Ops.push_back(Op); |
Chris Lattner | c76d441 | 2007-05-16 06:37:59 +0000 | [diff] [blame] | 1336 | else |
1337 | Changed = true; | ||||
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1338 | break; |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 1339 | } |
1340 | } | ||||
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1341 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1342 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1343 | SDValue Result; |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1344 | |
1345 | // If we've change things around then replace token factor. | ||||
1346 | if (Changed) { | ||||
Dan Gohman | 3035959 | 2008-01-29 13:02:09 +0000 | [diff] [blame] | 1347 | if (Ops.empty()) { |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1348 | // The entry token is the only possible outcome. |
1349 | Result = DAG.getEntryNode(); | ||||
1350 | } else { | ||||
1351 | // New and improved token factor. | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1352 | Result = DAG.getNode(ISD::TokenFactor, SDLoc(N), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1353 | MVT::Other, &Ops[0], Ops.size()); |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1354 | } |
Bill Wendling | 5c71acf | 2009-01-30 01:13:16 +0000 | [diff] [blame] | 1355 | |
Jim Laskey | 274062c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 1356 | // Don't add users to work list. |
1357 | return CombineTo(N, Result, false); | ||||
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1358 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1359 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 1360 | return Result; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1361 | } |
1362 | |||||
Chris Lattner | fec42eb | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1363 | /// MERGE_VALUES can always be eliminated. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1364 | SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) { |
Chris Lattner | fec42eb | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1365 | WorkListRemover DeadNodes(*this); |
Dan Gohman | 00edf39 | 2009-08-10 23:43:19 +0000 | [diff] [blame] | 1366 | // Replacing results may cause a different MERGE_VALUES to suddenly |
1367 | // be CSE'd with N, and carry its uses with it. Iterate until no | ||||
1368 | // uses remain, to ensure that the node can be safely deleted. | ||||
Pete Cooper | 3affd9e | 2012-06-20 19:35:43 +0000 | [diff] [blame] | 1369 | // First add the users of this node to the work list so that they |
1370 | // can be tried again once they have new operands. | ||||
1371 | AddUsersToWorkList(N); | ||||
Dan Gohman | 00edf39 | 2009-08-10 23:43:19 +0000 | [diff] [blame] | 1372 | do { |
1373 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) | ||||
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 1374 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i)); |
Dan Gohman | 00edf39 | 2009-08-10 23:43:19 +0000 | [diff] [blame] | 1375 | } while (!N->use_empty()); |
Chris Lattner | fec42eb | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1376 | removeFromWorkList(N); |
1377 | DAG.DeleteNode(N); | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1378 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | fec42eb | 2008-02-13 07:25:05 +0000 | [diff] [blame] | 1379 | } |
1380 | |||||
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1381 | static |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1382 | SDValue combineShlAddConstant(SDLoc DL, SDValue N0, SDValue N1, |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1383 | SelectionDAG &DAG) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1384 | EVT VT = N0.getValueType(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1385 | SDValue N00 = N0.getOperand(0); |
1386 | SDValue N01 = N0.getOperand(1); | ||||
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1387 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01); |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1388 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1389 | if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() && |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1390 | isa<ConstantSDNode>(N00.getOperand(1))) { |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1391 | // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), ) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1392 | N0 = DAG.getNode(ISD::ADD, SDLoc(N0), VT, |
1393 | DAG.getNode(ISD::SHL, SDLoc(N00), VT, | ||||
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1394 | N00.getOperand(0), N01), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1395 | DAG.getNode(ISD::SHL, SDLoc(N01), VT, |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1396 | N00.getOperand(1), N01)); |
1397 | return DAG.getNode(ISD::ADD, DL, VT, N0, N1); | ||||
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1398 | } |
Bill Wendling | d69c314 | 2009-01-30 02:23:43 +0000 | [diff] [blame] | 1399 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1400 | return SDValue(); |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1401 | } |
1402 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1403 | SDValue DAGCombiner::visitADD(SDNode *N) { |
1404 | SDValue N0 = N->getOperand(0); | ||||
1405 | SDValue N1 = N->getOperand(1); | ||||
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1406 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
1407 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1408 | EVT VT = N0.getValueType(); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1409 | |
1410 | // fold vector ops | ||||
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1411 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1412 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1413 | if (FoldedVOp.getNode()) return FoldedVOp; |
Craig Topper | 48b509c | 2012-12-10 08:12:29 +0000 | [diff] [blame] | 1414 | |
1415 | // fold (add x, 0) -> x, vector edition | ||||
1416 | if (ISD::isBuildVectorAllZeros(N1.getNode())) | ||||
1417 | return N0; | ||||
1418 | if (ISD::isBuildVectorAllZeros(N0.getNode())) | ||||
1419 | return N1; | ||||
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 1420 | } |
Bill Wendling | 2476e5d | 2008-12-10 22:36:00 +0000 | [diff] [blame] | 1421 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1422 | // fold (add x, undef) -> undef |
Dan Gohman | 70fb1ae | 2007-07-10 15:19:29 +0000 | [diff] [blame] | 1423 | if (N0.getOpcode() == ISD::UNDEF) |
1424 | return N0; | ||||
1425 | if (N1.getOpcode() == ISD::UNDEF) | ||||
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1426 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1427 | // fold (add c1, c2) -> c1+c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1428 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1429 | return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1430 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 1431 | if (N0C && !N1C) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1432 | return DAG.getNode(ISD::ADD, SDLoc(N), VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1433 | // fold (add x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1434 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1435 | return N0; |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1436 | // fold (add Sym, c) -> Sym+c |
1437 | if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0)) | ||||
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 1438 | if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C && |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1439 | GA->getOpcode() == ISD::GlobalAddress) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1440 | return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT, |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1441 | GA->getOffset() + |
1442 | (uint64_t)N1C->getSExtValue()); | ||||
Chris Lattner | 4aafb4f | 2006-01-12 20:22:43 +0000 | [diff] [blame] | 1443 | // fold ((c1-A)+c2) -> (c1+c2)-A |
1444 | if (N1C && N0.getOpcode() == ISD::SUB) | ||||
1445 | if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0))) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1446 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1447 | DAG.getConstant(N1C->getAPIntValue()+ |
1448 | N0C->getAPIntValue(), VT), | ||||
Chris Lattner | 4aafb4f | 2006-01-12 20:22:43 +0000 | [diff] [blame] | 1449 | N0.getOperand(1)); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1450 | // reassociate add |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1451 | SDValue RADD = ReassociateOps(ISD::ADD, SDLoc(N), N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1452 | if (RADD.getNode() != 0) |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1453 | return RADD; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1454 | // fold ((0-A) + B) -> B-A |
1455 | if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) && | ||||
1456 | cast<ConstantSDNode>(N0.getOperand(0))->isNullValue()) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1457 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1, N0.getOperand(1)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1458 | // fold (A + (0-B)) -> A-B |
1459 | if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) && | ||||
1460 | cast<ConstantSDNode>(N1.getOperand(0))->isNullValue()) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1461 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1.getOperand(1)); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1462 | // fold (A+(B-A)) -> B |
1463 | if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1)) | ||||
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1464 | return N1.getOperand(0); |
Dale Johannesen | 56eca91 | 2008-11-27 00:43:21 +0000 | [diff] [blame] | 1465 | // fold ((B-A)+A) -> B |
1466 | if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1)) | ||||
1467 | return N0.getOperand(0); | ||||
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1468 | // fold (A+(B-(A+C))) to (B-C) |
1469 | if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD && | ||||
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1470 | N0 == N1.getOperand(1).getOperand(0)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1471 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0), |
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1472 | N1.getOperand(1).getOperand(1)); |
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1473 | // fold (A+(B-(C+A))) to (B-C) |
1474 | if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD && | ||||
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1475 | N0 == N1.getOperand(1).getOperand(1)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1476 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0), |
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1477 | N1.getOperand(1).getOperand(0)); |
Dale Johannesen | 7c7bc72 | 2008-12-23 23:47:22 +0000 | [diff] [blame] | 1478 | // fold (A+((B-A)+or-C)) to (B+or-C) |
Dale Johannesen | 34d7985 | 2008-12-02 18:40:40 +0000 | [diff] [blame] | 1479 | if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) && |
1480 | N1.getOperand(0).getOpcode() == ISD::SUB && | ||||
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1481 | N0 == N1.getOperand(0).getOperand(1)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1482 | return DAG.getNode(N1.getOpcode(), SDLoc(N), VT, |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1483 | N1.getOperand(0).getOperand(0), N1.getOperand(1)); |
Dale Johannesen | 34d7985 | 2008-12-02 18:40:40 +0000 | [diff] [blame] | 1484 | |
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1485 | // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant |
1486 | if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) { | ||||
1487 | SDValue N00 = N0.getOperand(0); | ||||
1488 | SDValue N01 = N0.getOperand(1); | ||||
1489 | SDValue N10 = N1.getOperand(0); | ||||
1490 | SDValue N11 = N1.getOperand(1); | ||||
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1491 | |
1492 | if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10)) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1493 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, |
1494 | DAG.getNode(ISD::ADD, SDLoc(N0), VT, N00, N10), | ||||
1495 | DAG.getNode(ISD::ADD, SDLoc(N1), VT, N01, N11)); | ||||
Dale Johannesen | 221cd2f | 2008-12-02 01:30:54 +0000 | [diff] [blame] | 1496 | } |
Chris Lattner | 947c289 | 2006-03-13 06:51:27 +0000 | [diff] [blame] | 1497 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1498 | if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0))) |
1499 | return SDValue(N, 0); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1500 | |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1501 | // fold (a+b) -> (a|b) iff a and b share no bits. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1502 | if (VT.isInteger() && !VT.isVector()) { |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 1503 | APInt LHSZero, LHSOne; |
1504 | APInt RHSZero, RHSOne; | ||||
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1505 | DAG.ComputeMaskedBits(N0, LHSZero, LHSOne); |
Bill Wendling | f4eb226 | 2009-01-30 02:31:17 +0000 | [diff] [blame] | 1506 | |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 1507 | if (LHSZero.getBoolValue()) { |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1508 | DAG.ComputeMaskedBits(N1, RHSZero, RHSOne); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1509 | |
Chris Lattner | 947c289 | 2006-03-13 06:51:27 +0000 | [diff] [blame] | 1510 | // If all possibly-set bits on the LHS are clear on the RHS, return an OR. |
1511 | // If all possibly-set bits on the RHS are clear on the LHS, return an OR. | ||||
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1512 | if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1513 | return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1); |
Chris Lattner | 947c289 | 2006-03-13 06:51:27 +0000 | [diff] [blame] | 1514 | } |
1515 | } | ||||
Evan Cheng | 3ef554d | 2006-11-06 08:14:30 +0000 | [diff] [blame] | 1516 | |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1517 | // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), ) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1518 | if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1519 | SDValue Result = combineShlAddConstant(SDLoc(N), N0, N1, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1520 | if (Result.getNode()) return Result; |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1521 | } |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1522 | if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1523 | SDValue Result = combineShlAddConstant(SDLoc(N), N1, N0, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1524 | if (Result.getNode()) return Result; |
Evan Cheng | 42d7ccf | 2007-01-19 17:51:44 +0000 | [diff] [blame] | 1525 | } |
1526 | |||||
Dan Gohman | cd9e155 | 2010-01-19 23:30:49 +0000 | [diff] [blame] | 1527 | // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n)) |
1528 | if (N1.getOpcode() == ISD::SHL && | ||||
1529 | N1.getOperand(0).getOpcode() == ISD::SUB) | ||||
1530 | if (ConstantSDNode *C = | ||||
1531 | dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0))) | ||||
1532 | if (C->getAPIntValue() == 0) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1533 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, |
1534 | DAG.getNode(ISD::SHL, SDLoc(N), VT, | ||||
Dan Gohman | cd9e155 | 2010-01-19 23:30:49 +0000 | [diff] [blame] | 1535 | N1.getOperand(0).getOperand(1), |
1536 | N1.getOperand(1))); | ||||
1537 | if (N0.getOpcode() == ISD::SHL && | ||||
1538 | N0.getOperand(0).getOpcode() == ISD::SUB) | ||||
1539 | if (ConstantSDNode *C = | ||||
1540 | dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0))) | ||||
1541 | if (C->getAPIntValue() == 0) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1542 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1, |
1543 | DAG.getNode(ISD::SHL, SDLoc(N), VT, | ||||
Dan Gohman | cd9e155 | 2010-01-19 23:30:49 +0000 | [diff] [blame] | 1544 | N0.getOperand(0).getOperand(1), |
1545 | N0.getOperand(1))); | ||||
1546 | |||||
Owen Anderson | bc146b0 | 2010-09-21 20:42:50 +0000 | [diff] [blame] | 1547 | if (N1.getOpcode() == ISD::AND) { |
1548 | SDValue AndOp0 = N1.getOperand(0); | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1549 | ConstantSDNode *AndOp1 = dyn_cast<ConstantSDNode>(N1->getOperand(1)); |
Owen Anderson | bc146b0 | 2010-09-21 20:42:50 +0000 | [diff] [blame] | 1550 | unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0); |
1551 | unsigned DestBits = VT.getScalarType().getSizeInBits(); | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1552 | |
Owen Anderson | bc146b0 | 2010-09-21 20:42:50 +0000 | [diff] [blame] | 1553 | // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x)) |
1554 | // and similar xforms where the inner op is either ~0 or 0. | ||||
1555 | if (NumSignBits == DestBits && AndOp1 && AndOp1->isOne()) { | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1556 | SDLoc DL(N); |
Owen Anderson | bc146b0 | 2010-09-21 20:42:50 +0000 | [diff] [blame] | 1557 | return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), AndOp0); |
1558 | } | ||||
1559 | } | ||||
1560 | |||||
Benjamin Kramer | f50125e | 2010-12-22 23:17:45 +0000 | [diff] [blame] | 1561 | // add (sext i1), X -> sub X, (zext i1) |
1562 | if (N0.getOpcode() == ISD::SIGN_EXTEND && | ||||
1563 | N0.getOperand(0).getValueType() == MVT::i1 && | ||||
1564 | !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) { | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1565 | SDLoc DL(N); |
Benjamin Kramer | f50125e | 2010-12-22 23:17:45 +0000 | [diff] [blame] | 1566 | SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)); |
1567 | return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt); | ||||
1568 | } | ||||
1569 | |||||
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1570 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1571 | } |
1572 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1573 | SDValue DAGCombiner::visitADDC(SDNode *N) { |
1574 | SDValue N0 = N->getOperand(0); | ||||
1575 | SDValue N1 = N->getOperand(1); | ||||
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1576 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
1577 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1578 | EVT VT = N0.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1579 | |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1580 | // If the flag result is dead, turn this into an ADD. |
Craig Topper | 704e1a0 | 2012-01-07 18:31:09 +0000 | [diff] [blame] | 1581 | if (!N->hasAnyUseOfValue(1)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1582 | return CombineTo(N, DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, N1), |
Dale Johannesen | 874ae25 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 1583 | DAG.getNode(ISD::CARRY_FALSE, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1584 | SDLoc(N), MVT::Glue)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1585 | |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1586 | // canonicalize constant to RHS. |
Dan Gohman | 0a4627d | 2008-06-23 15:29:14 +0000 | [diff] [blame] | 1587 | if (N0C && !N1C) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1588 | return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N1, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1589 | |
Chris Lattner | b654176 | 2007-03-04 20:40:38 +0000 | [diff] [blame] | 1590 | // fold (addc x, 0) -> x + no carry out |
1591 | if (N1C && N1C->isNullValue()) | ||||
Dale Johannesen | 874ae25 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 1592 | return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1593 | SDLoc(N), MVT::Glue)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1594 | |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1595 | // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits. |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 1596 | APInt LHSZero, LHSOne; |
1597 | APInt RHSZero, RHSOne; | ||||
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1598 | DAG.ComputeMaskedBits(N0, LHSZero, LHSOne); |
Bill Wendling | 14036c0 | 2009-01-30 02:38:00 +0000 | [diff] [blame] | 1599 | |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 1600 | if (LHSZero.getBoolValue()) { |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1601 | DAG.ComputeMaskedBits(N1, RHSZero, RHSOne); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1602 | |
Chris Lattner | b654176 | 2007-03-04 20:40:38 +0000 | [diff] [blame] | 1603 | // If all possibly-set bits on the LHS are clear on the RHS, return an OR. |
1604 | // If all possibly-set bits on the RHS are clear on the LHS, return an OR. | ||||
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1605 | if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1606 | return CombineTo(N, DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1), |
Dale Johannesen | 874ae25 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 1607 | DAG.getNode(ISD::CARRY_FALSE, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1608 | SDLoc(N), MVT::Glue)); |
Chris Lattner | b654176 | 2007-03-04 20:40:38 +0000 | [diff] [blame] | 1609 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1610 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1611 | return SDValue(); |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1612 | } |
1613 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1614 | SDValue DAGCombiner::visitADDE(SDNode *N) { |
1615 | SDValue N0 = N->getOperand(0); | ||||
1616 | SDValue N1 = N->getOperand(1); | ||||
1617 | SDValue CarryIn = N->getOperand(2); | ||||
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1618 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
1619 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1620 | |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1621 | // canonicalize constant to RHS |
Dan Gohman | 0a4627d | 2008-06-23 15:29:14 +0000 | [diff] [blame] | 1622 | if (N0C && !N1C) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1623 | return DAG.getNode(ISD::ADDE, SDLoc(N), N->getVTList(), |
Bill Wendling | 14036c0 | 2009-01-30 02:38:00 +0000 | [diff] [blame] | 1624 | N1, N0, CarryIn); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1625 | |
Chris Lattner | b654176 | 2007-03-04 20:40:38 +0000 | [diff] [blame] | 1626 | // fold (adde x, y, false) -> (addc x, y) |
Dale Johannesen | 874ae25 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 1627 | if (CarryIn.getOpcode() == ISD::CARRY_FALSE) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1628 | return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N0, N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1629 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1630 | return SDValue(); |
Chris Lattner | 9115368 | 2007-03-04 20:03:15 +0000 | [diff] [blame] | 1631 | } |
1632 | |||||
Eric Christopher | 7bccf6a | 2011-02-16 04:50:12 +0000 | [diff] [blame] | 1633 | // Since it may not be valid to emit a fold to zero for vector initializers |
1634 | // check if we can before folding. | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1635 | static SDValue tryFoldToZero(SDLoc DL, const TargetLowering &TLI, EVT VT, |
Hal Finkel | bd6f1f6 | 2013-07-09 17:02:45 +0000 | [diff] [blame] | 1636 | SelectionDAG &DAG, |
1637 | bool LegalOperations, bool LegalTypes) { | ||||
Stephen Lin | b494015 | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 1638 | if (!VT.isVector()) |
Eric Christopher | 7bccf6a | 2011-02-16 04:50:12 +0000 | [diff] [blame] | 1639 | return DAG.getConstant(0, VT); |
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 1640 | if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) { |
Eric Christopher | 7bccf6a | 2011-02-16 04:50:12 +0000 | [diff] [blame] | 1641 | // Produce a vector of zeros. |
Hal Finkel | bd6f1f6 | 2013-07-09 17:02:45 +0000 | [diff] [blame] | 1642 | EVT ElemTy = VT.getVectorElementType(); |
1643 | if (LegalTypes && TLI.getTypeAction(*DAG.getContext(), ElemTy) == | ||||
1644 | TargetLowering::TypePromoteInteger) | ||||
1645 | ElemTy = TLI.getTypeToTransformTo(*DAG.getContext(), ElemTy); | ||||
1646 | assert((!LegalTypes || TLI.isTypeLegal(ElemTy)) && | ||||
1647 | "Type for zero vector elements is not legal"); | ||||
1648 | SDValue El = DAG.getConstant(0, ElemTy); | ||||
Eric Christopher | 7bccf6a | 2011-02-16 04:50:12 +0000 | [diff] [blame] | 1649 | std::vector<SDValue> Ops(VT.getVectorNumElements(), El); |
1650 | return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, | ||||
1651 | &Ops[0], Ops.size()); | ||||
1652 | } | ||||
1653 | return SDValue(); | ||||
1654 | } | ||||
1655 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1656 | SDValue DAGCombiner::visitSUB(SDNode *N) { |
1657 | SDValue N0 = N->getOperand(0); | ||||
1658 | SDValue N1 = N->getOperand(1); | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1659 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode()); |
1660 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); | ||||
Eric Christopher | 7332e6e | 2011-07-14 01:12:15 +0000 | [diff] [blame] | 1661 | ConstantSDNode *N1C1 = N1.getOpcode() != ISD::ADD ? 0 : |
1662 | dyn_cast<ConstantSDNode>(N1.getOperand(1).getNode()); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1663 | EVT VT = N0.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1664 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1665 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1666 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1667 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1668 | if (FoldedVOp.getNode()) return FoldedVOp; |
Craig Topper | 48b509c | 2012-12-10 08:12:29 +0000 | [diff] [blame] | 1669 | |
1670 | // fold (sub x, 0) -> x, vector edition | ||||
1671 | if (ISD::isBuildVectorAllZeros(N1.getNode())) | ||||
1672 | return N0; | ||||
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 1673 | } |
Bill Wendling | 2476e5d | 2008-12-10 22:36:00 +0000 | [diff] [blame] | 1674 | |
Chris Lattner | 854077d | 2005-10-17 01:07:11 +0000 | [diff] [blame] | 1675 | // fold (sub x, x) -> 0 |
Eric Christopher | 169e155 | 2011-02-16 01:10:03 +0000 | [diff] [blame] | 1676 | // FIXME: Refactor this and xor and other similar operations together. |
Eric Christopher | 7bccf6a | 2011-02-16 04:50:12 +0000 | [diff] [blame] | 1677 | if (N0 == N1) |
Hal Finkel | bd6f1f6 | 2013-07-09 17:02:45 +0000 | [diff] [blame] | 1678 | return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1679 | // fold (sub c1, c2) -> c1-c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1680 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1681 | return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C); |
Chris Lattner | 05b5743 | 2005-10-11 06:07:15 +0000 | [diff] [blame] | 1682 | // fold (sub x, c) -> (add x, -c) |
1683 | if (N1C) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1684 | return DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 1685 | DAG.getConstant(-N1C->getAPIntValue(), VT)); |
Evan Cheng | 1ad0e8b | 2010-01-18 21:38:44 +0000 | [diff] [blame] | 1686 | // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) |
1687 | if (N0C && N0C->isAllOnesValue()) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1688 | return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0); |
Benjamin Kramer | 2c94b42 | 2011-01-29 12:34:05 +0000 | [diff] [blame] | 1689 | // fold A-(A-B) -> B |
1690 | if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0)) | ||||
1691 | return N1.getOperand(1); | ||||
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1692 | // fold (A+B)-A -> B |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1693 | if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1694 | return N0.getOperand(1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1695 | // fold (A+B)-B -> A |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1696 | if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1697 | return N0.getOperand(0); |
Eric Christopher | 7332e6e | 2011-07-14 01:12:15 +0000 | [diff] [blame] | 1698 | // fold C2-(A+C1) -> (C2-C1)-A |
1699 | if (N1.getOpcode() == ISD::ADD && N0C && N1C1) { | ||||
Nadav Rotem | 6dfabb6 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 1700 | SDValue NewC = DAG.getConstant(N0C->getAPIntValue() - N1C1->getAPIntValue(), |
1701 | VT); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1702 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, NewC, |
Bill Wendling | 96cb112 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 1703 | N1.getOperand(0)); |
Eric Christopher | 7332e6e | 2011-07-14 01:12:15 +0000 | [diff] [blame] | 1704 | } |
Dale Johannesen | 7c7bc72 | 2008-12-23 23:47:22 +0000 | [diff] [blame] | 1705 | // fold ((A+(B+or-C))-B) -> A+or-C |
Dale Johannesen | fd3b7b7 | 2008-12-16 22:13:49 +0000 | [diff] [blame] | 1706 | if (N0.getOpcode() == ISD::ADD && |
Dale Johannesen | f9cbc1f | 2008-12-23 23:01:27 +0000 | [diff] [blame] | 1707 | (N0.getOperand(1).getOpcode() == ISD::SUB || |
1708 | N0.getOperand(1).getOpcode() == ISD::ADD) && | ||||
Dale Johannesen | fd3b7b7 | 2008-12-16 22:13:49 +0000 | [diff] [blame] | 1709 | N0.getOperand(1).getOperand(0) == N1) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1710 | return DAG.getNode(N0.getOperand(1).getOpcode(), SDLoc(N), VT, |
Bill Wendling | b0702e0 | 2009-01-30 02:42:10 +0000 | [diff] [blame] | 1711 | N0.getOperand(0), N0.getOperand(1).getOperand(1)); |
Dale Johannesen | f9cbc1f | 2008-12-23 23:01:27 +0000 | [diff] [blame] | 1712 | // fold ((A+(C+B))-B) -> A+C |
1713 | if (N0.getOpcode() == ISD::ADD && | ||||
1714 | N0.getOperand(1).getOpcode() == ISD::ADD && | ||||
1715 | N0.getOperand(1).getOperand(1) == N1) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1716 | return DAG.getNode(ISD::ADD, SDLoc(N), VT, |
Bill Wendling | b0702e0 | 2009-01-30 02:42:10 +0000 | [diff] [blame] | 1717 | N0.getOperand(0), N0.getOperand(1).getOperand(0)); |
Dale Johannesen | 58e39b0 | 2008-12-23 01:59:54 +0000 | [diff] [blame] | 1718 | // fold ((A-(B-C))-C) -> A-B |
1719 | if (N0.getOpcode() == ISD::SUB && | ||||
1720 | N0.getOperand(1).getOpcode() == ISD::SUB && | ||||
1721 | N0.getOperand(1).getOperand(1) == N1) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1722 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, |
Bill Wendling | b0702e0 | 2009-01-30 02:42:10 +0000 | [diff] [blame] | 1723 | N0.getOperand(0), N0.getOperand(1).getOperand(0)); |
Bill Wendling | b0702e0 | 2009-01-30 02:42:10 +0000 | [diff] [blame] | 1724 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1725 | // If either operand of a sub is undef, the result is undef |
Dan Gohman | 70fb1ae | 2007-07-10 15:19:29 +0000 | [diff] [blame] | 1726 | if (N0.getOpcode() == ISD::UNDEF) |
1727 | return N0; | ||||
1728 | if (N1.getOpcode() == ISD::UNDEF) | ||||
1729 | return N1; | ||||
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1730 | |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1731 | // If the relocation model supports it, consider symbol offsets. |
1732 | if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0)) | ||||
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 1733 | if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) { |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1734 | // fold (sub Sym, c) -> Sym-c |
1735 | if (N1C && GA->getOpcode() == ISD::GlobalAddress) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1736 | return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT, |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1737 | GA->getOffset() - |
1738 | (uint64_t)N1C->getSExtValue()); | ||||
1739 | // fold (sub Sym+c1, Sym+c2) -> c1-c2 | ||||
1740 | if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1)) | ||||
1741 | if (GA->getGlobal() == GB->getGlobal()) | ||||
1742 | return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(), | ||||
1743 | VT); | ||||
1744 | } | ||||
1745 | |||||
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1746 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1747 | } |
1748 | |||||
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1749 | SDValue DAGCombiner::visitSUBC(SDNode *N) { |
1750 | SDValue N0 = N->getOperand(0); | ||||
1751 | SDValue N1 = N->getOperand(1); | ||||
1752 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); | ||||
1753 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); | ||||
1754 | EVT VT = N0.getValueType(); | ||||
1755 | |||||
1756 | // If the flag result is dead, turn this into an SUB. | ||||
Craig Topper | 704e1a0 | 2012-01-07 18:31:09 +0000 | [diff] [blame] | 1757 | if (!N->hasAnyUseOfValue(1)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1758 | return CombineTo(N, DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1), |
1759 | DAG.getNode(ISD::CARRY_FALSE, SDLoc(N), | ||||
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1760 | MVT::Glue)); |
1761 | |||||
1762 | // fold (subc x, x) -> 0 + no borrow | ||||
1763 | if (N0 == N1) | ||||
1764 | return CombineTo(N, DAG.getConstant(0, VT), | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1765 | DAG.getNode(ISD::CARRY_FALSE, SDLoc(N), |
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1766 | MVT::Glue)); |
1767 | |||||
1768 | // fold (subc x, 0) -> x + no borrow | ||||
1769 | if (N1C && N1C->isNullValue()) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1770 | return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, SDLoc(N), |
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1771 | MVT::Glue)); |
1772 | |||||
1773 | // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow | ||||
1774 | if (N0C && N0C->isAllOnesValue()) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1775 | return CombineTo(N, DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0), |
1776 | DAG.getNode(ISD::CARRY_FALSE, SDLoc(N), | ||||
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1777 | MVT::Glue)); |
1778 | |||||
1779 | return SDValue(); | ||||
1780 | } | ||||
1781 | |||||
1782 | SDValue DAGCombiner::visitSUBE(SDNode *N) { | ||||
1783 | SDValue N0 = N->getOperand(0); | ||||
1784 | SDValue N1 = N->getOperand(1); | ||||
1785 | SDValue CarryIn = N->getOperand(2); | ||||
1786 | |||||
1787 | // fold (sube x, y, false) -> (subc x, y) | ||||
1788 | if (CarryIn.getOpcode() == ISD::CARRY_FALSE) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1789 | return DAG.getNode(ISD::SUBC, SDLoc(N), N->getVTList(), N0, N1); |
Craig Topper | cc27452 | 2012-01-07 09:06:39 +0000 | [diff] [blame] | 1790 | |
1791 | return SDValue(); | ||||
1792 | } | ||||
1793 | |||||
Elena Demikhovsky | d802670 | 2013-06-26 12:15:53 +0000 | [diff] [blame] | 1794 | /// isConstantSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are |
1795 | /// all the same constant or undefined. | ||||
Elena Demikhovsky | 87070fe | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1796 | static bool isConstantSplatVector(SDNode *N, APInt& SplatValue) { |
1797 | BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(N); | ||||
1798 | if (!C) | ||||
1799 | return false; | ||||
1800 | |||||
1801 | APInt SplatUndef; | ||||
1802 | unsigned SplatBitSize; | ||||
1803 | bool HasAnyUndefs; | ||||
1804 | EVT EltVT = N->getValueType(0).getVectorElementType(); | ||||
1805 | return (C->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, | ||||
1806 | HasAnyUndefs) && | ||||
1807 | EltVT.getSizeInBits() >= SplatBitSize); | ||||
1808 | } | ||||
1809 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1810 | SDValue DAGCombiner::visitMUL(SDNode *N) { |
1811 | SDValue N0 = N->getOperand(0); | ||||
1812 | SDValue N1 = N->getOperand(1); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1813 | EVT VT = N0.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1814 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1815 | // fold (mul x, undef) -> 0 |
Dan Gohman | d595b5f | 2007-07-10 14:20:37 +0000 | [diff] [blame] | 1816 | if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 1817 | return DAG.getConstant(0, VT); |
Elena Demikhovsky | 87070fe | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1818 | |
1819 | bool N0IsConst = false; | ||||
1820 | bool N1IsConst = false; | ||||
1821 | APInt ConstValue0, ConstValue1; | ||||
1822 | // fold vector ops | ||||
1823 | if (VT.isVector()) { | ||||
1824 | SDValue FoldedVOp = SimplifyVBinOp(N); | ||||
1825 | if (FoldedVOp.getNode()) return FoldedVOp; | ||||
1826 | |||||
1827 | N0IsConst = isConstantSplatVector(N0.getNode(), ConstValue0); | ||||
1828 | N1IsConst = isConstantSplatVector(N1.getNode(), ConstValue1); | ||||
1829 | } else { | ||||
1830 | N0IsConst = dyn_cast<ConstantSDNode>(N0) != 0; | ||||
1831 | ConstValue0 = N0IsConst? (dyn_cast<ConstantSDNode>(N0))->getAPIntValue() : APInt(); | ||||
1832 | N1IsConst = dyn_cast<ConstantSDNode>(N1) != 0; | ||||
1833 | ConstValue1 = N1IsConst? (dyn_cast<ConstantSDNode>(N1))->getAPIntValue() : APInt(); | ||||
1834 | } | ||||
1835 | |||||
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1836 | // fold (mul c1, c2) -> c1*c2 |
Elena Demikhovsky | 87070fe | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1837 | if (N0IsConst && N1IsConst) |
1838 | return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0.getNode(), N1.getNode()); | ||||
1839 | |||||
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1840 | // canonicalize constant to RHS |
Elena Demikhovsky | 87070fe | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1841 | if (N0IsConst && !N1IsConst) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1842 | return DAG.getNode(ISD::MUL, SDLoc(N), VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1843 | // fold (mul x, 0) -> 0 |
Elena Demikhovsky | 87070fe | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1844 | if (N1IsConst && ConstValue1 == 0) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1845 | return N1; |
Benjamin Kramer | 530d09a | 2013-09-19 13:28:20 +0000 | [diff] [blame] | 1846 | // We require a splat of the entire scalar bit width for non-contiguous |
1847 | // bit patterns. | ||||
1848 | bool IsFullSplat = | ||||
1849 | ConstValue1.getBitWidth() == VT.getScalarType().getSizeInBits(); | ||||
Elena Demikhovsky | 87070fe | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1850 | // fold (mul x, 1) -> x |
Benjamin Kramer | 530d09a | 2013-09-19 13:28:20 +0000 | [diff] [blame] | 1851 | if (N1IsConst && ConstValue1 == 1 && IsFullSplat) |
Elena Demikhovsky | 87070fe | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1852 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1853 | // fold (mul x, -1) -> 0-x |
Elena Demikhovsky | 87070fe | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1854 | if (N1IsConst && ConstValue1.isAllOnesValue()) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1855 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1856 | DAG.getConstant(0, VT), N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1857 | // fold (mul x, (1 << c)) -> x << c |
Benjamin Kramer | 530d09a | 2013-09-19 13:28:20 +0000 | [diff] [blame] | 1858 | if (N1IsConst && ConstValue1.isPowerOf2() && IsFullSplat) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1859 | return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0, |
Elena Demikhovsky | 87070fe | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1860 | DAG.getConstant(ConstValue1.logBase2(), |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1861 | getShiftAmountTy(N0.getValueType()))); |
Chris Lattner | 3e6099b | 2005-10-30 06:41:49 +0000 | [diff] [blame] | 1862 | // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c |
Benjamin Kramer | 530d09a | 2013-09-19 13:28:20 +0000 | [diff] [blame] | 1863 | if (N1IsConst && (-ConstValue1).isPowerOf2() && IsFullSplat) { |
Elena Demikhovsky | 87070fe | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1864 | unsigned Log2Val = (-ConstValue1).logBase2(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1865 | // FIXME: If the input is something that is easily negated (e.g. a |
Chris Lattner | 3e6099b | 2005-10-30 06:41:49 +0000 | [diff] [blame] | 1866 | // single-use add), we should put the negate there. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1867 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1868 | DAG.getConstant(0, VT), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1869 | DAG.getNode(ISD::SHL, SDLoc(N), VT, N0, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1870 | DAG.getConstant(Log2Val, |
1871 | getShiftAmountTy(N0.getValueType())))); | ||||
Chris Lattner | 66b8bc3 | 2009-03-09 20:22:18 +0000 | [diff] [blame] | 1872 | } |
Elena Demikhovsky | 87070fe | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1873 | |
1874 | APInt Val; | ||||
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1875 | // (mul (shl X, c1), c2) -> (mul X, c2 << c1) |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 1876 | if (N1IsConst && N0.getOpcode() == ISD::SHL && |
Elena Demikhovsky | 87070fe | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1877 | (isConstantSplatVector(N0.getOperand(1).getNode(), Val) || |
1878 | isa<ConstantSDNode>(N0.getOperand(1)))) { | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1879 | SDValue C3 = DAG.getNode(ISD::SHL, SDLoc(N), VT, |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1880 | N1, N0.getOperand(1)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1881 | AddToWorkList(C3.getNode()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1882 | return DAG.getNode(ISD::MUL, SDLoc(N), VT, |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1883 | N0.getOperand(0), C3); |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1884 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1885 | |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1886 | // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one |
1887 | // use. | ||||
1888 | { | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1889 | SDValue Sh(0,0), Y(0,0); |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1890 | // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)). |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 1891 | if (N0.getOpcode() == ISD::SHL && |
Elena Demikhovsky | 87070fe | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1892 | (isConstantSplatVector(N0.getOperand(1).getNode(), Val) || |
1893 | isa<ConstantSDNode>(N0.getOperand(1))) && | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1894 | N0.getNode()->hasOneUse()) { |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1895 | Sh = N0; Y = N1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1896 | } else if (N1.getOpcode() == ISD::SHL && |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 1897 | isa<ConstantSDNode>(N1.getOperand(1)) && |
1898 | N1.getNode()->hasOneUse()) { | ||||
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1899 | Sh = N1; Y = N0; |
1900 | } | ||||
Bill Wendling | 73e16b2 | 2009-01-30 02:49:26 +0000 | [diff] [blame] | 1901 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1902 | if (Sh.getNode()) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1903 | SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT, |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1904 | Sh.getOperand(0), Y); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1905 | return DAG.getNode(ISD::SHL, SDLoc(N), VT, |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1906 | Mul, Sh.getOperand(1)); |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 1907 | } |
1908 | } | ||||
Bill Wendling | 73e16b2 | 2009-01-30 02:49:26 +0000 | [diff] [blame] | 1909 | |
Chris Lattner | a1deca3 | 2006-03-04 23:33:26 +0000 | [diff] [blame] | 1910 | // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2) |
Elena Demikhovsky | 87070fe | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 1911 | if (N1IsConst && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() && |
1912 | (isConstantSplatVector(N0.getOperand(1).getNode(), Val) || | ||||
1913 | isa<ConstantSDNode>(N0.getOperand(1)))) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1914 | return DAG.getNode(ISD::ADD, SDLoc(N), VT, |
1915 | DAG.getNode(ISD::MUL, SDLoc(N0), VT, | ||||
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1916 | N0.getOperand(0), N1), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1917 | DAG.getNode(ISD::MUL, SDLoc(N1), VT, |
Bill Wendling | 9c8148a | 2009-01-30 02:45:56 +0000 | [diff] [blame] | 1918 | N0.getOperand(1), N1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1919 | |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1920 | // reassociate mul |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1921 | SDValue RMUL = ReassociateOps(ISD::MUL, SDLoc(N), N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1922 | if (RMUL.getNode() != 0) |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1923 | return RMUL; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1924 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 1925 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1926 | } |
1927 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1928 | SDValue DAGCombiner::visitSDIV(SDNode *N) { |
1929 | SDValue N0 = N->getOperand(0); | ||||
1930 | SDValue N1 = N->getOperand(1); | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1931 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode()); |
1932 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1933 | EVT VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1934 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1935 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1936 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1937 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1938 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 1939 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1940 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1941 | // fold (sdiv c1, c2) -> c1/c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1942 | if (N0C && N1C && !N1C->isNullValue()) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 1943 | return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C); |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1944 | // fold (sdiv X, 1) -> X |
Eli Friedman | fd58cd7 | 2011-10-27 02:06:39 +0000 | [diff] [blame] | 1945 | if (N1C && N1C->getAPIntValue() == 1LL) |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1946 | return N0; |
1947 | // fold (sdiv X, -1) -> 0-X | ||||
1948 | if (N1C && N1C->isAllOnesValue()) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1949 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1950 | DAG.getConstant(0, VT), N0); |
Chris Lattner | 094c8fc | 2005-10-07 06:10:46 +0000 | [diff] [blame] | 1951 | // If we know the sign bits of both operands are zero, strength reduce to a |
1952 | // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2 | ||||
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1953 | if (!VT.isVector()) { |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 1954 | if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1955 | return DAG.getNode(ISD::UDIV, SDLoc(N), N1.getValueType(), |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1956 | N0, N1); |
Chris Lattner | f32aac3 | 2008-01-27 23:32:17 +0000 | [diff] [blame] | 1957 | } |
Nate Begeman | cd6a6ed | 2006-02-17 07:26:20 +0000 | [diff] [blame] | 1958 | // fold (sdiv X, pow2) -> simple ops after legalize |
Eli Friedman | 1c663fe | 2011-12-07 03:55:52 +0000 | [diff] [blame] | 1959 | if (N1C && !N1C->isNullValue() && |
Eli Friedman | fd58cd7 | 2011-10-27 02:06:39 +0000 | [diff] [blame] | 1960 | (N1C->getAPIntValue().isPowerOf2() || |
1961 | (-N1C->getAPIntValue()).isPowerOf2())) { | ||||
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1962 | // If dividing by powers of two is cheap, then don't perform the following |
1963 | // fold. | ||||
1964 | if (TLI.isPow2DivCheap()) | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1965 | return SDValue(); |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1966 | |
Eli Friedman | fd58cd7 | 2011-10-27 02:06:39 +0000 | [diff] [blame] | 1967 | unsigned lg2 = N1C->getAPIntValue().countTrailingZeros(); |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1968 | |
Chris Lattner | 8f4880b | 2006-02-16 08:02:36 +0000 | [diff] [blame] | 1969 | // Splat the sign bit into the register |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1970 | SDValue SGN = DAG.getNode(ISD::SRA, SDLoc(N), VT, N0, |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1971 | DAG.getConstant(VT.getSizeInBits()-1, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1972 | getShiftAmountTy(N0.getValueType()))); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1973 | AddToWorkList(SGN.getNode()); |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1974 | |
Chris Lattner | 8f4880b | 2006-02-16 08:02:36 +0000 | [diff] [blame] | 1975 | // Add (N0 < 0) ? abs2 - 1 : 0; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1976 | SDValue SRL = DAG.getNode(ISD::SRL, SDLoc(N), VT, SGN, |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1977 | DAG.getConstant(VT.getSizeInBits() - lg2, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1978 | getShiftAmountTy(SGN.getValueType()))); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1979 | SDValue ADD = DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, SRL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1980 | AddToWorkList(SRL.getNode()); |
1981 | AddToWorkList(ADD.getNode()); // Divide by pow2 | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1982 | SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), VT, ADD, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1983 | DAG.getConstant(lg2, getShiftAmountTy(ADD.getValueType()))); |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1984 | |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1985 | // If we're dividing by a positive value, we're done. Otherwise, we must |
1986 | // negate the result. | ||||
Eli Friedman | fd58cd7 | 2011-10-27 02:06:39 +0000 | [diff] [blame] | 1987 | if (N1C->getAPIntValue().isNonNegative()) |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1988 | return SRA; |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1989 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1990 | AddToWorkList(SRA.getNode()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1991 | return DAG.getNode(ISD::SUB, SDLoc(N), VT, |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1992 | DAG.getConstant(0, VT), SRA); |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 1993 | } |
Bill Wendling | 944d34b | 2009-01-30 02:52:17 +0000 | [diff] [blame] | 1994 | |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 1995 | // if integer divide is expensive and we satisfy the requirements, emit an |
1996 | // alternate sequence. | ||||
Eli Friedman | fd58cd7 | 2011-10-27 02:06:39 +0000 | [diff] [blame] | 1997 | if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1998 | SDValue Op = BuildSDIV(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1999 | if (Op.getNode()) return Op; |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 2000 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2001 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2002 | // undef / X -> 0 |
2003 | if (N0.getOpcode() == ISD::UNDEF) | ||||
2004 | return DAG.getConstant(0, VT); | ||||
2005 | // X / undef -> undef | ||||
2006 | if (N1.getOpcode() == ISD::UNDEF) | ||||
2007 | return N1; | ||||
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2008 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2009 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2010 | } |
2011 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2012 | SDValue DAGCombiner::visitUDIV(SDNode *N) { |
2013 | SDValue N0 = N->getOperand(0); | ||||
2014 | SDValue N1 = N->getOperand(1); | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2015 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode()); |
2016 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2017 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2018 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2019 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2020 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2021 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2022 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 2023 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2024 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2025 | // fold (udiv c1, c2) -> c1/c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2026 | if (N0C && N1C && !N1C->isNullValue()) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 2027 | return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2028 | // fold (udiv x, (1 << c)) -> x >>u c |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2029 | if (N1C && N1C->getAPIntValue().isPowerOf2()) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2030 | return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2031 | DAG.getConstant(N1C->getAPIntValue().logBase2(), |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2032 | getShiftAmountTy(N0.getValueType()))); |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2033 | // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2 |
Nate Begeman | fb5e4bd | 2006-02-05 07:20:23 +0000 | [diff] [blame] | 2034 | if (N1.getOpcode() == ISD::SHL) { |
2035 | if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) { | ||||
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2036 | if (SHC->getAPIntValue().isPowerOf2()) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2037 | EVT ADDVT = N1.getOperand(1).getValueType(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2038 | SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N), ADDVT, |
Bill Wendling | 07d8514 | 2009-01-30 02:55:25 +0000 | [diff] [blame] | 2039 | N1.getOperand(1), |
2040 | DAG.getConstant(SHC->getAPIntValue() | ||||
2041 | .logBase2(), | ||||
2042 | ADDVT)); | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2043 | AddToWorkList(Add.getNode()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2044 | return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, Add); |
Nate Begeman | fb5e4bd | 2006-02-05 07:20:23 +0000 | [diff] [blame] | 2045 | } |
2046 | } | ||||
2047 | } | ||||
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 2048 | // fold (udiv x, c) -> alternate |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2049 | if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2050 | SDValue Op = BuildUDIV(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2051 | if (Op.getNode()) return Op; |
Chris Lattner | e9936d1 | 2005-10-22 18:50:15 +0000 | [diff] [blame] | 2052 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2053 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2054 | // undef / X -> 0 |
2055 | if (N0.getOpcode() == ISD::UNDEF) | ||||
2056 | return DAG.getConstant(0, VT); | ||||
2057 | // X / undef -> undef | ||||
2058 | if (N1.getOpcode() == ISD::UNDEF) | ||||
2059 | return N1; | ||||
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2060 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2061 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2062 | } |
2063 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2064 | SDValue DAGCombiner::visitSREM(SDNode *N) { |
2065 | SDValue N0 = N->getOperand(0); | ||||
2066 | SDValue N1 = N->getOperand(1); | ||||
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2067 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
2068 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2069 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2070 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2071 | // fold (srem c1, c2) -> c1%c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2072 | if (N0C && N1C && !N1C->isNullValue()) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 2073 | return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 2074 | // If we know the sign bits of both operands are zero, strength reduce to a |
2075 | // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15 | ||||
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2076 | if (!VT.isVector()) { |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2077 | if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2078 | return DAG.getNode(ISD::UREM, SDLoc(N), VT, N0, N1); |
Chris Lattner | ee339f4 | 2008-01-27 23:21:58 +0000 | [diff] [blame] | 2079 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2080 | |
Dan Gohman | 7700304 | 2007-11-26 23:46:11 +0000 | [diff] [blame] | 2081 | // If X/C can be simplified by the division-by-constant logic, lower |
2082 | // X%C to the equivalent of X-X/C*C. | ||||
Chris Lattner | 26d2990 | 2006-10-12 20:58:32 +0000 | [diff] [blame] | 2083 | if (N1C && !N1C->isNullValue()) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2084 | SDValue Div = DAG.getNode(ISD::SDIV, SDLoc(N), VT, N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2085 | AddToWorkList(Div.getNode()); |
2086 | SDValue OptimizedDiv = combine(Div.getNode()); | ||||
2087 | if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) { | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2088 | SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT, |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 2089 | OptimizedDiv, N1); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2090 | SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2091 | AddToWorkList(Mul.getNode()); |
Dan Gohman | 7700304 | 2007-11-26 23:46:11 +0000 | [diff] [blame] | 2092 | return Sub; |
2093 | } | ||||
Chris Lattner | 26d2990 | 2006-10-12 20:58:32 +0000 | [diff] [blame] | 2094 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2095 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2096 | // undef % X -> 0 |
2097 | if (N0.getOpcode() == ISD::UNDEF) | ||||
2098 | return DAG.getConstant(0, VT); | ||||
2099 | // X % undef -> undef | ||||
2100 | if (N1.getOpcode() == ISD::UNDEF) | ||||
2101 | return N1; | ||||
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2102 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2103 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2104 | } |
2105 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2106 | SDValue DAGCombiner::visitUREM(SDNode *N) { |
2107 | SDValue N0 = N->getOperand(0); | ||||
2108 | SDValue N1 = N->getOperand(1); | ||||
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2109 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
2110 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2111 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2112 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2113 | // fold (urem c1, c2) -> c1%c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2114 | if (N0C && N1C && !N1C->isNullValue()) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 2115 | return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 2116 | // fold (urem x, pow2) -> (and x, pow2-1) |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2117 | if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2()) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2118 | return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2119 | DAG.getConstant(N1C->getAPIntValue()-1,VT)); |
Nate Begeman | c031e33 | 2006-02-05 07:36:48 +0000 | [diff] [blame] | 2120 | // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1)) |
2121 | if (N1.getOpcode() == ISD::SHL) { | ||||
2122 | if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) { | ||||
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2123 | if (SHC->getAPIntValue().isPowerOf2()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2124 | SDValue Add = |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2125 | DAG.getNode(ISD::ADD, SDLoc(N), VT, N1, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2126 | DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2127 | VT)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2128 | AddToWorkList(Add.getNode()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2129 | return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, Add); |
Nate Begeman | c031e33 | 2006-02-05 07:36:48 +0000 | [diff] [blame] | 2130 | } |
2131 | } | ||||
2132 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2133 | |
Dan Gohman | 7700304 | 2007-11-26 23:46:11 +0000 | [diff] [blame] | 2134 | // If X/C can be simplified by the division-by-constant logic, lower |
2135 | // X%C to the equivalent of X-X/C*C. | ||||
Chris Lattner | 26d2990 | 2006-10-12 20:58:32 +0000 | [diff] [blame] | 2136 | if (N1C && !N1C->isNullValue()) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2137 | SDValue Div = DAG.getNode(ISD::UDIV, SDLoc(N), VT, N0, N1); |
Dan Gohman | 942ca7f | 2008-09-08 16:59:01 +0000 | [diff] [blame] | 2138 | AddToWorkList(Div.getNode()); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2139 | SDValue OptimizedDiv = combine(Div.getNode()); |
2140 | if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) { | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2141 | SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT, |
Bill Wendling | 6d3bf8c | 2009-01-30 02:57:00 +0000 | [diff] [blame] | 2142 | OptimizedDiv, N1); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2143 | SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2144 | AddToWorkList(Mul.getNode()); |
Dan Gohman | 7700304 | 2007-11-26 23:46:11 +0000 | [diff] [blame] | 2145 | return Sub; |
2146 | } | ||||
Chris Lattner | 26d2990 | 2006-10-12 20:58:32 +0000 | [diff] [blame] | 2147 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2148 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2149 | // undef % X -> 0 |
2150 | if (N0.getOpcode() == ISD::UNDEF) | ||||
2151 | return DAG.getConstant(0, VT); | ||||
2152 | // X % undef -> undef | ||||
2153 | if (N1.getOpcode() == ISD::UNDEF) | ||||
2154 | return N1; | ||||
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2155 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2156 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2157 | } |
2158 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2159 | SDValue DAGCombiner::visitMULHS(SDNode *N) { |
2160 | SDValue N0 = N->getOperand(0); | ||||
2161 | SDValue N1 = N->getOperand(1); | ||||
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2162 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2163 | EVT VT = N->getValueType(0); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2164 | SDLoc DL(N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2165 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2166 | // fold (mulhs x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2167 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2168 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2169 | // fold (mulhs x, 1) -> (sra x, size(x)-1) |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2170 | if (N1C && N1C->getAPIntValue() == 1) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2171 | return DAG.getNode(ISD::SRA, SDLoc(N), N0.getValueType(), N0, |
Bill Wendling | 326411d | 2009-01-30 03:00:18 +0000 | [diff] [blame] | 2172 | DAG.getConstant(N0.getValueType().getSizeInBits() - 1, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2173 | getShiftAmountTy(N0.getValueType()))); |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2174 | // fold (mulhs x, undef) -> 0 |
Dan Gohman | d595b5f | 2007-07-10 14:20:37 +0000 | [diff] [blame] | 2175 | if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2176 | return DAG.getConstant(0, VT); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2177 | |
Chris Lattner | de1c360 | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 2178 | // If the type twice as wide is legal, transform the mulhs to a wider multiply |
2179 | // plus a shift. | ||||
2180 | if (VT.isSimple() && !VT.isVector()) { | ||||
2181 | MVT Simple = VT.getSimpleVT(); | ||||
2182 | unsigned SimpleSize = Simple.getSizeInBits(); | ||||
2183 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2); | ||||
2184 | if (TLI.isOperationLegal(ISD::MUL, NewVT)) { | ||||
2185 | N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0); | ||||
2186 | N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1); | ||||
2187 | N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1); | ||||
Chris Lattner | 1a0fbe2 | 2010-12-15 05:51:39 +0000 | [diff] [blame] | 2188 | N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2189 | DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType()))); |
Chris Lattner | de1c360 | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 2190 | return DAG.getNode(ISD::TRUNCATE, DL, VT, N1); |
2191 | } | ||||
2192 | } | ||||
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2193 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2194 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2195 | } |
2196 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2197 | SDValue DAGCombiner::visitMULHU(SDNode *N) { |
2198 | SDValue N0 = N->getOperand(0); | ||||
2199 | SDValue N1 = N->getOperand(1); | ||||
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2200 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2201 | EVT VT = N->getValueType(0); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2202 | SDLoc DL(N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2203 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2204 | // fold (mulhu x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2205 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2206 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2207 | // fold (mulhu x, 1) -> 0 |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2208 | if (N1C && N1C->getAPIntValue() == 1) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2209 | return DAG.getConstant(0, N0.getValueType()); |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2210 | // fold (mulhu x, undef) -> 0 |
Dan Gohman | d595b5f | 2007-07-10 14:20:37 +0000 | [diff] [blame] | 2211 | if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2212 | return DAG.getConstant(0, VT); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2213 | |
Chris Lattner | de1c360 | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 2214 | // If the type twice as wide is legal, transform the mulhu to a wider multiply |
2215 | // plus a shift. | ||||
2216 | if (VT.isSimple() && !VT.isVector()) { | ||||
2217 | MVT Simple = VT.getSimpleVT(); | ||||
2218 | unsigned SimpleSize = Simple.getSizeInBits(); | ||||
2219 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2); | ||||
2220 | if (TLI.isOperationLegal(ISD::MUL, NewVT)) { | ||||
2221 | N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0); | ||||
2222 | N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1); | ||||
2223 | N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1); | ||||
2224 | N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1, | ||||
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2225 | DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType()))); |
Chris Lattner | de1c360 | 2010-12-13 08:39:01 +0000 | [diff] [blame] | 2226 | return DAG.getNode(ISD::TRUNCATE, DL, VT, N1); |
2227 | } | ||||
2228 | } | ||||
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2229 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2230 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2231 | } |
2232 | |||||
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2233 | /// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that |
2234 | /// compute two values. LoOp and HiOp give the opcodes for the two computations | ||||
2235 | /// that are being performed. Return true if a simplification was made. | ||||
2236 | /// | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2237 | SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2238 | unsigned HiOp) { |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2239 | // If the high half is not needed, just compute the low half. |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2240 | bool HiExists = N->hasAnyUseOfValue(1); |
2241 | if (!HiExists && | ||||
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2242 | (!LegalOperations || |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2243 | TLI.isOperationLegal(LoOp, N->getValueType(0)))) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2244 | SDValue Res = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0), |
Bill Wendling | 826d114 | 2009-01-30 03:08:40 +0000 | [diff] [blame] | 2245 | N->op_begin(), N->getNumOperands()); |
Chris Lattner | 5eee427 | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 2246 | return CombineTo(N, Res, Res); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2247 | } |
2248 | |||||
2249 | // If the low half is not needed, just compute the high half. | ||||
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2250 | bool LoExists = N->hasAnyUseOfValue(0); |
2251 | if (!LoExists && | ||||
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2252 | (!LegalOperations || |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2253 | TLI.isOperationLegal(HiOp, N->getValueType(1)))) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2254 | SDValue Res = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1), |
Bill Wendling | 826d114 | 2009-01-30 03:08:40 +0000 | [diff] [blame] | 2255 | N->op_begin(), N->getNumOperands()); |
Chris Lattner | 5eee427 | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 2256 | return CombineTo(N, Res, Res); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2257 | } |
2258 | |||||
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2259 | // If both halves are used, return as it is. |
2260 | if (LoExists && HiExists) | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2261 | return SDValue(); |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2262 | |
2263 | // If the two computed results can be simplified separately, separate them. | ||||
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2264 | if (LoExists) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2265 | SDValue Lo = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0), |
Bill Wendling | 826d114 | 2009-01-30 03:08:40 +0000 | [diff] [blame] | 2266 | N->op_begin(), N->getNumOperands()); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2267 | AddToWorkList(Lo.getNode()); |
2268 | SDValue LoOpt = combine(Lo.getNode()); | ||||
2269 | if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() && | ||||
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2270 | (!LegalOperations || |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 2271 | TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType()))) |
Chris Lattner | 5eee427 | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 2272 | return CombineTo(N, LoOpt, LoOpt); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2273 | } |
2274 | |||||
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2275 | if (HiExists) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2276 | SDValue Hi = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1), |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2277 | N->op_begin(), N->getNumOperands()); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2278 | AddToWorkList(Hi.getNode()); |
2279 | SDValue HiOpt = combine(Hi.getNode()); | ||||
2280 | if (HiOpt.getNode() && HiOpt != Hi && | ||||
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2281 | (!LegalOperations || |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 2282 | TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType()))) |
Chris Lattner | 5eee427 | 2008-01-26 01:09:19 +0000 | [diff] [blame] | 2283 | return CombineTo(N, HiOpt, HiOpt); |
Evan Cheng | 4471194 | 2007-11-08 09:25:29 +0000 | [diff] [blame] | 2284 | } |
Bill Wendling | 826d114 | 2009-01-30 03:08:40 +0000 | [diff] [blame] | 2285 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2286 | return SDValue(); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2287 | } |
2288 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2289 | SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) { |
2290 | SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS); | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2291 | if (Res.getNode()) return Res; |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2292 | |
Chris Lattner | 33e77d3 | 2010-12-15 06:04:19 +0000 | [diff] [blame] | 2293 | EVT VT = N->getValueType(0); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2294 | SDLoc DL(N); |
Chris Lattner | 33e77d3 | 2010-12-15 06:04:19 +0000 | [diff] [blame] | 2295 | |
2296 | // If the type twice as wide is legal, transform the mulhu to a wider multiply | ||||
2297 | // plus a shift. | ||||
2298 | if (VT.isSimple() && !VT.isVector()) { | ||||
2299 | MVT Simple = VT.getSimpleVT(); | ||||
2300 | unsigned SimpleSize = Simple.getSizeInBits(); | ||||
2301 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2); | ||||
2302 | if (TLI.isOperationLegal(ISD::MUL, NewVT)) { | ||||
2303 | SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0)); | ||||
2304 | SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1)); | ||||
2305 | Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi); | ||||
2306 | // Compute the high part as N1. | ||||
2307 | Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo, | ||||
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2308 | DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType()))); |
Chris Lattner | 33e77d3 | 2010-12-15 06:04:19 +0000 | [diff] [blame] | 2309 | Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi); |
2310 | // Compute the low part as N0. | ||||
2311 | Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo); | ||||
2312 | return CombineTo(N, Lo, Hi); | ||||
2313 | } | ||||
2314 | } | ||||
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2315 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2316 | return SDValue(); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2317 | } |
2318 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2319 | SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) { |
2320 | SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU); | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2321 | if (Res.getNode()) return Res; |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2322 | |
Chris Lattner | 33e77d3 | 2010-12-15 06:04:19 +0000 | [diff] [blame] | 2323 | EVT VT = N->getValueType(0); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2324 | SDLoc DL(N); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2325 | |
Chris Lattner | 33e77d3 | 2010-12-15 06:04:19 +0000 | [diff] [blame] | 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::ZERO_EXTEND, DL, NewVT, N->getOperand(0)); | ||||
2334 | SDValue Hi = DAG.getNode(ISD::ZERO_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 | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2338 | DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType()))); |
Chris Lattner | 33e77d3 | 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 | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2345 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2346 | return SDValue(); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2347 | } |
2348 | |||||
Benjamin Kramer | f55d26e | 2011-05-21 18:31:55 +0000 | [diff] [blame] | 2349 | SDValue DAGCombiner::visitSMULO(SDNode *N) { |
2350 | // (smulo x, 2) -> (saddo x, x) | ||||
2351 | if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1))) | ||||
2352 | if (C2->getAPIntValue() == 2) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2353 | return DAG.getNode(ISD::SADDO, SDLoc(N), N->getVTList(), |
Benjamin Kramer | f55d26e | 2011-05-21 18:31:55 +0000 | [diff] [blame] | 2354 | N->getOperand(0), N->getOperand(0)); |
2355 | |||||
2356 | return SDValue(); | ||||
2357 | } | ||||
2358 | |||||
2359 | SDValue DAGCombiner::visitUMULO(SDNode *N) { | ||||
2360 | // (umulo x, 2) -> (uaddo x, x) | ||||
2361 | if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1))) | ||||
2362 | if (C2->getAPIntValue() == 2) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2363 | return DAG.getNode(ISD::UADDO, SDLoc(N), N->getVTList(), |
Benjamin Kramer | f55d26e | 2011-05-21 18:31:55 +0000 | [diff] [blame] | 2364 | N->getOperand(0), N->getOperand(0)); |
2365 | |||||
2366 | return SDValue(); | ||||
2367 | } | ||||
2368 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2369 | SDValue DAGCombiner::visitSDIVREM(SDNode *N) { |
2370 | SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM); | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2371 | if (Res.getNode()) return Res; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2372 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2373 | return SDValue(); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2374 | } |
2375 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2376 | SDValue DAGCombiner::visitUDIVREM(SDNode *N) { |
2377 | SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM); | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2378 | if (Res.getNode()) return Res; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2379 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2380 | return SDValue(); |
Dan Gohman | 389079b | 2007-10-08 17:57:15 +0000 | [diff] [blame] | 2381 | } |
2382 | |||||
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2383 | /// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with |
2384 | /// two operands of the same opcode, try to simplify it. | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2385 | SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) { |
2386 | SDValue N0 = N->getOperand(0), N1 = N->getOperand(1); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2387 | EVT VT = N0.getValueType(); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2388 | assert(N0.getOpcode() == N1.getOpcode() && "Bad input!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2389 | |
Dan Gohman | ff00a55 | 2010-01-14 03:08:49 +0000 | [diff] [blame] | 2390 | // Bail early if none of these transforms apply. |
2391 | if (N0.getNode()->getNumOperands() == 0) return SDValue(); | ||||
2392 | |||||
Chris Lattner | 540121f | 2006-05-05 06:31:05 +0000 | [diff] [blame] | 2393 | // For each of OP in AND/OR/XOR: |
2394 | // fold (OP (zext x), (zext y)) -> (zext (OP x, y)) | ||||
2395 | // fold (OP (sext x), (sext y)) -> (sext (OP x, y)) | ||||
2396 | // fold (OP (aext x), (aext y)) -> (aext (OP x, y)) | ||||
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 2397 | // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free) |
Nate Begeman | 93e0ed3 | 2009-12-03 07:11:29 +0000 | [diff] [blame] | 2398 | // |
2399 | // do not sink logical op inside of a vector extend, since it may combine | ||||
2400 | // into a vsetcc. | ||||
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2401 | EVT Op0VT = N0.getOperand(0).getValueType(); |
2402 | if ((N0.getOpcode() == ISD::ZERO_EXTEND || | ||||
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 2403 | N0.getOpcode() == ISD::SIGN_EXTEND || |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 2404 | // Avoid infinite looping with PromoteIntBinOp. |
2405 | (N0.getOpcode() == ISD::ANY_EXTEND && | ||||
2406 | (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) || | ||||
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 2407 | (N0.getOpcode() == ISD::TRUNCATE && |
2408 | (!TLI.isZExtFree(VT, Op0VT) || | ||||
2409 | !TLI.isTruncateFree(Op0VT, VT)) && | ||||
2410 | TLI.isTypeLegal(Op0VT))) && | ||||
Nate Begeman | 93e0ed3 | 2009-12-03 07:11:29 +0000 | [diff] [blame] | 2411 | !VT.isVector() && |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2412 | Op0VT == N1.getOperand(0).getValueType() && |
2413 | (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) { | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2414 | SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0), |
Bill Wendling | b74c867 | 2009-01-30 19:25:47 +0000 | [diff] [blame] | 2415 | N0.getOperand(0).getValueType(), |
2416 | N0.getOperand(0), N1.getOperand(0)); | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2417 | AddToWorkList(ORNode.getNode()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2418 | return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, ORNode); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2419 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2420 | |
Chris Lattner | a3dc3f6 | 2006-05-05 06:10:43 +0000 | [diff] [blame] | 2421 | // For each of OP in SHL/SRL/SRA/AND... |
2422 | // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z) | ||||
2423 | // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z) | ||||
2424 | // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z) | ||||
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2425 | if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL || |
Chris Lattner | a3dc3f6 | 2006-05-05 06:10:43 +0000 | [diff] [blame] | 2426 | N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) && |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2427 | N0.getOperand(1) == N1.getOperand(1)) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2428 | SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0), |
Bill Wendling | b74c867 | 2009-01-30 19:25:47 +0000 | [diff] [blame] | 2429 | N0.getOperand(0).getValueType(), |
2430 | N0.getOperand(0), N1.getOperand(0)); | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2431 | AddToWorkList(ORNode.getNode()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2432 | return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, |
Bill Wendling | b74c867 | 2009-01-30 19:25:47 +0000 | [diff] [blame] | 2433 | ORNode, N0.getOperand(1)); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2434 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2435 | |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2436 | // Simplify xor/and/or (bitcast(A), bitcast(B)) -> bitcast(op (A,B)) |
2437 | // Only perform this optimization after type legalization and before | ||||
2438 | // LegalizeVectorOprs. LegalizeVectorOprs promotes vector operations by | ||||
2439 | // adding bitcasts. For example (xor v4i32) is promoted to (v2i64), and | ||||
2440 | // we don't want to undo this promotion. | ||||
2441 | // We also handle SCALAR_TO_VECTOR because xor/or/and operations are cheaper | ||||
2442 | // on scalars. | ||||
Nadav Rotem | 6dfabb6 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 2443 | if ((N0.getOpcode() == ISD::BITCAST || |
2444 | N0.getOpcode() == ISD::SCALAR_TO_VECTOR) && | ||||
2445 | Level == AfterLegalizeTypes) { | ||||
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2446 | SDValue In0 = N0.getOperand(0); |
2447 | SDValue In1 = N1.getOperand(0); | ||||
2448 | EVT In0Ty = In0.getValueType(); | ||||
2449 | EVT In1Ty = In1.getValueType(); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2450 | SDLoc DL(N); |
Nadav Rotem | 6dfabb6 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 2451 | // If both incoming values are integers, and the original types are the |
2452 | // same. | ||||
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2453 | if (In0Ty.isInteger() && In1Ty.isInteger() && In0Ty == In1Ty) { |
Nadav Rotem | 6dfabb6 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 2454 | SDValue Op = DAG.getNode(N->getOpcode(), DL, In0Ty, In0, In1); |
2455 | SDValue BC = DAG.getNode(N0.getOpcode(), DL, VT, Op); | ||||
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2456 | AddToWorkList(Op.getNode()); |
2457 | return BC; | ||||
2458 | } | ||||
2459 | } | ||||
2460 | |||||
2461 | // Xor/and/or are indifferent to the swizzle operation (shuffle of one value). | ||||
2462 | // Simplify xor/and/or (shuff(A), shuff(B)) -> shuff(op (A,B)) | ||||
2463 | // If both shuffles use the same mask, and both shuffle within a single | ||||
2464 | // vector, then it is worthwhile to move the swizzle after the operation. | ||||
2465 | // The type-legalizer generates this pattern when loading illegal | ||||
2466 | // vector types from memory. In many cases this allows additional shuffle | ||||
2467 | // optimizations. | ||||
Craig Topper | f920423 | 2012-04-09 07:19:09 +0000 | [diff] [blame] | 2468 | if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG && |
2469 | N0.getOperand(1).getOpcode() == ISD::UNDEF && | ||||
2470 | N1.getOperand(1).getOpcode() == ISD::UNDEF) { | ||||
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2471 | ShuffleVectorSDNode *SVN0 = cast<ShuffleVectorSDNode>(N0); |
2472 | ShuffleVectorSDNode *SVN1 = cast<ShuffleVectorSDNode>(N1); | ||||
Craig Topper | f920423 | 2012-04-09 07:19:09 +0000 | [diff] [blame] | 2473 | |
2474 | assert(N0.getOperand(0).getValueType() == N1.getOperand(1).getValueType() && | ||||
2475 | "Inputs to shuffles are not the same type"); | ||||
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2476 | |
2477 | unsigned NumElts = VT.getVectorNumElements(); | ||||
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2478 | |
2479 | // Check that both shuffles use the same mask. The masks are known to be of | ||||
2480 | // the same length because the result vector type is the same. | ||||
2481 | bool SameMask = true; | ||||
2482 | for (unsigned i = 0; i != NumElts; ++i) { | ||||
2483 | int Idx0 = SVN0->getMaskElt(i); | ||||
2484 | int Idx1 = SVN1->getMaskElt(i); | ||||
2485 | if (Idx0 != Idx1) { | ||||
2486 | SameMask = false; | ||||
2487 | break; | ||||
2488 | } | ||||
2489 | } | ||||
2490 | |||||
Craig Topper | f920423 | 2012-04-09 07:19:09 +0000 | [diff] [blame] | 2491 | if (SameMask) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2492 | SDValue Op = DAG.getNode(N->getOpcode(), SDLoc(N), VT, |
Craig Topper | f920423 | 2012-04-09 07:19:09 +0000 | [diff] [blame] | 2493 | N0.getOperand(0), N1.getOperand(0)); |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2494 | AddToWorkList(Op.getNode()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2495 | return DAG.getVectorShuffle(VT, SDLoc(N), Op, |
Craig Topper | f920423 | 2012-04-09 07:19:09 +0000 | [diff] [blame] | 2496 | DAG.getUNDEF(VT), &SVN0->getMask()[0]); |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 2497 | } |
2498 | } | ||||
Craig Topper | f920423 | 2012-04-09 07:19:09 +0000 | [diff] [blame] | 2499 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2500 | return SDValue(); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2501 | } |
2502 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2503 | SDValue DAGCombiner::visitAND(SDNode *N) { |
2504 | SDValue N0 = N->getOperand(0); | ||||
2505 | SDValue N1 = N->getOperand(1); | ||||
2506 | SDValue LL, LR, RL, RR, CC0, CC1; | ||||
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2507 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
2508 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2509 | EVT VT = N1.getValueType(); |
Dan Gohman | 6900a39 | 2010-03-04 00:23:16 +0000 | [diff] [blame] | 2510 | unsigned BitWidth = VT.getScalarType().getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2511 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2512 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2513 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2514 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2515 | if (FoldedVOp.getNode()) return FoldedVOp; |
Craig Topper | 9472b4f | 2012-12-08 22:49:19 +0000 | [diff] [blame] | 2516 | |
2517 | // fold (and x, 0) -> 0, vector edition | ||||
2518 | if (ISD::isBuildVectorAllZeros(N0.getNode())) | ||||
2519 | return N0; | ||||
2520 | if (ISD::isBuildVectorAllZeros(N1.getNode())) | ||||
2521 | return N1; | ||||
2522 | |||||
2523 | // fold (and x, -1) -> x, vector edition | ||||
2524 | if (ISD::isBuildVectorAllOnes(N0.getNode())) | ||||
2525 | return N1; | ||||
2526 | if (ISD::isBuildVectorAllOnes(N1.getNode())) | ||||
2527 | return N0; | ||||
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 2528 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2529 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2530 | // fold (and x, undef) -> 0 |
Dan Gohman | d595b5f | 2007-07-10 14:20:37 +0000 | [diff] [blame] | 2531 | if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 2532 | return DAG.getConstant(0, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2533 | // fold (and c1, c2) -> c1&c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2534 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 2535 | return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 2536 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 2537 | if (N0C && !N1C) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2538 | return DAG.getNode(ISD::AND, SDLoc(N), VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2539 | // fold (and x, -1) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2540 | if (N1C && N1C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2541 | return N0; |
2542 | // if (and x, c) is known to be zero, return 0 | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2543 | if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0), |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2544 | APInt::getAllOnesValue(BitWidth))) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2545 | return DAG.getConstant(0, VT); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 2546 | // reassociate and |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2547 | SDValue RAND = ReassociateOps(ISD::AND, SDLoc(N), N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2548 | if (RAND.getNode() != 0) |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 2549 | return RAND; |
Bill Wendling | 7d9f2b9 | 2010-03-03 00:35:56 +0000 | [diff] [blame] | 2550 | // fold (and (or x, C), D) -> D if (C & D) == D |
Nate Begeman | 5dc7e86 | 2005-11-02 18:42:59 +0000 | [diff] [blame] | 2551 | if (N1C && N0.getOpcode() == ISD::OR) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2552 | if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2553 | if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2554 | return N1; |
Chris Lattner | 3603cd6 | 2006-02-02 07:17:31 +0000 | [diff] [blame] | 2555 | // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits. |
2556 | if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) { | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2557 | SDValue N0Op0 = N0.getOperand(0); |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2558 | APInt Mask = ~N1C->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 2559 | Mask = Mask.trunc(N0Op0.getValueSizeInBits()); |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2560 | if (DAG.MaskedValueIsZero(N0Op0, Mask)) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2561 | SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2562 | N0.getValueType(), N0Op0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2563 | |
Chris Lattner | 1ec05d1 | 2006-03-01 21:47:21 +0000 | [diff] [blame] | 2564 | // Replace uses of the AND with uses of the Zero extend node. |
2565 | CombineTo(N, Zext); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2566 | |
Chris Lattner | 3603cd6 | 2006-02-02 07:17:31 +0000 | [diff] [blame] | 2567 | // We actually want to replace all uses of the any_extend with the |
2568 | // zero_extend, to avoid duplicating things. This will later cause this | ||||
2569 | // AND to be folded. | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2570 | CombineTo(N0.getNode(), Zext); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2571 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | 3603cd6 | 2006-02-02 07:17:31 +0000 | [diff] [blame] | 2572 | } |
2573 | } | ||||
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 2574 | // similarly fold (and (X (load ([non_ext|any_ext|zero_ext] V))), c) -> |
James Molloy | 6259dcd | 2012-02-20 12:02:38 +0000 | [diff] [blame] | 2575 | // (X (load ([non_ext|zero_ext] V))) if 'and' only clears top bits which must |
2576 | // already be zero by virtue of the width of the base type of the load. | ||||
2577 | // | ||||
2578 | // the 'X' node here can either be nothing or an extract_vector_elt to catch | ||||
2579 | // more cases. | ||||
2580 | if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT && | ||||
2581 | N0.getOperand(0).getOpcode() == ISD::LOAD) || | ||||
2582 | N0.getOpcode() == ISD::LOAD) { | ||||
2583 | LoadSDNode *Load = cast<LoadSDNode>( (N0.getOpcode() == ISD::LOAD) ? | ||||
2584 | N0 : N0.getOperand(0) ); | ||||
2585 | |||||
2586 | // Get the constant (if applicable) the zero'th operand is being ANDed with. | ||||
2587 | // This can be a pure constant or a vector splat, in which case we treat the | ||||
2588 | // vector as a scalar and use the splat value. | ||||
2589 | APInt Constant = APInt::getNullValue(1); | ||||
2590 | if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) { | ||||
2591 | Constant = C->getAPIntValue(); | ||||
2592 | } else if (BuildVectorSDNode *Vector = dyn_cast<BuildVectorSDNode>(N1)) { | ||||
2593 | APInt SplatValue, SplatUndef; | ||||
2594 | unsigned SplatBitSize; | ||||
2595 | bool HasAnyUndefs; | ||||
2596 | bool IsSplat = Vector->isConstantSplat(SplatValue, SplatUndef, | ||||
2597 | SplatBitSize, HasAnyUndefs); | ||||
2598 | if (IsSplat) { | ||||
2599 | // Undef bits can contribute to a possible optimisation if set, so | ||||
2600 | // set them. | ||||
2601 | SplatValue |= SplatUndef; | ||||
2602 | |||||
2603 | // The splat value may be something like "0x00FFFFFF", which means 0 for | ||||
2604 | // the first vector value and FF for the rest, repeating. We need a mask | ||||
2605 | // that will apply equally to all members of the vector, so AND all the | ||||
2606 | // lanes of the constant together. | ||||
2607 | EVT VT = Vector->getValueType(0); | ||||
2608 | unsigned BitWidth = VT.getVectorElementType().getSizeInBits(); | ||||
Silviu Baranga | 3d5e161 | 2012-09-05 08:57:21 +0000 | [diff] [blame] | 2609 | |
2610 | // If the splat value has been compressed to a bitlength lower | ||||
2611 | // than the size of the vector lane, we need to re-expand it to | ||||
2612 | // the lane size. | ||||
2613 | if (BitWidth > SplatBitSize) | ||||
2614 | for (SplatValue = SplatValue.zextOrTrunc(BitWidth); | ||||
2615 | SplatBitSize < BitWidth; | ||||
2616 | SplatBitSize = SplatBitSize * 2) | ||||
2617 | SplatValue |= SplatValue.shl(SplatBitSize); | ||||
2618 | |||||
James Molloy | 6259dcd | 2012-02-20 12:02:38 +0000 | [diff] [blame] | 2619 | Constant = APInt::getAllOnesValue(BitWidth); |
Silviu Baranga | 3d5e161 | 2012-09-05 08:57:21 +0000 | [diff] [blame] | 2620 | for (unsigned i = 0, n = SplatBitSize/BitWidth; i < n; ++i) |
James Molloy | 6259dcd | 2012-02-20 12:02:38 +0000 | [diff] [blame] | 2621 | Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth); |
2622 | } | ||||
2623 | } | ||||
2624 | |||||
2625 | // If we want to change an EXTLOAD to a ZEXTLOAD, ensure a ZEXTLOAD is | ||||
2626 | // actually legal and isn't going to get expanded, else this is a false | ||||
2627 | // optimisation. | ||||
2628 | bool CanZextLoadProfitably = TLI.isLoadExtLegal(ISD::ZEXTLOAD, | ||||
2629 | Load->getMemoryVT()); | ||||
2630 | |||||
2631 | // Resize the constant to the same size as the original memory access before | ||||
2632 | // extension. If it is still the AllOnesValue then this AND is completely | ||||
2633 | // unneeded. | ||||
2634 | Constant = | ||||
2635 | Constant.zextOrTrunc(Load->getMemoryVT().getScalarType().getSizeInBits()); | ||||
2636 | |||||
2637 | bool B; | ||||
2638 | switch (Load->getExtensionType()) { | ||||
2639 | default: B = false; break; | ||||
2640 | case ISD::EXTLOAD: B = CanZextLoadProfitably; break; | ||||
2641 | case ISD::ZEXTLOAD: | ||||
2642 | case ISD::NON_EXTLOAD: B = true; break; | ||||
2643 | } | ||||
2644 | |||||
2645 | if (B && Constant.isAllOnesValue()) { | ||||
2646 | // If the load type was an EXTLOAD, convert to ZEXTLOAD in order to | ||||
2647 | // preserve semantics once we get rid of the AND. | ||||
2648 | SDValue NewLoad(Load, 0); | ||||
2649 | if (Load->getExtensionType() == ISD::EXTLOAD) { | ||||
2650 | NewLoad = DAG.getLoad(Load->getAddressingMode(), ISD::ZEXTLOAD, | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2651 | Load->getValueType(0), SDLoc(Load), |
James Molloy | 6259dcd | 2012-02-20 12:02:38 +0000 | [diff] [blame] | 2652 | Load->getChain(), Load->getBasePtr(), |
2653 | Load->getOffset(), Load->getMemoryVT(), | ||||
2654 | Load->getMemOperand()); | ||||
2655 | // Replace uses of the EXTLOAD with the new ZEXTLOAD. | ||||
Hal Finkel | d65e463 | 2012-06-20 15:42:48 +0000 | [diff] [blame] | 2656 | if (Load->getNumValues() == 3) { |
2657 | // PRE/POST_INC loads have 3 values. | ||||
2658 | SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1), | ||||
2659 | NewLoad.getValue(2) }; | ||||
2660 | CombineTo(Load, To, 3, true); | ||||
2661 | } else { | ||||
2662 | CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1)); | ||||
2663 | } | ||||
James Molloy | 6259dcd | 2012-02-20 12:02:38 +0000 | [diff] [blame] | 2664 | } |
2665 | |||||
2666 | // Fold the AND away, taking care not to fold to the old load node if we | ||||
2667 | // replaced it. | ||||
2668 | CombineTo(N, (N0.getNode() == Load) ? NewLoad : N0); | ||||
2669 | |||||
2670 | return SDValue(N, 0); // Return N so it doesn't get rechecked! | ||||
2671 | } | ||||
2672 | } | ||||
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2673 | // fold (and (setcc x), (setcc y)) -> (setcc (and x, y)) |
2674 | if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){ | ||||
2675 | ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get(); | ||||
2676 | ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get(); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2677 | |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2678 | if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2679 | LL.getValueType().isInteger()) { |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2680 | // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0) |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2681 | if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2682 | SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0), |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2683 | LR.getValueType(), LL, RL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2684 | AddToWorkList(ORNode.getNode()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2685 | return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2686 | } |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2687 | // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1) |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2688 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2689 | SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(N0), |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2690 | LR.getValueType(), LL, RL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2691 | AddToWorkList(ANDNode.getNode()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2692 | return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2693 | } |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2694 | // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1) |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2695 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2696 | SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0), |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2697 | LR.getValueType(), LL, RL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2698 | AddToWorkList(ORNode.getNode()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2699 | return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2700 | } |
2701 | } | ||||
Jim Grosbach | 51a0280 | 2013-08-13 21:30:58 +0000 | [diff] [blame] | 2702 | // Simplify (and (setne X, 0), (setne X, -1)) -> (setuge (add X, 1), 2) |
2703 | if (LL == RL && isa<ConstantSDNode>(LR) && isa<ConstantSDNode>(RR) && | ||||
2704 | Op0 == Op1 && LL.getValueType().isInteger() && | ||||
2705 | Op0 == ISD::SETNE && ((cast<ConstantSDNode>(LR)->isNullValue() && | ||||
2706 | cast<ConstantSDNode>(RR)->isAllOnesValue()) || | ||||
2707 | (cast<ConstantSDNode>(LR)->isAllOnesValue() && | ||||
2708 | cast<ConstantSDNode>(RR)->isNullValue()))) { | ||||
2709 | SDValue ADDNode = DAG.getNode(ISD::ADD, SDLoc(N0), LL.getValueType(), | ||||
2710 | LL, DAG.getConstant(1, LL.getValueType())); | ||||
2711 | AddToWorkList(ADDNode.getNode()); | ||||
2712 | return DAG.getSetCC(SDLoc(N), VT, ADDNode, | ||||
2713 | DAG.getConstant(2, LL.getValueType()), ISD::SETUGE); | ||||
2714 | } | ||||
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2715 | // canonicalize equivalent to ll == rl |
2716 | if (LL == RR && LR == RL) { | ||||
2717 | Op1 = ISD::getSetCCSwappedOperands(Op1); | ||||
2718 | std::swap(RL, RR); | ||||
2719 | } | ||||
2720 | if (LL == RL && LR == RR) { | ||||
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2721 | bool isInteger = LL.getValueType().isInteger(); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2722 | ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger); |
Chris Lattner | 6e1c623 | 2008-10-28 07:11:07 +0000 | [diff] [blame] | 2723 | if (Result != ISD::SETCC_INVALID && |
Patrik Hagglund | fdbeb05 | 2012-12-19 10:19:55 +0000 | [diff] [blame] | 2724 | (!LegalOperations || |
Owen Anderson | 39125d9 | 2013-02-14 09:07:33 +0000 | [diff] [blame] | 2725 | (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) && |
2726 | TLI.isOperationLegal(ISD::SETCC, | ||||
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 2727 | getSetCCResultType(N0.getSimpleValueType()))))) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2728 | return DAG.getSetCC(SDLoc(N), N0.getValueType(), |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2729 | LL, LR, Result); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2730 | } |
2731 | } | ||||
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2732 | |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2733 | // Simplify: (and (op x...), (op y...)) -> (op (and x, y)) |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 2734 | if (N0.getOpcode() == N1.getOpcode()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2735 | SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2736 | if (Tmp.getNode()) return Tmp; |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 2737 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2738 | |
Nate Begeman | de99629 | 2006-02-03 22:24:05 +0000 | [diff] [blame] | 2739 | // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1) |
2740 | // fold (and (sra)) -> (and (srl)) when possible. | ||||
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2741 | if (!VT.isVector() && |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2742 | SimplifyDemandedBits(SDValue(N, 0))) |
2743 | return SDValue(N, 0); | ||||
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2744 | |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 2745 | // fold (zext_inreg (extload x)) -> (zextload x) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2746 | if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2747 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 2748 | EVT MemVT = LN0->getMemoryVT(); |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 2749 | // If we zero all the possible extended bits, then we can turn this into |
2750 | // a zextload if we are running before legalize or the operation is legal. | ||||
Dan Gohman | 6900a39 | 2010-03-04 00:23:16 +0000 | [diff] [blame] | 2751 | unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits(); |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2752 | if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth, |
Dan Gohman | 6900a39 | 2010-03-04 00:23:16 +0000 | [diff] [blame] | 2753 | BitWidth - MemVT.getScalarType().getSizeInBits())) && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2754 | ((!LegalOperations && !LN0->isVolatile()) || |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 2755 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2756 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT, |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2757 | LN0->getChain(), LN0->getBasePtr(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 2758 | LN0->getPointerInfo(), MemVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2759 | LN0->isVolatile(), LN0->isNonTemporal(), |
2760 | LN0->getAlignment()); | ||||
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 2761 | AddToWorkList(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2762 | CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2763 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 2764 | } |
2765 | } | ||||
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2766 | // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2767 | if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) && |
Evan Cheng | 83060c5 | 2007-03-07 08:07:03 +0000 | [diff] [blame] | 2768 | N0.hasOneUse()) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2769 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 2770 | EVT MemVT = LN0->getMemoryVT(); |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 2771 | // If we zero all the possible extended bits, then we can turn this into |
2772 | // a zextload if we are running before legalize or the operation is legal. | ||||
Dan Gohman | 6900a39 | 2010-03-04 00:23:16 +0000 | [diff] [blame] | 2773 | unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits(); |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2774 | if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth, |
Dan Gohman | 6900a39 | 2010-03-04 00:23:16 +0000 | [diff] [blame] | 2775 | BitWidth - MemVT.getScalarType().getSizeInBits())) && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 2776 | ((!LegalOperations && !LN0->isVolatile()) || |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 2777 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2778 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT, |
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2779 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 2780 | LN0->getBasePtr(), LN0->getPointerInfo(), |
2781 | MemVT, | ||||
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2782 | LN0->isVolatile(), LN0->isNonTemporal(), |
2783 | LN0->getAlignment()); | ||||
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 2784 | AddToWorkList(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2785 | CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2786 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 2787 | } |
2788 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2789 | |
Chris Lattner | 35a9f5a | 2006-02-28 06:49:37 +0000 | [diff] [blame] | 2790 | // fold (and (load x), 255) -> (zextload x, i8) |
2791 | // fold (and (extload x, i16), 255) -> (zextload x, i8) | ||||
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2792 | // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8) |
2793 | if (N1C && (N0.getOpcode() == ISD::LOAD || | ||||
2794 | (N0.getOpcode() == ISD::ANY_EXTEND && | ||||
2795 | N0.getOperand(0).getOpcode() == ISD::LOAD))) { | ||||
2796 | bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND; | ||||
2797 | LoadSDNode *LN0 = HasAnyExt | ||||
2798 | ? cast<LoadSDNode>(N0.getOperand(0)) | ||||
2799 | : cast<LoadSDNode>(N0); | ||||
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2800 | if (LN0->getExtensionType() != ISD::SEXTLOAD && |
Tim Northover | 5bce67a | 2013-07-02 09:58:53 +0000 | [diff] [blame] | 2801 | LN0->isUnindexed() && N0.hasOneUse() && SDValue(LN0, 0).hasOneUse()) { |
Duncan Sands | 8eab8a2 | 2008-06-09 11:32:28 +0000 | [diff] [blame] | 2802 | uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits(); |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2803 | if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){ |
2804 | EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits); | ||||
2805 | EVT LoadedVT = LN0->getMemoryVT(); | ||||
Duncan Sands | 8eab8a2 | 2008-06-09 11:32:28 +0000 | [diff] [blame] | 2806 | |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2807 | if (ExtVT == LoadedVT && |
2808 | (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) { | ||||
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2809 | EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2810 | |
2811 | SDValue NewLoad = | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2812 | DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy, |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2813 | LN0->getChain(), LN0->getBasePtr(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 2814 | LN0->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2815 | ExtVT, LN0->isVolatile(), LN0->isNonTemporal(), |
2816 | LN0->getAlignment()); | ||||
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2817 | AddToWorkList(N); |
2818 | CombineTo(LN0, NewLoad, NewLoad.getValue(1)); | ||||
2819 | return SDValue(N, 0); // Return N so it doesn't get rechecked! | ||||
2820 | } | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2821 | |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2822 | // Do not change the width of a volatile load. |
2823 | // Do not generate loads of non-round integer types since these can | ||||
2824 | // be expensive (and would be wrong if the type is not byte sized). | ||||
2825 | if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() && | ||||
2826 | (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) { | ||||
2827 | EVT PtrType = LN0->getOperand(1).getValueType(); | ||||
Bill Wendling | 2627a88 | 2009-01-30 20:43:18 +0000 | [diff] [blame] | 2828 | |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2829 | unsigned Alignment = LN0->getAlignment(); |
2830 | SDValue NewPtr = LN0->getBasePtr(); | ||||
2831 | |||||
2832 | // For big endian targets, we need to add an offset to the pointer | ||||
2833 | // to load the correct bytes. For little endian systems, we merely | ||||
2834 | // need to read fewer bytes from the same pointer. | ||||
2835 | if (TLI.isBigEndian()) { | ||||
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2836 | unsigned LVTStoreBytes = LoadedVT.getStoreSize(); |
2837 | unsigned EVTStoreBytes = ExtVT.getStoreSize(); | ||||
2838 | unsigned PtrOff = LVTStoreBytes - EVTStoreBytes; | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2839 | NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0), PtrType, |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2840 | NewPtr, DAG.getConstant(PtrOff, PtrType)); |
2841 | Alignment = MinAlign(Alignment, PtrOff); | ||||
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 2842 | } |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2843 | |
2844 | AddToWorkList(NewPtr.getNode()); | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2845 | |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2846 | EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT; |
2847 | SDValue Load = | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2848 | DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy, |
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2849 | LN0->getChain(), NewPtr, |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 2850 | LN0->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2851 | ExtVT, LN0->isVolatile(), LN0->isNonTemporal(), |
2852 | Alignment); | ||||
Chris Lattner | ef7634c | 2010-01-07 21:53:27 +0000 | [diff] [blame] | 2853 | AddToWorkList(N); |
2854 | CombineTo(LN0, Load, Load.getValue(1)); | ||||
2855 | return SDValue(N, 0); // Return N so it doesn't get rechecked! | ||||
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 2856 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2857 | } |
Chris Lattner | 15045b6 | 2006-02-28 06:35:35 +0000 | [diff] [blame] | 2858 | } |
2859 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2860 | |
Evan Cheng | a9e13ba | 2012-07-17 18:54:11 +0000 | [diff] [blame] | 2861 | if (N0.getOpcode() == ISD::ADD && N1.getOpcode() == ISD::SRL && |
2862 | VT.getSizeInBits() <= 64) { | ||||
2863 | if (ConstantSDNode *ADDI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { | ||||
2864 | APInt ADDC = ADDI->getAPIntValue(); | ||||
2865 | if (!TLI.isLegalAddImmediate(ADDC.getSExtValue())) { | ||||
2866 | // Look for (and (add x, c1), (lshr y, c2)). If C1 wasn't a legal | ||||
2867 | // immediate for an add, but it is legal if its top c2 bits are set, | ||||
2868 | // transform the ADD so the immediate doesn't need to be materialized | ||||
2869 | // in a register. | ||||
2870 | if (ConstantSDNode *SRLI = dyn_cast<ConstantSDNode>(N1.getOperand(1))) { | ||||
2871 | APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(), | ||||
2872 | SRLI->getZExtValue()); | ||||
2873 | if (DAG.MaskedValueIsZero(N0.getOperand(1), Mask)) { | ||||
2874 | ADDC |= Mask; | ||||
2875 | if (TLI.isLegalAddImmediate(ADDC.getSExtValue())) { | ||||
2876 | SDValue NewAdd = | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2877 | DAG.getNode(ISD::ADD, SDLoc(N0), VT, |
Evan Cheng | a9e13ba | 2012-07-17 18:54:11 +0000 | [diff] [blame] | 2878 | N0.getOperand(0), DAG.getConstant(ADDC, VT)); |
2879 | CombineTo(N0.getNode(), NewAdd); | ||||
2880 | return SDValue(N, 0); // Return N so it doesn't get rechecked! | ||||
2881 | } | ||||
2882 | } | ||||
2883 | } | ||||
2884 | } | ||||
2885 | } | ||||
2886 | } | ||||
Evan Cheng | a9e13ba | 2012-07-17 18:54:11 +0000 | [diff] [blame] | 2887 | |
Tim Northover | 5d8c2e4 | 2013-08-27 13:46:45 +0000 | [diff] [blame] | 2888 | // fold (and (or (srl N, 8), (shl N, 8)), 0xffff) -> (srl (bswap N), const) |
2889 | if (N1C && N1C->getAPIntValue() == 0xffff && N0.getOpcode() == ISD::OR) { | ||||
2890 | SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0), | ||||
2891 | N0.getOperand(1), false); | ||||
2892 | if (BSwap.getNode()) | ||||
2893 | return BSwap; | ||||
2894 | } | ||||
2895 | |||||
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 2896 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2897 | } |
2898 | |||||
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 2899 | /// MatchBSwapHWord - Match (a >> 8) | (a << 8) as (bswap a) >> 16 |
2900 | /// | ||||
2901 | SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1, | ||||
2902 | bool DemandHighBits) { | ||||
2903 | if (!LegalOperations) | ||||
2904 | return SDValue(); | ||||
2905 | |||||
2906 | EVT VT = N->getValueType(0); | ||||
2907 | if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16) | ||||
2908 | return SDValue(); | ||||
2909 | if (!TLI.isOperationLegal(ISD::BSWAP, VT)) | ||||
2910 | return SDValue(); | ||||
2911 | |||||
2912 | // Recognize (and (shl a, 8), 0xff), (and (srl a, 8), 0xff00) | ||||
2913 | bool LookPassAnd0 = false; | ||||
2914 | bool LookPassAnd1 = false; | ||||
2915 | if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL) | ||||
2916 | std::swap(N0, N1); | ||||
2917 | if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL) | ||||
2918 | std::swap(N0, N1); | ||||
2919 | if (N0.getOpcode() == ISD::AND) { | ||||
2920 | if (!N0.getNode()->hasOneUse()) | ||||
2921 | return SDValue(); | ||||
2922 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); | ||||
2923 | if (!N01C || N01C->getZExtValue() != 0xFF00) | ||||
2924 | return SDValue(); | ||||
2925 | N0 = N0.getOperand(0); | ||||
2926 | LookPassAnd0 = true; | ||||
2927 | } | ||||
2928 | |||||
2929 | if (N1.getOpcode() == ISD::AND) { | ||||
2930 | if (!N1.getNode()->hasOneUse()) | ||||
2931 | return SDValue(); | ||||
2932 | ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); | ||||
2933 | if (!N11C || N11C->getZExtValue() != 0xFF) | ||||
2934 | return SDValue(); | ||||
2935 | N1 = N1.getOperand(0); | ||||
2936 | LookPassAnd1 = true; | ||||
2937 | } | ||||
2938 | |||||
2939 | if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL) | ||||
2940 | std::swap(N0, N1); | ||||
2941 | if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL) | ||||
2942 | return SDValue(); | ||||
2943 | if (!N0.getNode()->hasOneUse() || | ||||
2944 | !N1.getNode()->hasOneUse()) | ||||
2945 | return SDValue(); | ||||
2946 | |||||
2947 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); | ||||
2948 | ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); | ||||
2949 | if (!N01C || !N11C) | ||||
2950 | return SDValue(); | ||||
2951 | if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8) | ||||
2952 | return SDValue(); | ||||
2953 | |||||
2954 | // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8) | ||||
2955 | SDValue N00 = N0->getOperand(0); | ||||
2956 | if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) { | ||||
2957 | if (!N00.getNode()->hasOneUse()) | ||||
2958 | return SDValue(); | ||||
2959 | ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1)); | ||||
2960 | if (!N001C || N001C->getZExtValue() != 0xFF) | ||||
2961 | return SDValue(); | ||||
2962 | N00 = N00.getOperand(0); | ||||
2963 | LookPassAnd0 = true; | ||||
2964 | } | ||||
2965 | |||||
2966 | SDValue N10 = N1->getOperand(0); | ||||
2967 | if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) { | ||||
2968 | if (!N10.getNode()->hasOneUse()) | ||||
2969 | return SDValue(); | ||||
2970 | ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1)); | ||||
2971 | if (!N101C || N101C->getZExtValue() != 0xFF00) | ||||
2972 | return SDValue(); | ||||
2973 | N10 = N10.getOperand(0); | ||||
2974 | LookPassAnd1 = true; | ||||
2975 | } | ||||
2976 | |||||
2977 | if (N00 != N10) | ||||
2978 | return SDValue(); | ||||
2979 | |||||
Tim Northover | 5d8c2e4 | 2013-08-27 13:46:45 +0000 | [diff] [blame] | 2980 | // Make sure everything beyond the low halfword gets set to zero since the SRL |
2981 | // 16 will clear the top bits. | ||||
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 2982 | unsigned OpSizeInBits = VT.getSizeInBits(); |
Tim Northover | 5d8c2e4 | 2013-08-27 13:46:45 +0000 | [diff] [blame] | 2983 | if (DemandHighBits && OpSizeInBits > 16) { |
2984 | // If the left-shift isn't masked out then the only way this is a bswap is | ||||
2985 | // if all bits beyond the low 8 are 0. In that case the entire pattern | ||||
2986 | // reduces to a left shift anyway: leave it for other parts of the combiner. | ||||
2987 | if (!LookPassAnd0) | ||||
2988 | return SDValue(); | ||||
2989 | |||||
2990 | // However, if the right shift isn't masked out then it might be because | ||||
2991 | // it's not needed. See if we can spot that too. | ||||
2992 | if (!LookPassAnd1 && | ||||
2993 | !DAG.MaskedValueIsZero( | ||||
2994 | N10, APInt::getHighBitsSet(OpSizeInBits, OpSizeInBits - 16))) | ||||
2995 | return SDValue(); | ||||
2996 | } | ||||
Eric Christopher | 7332e6e | 2011-07-14 01:12:15 +0000 | [diff] [blame] | 2997 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2998 | SDValue Res = DAG.getNode(ISD::BSWAP, SDLoc(N), VT, N00); |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 2999 | if (OpSizeInBits > 16) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3000 | Res = DAG.getNode(ISD::SRL, SDLoc(N), VT, Res, |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 3001 | DAG.getConstant(OpSizeInBits-16, getShiftAmountTy(VT))); |
3002 | return Res; | ||||
3003 | } | ||||
3004 | |||||
3005 | /// isBSwapHWordElement - Return true if the specified node is an element | ||||
3006 | /// that makes up a 32-bit packed halfword byteswap. i.e. | ||||
3007 | /// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8) | ||||
Craig Topper | a0ec3f9 | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 3008 | static bool isBSwapHWordElement(SDValue N, SmallVectorImpl<SDNode *> &Parts) { |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 3009 | if (!N.getNode()->hasOneUse()) |
3010 | return false; | ||||
3011 | |||||
3012 | unsigned Opc = N.getOpcode(); | ||||
3013 | if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL) | ||||
3014 | return false; | ||||
3015 | |||||
3016 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1)); | ||||
3017 | if (!N1C) | ||||
3018 | return false; | ||||
3019 | |||||
3020 | unsigned Num; | ||||
3021 | switch (N1C->getZExtValue()) { | ||||
3022 | default: | ||||
3023 | return false; | ||||
3024 | case 0xFF: Num = 0; break; | ||||
3025 | case 0xFF00: Num = 1; break; | ||||
3026 | case 0xFF0000: Num = 2; break; | ||||
3027 | case 0xFF000000: Num = 3; break; | ||||
3028 | } | ||||
3029 | |||||
3030 | // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00). | ||||
3031 | SDValue N0 = N.getOperand(0); | ||||
3032 | if (Opc == ISD::AND) { | ||||
3033 | if (Num == 0 || Num == 2) { | ||||
3034 | // (x >> 8) & 0xff | ||||
3035 | // (x >> 8) & 0xff0000 | ||||
3036 | if (N0.getOpcode() != ISD::SRL) | ||||
3037 | return false; | ||||
3038 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); | ||||
3039 | if (!C || C->getZExtValue() != 8) | ||||
3040 | return false; | ||||
3041 | } else { | ||||
3042 | // (x << 8) & 0xff00 | ||||
3043 | // (x << 8) & 0xff000000 | ||||
3044 | if (N0.getOpcode() != ISD::SHL) | ||||
3045 | return false; | ||||
3046 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); | ||||
3047 | if (!C || C->getZExtValue() != 8) | ||||
3048 | return false; | ||||
3049 | } | ||||
3050 | } else if (Opc == ISD::SHL) { | ||||
3051 | // (x & 0xff) << 8 | ||||
3052 | // (x & 0xff0000) << 8 | ||||
3053 | if (Num != 0 && Num != 2) | ||||
3054 | return false; | ||||
3055 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1)); | ||||
3056 | if (!C || C->getZExtValue() != 8) | ||||
3057 | return false; | ||||
3058 | } else { // Opc == ISD::SRL | ||||
3059 | // (x & 0xff00) >> 8 | ||||
3060 | // (x & 0xff000000) >> 8 | ||||
3061 | if (Num != 1 && Num != 3) | ||||
3062 | return false; | ||||
3063 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1)); | ||||
3064 | if (!C || C->getZExtValue() != 8) | ||||
3065 | return false; | ||||
3066 | } | ||||
3067 | |||||
3068 | if (Parts[Num]) | ||||
3069 | return false; | ||||
3070 | |||||
3071 | Parts[Num] = N0.getOperand(0).getNode(); | ||||
3072 | return true; | ||||
3073 | } | ||||
3074 | |||||
3075 | /// MatchBSwapHWord - Match a 32-bit packed halfword bswap. That is | ||||
3076 | /// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8) | ||||
3077 | /// => (rotl (bswap x), 16) | ||||
3078 | SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) { | ||||
3079 | if (!LegalOperations) | ||||
3080 | return SDValue(); | ||||
3081 | |||||
3082 | EVT VT = N->getValueType(0); | ||||
3083 | if (VT != MVT::i32) | ||||
3084 | return SDValue(); | ||||
3085 | if (!TLI.isOperationLegal(ISD::BSWAP, VT)) | ||||
3086 | return SDValue(); | ||||
3087 | |||||
3088 | SmallVector<SDNode*,4> Parts(4, (SDNode*)0); | ||||
3089 | // Look for either | ||||
3090 | // (or (or (and), (and)), (or (and), (and))) | ||||
3091 | // (or (or (or (and), (and)), (and)), (and)) | ||||
3092 | if (N0.getOpcode() != ISD::OR) | ||||
3093 | return SDValue(); | ||||
3094 | SDValue N00 = N0.getOperand(0); | ||||
3095 | SDValue N01 = N0.getOperand(1); | ||||
3096 | |||||
Evan Cheng | 9a65a01 | 2012-12-13 01:34:32 +0000 | [diff] [blame] | 3097 | if (N1.getOpcode() == ISD::OR && |
3098 | N00.getNumOperands() == 2 && N01.getNumOperands() == 2) { | ||||
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 3099 | // (or (or (and), (and)), (or (and), (and))) |
3100 | SDValue N000 = N00.getOperand(0); | ||||
3101 | if (!isBSwapHWordElement(N000, Parts)) | ||||
3102 | return SDValue(); | ||||
3103 | |||||
3104 | SDValue N001 = N00.getOperand(1); | ||||
3105 | if (!isBSwapHWordElement(N001, Parts)) | ||||
3106 | return SDValue(); | ||||
3107 | SDValue N010 = N01.getOperand(0); | ||||
3108 | if (!isBSwapHWordElement(N010, Parts)) | ||||
3109 | return SDValue(); | ||||
3110 | SDValue N011 = N01.getOperand(1); | ||||
3111 | if (!isBSwapHWordElement(N011, Parts)) | ||||
3112 | return SDValue(); | ||||
3113 | } else { | ||||
3114 | // (or (or (or (and), (and)), (and)), (and)) | ||||
3115 | if (!isBSwapHWordElement(N1, Parts)) | ||||
3116 | return SDValue(); | ||||
3117 | if (!isBSwapHWordElement(N01, Parts)) | ||||
3118 | return SDValue(); | ||||
3119 | if (N00.getOpcode() != ISD::OR) | ||||
3120 | return SDValue(); | ||||
3121 | SDValue N000 = N00.getOperand(0); | ||||
3122 | if (!isBSwapHWordElement(N000, Parts)) | ||||
3123 | return SDValue(); | ||||
3124 | SDValue N001 = N00.getOperand(1); | ||||
3125 | if (!isBSwapHWordElement(N001, Parts)) | ||||
3126 | return SDValue(); | ||||
3127 | } | ||||
3128 | |||||
3129 | // Make sure the parts are all coming from the same node. | ||||
3130 | if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3]) | ||||
3131 | return SDValue(); | ||||
3132 | |||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3133 | SDValue BSwap = DAG.getNode(ISD::BSWAP, SDLoc(N), VT, |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 3134 | SDValue(Parts[0],0)); |
3135 | |||||
Kay Tiong Khoo | 670711e | 2013-09-23 18:43:51 +0000 | [diff] [blame] | 3136 | // Result of the bswap should be rotated by 16. If it's not legal, then |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 3137 | // do (x << 16) | (x >> 16). |
3138 | SDValue ShAmt = DAG.getConstant(16, getShiftAmountTy(VT)); | ||||
3139 | if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT)) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3140 | return DAG.getNode(ISD::ROTL, SDLoc(N), VT, BSwap, ShAmt); |
Craig Topper | 0eb5dad | 2012-09-29 07:18:53 +0000 | [diff] [blame] | 3141 | if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3142 | return DAG.getNode(ISD::ROTR, SDLoc(N), VT, BSwap, ShAmt); |
3143 | return DAG.getNode(ISD::OR, SDLoc(N), VT, | ||||
3144 | DAG.getNode(ISD::SHL, SDLoc(N), VT, BSwap, ShAmt), | ||||
3145 | DAG.getNode(ISD::SRL, SDLoc(N), VT, BSwap, ShAmt)); | ||||
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 3146 | } |
3147 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3148 | SDValue DAGCombiner::visitOR(SDNode *N) { |
3149 | SDValue N0 = N->getOperand(0); | ||||
3150 | SDValue N1 = N->getOperand(1); | ||||
3151 | SDValue LL, LR, RL, RR, CC0, CC1; | ||||
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3152 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
3153 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3154 | EVT VT = N1.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3155 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3156 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3157 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3158 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3159 | if (FoldedVOp.getNode()) return FoldedVOp; |
Craig Topper | 9472b4f | 2012-12-08 22:49:19 +0000 | [diff] [blame] | 3160 | |
3161 | // fold (or x, 0) -> x, vector edition | ||||
3162 | if (ISD::isBuildVectorAllZeros(N0.getNode())) | ||||
3163 | return N1; | ||||
3164 | if (ISD::isBuildVectorAllZeros(N1.getNode())) | ||||
3165 | return N0; | ||||
3166 | |||||
3167 | // fold (or x, -1) -> -1, vector edition | ||||
3168 | if (ISD::isBuildVectorAllOnes(N0.getNode())) | ||||
3169 | return N0; | ||||
3170 | if (ISD::isBuildVectorAllOnes(N1.getNode())) | ||||
3171 | return N1; | ||||
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 3172 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3173 | |
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 3174 | // fold (or x, undef) -> -1 |
Bob Wilson | 8674949 | 2010-06-28 23:40:25 +0000 | [diff] [blame] | 3175 | if (!LegalOperations && |
3176 | (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)) { | ||||
Nate Begeman | 93e0ed3 | 2009-12-03 07:11:29 +0000 | [diff] [blame] | 3177 | EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT; |
3178 | return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT); | ||||
3179 | } | ||||
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3180 | // fold (or c1, c2) -> c1|c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3181 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 3182 | return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 3183 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 3184 | if (N0C && !N1C) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3185 | return DAG.getNode(ISD::OR, SDLoc(N), VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3186 | // fold (or x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3187 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3188 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3189 | // fold (or x, -1) -> -1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3190 | if (N1C && N1C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3191 | return N1; |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3192 | // fold (or x, c) -> c iff (x & ~c) == 0 |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 3193 | if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue())) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3194 | return N1; |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 3195 | |
3196 | // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16) | ||||
3197 | SDValue BSwap = MatchBSwapHWord(N, N0, N1); | ||||
3198 | if (BSwap.getNode() != 0) | ||||
3199 | return BSwap; | ||||
3200 | BSwap = MatchBSwapHWordLow(N, N0, N1); | ||||
3201 | if (BSwap.getNode() != 0) | ||||
3202 | return BSwap; | ||||
3203 | |||||
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 3204 | // reassociate or |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3205 | SDValue ROR = ReassociateOps(ISD::OR, SDLoc(N), N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3206 | if (ROR.getNode() != 0) |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 3207 | return ROR; |
3208 | // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2) | ||||
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3209 | // iff (c1 & c2) == 0. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3210 | if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() && |
Chris Lattner | 731d348 | 2005-10-27 05:06:38 +0000 | [diff] [blame] | 3211 | isa<ConstantSDNode>(N0.getOperand(1))) { |
Chris Lattner | 731d348 | 2005-10-27 05:06:38 +0000 | [diff] [blame] | 3212 | ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1)); |
Bill Wendling | 32f9eb2 | 2010-03-03 01:58:01 +0000 | [diff] [blame] | 3213 | if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3214 | return DAG.getNode(ISD::AND, SDLoc(N), VT, |
3215 | DAG.getNode(ISD::OR, SDLoc(N0), VT, | ||||
Bill Wendling | 7d9f2b9 | 2010-03-03 00:35:56 +0000 | [diff] [blame] | 3216 | N0.getOperand(0), N1), |
3217 | DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1)); | ||||
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 3218 | } |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3219 | // fold (or (setcc x), (setcc y)) -> (setcc (or x, y)) |
3220 | if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){ | ||||
3221 | ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get(); | ||||
3222 | ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get(); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3223 | |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3224 | if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3225 | LL.getValueType().isInteger()) { |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3226 | // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0) |
3227 | // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0) | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3228 | if (cast<ConstantSDNode>(LR)->isNullValue() && |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3229 | (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3230 | SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(LR), |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3231 | LR.getValueType(), LL, RL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3232 | AddToWorkList(ORNode.getNode()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3233 | return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3234 | } |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3235 | // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1) |
3236 | // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1) | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3237 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3238 | (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3239 | SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(LR), |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3240 | LR.getValueType(), LL, RL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3241 | AddToWorkList(ANDNode.getNode()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3242 | return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3243 | } |
3244 | } | ||||
3245 | // canonicalize equivalent to ll == rl | ||||
3246 | if (LL == RR && LR == RL) { | ||||
3247 | Op1 = ISD::getSetCCSwappedOperands(Op1); | ||||
3248 | std::swap(RL, RR); | ||||
3249 | } | ||||
3250 | if (LL == RL && LR == RR) { | ||||
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3251 | bool isInteger = LL.getValueType().isInteger(); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3252 | ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger); |
Chris Lattner | 6e1c623 | 2008-10-28 07:11:07 +0000 | [diff] [blame] | 3253 | if (Result != ISD::SETCC_INVALID && |
Patrik Hagglund | fdbeb05 | 2012-12-19 10:19:55 +0000 | [diff] [blame] | 3254 | (!LegalOperations || |
Owen Anderson | 39125d9 | 2013-02-14 09:07:33 +0000 | [diff] [blame] | 3255 | (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) && |
3256 | TLI.isOperationLegal(ISD::SETCC, | ||||
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 3257 | getSetCCResultType(N0.getValueType()))))) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3258 | return DAG.getSetCC(SDLoc(N), N0.getValueType(), |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3259 | LL, LR, Result); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3260 | } |
3261 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3262 | |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3263 | // Simplify: (or (op x...), (op y...)) -> (op (or x, y)) |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 3264 | if (N0.getOpcode() == N1.getOpcode()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3265 | SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3266 | if (Tmp.getNode()) return Tmp; |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3267 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3268 | |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3269 | // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible. |
Chris Lattner | 1ec7273 | 2006-09-14 21:11:37 +0000 | [diff] [blame] | 3270 | if (N0.getOpcode() == ISD::AND && |
3271 | N1.getOpcode() == ISD::AND && | ||||
3272 | N0.getOperand(1).getOpcode() == ISD::Constant && | ||||
3273 | N1.getOperand(1).getOpcode() == ISD::Constant && | ||||
3274 | // Don't increase # computations. | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3275 | (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) { |
Chris Lattner | 1ec7273 | 2006-09-14 21:11:37 +0000 | [diff] [blame] | 3276 | // We can only do this xform if we know that bits from X that are set in C2 |
3277 | // but not in C1 are already zero. Likewise for Y. | ||||
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 3278 | const APInt &LHSMask = |
3279 | cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); | ||||
3280 | const APInt &RHSMask = | ||||
3281 | cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue(); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3282 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 3283 | if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) && |
3284 | DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) { | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3285 | SDValue X = DAG.getNode(ISD::OR, SDLoc(N0), VT, |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3286 | N0.getOperand(0), N1.getOperand(0)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3287 | return DAG.getNode(ISD::AND, SDLoc(N), VT, X, |
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3288 | DAG.getConstant(LHSMask | RHSMask, VT)); |
Chris Lattner | 1ec7273 | 2006-09-14 21:11:37 +0000 | [diff] [blame] | 3289 | } |
3290 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3291 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3292 | // See if this is some rotate idiom. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3293 | if (SDNode *Rot = MatchRotate(N0, N1, SDLoc(N))) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3294 | return SDValue(Rot, 0); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 3295 | |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 3296 | // Simplify the operands using demanded-bits information. |
3297 | if (!VT.isVector() && | ||||
3298 | SimplifyDemandedBits(SDValue(N, 0))) | ||||
3299 | return SDValue(N, 0); | ||||
3300 | |||||
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 3301 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3302 | } |
3303 | |||||
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3304 | /// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3305 | static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) { |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3306 | if (Op.getOpcode() == ISD::AND) { |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 3307 | if (isa<ConstantSDNode>(Op.getOperand(1))) { |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3308 | Mask = Op.getOperand(1); |
3309 | Op = Op.getOperand(0); | ||||
3310 | } else { | ||||
3311 | return false; | ||||
3312 | } | ||||
3313 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3314 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3315 | if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) { |
3316 | Shift = Op; | ||||
3317 | return true; | ||||
3318 | } | ||||
Bill Wendling | 0902564 | 2009-01-30 20:59:34 +0000 | [diff] [blame] | 3319 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3320 | return false; |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3321 | } |
3322 | |||||
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3323 | // MatchRotate - Handle an 'or' of two operands. If this is one of the many |
3324 | // idioms for rotate, and if the target supports rotation instructions, generate | ||||
3325 | // a rot[lr]. | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3326 | SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL) { |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 3327 | // Must be a legal type. Expanded 'n promoted things won't work with rotates. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3328 | EVT VT = LHS.getValueType(); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3329 | if (!TLI.isTypeLegal(VT)) return 0; |
3330 | |||||
3331 | // The target must have at least one rotate flavor. | ||||
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 3332 | bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT); |
3333 | bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT); | ||||
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3334 | if (!HasROTL && !HasROTR) return 0; |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 3335 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3336 | // Match "(X shl/srl V1) & V2" where V2 may not be present. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3337 | SDValue LHSShift; // The shift. |
3338 | SDValue LHSMask; // AND value if any. | ||||
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3339 | if (!MatchRotateHalf(LHS, LHSShift, LHSMask)) |
3340 | return 0; // Not part of a rotate. | ||||
3341 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3342 | SDValue RHSShift; // The shift. |
3343 | SDValue RHSMask; // AND value if any. | ||||
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3344 | if (!MatchRotateHalf(RHS, RHSShift, RHSMask)) |
3345 | return 0; // Not part of a rotate. | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3346 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3347 | if (LHSShift.getOperand(0) != RHSShift.getOperand(0)) |
3348 | return 0; // Not shifting the same value. | ||||
3349 | |||||
3350 | if (LHSShift.getOpcode() == RHSShift.getOpcode()) | ||||
3351 | return 0; // Shifts must disagree. | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3352 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3353 | // Canonicalize shl to left side in a shl/srl pair. |
3354 | if (RHSShift.getOpcode() == ISD::SHL) { | ||||
3355 | std::swap(LHS, RHS); | ||||
3356 | std::swap(LHSShift, RHSShift); | ||||
3357 | std::swap(LHSMask , RHSMask ); | ||||
3358 | } | ||||
3359 | |||||
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3360 | unsigned OpSizeInBits = VT.getSizeInBits(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3361 | SDValue LHSShiftArg = LHSShift.getOperand(0); |
3362 | SDValue LHSShiftAmt = LHSShift.getOperand(1); | ||||
Kai Nacke | ceb3b46 | 2013-09-19 23:00:28 +0000 | [diff] [blame] | 3363 | SDValue RHSShiftArg = RHSShift.getOperand(0); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3364 | SDValue RHSShiftAmt = RHSShift.getOperand(1); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3365 | |
3366 | // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1) | ||||
3367 | // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2) | ||||
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3368 | if (LHSShiftAmt.getOpcode() == ISD::Constant && |
3369 | RHSShiftAmt.getOpcode() == ISD::Constant) { | ||||
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3370 | uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue(); |
3371 | uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue(); | ||||
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3372 | if ((LShVal + RShVal) != OpSizeInBits) |
3373 | return 0; | ||||
3374 | |||||
Craig Topper | 32b7343 | 2012-09-29 06:54:22 +0000 | [diff] [blame] | 3375 | SDValue Rot = DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT, |
3376 | LHSShiftArg, HasROTL ? LHSShiftAmt : RHSShiftAmt); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3377 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3378 | // If there is an AND of either shifted operand, apply it to the result. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3379 | if (LHSMask.getNode() || RHSMask.getNode()) { |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 3380 | APInt Mask = APInt::getAllOnesValue(OpSizeInBits); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3381 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3382 | if (LHSMask.getNode()) { |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 3383 | APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal); |
3384 | Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits; | ||||
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3385 | } |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3386 | if (RHSMask.getNode()) { |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 3387 | APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal); |
3388 | Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits; | ||||
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3389 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3390 | |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3391 | Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT)); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3392 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3393 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3394 | return Rot.getNode(); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3395 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3396 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3397 | // If there is a mask here, and we have a variable shift, we can't be sure |
3398 | // that we're masking out the right stuff. | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3399 | if (LHSMask.getNode() || RHSMask.getNode()) |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3400 | return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3401 | |
Benjamin Kramer | cd216b2 | 2013-09-24 14:21:28 +0000 | [diff] [blame] | 3402 | // If the shift amount is sign/zext/any-extended just peel it off. |
3403 | SDValue LExtOp0 = LHSShiftAmt; | ||||
3404 | SDValue RExtOp0 = RHSShiftAmt; | ||||
Craig Topper | 0eb5dad | 2012-09-29 07:18:53 +0000 | [diff] [blame] | 3405 | if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND || |
3406 | LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND || | ||||
3407 | LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND || | ||||
3408 | LHSShiftAmt.getOpcode() == ISD::TRUNCATE) && | ||||
3409 | (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND || | ||||
3410 | RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND || | ||||
3411 | RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND || | ||||
3412 | RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) { | ||||
Benjamin Kramer | cd216b2 | 2013-09-24 14:21:28 +0000 | [diff] [blame] | 3413 | LExtOp0 = LHSShiftAmt.getOperand(0); |
3414 | RExtOp0 = RHSShiftAmt.getOperand(0); | ||||
3415 | } | ||||
3416 | |||||
3417 | if (RExtOp0.getOpcode() == ISD::SUB && RExtOp0.getOperand(1) == LExtOp0) { | ||||
3418 | // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) -> | ||||
3419 | // (rotl x, y) | ||||
3420 | // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) -> | ||||
3421 | // (rotr x, (sub 32, y)) | ||||
3422 | if (ConstantSDNode *SUBC = | ||||
3423 | dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) { | ||||
3424 | if (SUBC->getAPIntValue() == OpSizeInBits) { | ||||
3425 | return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT, LHSShiftArg, | ||||
3426 | HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode(); | ||||
3427 | } else if (LHSShiftArg.getOpcode() == ISD::ZERO_EXTEND || | ||||
Kai Nacke | ceb3b46 | 2013-09-19 23:00:28 +0000 | [diff] [blame] | 3428 | LHSShiftArg.getOpcode() == ISD::ANY_EXTEND) { |
Benjamin Kramer | cd216b2 | 2013-09-24 14:21:28 +0000 | [diff] [blame] | 3429 | // fold (or (shl (*ext x), (*ext y)), |
3430 | // (srl (*ext x), (*ext (sub 32, y)))) -> | ||||
3431 | // (*ext (rotl x, y)) | ||||
3432 | // fold (or (shl (*ext x), (*ext y)), | ||||
3433 | // (srl (*ext x), (*ext (sub 32, y)))) -> | ||||
3434 | // (*ext (rotr x, (sub 32, y))) | ||||
3435 | SDValue LArgExtOp0 = LHSShiftArg.getOperand(0); | ||||
3436 | EVT LArgVT = LArgExtOp0.getValueType(); | ||||
Jin-Gu Kang | b70a05a | 2013-10-03 15:58:48 +0000 | [diff] [blame] | 3437 | bool HasROTRWithLArg = TLI.isOperationLegalOrCustom(ISD::ROTR, LArgVT); |
3438 | bool HasROTLWithLArg = TLI.isOperationLegalOrCustom(ISD::ROTL, LArgVT); | ||||
3439 | if (HasROTRWithLArg || HasROTLWithLArg) { | ||||
3440 | if (LArgVT.getSizeInBits() == SUBC->getAPIntValue()) { | ||||
3441 | SDValue V = | ||||
3442 | DAG.getNode(HasROTLWithLArg ? ISD::ROTL : ISD::ROTR, DL, LArgVT, | ||||
3443 | LArgExtOp0, HasROTL ? LHSShiftAmt : RHSShiftAmt); | ||||
3444 | return DAG.getNode(LHSShiftArg.getOpcode(), DL, VT, V).getNode(); | ||||
3445 | } | ||||
3446 | } | ||||
David Blaikie | c228672 | 2013-09-20 00:33:11 +0000 | [diff] [blame] | 3447 | } |
Benjamin Kramer | cd216b2 | 2013-09-24 14:21:28 +0000 | [diff] [blame] | 3448 | } |
3449 | } else if (LExtOp0.getOpcode() == ISD::SUB && | ||||
3450 | RExtOp0 == LExtOp0.getOperand(1)) { | ||||
3451 | // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) -> | ||||
3452 | // (rotr x, y) | ||||
3453 | // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) -> | ||||
3454 | // (rotl x, (sub 32, y)) | ||||
3455 | if (ConstantSDNode *SUBC = | ||||
3456 | dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) { | ||||
3457 | if (SUBC->getAPIntValue() == OpSizeInBits) { | ||||
3458 | return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT, LHSShiftArg, | ||||
3459 | HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode(); | ||||
3460 | } else if (RHSShiftArg.getOpcode() == ISD::ZERO_EXTEND || | ||||
Kai Nacke | ceb3b46 | 2013-09-19 23:00:28 +0000 | [diff] [blame] | 3461 | RHSShiftArg.getOpcode() == ISD::ANY_EXTEND) { |
Benjamin Kramer | cd216b2 | 2013-09-24 14:21:28 +0000 | [diff] [blame] | 3462 | // fold (or (shl (*ext x), (*ext (sub 32, y))), |
3463 | // (srl (*ext x), (*ext y))) -> | ||||
3464 | // (*ext (rotl x, y)) | ||||
3465 | // fold (or (shl (*ext x), (*ext (sub 32, y))), | ||||
3466 | // (srl (*ext x), (*ext y))) -> | ||||
3467 | // (*ext (rotr x, (sub 32, y))) | ||||
3468 | SDValue RArgExtOp0 = RHSShiftArg.getOperand(0); | ||||
3469 | EVT RArgVT = RArgExtOp0.getValueType(); | ||||
Jin-Gu Kang | b70a05a | 2013-10-03 15:58:48 +0000 | [diff] [blame] | 3470 | bool HasROTRWithRArg = TLI.isOperationLegalOrCustom(ISD::ROTR, RArgVT); |
3471 | bool HasROTLWithRArg = TLI.isOperationLegalOrCustom(ISD::ROTL, RArgVT); | ||||
3472 | if (HasROTRWithRArg || HasROTLWithRArg) { | ||||
3473 | if (RArgVT.getSizeInBits() == SUBC->getAPIntValue()) { | ||||
3474 | SDValue V = | ||||
3475 | DAG.getNode(HasROTRWithRArg ? ISD::ROTR : ISD::ROTL, DL, RArgVT, | ||||
3476 | RArgExtOp0, HasROTR ? RHSShiftAmt : LHSShiftAmt); | ||||
3477 | return DAG.getNode(RHSShiftArg.getOpcode(), DL, VT, V).getNode(); | ||||
3478 | } | ||||
Kai Nacke | ceb3b46 | 2013-09-19 23:00:28 +0000 | [diff] [blame] | 3479 | } |
David Blaikie | c228672 | 2013-09-20 00:33:11 +0000 | [diff] [blame] | 3480 | } |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3481 | } |
3482 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3483 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3484 | return 0; |
3485 | } | ||||
3486 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3487 | SDValue DAGCombiner::visitXOR(SDNode *N) { |
3488 | SDValue N0 = N->getOperand(0); | ||||
3489 | SDValue N1 = N->getOperand(1); | ||||
3490 | SDValue LHS, RHS, CC; | ||||
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3491 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
3492 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3493 | EVT VT = N0.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3494 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3495 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3496 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3497 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3498 | if (FoldedVOp.getNode()) return FoldedVOp; |
Craig Topper | 9472b4f | 2012-12-08 22:49:19 +0000 | [diff] [blame] | 3499 | |
3500 | // fold (xor x, 0) -> x, vector edition | ||||
3501 | if (ISD::isBuildVectorAllZeros(N0.getNode())) | ||||
3502 | return N1; | ||||
3503 | if (ISD::isBuildVectorAllZeros(N1.getNode())) | ||||
3504 | return N0; | ||||
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 3505 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3506 | |
Evan Cheng | 26471c4 | 2008-03-25 20:08:07 +0000 | [diff] [blame] | 3507 | // fold (xor undef, undef) -> 0. This is a common idiom (misuse). |
3508 | if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF) | ||||
3509 | return DAG.getConstant(0, VT); | ||||
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 3510 | // fold (xor x, undef) -> undef |
Dan Gohman | 70fb1ae | 2007-07-10 15:19:29 +0000 | [diff] [blame] | 3511 | if (N0.getOpcode() == ISD::UNDEF) |
3512 | return N0; | ||||
3513 | if (N1.getOpcode() == ISD::UNDEF) | ||||
Dan Gohman | 613e0d8 | 2007-07-03 14:03:57 +0000 | [diff] [blame] | 3514 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3515 | // fold (xor c1, c2) -> c1^c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3516 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 3517 | return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 3518 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 3519 | if (N0C && !N1C) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3520 | return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3521 | // fold (xor x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3522 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3523 | return N0; |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 3524 | // reassociate xor |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3525 | SDValue RXOR = ReassociateOps(ISD::XOR, SDLoc(N), N0, N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3526 | if (RXOR.getNode() != 0) |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 3527 | return RXOR; |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3528 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3529 | // fold !(x cc y) -> (x !cc y) |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3530 | if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3531 | bool isInt = LHS.getValueType().isInteger(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3532 | ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(), |
3533 | isInt); | ||||
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3534 | |
Patrik Hagglund | fdbeb05 | 2012-12-19 10:19:55 +0000 | [diff] [blame] | 3535 | if (!LegalOperations || |
3536 | TLI.isCondCodeLegal(NotCC, LHS.getSimpleValueType())) { | ||||
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3537 | switch (N0.getOpcode()) { |
3538 | default: | ||||
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3539 | llvm_unreachable("Unhandled SetCC Equivalent!"); |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3540 | case ISD::SETCC: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3541 | return DAG.getSetCC(SDLoc(N), VT, LHS, RHS, NotCC); |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3542 | case ISD::SELECT_CC: |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3543 | return DAG.getSelectCC(SDLoc(N), LHS, RHS, N0.getOperand(2), |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3544 | N0.getOperand(3), NotCC); |
3545 | } | ||||
3546 | } | ||||
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3547 | } |
Bill Wendling | ae89bb1 | 2008-11-11 08:25:46 +0000 | [diff] [blame] | 3548 | |
Chris Lattner | 61c5ff4 | 2007-09-10 21:39:07 +0000 | [diff] [blame] | 3549 | // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y))) |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3550 | if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND && |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 3551 | N0.getNode()->hasOneUse() && |
3552 | isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){ | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3553 | SDValue V = N0.getOperand(0); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3554 | V = DAG.getNode(ISD::XOR, SDLoc(N0), V.getValueType(), V, |
Duncan Sands | 272dce0 | 2007-10-10 09:54:50 +0000 | [diff] [blame] | 3555 | DAG.getConstant(1, V.getValueType())); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3556 | AddToWorkList(V.getNode()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3557 | return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, V); |
Chris Lattner | 61c5ff4 | 2007-09-10 21:39:07 +0000 | [diff] [blame] | 3558 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3559 | |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3560 | // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3561 | if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 && |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 3562 | (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3563 | SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 3564 | if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) { |
3565 | unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND; | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3566 | LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS |
3567 | RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3568 | AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3569 | return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3570 | } |
3571 | } | ||||
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3572 | // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3573 | if (N1C && N1C->isAllOnesValue() && |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 3574 | (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3575 | SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 3576 | if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) { |
3577 | unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND; | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3578 | LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS |
3579 | RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3580 | AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3581 | return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3582 | } |
3583 | } | ||||
David Majnemer | 363160a | 2013-05-08 06:44:42 +0000 | [diff] [blame] | 3584 | // fold (xor (and x, y), y) -> (and (not x), y) |
3585 | if (N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() && | ||||
Benjamin Kramer | 72a3ee7 | 2013-10-16 14:16:19 +0000 | [diff] [blame] | 3586 | N0->getOperand(1) == N1 && isTypeLegal(VT.getScalarType())) { |
David Majnemer | 363160a | 2013-05-08 06:44:42 +0000 | [diff] [blame] | 3587 | SDValue X = N0->getOperand(0); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3588 | SDValue NotX = DAG.getNOT(SDLoc(X), X, VT); |
David Majnemer | 363160a | 2013-05-08 06:44:42 +0000 | [diff] [blame] | 3589 | AddToWorkList(NotX.getNode()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3590 | return DAG.getNode(ISD::AND, SDLoc(N), VT, NotX, N1); |
David Majnemer | 363160a | 2013-05-08 06:44:42 +0000 | [diff] [blame] | 3591 | } |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3592 | // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2)) |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 3593 | if (N1C && N0.getOpcode() == ISD::XOR) { |
3594 | ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0)); | ||||
3595 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); | ||||
3596 | if (N00C) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3597 | return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(1), |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3598 | DAG.getConstant(N1C->getAPIntValue() ^ |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3599 | N00C->getAPIntValue(), VT)); |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 3600 | if (N01C) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3601 | return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(0), |
Bill Wendling | 317bd70 | 2009-01-30 21:14:50 +0000 | [diff] [blame] | 3602 | DAG.getConstant(N1C->getAPIntValue() ^ |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 3603 | N01C->getAPIntValue(), VT)); |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 3604 | } |
3605 | // fold (xor x, x) -> 0 | ||||
Eric Christopher | 7bccf6a | 2011-02-16 04:50:12 +0000 | [diff] [blame] | 3606 | if (N0 == N1) |
Hal Finkel | bd6f1f6 | 2013-07-09 17:02:45 +0000 | [diff] [blame] | 3607 | return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3608 | |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 3609 | // Simplify: xor (op x...), (op y...) -> (op (xor x, y)) |
3610 | if (N0.getOpcode() == N1.getOpcode()) { | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3611 | SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3612 | if (Tmp.getNode()) return Tmp; |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 3613 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3614 | |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 3615 | // Simplify the expression using non-local knowledge. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3616 | if (!VT.isVector() && |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3617 | SimplifyDemandedBits(SDValue(N, 0))) |
3618 | return SDValue(N, 0); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3619 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 3620 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3621 | } |
3622 | |||||
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3623 | /// visitShiftByConstant - Handle transforms common to the three shifts, when |
3624 | /// the shift amount is a constant. | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3625 | SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3626 | SDNode *LHS = N->getOperand(0).getNode(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3627 | if (!LHS->hasOneUse()) return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3628 | |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3629 | // We want to pull some binops through shifts, so that we have (and (shift)) |
3630 | // instead of (shift (and)), likewise for add, or, xor, etc. This sort of | ||||
3631 | // thing happens with address calculations, so it's important to canonicalize | ||||
3632 | // it. | ||||
3633 | bool HighBitSet = false; // Can we transform this if the high bit is set? | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3634 | |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3635 | switch (LHS->getOpcode()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3636 | default: return SDValue(); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3637 | case ISD::OR: |
3638 | case ISD::XOR: | ||||
3639 | HighBitSet = false; // We can only transform sra if the high bit is clear. | ||||
3640 | break; | ||||
3641 | case ISD::AND: | ||||
3642 | HighBitSet = true; // We can only transform sra if the high bit is set. | ||||
3643 | break; | ||||
3644 | case ISD::ADD: | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3645 | if (N->getOpcode() != ISD::SHL) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3646 | return SDValue(); // only shl(add) not sr[al](add). |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3647 | HighBitSet = false; // We can only transform sra if the high bit is clear. |
3648 | break; | ||||
3649 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3650 | |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3651 | // We require the RHS of the binop to be a constant as well. |
3652 | ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1)); | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3653 | if (!BinOpCst) return SDValue(); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3654 | |
3655 | // FIXME: disable this unless the input to the binop is a shift by a constant. | ||||
3656 | // If it is not a shift, it pessimizes some common cases like: | ||||
Chris Lattner | d3fd6d2 | 2007-12-06 07:47:55 +0000 | [diff] [blame] | 3657 | // |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3658 | // void foo(int *X, int i) { X[i & 1235] = 1; } |
3659 | // int bar(int *X, int i) { return X[i & 255]; } | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3660 | SDNode *BinOpLHSVal = LHS->getOperand(0).getNode(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3661 | if ((BinOpLHSVal->getOpcode() != ISD::SHL && |
Chris Lattner | d3fd6d2 | 2007-12-06 07:47:55 +0000 | [diff] [blame] | 3662 | BinOpLHSVal->getOpcode() != ISD::SRA && |
3663 | BinOpLHSVal->getOpcode() != ISD::SRL) || | ||||
3664 | !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1))) | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3665 | return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3666 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3667 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3668 | |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3669 | // If this is a signed shift right, and the high bit is modified by the |
3670 | // logical operation, do not perform the transformation. The highBitSet | ||||
3671 | // boolean indicates the value of the high bit of the constant which would | ||||
3672 | // cause it to be modified for this operation. | ||||
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3673 | if (N->getOpcode() == ISD::SRA) { |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 3674 | bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative(); |
3675 | if (BinOpRHSSignSet != HighBitSet) | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3676 | return SDValue(); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3677 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3678 | |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3679 | // Fold the constants, shifting the binop RHS by the shift amount. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3680 | SDValue NewRHS = DAG.getNode(N->getOpcode(), SDLoc(LHS->getOperand(1)), |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3681 | N->getValueType(0), |
3682 | LHS->getOperand(1), N->getOperand(1)); | ||||
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3683 | |
3684 | // Create the new shift. | ||||
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 3685 | SDValue NewShift = DAG.getNode(N->getOpcode(), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3686 | SDLoc(LHS->getOperand(0)), |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3687 | VT, LHS->getOperand(0), N->getOperand(1)); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3688 | |
3689 | // Create the new binop. | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3690 | return DAG.getNode(LHS->getOpcode(), SDLoc(N), VT, NewShift, NewRHS); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3691 | } |
3692 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3693 | SDValue DAGCombiner::visitSHL(SDNode *N) { |
3694 | SDValue N0 = N->getOperand(0); | ||||
3695 | SDValue N1 = N->getOperand(1); | ||||
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3696 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
3697 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3698 | EVT VT = N0.getValueType(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3699 | unsigned OpSizeInBits = VT.getScalarType().getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3700 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3701 | // fold (shl c1, c2) -> c1<<c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3702 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 3703 | return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3704 | // fold (shl 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3705 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3706 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3707 | // fold (shl x, c >= size(x)) -> undef |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3708 | if (N1C && N1C->getZExtValue() >= OpSizeInBits) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3709 | return DAG.getUNDEF(VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3710 | // fold (shl x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3711 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3712 | return N0; |
Chad Rosier | 92bcd96 | 2011-06-14 22:29:10 +0000 | [diff] [blame] | 3713 | // fold (shl undef, x) -> 0 |
3714 | if (N0.getOpcode() == ISD::UNDEF) | ||||
3715 | return DAG.getConstant(0, VT); | ||||
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3716 | // if (shl x, c) is known to be zero, return 0 |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3717 | if (DAG.MaskedValueIsZero(SDValue(N, 0), |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3718 | APInt::getAllOnesValue(OpSizeInBits))) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3719 | return DAG.getConstant(0, VT); |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3720 | // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))). |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3721 | if (N1.getOpcode() == ISD::TRUNCATE && |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3722 | N1.getOperand(0).getOpcode() == ISD::AND && |
3723 | N1.hasOneUse() && N1.getOperand(0).hasOneUse()) { | ||||
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3724 | SDValue N101 = N1.getOperand(0).getOperand(1); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3725 | if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3726 | EVT TruncVT = N1.getValueType(); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3727 | SDValue N100 = N1.getOperand(0).getOperand(0); |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3728 | APInt TruncC = N101C->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 3729 | TruncC = TruncC.trunc(TruncVT.getSizeInBits()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3730 | return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0, |
3731 | DAG.getNode(ISD::AND, SDLoc(N), TruncVT, | ||||
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 3732 | DAG.getNode(ISD::TRUNCATE, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3733 | SDLoc(N), |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 3734 | TruncVT, N100), |
Dan Gohman | ce9bc12 | 2009-01-27 20:39:34 +0000 | [diff] [blame] | 3735 | DAG.getConstant(TruncC, TruncVT))); |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3736 | } |
3737 | } | ||||
3738 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3739 | if (N1C && SimplifyDemandedBits(SDValue(N, 0))) |
3740 | return SDValue(N, 0); | ||||
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3741 | |
3742 | // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2)) | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3743 | if (N1C && N0.getOpcode() == ISD::SHL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3744 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3745 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue(); |
3746 | uint64_t c2 = N1C->getZExtValue(); | ||||
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 3747 | if (c1 + c2 >= OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3748 | return DAG.getConstant(0, VT); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3749 | return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3750 | DAG.getConstant(c1 + c2, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3751 | } |
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 3752 | |
3753 | // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2))) | ||||
3754 | // For this to be valid, the second form must not preserve any of the bits | ||||
3755 | // that are shifted out by the inner shift in the first form. This means | ||||
3756 | // the outer shift size must be >= the number of bits added by the ext. | ||||
3757 | // As a corollary, we don't care what kind of ext it is. | ||||
3758 | if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND || | ||||
3759 | N0.getOpcode() == ISD::ANY_EXTEND || | ||||
3760 | N0.getOpcode() == ISD::SIGN_EXTEND) && | ||||
3761 | N0.getOperand(0).getOpcode() == ISD::SHL && | ||||
3762 | isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) { | ||||
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 3763 | uint64_t c1 = |
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 3764 | cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue(); |
3765 | uint64_t c2 = N1C->getZExtValue(); | ||||
3766 | EVT InnerShiftVT = N0.getOperand(0).getValueType(); | ||||
3767 | uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits(); | ||||
3768 | if (c2 >= OpSizeInBits - InnerShiftSize) { | ||||
3769 | if (c1 + c2 >= OpSizeInBits) | ||||
3770 | return DAG.getConstant(0, VT); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3771 | return DAG.getNode(ISD::SHL, SDLoc(N0), VT, |
3772 | DAG.getNode(N0.getOpcode(), SDLoc(N0), VT, | ||||
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 3773 | N0.getOperand(0)->getOperand(0)), |
3774 | DAG.getConstant(c1 + c2, N1.getValueType())); | ||||
3775 | } | ||||
3776 | } | ||||
3777 | |||||
Andrea Di Biagio | a9f113d | 2013-09-27 11:37:05 +0000 | [diff] [blame] | 3778 | // fold (shl (zext (srl x, C)), C) -> (zext (shl (srl x, C), C)) |
3779 | // Only fold this if the inner zext has no other uses to avoid increasing | ||||
3780 | // the total number of instructions. | ||||
3781 | if (N1C && N0.getOpcode() == ISD::ZERO_EXTEND && N0.hasOneUse() && | ||||
3782 | N0.getOperand(0).getOpcode() == ISD::SRL && | ||||
3783 | isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) { | ||||
3784 | uint64_t c1 = | ||||
3785 | cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue(); | ||||
3786 | if (c1 < VT.getSizeInBits()) { | ||||
3787 | uint64_t c2 = N1C->getZExtValue(); | ||||
3788 | if (c1 == c2) { | ||||
3789 | SDValue NewOp0 = N0.getOperand(0); | ||||
3790 | EVT CountVT = NewOp0.getOperand(1).getValueType(); | ||||
3791 | SDValue NewSHL = DAG.getNode(ISD::SHL, SDLoc(N), NewOp0.getValueType(), | ||||
3792 | NewOp0, DAG.getConstant(c2, CountVT)); | ||||
3793 | return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N0), VT, NewSHL); | ||||
3794 | } | ||||
3795 | } | ||||
3796 | } | ||||
3797 | |||||
Eli Friedman | 2a6d9eb | 2011-06-09 22:14:44 +0000 | [diff] [blame] | 3798 | // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or |
3799 | // (and (srl x, (sub c1, c2), MASK) | ||||
Chandler Carruth | 62dfc51 | 2012-01-05 11:05:55 +0000 | [diff] [blame] | 3800 | // Only fold this if the inner shift has no other uses -- if it does, folding |
3801 | // this will increase the total number of instructions. | ||||
3802 | if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse() && | ||||
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3803 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3804 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue(); |
Evan Cheng | d101a72 | 2009-07-21 05:40:15 +0000 | [diff] [blame] | 3805 | if (c1 < VT.getSizeInBits()) { |
3806 | uint64_t c2 = N1C->getZExtValue(); | ||||
Eli Friedman | 2a6d9eb | 2011-06-09 22:14:44 +0000 | [diff] [blame] | 3807 | APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(), |
3808 | VT.getSizeInBits() - c1); | ||||
3809 | SDValue Shift; | ||||
3810 | if (c2 > c1) { | ||||
3811 | Mask = Mask.shl(c2-c1); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3812 | Shift = DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0), |
Eli Friedman | 2a6d9eb | 2011-06-09 22:14:44 +0000 | [diff] [blame] | 3813 | DAG.getConstant(c2-c1, N1.getValueType())); |
3814 | } else { | ||||
3815 | Mask = Mask.lshr(c1-c2); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3816 | Shift = DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0), |
Eli Friedman | 2a6d9eb | 2011-06-09 22:14:44 +0000 | [diff] [blame] | 3817 | DAG.getConstant(c1-c2, N1.getValueType())); |
3818 | } | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3819 | return DAG.getNode(ISD::AND, SDLoc(N0), VT, Shift, |
Eli Friedman | 2a6d9eb | 2011-06-09 22:14:44 +0000 | [diff] [blame] | 3820 | DAG.getConstant(Mask, VT)); |
Evan Cheng | d101a72 | 2009-07-21 05:40:15 +0000 | [diff] [blame] | 3821 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3822 | } |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3823 | // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1)) |
Dan Gohman | 5cbd37e | 2009-08-06 09:18:59 +0000 | [diff] [blame] | 3824 | if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) { |
3825 | SDValue HiBitsMask = | ||||
3826 | DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(), | ||||
3827 | VT.getSizeInBits() - | ||||
3828 | N1C->getZExtValue()), | ||||
3829 | VT); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3830 | return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0), |
Dan Gohman | 5cbd37e | 2009-08-06 09:18:59 +0000 | [diff] [blame] | 3831 | HiBitsMask); |
3832 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3833 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 3834 | if (N1C) { |
3835 | SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue()); | ||||
3836 | if (NewSHL.getNode()) | ||||
3837 | return NewSHL; | ||||
3838 | } | ||||
3839 | |||||
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 3840 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3841 | } |
3842 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3843 | SDValue DAGCombiner::visitSRA(SDNode *N) { |
3844 | SDValue N0 = N->getOperand(0); | ||||
3845 | SDValue N1 = N->getOperand(1); | ||||
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3846 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
3847 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3848 | EVT VT = N0.getValueType(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3849 | unsigned OpSizeInBits = VT.getScalarType().getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3850 | |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3851 | // fold (sra c1, c2) -> (sra c1, c2) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3852 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 3853 | return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3854 | // fold (sra 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3855 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3856 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3857 | // fold (sra -1, x) -> -1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3858 | if (N0C && N0C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3859 | return N0; |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3860 | // fold (sra x, (setge c, size(x))) -> undef |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3861 | if (N1C && N1C->getZExtValue() >= OpSizeInBits) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3862 | return DAG.getUNDEF(VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3863 | // fold (sra x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3864 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 3865 | return N0; |
Nate Begeman | fb7217b | 2006-02-17 19:54:08 +0000 | [diff] [blame] | 3866 | // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports |
3867 | // sext_inreg. | ||||
3868 | if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) { | ||||
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3869 | unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue(); |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 3870 | EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits); |
3871 | if (VT.isVector()) | ||||
3872 | ExtVT = EVT::getVectorVT(*DAG.getContext(), | ||||
3873 | ExtVT, VT.getVectorNumElements()); | ||||
3874 | if ((!LegalOperations || | ||||
3875 | TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT))) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3876 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 3877 | N0.getOperand(0), DAG.getValueType(ExtVT)); |
Nate Begeman | fb7217b | 2006-02-17 19:54:08 +0000 | [diff] [blame] | 3878 | } |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 3879 | |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3880 | // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2)) |
Chris Lattner | 71d9ebc | 2006-02-28 06:23:04 +0000 | [diff] [blame] | 3881 | if (N1C && N0.getOpcode() == ISD::SRA) { |
3882 | if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { | ||||
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3883 | unsigned Sum = N1C->getZExtValue() + C1->getZExtValue(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3884 | if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3885 | return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0.getOperand(0), |
Chris Lattner | 71d9ebc | 2006-02-28 06:23:04 +0000 | [diff] [blame] | 3886 | DAG.getConstant(Sum, N1C->getValueType(0))); |
3887 | } | ||||
3888 | } | ||||
Christopher Lamb | 15cbde3 | 2008-03-19 08:30:06 +0000 | [diff] [blame] | 3889 | |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3890 | // fold (sra (shl X, m), (sub result_size, n)) |
3891 | // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3892 | // result_size - n != m. |
3893 | // If truncate is free for the target sext(shl) is likely to result in better | ||||
Christopher Lamb | b9b0428 | 2008-03-20 04:31:39 +0000 | [diff] [blame] | 3894 | // code. |
Christopher Lamb | 15cbde3 | 2008-03-19 08:30:06 +0000 | [diff] [blame] | 3895 | if (N0.getOpcode() == ISD::SHL) { |
3896 | // Get the two constanst of the shifts, CN0 = m, CN = n. | ||||
3897 | const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); | ||||
3898 | if (N01C && N1C) { | ||||
Christopher Lamb | b9b0428 | 2008-03-20 04:31:39 +0000 | [diff] [blame] | 3899 | // Determine what the truncate's result bitsize and type would be. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3900 | EVT TruncVT = |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 3901 | EVT::getIntegerVT(*DAG.getContext(), |
3902 | OpSizeInBits - N1C->getZExtValue()); | ||||
Christopher Lamb | b9b0428 | 2008-03-20 04:31:39 +0000 | [diff] [blame] | 3903 | // Determine the residual right-shift amount. |
Torok Edwin | 6bb4958 | 2009-05-23 17:29:48 +0000 | [diff] [blame] | 3904 | signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue(); |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 3905 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3906 | // If the shift is not a no-op (in which case this should be just a sign |
3907 | // extend already), the truncated to type is legal, sign_extend is legal | ||||
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 3908 | // on that type, and the truncate to that type is both legal and free, |
Christopher Lamb | b9b0428 | 2008-03-20 04:31:39 +0000 | [diff] [blame] | 3909 | // perform the transform. |
Torok Edwin | 6bb4958 | 2009-05-23 17:29:48 +0000 | [diff] [blame] | 3910 | if ((ShiftAmt > 0) && |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 3911 | TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) && |
3912 | TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) && | ||||
Evan Cheng | 260e07e | 2008-03-20 02:18:41 +0000 | [diff] [blame] | 3913 | TLI.isTruncateFree(VT, TruncVT)) { |
Christopher Lamb | b9b0428 | 2008-03-20 04:31:39 +0000 | [diff] [blame] | 3914 | |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 3915 | SDValue Amt = DAG.getConstant(ShiftAmt, |
3916 | getShiftAmountTy(N0.getOperand(0).getValueType())); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3917 | SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0), VT, |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3918 | N0.getOperand(0), Amt); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3919 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), TruncVT, |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3920 | Shift); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3921 | return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3922 | N->getValueType(0), Trunc); |
Christopher Lamb | 15cbde3 | 2008-03-19 08:30:06 +0000 | [diff] [blame] | 3923 | } |
3924 | } | ||||
3925 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3926 | |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3927 | // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))). |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3928 | if (N1.getOpcode() == ISD::TRUNCATE && |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3929 | N1.getOperand(0).getOpcode() == ISD::AND && |
3930 | N1.hasOneUse() && N1.getOperand(0).hasOneUse()) { | ||||
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3931 | SDValue N101 = N1.getOperand(0).getOperand(1); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3932 | if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3933 | EVT TruncVT = N1.getValueType(); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 3934 | SDValue N100 = N1.getOperand(0).getOperand(0); |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3935 | APInt TruncC = N101C->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 3936 | TruncC = TruncC.trunc(TruncVT.getScalarType().getSizeInBits()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3937 | return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0, |
3938 | DAG.getNode(ISD::AND, SDLoc(N), | ||||
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 3939 | TruncVT, |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 3940 | DAG.getNode(ISD::TRUNCATE, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3941 | SDLoc(N), |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 3942 | TruncVT, N100), |
Dan Gohman | ce9bc12 | 2009-01-27 20:39:34 +0000 | [diff] [blame] | 3943 | DAG.getConstant(TruncC, TruncVT))); |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 3944 | } |
3945 | } | ||||
3946 | |||||
Benjamin Kramer | 9b108a3 | 2011-01-30 16:38:43 +0000 | [diff] [blame] | 3947 | // fold (sra (trunc (sr x, c1)), c2) -> (trunc (sra x, c1+c2)) |
3948 | // if c1 is equal to the number of bits the trunc removes | ||||
3949 | if (N0.getOpcode() == ISD::TRUNCATE && | ||||
3950 | (N0.getOperand(0).getOpcode() == ISD::SRL || | ||||
3951 | N0.getOperand(0).getOpcode() == ISD::SRA) && | ||||
3952 | N0.getOperand(0).hasOneUse() && | ||||
3953 | N0.getOperand(0).getOperand(1).hasOneUse() && | ||||
3954 | N1C && isa<ConstantSDNode>(N0.getOperand(0).getOperand(1))) { | ||||
3955 | EVT LargeVT = N0.getOperand(0).getValueType(); | ||||
3956 | ConstantSDNode *LargeShiftAmt = | ||||
3957 | cast<ConstantSDNode>(N0.getOperand(0).getOperand(1)); | ||||
3958 | |||||
3959 | if (LargeVT.getScalarType().getSizeInBits() - OpSizeInBits == | ||||
3960 | LargeShiftAmt->getZExtValue()) { | ||||
3961 | SDValue Amt = | ||||
3962 | DAG.getConstant(LargeShiftAmt->getZExtValue() + N1C->getZExtValue(), | ||||
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 3963 | getShiftAmountTy(N0.getOperand(0).getOperand(0).getValueType())); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3964 | SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), LargeVT, |
Benjamin Kramer | 9b108a3 | 2011-01-30 16:38:43 +0000 | [diff] [blame] | 3965 | N0.getOperand(0).getOperand(0), Amt); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3966 | return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, SRA); |
Benjamin Kramer | 9b108a3 | 2011-01-30 16:38:43 +0000 | [diff] [blame] | 3967 | } |
3968 | } | ||||
3969 | |||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3970 | // Simplify, based on bits shifted out of the LHS. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3971 | if (N1C && SimplifyDemandedBits(SDValue(N, 0))) |
3972 | return SDValue(N, 0); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3973 | |
3974 | |||||
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3975 | // If the sign bit is known to be zero, switch this to a SRL. |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 3976 | if (DAG.SignBitIsZero(N0)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3977 | return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, N1); |
Chris Lattner | e70da20 | 2007-12-06 07:33:36 +0000 | [diff] [blame] | 3978 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 3979 | if (N1C) { |
3980 | SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue()); | ||||
3981 | if (NewSRA.getNode()) | ||||
3982 | return NewSRA; | ||||
3983 | } | ||||
3984 | |||||
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 3985 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3986 | } |
3987 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3988 | SDValue DAGCombiner::visitSRL(SDNode *N) { |
3989 | SDValue N0 = N->getOperand(0); | ||||
3990 | SDValue N1 = N->getOperand(1); | ||||
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3991 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
3992 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3993 | EVT VT = N0.getValueType(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3994 | unsigned OpSizeInBits = VT.getScalarType().getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3995 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3996 | // fold (srl c1, c2) -> c1 >>u c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 3997 | if (N0C && N1C) |
Bill Wendling | f3cbca2 | 2008-09-24 10:25:02 +0000 | [diff] [blame] | 3998 | return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3999 | // fold (srl 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 4000 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 4001 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4002 | // fold (srl x, c >= size(x)) -> undef |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4003 | if (N1C && N1C->getZExtValue() >= OpSizeInBits) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 4004 | return DAG.getUNDEF(VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4005 | // fold (srl x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 4006 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 4007 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4008 | // if (srl x, c) is known to be zero, return 0 |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4009 | if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0), |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 4010 | APInt::getAllOnesValue(OpSizeInBits))) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 4011 | return DAG.getConstant(0, VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4012 | |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 4013 | // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2)) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4014 | if (N1C && N0.getOpcode() == ISD::SRL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4015 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4016 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue(); |
4017 | uint64_t c2 = N1C->getZExtValue(); | ||||
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 4018 | if (c1 + c2 >= OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 4019 | return DAG.getConstant(0, VT); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4020 | return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 4021 | DAG.getConstant(c1 + c2, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4022 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4023 | |
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 4024 | // fold (srl (trunc (srl x, c1)), c2) -> 0 or (trunc (srl x, (add c1, c2))) |
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 4025 | if (N1C && N0.getOpcode() == ISD::TRUNCATE && |
4026 | N0.getOperand(0).getOpcode() == ISD::SRL && | ||||
Dale Johannesen | 025cc6e | 2010-12-20 20:10:50 +0000 | [diff] [blame] | 4027 | isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) { |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 4028 | uint64_t c1 = |
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 4029 | cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue(); |
4030 | uint64_t c2 = N1C->getZExtValue(); | ||||
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 4031 | EVT InnerShiftVT = N0.getOperand(0).getValueType(); |
4032 | EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType(); | ||||
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 4033 | uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits(); |
Dale Johannesen | 025cc6e | 2010-12-20 20:10:50 +0000 | [diff] [blame] | 4034 | // This is only valid if the OpSizeInBits + c1 = size of inner shift. |
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 4035 | if (c1 + OpSizeInBits == InnerShiftSize) { |
4036 | if (c1 + c2 >= InnerShiftSize) | ||||
4037 | return DAG.getConstant(0, VT); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4038 | return DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT, |
4039 | DAG.getNode(ISD::SRL, SDLoc(N0), InnerShiftVT, | ||||
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 4040 | N0.getOperand(0)->getOperand(0), |
Dale Johannesen | c72b18c | 2010-12-21 21:55:50 +0000 | [diff] [blame] | 4041 | DAG.getConstant(c1 + c2, ShiftCountVT))); |
Dale Johannesen | f5daf8b | 2010-12-17 21:45:49 +0000 | [diff] [blame] | 4042 | } |
4043 | } | ||||
4044 | |||||
Chris Lattner | efcddc3 | 2010-04-15 05:28:43 +0000 | [diff] [blame] | 4045 | // fold (srl (shl x, c), c) -> (and x, cst2) |
4046 | if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 && | ||||
4047 | N0.getValueSizeInBits() <= 64) { | ||||
4048 | uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits(); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4049 | return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0), |
Chris Lattner | efcddc3 | 2010-04-15 05:28:43 +0000 | [diff] [blame] | 4050 | DAG.getConstant(~0ULL >> ShAmt, VT)); |
4051 | } | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4052 | |
Michael Liao | 2da8639 | 2013-06-21 18:45:27 +0000 | [diff] [blame] | 4053 | // fold (srl (anyextend x), c) -> (and (anyextend (srl x, c)), mask) |
Chris Lattner | 06afe07 | 2006-05-05 22:53:17 +0000 | [diff] [blame] | 4054 | if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) { |
4055 | // Shifting in all undef bits? | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4056 | EVT SmallVT = N0.getOperand(0).getValueType(); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4057 | if (N1C->getZExtValue() >= SmallVT.getSizeInBits()) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 4058 | return DAG.getUNDEF(VT); |
Chris Lattner | 06afe07 | 2006-05-05 22:53:17 +0000 | [diff] [blame] | 4059 | |
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 4060 | if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) { |
Owen Anderson | a34d936 | 2011-04-14 17:30:49 +0000 | [diff] [blame] | 4061 | uint64_t ShiftAmt = N1C->getZExtValue(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4062 | SDValue SmallShift = DAG.getNode(ISD::SRL, SDLoc(N0), SmallVT, |
Owen Anderson | a34d936 | 2011-04-14 17:30:49 +0000 | [diff] [blame] | 4063 | N0.getOperand(0), |
4064 | DAG.getConstant(ShiftAmt, getShiftAmountTy(SmallVT))); | ||||
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 4065 | AddToWorkList(SmallShift.getNode()); |
Michael Liao | 2da8639 | 2013-06-21 18:45:27 +0000 | [diff] [blame] | 4066 | APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits()).lshr(ShiftAmt); |
4067 | return DAG.getNode(ISD::AND, SDLoc(N), VT, | ||||
4068 | DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, SmallShift), | ||||
4069 | DAG.getConstant(Mask, VT)); | ||||
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 4070 | } |
Chris Lattner | 06afe07 | 2006-05-05 22:53:17 +0000 | [diff] [blame] | 4071 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4072 | |
Chris Lattner | 3657ffe | 2006-10-12 20:23:19 +0000 | [diff] [blame] | 4073 | // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign |
4074 | // bit, which is unmodified by sra. | ||||
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 4075 | if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) { |
Chris Lattner | 3657ffe | 2006-10-12 20:23:19 +0000 | [diff] [blame] | 4076 | if (N0.getOpcode() == ISD::SRA) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4077 | return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0), N1); |
Chris Lattner | 3657ffe | 2006-10-12 20:23:19 +0000 | [diff] [blame] | 4078 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4079 | |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 4080 | // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit). |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4081 | if (N1C && N0.getOpcode() == ISD::CTLZ && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4082 | N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) { |
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 4083 | APInt KnownZero, KnownOne; |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 4084 | DAG.ComputeMaskedBits(N0.getOperand(0), KnownZero, KnownOne); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4085 | |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 4086 | // If any of the input bits are KnownOne, then the input couldn't be all |
4087 | // zeros, thus the result of the srl will always be zero. | ||||
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 4088 | if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4089 | |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 4090 | // If all of the bits input the to ctlz node are known to be zero, then |
4091 | // the result of the ctlz is "32" and the result of the shift is one. | ||||
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 4092 | APInt UnknownBits = ~KnownZero; |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 4093 | if (UnknownBits == 0) return DAG.getConstant(1, VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4094 | |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 4095 | // Otherwise, check to see if there is exactly one bit input to the ctlz. |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 4096 | if ((UnknownBits & (UnknownBits - 1)) == 0) { |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 4097 | // Okay, we know that only that the single bit specified by UnknownBits |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 4098 | // could be set on input to the CTLZ node. If this bit is set, the SRL |
4099 | // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair | ||||
4100 | // to an SRL/XOR pair, which is likely to simplify more. | ||||
Dan Gohman | 948d8ea | 2008-02-20 16:33:30 +0000 | [diff] [blame] | 4101 | unsigned ShAmt = UnknownBits.countTrailingZeros(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4102 | SDValue Op = N0.getOperand(0); |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 4103 | |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 4104 | if (ShAmt) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4105 | Op = DAG.getNode(ISD::SRL, SDLoc(N0), VT, Op, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 4106 | DAG.getConstant(ShAmt, getShiftAmountTy(Op.getValueType()))); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4107 | AddToWorkList(Op.getNode()); |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 4108 | } |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 4109 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4110 | return DAG.getNode(ISD::XOR, SDLoc(N), VT, |
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 4111 | Op, DAG.getConstant(1, VT)); |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 4112 | } |
4113 | } | ||||
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 4114 | |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 4115 | // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))). |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 4116 | if (N1.getOpcode() == ISD::TRUNCATE && |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 4117 | N1.getOperand(0).getOpcode() == ISD::AND && |
4118 | N1.hasOneUse() && N1.getOperand(0).hasOneUse()) { | ||||
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 4119 | SDValue N101 = N1.getOperand(0).getOperand(1); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 4120 | if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4121 | EVT TruncVT = N1.getValueType(); |
Evan Cheng | 242ebd1 | 2008-09-22 18:19:24 +0000 | [diff] [blame] | 4122 | SDValue N100 = N1.getOperand(0).getOperand(0); |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 4123 | APInt TruncC = N101C->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 4124 | TruncC = TruncC.trunc(TruncVT.getSizeInBits()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4125 | return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, |
4126 | DAG.getNode(ISD::AND, SDLoc(N), | ||||
Bill Wendling | 8810337 | 2009-01-30 21:37:17 +0000 | [diff] [blame] | 4127 | TruncVT, |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4128 | DAG.getNode(ISD::TRUNCATE, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4129 | SDLoc(N), |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 4130 | TruncVT, N100), |
Dan Gohman | ce9bc12 | 2009-01-27 20:39:34 +0000 | [diff] [blame] | 4131 | DAG.getConstant(TruncC, TruncVT))); |
Evan Cheng | eb9f892 | 2008-08-30 02:03:58 +0000 | [diff] [blame] | 4132 | } |
4133 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4134 | |
Chris Lattner | 61a4c07 | 2007-04-18 03:06:49 +0000 | [diff] [blame] | 4135 | // fold operands of srl based on knowledge that the low bits are not |
4136 | // demanded. | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4137 | if (N1C && SimplifyDemandedBits(SDValue(N, 0))) |
4138 | return SDValue(N, 0); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4139 | |
Evan Cheng | 9ab2b98 | 2009-12-18 21:31:31 +0000 | [diff] [blame] | 4140 | if (N1C) { |
4141 | SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue()); | ||||
4142 | if (NewSRL.getNode()) | ||||
4143 | return NewSRL; | ||||
4144 | } | ||||
4145 | |||||
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 4146 | // Attempt to convert a srl of a load into a narrower zero-extending load. |
4147 | SDValue NarrowLoad = ReduceLoadWidth(N); | ||||
4148 | if (NarrowLoad.getNode()) | ||||
4149 | return NarrowLoad; | ||||
4150 | |||||
Evan Cheng | 9ab2b98 | 2009-12-18 21:31:31 +0000 | [diff] [blame] | 4151 | // Here is a common situation. We want to optimize: |
4152 | // | ||||
4153 | // %a = ... | ||||
4154 | // %b = and i32 %a, 2 | ||||
4155 | // %c = srl i32 %b, 1 | ||||
4156 | // brcond i32 %c ... | ||||
4157 | // | ||||
4158 | // into | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4159 | // |
Evan Cheng | 9ab2b98 | 2009-12-18 21:31:31 +0000 | [diff] [blame] | 4160 | // %a = ... |
4161 | // %b = and %a, 2 | ||||
4162 | // %c = setcc eq %b, 0 | ||||
4163 | // brcond %c ... | ||||
4164 | // | ||||
4165 | // However when after the source operand of SRL is optimized into AND, the SRL | ||||
4166 | // itself may not be optimized further. Look for it and add the BRCOND into | ||||
4167 | // the worklist. | ||||
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 4168 | if (N->hasOneUse()) { |
4169 | SDNode *Use = *N->use_begin(); | ||||
4170 | if (Use->getOpcode() == ISD::BRCOND) | ||||
4171 | AddToWorkList(Use); | ||||
4172 | else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) { | ||||
4173 | // Also look pass the truncate. | ||||
4174 | Use = *Use->use_begin(); | ||||
4175 | if (Use->getOpcode() == ISD::BRCOND) | ||||
4176 | AddToWorkList(Use); | ||||
4177 | } | ||||
4178 | } | ||||
Evan Cheng | 9ab2b98 | 2009-12-18 21:31:31 +0000 | [diff] [blame] | 4179 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 4180 | return SDValue(); |
Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 4181 | } |
4182 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4183 | SDValue DAGCombiner::visitCTLZ(SDNode *N) { |
4184 | SDValue N0 = N->getOperand(0); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4185 | EVT VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4186 | |
4187 | // fold (ctlz c1) -> c2 | ||||
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 4188 | if (isa<ConstantSDNode>(N0)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4189 | return DAG.getNode(ISD::CTLZ, SDLoc(N), VT, N0); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4190 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4191 | } |
4192 | |||||
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 4193 | SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) { |
4194 | SDValue N0 = N->getOperand(0); | ||||
4195 | EVT VT = N->getValueType(0); | ||||
4196 | |||||
4197 | // fold (ctlz_zero_undef c1) -> c2 | ||||
4198 | if (isa<ConstantSDNode>(N0)) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4199 | return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SDLoc(N), VT, N0); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 4200 | return SDValue(); |
4201 | } | ||||
4202 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4203 | SDValue DAGCombiner::visitCTTZ(SDNode *N) { |
4204 | SDValue N0 = N->getOperand(0); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4205 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4206 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4207 | // fold (cttz c1) -> c2 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 4208 | if (isa<ConstantSDNode>(N0)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4209 | return DAG.getNode(ISD::CTTZ, SDLoc(N), VT, N0); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4210 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4211 | } |
4212 | |||||
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 4213 | SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) { |
4214 | SDValue N0 = N->getOperand(0); | ||||
4215 | EVT VT = N->getValueType(0); | ||||
4216 | |||||
4217 | // fold (cttz_zero_undef c1) -> c2 | ||||
4218 | if (isa<ConstantSDNode>(N0)) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4219 | return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, SDLoc(N), VT, N0); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 4220 | return SDValue(); |
4221 | } | ||||
4222 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4223 | SDValue DAGCombiner::visitCTPOP(SDNode *N) { |
4224 | SDValue N0 = N->getOperand(0); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4225 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4226 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4227 | // fold (ctpop c1) -> c2 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 4228 | if (isa<ConstantSDNode>(N0)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4229 | return DAG.getNode(ISD::CTPOP, SDLoc(N), VT, N0); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4230 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4231 | } |
4232 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4233 | SDValue DAGCombiner::visitSELECT(SDNode *N) { |
4234 | SDValue N0 = N->getOperand(0); | ||||
4235 | SDValue N1 = N->getOperand(1); | ||||
4236 | SDValue N2 = N->getOperand(2); | ||||
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4237 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
4238 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); | ||||
4239 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4240 | EVT VT = N->getValueType(0); |
4241 | EVT VT0 = N0.getValueType(); | ||||
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 4242 | |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4243 | // fold (select C, X, X) -> X |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4244 | if (N1 == N2) |
4245 | return N1; | ||||
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4246 | // fold (select true, X, Y) -> X |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4247 | if (N0C && !N0C->isNullValue()) |
4248 | return N1; | ||||
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4249 | // fold (select false, X, Y) -> Y |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4250 | if (N0C && N0C->isNullValue()) |
4251 | return N2; | ||||
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4252 | // fold (select C, 1, X) -> (or C, X) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4253 | if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4254 | return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2); |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4255 | // fold (select C, 0, 1) -> (xor C, 1) |
Bob Wilson | 67ba223 | 2009-01-22 22:05:48 +0000 | [diff] [blame] | 4256 | if (VT.isInteger() && |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4257 | (VT0 == MVT::i1 || |
Bob Wilson | 67ba223 | 2009-01-22 22:05:48 +0000 | [diff] [blame] | 4258 | (VT0.isInteger() && |
Nadav Rotem | 6dfabb6 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 4259 | TLI.getBooleanContents(false) == |
4260 | TargetLowering::ZeroOrOneBooleanContent)) && | ||||
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 4261 | N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) { |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4262 | SDValue XORNode; |
Evan Cheng | 571c478 | 2007-08-18 05:57:05 +0000 | [diff] [blame] | 4263 | if (VT == VT0) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4264 | return DAG.getNode(ISD::XOR, SDLoc(N), VT0, |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4265 | N0, DAG.getConstant(1, VT0)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4266 | XORNode = DAG.getNode(ISD::XOR, SDLoc(N0), VT0, |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4267 | N0, DAG.getConstant(1, VT0)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4268 | AddToWorkList(XORNode.getNode()); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4269 | if (VT.bitsGT(VT0)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4270 | return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, XORNode); |
4271 | return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, XORNode); | ||||
Evan Cheng | 571c478 | 2007-08-18 05:57:05 +0000 | [diff] [blame] | 4272 | } |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4273 | // fold (select C, 0, X) -> (and (not C), X) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4274 | if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4275 | SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT); |
Bob Wilson | 4c24546 | 2009-01-22 17:39:32 +0000 | [diff] [blame] | 4276 | AddToWorkList(NOTNode.getNode()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4277 | return DAG.getNode(ISD::AND, SDLoc(N), VT, NOTNode, N2); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4278 | } |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4279 | // fold (select C, X, 1) -> (or (not C), X) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4280 | if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4281 | SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT); |
Bob Wilson | 4c24546 | 2009-01-22 17:39:32 +0000 | [diff] [blame] | 4282 | AddToWorkList(NOTNode.getNode()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4283 | return DAG.getNode(ISD::OR, SDLoc(N), VT, NOTNode, N1); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4284 | } |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4285 | // fold (select C, X, 0) -> (and C, X) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4286 | if (VT == MVT::i1 && N2C && N2C->isNullValue()) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4287 | return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1); |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4288 | // fold (select X, X, Y) -> (or X, Y) |
4289 | // fold (select X, 1, Y) -> (or X, Y) | ||||
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4290 | if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1))) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4291 | return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2); |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4292 | // fold (select X, Y, X) -> (and X, Y) |
4293 | // fold (select X, Y, 0) -> (and X, Y) | ||||
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4294 | if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0))) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4295 | return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4296 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 4297 | // If we can fold this based on the true/false value, do so. |
4298 | if (SimplifySelectOps(N, N1, N2)) | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4299 | return SDValue(N, 0); // Don't revisit N. |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 4300 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 4301 | // fold selects based on a setcc into other things, such as min/max/abs |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 4302 | if (N0.getOpcode() == ISD::SETCC) { |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4303 | // FIXME: |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4304 | // Check against MVT::Other for SELECT_CC, which is a workaround for targets |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4305 | // having to say they don't support SELECT_CC on every type the DAG knows |
4306 | // about, since there is no way to mark an opcode illegal at all value types | ||||
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4307 | if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) && |
Dan Gohman | 4ea4804 | 2009-08-02 16:19:38 +0000 | [diff] [blame] | 4308 | TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4309 | return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4310 | N0.getOperand(0), N0.getOperand(1), |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4311 | N1, N2, N0.getOperand(2)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4312 | return SimplifySelect(SDLoc(N), N0, N1, N2); |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 4313 | } |
Bill Wendling | 34584e6 | 2009-01-30 22:02:18 +0000 | [diff] [blame] | 4314 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4315 | return SDValue(); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4316 | } |
4317 | |||||
Benjamin Kramer | 6242fda | 2013-04-26 09:19:19 +0000 | [diff] [blame] | 4318 | SDValue DAGCombiner::visitVSELECT(SDNode *N) { |
4319 | SDValue N0 = N->getOperand(0); | ||||
4320 | SDValue N1 = N->getOperand(1); | ||||
4321 | SDValue N2 = N->getOperand(2); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4322 | SDLoc DL(N); |
Benjamin Kramer | 6242fda | 2013-04-26 09:19:19 +0000 | [diff] [blame] | 4323 | |
4324 | // Canonicalize integer abs. | ||||
4325 | // vselect (setg[te] X, 0), X, -X -> | ||||
4326 | // vselect (setgt X, -1), X, -X -> | ||||
4327 | // vselect (setl[te] X, 0), -X, X -> | ||||
4328 | // Y = sra (X, size(X)-1); xor (add (X, Y), Y) | ||||
4329 | if (N0.getOpcode() == ISD::SETCC) { | ||||
4330 | SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1); | ||||
4331 | ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get(); | ||||
4332 | bool isAbs = false; | ||||
4333 | bool RHSIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode()); | ||||
4334 | |||||
4335 | if (((RHSIsAllZeros && (CC == ISD::SETGT || CC == ISD::SETGE)) || | ||||
4336 | (ISD::isBuildVectorAllOnes(RHS.getNode()) && CC == ISD::SETGT)) && | ||||
4337 | N1 == LHS && N2.getOpcode() == ISD::SUB && N1 == N2.getOperand(1)) | ||||
4338 | isAbs = ISD::isBuildVectorAllZeros(N2.getOperand(0).getNode()); | ||||
4339 | else if ((RHSIsAllZeros && (CC == ISD::SETLT || CC == ISD::SETLE)) && | ||||
4340 | N2 == LHS && N1.getOpcode() == ISD::SUB && N2 == N1.getOperand(1)) | ||||
4341 | isAbs = ISD::isBuildVectorAllZeros(N1.getOperand(0).getNode()); | ||||
4342 | |||||
4343 | if (isAbs) { | ||||
4344 | EVT VT = LHS.getValueType(); | ||||
4345 | SDValue Shift = DAG.getNode( | ||||
4346 | ISD::SRA, DL, VT, LHS, | ||||
4347 | DAG.getConstant(VT.getScalarType().getSizeInBits() - 1, VT)); | ||||
4348 | SDValue Add = DAG.getNode(ISD::ADD, DL, VT, LHS, Shift); | ||||
4349 | AddToWorkList(Shift.getNode()); | ||||
4350 | AddToWorkList(Add.getNode()); | ||||
4351 | return DAG.getNode(ISD::XOR, DL, VT, Add, Shift); | ||||
4352 | } | ||||
4353 | } | ||||
4354 | |||||
4355 | return SDValue(); | ||||
4356 | } | ||||
4357 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4358 | SDValue DAGCombiner::visitSELECT_CC(SDNode *N) { |
4359 | SDValue N0 = N->getOperand(0); | ||||
4360 | SDValue N1 = N->getOperand(1); | ||||
4361 | SDValue N2 = N->getOperand(2); | ||||
4362 | SDValue N3 = N->getOperand(3); | ||||
4363 | SDValue N4 = N->getOperand(4); | ||||
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 4364 | ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4365 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 4366 | // fold select_cc lhs, rhs, x, x, cc -> x |
4367 | if (N2 == N3) | ||||
4368 | return N2; | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4369 | |
Chris Lattner | 5f42a24 | 2006-09-20 06:19:26 +0000 | [diff] [blame] | 4370 | // Determine if the condition we're dealing with is constant |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 4371 | SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4372 | N0, N1, CC, SDLoc(N), false); |
Stephen Lin | 7e6d620 | 2013-06-15 04:03:33 +0000 | [diff] [blame] | 4373 | if (SCC.getNode()) { |
4374 | AddToWorkList(SCC.getNode()); | ||||
Chris Lattner | 5f42a24 | 2006-09-20 06:19:26 +0000 | [diff] [blame] | 4375 | |
Stephen Lin | 7e6d620 | 2013-06-15 04:03:33 +0000 | [diff] [blame] | 4376 | if (ConstantSDNode *SCCC = dyn_cast<ConstantSDNode>(SCC.getNode())) { |
4377 | if (!SCCC->isNullValue()) | ||||
4378 | return N2; // cond always true -> true val | ||||
4379 | else | ||||
4380 | return N3; // cond always false -> false val | ||||
4381 | } | ||||
4382 | |||||
4383 | // Fold to a simpler select_cc | ||||
4384 | if (SCC.getOpcode() == ISD::SETCC) | ||||
4385 | return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N2.getValueType(), | ||||
4386 | SCC.getOperand(0), SCC.getOperand(1), N2, N3, | ||||
4387 | SCC.getOperand(2)); | ||||
Chris Lattner | 5f42a24 | 2006-09-20 06:19:26 +0000 | [diff] [blame] | 4388 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4389 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 4390 | // If we can fold this based on the true/false value, do so. |
4391 | if (SimplifySelectOps(N, N2, N3)) | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4392 | return SDValue(N, 0); // Don't revisit N. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4393 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 4394 | // fold select_cc into other things, such as min/max/abs |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4395 | return SimplifySelectCC(SDLoc(N), N0, N1, N2, N3, CC); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4396 | } |
4397 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4398 | SDValue DAGCombiner::visitSETCC(SDNode *N) { |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4399 | return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1), |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 4400 | cast<CondCodeSDNode>(N->getOperand(2))->get(), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4401 | SDLoc(N)); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 4402 | } |
4403 | |||||
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4404 | // ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this: |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4405 | // "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))" |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4406 | // transformation. Returns true if extension are possible and the above |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4407 | // mentioned transformation is profitable. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4408 | static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0, |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4409 | unsigned ExtOpc, |
Craig Topper | a0ec3f9 | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 4410 | SmallVectorImpl<SDNode *> &ExtendNodes, |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 4411 | const TargetLowering &TLI) { |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4412 | bool HasCopyToRegUses = false; |
4413 | bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType()); | ||||
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 4414 | for (SDNode::use_iterator UI = N0.getNode()->use_begin(), |
4415 | UE = N0.getNode()->use_end(); | ||||
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4416 | UI != UE; ++UI) { |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 4417 | SDNode *User = *UI; |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4418 | if (User == N) |
4419 | continue; | ||||
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4420 | if (UI.getUse().getResNo() != N0.getResNo()) |
4421 | continue; | ||||
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4422 | // FIXME: Only extend SETCC N, N and SETCC N, c for now. |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4423 | if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) { |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4424 | ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get(); |
4425 | if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC)) | ||||
4426 | // Sign bits will be lost after a zext. | ||||
4427 | return false; | ||||
4428 | bool Add = false; | ||||
4429 | for (unsigned i = 0; i != 2; ++i) { | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4430 | SDValue UseOp = User->getOperand(i); |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4431 | if (UseOp == N0) |
4432 | continue; | ||||
4433 | if (!isa<ConstantSDNode>(UseOp)) | ||||
4434 | return false; | ||||
4435 | Add = true; | ||||
4436 | } | ||||
4437 | if (Add) | ||||
4438 | ExtendNodes.push_back(User); | ||||
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4439 | continue; |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4440 | } |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4441 | // If truncates aren't free and there are users we can't |
4442 | // extend, it isn't worthwhile. | ||||
4443 | if (!isTruncFree) | ||||
4444 | return false; | ||||
4445 | // Remember if this value is live-out. | ||||
4446 | if (User->getOpcode() == ISD::CopyToReg) | ||||
4447 | HasCopyToRegUses = true; | ||||
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4448 | } |
4449 | |||||
4450 | if (HasCopyToRegUses) { | ||||
4451 | bool BothLiveOut = false; | ||||
4452 | for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); | ||||
4453 | UI != UE; ++UI) { | ||||
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4454 | SDUse &Use = UI.getUse(); |
4455 | if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) { | ||||
4456 | BothLiveOut = true; | ||||
4457 | break; | ||||
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4458 | } |
4459 | } | ||||
4460 | if (BothLiveOut) | ||||
4461 | // Both unextended and extended values are live out. There had better be | ||||
Bob Wilson | bebfbc5 | 2010-11-28 06:51:19 +0000 | [diff] [blame] | 4462 | // a good reason for the transformation. |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4463 | return ExtendNodes.size(); |
4464 | } | ||||
4465 | return true; | ||||
4466 | } | ||||
4467 | |||||
Craig Topper | 6c64fba | 2013-07-13 07:43:40 +0000 | [diff] [blame] | 4468 | void DAGCombiner::ExtendSetCCUses(const SmallVectorImpl<SDNode *> &SetCCs, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4469 | SDValue Trunc, SDValue ExtLoad, SDLoc DL, |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4470 | ISD::NodeType ExtType) { |
4471 | // Extend SetCC uses if necessary. | ||||
4472 | for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) { | ||||
4473 | SDNode *SetCC = SetCCs[i]; | ||||
4474 | SmallVector<SDValue, 4> Ops; | ||||
4475 | |||||
4476 | for (unsigned j = 0; j != 2; ++j) { | ||||
4477 | SDValue SOp = SetCC->getOperand(j); | ||||
4478 | if (SOp == Trunc) | ||||
4479 | Ops.push_back(ExtLoad); | ||||
4480 | else | ||||
4481 | Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp)); | ||||
4482 | } | ||||
4483 | |||||
4484 | Ops.push_back(SetCC->getOperand(2)); | ||||
4485 | CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0), | ||||
4486 | &Ops[0], Ops.size())); | ||||
4487 | } | ||||
4488 | } | ||||
4489 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4490 | SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) { |
4491 | SDValue N0 = N->getOperand(0); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4492 | EVT VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4493 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4494 | // fold (sext c1) -> c1 |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 4495 | if (isa<ConstantSDNode>(N0)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4496 | return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4497 | |
Nadav Rotem | 0c8607b | 2013-01-20 08:35:56 +0000 | [diff] [blame] | 4498 | // fold (sext (sext x)) -> (sext x) |
4499 | // fold (sext (aext x)) -> (sext x) | ||||
4500 | if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4501 | return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, |
Nadav Rotem | 0c8607b | 2013-01-20 08:35:56 +0000 | [diff] [blame] | 4502 | N0.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4503 | |
Chris Lattner | 2255887 | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 4504 | if (N0.getOpcode() == ISD::TRUNCATE) { |
Dan Gohman | 1fdfa6a | 2008-05-20 20:56:33 +0000 | [diff] [blame] | 4505 | // fold (sext (truncate (load x))) -> (sext (smaller load x)) |
4506 | // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n))) | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4507 | SDValue NarrowLoad = ReduceLoadWidth(N0.getNode()); |
4508 | if (NarrowLoad.getNode()) { | ||||
Dale Johannesen | 61734eb | 2010-05-25 17:50:03 +0000 | [diff] [blame] | 4509 | SDNode* oye = N0.getNode()->getOperand(0).getNode(); |
4510 | if (NarrowLoad.getNode() != N0.getNode()) { | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4511 | CombineTo(N0.getNode(), NarrowLoad); |
Dale Johannesen | 61734eb | 2010-05-25 17:50:03 +0000 | [diff] [blame] | 4512 | // CombineTo deleted the truncate, if needed, but not what's under it. |
4513 | AddToWorkList(oye); | ||||
4514 | } | ||||
Dan Gohman | c7b3444 | 2009-04-27 02:00:55 +0000 | [diff] [blame] | 4515 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 0b063de | 2007-03-23 02:16:52 +0000 | [diff] [blame] | 4516 | } |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4517 | |
Dan Gohman | 1fdfa6a | 2008-05-20 20:56:33 +0000 | [diff] [blame] | 4518 | // See if the value being truncated is already sign extended. If so, just |
4519 | // eliminate the trunc/sext pair. | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4520 | SDValue Op = N0.getOperand(0); |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 4521 | unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits(); |
4522 | unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits(); | ||||
4523 | unsigned DestBits = VT.getScalarType().getSizeInBits(); | ||||
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 4524 | unsigned NumSignBits = DAG.ComputeNumSignBits(Op); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4525 | |
Chris Lattner | 2255887 | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 4526 | if (OpBits == DestBits) { |
4527 | // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign | ||||
4528 | // bits, it is already ready. | ||||
4529 | if (NumSignBits > DestBits-MidBits) | ||||
4530 | return Op; | ||||
4531 | } else if (OpBits < DestBits) { | ||||
4532 | // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign | ||||
4533 | // bits, just sext from i32. | ||||
4534 | if (NumSignBits > OpBits-MidBits) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4535 | return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, Op); |
Chris Lattner | 2255887 | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 4536 | } else { |
4537 | // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign | ||||
4538 | // bits, just truncate to i32. | ||||
4539 | if (NumSignBits > OpBits-MidBits) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4540 | return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op); |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 4541 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4542 | |
Chris Lattner | 2255887 | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 4543 | // fold (sext (truncate x)) -> (sextinreg x). |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4544 | if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, |
4545 | N0.getValueType())) { | ||||
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 4546 | if (OpBits < DestBits) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4547 | Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N0), VT, Op); |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 4548 | else if (OpBits > DestBits) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4549 | Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT, Op); |
4550 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, Op, | ||||
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 4551 | DAG.getValueType(N0.getValueType())); |
Chris Lattner | 2255887 | 2007-02-26 03:13:59 +0000 | [diff] [blame] | 4552 | } |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 4553 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4554 | |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 4555 | // fold (sext (load x)) -> (sext (truncate (sextload x))) |
Nadav Rotem | 8c20ec5 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 4556 | // None of the supported targets knows how to perform load and sign extend |
Nadav Rotem | fcd9619 | 2011-02-27 07:40:43 +0000 | [diff] [blame] | 4557 | // on vectors in one instruction. We only perform this transformation on |
4558 | // scalars. | ||||
Nadav Rotem | 8c20ec5 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 4559 | if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4560 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 4561 | TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) { |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4562 | bool DoXform = true; |
4563 | SmallVector<SDNode*, 4> SetCCs; | ||||
4564 | if (!N0.hasOneUse()) | ||||
4565 | DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI); | ||||
4566 | if (DoXform) { | ||||
4567 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4568 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT, |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 4569 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 4570 | LN0->getBasePtr(), LN0->getPointerInfo(), |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4571 | N0.getValueType(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4572 | LN0->isVolatile(), LN0->isNonTemporal(), |
4573 | LN0->getAlignment()); | ||||
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4574 | CombineTo(N, ExtLoad); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4575 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4576 | N0.getValueType(), ExtLoad); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4577 | CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4578 | ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N), |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4579 | ISD::SIGN_EXTEND); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4580 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4581 | } |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 4582 | } |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 4583 | |
4584 | // fold (sext (sextload x)) -> (sext (truncate (sextload x))) | ||||
4585 | // fold (sext ( extload x)) -> (sext (truncate (sextload x))) | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4586 | if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) && |
4587 | ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) { | ||||
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4588 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 4589 | EVT MemVT = LN0->getMemoryVT(); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4590 | if ((!LegalOperations && !LN0->isVolatile()) || |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 4591 | TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4592 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT, |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4593 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 4594 | LN0->getBasePtr(), LN0->getPointerInfo(), |
4595 | MemVT, | ||||
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4596 | LN0->isVolatile(), LN0->isNonTemporal(), |
4597 | LN0->getAlignment()); | ||||
Jim Laskey | f6c4ccf | 2006-12-15 21:38:30 +0000 | [diff] [blame] | 4598 | CombineTo(N, ExtLoad); |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 4599 | CombineTo(N0.getNode(), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4600 | DAG.getNode(ISD::TRUNCATE, SDLoc(N0), |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4601 | N0.getValueType(), ExtLoad), |
Jim Laskey | f6c4ccf | 2006-12-15 21:38:30 +0000 | [diff] [blame] | 4602 | ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4603 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Jim Laskey | f6c4ccf | 2006-12-15 21:38:30 +0000 | [diff] [blame] | 4604 | } |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 4605 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4606 | |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4607 | // fold (sext (and/or/xor (load x), cst)) -> |
4608 | // (and/or/xor (sextload x), (sext cst)) | ||||
4609 | if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR || | ||||
4610 | N0.getOpcode() == ISD::XOR) && | ||||
4611 | isa<LoadSDNode>(N0.getOperand(0)) && | ||||
4612 | N0.getOperand(1).getOpcode() == ISD::Constant && | ||||
4613 | TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()) && | ||||
4614 | (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) { | ||||
4615 | LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0)); | ||||
4616 | if (LN0->getExtensionType() != ISD::ZEXTLOAD) { | ||||
4617 | bool DoXform = true; | ||||
4618 | SmallVector<SDNode*, 4> SetCCs; | ||||
4619 | if (!N0.hasOneUse()) | ||||
4620 | DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::SIGN_EXTEND, | ||||
4621 | SetCCs, TLI); | ||||
4622 | if (DoXform) { | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4623 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(LN0), VT, |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4624 | LN0->getChain(), LN0->getBasePtr(), |
4625 | LN0->getPointerInfo(), | ||||
4626 | LN0->getMemoryVT(), | ||||
4627 | LN0->isVolatile(), | ||||
4628 | LN0->isNonTemporal(), | ||||
4629 | LN0->getAlignment()); | ||||
4630 | APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); | ||||
4631 | Mask = Mask.sext(VT.getSizeInBits()); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4632 | SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT, |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4633 | ExtLoad, DAG.getConstant(Mask, VT)); |
4634 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4635 | SDLoc(N0.getOperand(0)), |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4636 | N0.getOperand(0).getValueType(), ExtLoad); |
4637 | CombineTo(N, And); | ||||
4638 | CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1)); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4639 | ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N), |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4640 | ISD::SIGN_EXTEND); |
4641 | return SDValue(N, 0); // Return N so it doesn't get rechecked! | ||||
4642 | } | ||||
4643 | } | ||||
4644 | } | ||||
4645 | |||||
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 4646 | if (N0.getOpcode() == ISD::SETCC) { |
Chris Lattner | 2b7a271 | 2009-07-08 00:31:33 +0000 | [diff] [blame] | 4647 | // sext(setcc) -> sext_in_reg(vsetcc) for vectors. |
Dan Gohman | 3ce89f4 | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 4648 | // Only do this before legalize for now. |
Owen Anderson | ed5707b | 2013-04-23 18:09:28 +0000 | [diff] [blame] | 4649 | if (VT.isVector() && !LegalOperations && |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 4650 | TLI.getBooleanContents(true) == |
Owen Anderson | ed5707b | 2013-04-23 18:09:28 +0000 | [diff] [blame] | 4651 | TargetLowering::ZeroOrNegativeOneBooleanContent) { |
Dan Gohman | 3ce89f4 | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 4652 | EVT N0VT = N0.getOperand(0).getValueType(); |
Nadav Rotem | 2e50619 | 2012-04-11 08:26:11 +0000 | [diff] [blame] | 4653 | // On some architectures (such as SSE/NEON/etc) the SETCC result type is |
4654 | // of the same size as the compared operands. Only optimize sext(setcc()) | ||||
4655 | // if this is the case. | ||||
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 4656 | EVT SVT = getSetCCResultType(N0VT); |
Nadav Rotem | 2e50619 | 2012-04-11 08:26:11 +0000 | [diff] [blame] | 4657 | |
4658 | // We know that the # elements of the results is the same as the | ||||
4659 | // # elements of the compare (and the # elements of the compare result | ||||
4660 | // for that matter). Check to see that they are the same size. If so, | ||||
4661 | // we know that the element size of the sext'd result matches the | ||||
4662 | // element size of the compare operands. | ||||
4663 | if (VT.getSizeInBits() == SVT.getSizeInBits()) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4664 | return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0), |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 4665 | N0.getOperand(1), |
4666 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); | ||||
Matt Arsenault | 9aa8fdf | 2013-05-17 21:43:43 +0000 | [diff] [blame] | 4667 | |
Dan Gohman | 3ce89f4 | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 4668 | // If the desired elements are smaller or larger than the source |
4669 | // elements we can use a matching integer vector type and then | ||||
4670 | // truncate/sign extend | ||||
Matt Arsenault | 9aa8fdf | 2013-05-17 21:43:43 +0000 | [diff] [blame] | 4671 | EVT MatchingVectorType = N0VT.changeVectorElementTypeToInteger(); |
Craig Topper | 0eb5dad | 2012-09-29 07:18:53 +0000 | [diff] [blame] | 4672 | if (SVT == MatchingVectorType) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4673 | SDValue VsetCC = DAG.getSetCC(SDLoc(N), MatchingVectorType, |
Craig Topper | 0eb5dad | 2012-09-29 07:18:53 +0000 | [diff] [blame] | 4674 | N0.getOperand(0), N0.getOperand(1), |
4675 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4676 | return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT); |
Dan Gohman | 3ce89f4 | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 4677 | } |
Chris Lattner | 2b7a271 | 2009-07-08 00:31:33 +0000 | [diff] [blame] | 4678 | } |
Dan Gohman | 3ce89f4 | 2010-04-30 17:19:19 +0000 | [diff] [blame] | 4679 | |
Chris Lattner | 2b7a271 | 2009-07-08 00:31:33 +0000 | [diff] [blame] | 4680 | // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc) |
Dan Gohman | a7bcef1 | 2010-04-24 01:17:30 +0000 | [diff] [blame] | 4681 | unsigned ElementWidth = VT.getScalarType().getSizeInBits(); |
Dan Gohman | 5cbd37e | 2009-08-06 09:18:59 +0000 | [diff] [blame] | 4682 | SDValue NegOne = |
Dan Gohman | a7bcef1 | 2010-04-24 01:17:30 +0000 | [diff] [blame] | 4683 | DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4684 | SDValue SCC = |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4685 | SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1), |
Dan Gohman | 5cbd37e | 2009-08-06 09:18:59 +0000 | [diff] [blame] | 4686 | NegOne, DAG.getConstant(0, VT), |
Chris Lattner | 1eba01e | 2007-04-11 06:50:51 +0000 | [diff] [blame] | 4687 | cast<CondCodeSDNode>(N0.getOperand(2))->get(), true); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4688 | if (SCC.getNode()) return SCC; |
Matt Arsenault | b05e477 | 2013-06-14 22:04:37 +0000 | [diff] [blame] | 4689 | if (!VT.isVector() && |
4690 | (!LegalOperations || | ||||
4691 | TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(VT)))) { | ||||
4692 | return DAG.getSelect(SDLoc(N), VT, | ||||
4693 | DAG.getSetCC(SDLoc(N), | ||||
4694 | getSetCCResultType(VT), | ||||
4695 | N0.getOperand(0), N0.getOperand(1), | ||||
4696 | cast<CondCodeSDNode>(N0.getOperand(2))->get()), | ||||
4697 | NegOne, DAG.getConstant(0, VT)); | ||||
4698 | } | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4699 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4700 | |
Dan Gohman | 8f0ad58 | 2008-04-28 16:58:24 +0000 | [diff] [blame] | 4701 | // fold (sext x) -> (zext x) if the sign bit is known zero. |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4702 | if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) && |
Dan Gohman | 187db7b | 2008-04-28 18:47:17 +0000 | [diff] [blame] | 4703 | DAG.SignBitIsZero(N0)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4704 | return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4705 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 4706 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4707 | } |
4708 | |||||
Rafael Espindola | decbc43 | 2012-04-09 16:06:03 +0000 | [diff] [blame] | 4709 | // isTruncateOf - If N is a truncate of some other value, return true, record |
4710 | // the value being truncated in Op and which of Op's bits are zero in KnownZero. | ||||
4711 | // This function computes KnownZero to avoid a duplicated call to | ||||
4712 | // ComputeMaskedBits in the caller. | ||||
4713 | static bool isTruncateOf(SelectionDAG &DAG, SDValue N, SDValue &Op, | ||||
4714 | APInt &KnownZero) { | ||||
4715 | APInt KnownOne; | ||||
4716 | if (N->getOpcode() == ISD::TRUNCATE) { | ||||
4717 | Op = N->getOperand(0); | ||||
4718 | DAG.ComputeMaskedBits(Op, KnownZero, KnownOne); | ||||
4719 | return true; | ||||
4720 | } | ||||
4721 | |||||
4722 | if (N->getOpcode() != ISD::SETCC || N->getValueType(0) != MVT::i1 || | ||||
4723 | cast<CondCodeSDNode>(N->getOperand(2))->get() != ISD::SETNE) | ||||
4724 | return false; | ||||
4725 | |||||
4726 | SDValue Op0 = N->getOperand(0); | ||||
4727 | SDValue Op1 = N->getOperand(1); | ||||
4728 | assert(Op0.getValueType() == Op1.getValueType()); | ||||
4729 | |||||
4730 | ConstantSDNode *COp0 = dyn_cast<ConstantSDNode>(Op0); | ||||
4731 | ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1); | ||||
Rafael Espindola | fdb230a | 2012-04-10 00:16:22 +0000 | [diff] [blame] | 4732 | if (COp0 && COp0->isNullValue()) |
Rafael Espindola | decbc43 | 2012-04-09 16:06:03 +0000 | [diff] [blame] | 4733 | Op = Op1; |
Rafael Espindola | fdb230a | 2012-04-10 00:16:22 +0000 | [diff] [blame] | 4734 | else if (COp1 && COp1->isNullValue()) |
Rafael Espindola | decbc43 | 2012-04-09 16:06:03 +0000 | [diff] [blame] | 4735 | Op = Op0; |
4736 | else | ||||
4737 | return false; | ||||
4738 | |||||
4739 | DAG.ComputeMaskedBits(Op, KnownZero, KnownOne); | ||||
4740 | |||||
4741 | if (!(KnownZero | APInt(Op.getValueSizeInBits(), 1)).isAllOnesValue()) | ||||
4742 | return false; | ||||
4743 | |||||
4744 | return true; | ||||
4745 | } | ||||
4746 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4747 | SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) { |
4748 | SDValue N0 = N->getOperand(0); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4749 | EVT VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4750 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4751 | // fold (zext c1) -> c1 |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 4752 | if (isa<ConstantSDNode>(N0)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4753 | return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 4754 | // fold (zext (zext x)) -> (zext x) |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 4755 | // fold (zext (aext x)) -> (zext x) |
4756 | if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4757 | return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4758 | N0.getOperand(0)); |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 4759 | |
Chandler Carruth | f103b3d | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 4760 | // fold (zext (truncate x)) -> (zext x) or |
4761 | // (zext (truncate x)) -> (truncate x) | ||||
4762 | // This is valid when the truncated bits of x are already zero. | ||||
4763 | // FIXME: We should extend this to work for vectors too. | ||||
Rafael Espindola | decbc43 | 2012-04-09 16:06:03 +0000 | [diff] [blame] | 4764 | SDValue Op; |
4765 | APInt KnownZero; | ||||
4766 | if (!VT.isVector() && isTruncateOf(DAG, N0, Op, KnownZero)) { | ||||
4767 | APInt TruncatedBits = | ||||
4768 | (Op.getValueSizeInBits() == N0.getValueSizeInBits()) ? | ||||
4769 | APInt(Op.getValueSizeInBits(), 0) : | ||||
4770 | APInt::getBitsSet(Op.getValueSizeInBits(), | ||||
4771 | N0.getValueSizeInBits(), | ||||
4772 | std::min(Op.getValueSizeInBits(), | ||||
4773 | VT.getSizeInBits())); | ||||
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 4774 | if (TruncatedBits == (KnownZero & TruncatedBits)) { |
Chandler Carruth | f103b3d | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 4775 | if (VT.bitsGT(Op.getValueType())) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4776 | return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, Op); |
Chandler Carruth | f103b3d | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 4777 | if (VT.bitsLT(Op.getValueType())) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4778 | return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op); |
Chandler Carruth | f103b3d | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 4779 | |
4780 | return Op; | ||||
4781 | } | ||||
4782 | } | ||||
4783 | |||||
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4784 | // fold (zext (truncate (load x))) -> (zext (smaller load x)) |
4785 | // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n))) | ||||
Dale Johannesen | 2041a0e | 2007-03-30 21:38:07 +0000 | [diff] [blame] | 4786 | if (N0.getOpcode() == ISD::TRUNCATE) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4787 | SDValue NarrowLoad = ReduceLoadWidth(N0.getNode()); |
4788 | if (NarrowLoad.getNode()) { | ||||
Dale Johannesen | 61734eb | 2010-05-25 17:50:03 +0000 | [diff] [blame] | 4789 | SDNode* oye = N0.getNode()->getOperand(0).getNode(); |
4790 | if (NarrowLoad.getNode() != N0.getNode()) { | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4791 | CombineTo(N0.getNode(), NarrowLoad); |
Dale Johannesen | 61734eb | 2010-05-25 17:50:03 +0000 | [diff] [blame] | 4792 | // CombineTo deleted the truncate, if needed, but not what's under it. |
4793 | AddToWorkList(oye); | ||||
4794 | } | ||||
Eli Friedman | e545d38 | 2011-04-16 23:25:34 +0000 | [diff] [blame] | 4795 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 0b063de | 2007-03-23 02:16:52 +0000 | [diff] [blame] | 4796 | } |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 4797 | } |
4798 | |||||
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 4799 | // fold (zext (truncate x)) -> (and x, mask) |
4800 | if (N0.getOpcode() == ISD::TRUNCATE && | ||||
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 4801 | (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) { |
Dan Gohman | 394d629 | 2010-11-03 01:47:46 +0000 | [diff] [blame] | 4802 | |
4803 | // fold (zext (truncate (load x))) -> (zext (smaller load x)) | ||||
4804 | // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n))) | ||||
4805 | SDValue NarrowLoad = ReduceLoadWidth(N0.getNode()); | ||||
4806 | if (NarrowLoad.getNode()) { | ||||
4807 | SDNode* oye = N0.getNode()->getOperand(0).getNode(); | ||||
4808 | if (NarrowLoad.getNode() != N0.getNode()) { | ||||
4809 | CombineTo(N0.getNode(), NarrowLoad); | ||||
4810 | // CombineTo deleted the truncate, if needed, but not what's under it. | ||||
4811 | AddToWorkList(oye); | ||||
4812 | } | ||||
4813 | return SDValue(N, 0); // Return N so it doesn't get rechecked! | ||||
4814 | } | ||||
4815 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4816 | SDValue Op = N0.getOperand(0); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4817 | if (Op.getValueType().bitsLT(VT)) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4818 | Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, Op); |
Elena Demikhovsky | 1da5867 | 2012-04-22 09:39:03 +0000 | [diff] [blame] | 4819 | AddToWorkList(Op.getNode()); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4820 | } else if (Op.getValueType().bitsGT(VT)) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4821 | Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op); |
Elena Demikhovsky | 1da5867 | 2012-04-22 09:39:03 +0000 | [diff] [blame] | 4822 | AddToWorkList(Op.getNode()); |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 4823 | } |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4824 | return DAG.getZeroExtendInReg(Op, SDLoc(N), |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 4825 | N0.getValueType().getScalarType()); |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 4826 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4827 | |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 4828 | // Fold (zext (and (trunc x), cst)) -> (and x, cst), |
4829 | // if either of the casts is not free. | ||||
Chris Lattner | 111c228 | 2006-09-21 06:14:31 +0000 | [diff] [blame] | 4830 | if (N0.getOpcode() == ISD::AND && |
4831 | N0.getOperand(0).getOpcode() == ISD::TRUNCATE && | ||||
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 4832 | N0.getOperand(1).getOpcode() == ISD::Constant && |
4833 | (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(), | ||||
4834 | N0.getValueType()) || | ||||
4835 | !TLI.isZExtFree(N0.getValueType(), VT))) { | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4836 | SDValue X = N0.getOperand(0).getOperand(0); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4837 | if (X.getValueType().bitsLT(VT)) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4838 | X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(X), VT, X); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4839 | } else if (X.getValueType().bitsGT(VT)) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4840 | X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X); |
Chris Lattner | 111c228 | 2006-09-21 06:14:31 +0000 | [diff] [blame] | 4841 | } |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 4842 | APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 4843 | Mask = Mask.zext(VT.getSizeInBits()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4844 | return DAG.getNode(ISD::AND, SDLoc(N), VT, |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4845 | X, DAG.getConstant(Mask, VT)); |
Chris Lattner | 111c228 | 2006-09-21 06:14:31 +0000 | [diff] [blame] | 4846 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4847 | |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 4848 | // fold (zext (load x)) -> (zext (truncate (zextload x))) |
Nadav Rotem | ed9b934 | 2011-02-20 12:37:50 +0000 | [diff] [blame] | 4849 | // None of the supported targets knows how to perform load and vector_zext |
Nadav Rotem | fcd9619 | 2011-02-27 07:40:43 +0000 | [diff] [blame] | 4850 | // on vectors in one instruction. We only perform this transformation on |
4851 | // scalars. | ||||
Nadav Rotem | ed9b934 | 2011-02-20 12:37:50 +0000 | [diff] [blame] | 4852 | if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4853 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 4854 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) { |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4855 | bool DoXform = true; |
4856 | SmallVector<SDNode*, 4> SetCCs; | ||||
4857 | if (!N0.hasOneUse()) | ||||
4858 | DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI); | ||||
4859 | if (DoXform) { | ||||
4860 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4861 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT, |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4862 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 4863 | LN0->getBasePtr(), LN0->getPointerInfo(), |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4864 | N0.getValueType(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4865 | LN0->isVolatile(), LN0->isNonTemporal(), |
4866 | LN0->getAlignment()); | ||||
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4867 | CombineTo(N, ExtLoad); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4868 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4869 | N0.getValueType(), ExtLoad); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4870 | CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1)); |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4871 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4872 | ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N), |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4873 | ISD::ZERO_EXTEND); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4874 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 4875 | } |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 4876 | } |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 4877 | |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4878 | // fold (zext (and/or/xor (load x), cst)) -> |
4879 | // (and/or/xor (zextload x), (zext cst)) | ||||
4880 | if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR || | ||||
4881 | N0.getOpcode() == ISD::XOR) && | ||||
4882 | isa<LoadSDNode>(N0.getOperand(0)) && | ||||
4883 | N0.getOperand(1).getOpcode() == ISD::Constant && | ||||
4884 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()) && | ||||
4885 | (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) { | ||||
4886 | LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0)); | ||||
4887 | if (LN0->getExtensionType() != ISD::SEXTLOAD) { | ||||
4888 | bool DoXform = true; | ||||
4889 | SmallVector<SDNode*, 4> SetCCs; | ||||
4890 | if (!N0.hasOneUse()) | ||||
4891 | DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::ZERO_EXTEND, | ||||
4892 | SetCCs, TLI); | ||||
4893 | if (DoXform) { | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4894 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), VT, |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4895 | LN0->getChain(), LN0->getBasePtr(), |
4896 | LN0->getPointerInfo(), | ||||
4897 | LN0->getMemoryVT(), | ||||
4898 | LN0->isVolatile(), | ||||
4899 | LN0->isNonTemporal(), | ||||
4900 | LN0->getAlignment()); | ||||
4901 | APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); | ||||
4902 | Mask = Mask.zext(VT.getSizeInBits()); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4903 | SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT, |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4904 | ExtLoad, DAG.getConstant(Mask, VT)); |
4905 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4906 | SDLoc(N0.getOperand(0)), |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4907 | N0.getOperand(0).getValueType(), ExtLoad); |
4908 | CombineTo(N, And); | ||||
4909 | CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1)); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4910 | ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N), |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 4911 | ISD::ZERO_EXTEND); |
4912 | return SDValue(N, 0); // Return N so it doesn't get rechecked! | ||||
4913 | } | ||||
4914 | } | ||||
4915 | } | ||||
4916 | |||||
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 4917 | // fold (zext (zextload x)) -> (zext (truncate (zextload x))) |
4918 | // fold (zext ( extload x)) -> (zext (truncate (zextload x))) | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4919 | if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) && |
4920 | ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) { | ||||
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4921 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 4922 | EVT MemVT = LN0->getMemoryVT(); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4923 | if ((!LegalOperations && !LN0->isVolatile()) || |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 4924 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4925 | SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT, |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4926 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 4927 | LN0->getBasePtr(), LN0->getPointerInfo(), |
4928 | MemVT, | ||||
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4929 | LN0->isVolatile(), LN0->isNonTemporal(), |
4930 | LN0->getAlignment()); | ||||
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 4931 | CombineTo(N, ExtLoad); |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 4932 | CombineTo(N0.getNode(), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4933 | DAG.getNode(ISD::TRUNCATE, SDLoc(N0), N0.getValueType(), |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 4934 | ExtLoad), |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 4935 | ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4936 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 4937 | } |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 4938 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4939 | |
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 4940 | if (N0.getOpcode() == ISD::SETCC) { |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 4941 | if (!LegalOperations && VT.isVector()) { |
4942 | // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors. | ||||
4943 | // Only do this before legalize for now. | ||||
4944 | EVT N0VT = N0.getOperand(0).getValueType(); | ||||
4945 | EVT EltVT = VT.getVectorElementType(); | ||||
4946 | SmallVector<SDValue,8> OneOps(VT.getVectorNumElements(), | ||||
4947 | DAG.getConstant(1, EltVT)); | ||||
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 4948 | if (VT.getSizeInBits() == N0VT.getSizeInBits()) |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 4949 | // We know that the # elements of the results is the same as the |
4950 | // # elements of the compare (and the # elements of the compare result | ||||
4951 | // for that matter). Check to see that they are the same size. If so, | ||||
4952 | // we know that the element size of the sext'd result matches the | ||||
4953 | // element size of the compare operands. | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4954 | return DAG.getNode(ISD::AND, SDLoc(N), VT, |
4955 | DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0), | ||||
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 4956 | N0.getOperand(1), |
4957 | cast<CondCodeSDNode>(N0.getOperand(2))->get()), | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4958 | DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 4959 | &OneOps[0], OneOps.size())); |
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 4960 | |
4961 | // If the desired elements are smaller or larger than the source | ||||
4962 | // elements we can use a matching integer vector type and then | ||||
4963 | // truncate/sign extend | ||||
4964 | EVT MatchingElementType = | ||||
4965 | EVT::getIntegerVT(*DAG.getContext(), | ||||
4966 | N0VT.getScalarType().getSizeInBits()); | ||||
4967 | EVT MatchingVectorType = | ||||
4968 | EVT::getVectorVT(*DAG.getContext(), MatchingElementType, | ||||
4969 | N0VT.getVectorNumElements()); | ||||
4970 | SDValue VsetCC = | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4971 | DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0), |
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 4972 | N0.getOperand(1), |
4973 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4974 | return DAG.getNode(ISD::AND, SDLoc(N), VT, |
4975 | DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT), | ||||
4976 | DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, | ||||
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 4977 | &OneOps[0], OneOps.size())); |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 4978 | } |
4979 | |||||
4980 | // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4981 | SDValue SCC = |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4982 | SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1), |
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 4983 | DAG.getConstant(1, VT), DAG.getConstant(0, VT), |
Chris Lattner | 1eba01e | 2007-04-11 06:50:51 +0000 | [diff] [blame] | 4984 | cast<CondCodeSDNode>(N0.getOperand(2))->get(), true); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4985 | if (SCC.getNode()) return SCC; |
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 4986 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4987 | |
Evan Cheng | 9818c04 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 4988 | // (zext (shl (zext x), cst)) -> (shl (zext x), cst) |
Evan Cheng | 99b653c | 2009-12-15 00:41:36 +0000 | [diff] [blame] | 4989 | if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) && |
Evan Cheng | 9818c04 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 4990 | isa<ConstantSDNode>(N0.getOperand(1)) && |
Evan Cheng | 99b653c | 2009-12-15 00:41:36 +0000 | [diff] [blame] | 4991 | N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND && |
4992 | N0.hasOneUse()) { | ||||
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 4993 | SDValue ShAmt = N0.getOperand(1); |
4994 | unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue(); | ||||
Evan Cheng | 9818c04 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 4995 | if (N0.getOpcode() == ISD::SHL) { |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 4996 | SDValue InnerZExt = N0.getOperand(0); |
Evan Cheng | 9818c04 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 4997 | // If the original shl may be shifting out bits, do not perform this |
4998 | // transformation. | ||||
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 4999 | unsigned KnownZeroBits = InnerZExt.getValueType().getSizeInBits() - |
5000 | InnerZExt.getOperand(0).getValueType().getSizeInBits(); | ||||
5001 | if (ShAmtVal > KnownZeroBits) | ||||
Evan Cheng | 9818c04 | 2009-12-15 03:00:32 +0000 | [diff] [blame] | 5002 | return SDValue(); |
5003 | } | ||||
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 5004 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5005 | SDLoc DL(N); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5006 | |
5007 | // Ensure that the shift amount is wide enough for the shifted value. | ||||
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 5008 | if (VT.getSizeInBits() >= 256) |
5009 | ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt); | ||||
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5010 | |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 5011 | return DAG.getNode(N0.getOpcode(), DL, VT, |
5012 | DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)), | ||||
5013 | ShAmt); | ||||
Evan Cheng | 99b653c | 2009-12-15 00:41:36 +0000 | [diff] [blame] | 5014 | } |
5015 | |||||
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 5016 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5017 | } |
5018 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5019 | SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) { |
5020 | SDValue N0 = N->getOperand(0); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5021 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5022 | |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 5023 | // fold (aext c1) -> c1 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 5024 | if (isa<ConstantSDNode>(N0)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5025 | return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, N0); |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 5026 | // fold (aext (aext x)) -> (aext x) |
5027 | // fold (aext (zext x)) -> (zext x) | ||||
5028 | // fold (aext (sext x)) -> (sext x) | ||||
5029 | if (N0.getOpcode() == ISD::ANY_EXTEND || | ||||
5030 | N0.getOpcode() == ISD::ZERO_EXTEND || | ||||
5031 | N0.getOpcode() == ISD::SIGN_EXTEND) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5032 | return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, N0.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5033 | |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5034 | // fold (aext (truncate (load x))) -> (aext (smaller load x)) |
5035 | // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n))) | ||||
5036 | if (N0.getOpcode() == ISD::TRUNCATE) { | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5037 | SDValue NarrowLoad = ReduceLoadWidth(N0.getNode()); |
5038 | if (NarrowLoad.getNode()) { | ||||
Dale Johannesen | 86234c3 | 2010-05-25 18:47:23 +0000 | [diff] [blame] | 5039 | SDNode* oye = N0.getNode()->getOperand(0).getNode(); |
5040 | if (NarrowLoad.getNode() != N0.getNode()) { | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5041 | CombineTo(N0.getNode(), NarrowLoad); |
Dale Johannesen | 86234c3 | 2010-05-25 18:47:23 +0000 | [diff] [blame] | 5042 | // CombineTo deleted the truncate, if needed, but not what's under it. |
5043 | AddToWorkList(oye); | ||||
5044 | } | ||||
Eli Friedman | e545d38 | 2011-04-16 23:25:34 +0000 | [diff] [blame] | 5045 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 0b063de | 2007-03-23 02:16:52 +0000 | [diff] [blame] | 5046 | } |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5047 | } |
5048 | |||||
Chris Lattner | 8475058 | 2006-09-20 06:29:17 +0000 | [diff] [blame] | 5049 | // fold (aext (truncate x)) |
5050 | if (N0.getOpcode() == ISD::TRUNCATE) { | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5051 | SDValue TruncOp = N0.getOperand(0); |
Chris Lattner | 8475058 | 2006-09-20 06:29:17 +0000 | [diff] [blame] | 5052 | if (TruncOp.getValueType() == VT) |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 5053 | return TruncOp; // x iff x size == zext size. |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 5054 | if (TruncOp.getValueType().bitsGT(VT)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5055 | return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, TruncOp); |
5056 | return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, TruncOp); | ||||
Chris Lattner | 8475058 | 2006-09-20 06:29:17 +0000 | [diff] [blame] | 5057 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5058 | |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 5059 | // Fold (aext (and (trunc x), cst)) -> (and x, cst) |
5060 | // if the trunc is not free. | ||||
Chris Lattner | 0e4b922 | 2006-09-21 06:40:43 +0000 | [diff] [blame] | 5061 | if (N0.getOpcode() == ISD::AND && |
5062 | N0.getOperand(0).getOpcode() == ISD::TRUNCATE && | ||||
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 5063 | N0.getOperand(1).getOpcode() == ISD::Constant && |
5064 | !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(), | ||||
5065 | N0.getValueType())) { | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5066 | SDValue X = N0.getOperand(0).getOperand(0); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 5067 | if (X.getValueType().bitsLT(VT)) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5068 | X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, X); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 5069 | } else if (X.getValueType().bitsGT(VT)) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5070 | X = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, X); |
Chris Lattner | 0e4b922 | 2006-09-21 06:40:43 +0000 | [diff] [blame] | 5071 | } |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 5072 | APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 5073 | Mask = Mask.zext(VT.getSizeInBits()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5074 | return DAG.getNode(ISD::AND, SDLoc(N), VT, |
Bill Wendling | 683c957 | 2009-01-30 22:27:33 +0000 | [diff] [blame] | 5075 | X, DAG.getConstant(Mask, VT)); |
Chris Lattner | 0e4b922 | 2006-09-21 06:40:43 +0000 | [diff] [blame] | 5076 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5077 | |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 5078 | // fold (aext (load x)) -> (aext (truncate (extload x))) |
Nadav Rotem | 8c20ec5 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 5079 | // None of the supported targets knows how to perform load and any_ext |
Nadav Rotem | fcd9619 | 2011-02-27 07:40:43 +0000 | [diff] [blame] | 5080 | // on vectors in one instruction. We only perform this transformation on |
5081 | // scalars. | ||||
Nadav Rotem | 8c20ec5 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 5082 | if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5083 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 5084 | TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) { |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 5085 | bool DoXform = true; |
5086 | SmallVector<SDNode*, 4> SetCCs; | ||||
5087 | if (!N0.hasOneUse()) | ||||
5088 | DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI); | ||||
5089 | if (DoXform) { | ||||
5090 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5091 | SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT, |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 5092 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 5093 | LN0->getBasePtr(), LN0->getPointerInfo(), |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 5094 | N0.getValueType(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5095 | LN0->isVolatile(), LN0->isNonTemporal(), |
5096 | LN0->getAlignment()); | ||||
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 5097 | CombineTo(N, ExtLoad); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5098 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 5099 | N0.getValueType(), ExtLoad); |
5100 | CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1)); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5101 | ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N), |
Nick Lewycky | c06b5bf | 2011-06-16 01:15:49 +0000 | [diff] [blame] | 5102 | ISD::ANY_EXTEND); |
Dan Gohman | 57fc82d | 2009-04-09 03:51:29 +0000 | [diff] [blame] | 5103 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
5104 | } | ||||
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 5105 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5106 | |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 5107 | // fold (aext (zextload x)) -> (aext (truncate (zextload x))) |
5108 | // fold (aext (sextload x)) -> (aext (truncate (sextload x))) | ||||
5109 | // fold (aext ( extload x)) -> (aext (truncate (extload x))) | ||||
Evan Cheng | 83060c5 | 2007-03-07 08:07:03 +0000 | [diff] [blame] | 5110 | if (N0.getOpcode() == ISD::LOAD && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5111 | !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) && |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5112 | N0.hasOneUse()) { |
5113 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); | ||||
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 5114 | EVT MemVT = LN0->getMemoryVT(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5115 | SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), SDLoc(N), |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 5116 | VT, LN0->getChain(), LN0->getBasePtr(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 5117 | LN0->getPointerInfo(), MemVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5118 | LN0->isVolatile(), LN0->isNonTemporal(), |
5119 | LN0->getAlignment()); | ||||
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 5120 | CombineTo(N, ExtLoad); |
Evan Cheng | 4529966 | 2008-08-29 23:20:46 +0000 | [diff] [blame] | 5121 | CombineTo(N0.getNode(), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5122 | DAG.getNode(ISD::TRUNCATE, SDLoc(N0), |
Bill Wendling | 683c957 | 2009-01-30 22:27:33 +0000 | [diff] [blame] | 5123 | N0.getValueType(), ExtLoad), |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 5124 | ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5125 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 5126 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5127 | |
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 5128 | if (N0.getOpcode() == ISD::SETCC) { |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 5129 | // aext(setcc) -> sext_in_reg(vsetcc) for vectors. |
5130 | // Only do this before legalize for now. | ||||
5131 | if (VT.isVector() && !LegalOperations) { | ||||
5132 | EVT N0VT = N0.getOperand(0).getValueType(); | ||||
5133 | // We know that the # elements of the results is the same as the | ||||
5134 | // # elements of the compare (and the # elements of the compare result | ||||
5135 | // for that matter). Check to see that they are the same size. If so, | ||||
5136 | // we know that the element size of the sext'd result matches the | ||||
5137 | // element size of the compare operands. | ||||
5138 | if (VT.getSizeInBits() == N0VT.getSizeInBits()) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5139 | return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0), |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 5140 | N0.getOperand(1), |
5141 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); | ||||
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 5142 | // If the desired elements are smaller or larger than the source |
5143 | // elements we can use a matching integer vector type and then | ||||
5144 | // truncate/sign extend | ||||
5145 | else { | ||||
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 5146 | EVT MatchingElementType = |
5147 | EVT::getIntegerVT(*DAG.getContext(), | ||||
5148 | N0VT.getScalarType().getSizeInBits()); | ||||
5149 | EVT MatchingVectorType = | ||||
5150 | EVT::getVectorVT(*DAG.getContext(), MatchingElementType, | ||||
5151 | N0VT.getVectorNumElements()); | ||||
5152 | SDValue VsetCC = | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5153 | DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0), |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 5154 | N0.getOperand(1), |
5155 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5156 | return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT); |
Evan Cheng | 0a942db | 2010-05-19 01:08:17 +0000 | [diff] [blame] | 5157 | } |
5158 | } | ||||
5159 | |||||
5160 | // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5161 | SDValue SCC = |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5162 | SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1), |
Chris Lattner | 1eba01e | 2007-04-11 06:50:51 +0000 | [diff] [blame] | 5163 | DAG.getConstant(1, VT), DAG.getConstant(0, VT), |
Chris Lattner | c24bbad | 2007-04-11 16:51:53 +0000 | [diff] [blame] | 5164 | cast<CondCodeSDNode>(N0.getOperand(2))->get(), true); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5165 | if (SCC.getNode()) |
Chris Lattner | c56a81d | 2007-04-11 06:43:25 +0000 | [diff] [blame] | 5166 | return SCC; |
Chris Lattner | 20a35c3 | 2007-04-11 05:32:27 +0000 | [diff] [blame] | 5167 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5168 | |
Evan Cheng | b3a3d5e | 2010-04-28 07:10:39 +0000 | [diff] [blame] | 5169 | return SDValue(); |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 5170 | } |
5171 | |||||
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 5172 | /// GetDemandedBits - See if the specified operand can be simplified with the |
5173 | /// knowledge that only the bits specified by Mask are used. If so, return the | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5174 | /// simpler operand, otherwise return a null SDValue. |
5175 | SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) { | ||||
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 5176 | switch (V.getOpcode()) { |
5177 | default: break; | ||||
Lang Hames | 5207bf2 | 2011-11-08 18:56:23 +0000 | [diff] [blame] | 5178 | case ISD::Constant: { |
5179 | const ConstantSDNode *CV = cast<ConstantSDNode>(V.getNode()); | ||||
5180 | assert(CV != 0 && "Const value should be ConstSDNode."); | ||||
5181 | const APInt &CVal = CV->getAPIntValue(); | ||||
5182 | APInt NewVal = CVal & Mask; | ||||
Stephen Lin | b494015 | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 5183 | if (NewVal != CVal) |
Lang Hames | 5207bf2 | 2011-11-08 18:56:23 +0000 | [diff] [blame] | 5184 | return DAG.getConstant(NewVal, V.getValueType()); |
Lang Hames | 5207bf2 | 2011-11-08 18:56:23 +0000 | [diff] [blame] | 5185 | break; |
5186 | } | ||||
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 5187 | case ISD::OR: |
5188 | case ISD::XOR: | ||||
5189 | // If the LHS or RHS don't contribute bits to the or, drop them. | ||||
5190 | if (DAG.MaskedValueIsZero(V.getOperand(0), Mask)) | ||||
5191 | return V.getOperand(1); | ||||
5192 | if (DAG.MaskedValueIsZero(V.getOperand(1), Mask)) | ||||
5193 | return V.getOperand(0); | ||||
5194 | break; | ||||
Chris Lattner | e33544c | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 5195 | case ISD::SRL: |
5196 | // Only look at single-use SRLs. | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5197 | if (!V.getNode()->hasOneUse()) |
Chris Lattner | e33544c | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 5198 | break; |
5199 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) { | ||||
5200 | // See if we can recursively simplify the LHS. | ||||
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5201 | unsigned Amt = RHSC->getZExtValue(); |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5202 | |
Dan Gohman | cc91d63 | 2009-01-03 19:22:06 +0000 | [diff] [blame] | 5203 | // Watch out for shift count overflow though. |
5204 | if (Amt >= Mask.getBitWidth()) break; | ||||
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 5205 | APInt NewMask = Mask << Amt; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5206 | SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask); |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5207 | if (SimplifyLHS.getNode()) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5208 | return DAG.getNode(ISD::SRL, SDLoc(V), V.getValueType(), |
Chris Lattner | e33544c | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 5209 | SimplifyLHS, V.getOperand(1)); |
Chris Lattner | e33544c | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 5210 | } |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 5211 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5212 | return SDValue(); |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 5213 | } |
5214 | |||||
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5215 | /// ReduceLoadWidth - If the result of a wider load is shifted to right of N |
5216 | /// bits and then truncated to a narrower type and where N is a multiple | ||||
5217 | /// of number of bits of the narrower type, transform it to a narrower load | ||||
5218 | /// from address + N / num of bits of new type. If the result is to be | ||||
5219 | /// extended, also fold the extension to form a extending load. | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5220 | SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) { |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5221 | unsigned Opc = N->getOpcode(); |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 5222 | |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5223 | ISD::LoadExtType ExtType = ISD::NON_EXTLOAD; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5224 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5225 | EVT VT = N->getValueType(0); |
5226 | EVT ExtVT = VT; | ||||
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5227 | |
Dan Gohman | 7f8613e | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 5228 | // This transformation isn't valid for vector loads. |
5229 | if (VT.isVector()) | ||||
5230 | return SDValue(); | ||||
5231 | |||||
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 5232 | // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then |
Evan Cheng | e177e30 | 2007-03-23 22:13:36 +0000 | [diff] [blame] | 5233 | // extended to VT. |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5234 | if (Opc == ISD::SIGN_EXTEND_INREG) { |
5235 | ExtType = ISD::SEXTLOAD; | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5236 | ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT(); |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 5237 | } else if (Opc == ISD::SRL) { |
Chris Lattner | 90b0364 | 2010-12-21 18:05:22 +0000 | [diff] [blame] | 5238 | // Another special-case: SRL is basically zero-extending a narrower value. |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 5239 | ExtType = ISD::ZEXTLOAD; |
5240 | N0 = SDValue(N, 0); | ||||
5241 | ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1)); | ||||
5242 | if (!N01) return SDValue(); | ||||
5243 | ExtVT = EVT::getIntegerVT(*DAG.getContext(), | ||||
5244 | VT.getSizeInBits() - N01->getZExtValue()); | ||||
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5245 | } |
Richard Osborne | 4e3740e | 2011-01-31 17:41:44 +0000 | [diff] [blame] | 5246 | if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT)) |
5247 | return SDValue(); | ||||
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5248 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5249 | unsigned EVTBits = ExtVT.getSizeInBits(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5250 | |
Chris Lattner | 7a2a7fa | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5251 | // Do not generate loads of non-round integer types since these can |
5252 | // be expensive (and would be wrong if the type is not byte sized). | ||||
5253 | if (!ExtVT.isRound()) | ||||
5254 | return SDValue(); | ||||
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5255 | |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5256 | unsigned ShAmt = 0; |
Chris Lattner | 7a2a7fa | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5257 | if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) { |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5258 | if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5259 | ShAmt = N01->getZExtValue(); |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5260 | // Is the shift amount a multiple of size of VT? |
5261 | if ((ShAmt & (EVTBits-1)) == 0) { | ||||
5262 | N0 = N0.getOperand(0); | ||||
Eli Friedman | d68eea2 | 2009-08-19 08:46:10 +0000 | [diff] [blame] | 5263 | // Is the load width a multiple of size of VT? |
5264 | if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0) | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5265 | return SDValue(); |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5266 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5267 | |
Chris Lattner | cbf68df | 2010-12-22 08:02:57 +0000 | [diff] [blame] | 5268 | // At this point, we must have a load or else we can't do the transform. |
5269 | if (!isa<LoadSDNode>(N0)) return SDValue(); | ||||
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5270 | |
Chandler Carruth | 1c49fda | 2012-12-11 00:36:57 +0000 | [diff] [blame] | 5271 | // Because a SRL must be assumed to *need* to zero-extend the high bits |
5272 | // (as opposed to anyext the high bits), we can't combine the zextload | ||||
5273 | // lowering of SRL and an sextload. | ||||
5274 | if (cast<LoadSDNode>(N0)->getExtensionType() == ISD::SEXTLOAD) | ||||
5275 | return SDValue(); | ||||
5276 | |||||
Chris Lattner | 2831a19 | 2010-10-01 05:36:09 +0000 | [diff] [blame] | 5277 | // If the shift amount is larger than the input type then we're not |
5278 | // accessing any of the loaded bytes. If the load was a zextload/extload | ||||
5279 | // then the result of the shift+trunc is zero/undef (handled elsewhere). | ||||
Chris Lattner | cbf68df | 2010-12-22 08:02:57 +0000 | [diff] [blame] | 5280 | if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits()) |
Chris Lattner | 2831a19 | 2010-10-01 05:36:09 +0000 | [diff] [blame] | 5281 | return SDValue(); |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5282 | } |
5283 | } | ||||
5284 | |||||
Dan Gohman | 394d629 | 2010-11-03 01:47:46 +0000 | [diff] [blame] | 5285 | // If the load is shifted left (and the result isn't shifted back right), |
5286 | // we can fold the truncate through the shift. | ||||
5287 | unsigned ShLeftAmt = 0; | ||||
5288 | if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() && | ||||
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5289 | ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) { |
Dan Gohman | 394d629 | 2010-11-03 01:47:46 +0000 | [diff] [blame] | 5290 | if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
5291 | ShLeftAmt = N01->getZExtValue(); | ||||
5292 | N0 = N0.getOperand(0); | ||||
5293 | } | ||||
5294 | } | ||||
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5295 | |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5296 | // If we haven't found a load, we can't narrow it. Don't transform one with |
5297 | // multiple uses, this would require adding a new load. | ||||
Bill Schmidt | 89e88e3 | 2013-01-14 22:04:38 +0000 | [diff] [blame] | 5298 | if (!isa<LoadSDNode>(N0) || !N0.hasOneUse()) |
5299 | return SDValue(); | ||||
5300 | |||||
5301 | // Don't change the width of a volatile load. | ||||
5302 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); | ||||
5303 | if (LN0->isVolatile()) | ||||
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5304 | return SDValue(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5305 | |
Chris Lattner | 7a2a7fa | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5306 | // Verify that we are actually reducing a load width here. |
Bill Schmidt | 89e88e3 | 2013-01-14 22:04:38 +0000 | [diff] [blame] | 5307 | if (LN0->getMemoryVT().getSizeInBits() < EVTBits) |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5308 | return SDValue(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5309 | |
Bill Schmidt | 89e88e3 | 2013-01-14 22:04:38 +0000 | [diff] [blame] | 5310 | // For the transform to be legal, the load must produce only two values |
5311 | // (the value loaded and the chain). Don't transform a pre-increment | ||||
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 5312 | // load, for example, which produces an extra value. Otherwise the |
Bill Schmidt | 89e88e3 | 2013-01-14 22:04:38 +0000 | [diff] [blame] | 5313 | // transformation is not equivalent, and the downstream logic to replace |
5314 | // uses gets things wrong. | ||||
5315 | if (LN0->getNumValues() > 2) | ||||
5316 | return SDValue(); | ||||
5317 | |||||
Benjamin Kramer | f4eeab4 | 2013-07-06 14:05:09 +0000 | [diff] [blame] | 5318 | // If the load that we're shrinking is an extload and we're not just |
5319 | // discarding the extension we can't simply shrink the load. Bail. | ||||
5320 | // TODO: It would be possible to merge the extensions in some cases. | ||||
5321 | if (LN0->getExtensionType() != ISD::NON_EXTLOAD && | ||||
5322 | LN0->getMemoryVT().getSizeInBits() < ExtVT.getSizeInBits() + ShAmt) | ||||
5323 | return SDValue(); | ||||
5324 | |||||
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5325 | EVT PtrType = N0.getOperand(1).getValueType(); |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5326 | |
Evan Cheng | 16436df | 2012-06-26 01:19:33 +0000 | [diff] [blame] | 5327 | if (PtrType == MVT::Untyped || PtrType.isExtended()) |
5328 | // It's not possible to generate a constant of extended or untyped type. | ||||
5329 | return SDValue(); | ||||
5330 | |||||
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5331 | // For big endian targets, we need to adjust the offset to the pointer to |
5332 | // load the correct bytes. | ||||
5333 | if (TLI.isBigEndian()) { | ||||
5334 | unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits(); | ||||
5335 | unsigned EVTStoreBits = ExtVT.getStoreSizeInBits(); | ||||
5336 | ShAmt = LVTStoreBits - EVTStoreBits - ShAmt; | ||||
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5337 | } |
5338 | |||||
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5339 | uint64_t PtrOff = ShAmt / 8; |
5340 | unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5341 | SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0), |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5342 | PtrType, LN0->getBasePtr(), |
5343 | DAG.getConstant(PtrOff, PtrType)); | ||||
5344 | AddToWorkList(NewPtr.getNode()); | ||||
5345 | |||||
Chris Lattner | 7a2a7fa | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5346 | SDValue Load; |
5347 | if (ExtType == ISD::NON_EXTLOAD) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5348 | Load = DAG.getLoad(VT, SDLoc(N0), LN0->getChain(), NewPtr, |
Chris Lattner | 7a2a7fa | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5349 | LN0->getPointerInfo().getWithOffset(PtrOff), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 5350 | LN0->isVolatile(), LN0->isNonTemporal(), |
5351 | LN0->isInvariant(), NewAlign); | ||||
Chris Lattner | 7a2a7fa | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5352 | else |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5353 | Load = DAG.getExtLoad(ExtType, SDLoc(N0), VT, LN0->getChain(),NewPtr, |
Chris Lattner | 7a2a7fa | 2010-12-22 08:01:44 +0000 | [diff] [blame] | 5354 | LN0->getPointerInfo().getWithOffset(PtrOff), |
5355 | ExtVT, LN0->isVolatile(), LN0->isNonTemporal(), | ||||
5356 | NewAlign); | ||||
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5357 | |
5358 | // Replace the old load's chain with the new load's chain. | ||||
5359 | WorkListRemover DeadNodes(*this); | ||||
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 5360 | DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1)); |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5361 | |
5362 | // Shift the result left, if we've swallowed a left shift. | ||||
5363 | SDValue Result = Load; | ||||
5364 | if (ShLeftAmt != 0) { | ||||
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 5365 | EVT ShImmTy = getShiftAmountTy(Result.getValueType()); |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5366 | if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt)) |
5367 | ShImmTy = VT; | ||||
Paul Redmond | 5c97450 | 2013-02-12 15:21:21 +0000 | [diff] [blame] | 5368 | // If the shift amount is as large as the result size (but, presumably, |
5369 | // no larger than the source) then the useful bits of the result are | ||||
5370 | // zero; we can't simply return the shortened shift, because the result | ||||
5371 | // of that operation is undefined. | ||||
5372 | if (ShLeftAmt >= VT.getSizeInBits()) | ||||
5373 | Result = DAG.getConstant(0, VT); | ||||
5374 | else | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5375 | Result = DAG.getNode(ISD::SHL, SDLoc(N0), VT, |
Paul Redmond | 5c97450 | 2013-02-12 15:21:21 +0000 | [diff] [blame] | 5376 | Result, DAG.getConstant(ShLeftAmt, ShImmTy)); |
Chris Lattner | 4c32bc2 | 2010-12-22 07:36:50 +0000 | [diff] [blame] | 5377 | } |
5378 | |||||
5379 | // Return the new loaded value. | ||||
5380 | return Result; | ||||
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5381 | } |
5382 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5383 | SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) { |
5384 | SDValue N0 = N->getOperand(0); | ||||
5385 | SDValue N1 = N->getOperand(1); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5386 | EVT VT = N->getValueType(0); |
5387 | EVT EVT = cast<VTSDNode>(N1)->getVT(); | ||||
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 5388 | unsigned VTBits = VT.getScalarType().getSizeInBits(); |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 5389 | unsigned EVTBits = EVT.getScalarType().getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5390 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5391 | // fold (sext_in_reg c1) -> c1 |
Chris Lattner | eaeda56 | 2006-05-08 20:59:41 +0000 | [diff] [blame] | 5392 | if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5393 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, N0, N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5394 | |
Chris Lattner | 541a24f | 2006-05-06 22:43:44 +0000 | [diff] [blame] | 5395 | // If the input is already sign extended, just drop the extension. |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 5396 | if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1) |
Chris Lattner | ee4ea92 | 2006-05-06 09:30:03 +0000 | [diff] [blame] | 5397 | return N0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5398 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 5399 | // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2 |
5400 | if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG && | ||||
Stephen Lin | b494015 | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 5401 | EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5402 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5403 | N0.getOperand(0), N1); |
Chris Lattner | 4b37e87 | 2006-05-08 21:18:59 +0000 | [diff] [blame] | 5404 | |
Dan Gohman | 75dcf08 | 2008-07-31 00:50:31 +0000 | [diff] [blame] | 5405 | // fold (sext_in_reg (sext x)) -> (sext x) |
5406 | // fold (sext_in_reg (aext x)) -> (sext x) | ||||
5407 | // if x is small enough. | ||||
5408 | if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) { | ||||
5409 | SDValue N00 = N0.getOperand(0); | ||||
Evan Cheng | 003d7c4 | 2010-04-16 22:26:19 +0000 | [diff] [blame] | 5410 | if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits && |
5411 | (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT))) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5412 | return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N00, N1); |
Dan Gohman | 75dcf08 | 2008-07-31 00:50:31 +0000 | [diff] [blame] | 5413 | } |
5414 | |||||
Chris Lattner | 95a5e05 | 2007-04-17 19:03:21 +0000 | [diff] [blame] | 5415 | // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero. |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 5416 | if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits))) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5417 | return DAG.getZeroExtendInReg(N0, SDLoc(N), EVT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5418 | |
Chris Lattner | 95a5e05 | 2007-04-17 19:03:21 +0000 | [diff] [blame] | 5419 | // fold operands of sext_in_reg based on knowledge that the top bits are not |
5420 | // demanded. | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5421 | if (SimplifyDemandedBits(SDValue(N, 0))) |
5422 | return SDValue(N, 0); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5423 | |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5424 | // fold (sext_in_reg (load x)) -> (smaller sextload x) |
5425 | // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits)) | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5426 | SDValue NarrowLoad = ReduceLoadWidth(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5427 | if (NarrowLoad.getNode()) |
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5428 | return NarrowLoad; |
5429 | |||||
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5430 | // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24) |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 5431 | // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible. |
Chris Lattner | 4b37e87 | 2006-05-08 21:18:59 +0000 | [diff] [blame] | 5432 | // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above. |
5433 | if (N0.getOpcode() == ISD::SRL) { | ||||
5434 | if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1))) | ||||
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 5435 | if (ShAmt->getZExtValue()+EVTBits <= VTBits) { |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 5436 | // We can turn this into an SRA iff the input to the SRL is already sign |
Chris Lattner | 4b37e87 | 2006-05-08 21:18:59 +0000 | [diff] [blame] | 5437 | // extended enough. |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 5438 | unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0)); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 5439 | if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5440 | return DAG.getNode(ISD::SRA, SDLoc(N), VT, |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5441 | N0.getOperand(0), N0.getOperand(1)); |
Chris Lattner | 4b37e87 | 2006-05-08 21:18:59 +0000 | [diff] [blame] | 5442 | } |
5443 | } | ||||
Evan Cheng | c88138f | 2007-03-22 01:54:19 +0000 | [diff] [blame] | 5444 | |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 5445 | // fold (sext_inreg (extload x)) -> (sextload x) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5446 | if (ISD::isEXTLoad(N0.getNode()) && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5447 | ISD::isUNINDEXEDLoad(N0.getNode()) && |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 5448 | EVT == cast<LoadSDNode>(N0)->getMemoryVT() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5449 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 5450 | TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5451 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5452 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT, |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5453 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 5454 | LN0->getBasePtr(), LN0->getPointerInfo(), |
5455 | EVT, | ||||
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5456 | LN0->isVolatile(), LN0->isNonTemporal(), |
5457 | LN0->getAlignment()); | ||||
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 5458 | CombineTo(N, ExtLoad); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5459 | CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); |
Elena Demikhovsky | 4b97731 | 2012-12-19 07:50:20 +0000 | [diff] [blame] | 5460 | AddToWorkList(ExtLoad.getNode()); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5461 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 5462 | } |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 5463 | // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5464 | if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) && |
Evan Cheng | 83060c5 | 2007-03-07 08:07:03 +0000 | [diff] [blame] | 5465 | N0.hasOneUse() && |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 5466 | EVT == cast<LoadSDNode>(N0)->getMemoryVT() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5467 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 5468 | TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5469 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5470 | SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT, |
Bill Wendling | 8509c90 | 2009-01-30 22:33:24 +0000 | [diff] [blame] | 5471 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 5472 | LN0->getBasePtr(), LN0->getPointerInfo(), |
5473 | EVT, | ||||
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5474 | LN0->isVolatile(), LN0->isNonTemporal(), |
5475 | LN0->getAlignment()); | ||||
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 5476 | CombineTo(N, ExtLoad); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5477 | CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5478 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 5479 | } |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 5480 | |
5481 | // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16)) | ||||
5482 | if (EVTBits <= 16 && N0.getOpcode() == ISD::OR) { | ||||
5483 | SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0), | ||||
5484 | N0.getOperand(1), false); | ||||
5485 | if (BSwap.getNode() != 0) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5486 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 5487 | BSwap, N1); |
5488 | } | ||||
5489 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5490 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5491 | } |
5492 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5493 | SDValue DAGCombiner::visitTRUNCATE(SDNode *N) { |
5494 | SDValue N0 = N->getOperand(0); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5495 | EVT VT = N->getValueType(0); |
Nadav Rotem | 7e413e9c | 2012-02-03 13:18:25 +0000 | [diff] [blame] | 5496 | bool isLE = TLI.isLittleEndian(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5497 | |
5498 | // noop truncate | ||||
5499 | if (N0.getValueType() == N->getValueType(0)) | ||||
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 5500 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5501 | // fold (truncate c1) -> c1 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 5502 | if (isa<ConstantSDNode>(N0)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5503 | return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5504 | // fold (truncate (truncate x)) -> (truncate x) |
5505 | if (N0.getOpcode() == ISD::TRUNCATE) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5506 | return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5507 | // fold (truncate (ext x)) -> (ext x) or (truncate x) or x |
Chris Lattner | 7f893c0 | 2010-04-07 18:13:33 +0000 | [diff] [blame] | 5508 | if (N0.getOpcode() == ISD::ZERO_EXTEND || |
5509 | N0.getOpcode() == ISD::SIGN_EXTEND || | ||||
Chris Lattner | b72773b | 2006-05-05 22:56:26 +0000 | [diff] [blame] | 5510 | N0.getOpcode() == ISD::ANY_EXTEND) { |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 5511 | if (N0.getOperand(0).getValueType().bitsLT(VT)) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5512 | // if the source is smaller than the dest, we still need an extend |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5513 | return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5514 | N0.getOperand(0)); |
Craig Topper | 0eb5dad | 2012-09-29 07:18:53 +0000 | [diff] [blame] | 5515 | if (N0.getOperand(0).getValueType().bitsGT(VT)) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5516 | // if the source is larger than the dest, than we just need the truncate |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5517 | return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0)); |
Craig Topper | 0eb5dad | 2012-09-29 07:18:53 +0000 | [diff] [blame] | 5518 | // if the source and dest are the same type, we can drop both the extend |
5519 | // and the truncate. | ||||
5520 | return N0.getOperand(0); | ||||
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5521 | } |
Evan Cheng | 007b69e | 2007-03-21 20:14:05 +0000 | [diff] [blame] | 5522 | |
Nadav Rotem | cc870a8 | 2012-02-05 11:39:23 +0000 | [diff] [blame] | 5523 | // Fold extract-and-trunc into a narrow extract. For example: |
5524 | // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1) | ||||
5525 | // i32 y = TRUNCATE(i64 x) | ||||
5526 | // -- becomes -- | ||||
5527 | // v16i8 b = BITCAST (v2i64 val) | ||||
5528 | // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8) | ||||
5529 | // | ||||
5530 | // Note: We only run this optimization after type legalization (which often | ||||
Nadav Rotem | 7e413e9c | 2012-02-03 13:18:25 +0000 | [diff] [blame] | 5531 | // creates this pattern) and before operation legalization after which |
5532 | // we need to be more careful about the vector instructions that we generate. | ||||
5533 | if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT && | ||||
5534 | LegalTypes && !LegalOperations && N0->hasOneUse()) { | ||||
5535 | |||||
5536 | EVT VecTy = N0.getOperand(0).getValueType(); | ||||
5537 | EVT ExTy = N0.getValueType(); | ||||
5538 | EVT TrTy = N->getValueType(0); | ||||
5539 | |||||
5540 | unsigned NumElem = VecTy.getVectorNumElements(); | ||||
5541 | unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits(); | ||||
5542 | |||||
5543 | EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, SizeRatio * NumElem); | ||||
5544 | assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size"); | ||||
5545 | |||||
5546 | SDValue EltNo = N0->getOperand(1); | ||||
5547 | if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) { | ||||
5548 | int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); | ||||
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 5549 | EVT IndexTy = TLI.getVectorIdxTy(); |
Nadav Rotem | 7e413e9c | 2012-02-03 13:18:25 +0000 | [diff] [blame] | 5550 | int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1)); |
5551 | |||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5552 | SDValue V = DAG.getNode(ISD::BITCAST, SDLoc(N), |
Nadav Rotem | 7e413e9c | 2012-02-03 13:18:25 +0000 | [diff] [blame] | 5553 | NVT, N0.getOperand(0)); |
5554 | |||||
5555 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5556 | SDLoc(N), TrTy, V, |
Jim Grosbach | a249f7d | 2012-05-08 20:56:07 +0000 | [diff] [blame] | 5557 | DAG.getConstant(Index, IndexTy)); |
Nadav Rotem | 7e413e9c | 2012-02-03 13:18:25 +0000 | [diff] [blame] | 5558 | } |
5559 | } | ||||
5560 | |||||
Arnold Schwaighofer | c46e2df | 2013-02-20 21:33:32 +0000 | [diff] [blame] | 5561 | // Fold a series of buildvector, bitcast, and truncate if possible. |
5562 | // For example fold | ||||
5563 | // (2xi32 trunc (bitcast ((4xi32)buildvector x, x, y, y) 2xi64)) to | ||||
5564 | // (2xi32 (buildvector x, y)). | ||||
5565 | if (Level == AfterLegalizeVectorOps && VT.isVector() && | ||||
5566 | N0.getOpcode() == ISD::BITCAST && N0.hasOneUse() && | ||||
5567 | N0.getOperand(0).getOpcode() == ISD::BUILD_VECTOR && | ||||
5568 | N0.getOperand(0).hasOneUse()) { | ||||
5569 | |||||
5570 | SDValue BuildVect = N0.getOperand(0); | ||||
5571 | EVT BuildVectEltTy = BuildVect.getValueType().getVectorElementType(); | ||||
5572 | EVT TruncVecEltTy = VT.getVectorElementType(); | ||||
5573 | |||||
5574 | // Check that the element types match. | ||||
5575 | if (BuildVectEltTy == TruncVecEltTy) { | ||||
5576 | // Now we only need to compute the offset of the truncated elements. | ||||
5577 | unsigned BuildVecNumElts = BuildVect.getNumOperands(); | ||||
5578 | unsigned TruncVecNumElts = VT.getVectorNumElements(); | ||||
5579 | unsigned TruncEltOffset = BuildVecNumElts / TruncVecNumElts; | ||||
5580 | |||||
5581 | assert((BuildVecNumElts % TruncVecNumElts) == 0 && | ||||
5582 | "Invalid number of elements"); | ||||
5583 | |||||
5584 | SmallVector<SDValue, 8> Opnds; | ||||
5585 | for (unsigned i = 0, e = BuildVecNumElts; i != e; i += TruncEltOffset) | ||||
5586 | Opnds.push_back(BuildVect.getOperand(i)); | ||||
5587 | |||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5588 | return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, &Opnds[0], |
Arnold Schwaighofer | c46e2df | 2013-02-20 21:33:32 +0000 | [diff] [blame] | 5589 | Opnds.size()); |
5590 | } | ||||
5591 | } | ||||
5592 | |||||
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 5593 | // See if we can simplify the input to this truncate through knowledge that |
Nadav Rotem | 8c20ec5 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 5594 | // only the low bits are being used. |
5595 | // For example "trunc (or (shl x, 8), y)" // -> trunc y | ||||
Nadav Rotem | fcd9619 | 2011-02-27 07:40:43 +0000 | [diff] [blame] | 5596 | // Currently we only perform this optimization on scalars because vectors |
Nadav Rotem | 8c20ec5 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 5597 | // may have different active low bits. |
5598 | if (!VT.isVector()) { | ||||
5599 | SDValue Shorter = | ||||
5600 | GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(), | ||||
5601 | VT.getSizeInBits())); | ||||
5602 | if (Shorter.getNode()) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5603 | return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Shorter); |
Nadav Rotem | 8c20ec5 | 2011-02-24 21:01:34 +0000 | [diff] [blame] | 5604 | } |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 5605 | // fold (truncate (load x)) -> (smaller load x) |
Evan Cheng | 007b69e | 2007-03-21 20:14:05 +0000 | [diff] [blame] | 5606 | // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits)) |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 5607 | if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) { |
5608 | SDValue Reduced = ReduceLoadWidth(N); | ||||
5609 | if (Reduced.getNode()) | ||||
5610 | return Reduced; | ||||
5611 | } | ||||
Michael Liao | 07edaf3 | 2012-10-17 23:45:54 +0000 | [diff] [blame] | 5612 | // fold (trunc (concat ... x ...)) -> (concat ..., (trunc x), ...)), |
5613 | // where ... are all 'undef'. | ||||
5614 | if (N0.getOpcode() == ISD::CONCAT_VECTORS && !LegalTypes) { | ||||
5615 | SmallVector<EVT, 8> VTs; | ||||
5616 | SDValue V; | ||||
5617 | unsigned Idx = 0; | ||||
5618 | unsigned NumDefs = 0; | ||||
5619 | |||||
5620 | for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) { | ||||
5621 | SDValue X = N0.getOperand(i); | ||||
5622 | if (X.getOpcode() != ISD::UNDEF) { | ||||
5623 | V = X; | ||||
5624 | Idx = i; | ||||
5625 | NumDefs++; | ||||
5626 | } | ||||
5627 | // Stop if more than one members are non-undef. | ||||
5628 | if (NumDefs > 1) | ||||
5629 | break; | ||||
5630 | VTs.push_back(EVT::getVectorVT(*DAG.getContext(), | ||||
5631 | VT.getVectorElementType(), | ||||
5632 | X.getValueType().getVectorNumElements())); | ||||
5633 | } | ||||
5634 | |||||
5635 | if (NumDefs == 0) | ||||
5636 | return DAG.getUNDEF(VT); | ||||
5637 | |||||
5638 | if (NumDefs == 1) { | ||||
5639 | assert(V.getNode() && "The single defined operand is empty!"); | ||||
5640 | SmallVector<SDValue, 8> Opnds; | ||||
5641 | for (unsigned i = 0, e = VTs.size(); i != e; ++i) { | ||||
5642 | if (i != Idx) { | ||||
5643 | Opnds.push_back(DAG.getUNDEF(VTs[i])); | ||||
5644 | continue; | ||||
5645 | } | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5646 | SDValue NV = DAG.getNode(ISD::TRUNCATE, SDLoc(V), VTs[i], V); |
Michael Liao | 07edaf3 | 2012-10-17 23:45:54 +0000 | [diff] [blame] | 5647 | AddToWorkList(NV.getNode()); |
5648 | Opnds.push_back(NV); | ||||
5649 | } | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5650 | return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, |
Michael Liao | 07edaf3 | 2012-10-17 23:45:54 +0000 | [diff] [blame] | 5651 | &Opnds[0], Opnds.size()); |
5652 | } | ||||
5653 | } | ||||
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 5654 | |
5655 | // Simplify the operands using demanded-bits information. | ||||
5656 | if (!VT.isVector() && | ||||
5657 | SimplifyDemandedBits(SDValue(N, 0))) | ||||
5658 | return SDValue(N, 0); | ||||
5659 | |||||
Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 5660 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 5661 | } |
5662 | |||||
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5663 | static SDNode *getBuildPairElt(SDNode *N, unsigned i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5664 | SDValue Elt = N->getOperand(i); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5665 | if (Elt.getOpcode() != ISD::MERGE_VALUES) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5666 | return Elt.getNode(); |
5667 | return Elt.getOperand(Elt.getResNo()).getNode(); | ||||
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5668 | } |
5669 | |||||
5670 | /// CombineConsecutiveLoads - build_pair (load, load) -> load | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5671 | /// if load locations are consecutive. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5672 | SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) { |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5673 | assert(N->getOpcode() == ISD::BUILD_PAIR); |
5674 | |||||
Nate Begeman | abc0199 | 2009-06-05 21:37:30 +0000 | [diff] [blame] | 5675 | LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0)); |
5676 | LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1)); | ||||
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 5677 | if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() || |
5678 | LD1->getPointerInfo().getAddrSpace() != | ||||
5679 | LD2->getPointerInfo().getAddrSpace()) | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5680 | return SDValue(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5681 | EVT LD1VT = LD1->getValueType(0); |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5682 | |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5683 | if (ISD::isNON_EXTLoad(LD2) && |
5684 | LD2->hasOneUse() && | ||||
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 5685 | // If both are volatile this would reduce the number of volatile loads. |
5686 | // If one is volatile it might be ok, but play conservative and bail out. | ||||
Nate Begeman | abc0199 | 2009-06-05 21:37:30 +0000 | [diff] [blame] | 5687 | !LD1->isVolatile() && |
5688 | !LD2->isVolatile() && | ||||
Evan Cheng | 64fa4a9 | 2009-12-09 01:36:00 +0000 | [diff] [blame] | 5689 | DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) { |
Nate Begeman | abc0199 | 2009-06-05 21:37:30 +0000 | [diff] [blame] | 5690 | unsigned Align = LD1->getAlignment(); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 5691 | unsigned NewAlign = TLI.getDataLayout()-> |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5692 | getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext())); |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5693 | |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 5694 | if (NewAlign <= Align && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5695 | (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5696 | return DAG.getLoad(VT, SDLoc(N), LD1->getChain(), |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 5697 | LD1->getBasePtr(), LD1->getPointerInfo(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 5698 | false, false, false, Align); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5699 | } |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5700 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5701 | return SDValue(); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5702 | } |
5703 | |||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5704 | SDValue DAGCombiner::visitBITCAST(SDNode *N) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5705 | SDValue N0 = N->getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5706 | EVT VT = N->getValueType(0); |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 5707 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5708 | // If the input is a BUILD_VECTOR with all constant elements, fold this now. |
5709 | // Only do this before legalize, since afterward the target may be depending | ||||
5710 | // on the bitconvert. | ||||
5711 | // First check to see if this is all constant. | ||||
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5712 | if (!LegalTypes && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5713 | N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5714 | VT.isVector()) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5715 | bool isSimple = true; |
5716 | for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) | ||||
5717 | if (N0.getOperand(i).getOpcode() != ISD::UNDEF && | ||||
5718 | N0.getOperand(i).getOpcode() != ISD::Constant && | ||||
5719 | N0.getOperand(i).getOpcode() != ISD::ConstantFP) { | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5720 | isSimple = false; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5721 | break; |
5722 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5723 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5724 | EVT DestEltVT = N->getValueType(0).getVectorElementType(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5725 | assert(!DestEltVT.isVector() && |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5726 | "Element type of vector ValueType must not be vector!"); |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5727 | if (isSimple) |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5728 | return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5729 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5730 | |
Dan Gohman | 3dd168d | 2008-09-05 01:58:21 +0000 | [diff] [blame] | 5731 | // If the input is a constant, let getNode fold it. |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 5732 | if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5733 | SDValue Res = DAG.getNode(ISD::BITCAST, SDLoc(N), VT, N0); |
Dan Gohman | a407ca1 | 2009-08-10 23:15:10 +0000 | [diff] [blame] | 5734 | if (Res.getNode() != N) { |
5735 | if (!LegalOperations || | ||||
5736 | TLI.isOperationLegal(Res.getNode()->getOpcode(), VT)) | ||||
5737 | return Res; | ||||
5738 | |||||
5739 | // Folding it resulted in an illegal node, and it's too late to | ||||
5740 | // do that. Clean up the old node and forego the transformation. | ||||
5741 | // Ideally this won't happen very often, because instcombine | ||||
5742 | // and the earlier dagcombine runs (where illegal nodes are | ||||
5743 | // permitted) should have folded most of them already. | ||||
5744 | DAG.DeleteNode(Res.getNode()); | ||||
5745 | } | ||||
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 5746 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5747 | |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5748 | // (conv (conv x, t1), t2) -> (conv x, t2) |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5749 | if (N0.getOpcode() == ISD::BITCAST) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5750 | return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5751 | N0.getOperand(0)); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5752 | |
Chris Lattner | 5710410 | 2005-12-23 05:44:41 +0000 | [diff] [blame] | 5753 | // fold (conv (load x)) -> (load (conv*)x) |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 5754 | // If the resultant load doesn't need a higher alignment than the original! |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5755 | if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() && |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 5756 | // Do not change the width of a volatile load. |
5757 | !cast<LoadSDNode>(N0)->isVolatile() && | ||||
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5758 | (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5759 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 5760 | unsigned Align = TLI.getDataLayout()-> |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5761 | getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext())); |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 5762 | unsigned OrigAlign = LN0->getAlignment(); |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5763 | |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 5764 | if (Align <= OrigAlign) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5765 | SDValue Load = DAG.getLoad(VT, SDLoc(N), LN0->getChain(), |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 5766 | LN0->getBasePtr(), LN0->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 5767 | LN0->isVolatile(), LN0->isNonTemporal(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 5768 | LN0->isInvariant(), OrigAlign); |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 5769 | AddToWorkList(N); |
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 5770 | CombineTo(N0.getNode(), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5771 | DAG.getNode(ISD::BITCAST, SDLoc(N0), |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5772 | N0.getValueType(), Load), |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 5773 | Load.getValue(1)); |
5774 | return Load; | ||||
5775 | } | ||||
Chris Lattner | 5710410 | 2005-12-23 05:44:41 +0000 | [diff] [blame] | 5776 | } |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 5777 | |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5778 | // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit) |
5779 | // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit)) | ||||
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5780 | // This often reduces constant pool loads. |
Tom Stellard | 1f67c63 | 2013-07-23 23:55:03 +0000 | [diff] [blame] | 5781 | if (((N0.getOpcode() == ISD::FNEG && !TLI.isFNegFree(N0.getValueType())) || |
5782 | (N0.getOpcode() == ISD::FABS && !TLI.isFAbsFree(N0.getValueType()))) && | ||||
Nadav Rotem | 91a7e01 | 2012-09-13 14:54:28 +0000 | [diff] [blame] | 5783 | N0.getNode()->hasOneUse() && VT.isInteger() && |
5784 | !VT.isVector() && !N0.getValueType().isVector()) { | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5785 | SDValue NewConv = DAG.getNode(ISD::BITCAST, SDLoc(N0), VT, |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5786 | N0.getOperand(0)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5787 | AddToWorkList(NewConv.getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5788 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5789 | APInt SignBit = APInt::getSignBit(VT.getSizeInBits()); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5790 | if (N0.getOpcode() == ISD::FNEG) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5791 | return DAG.getNode(ISD::XOR, SDLoc(N), VT, |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5792 | NewConv, DAG.getConstant(SignBit, VT)); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5793 | assert(N0.getOpcode() == ISD::FABS); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5794 | return DAG.getNode(ISD::AND, SDLoc(N), VT, |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5795 | NewConv, DAG.getConstant(~SignBit, VT)); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5796 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5797 | |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5798 | // fold (bitconvert (fcopysign cst, x)) -> |
5799 | // (or (and (bitconvert x), sign), (and cst, (not sign))) | ||||
5800 | // Note that we don't handle (copysign x, cst) because this can always be | ||||
5801 | // folded to an fneg or fabs. | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5802 | if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() && |
Chris Lattner | f32aac3 | 2008-01-27 23:32:17 +0000 | [diff] [blame] | 5803 | isa<ConstantFPSDNode>(N0.getOperand(0)) && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5804 | VT.isInteger() && !VT.isVector()) { |
5805 | unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits(); | ||||
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5806 | EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth); |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 5807 | if (isTypeLegal(IntXVT)) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5808 | SDValue X = DAG.getNode(ISD::BITCAST, SDLoc(N0), |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5809 | IntXVT, N0.getOperand(1)); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5810 | AddToWorkList(X.getNode()); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5811 | |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5812 | // If X has a different width than the result/lhs, sext it or truncate it. |
5813 | unsigned VTWidth = VT.getSizeInBits(); | ||||
5814 | if (OrigXWidth < VTWidth) { | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5815 | X = DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, X); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5816 | AddToWorkList(X.getNode()); |
5817 | } else if (OrigXWidth > VTWidth) { | ||||
5818 | // To get the sign bit in the right place, we have to shift it right | ||||
5819 | // before truncating. | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5820 | X = DAG.getNode(ISD::SRL, SDLoc(X), |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5821 | X.getValueType(), X, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5822 | DAG.getConstant(OrigXWidth-VTWidth, X.getValueType())); |
5823 | AddToWorkList(X.getNode()); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5824 | X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5825 | AddToWorkList(X.getNode()); |
5826 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5827 | |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5828 | APInt SignBit = APInt::getSignBit(VT.getSizeInBits()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5829 | X = DAG.getNode(ISD::AND, SDLoc(X), VT, |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5830 | X, DAG.getConstant(SignBit, VT)); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5831 | AddToWorkList(X.getNode()); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5832 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5833 | SDValue Cst = DAG.getNode(ISD::BITCAST, SDLoc(N0), |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5834 | VT, N0.getOperand(0)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5835 | Cst = DAG.getNode(ISD::AND, SDLoc(Cst), VT, |
Bill Wendling | 67a6768 | 2009-01-30 22:44:24 +0000 | [diff] [blame] | 5836 | Cst, DAG.getConstant(~SignBit, VT)); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5837 | AddToWorkList(Cst.getNode()); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5838 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5839 | return DAG.getNode(ISD::OR, SDLoc(N), VT, X, Cst); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 5840 | } |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 5841 | } |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5842 | |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 5843 | // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive. |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5844 | if (N0.getOpcode() == ISD::BUILD_PAIR) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5845 | SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT); |
5846 | if (CombineLD.getNode()) | ||||
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5847 | return CombineLD; |
5848 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5849 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5850 | return SDValue(); |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 5851 | } |
5852 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5853 | SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5854 | EVT VT = N->getValueType(0); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 5855 | return CombineConsecutiveLoads(N, VT); |
5856 | } | ||||
5857 | |||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5858 | /// ConstantFoldBITCASTofBUILD_VECTOR - We know that BV is a build_vector |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5859 | /// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5860 | /// destination element value type. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5861 | SDValue DAGCombiner:: |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5862 | ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5863 | EVT SrcEltVT = BV->getValueType(0).getVectorElementType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5864 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5865 | // If this is already the right type, we're done. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5866 | if (SrcEltVT == DstEltVT) return SDValue(BV, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5867 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5868 | unsigned SrcBitSize = SrcEltVT.getSizeInBits(); |
5869 | unsigned DstBitSize = DstEltVT.getSizeInBits(); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5870 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5871 | // If this is a conversion of N elements of one type to N elements of another |
5872 | // type, convert each element. This handles FP<->INT cases. | ||||
5873 | if (SrcBitSize == DstBitSize) { | ||||
Nate Begeman | e0efc21 | 2010-07-27 18:02:18 +0000 | [diff] [blame] | 5874 | EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, |
5875 | BV->getValueType(0).getVectorNumElements()); | ||||
5876 | |||||
5877 | // Due to the FP element handling below calling this routine recursively, | ||||
5878 | // we can end up with a scalar-to-vector node here. | ||||
5879 | if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5880 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT, |
5881 | DAG.getNode(ISD::BITCAST, SDLoc(BV), | ||||
Nate Begeman | e0efc21 | 2010-07-27 18:02:18 +0000 | [diff] [blame] | 5882 | DstEltVT, BV->getOperand(0))); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5883 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5884 | SmallVector<SDValue, 8> Ops; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5885 | for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) { |
Bob Wilson | b1303d0 | 2009-04-13 22:05:19 +0000 | [diff] [blame] | 5886 | SDValue Op = BV->getOperand(i); |
5887 | // If the vector element type is not legal, the BUILD_VECTOR operands | ||||
5888 | // are promoted and implicitly truncated. Make that explicit here. | ||||
Bob Wilson | c885165 | 2009-04-20 17:27:09 +0000 | [diff] [blame] | 5889 | if (Op.getValueType() != SrcEltVT) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5890 | Op = DAG.getNode(ISD::TRUNCATE, SDLoc(BV), SrcEltVT, Op); |
5891 | Ops.push_back(DAG.getNode(ISD::BITCAST, SDLoc(BV), | ||||
Bob Wilson | b1303d0 | 2009-04-13 22:05:19 +0000 | [diff] [blame] | 5892 | DstEltVT, Op)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5893 | AddToWorkList(Ops.back().getNode()); |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 5894 | } |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5895 | return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT, |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 5896 | &Ops[0], Ops.size()); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5897 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5898 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5899 | // Otherwise, we're growing or shrinking the elements. To avoid having to |
5900 | // handle annoying details of growing/shrinking FP values, we convert them to | ||||
5901 | // int first. | ||||
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5902 | if (SrcEltVT.isFloatingPoint()) { |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5903 | // Convert the input float vector to a int vector where the elements are the |
5904 | // same sizes. | ||||
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5905 | assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!"); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5906 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5907 | BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode(); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5908 | SrcEltVT = IntVT; |
5909 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5910 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5911 | // Now we know the input is an integer vector. If the output is a FP type, |
5912 | // convert to integer first, then to FP of the right size. | ||||
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5913 | if (DstEltVT.isFloatingPoint()) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5914 | assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!"); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5915 | EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5916 | SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5917 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5918 | // Next, convert to FP elements of the same size. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5919 | return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5920 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5921 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5922 | // Okay, we know the src/dst types are both integers of differing types. |
5923 | // Handling growing first. | ||||
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5924 | assert(SrcEltVT.isInteger() && DstEltVT.isInteger()); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5925 | if (SrcBitSize < DstBitSize) { |
5926 | unsigned NumInputsPerOutput = DstBitSize/SrcBitSize; | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5927 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5928 | SmallVector<SDValue, 8> Ops; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5929 | for (unsigned i = 0, e = BV->getNumOperands(); i != e; |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5930 | i += NumInputsPerOutput) { |
5931 | bool isLE = TLI.isLittleEndian(); | ||||
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 5932 | APInt NewBits = APInt(DstBitSize, 0); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5933 | bool EltIsUndef = true; |
5934 | for (unsigned j = 0; j != NumInputsPerOutput; ++j) { | ||||
5935 | // Shift the previously computed bits over. | ||||
5936 | NewBits <<= SrcBitSize; | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5937 | SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j)); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5938 | if (Op.getOpcode() == ISD::UNDEF) continue; |
5939 | EltIsUndef = false; | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5940 | |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 5941 | NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue(). |
Dan Gohman | 58c2587 | 2010-04-12 02:24:01 +0000 | [diff] [blame] | 5942 | zextOrTrunc(SrcBitSize).zext(DstBitSize); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5943 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5944 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5945 | if (EltIsUndef) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 5946 | Ops.push_back(DAG.getUNDEF(DstEltVT)); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5947 | else |
5948 | Ops.push_back(DAG.getConstant(NewBits, DstEltVT)); | ||||
5949 | } | ||||
5950 | |||||
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5951 | EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5952 | return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT, |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 5953 | &Ops[0], Ops.size()); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5954 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5955 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5956 | // Finally, this must be the case where we are shrinking elements: each input |
5957 | // turns into multiple outputs. | ||||
Evan Cheng | efec751 | 2008-02-18 23:04:32 +0000 | [diff] [blame] | 5958 | bool isS2V = ISD::isScalarToVector(BV); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5959 | unsigned NumOutputsPerInput = SrcBitSize/DstBitSize; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 5960 | EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, |
5961 | NumOutputsPerInput*BV->getNumOperands()); | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5962 | SmallVector<SDValue, 8> Ops; |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5963 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5964 | for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) { |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5965 | if (BV->getOperand(i).getOpcode() == ISD::UNDEF) { |
5966 | for (unsigned j = 0; j != NumOutputsPerInput; ++j) | ||||
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 5967 | Ops.push_back(DAG.getUNDEF(DstEltVT)); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5968 | continue; |
5969 | } | ||||
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5970 | |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 5971 | APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))-> |
5972 | getAPIntValue().zextOrTrunc(SrcBitSize); | ||||
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5973 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5974 | for (unsigned j = 0; j != NumOutputsPerInput; ++j) { |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 5975 | APInt ThisVal = OpVal.trunc(DstBitSize); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5976 | Ops.push_back(DAG.getConstant(ThisVal, DstEltVT)); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 5977 | if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal) |
Evan Cheng | efec751 | 2008-02-18 23:04:32 +0000 | [diff] [blame] | 5978 | // Simply turn this into a SCALAR_TO_VECTOR of the new type. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5979 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT, |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5980 | Ops[0]); |
Dan Gohman | 220a823 | 2008-03-03 23:51:38 +0000 | [diff] [blame] | 5981 | OpVal = OpVal.lshr(DstBitSize); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5982 | } |
5983 | |||||
5984 | // For big endian targets, swap the order of the pieces of each element. | ||||
Duncan Sands | 0753fc1 | 2008-02-11 10:37:04 +0000 | [diff] [blame] | 5985 | if (TLI.isBigEndian()) |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5986 | std::reverse(Ops.end()-NumOutputsPerInput, Ops.end()); |
5987 | } | ||||
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 5988 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5989 | return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT, |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 5990 | &Ops[0], Ops.size()); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 5991 | } |
5992 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5993 | SDValue DAGCombiner::visitFADD(SDNode *N) { |
5994 | SDValue N0 = N->getOperand(0); | ||||
5995 | SDValue N1 = N->getOperand(1); | ||||
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 5996 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
5997 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5998 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5999 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6000 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6001 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6002 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6003 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 6004 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6005 | |
Lang Hames | 0180694 | 2012-06-14 20:37:15 +0000 | [diff] [blame] | 6006 | // fold (fadd c1, c2) -> c1 + c2 |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6007 | if (N0CFP && N1CFP) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6008 | return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N1); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 6009 | // canonicalize constant to RHS |
6010 | if (N0CFP && !N1CFP) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6011 | return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N0); |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 6012 | // fold (fadd A, 0) -> A |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6013 | if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && |
6014 | N1CFP->getValueAPF().isZero()) | ||||
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 6015 | return N0; |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 6016 | // fold (fadd A, (fneg B)) -> (fsub A, B) |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6017 | if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) && |
Nadav Rotem | 6dfabb6 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 6018 | isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options) == 2) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6019 | return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6020 | GetNegatedExpression(N1, DAG, LegalOperations)); |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 6021 | // fold (fadd (fneg A), B) -> (fsub B, A) |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6022 | if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) && |
Nadav Rotem | 6dfabb6 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 6023 | isNegatibleForFree(N0, LegalOperations, TLI, &DAG.getTarget().Options) == 2) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6024 | return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N1, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6025 | GetNegatedExpression(N0, DAG, LegalOperations)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6026 | |
Chris Lattner | ddae4bd | 2007-01-08 23:04:05 +0000 | [diff] [blame] | 6027 | // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2)) |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6028 | if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && |
6029 | N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() && | ||||
6030 | isa<ConstantFPSDNode>(N0.getOperand(1))) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6031 | return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0.getOperand(0), |
6032 | DAG.getNode(ISD::FADD, SDLoc(N), VT, | ||||
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 6033 | N0.getOperand(1), N1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6034 | |
Shuxin Yang | 1cd1d02 | 2013-03-25 22:52:29 +0000 | [diff] [blame] | 6035 | // No FP constant should be created after legalization as Instruction |
6036 | // Selection pass has hard time in dealing with FP constant. | ||||
6037 | // | ||||
6038 | // We don't need test this condition for transformation like following, as | ||||
6039 | // the DAG being transformed implies it is legal to take FP constant as | ||||
6040 | // operand. | ||||
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 6041 | // |
Shuxin Yang | 1cd1d02 | 2013-03-25 22:52:29 +0000 | [diff] [blame] | 6042 | // (fadd (fmul c, x), x) -> (fmul c+1, x) |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 6043 | // |
Shuxin Yang | 1cd1d02 | 2013-03-25 22:52:29 +0000 | [diff] [blame] | 6044 | bool AllowNewFpConst = (Level < AfterLegalizeDAG); |
6045 | |||||
Owen Anderson | 607ebde | 2012-11-01 02:00:53 +0000 | [diff] [blame] | 6046 | // If allow, fold (fadd (fneg x), x) -> 0.0 |
Shuxin Yang | 1cd1d02 | 2013-03-25 22:52:29 +0000 | [diff] [blame] | 6047 | if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath && |
Stephen Lin | b494015 | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6048 | N0.getOpcode() == ISD::FNEG && N0.getOperand(0) == N1) |
Owen Anderson | 607ebde | 2012-11-01 02:00:53 +0000 | [diff] [blame] | 6049 | return DAG.getConstantFP(0.0, VT); |
Owen Anderson | 607ebde | 2012-11-01 02:00:53 +0000 | [diff] [blame] | 6050 | |
6051 | // If allow, fold (fadd x, (fneg x)) -> 0.0 | ||||
Shuxin Yang | 1cd1d02 | 2013-03-25 22:52:29 +0000 | [diff] [blame] | 6052 | if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath && |
Stephen Lin | b494015 | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6053 | N1.getOpcode() == ISD::FNEG && N1.getOperand(0) == N0) |
Owen Anderson | 607ebde | 2012-11-01 02:00:53 +0000 | [diff] [blame] | 6054 | return DAG.getConstantFP(0.0, VT); |
Owen Anderson | 607ebde | 2012-11-01 02:00:53 +0000 | [diff] [blame] | 6055 | |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6056 | // In unsafe math mode, we can fold chains of FADD's of the same value |
6057 | // into multiplications. This transform is not safe in general because | ||||
6058 | // we are reducing the number of rounding steps. | ||||
6059 | if (DAG.getTarget().Options.UnsafeFPMath && | ||||
6060 | TLI.isOperationLegalOrCustom(ISD::FMUL, VT) && | ||||
6061 | !N0CFP && !N1CFP) { | ||||
6062 | if (N0.getOpcode() == ISD::FMUL) { | ||||
6063 | ConstantFPSDNode *CFP00 = dyn_cast<ConstantFPSDNode>(N0.getOperand(0)); | ||||
6064 | ConstantFPSDNode *CFP01 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1)); | ||||
6065 | |||||
Stephen Lin | 38103d1 | 2013-06-14 18:17:35 +0000 | [diff] [blame] | 6066 | // (fadd (fmul c, x), x) -> (fmul x, c+1) |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6067 | if (CFP00 && !CFP01 && N0.getOperand(1) == N1) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6068 | SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT, |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6069 | SDValue(CFP00, 0), |
6070 | DAG.getConstantFP(1.0, VT)); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6071 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6072 | N1, NewCFP); |
6073 | } | ||||
6074 | |||||
Stephen Lin | 38103d1 | 2013-06-14 18:17:35 +0000 | [diff] [blame] | 6075 | // (fadd (fmul x, c), x) -> (fmul x, c+1) |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6076 | if (CFP01 && !CFP00 && N0.getOperand(0) == N1) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6077 | SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT, |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6078 | SDValue(CFP01, 0), |
6079 | DAG.getConstantFP(1.0, VT)); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6080 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6081 | N1, NewCFP); |
6082 | } | ||||
6083 | |||||
Stephen Lin | 38103d1 | 2013-06-14 18:17:35 +0000 | [diff] [blame] | 6084 | // (fadd (fmul c, x), (fadd x, x)) -> (fmul x, c+2) |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6085 | if (CFP00 && !CFP01 && N1.getOpcode() == ISD::FADD && |
6086 | N1.getOperand(0) == N1.getOperand(1) && | ||||
6087 | N0.getOperand(1) == N1.getOperand(0)) { | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6088 | SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT, |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6089 | SDValue(CFP00, 0), |
6090 | DAG.getConstantFP(2.0, VT)); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6091 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6092 | N0.getOperand(1), NewCFP); |
6093 | } | ||||
6094 | |||||
Stephen Lin | 38103d1 | 2013-06-14 18:17:35 +0000 | [diff] [blame] | 6095 | // (fadd (fmul x, c), (fadd x, x)) -> (fmul x, c+2) |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6096 | if (CFP01 && !CFP00 && N1.getOpcode() == ISD::FADD && |
6097 | N1.getOperand(0) == N1.getOperand(1) && | ||||
6098 | N0.getOperand(0) == N1.getOperand(0)) { | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6099 | SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT, |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6100 | SDValue(CFP01, 0), |
6101 | DAG.getConstantFP(2.0, VT)); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6102 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6103 | N0.getOperand(0), NewCFP); |
6104 | } | ||||
6105 | } | ||||
6106 | |||||
6107 | if (N1.getOpcode() == ISD::FMUL) { | ||||
6108 | ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0)); | ||||
6109 | ConstantFPSDNode *CFP11 = dyn_cast<ConstantFPSDNode>(N1.getOperand(1)); | ||||
6110 | |||||
Stephen Lin | 38103d1 | 2013-06-14 18:17:35 +0000 | [diff] [blame] | 6111 | // (fadd x, (fmul c, x)) -> (fmul x, c+1) |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6112 | if (CFP10 && !CFP11 && N1.getOperand(1) == N0) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6113 | SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT, |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6114 | SDValue(CFP10, 0), |
6115 | DAG.getConstantFP(1.0, VT)); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6116 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6117 | N0, NewCFP); |
6118 | } | ||||
6119 | |||||
Stephen Lin | 38103d1 | 2013-06-14 18:17:35 +0000 | [diff] [blame] | 6120 | // (fadd x, (fmul x, c)) -> (fmul x, c+1) |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6121 | if (CFP11 && !CFP10 && N1.getOperand(0) == N0) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6122 | SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT, |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6123 | SDValue(CFP11, 0), |
6124 | DAG.getConstantFP(1.0, VT)); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6125 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6126 | N0, NewCFP); |
6127 | } | ||||
6128 | |||||
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6129 | |
Stephen Lin | 38103d1 | 2013-06-14 18:17:35 +0000 | [diff] [blame] | 6130 | // (fadd (fadd x, x), (fmul c, x)) -> (fmul x, c+2) |
6131 | if (CFP10 && !CFP11 && N0.getOpcode() == ISD::FADD && | ||||
6132 | N0.getOperand(0) == N0.getOperand(1) && | ||||
6133 | N1.getOperand(1) == N0.getOperand(0)) { | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6134 | SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT, |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6135 | SDValue(CFP10, 0), |
6136 | DAG.getConstantFP(2.0, VT)); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6137 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
Stephen Lin | 38103d1 | 2013-06-14 18:17:35 +0000 | [diff] [blame] | 6138 | N1.getOperand(1), NewCFP); |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6139 | } |
6140 | |||||
Stephen Lin | 38103d1 | 2013-06-14 18:17:35 +0000 | [diff] [blame] | 6141 | // (fadd (fadd x, x), (fmul x, c)) -> (fmul x, c+2) |
6142 | if (CFP11 && !CFP10 && N0.getOpcode() == ISD::FADD && | ||||
6143 | N0.getOperand(0) == N0.getOperand(1) && | ||||
6144 | N1.getOperand(0) == N0.getOperand(0)) { | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6145 | SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT, |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6146 | SDValue(CFP11, 0), |
6147 | DAG.getConstantFP(2.0, VT)); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6148 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
Stephen Lin | 38103d1 | 2013-06-14 18:17:35 +0000 | [diff] [blame] | 6149 | N1.getOperand(0), NewCFP); |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6150 | } |
6151 | } | ||||
6152 | |||||
Shuxin Yang | 1cd1d02 | 2013-03-25 22:52:29 +0000 | [diff] [blame] | 6153 | if (N0.getOpcode() == ISD::FADD && AllowNewFpConst) { |
Shuxin Yang | 98b93e5 | 2013-02-02 00:22:03 +0000 | [diff] [blame] | 6154 | ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N0.getOperand(0)); |
Stephen Lin | a553bed | 2013-06-14 21:33:58 +0000 | [diff] [blame] | 6155 | // (fadd (fadd x, x), x) -> (fmul x, 3.0) |
Shuxin Yang | 98b93e5 | 2013-02-02 00:22:03 +0000 | [diff] [blame] | 6156 | if (!CFP && N0.getOperand(0) == N0.getOperand(1) && |
Stephen Lin | b494015 | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6157 | (N0.getOperand(0) == N1)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6158 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
Shuxin Yang | 98b93e5 | 2013-02-02 00:22:03 +0000 | [diff] [blame] | 6159 | N1, DAG.getConstantFP(3.0, VT)); |
Shuxin Yang | 98b93e5 | 2013-02-02 00:22:03 +0000 | [diff] [blame] | 6160 | } |
6161 | |||||
Shuxin Yang | 1cd1d02 | 2013-03-25 22:52:29 +0000 | [diff] [blame] | 6162 | if (N1.getOpcode() == ISD::FADD && AllowNewFpConst) { |
Shuxin Yang | 98b93e5 | 2013-02-02 00:22:03 +0000 | [diff] [blame] | 6163 | ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0)); |
Stephen Lin | a553bed | 2013-06-14 21:33:58 +0000 | [diff] [blame] | 6164 | // (fadd x, (fadd x, x)) -> (fmul x, 3.0) |
Shuxin Yang | 98b93e5 | 2013-02-02 00:22:03 +0000 | [diff] [blame] | 6165 | if (!CFP10 && N1.getOperand(0) == N1.getOperand(1) && |
Stephen Lin | b494015 | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6166 | N1.getOperand(0) == N0) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6167 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
Shuxin Yang | 98b93e5 | 2013-02-02 00:22:03 +0000 | [diff] [blame] | 6168 | N0, DAG.getConstantFP(3.0, VT)); |
Shuxin Yang | 98b93e5 | 2013-02-02 00:22:03 +0000 | [diff] [blame] | 6169 | } |
6170 | |||||
Stephen Lin | a553bed | 2013-06-14 21:33:58 +0000 | [diff] [blame] | 6171 | // (fadd (fadd x, x), (fadd x, x)) -> (fmul x, 4.0) |
Shuxin Yang | 1cd1d02 | 2013-03-25 22:52:29 +0000 | [diff] [blame] | 6172 | if (AllowNewFpConst && |
6173 | N0.getOpcode() == ISD::FADD && N1.getOpcode() == ISD::FADD && | ||||
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6174 | N0.getOperand(0) == N0.getOperand(1) && |
6175 | N1.getOperand(0) == N1.getOperand(1) && | ||||
Stephen Lin | b494015 | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6176 | N0.getOperand(0) == N1.getOperand(0)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6177 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6178 | N0.getOperand(0), |
6179 | DAG.getConstantFP(4.0, VT)); | ||||
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6180 | } |
6181 | |||||
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6182 | // FADD -> FMA combines: |
Lang Hames | e023141 | 2012-06-22 01:09:09 +0000 | [diff] [blame] | 6183 | if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast || |
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6184 | DAG.getTarget().Options.UnsafeFPMath) && |
Stephen Lin | e54885a | 2013-07-09 18:16:56 +0000 | [diff] [blame] | 6185 | DAG.getTarget().getTargetLowering()->isFMAFasterThanFMulAndFAdd(VT) && |
6186 | (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT))) { | ||||
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6187 | |
6188 | // fold (fadd (fmul x, y), z) -> (fma x, y, z) | ||||
Stephen Lin | b494015 | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6189 | if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse()) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6190 | return DAG.getNode(ISD::FMA, SDLoc(N), VT, |
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6191 | N0.getOperand(0), N0.getOperand(1), N1); |
Owen Anderson | 43da6c7 | 2012-08-30 23:35:16 +0000 | [diff] [blame] | 6192 | |
Michael Liao | b79bff5 | 2012-09-01 04:09:16 +0000 | [diff] [blame] | 6193 | // fold (fadd x, (fmul y, z)) -> (fma y, z, x) |
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6194 | // Note: Commutes FADD operands. |
Stephen Lin | b494015 | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6195 | if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse()) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6196 | return DAG.getNode(ISD::FMA, SDLoc(N), VT, |
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6197 | N1.getOperand(0), N1.getOperand(1), N0); |
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6198 | } |
6199 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6200 | return SDValue(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6201 | } |
6202 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6203 | SDValue DAGCombiner::visitFSUB(SDNode *N) { |
6204 | SDValue N0 = N->getOperand(0); | ||||
6205 | SDValue N1 = N->getOperand(1); | ||||
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 6206 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
6207 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6208 | EVT VT = N->getValueType(0); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6209 | SDLoc dl(N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6210 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6211 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6212 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6213 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6214 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 6215 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6216 | |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 6217 | // fold (fsub c1, c2) -> c1-c2 |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6218 | if (N0CFP && N1CFP) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6219 | return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0, N1); |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 6220 | // fold (fsub A, 0) -> A |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6221 | if (DAG.getTarget().Options.UnsafeFPMath && |
6222 | N1CFP && N1CFP->getValueAPF().isZero()) | ||||
Dan Gohman | a90c8e6 | 2009-01-23 19:10:37 +0000 | [diff] [blame] | 6223 | return N0; |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 6224 | // fold (fsub 0, B) -> -B |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6225 | if (DAG.getTarget().Options.UnsafeFPMath && |
6226 | N0CFP && N0CFP->getValueAPF().isZero()) { | ||||
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6227 | if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options)) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6228 | return GetNegatedExpression(N1, DAG, LegalOperations); |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 6229 | if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT)) |
Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 6230 | return DAG.getNode(ISD::FNEG, dl, VT, N1); |
Dan Gohman | 23ff182 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 6231 | } |
Bill Wendling | b0162f5 | 2009-01-30 22:53:48 +0000 | [diff] [blame] | 6232 | // fold (fsub A, (fneg B)) -> (fadd A, B) |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6233 | if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options)) |
Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 6234 | return DAG.getNode(ISD::FADD, dl, VT, N0, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6235 | GetNegatedExpression(N1, DAG, LegalOperations)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6236 | |
Bill Wendling | 5a89434 | 2012-03-15 05:12:00 +0000 | [diff] [blame] | 6237 | // If 'unsafe math' is enabled, fold |
Owen Anderson | 713e953 | 2012-05-07 20:51:25 +0000 | [diff] [blame] | 6238 | // (fsub x, x) -> 0.0 & |
Bill Wendling | 5a89434 | 2012-03-15 05:12:00 +0000 | [diff] [blame] | 6239 | // (fsub x, (fadd x, y)) -> (fneg y) & |
6240 | // (fsub x, (fadd y, x)) -> (fneg y) | ||||
6241 | if (DAG.getTarget().Options.UnsafeFPMath) { | ||||
Owen Anderson | 713e953 | 2012-05-07 20:51:25 +0000 | [diff] [blame] | 6242 | if (N0 == N1) |
6243 | return DAG.getConstantFP(0.0f, VT); | ||||
6244 | |||||
Bill Wendling | 5a89434 | 2012-03-15 05:12:00 +0000 | [diff] [blame] | 6245 | if (N1.getOpcode() == ISD::FADD) { |
6246 | SDValue N10 = N1->getOperand(0); | ||||
6247 | SDValue N11 = N1->getOperand(1); | ||||
6248 | |||||
6249 | if (N10 == N0 && isNegatibleForFree(N11, LegalOperations, TLI, | ||||
6250 | &DAG.getTarget().Options)) | ||||
6251 | return GetNegatedExpression(N11, DAG, LegalOperations); | ||||
Stephen Lin | 75d1306 | 2013-07-10 20:47:39 +0000 | [diff] [blame] | 6252 | |
Stephen Lin | b494015 | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6253 | if (N11 == N0 && isNegatibleForFree(N10, LegalOperations, TLI, |
6254 | &DAG.getTarget().Options)) | ||||
Bill Wendling | 5a89434 | 2012-03-15 05:12:00 +0000 | [diff] [blame] | 6255 | return GetNegatedExpression(N10, DAG, LegalOperations); |
6256 | } | ||||
6257 | } | ||||
6258 | |||||
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6259 | // FSUB -> FMA combines: |
Lang Hames | e023141 | 2012-06-22 01:09:09 +0000 | [diff] [blame] | 6260 | if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast || |
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6261 | DAG.getTarget().Options.UnsafeFPMath) && |
Stephen Lin | e54885a | 2013-07-09 18:16:56 +0000 | [diff] [blame] | 6262 | DAG.getTarget().getTargetLowering()->isFMAFasterThanFMulAndFAdd(VT) && |
6263 | (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT))) { | ||||
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6264 | |
6265 | // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z)) | ||||
Stephen Lin | b494015 | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6266 | if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse()) |
Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 6267 | return DAG.getNode(ISD::FMA, dl, VT, |
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6268 | N0.getOperand(0), N0.getOperand(1), |
Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 6269 | DAG.getNode(ISD::FNEG, dl, VT, N1)); |
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6270 | |
6271 | // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x) | ||||
6272 | // Note: Commutes FSUB operands. | ||||
Stephen Lin | 75d1306 | 2013-07-10 20:47:39 +0000 | [diff] [blame] | 6273 | if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse()) |
Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 6274 | return DAG.getNode(ISD::FMA, dl, VT, |
6275 | DAG.getNode(ISD::FNEG, dl, VT, | ||||
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6276 | N1.getOperand(0)), |
6277 | N1.getOperand(1), N0); | ||||
Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 6278 | |
Stephen Lin | b494015 | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6279 | // fold (fsub (fneg (fmul, x, y)), z) -> (fma (fneg x), y, (fneg z)) |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 6280 | if (N0.getOpcode() == ISD::FNEG && |
Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 6281 | N0.getOperand(0).getOpcode() == ISD::FMUL && |
6282 | N0->hasOneUse() && N0.getOperand(0).hasOneUse()) { | ||||
6283 | SDValue N00 = N0.getOperand(0).getOperand(0); | ||||
6284 | SDValue N01 = N0.getOperand(0).getOperand(1); | ||||
6285 | return DAG.getNode(ISD::FMA, dl, VT, | ||||
6286 | DAG.getNode(ISD::FNEG, dl, VT, N00), N01, | ||||
6287 | DAG.getNode(ISD::FNEG, dl, VT, N1)); | ||||
6288 | } | ||||
Lang Hames | d693caf | 2012-06-19 22:51:23 +0000 | [diff] [blame] | 6289 | } |
6290 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6291 | return SDValue(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6292 | } |
6293 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6294 | SDValue DAGCombiner::visitFMUL(SDNode *N) { |
6295 | SDValue N0 = N->getOperand(0); | ||||
6296 | SDValue N1 = N->getOperand(1); | ||||
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 6297 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
6298 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6299 | EVT VT = N->getValueType(0); |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6300 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6301 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6302 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6303 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6304 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6305 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 6306 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6307 | |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 6308 | // fold (fmul c1, c2) -> c1*c2 |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6309 | if (N0CFP && N1CFP) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6310 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0, N1); |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 6311 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 6312 | if (N0CFP && !N1CFP) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6313 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N1, N0); |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 6314 | // fold (fmul A, 0) -> 0 |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6315 | if (DAG.getTarget().Options.UnsafeFPMath && |
6316 | N1CFP && N1CFP->getValueAPF().isZero()) | ||||
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 6317 | return N1; |
Dan Gohman | 77b81fe | 2009-06-04 17:12:12 +0000 | [diff] [blame] | 6318 | // fold (fmul A, 0) -> 0, vector edition. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6319 | if (DAG.getTarget().Options.UnsafeFPMath && |
6320 | ISD::isBuildVectorAllZeros(N1.getNode())) | ||||
Dan Gohman | 77b81fe | 2009-06-04 17:12:12 +0000 | [diff] [blame] | 6321 | return N1; |
Owen Anderson | 363e4b9 | 2012-05-02 21:32:35 +0000 | [diff] [blame] | 6322 | // fold (fmul A, 1.0) -> A |
6323 | if (N1CFP && N1CFP->isExactlyValue(1.0)) | ||||
6324 | return N0; | ||||
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 6325 | // fold (fmul X, 2.0) -> (fadd X, X) |
6326 | if (N1CFP && N1CFP->isExactlyValue(+2.0)) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6327 | return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N0); |
Dan Gohman | eb1fedc | 2009-08-10 16:50:32 +0000 | [diff] [blame] | 6328 | // fold (fmul X, -1.0) -> (fneg X) |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 6329 | if (N1CFP && N1CFP->isExactlyValue(-1.0)) |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 6330 | if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6331 | return DAG.getNode(ISD::FNEG, SDLoc(N), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6332 | |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 6333 | // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y) |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6334 | if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6335 | &DAG.getTarget().Options)) { |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 6336 | if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6337 | &DAG.getTarget().Options)) { |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 6338 | // Both can be negated for free, check to see if at least one is cheaper |
6339 | // negated. | ||||
6340 | if (LHSNeg == 2 || RHSNeg == 2) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6341 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6342 | GetNegatedExpression(N0, DAG, LegalOperations), |
6343 | GetNegatedExpression(N1, DAG, LegalOperations)); | ||||
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 6344 | } |
6345 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6346 | |
Chris Lattner | ddae4bd | 2007-01-08 23:04:05 +0000 | [diff] [blame] | 6347 | // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2)) |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6348 | if (DAG.getTarget().Options.UnsafeFPMath && |
6349 | N1CFP && N0.getOpcode() == ISD::FMUL && | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6350 | N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1))) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6351 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0.getOperand(0), |
6352 | DAG.getNode(ISD::FMUL, SDLoc(N), VT, | ||||
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 6353 | N0.getOperand(1), N1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6354 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6355 | return SDValue(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6356 | } |
6357 | |||||
Owen Anderson | 062c0a5 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 6358 | SDValue DAGCombiner::visitFMA(SDNode *N) { |
6359 | SDValue N0 = N->getOperand(0); | ||||
6360 | SDValue N1 = N->getOperand(1); | ||||
6361 | SDValue N2 = N->getOperand(2); | ||||
6362 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); | ||||
6363 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); | ||||
6364 | EVT VT = N->getValueType(0); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6365 | SDLoc dl(N); |
Owen Anderson | 062c0a5 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 6366 | |
Owen Anderson | 607ebde | 2012-11-01 02:00:53 +0000 | [diff] [blame] | 6367 | if (DAG.getTarget().Options.UnsafeFPMath) { |
6368 | if (N0CFP && N0CFP->isZero()) | ||||
6369 | return N2; | ||||
6370 | if (N1CFP && N1CFP->isZero()) | ||||
6371 | return N2; | ||||
6372 | } | ||||
Owen Anderson | 062c0a5 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 6373 | if (N0CFP && N0CFP->isExactlyValue(1.0)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6374 | return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N2); |
Owen Anderson | 062c0a5 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 6375 | if (N1CFP && N1CFP->isExactlyValue(1.0)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6376 | return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N2); |
Owen Anderson | 062c0a5 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 6377 | |
Owen Anderson | 85ef6f4 | 2012-05-30 18:50:39 +0000 | [diff] [blame] | 6378 | // Canonicalize (fma c, x, y) -> (fma x, c, y) |
Owen Anderson | f917d20 | 2012-05-30 18:54:50 +0000 | [diff] [blame] | 6379 | if (N0CFP && !N1CFP) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6380 | return DAG.getNode(ISD::FMA, SDLoc(N), VT, N1, N0, N2); |
Owen Anderson | 85ef6f4 | 2012-05-30 18:50:39 +0000 | [diff] [blame] | 6381 | |
Owen Anderson | 58d5729 | 2012-09-01 06:04:27 +0000 | [diff] [blame] | 6382 | // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2) |
6383 | if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && | ||||
6384 | N2.getOpcode() == ISD::FMUL && | ||||
6385 | N0 == N2.getOperand(0) && | ||||
6386 | N2.getOperand(1).getOpcode() == ISD::ConstantFP) { | ||||
6387 | return DAG.getNode(ISD::FMUL, dl, VT, N0, | ||||
6388 | DAG.getNode(ISD::FADD, dl, VT, N1, N2.getOperand(1))); | ||||
6389 | } | ||||
6390 | |||||
6391 | |||||
6392 | // (fma (fmul x, c1), c2, y) -> (fma x, c1*c2, y) | ||||
6393 | if (DAG.getTarget().Options.UnsafeFPMath && | ||||
6394 | N0.getOpcode() == ISD::FMUL && N1CFP && | ||||
6395 | N0.getOperand(1).getOpcode() == ISD::ConstantFP) { | ||||
6396 | return DAG.getNode(ISD::FMA, dl, VT, | ||||
6397 | N0.getOperand(0), | ||||
6398 | DAG.getNode(ISD::FMUL, dl, VT, N1, N0.getOperand(1)), | ||||
6399 | N2); | ||||
6400 | } | ||||
6401 | |||||
6402 | // (fma x, 1, y) -> (fadd x, y) | ||||
6403 | // (fma x, -1, y) -> (fadd (fneg x), y) | ||||
6404 | if (N1CFP) { | ||||
6405 | if (N1CFP->isExactlyValue(1.0)) | ||||
6406 | return DAG.getNode(ISD::FADD, dl, VT, N0, N2); | ||||
6407 | |||||
6408 | if (N1CFP->isExactlyValue(-1.0) && | ||||
6409 | (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))) { | ||||
6410 | SDValue RHSNeg = DAG.getNode(ISD::FNEG, dl, VT, N0); | ||||
6411 | AddToWorkList(RHSNeg.getNode()); | ||||
6412 | return DAG.getNode(ISD::FADD, dl, VT, N2, RHSNeg); | ||||
6413 | } | ||||
6414 | } | ||||
6415 | |||||
6416 | // (fma x, c, x) -> (fmul x, (c+1)) | ||||
Stephen Lin | b494015 | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6417 | if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && N0 == N2) |
6418 | return DAG.getNode(ISD::FMUL, dl, VT, N0, | ||||
Owen Anderson | 58d5729 | 2012-09-01 06:04:27 +0000 | [diff] [blame] | 6419 | DAG.getNode(ISD::FADD, dl, VT, |
6420 | N1, DAG.getConstantFP(1.0, VT))); | ||||
Owen Anderson | 58d5729 | 2012-09-01 06:04:27 +0000 | [diff] [blame] | 6421 | |
6422 | // (fma x, c, (fneg x)) -> (fmul x, (c-1)) | ||||
6423 | if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && | ||||
Stephen Lin | b494015 | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6424 | N2.getOpcode() == ISD::FNEG && N2.getOperand(0) == N0) |
6425 | return DAG.getNode(ISD::FMUL, dl, VT, N0, | ||||
Owen Anderson | 58d5729 | 2012-09-01 06:04:27 +0000 | [diff] [blame] | 6426 | DAG.getNode(ISD::FADD, dl, VT, |
6427 | N1, DAG.getConstantFP(-1.0, VT))); | ||||
Owen Anderson | 58d5729 | 2012-09-01 06:04:27 +0000 | [diff] [blame] | 6428 | |
6429 | |||||
Owen Anderson | 062c0a5 | 2012-05-02 22:17:40 +0000 | [diff] [blame] | 6430 | return SDValue(); |
6431 | } | ||||
6432 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6433 | SDValue DAGCombiner::visitFDIV(SDNode *N) { |
6434 | SDValue N0 = N->getOperand(0); | ||||
6435 | SDValue N1 = N->getOperand(1); | ||||
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6436 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
6437 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6438 | EVT VT = N->getValueType(0); |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6439 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6440 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6441 | // fold vector ops |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6442 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6443 | SDValue FoldedVOp = SimplifyVBinOp(N); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6444 | if (FoldedVOp.getNode()) return FoldedVOp; |
Dan Gohman | 05d92fe | 2007-07-13 20:03:40 +0000 | [diff] [blame] | 6445 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6446 | |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6447 | // fold (fdiv c1, c2) -> c1/c2 |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6448 | if (N0CFP && N1CFP) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6449 | return DAG.getNode(ISD::FDIV, SDLoc(N), VT, N0, N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6450 | |
Duncan Sands | 3ef3fcf | 2012-04-08 18:08:12 +0000 | [diff] [blame] | 6451 | // fold (fdiv X, c2) -> fmul X, 1/c2 if losing precision is acceptable. |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6452 | if (N1CFP && DAG.getTarget().Options.UnsafeFPMath) { |
Duncan Sands | 961d666 | 2012-04-07 20:04:00 +0000 | [diff] [blame] | 6453 | // Compute the reciprocal 1.0 / c2. |
6454 | APFloat N1APF = N1CFP->getValueAPF(); | ||||
6455 | APFloat Recip(N1APF.getSemantics(), 1); // 1.0 | ||||
6456 | APFloat::opStatus st = Recip.divide(N1APF, APFloat::rmNearestTiesToEven); | ||||
Duncan Sands | 507bb7a | 2012-04-10 20:35:27 +0000 | [diff] [blame] | 6457 | // Only do the transform if the reciprocal is a legal fp immediate that |
6458 | // isn't too nasty (eg NaN, denormal, ...). | ||||
6459 | if ((st == APFloat::opOK || st == APFloat::opInexact) && // Not too nasty | ||||
Anton Korobeynikov | 999821c | 2012-04-10 13:22:49 +0000 | [diff] [blame] | 6460 | (!LegalOperations || |
6461 | // FIXME: custom lowering of ConstantFP might fail (see e.g. ARM | ||||
6462 | // backend)... we should handle this gracefully after Legalize. | ||||
6463 | // TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT) || | ||||
6464 | TLI.isOperationLegal(llvm::ISD::ConstantFP, VT) || | ||||
6465 | TLI.isFPImmLegal(Recip, VT))) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6466 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0, |
Duncan Sands | 961d666 | 2012-04-07 20:04:00 +0000 | [diff] [blame] | 6467 | DAG.getConstantFP(Recip, VT)); |
6468 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6469 | |
Bill Wendling | a03e74b | 2009-01-30 22:57:07 +0000 | [diff] [blame] | 6470 | // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y) |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6471 | if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6472 | &DAG.getTarget().Options)) { |
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6473 | if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI, |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 6474 | &DAG.getTarget().Options)) { |
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 6475 | // Both can be negated for free, check to see if at least one is cheaper |
6476 | // negated. | ||||
6477 | if (LHSNeg == 2 || RHSNeg == 2) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6478 | return DAG.getNode(ISD::FDIV, SDLoc(N), VT, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6479 | GetNegatedExpression(N0, DAG, LegalOperations), |
6480 | GetNegatedExpression(N1, DAG, LegalOperations)); | ||||
Chris Lattner | 2944652 | 2007-05-14 22:04:50 +0000 | [diff] [blame] | 6481 | } |
6482 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6483 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6484 | return SDValue(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6485 | } |
6486 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6487 | SDValue DAGCombiner::visitFREM(SDNode *N) { |
6488 | SDValue N0 = N->getOperand(0); | ||||
6489 | SDValue N1 = N->getOperand(1); | ||||
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6490 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
6491 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6492 | EVT VT = N->getValueType(0); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6493 | |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6494 | // fold (frem c1, c2) -> fmod(c1,c2) |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6495 | if (N0CFP && N1CFP) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6496 | return DAG.getNode(ISD::FREM, SDLoc(N), VT, N0, N1); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6497 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6498 | return SDValue(); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 6499 | } |
6500 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6501 | SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) { |
6502 | SDValue N0 = N->getOperand(0); | ||||
6503 | SDValue N1 = N->getOperand(1); | ||||
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6504 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
6505 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6506 | EVT VT = N->getValueType(0); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6507 | |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6508 | if (N0CFP && N1CFP) // Constant fold |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6509 | return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, N0, N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6510 | |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6511 | if (N1CFP) { |
Dale Johannesen | e6c1742 | 2007-08-26 01:18:27 +0000 | [diff] [blame] | 6512 | const APFloat& V = N1CFP->getValueAPF(); |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 6513 | // copysign(x, c1) -> fabs(x) iff ispos(c1) |
6514 | // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1) | ||||
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 6515 | if (!V.isNegative()) { |
6516 | if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT)) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6517 | return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0); |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 6518 | } else { |
6519 | if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT)) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6520 | return DAG.getNode(ISD::FNEG, SDLoc(N), VT, |
6521 | DAG.getNode(ISD::FABS, SDLoc(N0), VT, N0)); | ||||
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 6522 | } |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6523 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6524 | |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6525 | // copysign(fabs(x), y) -> copysign(x, y) |
6526 | // copysign(fneg(x), y) -> copysign(x, y) | ||||
6527 | // copysign(copysign(x,z), y) -> copysign(x, y) | ||||
6528 | if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG || | ||||
6529 | N0.getOpcode() == ISD::FCOPYSIGN) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6530 | return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6531 | N0.getOperand(0), N1); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6532 | |
6533 | // copysign(x, abs(y)) -> abs(x) | ||||
6534 | if (N1.getOpcode() == ISD::FABS) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6535 | return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6536 | |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6537 | // copysign(x, copysign(y,z)) -> copysign(x, z) |
6538 | if (N1.getOpcode() == ISD::FCOPYSIGN) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6539 | return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6540 | N0, N1.getOperand(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6541 | |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6542 | // copysign(x, fp_extend(y)) -> copysign(x, y) |
6543 | // copysign(x, fp_round(y)) -> copysign(x, y) | ||||
6544 | if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6545 | return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6546 | N0, N1.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6547 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6548 | return SDValue(); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6549 | } |
6550 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6551 | SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) { |
6552 | SDValue N0 = N->getOperand(0); | ||||
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 6553 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6554 | EVT VT = N->getValueType(0); |
6555 | EVT OpVT = N0.getValueType(); | ||||
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 6556 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6557 | // fold (sint_to_fp c1) -> c1fp |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6558 | if (N0C && |
Stuart Hastings | 7e33418 | 2011-03-02 19:36:30 +0000 | [diff] [blame] | 6559 | // ...but only if the target supports immediate floating-point values |
Eli Friedman | 5018524 | 2011-11-12 00:35:34 +0000 | [diff] [blame] | 6560 | (!LegalOperations || |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 6561 | TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6562 | return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6563 | |
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 6564 | // If the input is a legal type, and SINT_TO_FP is not legal on this target, |
6565 | // but UINT_TO_FP is legal on this target, try to convert. | ||||
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 6566 | if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) && |
6567 | TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) { | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6568 | // If the sign bit is known to be zero, we can change this to UINT_TO_FP. |
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 6569 | if (DAG.SignBitIsZero(N0)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6570 | return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0); |
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 6571 | } |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6572 | |
Nadav Rotem | ed1a335 | 2012-07-23 07:59:50 +0000 | [diff] [blame] | 6573 | // The next optimizations are desireable only if SELECT_CC can be lowered. |
6574 | // Check against MVT::Other for SELECT_CC, which is a workaround for targets | ||||
6575 | // having to say they don't support SELECT_CC on every type the DAG knows | ||||
6576 | // about, since there is no way to mark an opcode illegal at all value types | ||||
6577 | // (See also visitSELECT) | ||||
6578 | if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) { | ||||
6579 | // fold (sint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc) | ||||
6580 | if (N0.getOpcode() == ISD::SETCC && N0.getValueType() == MVT::i1 && | ||||
6581 | !VT.isVector() && | ||||
6582 | (!LegalOperations || | ||||
6583 | TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) { | ||||
6584 | SDValue Ops[] = | ||||
6585 | { N0.getOperand(0), N0.getOperand(1), | ||||
6586 | DAG.getConstantFP(-1.0, VT) , DAG.getConstantFP(0.0, VT), | ||||
6587 | N0.getOperand(2) }; | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6588 | return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5); |
Nadav Rotem | ed1a335 | 2012-07-23 07:59:50 +0000 | [diff] [blame] | 6589 | } |
Owen Anderson | d9bf71f | 2012-07-09 20:31:12 +0000 | [diff] [blame] | 6590 | |
Nadav Rotem | ed1a335 | 2012-07-23 07:59:50 +0000 | [diff] [blame] | 6591 | // fold (sint_to_fp (zext (setcc x, y, cc))) -> |
6592 | // (select_cc x, y, 1.0, 0.0,, cc) | ||||
6593 | if (N0.getOpcode() == ISD::ZERO_EXTEND && | ||||
6594 | N0.getOperand(0).getOpcode() == ISD::SETCC &&!VT.isVector() && | ||||
6595 | (!LegalOperations || | ||||
6596 | TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) { | ||||
6597 | SDValue Ops[] = | ||||
6598 | { N0.getOperand(0).getOperand(0), N0.getOperand(0).getOperand(1), | ||||
6599 | DAG.getConstantFP(1.0, VT) , DAG.getConstantFP(0.0, VT), | ||||
6600 | N0.getOperand(0).getOperand(2) }; | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6601 | return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5); |
Nadav Rotem | ed1a335 | 2012-07-23 07:59:50 +0000 | [diff] [blame] | 6602 | } |
Owen Anderson | d9bf71f | 2012-07-09 20:31:12 +0000 | [diff] [blame] | 6603 | } |
6604 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6605 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6606 | } |
6607 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6608 | SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) { |
6609 | SDValue N0 = N->getOperand(0); | ||||
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 6610 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6611 | EVT VT = N->getValueType(0); |
6612 | EVT OpVT = N0.getValueType(); | ||||
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6613 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6614 | // fold (uint_to_fp c1) -> c1fp |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6615 | if (N0C && |
Stuart Hastings | 7e33418 | 2011-03-02 19:36:30 +0000 | [diff] [blame] | 6616 | // ...but only if the target supports immediate floating-point values |
Eli Friedman | 5018524 | 2011-11-12 00:35:34 +0000 | [diff] [blame] | 6617 | (!LegalOperations || |
Evan Cheng | 9568e5c | 2011-06-21 06:01:08 +0000 | [diff] [blame] | 6618 | TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6619 | return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6620 | |
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 6621 | // If the input is a legal type, and UINT_TO_FP is not legal on this target, |
6622 | // but SINT_TO_FP is legal on this target, try to convert. | ||||
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 6623 | if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) && |
6624 | TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) { | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6625 | // If the sign bit is known to be zero, we can change this to SINT_TO_FP. |
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 6626 | if (DAG.SignBitIsZero(N0)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6627 | return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0); |
Chris Lattner | cda8875 | 2008-06-26 00:16:49 +0000 | [diff] [blame] | 6628 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6629 | |
Nadav Rotem | ed1a335 | 2012-07-23 07:59:50 +0000 | [diff] [blame] | 6630 | // The next optimizations are desireable only if SELECT_CC can be lowered. |
6631 | // Check against MVT::Other for SELECT_CC, which is a workaround for targets | ||||
6632 | // having to say they don't support SELECT_CC on every type the DAG knows | ||||
6633 | // about, since there is no way to mark an opcode illegal at all value types | ||||
6634 | // (See also visitSELECT) | ||||
6635 | if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) { | ||||
6636 | // fold (uint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc) | ||||
Owen Anderson | d9bf71f | 2012-07-09 20:31:12 +0000 | [diff] [blame] | 6637 | |
Nadav Rotem | ed1a335 | 2012-07-23 07:59:50 +0000 | [diff] [blame] | 6638 | if (N0.getOpcode() == ISD::SETCC && !VT.isVector() && |
6639 | (!LegalOperations || | ||||
6640 | TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) { | ||||
6641 | SDValue Ops[] = | ||||
6642 | { N0.getOperand(0), N0.getOperand(1), | ||||
6643 | DAG.getConstantFP(1.0, VT), DAG.getConstantFP(0.0, VT), | ||||
6644 | N0.getOperand(2) }; | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6645 | return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5); |
Nadav Rotem | ed1a335 | 2012-07-23 07:59:50 +0000 | [diff] [blame] | 6646 | } |
6647 | } | ||||
Owen Anderson | d9bf71f | 2012-07-09 20:31:12 +0000 | [diff] [blame] | 6648 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6649 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6650 | } |
6651 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6652 | SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) { |
6653 | SDValue N0 = N->getOperand(0); | ||||
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6654 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6655 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6656 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6657 | // fold (fp_to_sint c1fp) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 6658 | if (N0CFP) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6659 | return DAG.getNode(ISD::FP_TO_SINT, SDLoc(N), VT, N0); |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6660 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6661 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6662 | } |
6663 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6664 | SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) { |
6665 | SDValue N0 = N->getOperand(0); | ||||
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6666 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6667 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6668 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6669 | // fold (fp_to_uint c1fp) -> c1 |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6670 | if (N0CFP) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6671 | return DAG.getNode(ISD::FP_TO_UINT, SDLoc(N), VT, N0); |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6672 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6673 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6674 | } |
6675 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6676 | SDValue DAGCombiner::visitFP_ROUND(SDNode *N) { |
6677 | SDValue N0 = N->getOperand(0); | ||||
6678 | SDValue N1 = N->getOperand(1); | ||||
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6679 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6680 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6681 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6682 | // fold (fp_round c1fp) -> c1fp |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6683 | if (N0CFP) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6684 | return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0, N1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6685 | |
Chris Lattner | 79dbea5 | 2006-03-13 06:26:26 +0000 | [diff] [blame] | 6686 | // fold (fp_round (fp_extend x)) -> x |
6687 | if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType()) | ||||
6688 | return N0.getOperand(0); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6689 | |
Chris Lattner | 0aa5e6f | 2008-01-24 06:45:35 +0000 | [diff] [blame] | 6690 | // fold (fp_round (fp_round x)) -> (fp_round x) |
6691 | if (N0.getOpcode() == ISD::FP_ROUND) { | ||||
6692 | // This is a value preserving truncation if both round's are. | ||||
6693 | bool IsTrunc = N->getConstantOperandVal(1) == 1 && | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6694 | N0.getNode()->getConstantOperandVal(1) == 1; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6695 | return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0.getOperand(0), |
Chris Lattner | 0aa5e6f | 2008-01-24 06:45:35 +0000 | [diff] [blame] | 6696 | DAG.getIntPtrConstant(IsTrunc)); |
6697 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6698 | |
Chris Lattner | 79dbea5 | 2006-03-13 06:26:26 +0000 | [diff] [blame] | 6699 | // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6700 | if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6701 | SDValue Tmp = DAG.getNode(ISD::FP_ROUND, SDLoc(N0), VT, |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6702 | N0.getOperand(0), N1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6703 | AddToWorkList(Tmp.getNode()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6704 | return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6705 | Tmp, N0.getOperand(1)); |
Chris Lattner | 79dbea5 | 2006-03-13 06:26:26 +0000 | [diff] [blame] | 6706 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6707 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6708 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6709 | } |
6710 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6711 | SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) { |
6712 | SDValue N0 = N->getOperand(0); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6713 | EVT VT = N->getValueType(0); |
6714 | EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT(); | ||||
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 6715 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6716 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6717 | // fold (fp_round_inreg c1fp) -> c1fp |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 6718 | if (N0CFP && isTypeLegal(EVT)) { |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 6719 | SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6720 | return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Round); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6721 | } |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6722 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6723 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6724 | } |
6725 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6726 | SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) { |
6727 | SDValue N0 = N->getOperand(0); | ||||
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6728 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6729 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6730 | |
Chris Lattner | 5938bef | 2007-12-29 06:55:23 +0000 | [diff] [blame] | 6731 | // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6732 | if (N->hasOneUse() && |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 6733 | N->use_begin()->getOpcode() == ISD::FP_ROUND) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6734 | return SDValue(); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 6735 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6736 | // fold (fp_extend c1fp) -> c1fp |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6737 | if (N0CFP) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6738 | return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, N0); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 6739 | |
6740 | // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the | ||||
6741 | // value of X. | ||||
Gabor Greif | 12632d2 | 2008-08-30 19:29:20 +0000 | [diff] [blame] | 6742 | if (N0.getOpcode() == ISD::FP_ROUND |
6743 | && N0.getNode()->getConstantOperandVal(1) == 1) { | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6744 | SDValue In = N0.getOperand(0); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 6745 | if (In.getValueType() == VT) return In; |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 6746 | if (VT.bitsLT(In.getValueType())) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6747 | return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6748 | In, N0.getOperand(1)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6749 | return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, In); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 6750 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6751 | |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 6752 | // fold (fpext (load x)) -> (fpext (fptrunc (extload x))) |
Hal Finkel | 03c8f8f | 2013-10-04 22:18:12 +0000 | [diff] [blame] | 6753 | if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6754 | ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 6755 | TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 6756 | LoadSDNode *LN0 = cast<LoadSDNode>(N0); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6757 | SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT, |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6758 | LN0->getChain(), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 6759 | LN0->getBasePtr(), LN0->getPointerInfo(), |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6760 | N0.getValueType(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 6761 | LN0->isVolatile(), LN0->isNonTemporal(), |
6762 | LN0->getAlignment()); | ||||
Chris Lattner | e564dbb | 2006-05-05 21:34:35 +0000 | [diff] [blame] | 6763 | CombineTo(N, ExtLoad); |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6764 | CombineTo(N0.getNode(), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6765 | DAG.getNode(ISD::FP_ROUND, SDLoc(N0), |
Bill Wendling | 0225a1d | 2009-01-30 23:15:49 +0000 | [diff] [blame] | 6766 | N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)), |
Chris Lattner | e564dbb | 2006-05-05 21:34:35 +0000 | [diff] [blame] | 6767 | ExtLoad.getValue(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6768 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | e564dbb | 2006-05-05 21:34:35 +0000 | [diff] [blame] | 6769 | } |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 6770 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6771 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6772 | } |
6773 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6774 | SDValue DAGCombiner::visitFNEG(SDNode *N) { |
6775 | SDValue N0 = N->getOperand(0); | ||||
Anton Korobeynikov | 2bcf60a | 2009-10-20 21:37:45 +0000 | [diff] [blame] | 6776 | EVT VT = N->getValueType(0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6777 | |
Craig Topper | dd201ff | 2012-09-11 01:45:21 +0000 | [diff] [blame] | 6778 | if (VT.isVector()) { |
6779 | SDValue FoldedVOp = SimplifyVUnaryOp(N); | ||||
6780 | if (FoldedVOp.getNode()) return FoldedVOp; | ||||
Craig Topper | 956342b | 2012-09-09 22:58:45 +0000 | [diff] [blame] | 6781 | } |
6782 | |||||
Owen Anderson | afd3d56 | 2012-03-06 00:29:31 +0000 | [diff] [blame] | 6783 | if (isNegatibleForFree(N0, LegalOperations, DAG.getTargetLoweringInfo(), |
6784 | &DAG.getTarget().Options)) | ||||
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6785 | return GetNegatedExpression(N0, DAG, LegalOperations); |
Dan Gohman | 23ff182 | 2007-07-02 15:48:56 +0000 | [diff] [blame] | 6786 | |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 6787 | // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading |
6788 | // constant pool values. | ||||
Owen Anderson | 29f60f3 | 2012-04-02 22:10:29 +0000 | [diff] [blame] | 6789 | if (!TLI.isFNegFree(VT) && N0.getOpcode() == ISD::BITCAST && |
Anton Korobeynikov | 2bcf60a | 2009-10-20 21:37:45 +0000 | [diff] [blame] | 6790 | !VT.isVector() && |
6791 | N0.getNode()->hasOneUse() && | ||||
6792 | N0.getOperand(0).getValueType().isInteger()) { | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6793 | SDValue Int = N0.getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6794 | EVT IntVT = Int.getValueType(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6795 | if (IntVT.isInteger() && !IntVT.isVector()) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6796 | Int = DAG.getNode(ISD::XOR, SDLoc(N0), IntVT, Int, |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 6797 | DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6798 | AddToWorkList(Int.getNode()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6799 | return DAG.getNode(ISD::BITCAST, SDLoc(N), |
Anton Korobeynikov | 2bcf60a | 2009-10-20 21:37:45 +0000 | [diff] [blame] | 6800 | VT, Int); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 6801 | } |
6802 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6803 | |
Owen Anderson | 58d5729 | 2012-09-01 06:04:27 +0000 | [diff] [blame] | 6804 | // (fneg (fmul c, x)) -> (fmul -c, x) |
6805 | if (N0.getOpcode() == ISD::FMUL) { | ||||
6806 | ConstantFPSDNode *CFP1 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1)); | ||||
Stephen Lin | b494015 | 2013-07-09 00:44:49 +0000 | [diff] [blame] | 6807 | if (CFP1) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6808 | return DAG.getNode(ISD::FMUL, SDLoc(N), VT, |
Owen Anderson | 58d5729 | 2012-09-01 06:04:27 +0000 | [diff] [blame] | 6809 | N0.getOperand(0), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6810 | DAG.getNode(ISD::FNEG, SDLoc(N), VT, |
Owen Anderson | 58d5729 | 2012-09-01 06:04:27 +0000 | [diff] [blame] | 6811 | N0.getOperand(1))); |
Owen Anderson | 58d5729 | 2012-09-01 06:04:27 +0000 | [diff] [blame] | 6812 | } |
6813 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6814 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6815 | } |
6816 | |||||
Owen Anderson | 7c626d3 | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 6817 | SDValue DAGCombiner::visitFCEIL(SDNode *N) { |
6818 | SDValue N0 = N->getOperand(0); | ||||
6819 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); | ||||
6820 | EVT VT = N->getValueType(0); | ||||
6821 | |||||
6822 | // fold (fceil c1) -> fceil(c1) | ||||
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6823 | if (N0CFP) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6824 | return DAG.getNode(ISD::FCEIL, SDLoc(N), VT, N0); |
Owen Anderson | 7c626d3 | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 6825 | |
6826 | return SDValue(); | ||||
6827 | } | ||||
6828 | |||||
6829 | SDValue DAGCombiner::visitFTRUNC(SDNode *N) { | ||||
6830 | SDValue N0 = N->getOperand(0); | ||||
6831 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); | ||||
6832 | EVT VT = N->getValueType(0); | ||||
6833 | |||||
6834 | // fold (ftrunc c1) -> ftrunc(c1) | ||||
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6835 | if (N0CFP) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6836 | return DAG.getNode(ISD::FTRUNC, SDLoc(N), VT, N0); |
Owen Anderson | 7c626d3 | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 6837 | |
6838 | return SDValue(); | ||||
6839 | } | ||||
6840 | |||||
6841 | SDValue DAGCombiner::visitFFLOOR(SDNode *N) { | ||||
6842 | SDValue N0 = N->getOperand(0); | ||||
6843 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); | ||||
6844 | EVT VT = N->getValueType(0); | ||||
6845 | |||||
6846 | // fold (ffloor c1) -> ffloor(c1) | ||||
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6847 | if (N0CFP) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6848 | return DAG.getNode(ISD::FFLOOR, SDLoc(N), VT, N0); |
Owen Anderson | 7c626d3 | 2012-08-13 23:32:49 +0000 | [diff] [blame] | 6849 | |
6850 | return SDValue(); | ||||
6851 | } | ||||
6852 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6853 | SDValue DAGCombiner::visitFABS(SDNode *N) { |
6854 | SDValue N0 = N->getOperand(0); | ||||
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 6855 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6856 | EVT VT = N->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6857 | |
Craig Topper | dd201ff | 2012-09-11 01:45:21 +0000 | [diff] [blame] | 6858 | if (VT.isVector()) { |
6859 | SDValue FoldedVOp = SimplifyVUnaryOp(N); | ||||
6860 | if (FoldedVOp.getNode()) return FoldedVOp; | ||||
6861 | } | ||||
6862 | |||||
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6863 | // fold (fabs c1) -> fabs(c1) |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 6864 | if (N0CFP) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6865 | return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6866 | // fold (fabs (fabs x)) -> (fabs x) |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6867 | if (N0.getOpcode() == ISD::FABS) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 6868 | return N->getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6869 | // fold (fabs (fneg x)) -> (fabs x) |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 6870 | // fold (fabs (fcopysign x, y)) -> (fabs x) |
6871 | if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6872 | return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6873 | |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 6874 | // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading |
6875 | // constant pool values. | ||||
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 6876 | if (!TLI.isFAbsFree(VT) && |
Owen Anderson | 29f60f3 | 2012-04-02 22:10:29 +0000 | [diff] [blame] | 6877 | N0.getOpcode() == ISD::BITCAST && N0.getNode()->hasOneUse() && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6878 | N0.getOperand(0).getValueType().isInteger() && |
6879 | !N0.getOperand(0).getValueType().isVector()) { | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6880 | SDValue Int = N0.getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6881 | EVT IntVT = Int.getValueType(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6882 | if (IntVT.isInteger() && !IntVT.isVector()) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6883 | Int = DAG.getNode(ISD::AND, SDLoc(N0), IntVT, Int, |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 6884 | DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6885 | AddToWorkList(Int.getNode()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6886 | return DAG.getNode(ISD::BITCAST, SDLoc(N), |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6887 | N->getValueType(0), Int); |
Chris Lattner | 3bd39d4 | 2008-01-27 17:42:27 +0000 | [diff] [blame] | 6888 | } |
6889 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6890 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6891 | return SDValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 6892 | } |
6893 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6894 | SDValue DAGCombiner::visitBRCOND(SDNode *N) { |
6895 | SDValue Chain = N->getOperand(0); | ||||
6896 | SDValue N1 = N->getOperand(1); | ||||
6897 | SDValue N2 = N->getOperand(2); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6898 | |
Dan Gohman | e0f06c7 | 2009-11-17 00:47:23 +0000 | [diff] [blame] | 6899 | // If N is a constant we could fold this into a fallthrough or unconditional |
6900 | // branch. However that doesn't happen very often in normal code, because | ||||
6901 | // Instcombine/SimplifyCFG should have handled the available opportunities. | ||||
6902 | // If we did this folding here, it would be necessary to update the | ||||
6903 | // MachineBasicBlock CFG, which is awkward. | ||||
6904 | |||||
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 6905 | // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal |
6906 | // on the target. | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6907 | if (N1.getOpcode() == ISD::SETCC && |
Tom Stellard | 3ef5383 | 2013-03-08 15:36:57 +0000 | [diff] [blame] | 6908 | TLI.isOperationLegalOrCustom(ISD::BR_CC, |
6909 | N1.getOperand(0).getValueType())) { | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6910 | return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other, |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6911 | Chain, N1.getOperand(2), |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 6912 | N1.getOperand(0), N1.getOperand(1), N2); |
6913 | } | ||||
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 6914 | |
Evan Cheng | 2a135ae | 2010-10-04 22:41:01 +0000 | [diff] [blame] | 6915 | if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) || |
6916 | ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) && | ||||
6917 | (N1.getOperand(0).hasOneUse() && | ||||
6918 | N1.getOperand(0).getOpcode() == ISD::SRL))) { | ||||
6919 | SDNode *Trunc = 0; | ||||
6920 | if (N1.getOpcode() == ISD::TRUNCATE) { | ||||
6921 | // Look pass the truncate. | ||||
6922 | Trunc = N1.getNode(); | ||||
6923 | N1 = N1.getOperand(0); | ||||
6924 | } | ||||
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 6925 | |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6926 | // Match this pattern so that we can generate simpler code: |
6927 | // | ||||
6928 | // %a = ... | ||||
6929 | // %b = and i32 %a, 2 | ||||
6930 | // %c = srl i32 %b, 1 | ||||
6931 | // brcond i32 %c ... | ||||
6932 | // | ||||
6933 | // into | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6934 | // |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6935 | // %a = ... |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 6936 | // %b = and i32 %a, 2 |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6937 | // %c = setcc eq %b, 0 |
6938 | // brcond %c ... | ||||
6939 | // | ||||
6940 | // This applies only when the AND constant value has one bit set and the | ||||
6941 | // SRL constant is equal to the log2 of the AND constant. The back-end is | ||||
6942 | // smart enough to convert the result into a TEST/JMP sequence. | ||||
6943 | SDValue Op0 = N1.getOperand(0); | ||||
6944 | SDValue Op1 = N1.getOperand(1); | ||||
6945 | |||||
6946 | if (Op0.getOpcode() == ISD::AND && | ||||
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6947 | Op1.getOpcode() == ISD::Constant) { |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6948 | SDValue AndOp1 = Op0.getOperand(1); |
6949 | |||||
6950 | if (AndOp1.getOpcode() == ISD::Constant) { | ||||
6951 | const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue(); | ||||
6952 | |||||
6953 | if (AndConst.isPowerOf2() && | ||||
6954 | cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) { | ||||
6955 | SDValue SetCC = | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6956 | DAG.getSetCC(SDLoc(N), |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 6957 | getSetCCResultType(Op0.getValueType()), |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6958 | Op0, DAG.getConstant(0, Op0.getValueType()), |
6959 | ISD::SETNE); | ||||
6960 | |||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6961 | SDValue NewBRCond = DAG.getNode(ISD::BRCOND, SDLoc(N), |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 6962 | MVT::Other, Chain, SetCC, N2); |
6963 | // Don't add the new BRCond into the worklist or else SimplifySelectCC | ||||
6964 | // will convert it back to (X & C1) >> C2. | ||||
6965 | CombineTo(N, NewBRCond, false); | ||||
6966 | // Truncate is dead. | ||||
6967 | if (Trunc) { | ||||
6968 | removeFromWorkList(Trunc); | ||||
6969 | DAG.DeleteNode(Trunc); | ||||
6970 | } | ||||
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6971 | // Replace the uses of SRL with SETCC |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 6972 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 6973 | DAG.ReplaceAllUsesOfValueWith(N1, SetCC); |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6974 | removeFromWorkList(N1.getNode()); |
6975 | DAG.DeleteNode(N1.getNode()); | ||||
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 6976 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6977 | } |
6978 | } | ||||
6979 | } | ||||
Evan Cheng | 2a135ae | 2010-10-04 22:41:01 +0000 | [diff] [blame] | 6980 | |
6981 | if (Trunc) | ||||
6982 | // Restore N1 if the above transformation doesn't match. | ||||
6983 | N1 = N->getOperand(1); | ||||
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 6984 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6985 | |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 6986 | // Transform br(xor(x, y)) -> br(x != y) |
6987 | // Transform br(xor(xor(x,y), 1)) -> br (x == y) | ||||
6988 | if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) { | ||||
6989 | SDNode *TheXor = N1.getNode(); | ||||
6990 | SDValue Op0 = TheXor->getOperand(0); | ||||
6991 | SDValue Op1 = TheXor->getOperand(1); | ||||
6992 | if (Op0.getOpcode() == Op1.getOpcode()) { | ||||
6993 | // Avoid missing important xor optimizations. | ||||
6994 | SDValue Tmp = visitXOR(TheXor); | ||||
Evan Cheng | 78ec025 | 2013-01-09 20:56:40 +0000 | [diff] [blame] | 6995 | if (Tmp.getNode()) { |
6996 | if (Tmp.getNode() != TheXor) { | ||||
6997 | DEBUG(dbgs() << "\nReplacing.8 "; | ||||
6998 | TheXor->dump(&DAG); | ||||
6999 | dbgs() << "\nWith: "; | ||||
7000 | Tmp.getNode()->dump(&DAG); | ||||
7001 | dbgs() << '\n'); | ||||
7002 | WorkListRemover DeadNodes(*this); | ||||
7003 | DAG.ReplaceAllUsesOfValueWith(N1, Tmp); | ||||
7004 | removeFromWorkList(TheXor); | ||||
7005 | DAG.DeleteNode(TheXor); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7006 | return DAG.getNode(ISD::BRCOND, SDLoc(N), |
Evan Cheng | 78ec025 | 2013-01-09 20:56:40 +0000 | [diff] [blame] | 7007 | MVT::Other, Chain, Tmp, N2); |
7008 | } | ||||
7009 | |||||
Benjamin Kramer | 0b68b75 | 2013-03-30 21:28:18 +0000 | [diff] [blame] | 7010 | // visitXOR has changed XOR's operands or replaced the XOR completely, |
7011 | // bail out. | ||||
7012 | return SDValue(N, 0); | ||||
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 7013 | } |
7014 | } | ||||
7015 | |||||
7016 | if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) { | ||||
7017 | bool Equal = false; | ||||
7018 | if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0)) | ||||
7019 | if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() && | ||||
7020 | Op0.getOpcode() == ISD::XOR) { | ||||
7021 | TheXor = Op0.getNode(); | ||||
7022 | Equal = true; | ||||
7023 | } | ||||
7024 | |||||
Evan Cheng | 2a135ae | 2010-10-04 22:41:01 +0000 | [diff] [blame] | 7025 | EVT SetCCVT = N1.getValueType(); |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 7026 | if (LegalTypes) |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 7027 | SetCCVT = getSetCCResultType(SetCCVT); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7028 | SDValue SetCC = DAG.getSetCC(SDLoc(TheXor), |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 7029 | SetCCVT, |
7030 | Op0, Op1, | ||||
7031 | Equal ? ISD::SETEQ : ISD::SETNE); | ||||
7032 | // Replace the uses of XOR with SETCC | ||||
7033 | WorkListRemover DeadNodes(*this); | ||||
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7034 | DAG.ReplaceAllUsesOfValueWith(N1, SetCC); |
Evan Cheng | 2a135ae | 2010-10-04 22:41:01 +0000 | [diff] [blame] | 7035 | removeFromWorkList(N1.getNode()); |
7036 | DAG.DeleteNode(N1.getNode()); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7037 | return DAG.getNode(ISD::BRCOND, SDLoc(N), |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 7038 | MVT::Other, Chain, SetCC, N2); |
7039 | } | ||||
7040 | } | ||||
Bill Wendling | a02a3dd | 2009-03-26 06:14:09 +0000 | [diff] [blame] | 7041 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7042 | return SDValue(); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 7043 | } |
7044 | |||||
Chris Lattner | 3ea0b47 | 2005-10-05 06:47:48 +0000 | [diff] [blame] | 7045 | // Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB. |
7046 | // | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7047 | SDValue DAGCombiner::visitBR_CC(SDNode *N) { |
Chris Lattner | 3ea0b47 | 2005-10-05 06:47:48 +0000 | [diff] [blame] | 7048 | CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7049 | SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7050 | |
Dan Gohman | e0f06c7 | 2009-11-17 00:47:23 +0000 | [diff] [blame] | 7051 | // If N is a constant we could fold this into a fallthrough or unconditional |
7052 | // branch. However that doesn't happen very often in normal code, because | ||||
7053 | // Instcombine/SimplifyCFG should have handled the available opportunities. | ||||
7054 | // If we did this folding here, it would be necessary to update the | ||||
7055 | // MachineBasicBlock CFG, which is awkward. | ||||
7056 | |||||
Duncan Sands | 8eab8a2 | 2008-06-09 11:32:28 +0000 | [diff] [blame] | 7057 | // Use SimplifySetCC to simplify SETCC's. |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 7058 | SDValue Simp = SimplifySetCC(getSetCCResultType(CondLHS.getValueType()), |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7059 | CondLHS, CondRHS, CC->get(), SDLoc(N), |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 7060 | false); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7061 | if (Simp.getNode()) AddToWorkList(Simp.getNode()); |
Chris Lattner | 30f73e7 | 2006-10-14 03:52:46 +0000 | [diff] [blame] | 7062 | |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 7063 | // fold to a simpler setcc |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7064 | if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7065 | return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other, |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7066 | N->getOperand(0), Simp.getOperand(2), |
7067 | Simp.getOperand(0), Simp.getOperand(1), | ||||
7068 | N->getOperand(4)); | ||||
7069 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7070 | return SDValue(); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 7071 | } |
7072 | |||||
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 7073 | /// canFoldInAddressingMode - Return true if 'Use' is a load or a store that |
7074 | /// uses N as its base pointer and that N may be folded in the load / store | ||||
Evan Cheng | 03be362 | 2012-03-06 23:33:32 +0000 | [diff] [blame] | 7075 | /// addressing mode. |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 7076 | static bool canFoldInAddressingMode(SDNode *N, SDNode *Use, |
7077 | SelectionDAG &DAG, | ||||
7078 | const TargetLowering &TLI) { | ||||
7079 | EVT VT; | ||||
7080 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) { | ||||
7081 | if (LD->isIndexed() || LD->getBasePtr().getNode() != N) | ||||
7082 | return false; | ||||
7083 | VT = Use->getValueType(0); | ||||
7084 | } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) { | ||||
7085 | if (ST->isIndexed() || ST->getBasePtr().getNode() != N) | ||||
7086 | return false; | ||||
7087 | VT = ST->getValue().getValueType(); | ||||
7088 | } else | ||||
7089 | return false; | ||||
7090 | |||||
Chandler Carruth | 56d433d | 2013-01-07 15:14:13 +0000 | [diff] [blame] | 7091 | TargetLowering::AddrMode AM; |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 7092 | if (N->getOpcode() == ISD::ADD) { |
7093 | ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1)); | ||||
7094 | if (Offset) | ||||
Evan Cheng | 03be362 | 2012-03-06 23:33:32 +0000 | [diff] [blame] | 7095 | // [reg +/- imm] |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 7096 | AM.BaseOffs = Offset->getSExtValue(); |
7097 | else | ||||
Evan Cheng | 03be362 | 2012-03-06 23:33:32 +0000 | [diff] [blame] | 7098 | // [reg +/- reg] |
7099 | AM.Scale = 1; | ||||
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 7100 | } else if (N->getOpcode() == ISD::SUB) { |
7101 | ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1)); | ||||
7102 | if (Offset) | ||||
Evan Cheng | 03be362 | 2012-03-06 23:33:32 +0000 | [diff] [blame] | 7103 | // [reg +/- imm] |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 7104 | AM.BaseOffs = -Offset->getSExtValue(); |
7105 | else | ||||
Evan Cheng | 03be362 | 2012-03-06 23:33:32 +0000 | [diff] [blame] | 7106 | // [reg +/- reg] |
7107 | AM.Scale = 1; | ||||
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 7108 | } else |
7109 | return false; | ||||
7110 | |||||
7111 | return TLI.isLegalAddressingMode(AM, VT.getTypeForEVT(*DAG.getContext())); | ||||
7112 | } | ||||
7113 | |||||
Duncan Sands | ec87aa8 | 2008-06-15 20:12:31 +0000 | [diff] [blame] | 7114 | /// CombineToPreIndexedLoadStore - Try turning a load / store into a |
7115 | /// pre-indexed load / store when the base pointer is an add or subtract | ||||
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7116 | /// and it has other uses besides the load / store. After the |
7117 | /// transformation, the new indexed load / store has effectively folded | ||||
7118 | /// the add / subtract in and all of its other uses are redirected to the | ||||
7119 | /// new load / store. | ||||
7120 | bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) { | ||||
Eli Friedman | 5018524 | 2011-11-12 00:35:34 +0000 | [diff] [blame] | 7121 | if (Level < AfterLegalizeDAG) |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7122 | return false; |
7123 | |||||
7124 | bool isLoad = true; | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7125 | SDValue Ptr; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7126 | EVT VT; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7127 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 7128 | if (LD->isIndexed()) |
Evan Cheng | e90460e | 2006-12-16 06:25:23 +0000 | [diff] [blame] | 7129 | return false; |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 7130 | VT = LD->getMemoryVT(); |
Evan Cheng | 83060c5 | 2007-03-07 08:07:03 +0000 | [diff] [blame] | 7131 | if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) && |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7132 | !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT)) |
7133 | return false; | ||||
7134 | Ptr = LD->getBasePtr(); | ||||
7135 | } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { | ||||
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 7136 | if (ST->isIndexed()) |
Evan Cheng | e90460e | 2006-12-16 06:25:23 +0000 | [diff] [blame] | 7137 | return false; |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 7138 | VT = ST->getMemoryVT(); |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7139 | if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) && |
7140 | !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT)) | ||||
7141 | return false; | ||||
7142 | Ptr = ST->getBasePtr(); | ||||
7143 | isLoad = false; | ||||
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7144 | } else { |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7145 | return false; |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7146 | } |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7147 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7148 | // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail |
7149 | // out. There is no reason to make this a preinc/predec. | ||||
7150 | if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) || | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7151 | Ptr.getNode()->hasOneUse()) |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7152 | return false; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7153 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7154 | // Ask the target to do addressing mode selection. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7155 | SDValue BasePtr; |
7156 | SDValue Offset; | ||||
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7157 | ISD::MemIndexedMode AM = ISD::UNINDEXED; |
7158 | if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG)) | ||||
7159 | return false; | ||||
Hal Finkel | 089a5f8 | 2013-02-08 21:35:47 +0000 | [diff] [blame] | 7160 | |
7161 | // Backends without true r+i pre-indexed forms may need to pass a | ||||
7162 | // constant base with a variable offset so that constant coercion | ||||
7163 | // will work with the patterns in canonical form. | ||||
7164 | bool Swapped = false; | ||||
7165 | if (isa<ConstantSDNode>(BasePtr)) { | ||||
7166 | std::swap(BasePtr, Offset); | ||||
7167 | Swapped = true; | ||||
7168 | } | ||||
7169 | |||||
Evan Cheng | a7d4a04 | 2007-05-03 23:52:19 +0000 | [diff] [blame] | 7170 | // Don't create a indexed load / store with zero offset. |
7171 | if (isa<ConstantSDNode>(Offset) && | ||||
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 7172 | cast<ConstantSDNode>(Offset)->isNullValue()) |
Evan Cheng | a7d4a04 | 2007-05-03 23:52:19 +0000 | [diff] [blame] | 7173 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7174 | |
Chris Lattner | 41e53fd | 2006-11-11 01:00:15 +0000 | [diff] [blame] | 7175 | // Try turning it into a pre-indexed load / store except when: |
Evan Cheng | c843abe | 2007-05-24 02:35:39 +0000 | [diff] [blame] | 7176 | // 1) The new base ptr is a frame index. |
7177 | // 2) If N is a store and the new base ptr is either the same as or is a | ||||
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7178 | // predecessor of the value being stored. |
Evan Cheng | c843abe | 2007-05-24 02:35:39 +0000 | [diff] [blame] | 7179 | // 3) Another use of old base ptr is a predecessor of N. If ptr is folded |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7180 | // that would create a cycle. |
Evan Cheng | c843abe | 2007-05-24 02:35:39 +0000 | [diff] [blame] | 7181 | // 4) All uses are load / store ops that use it as old base ptr. |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7182 | |
Chris Lattner | 41e53fd | 2006-11-11 01:00:15 +0000 | [diff] [blame] | 7183 | // Check #1. Preinc'ing a frame index would require copying the stack pointer |
7184 | // (plus the implicit offset) to a register to preinc anyway. | ||||
Evan Cheng | caab129 | 2009-05-06 18:25:01 +0000 | [diff] [blame] | 7185 | if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr)) |
Chris Lattner | 41e53fd | 2006-11-11 01:00:15 +0000 | [diff] [blame] | 7186 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7187 | |
Chris Lattner | 41e53fd | 2006-11-11 01:00:15 +0000 | [diff] [blame] | 7188 | // Check #2. |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7189 | if (!isLoad) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7190 | SDValue Val = cast<StoreSDNode>(N)->getValue(); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7191 | if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode())) |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7192 | return false; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7193 | } |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7194 | |
Hal Finkel | 089a5f8 | 2013-02-08 21:35:47 +0000 | [diff] [blame] | 7195 | // If the offset is a constant, there may be other adds of constants that |
7196 | // can be folded with this one. We should do this to avoid having to keep | ||||
7197 | // a copy of the original base pointer. | ||||
7198 | SmallVector<SDNode *, 16> OtherUses; | ||||
7199 | if (isa<ConstantSDNode>(Offset)) | ||||
7200 | for (SDNode::use_iterator I = BasePtr.getNode()->use_begin(), | ||||
7201 | E = BasePtr.getNode()->use_end(); I != E; ++I) { | ||||
7202 | SDNode *Use = *I; | ||||
7203 | if (Use == Ptr.getNode()) | ||||
7204 | continue; | ||||
7205 | |||||
7206 | if (Use->isPredecessorOf(N)) | ||||
7207 | continue; | ||||
7208 | |||||
7209 | if (Use->getOpcode() != ISD::ADD && Use->getOpcode() != ISD::SUB) { | ||||
7210 | OtherUses.clear(); | ||||
7211 | break; | ||||
7212 | } | ||||
7213 | |||||
7214 | SDValue Op0 = Use->getOperand(0), Op1 = Use->getOperand(1); | ||||
7215 | if (Op1.getNode() == BasePtr.getNode()) | ||||
7216 | std::swap(Op0, Op1); | ||||
7217 | assert(Op0.getNode() == BasePtr.getNode() && | ||||
7218 | "Use of ADD/SUB but not an operand"); | ||||
7219 | |||||
7220 | if (!isa<ConstantSDNode>(Op1)) { | ||||
7221 | OtherUses.clear(); | ||||
7222 | break; | ||||
7223 | } | ||||
7224 | |||||
7225 | // FIXME: In some cases, we can be smarter about this. | ||||
7226 | if (Op1.getValueType() != Offset.getValueType()) { | ||||
7227 | OtherUses.clear(); | ||||
7228 | break; | ||||
7229 | } | ||||
7230 | |||||
7231 | OtherUses.push_back(Use); | ||||
7232 | } | ||||
7233 | |||||
7234 | if (Swapped) | ||||
7235 | std::swap(BasePtr, Offset); | ||||
7236 | |||||
Evan Cheng | c843abe | 2007-05-24 02:35:39 +0000 | [diff] [blame] | 7237 | // Now check for #3 and #4. |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7238 | bool RealUse = false; |
Lang Hames | 944520f | 2011-07-07 04:31:51 +0000 | [diff] [blame] | 7239 | |
7240 | // Caches for hasPredecessorHelper | ||||
7241 | SmallPtrSet<const SDNode *, 32> Visited; | ||||
7242 | SmallVector<const SDNode *, 16> Worklist; | ||||
7243 | |||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7244 | for (SDNode::use_iterator I = Ptr.getNode()->use_begin(), |
7245 | E = Ptr.getNode()->use_end(); I != E; ++I) { | ||||
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 7246 | SDNode *Use = *I; |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7247 | if (Use == N) |
7248 | continue; | ||||
Lang Hames | 944520f | 2011-07-07 04:31:51 +0000 | [diff] [blame] | 7249 | if (N->hasPredecessorHelper(Use, Visited, Worklist)) |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7250 | return false; |
7251 | |||||
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 7252 | // If Ptr may be folded in addressing mode of other use, then it's |
7253 | // not profitable to do this transformation. | ||||
7254 | if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI)) | ||||
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7255 | RealUse = true; |
7256 | } | ||||
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7257 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7258 | if (!RealUse) |
7259 | return false; | ||||
7260 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7261 | SDValue Result; |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7262 | if (isLoad) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7263 | Result = DAG.getIndexedLoad(SDValue(N,0), SDLoc(N), |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7264 | BasePtr, Offset, AM); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7265 | else |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7266 | Result = DAG.getIndexedStore(SDValue(N,0), SDLoc(N), |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7267 | BasePtr, Offset, AM); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7268 | ++PreIndexedNodes; |
7269 | ++NodesCombined; | ||||
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7270 | DEBUG(dbgs() << "\nReplacing.4 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7271 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7272 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7273 | Result.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7274 | dbgs() << '\n'); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 7275 | WorkListRemover DeadNodes(*this); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7276 | if (isLoad) { |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7277 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0)); |
7278 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2)); | ||||
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7279 | } else { |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7280 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1)); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7281 | } |
7282 | |||||
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7283 | // Finally, since the node is now dead, remove it from the graph. |
7284 | DAG.DeleteNode(N); | ||||
7285 | |||||
Hal Finkel | 089a5f8 | 2013-02-08 21:35:47 +0000 | [diff] [blame] | 7286 | if (Swapped) |
7287 | std::swap(BasePtr, Offset); | ||||
7288 | |||||
7289 | // Replace other uses of BasePtr that can be updated to use Ptr | ||||
7290 | for (unsigned i = 0, e = OtherUses.size(); i != e; ++i) { | ||||
7291 | unsigned OffsetIdx = 1; | ||||
7292 | if (OtherUses[i]->getOperand(OffsetIdx).getNode() == BasePtr.getNode()) | ||||
7293 | OffsetIdx = 0; | ||||
7294 | assert(OtherUses[i]->getOperand(!OffsetIdx).getNode() == | ||||
7295 | BasePtr.getNode() && "Expected BasePtr operand"); | ||||
7296 | |||||
Silviu Baranga | 730a570 | 2013-04-26 15:52:24 +0000 | [diff] [blame] | 7297 | // We need to replace ptr0 in the following expression: |
7298 | // x0 * offset0 + y0 * ptr0 = t0 | ||||
7299 | // knowing that | ||||
7300 | // x1 * offset1 + y1 * ptr0 = t1 (the indexed load/store) | ||||
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 7301 | // |
Silviu Baranga | 730a570 | 2013-04-26 15:52:24 +0000 | [diff] [blame] | 7302 | // where x0, x1, y0 and y1 in {-1, 1} are given by the types of the |
7303 | // indexed load/store and the expresion that needs to be re-written. | ||||
7304 | // | ||||
7305 | // Therefore, we have: | ||||
7306 | // t0 = (x0 * offset0 - x1 * y0 * y1 *offset1) + (y0 * y1) * t1 | ||||
Hal Finkel | 089a5f8 | 2013-02-08 21:35:47 +0000 | [diff] [blame] | 7307 | |
7308 | ConstantSDNode *CN = | ||||
7309 | cast<ConstantSDNode>(OtherUses[i]->getOperand(OffsetIdx)); | ||||
Silviu Baranga | 730a570 | 2013-04-26 15:52:24 +0000 | [diff] [blame] | 7310 | int X0, X1, Y0, Y1; |
7311 | APInt Offset0 = CN->getAPIntValue(); | ||||
7312 | APInt Offset1 = cast<ConstantSDNode>(Offset)->getAPIntValue(); | ||||
Hal Finkel | 089a5f8 | 2013-02-08 21:35:47 +0000 | [diff] [blame] | 7313 | |
Silviu Baranga | 730a570 | 2013-04-26 15:52:24 +0000 | [diff] [blame] | 7314 | X0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 1) ? -1 : 1; |
7315 | Y0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 0) ? -1 : 1; | ||||
7316 | X1 = (AM == ISD::PRE_DEC && !Swapped) ? -1 : 1; | ||||
7317 | Y1 = (AM == ISD::PRE_DEC && Swapped) ? -1 : 1; | ||||
Hal Finkel | 089a5f8 | 2013-02-08 21:35:47 +0000 | [diff] [blame] | 7318 | |
Silviu Baranga | 730a570 | 2013-04-26 15:52:24 +0000 | [diff] [blame] | 7319 | unsigned Opcode = (Y0 * Y1 < 0) ? ISD::SUB : ISD::ADD; |
7320 | |||||
7321 | APInt CNV = Offset0; | ||||
7322 | if (X0 < 0) CNV = -CNV; | ||||
7323 | if (X1 * Y0 * Y1 < 0) CNV = CNV + Offset1; | ||||
7324 | else CNV = CNV - Offset1; | ||||
7325 | |||||
7326 | // We can now generate the new expression. | ||||
7327 | SDValue NewOp1 = DAG.getConstant(CNV, CN->getValueType(0)); | ||||
7328 | SDValue NewOp2 = Result.getValue(isLoad ? 1 : 0); | ||||
7329 | |||||
7330 | SDValue NewUse = DAG.getNode(Opcode, | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7331 | SDLoc(OtherUses[i]), |
Hal Finkel | 089a5f8 | 2013-02-08 21:35:47 +0000 | [diff] [blame] | 7332 | OtherUses[i]->getValueType(0), NewOp1, NewOp2); |
7333 | DAG.ReplaceAllUsesOfValueWith(SDValue(OtherUses[i], 0), NewUse); | ||||
7334 | removeFromWorkList(OtherUses[i]); | ||||
7335 | DAG.DeleteNode(OtherUses[i]); | ||||
7336 | } | ||||
7337 | |||||
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7338 | // Replace the uses of Ptr with uses of the updated base value. |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7339 | DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7340 | removeFromWorkList(Ptr.getNode()); |
7341 | DAG.DeleteNode(Ptr.getNode()); | ||||
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7342 | |
7343 | return true; | ||||
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7344 | } |
7345 | |||||
Duncan Sands | ec87aa8 | 2008-06-15 20:12:31 +0000 | [diff] [blame] | 7346 | /// CombineToPostIndexedLoadStore - Try to combine a load / store with a |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7347 | /// add / sub of the base pointer node into a post-indexed load / store. |
7348 | /// The transformation folded the add / subtract into the new indexed | ||||
7349 | /// load / store effectively and all of its uses are redirected to the | ||||
7350 | /// new load / store. | ||||
7351 | bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) { | ||||
Eli Friedman | 5018524 | 2011-11-12 00:35:34 +0000 | [diff] [blame] | 7352 | if (Level < AfterLegalizeDAG) |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7353 | return false; |
7354 | |||||
7355 | bool isLoad = true; | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7356 | SDValue Ptr; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7357 | EVT VT; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7358 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 7359 | if (LD->isIndexed()) |
Evan Cheng | e90460e | 2006-12-16 06:25:23 +0000 | [diff] [blame] | 7360 | return false; |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 7361 | VT = LD->getMemoryVT(); |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7362 | if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) && |
7363 | !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT)) | ||||
7364 | return false; | ||||
7365 | Ptr = LD->getBasePtr(); | ||||
7366 | } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { | ||||
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 7367 | if (ST->isIndexed()) |
Evan Cheng | e90460e | 2006-12-16 06:25:23 +0000 | [diff] [blame] | 7368 | return false; |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 7369 | VT = ST->getMemoryVT(); |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7370 | if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) && |
7371 | !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT)) | ||||
7372 | return false; | ||||
7373 | Ptr = ST->getBasePtr(); | ||||
7374 | isLoad = false; | ||||
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7375 | } else { |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7376 | return false; |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7377 | } |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7378 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7379 | if (Ptr.getNode()->hasOneUse()) |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7380 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7381 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7382 | for (SDNode::use_iterator I = Ptr.getNode()->use_begin(), |
7383 | E = Ptr.getNode()->use_end(); I != E; ++I) { | ||||
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 7384 | SDNode *Op = *I; |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7385 | if (Op == N || |
7386 | (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB)) | ||||
7387 | continue; | ||||
7388 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7389 | SDValue BasePtr; |
7390 | SDValue Offset; | ||||
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7391 | ISD::MemIndexedMode AM = ISD::UNINDEXED; |
7392 | if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) { | ||||
Evan Cheng | a7d4a04 | 2007-05-03 23:52:19 +0000 | [diff] [blame] | 7393 | // Don't create a indexed load / store with zero offset. |
7394 | if (isa<ConstantSDNode>(Offset) && | ||||
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 7395 | cast<ConstantSDNode>(Offset)->isNullValue()) |
Evan Cheng | a7d4a04 | 2007-05-03 23:52:19 +0000 | [diff] [blame] | 7396 | continue; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7397 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7398 | // Try turning it into a post-indexed load / store except when |
Evan Cheng | c4b527a | 2012-01-13 01:37:24 +0000 | [diff] [blame] | 7399 | // 1) All uses are load / store ops that use it as base ptr (and |
7400 | // it may be folded as addressing mmode). | ||||
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7401 | // 2) Op must be independent of N, i.e. Op is neither a predecessor |
7402 | // nor a successor of N. Otherwise, if Op is folded that would | ||||
7403 | // create a cycle. | ||||
7404 | |||||
Evan Cheng | caab129 | 2009-05-06 18:25:01 +0000 | [diff] [blame] | 7405 | if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr)) |
7406 | continue; | ||||
7407 | |||||
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7408 | // Check for #1. |
7409 | bool TryNext = false; | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7410 | for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(), |
7411 | EE = BasePtr.getNode()->use_end(); II != EE; ++II) { | ||||
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 7412 | SDNode *Use = *II; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7413 | if (Use == Ptr.getNode()) |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7414 | continue; |
7415 | |||||
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7416 | // If all the uses are load / store addresses, then don't do the |
7417 | // transformation. | ||||
7418 | if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){ | ||||
7419 | bool RealUse = false; | ||||
7420 | for (SDNode::use_iterator III = Use->use_begin(), | ||||
7421 | EEE = Use->use_end(); III != EEE; ++III) { | ||||
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 7422 | SDNode *UseUse = *III; |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 7423 | if (!canFoldInAddressingMode(Use, UseUse, DAG, TLI)) |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7424 | RealUse = true; |
7425 | } | ||||
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7426 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7427 | if (!RealUse) { |
7428 | TryNext = true; | ||||
7429 | break; | ||||
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7430 | } |
7431 | } | ||||
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7432 | } |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7433 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7434 | if (TryNext) |
7435 | continue; | ||||
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7436 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7437 | // Check for #2 |
Evan Cheng | 917be68 | 2008-03-04 00:41:45 +0000 | [diff] [blame] | 7438 | if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7439 | SDValue Result = isLoad |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7440 | ? DAG.getIndexedLoad(SDValue(N,0), SDLoc(N), |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7441 | BasePtr, Offset, AM) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7442 | : DAG.getIndexedStore(SDValue(N,0), SDLoc(N), |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7443 | BasePtr, Offset, AM); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7444 | ++PostIndexedNodes; |
7445 | ++NodesCombined; | ||||
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7446 | DEBUG(dbgs() << "\nReplacing.5 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7447 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7448 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7449 | Result.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7450 | dbgs() << '\n'); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 7451 | WorkListRemover DeadNodes(*this); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7452 | if (isLoad) { |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7453 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0)); |
7454 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2)); | ||||
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7455 | } else { |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7456 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1)); |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7457 | } |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7458 | |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7459 | // Finally, since the node is now dead, remove it from the graph. |
7460 | DAG.DeleteNode(N); | ||||
7461 | |||||
7462 | // Replace the uses of Use with uses of the updated base value. | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7463 | DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0), |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7464 | Result.getValue(isLoad ? 1 : 0)); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7465 | removeFromWorkList(Op); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7466 | DAG.DeleteNode(Op); |
Chris Lattner | 9f1794e | 2006-11-11 00:56:29 +0000 | [diff] [blame] | 7467 | return true; |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7468 | } |
7469 | } | ||||
7470 | } | ||||
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7471 | |
Chris Lattner | 448f219 | 2006-11-11 00:39:41 +0000 | [diff] [blame] | 7472 | return false; |
7473 | } | ||||
7474 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7475 | SDValue DAGCombiner::visitLOAD(SDNode *N) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 7476 | LoadSDNode *LD = cast<LoadSDNode>(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7477 | SDValue Chain = LD->getChain(); |
7478 | SDValue Ptr = LD->getBasePtr(); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7479 | |
Evan Cheng | 45a7ca9 | 2007-05-01 00:38:21 +0000 | [diff] [blame] | 7480 | // If load is not volatile and there are no uses of the loaded value (and |
7481 | // the updated indexed value in case of indexed loads), change uses of the | ||||
7482 | // chain value into uses of the chain input (i.e. delete the dead load). | ||||
7483 | if (!LD->isVolatile()) { | ||||
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7484 | if (N->getValueType(1) == MVT::Other) { |
Evan Cheng | 498f559 | 2007-05-01 08:53:39 +0000 | [diff] [blame] | 7485 | // Unindexed loads. |
Craig Topper | 704e1a0 | 2012-01-07 18:31:09 +0000 | [diff] [blame] | 7486 | if (!N->hasAnyUseOfValue(0)) { |
Evan Cheng | 02c4285 | 2008-01-16 23:11:54 +0000 | [diff] [blame] | 7487 | // It's not safe to use the two value CombineTo variant here. e.g. |
7488 | // v1, chain2 = load chain1, loc | ||||
7489 | // v2, chain3 = load chain2, loc | ||||
7490 | // v3 = add v2, c | ||||
Chris Lattner | 125991a | 2008-01-24 07:57:06 +0000 | [diff] [blame] | 7491 | // Now we replace use of chain2 with chain1. This makes the second load |
7492 | // isomorphic to the one we are deleting, and thus makes this load live. | ||||
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7493 | DEBUG(dbgs() << "\nReplacing.6 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7494 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7495 | dbgs() << "\nWith chain: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7496 | Chain.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7497 | dbgs() << "\n"); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 7498 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7499 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain); |
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7500 | |
Chris Lattner | 125991a | 2008-01-24 07:57:06 +0000 | [diff] [blame] | 7501 | if (N->use_empty()) { |
7502 | removeFromWorkList(N); | ||||
7503 | DAG.DeleteNode(N); | ||||
7504 | } | ||||
Bill Wendling | c0debad | 2009-01-30 23:27:35 +0000 | [diff] [blame] | 7505 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7506 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 02c4285 | 2008-01-16 23:11:54 +0000 | [diff] [blame] | 7507 | } |
Evan Cheng | 498f559 | 2007-05-01 08:53:39 +0000 | [diff] [blame] | 7508 | } else { |
7509 | // Indexed loads. | ||||
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7510 | assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?"); |
Craig Topper | 704e1a0 | 2012-01-07 18:31:09 +0000 | [diff] [blame] | 7511 | if (!N->hasAnyUseOfValue(0) && !N->hasAnyUseOfValue(1)) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 7512 | SDValue Undef = DAG.getUNDEF(N->getValueType(0)); |
Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 7513 | DEBUG(dbgs() << "\nReplacing.7 "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7514 | N->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7515 | dbgs() << "\nWith: "; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 7516 | Undef.getNode()->dump(&DAG); |
David Greene | f109029 | 2010-01-05 01:25:00 +0000 | [diff] [blame] | 7517 | dbgs() << " and 2 other values\n"); |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 7518 | WorkListRemover DeadNodes(*this); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7519 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7520 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 7521 | DAG.getUNDEF(N->getValueType(1))); |
7522 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain); | ||||
Evan Cheng | 02c4285 | 2008-01-16 23:11:54 +0000 | [diff] [blame] | 7523 | removeFromWorkList(N); |
Evan Cheng | 02c4285 | 2008-01-16 23:11:54 +0000 | [diff] [blame] | 7524 | DAG.DeleteNode(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7525 | return SDValue(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 45a7ca9 | 2007-05-01 00:38:21 +0000 | [diff] [blame] | 7526 | } |
Evan Cheng | 45a7ca9 | 2007-05-01 00:38:21 +0000 | [diff] [blame] | 7527 | } |
7528 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7529 | |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 7530 | // If this load is directly stored, replace the load value with the stored |
7531 | // value. | ||||
7532 | // TODO: Handle store large -> read small portion. | ||||
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 7533 | // TODO: Handle TRUNCSTORE/LOADEXT |
Evan Cheng | 9ef82ce | 2011-03-11 00:48:56 +0000 | [diff] [blame] | 7534 | if (ISD::isNormalLoad(N) && !LD->isVolatile()) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7535 | if (ISD::isNON_TRUNCStore(Chain.getNode())) { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 7536 | StoreSDNode *PrevST = cast<StoreSDNode>(Chain); |
7537 | if (PrevST->getBasePtr() == Ptr && | ||||
7538 | PrevST->getValue().getValueType() == N->getValueType(0)) | ||||
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 7539 | return CombineTo(N, Chain.getOperand(1), Chain); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 7540 | } |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 7541 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7542 | |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 7543 | // Try to infer better alignment information than the load already has. |
7544 | if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) { | ||||
Evan Cheng | ed1c0c7 | 2011-11-28 22:37:34 +0000 | [diff] [blame] | 7545 | if (unsigned Align = DAG.InferPtrAlignment(Ptr)) { |
Owen Anderson | b48783b | 2013-02-05 19:24:39 +0000 | [diff] [blame] | 7546 | if (Align > LD->getMemOperand()->getBaseAlignment()) { |
7547 | SDValue NewLoad = | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7548 | DAG.getExtLoad(LD->getExtensionType(), SDLoc(N), |
Evan Cheng | ed1c0c7 | 2011-11-28 22:37:34 +0000 | [diff] [blame] | 7549 | LD->getValueType(0), |
7550 | Chain, Ptr, LD->getPointerInfo(), | ||||
7551 | LD->getMemoryVT(), | ||||
7552 | LD->isVolatile(), LD->isNonTemporal(), Align); | ||||
Owen Anderson | b48783b | 2013-02-05 19:24:39 +0000 | [diff] [blame] | 7553 | return CombineTo(N, NewLoad, SDValue(NewLoad.getNode(), 1), true); |
7554 | } | ||||
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 7555 | } |
7556 | } | ||||
7557 | |||||
Hal Finkel | 253acef | 2013-08-29 03:29:55 +0000 | [diff] [blame] | 7558 | bool UseAA = CombinerAA.getNumOccurrences() > 0 ? CombinerAA : |
7559 | TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA(); | ||||
7560 | if (UseAA) { | ||||
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7561 | // Walk up chain skipping non-aliasing memory nodes. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7562 | SDValue BetterChain = FindBetterChain(N, Chain); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7563 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 7564 | // If there is a better chain. |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7565 | if (Chain != BetterChain) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7566 | SDValue ReplLoad; |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 7567 | |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7568 | // Replace the chain to void dependency. |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 7569 | if (LD->getExtensionType() == ISD::NON_EXTLOAD) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7570 | ReplLoad = DAG.getLoad(N->getValueType(0), SDLoc(LD), |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 7571 | BetterChain, Ptr, LD->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 7572 | LD->isVolatile(), LD->isNonTemporal(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 7573 | LD->isInvariant(), LD->getAlignment()); |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 7574 | } else { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7575 | ReplLoad = DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 7576 | LD->getValueType(0), |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 7577 | BetterChain, Ptr, LD->getPointerInfo(), |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 7578 | LD->getMemoryVT(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7579 | LD->isVolatile(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 7580 | LD->isNonTemporal(), |
Christopher Lamb | 95c218a | 2007-04-22 23:15:30 +0000 | [diff] [blame] | 7581 | LD->getAlignment()); |
Jim Laskey | c2b19f3 | 2006-10-11 17:47:52 +0000 | [diff] [blame] | 7582 | } |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7583 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 7584 | // Create token factor to keep old chain connected. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7585 | SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7586 | MVT::Other, Chain, ReplLoad.getValue(1)); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7587 | |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 7588 | // Make sure the new and old chains are cleaned up. |
7589 | AddToWorkList(Token.getNode()); | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7590 | |
Jim Laskey | 274062c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 7591 | // Replace uses with load result and token factor. Don't add users |
7592 | // to work list. | ||||
7593 | return CombineTo(N, ReplLoad.getValue(0), Token, false); | ||||
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 7594 | } |
7595 | } | ||||
7596 | |||||
Evan Cheng | 7fc033a | 2006-11-03 03:06:21 +0000 | [diff] [blame] | 7597 | // Try transforming N to an indexed load. |
Evan Cheng | bbd6f6e | 2006-11-07 09:03:05 +0000 | [diff] [blame] | 7598 | if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7599 | return SDValue(N, 0); |
Evan Cheng | 7fc033a | 2006-11-03 03:06:21 +0000 | [diff] [blame] | 7600 | |
Quentin Colombet | 83f743a | 2013-10-11 18:29:42 +0000 | [diff] [blame] | 7601 | // Try to slice up N to more direct loads if the slices are mapped to |
7602 | // different register banks or pairing can take place. | ||||
7603 | if (SliceUpLoad(N)) | ||||
7604 | return SDValue(N, 0); | ||||
7605 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7606 | return SDValue(); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 7607 | } |
7608 | |||||
Quentin Colombet | 83f743a | 2013-10-11 18:29:42 +0000 | [diff] [blame] | 7609 | namespace { |
7610 | /// \brief Helper structure used to slice a load in smaller loads. | ||||
7611 | /// Basically a slice is obtained from the following sequence: | ||||
7612 | /// Origin = load Ty1, Base | ||||
7613 | /// Shift = srl Ty1 Origin, CstTy Amount | ||||
7614 | /// Inst = trunc Shift to Ty2 | ||||
7615 | /// | ||||
7616 | /// Then, it will be rewriten into: | ||||
7617 | /// Slice = load SliceTy, Base + SliceOffset | ||||
7618 | /// [Inst = zext Slice to Ty2], only if SliceTy <> Ty2 | ||||
7619 | /// | ||||
7620 | /// SliceTy is deduced from the number of bits that are actually used to | ||||
7621 | /// build Inst. | ||||
7622 | struct LoadedSlice { | ||||
7623 | /// \brief Helper structure used to compute the cost of a slice. | ||||
7624 | struct Cost { | ||||
7625 | /// Are we optimizing for code size. | ||||
7626 | bool ForCodeSize; | ||||
7627 | /// Various cost. | ||||
7628 | unsigned Loads; | ||||
7629 | unsigned Truncates; | ||||
7630 | unsigned CrossRegisterBanksCopies; | ||||
7631 | unsigned ZExts; | ||||
7632 | unsigned Shift; | ||||
7633 | |||||
7634 | Cost(bool ForCodeSize = false) | ||||
7635 | : ForCodeSize(ForCodeSize), Loads(0), Truncates(0), | ||||
7636 | CrossRegisterBanksCopies(0), ZExts(0), Shift(0) {} | ||||
7637 | |||||
7638 | /// \brief Get the cost of one isolated slice. | ||||
7639 | Cost(const LoadedSlice &LS, bool ForCodeSize = false) | ||||
7640 | : ForCodeSize(ForCodeSize), Loads(1), Truncates(0), | ||||
7641 | CrossRegisterBanksCopies(0), ZExts(0), Shift(0) { | ||||
7642 | EVT TruncType = LS.Inst->getValueType(0); | ||||
7643 | EVT LoadedType = LS.getLoadedType(); | ||||
7644 | if (TruncType != LoadedType && | ||||
7645 | !LS.DAG->getTargetLoweringInfo().isZExtFree(LoadedType, TruncType)) | ||||
7646 | ZExts = 1; | ||||
7647 | } | ||||
7648 | |||||
7649 | /// \brief Account for slicing gain in the current cost. | ||||
7650 | /// Slicing provide a few gains like removing a shift or a | ||||
7651 | /// truncate. This method allows to grow the cost of the original | ||||
7652 | /// load with the gain from this slice. | ||||
7653 | void addSliceGain(const LoadedSlice &LS) { | ||||
7654 | // Each slice saves a truncate. | ||||
7655 | const TargetLowering &TLI = LS.DAG->getTargetLoweringInfo(); | ||||
7656 | if (!TLI.isTruncateFree(LS.Inst->getValueType(0), | ||||
7657 | LS.Inst->getOperand(0).getValueType())) | ||||
7658 | ++Truncates; | ||||
7659 | // If there is a shift amount, this slice gets rid of it. | ||||
7660 | if (LS.Shift) | ||||
7661 | ++Shift; | ||||
7662 | // If this slice can merge a cross register bank copy, account for it. | ||||
7663 | if (LS.canMergeExpensiveCrossRegisterBankCopy()) | ||||
7664 | ++CrossRegisterBanksCopies; | ||||
7665 | } | ||||
7666 | |||||
7667 | Cost &operator+=(const Cost &RHS) { | ||||
7668 | Loads += RHS.Loads; | ||||
7669 | Truncates += RHS.Truncates; | ||||
7670 | CrossRegisterBanksCopies += RHS.CrossRegisterBanksCopies; | ||||
7671 | ZExts += RHS.ZExts; | ||||
7672 | Shift += RHS.Shift; | ||||
7673 | return *this; | ||||
7674 | } | ||||
7675 | |||||
7676 | bool operator==(const Cost &RHS) const { | ||||
7677 | return Loads == RHS.Loads && Truncates == RHS.Truncates && | ||||
7678 | CrossRegisterBanksCopies == RHS.CrossRegisterBanksCopies && | ||||
7679 | ZExts == RHS.ZExts && Shift == RHS.Shift; | ||||
7680 | } | ||||
7681 | |||||
7682 | bool operator!=(const Cost &RHS) const { return !(*this == RHS); } | ||||
7683 | |||||
7684 | bool operator<(const Cost &RHS) const { | ||||
7685 | // Assume cross register banks copies are as expensive as loads. | ||||
7686 | // FIXME: Do we want some more target hooks? | ||||
7687 | unsigned ExpensiveOpsLHS = Loads + CrossRegisterBanksCopies; | ||||
7688 | unsigned ExpensiveOpsRHS = RHS.Loads + RHS.CrossRegisterBanksCopies; | ||||
7689 | // Unless we are optimizing for code size, consider the | ||||
7690 | // expensive operation first. | ||||
7691 | if (!ForCodeSize && ExpensiveOpsLHS != ExpensiveOpsRHS) | ||||
7692 | return ExpensiveOpsLHS < ExpensiveOpsRHS; | ||||
7693 | return (Truncates + ZExts + Shift + ExpensiveOpsLHS) < | ||||
7694 | (RHS.Truncates + RHS.ZExts + RHS.Shift + ExpensiveOpsRHS); | ||||
7695 | } | ||||
7696 | |||||
7697 | bool operator>(const Cost &RHS) const { return RHS < *this; } | ||||
7698 | |||||
7699 | bool operator<=(const Cost &RHS) const { return !(RHS < *this); } | ||||
7700 | |||||
7701 | bool operator>=(const Cost &RHS) const { return !(*this < RHS); } | ||||
7702 | }; | ||||
7703 | // The last instruction that represent the slice. This should be a | ||||
7704 | // truncate instruction. | ||||
7705 | SDNode *Inst; | ||||
7706 | // The original load instruction. | ||||
7707 | LoadSDNode *Origin; | ||||
7708 | // The right shift amount in bits from the original load. | ||||
7709 | unsigned Shift; | ||||
7710 | // The DAG from which Origin came from. | ||||
7711 | // This is used to get some contextual information about legal types, etc. | ||||
7712 | SelectionDAG *DAG; | ||||
7713 | |||||
7714 | LoadedSlice(SDNode *Inst = NULL, LoadSDNode *Origin = NULL, | ||||
7715 | unsigned Shift = 0, SelectionDAG *DAG = NULL) | ||||
7716 | : Inst(Inst), Origin(Origin), Shift(Shift), DAG(DAG) {} | ||||
7717 | |||||
7718 | LoadedSlice(const LoadedSlice &LS) | ||||
7719 | : Inst(LS.Inst), Origin(LS.Origin), Shift(LS.Shift), DAG(LS.DAG) {} | ||||
7720 | |||||
7721 | /// \brief Get the bits used in a chunk of bits \p BitWidth large. | ||||
7722 | /// \return Result is \p BitWidth and has used bits set to 1 and | ||||
7723 | /// not used bits set to 0. | ||||
7724 | APInt getUsedBits() const { | ||||
7725 | // Reproduce the trunc(lshr) sequence: | ||||
7726 | // - Start from the truncated value. | ||||
7727 | // - Zero extend to the desired bit width. | ||||
7728 | // - Shift left. | ||||
7729 | assert(Origin && "No original load to compare against."); | ||||
7730 | unsigned BitWidth = Origin->getValueSizeInBits(0); | ||||
7731 | assert(Inst && "This slice is not bound to an instruction"); | ||||
7732 | assert(Inst->getValueSizeInBits(0) <= BitWidth && | ||||
7733 | "Extracted slice is bigger than the whole type!"); | ||||
7734 | APInt UsedBits(Inst->getValueSizeInBits(0), 0); | ||||
7735 | UsedBits.setAllBits(); | ||||
7736 | UsedBits = UsedBits.zext(BitWidth); | ||||
7737 | UsedBits <<= Shift; | ||||
7738 | return UsedBits; | ||||
7739 | } | ||||
7740 | |||||
7741 | /// \brief Get the size of the slice to be loaded in bytes. | ||||
7742 | unsigned getLoadedSize() const { | ||||
7743 | unsigned SliceSize = getUsedBits().countPopulation(); | ||||
7744 | assert(!(SliceSize & 0x7) && "Size is not a multiple of a byte."); | ||||
7745 | return SliceSize / 8; | ||||
7746 | } | ||||
7747 | |||||
7748 | /// \brief Get the type that will be loaded for this slice. | ||||
7749 | /// Note: This may not be the final type for the slice. | ||||
7750 | EVT getLoadedType() const { | ||||
7751 | assert(DAG && "Missing context"); | ||||
7752 | LLVMContext &Ctxt = *DAG->getContext(); | ||||
7753 | return EVT::getIntegerVT(Ctxt, getLoadedSize() * 8); | ||||
7754 | } | ||||
7755 | |||||
7756 | /// \brief Get the alignment of the load used for this slice. | ||||
7757 | unsigned getAlignment() const { | ||||
7758 | unsigned Alignment = Origin->getAlignment(); | ||||
7759 | unsigned Offset = getOffsetFromBase(); | ||||
7760 | if (Offset != 0) | ||||
7761 | Alignment = MinAlign(Alignment, Alignment + Offset); | ||||
7762 | return Alignment; | ||||
7763 | } | ||||
7764 | |||||
7765 | /// \brief Check if this slice can be rewritten with legal operations. | ||||
7766 | bool isLegal() const { | ||||
7767 | // An invalid slice is not legal. | ||||
7768 | if (!Origin || !Inst || !DAG) | ||||
7769 | return false; | ||||
7770 | |||||
7771 | // Offsets are for indexed load only, we do not handle that. | ||||
7772 | if (Origin->getOffset().getOpcode() != ISD::UNDEF) | ||||
7773 | return false; | ||||
7774 | |||||
7775 | const TargetLowering &TLI = DAG->getTargetLoweringInfo(); | ||||
7776 | |||||
7777 | // Check that the type is legal. | ||||
7778 | EVT SliceType = getLoadedType(); | ||||
7779 | if (!TLI.isTypeLegal(SliceType)) | ||||
7780 | return false; | ||||
7781 | |||||
7782 | // Check that the load is legal for this type. | ||||
7783 | if (!TLI.isOperationLegal(ISD::LOAD, SliceType)) | ||||
7784 | return false; | ||||
7785 | |||||
7786 | // Check that the offset can be computed. | ||||
7787 | // 1. Check its type. | ||||
7788 | EVT PtrType = Origin->getBasePtr().getValueType(); | ||||
7789 | if (PtrType == MVT::Untyped || PtrType.isExtended()) | ||||
7790 | return false; | ||||
7791 | |||||
7792 | // 2. Check that it fits in the immediate. | ||||
7793 | if (!TLI.isLegalAddImmediate(getOffsetFromBase())) | ||||
7794 | return false; | ||||
7795 | |||||
7796 | // 3. Check that the computation is legal. | ||||
7797 | if (!TLI.isOperationLegal(ISD::ADD, PtrType)) | ||||
7798 | return false; | ||||
7799 | |||||
7800 | // Check that the zext is legal if it needs one. | ||||
7801 | EVT TruncateType = Inst->getValueType(0); | ||||
7802 | if (TruncateType != SliceType && | ||||
7803 | !TLI.isOperationLegal(ISD::ZERO_EXTEND, TruncateType)) | ||||
7804 | return false; | ||||
7805 | |||||
7806 | return true; | ||||
7807 | } | ||||
7808 | |||||
7809 | /// \brief Get the offset in bytes of this slice in the original chunk of | ||||
7810 | /// bits. | ||||
7811 | /// \pre DAG != NULL. | ||||
7812 | uint64_t getOffsetFromBase() const { | ||||
7813 | assert(DAG && "Missing context."); | ||||
7814 | bool IsBigEndian = | ||||
7815 | DAG->getTargetLoweringInfo().getDataLayout()->isBigEndian(); | ||||
7816 | assert(!(Shift & 0x7) && "Shifts not aligned on Bytes are not supported."); | ||||
7817 | uint64_t Offset = Shift / 8; | ||||
7818 | unsigned TySizeInBytes = Origin->getValueSizeInBits(0) / 8; | ||||
7819 | assert(!(Origin->getValueSizeInBits(0) & 0x7) && | ||||
7820 | "The size of the original loaded type is not a multiple of a" | ||||
7821 | " byte."); | ||||
7822 | // If Offset is bigger than TySizeInBytes, it means we are loading all | ||||
7823 | // zeros. This should have been optimized before in the process. | ||||
7824 | assert(TySizeInBytes > Offset && | ||||
7825 | "Invalid shift amount for given loaded size"); | ||||
7826 | if (IsBigEndian) | ||||
7827 | Offset = TySizeInBytes - Offset - getLoadedSize(); | ||||
7828 | return Offset; | ||||
7829 | } | ||||
7830 | |||||
7831 | /// \brief Generate the sequence of instructions to load the slice | ||||
7832 | /// represented by this object and redirect the uses of this slice to | ||||
7833 | /// this new sequence of instructions. | ||||
7834 | /// \pre this->Inst && this->Origin are valid Instructions and this | ||||
7835 | /// object passed the legal check: LoadedSlice::isLegal returned true. | ||||
7836 | /// \return The last instruction of the sequence used to load the slice. | ||||
7837 | SDValue loadSlice() const { | ||||
7838 | assert(Inst && Origin && "Unable to replace a non-existing slice."); | ||||
7839 | const SDValue &OldBaseAddr = Origin->getBasePtr(); | ||||
7840 | SDValue BaseAddr = OldBaseAddr; | ||||
7841 | // Get the offset in that chunk of bytes w.r.t. the endianess. | ||||
7842 | int64_t Offset = static_cast<int64_t>(getOffsetFromBase()); | ||||
7843 | assert(Offset >= 0 && "Offset too big to fit in int64_t!"); | ||||
7844 | if (Offset) { | ||||
7845 | // BaseAddr = BaseAddr + Offset. | ||||
7846 | EVT ArithType = BaseAddr.getValueType(); | ||||
7847 | BaseAddr = DAG->getNode(ISD::ADD, SDLoc(Origin), ArithType, BaseAddr, | ||||
7848 | DAG->getConstant(Offset, ArithType)); | ||||
7849 | } | ||||
7850 | |||||
7851 | // Create the type of the loaded slice according to its size. | ||||
7852 | EVT SliceType = getLoadedType(); | ||||
7853 | |||||
7854 | // Create the load for the slice. | ||||
7855 | SDValue LastInst = DAG->getLoad( | ||||
7856 | SliceType, SDLoc(Origin), Origin->getChain(), BaseAddr, | ||||
7857 | Origin->getPointerInfo().getWithOffset(Offset), Origin->isVolatile(), | ||||
7858 | Origin->isNonTemporal(), Origin->isInvariant(), getAlignment()); | ||||
7859 | // If the final type is not the same as the loaded type, this means that | ||||
7860 | // we have to pad with zero. Create a zero extend for that. | ||||
7861 | EVT FinalType = Inst->getValueType(0); | ||||
7862 | if (SliceType != FinalType) | ||||
7863 | LastInst = | ||||
7864 | DAG->getNode(ISD::ZERO_EXTEND, SDLoc(LastInst), FinalType, LastInst); | ||||
7865 | return LastInst; | ||||
7866 | } | ||||
7867 | |||||
7868 | /// \brief Check if this slice can be merged with an expensive cross register | ||||
7869 | /// bank copy. E.g., | ||||
7870 | /// i = load i32 | ||||
7871 | /// f = bitcast i32 i to float | ||||
7872 | bool canMergeExpensiveCrossRegisterBankCopy() const { | ||||
7873 | if (!Inst || !Inst->hasOneUse()) | ||||
7874 | return false; | ||||
7875 | SDNode *Use = *Inst->use_begin(); | ||||
7876 | if (Use->getOpcode() != ISD::BITCAST) | ||||
7877 | return false; | ||||
7878 | assert(DAG && "Missing context"); | ||||
7879 | const TargetLowering &TLI = DAG->getTargetLoweringInfo(); | ||||
7880 | EVT ResVT = Use->getValueType(0); | ||||
7881 | const TargetRegisterClass *ResRC = TLI.getRegClassFor(ResVT.getSimpleVT()); | ||||
7882 | const TargetRegisterClass *ArgRC = | ||||
7883 | TLI.getRegClassFor(Use->getOperand(0).getValueType().getSimpleVT()); | ||||
7884 | if (ArgRC == ResRC || !TLI.isOperationLegal(ISD::LOAD, ResVT)) | ||||
7885 | return false; | ||||
7886 | |||||
7887 | // At this point, we know that we perform a cross-register-bank copy. | ||||
7888 | // Check if it is expensive. | ||||
7889 | const TargetRegisterInfo *TRI = TLI.getTargetMachine().getRegisterInfo(); | ||||
7890 | // Assume bitcasts are cheap, unless both register classes do not | ||||
7891 | // explicitly share a common sub class. | ||||
7892 | if (!TRI || TRI->getCommonSubClass(ArgRC, ResRC)) | ||||
7893 | return false; | ||||
7894 | |||||
7895 | // Check if it will be merged with the load. | ||||
7896 | // 1. Check the alignment constraint. | ||||
7897 | unsigned RequiredAlignment = TLI.getDataLayout()->getABITypeAlignment( | ||||
7898 | ResVT.getTypeForEVT(*DAG->getContext())); | ||||
7899 | |||||
7900 | if (RequiredAlignment > getAlignment()) | ||||
7901 | return false; | ||||
7902 | |||||
7903 | // 2. Check that the load is a legal operation for that type. | ||||
7904 | if (!TLI.isOperationLegal(ISD::LOAD, ResVT)) | ||||
7905 | return false; | ||||
7906 | |||||
7907 | // 3. Check that we do not have a zext in the way. | ||||
7908 | if (Inst->getValueType(0) != getLoadedType()) | ||||
7909 | return false; | ||||
7910 | |||||
7911 | return true; | ||||
7912 | } | ||||
7913 | }; | ||||
7914 | } | ||||
7915 | |||||
7916 | /// \brief Sorts LoadedSlice according to their offset. | ||||
7917 | struct LoadedSliceSorter { | ||||
7918 | bool operator()(const LoadedSlice &LHS, const LoadedSlice &RHS) { | ||||
7919 | assert(LHS.Origin == RHS.Origin && "Different bases not implemented."); | ||||
7920 | return LHS.getOffsetFromBase() < RHS.getOffsetFromBase(); | ||||
7921 | } | ||||
7922 | }; | ||||
7923 | |||||
7924 | /// \brief Check that all bits set in \p UsedBits form a dense region, i.e., | ||||
7925 | /// \p UsedBits looks like 0..0 1..1 0..0. | ||||
7926 | static bool areUsedBitsDense(const APInt &UsedBits) { | ||||
7927 | // If all the bits are one, this is dense! | ||||
7928 | if (UsedBits.isAllOnesValue()) | ||||
7929 | return true; | ||||
7930 | |||||
7931 | // Get rid of the unused bits on the right. | ||||
7932 | APInt NarrowedUsedBits = UsedBits.lshr(UsedBits.countTrailingZeros()); | ||||
7933 | // Get rid of the unused bits on the left. | ||||
7934 | if (NarrowedUsedBits.countLeadingZeros()) | ||||
7935 | NarrowedUsedBits = NarrowedUsedBits.trunc(NarrowedUsedBits.getActiveBits()); | ||||
7936 | // Check that the chunk of bits is completely used. | ||||
7937 | return NarrowedUsedBits.isAllOnesValue(); | ||||
7938 | } | ||||
7939 | |||||
7940 | /// \brief Check whether or not \p First and \p Second are next to each other | ||||
7941 | /// in memory. This means that there is no hole between the bits loaded | ||||
7942 | /// by \p First and the bits loaded by \p Second. | ||||
7943 | static bool areSlicesNextToEachOther(const LoadedSlice &First, | ||||
7944 | const LoadedSlice &Second) { | ||||
7945 | assert(First.Origin == Second.Origin && First.Origin && | ||||
7946 | "Unable to match different memory origins."); | ||||
7947 | APInt UsedBits = First.getUsedBits(); | ||||
7948 | assert((UsedBits & Second.getUsedBits()) == 0 && | ||||
7949 | "Slices are not supposed to overlap."); | ||||
7950 | UsedBits |= Second.getUsedBits(); | ||||
7951 | return areUsedBitsDense(UsedBits); | ||||
7952 | } | ||||
7953 | |||||
7954 | /// \brief Adjust the \p GlobalLSCost according to the target | ||||
7955 | /// paring capabilities and the layout of the slices. | ||||
7956 | /// \pre \p GlobalLSCost should account for at least as many loads as | ||||
7957 | /// there is in the slices in \p LoadedSlices. | ||||
7958 | static void adjustCostForPairing(SmallVectorImpl<LoadedSlice> &LoadedSlices, | ||||
7959 | LoadedSlice::Cost &GlobalLSCost) { | ||||
7960 | unsigned NumberOfSlices = LoadedSlices.size(); | ||||
7961 | // If there is less than 2 elements, no pairing is possible. | ||||
7962 | if (NumberOfSlices < 2) | ||||
7963 | return; | ||||
7964 | |||||
7965 | // Sort the slices so that elements that are likely to be next to each | ||||
7966 | // other in memory are next to each other in the list. | ||||
7967 | std::sort(LoadedSlices.begin(), LoadedSlices.end(), LoadedSliceSorter()); | ||||
7968 | const TargetLowering &TLI = LoadedSlices[0].DAG->getTargetLoweringInfo(); | ||||
7969 | // First (resp. Second) is the first (resp. Second) potentially candidate | ||||
7970 | // to be placed in a paired load. | ||||
7971 | const LoadedSlice *First = NULL; | ||||
7972 | const LoadedSlice *Second = NULL; | ||||
7973 | for (unsigned CurrSlice = 0; CurrSlice < NumberOfSlices; ++CurrSlice, | ||||
7974 | // Set the beginning of the pair. | ||||
7975 | First = Second) { | ||||
7976 | |||||
7977 | Second = &LoadedSlices[CurrSlice]; | ||||
7978 | |||||
7979 | // If First is NULL, it means we start a new pair. | ||||
7980 | // Get to the next slice. | ||||
7981 | if (!First) | ||||
7982 | continue; | ||||
7983 | |||||
7984 | EVT LoadedType = First->getLoadedType(); | ||||
7985 | |||||
7986 | // If the types of the slices are different, we cannot pair them. | ||||
7987 | if (LoadedType != Second->getLoadedType()) | ||||
7988 | continue; | ||||
7989 | |||||
7990 | // Check if the target supplies paired loads for this type. | ||||
7991 | unsigned RequiredAlignment = 0; | ||||
7992 | if (!TLI.hasPairedLoad(LoadedType, RequiredAlignment)) { | ||||
7993 | // move to the next pair, this type is hopeless. | ||||
7994 | Second = NULL; | ||||
7995 | continue; | ||||
7996 | } | ||||
7997 | // Check if we meet the alignment requirement. | ||||
7998 | if (RequiredAlignment > First->getAlignment()) | ||||
7999 | continue; | ||||
8000 | |||||
8001 | // Check that both loads are next to each other in memory. | ||||
8002 | if (!areSlicesNextToEachOther(*First, *Second)) | ||||
8003 | continue; | ||||
8004 | |||||
8005 | assert(GlobalLSCost.Loads > 0 && "We save more loads than we created!"); | ||||
8006 | --GlobalLSCost.Loads; | ||||
8007 | // Move to the next pair. | ||||
8008 | Second = NULL; | ||||
8009 | } | ||||
8010 | } | ||||
8011 | |||||
8012 | /// \brief Check the profitability of all involved LoadedSlice. | ||||
8013 | /// Currently, it is considered profitable if there is exactly two | ||||
8014 | /// involved slices (1) which are (2) next to each other in memory, and | ||||
8015 | /// whose cost (\see LoadedSlice::Cost) is smaller than the original load (3). | ||||
8016 | /// | ||||
8017 | /// Note: The order of the elements in \p LoadedSlices may be modified, but not | ||||
8018 | /// the elements themselves. | ||||
8019 | /// | ||||
8020 | /// FIXME: When the cost model will be mature enough, we can relax | ||||
8021 | /// constraints (1) and (2). | ||||
8022 | static bool isSlicingProfitable(SmallVectorImpl<LoadedSlice> &LoadedSlices, | ||||
8023 | const APInt &UsedBits, bool ForCodeSize) { | ||||
8024 | unsigned NumberOfSlices = LoadedSlices.size(); | ||||
8025 | if (StressLoadSlicing) | ||||
8026 | return NumberOfSlices > 1; | ||||
8027 | |||||
8028 | // Check (1). | ||||
8029 | if (NumberOfSlices != 2) | ||||
8030 | return false; | ||||
8031 | |||||
8032 | // Check (2). | ||||
8033 | if (!areUsedBitsDense(UsedBits)) | ||||
8034 | return false; | ||||
8035 | |||||
8036 | // Check (3). | ||||
8037 | LoadedSlice::Cost OrigCost(ForCodeSize), GlobalSlicingCost(ForCodeSize); | ||||
8038 | // The original code has one big load. | ||||
8039 | OrigCost.Loads = 1; | ||||
8040 | for (unsigned CurrSlice = 0; CurrSlice < NumberOfSlices; ++CurrSlice) { | ||||
8041 | const LoadedSlice &LS = LoadedSlices[CurrSlice]; | ||||
8042 | // Accumulate the cost of all the slices. | ||||
8043 | LoadedSlice::Cost SliceCost(LS, ForCodeSize); | ||||
8044 | GlobalSlicingCost += SliceCost; | ||||
8045 | |||||
8046 | // Account as cost in the original configuration the gain obtained | ||||
8047 | // with the current slices. | ||||
8048 | OrigCost.addSliceGain(LS); | ||||
8049 | } | ||||
8050 | |||||
8051 | // If the target supports paired load, adjust the cost accordingly. | ||||
8052 | adjustCostForPairing(LoadedSlices, GlobalSlicingCost); | ||||
8053 | return OrigCost > GlobalSlicingCost; | ||||
8054 | } | ||||
8055 | |||||
8056 | /// \brief If the given load, \p LI, is used only by trunc or trunc(lshr) | ||||
8057 | /// operations, split it in the various pieces being extracted. | ||||
8058 | /// | ||||
8059 | /// This sort of thing is introduced by SROA. | ||||
8060 | /// This slicing takes care not to insert overlapping loads. | ||||
8061 | /// \pre LI is a simple load (i.e., not an atomic or volatile load). | ||||
8062 | bool DAGCombiner::SliceUpLoad(SDNode *N) { | ||||
8063 | if (Level < AfterLegalizeDAG) | ||||
8064 | return false; | ||||
8065 | |||||
8066 | LoadSDNode *LD = cast<LoadSDNode>(N); | ||||
8067 | if (LD->isVolatile() || !ISD::isNormalLoad(LD) || | ||||
8068 | !LD->getValueType(0).isInteger()) | ||||
8069 | return false; | ||||
8070 | |||||
8071 | // Keep track of already used bits to detect overlapping values. | ||||
8072 | // In that case, we will just abort the transformation. | ||||
8073 | APInt UsedBits(LD->getValueSizeInBits(0), 0); | ||||
8074 | |||||
8075 | SmallVector<LoadedSlice, 4> LoadedSlices; | ||||
8076 | |||||
8077 | // Check if this load is used as several smaller chunks of bits. | ||||
8078 | // Basically, look for uses in trunc or trunc(lshr) and record a new chain | ||||
8079 | // of computation for each trunc. | ||||
8080 | for (SDNode::use_iterator UI = LD->use_begin(), UIEnd = LD->use_end(); | ||||
8081 | UI != UIEnd; ++UI) { | ||||
8082 | // Skip the uses of the chain. | ||||
8083 | if (UI.getUse().getResNo() != 0) | ||||
8084 | continue; | ||||
8085 | |||||
8086 | SDNode *User = *UI; | ||||
8087 | unsigned Shift = 0; | ||||
8088 | |||||
8089 | // Check if this is a trunc(lshr). | ||||
8090 | if (User->getOpcode() == ISD::SRL && User->hasOneUse() && | ||||
8091 | isa<ConstantSDNode>(User->getOperand(1))) { | ||||
8092 | Shift = cast<ConstantSDNode>(User->getOperand(1))->getZExtValue(); | ||||
8093 | User = *User->use_begin(); | ||||
8094 | } | ||||
8095 | |||||
8096 | // At this point, User is a Truncate, iff we encountered, trunc or | ||||
8097 | // trunc(lshr). | ||||
8098 | if (User->getOpcode() != ISD::TRUNCATE) | ||||
8099 | return false; | ||||
8100 | |||||
8101 | // The width of the type must be a power of 2 and greater than 8-bits. | ||||
8102 | // Otherwise the load cannot be represented in LLVM IR. | ||||
8103 | // Moreover, if we shifted with a non 8-bits multiple, the slice | ||||
8104 | // will be accross several bytes. We do not support that. | ||||
8105 | unsigned Width = User->getValueSizeInBits(0); | ||||
8106 | if (Width < 8 || !isPowerOf2_32(Width) || (Shift & 0x7)) | ||||
8107 | return 0; | ||||
8108 | |||||
8109 | // Build the slice for this chain of computations. | ||||
8110 | LoadedSlice LS(User, LD, Shift, &DAG); | ||||
8111 | APInt CurrentUsedBits = LS.getUsedBits(); | ||||
8112 | |||||
8113 | // Check if this slice overlaps with another. | ||||
8114 | if ((CurrentUsedBits & UsedBits) != 0) | ||||
8115 | return false; | ||||
8116 | // Update the bits used globally. | ||||
8117 | UsedBits |= CurrentUsedBits; | ||||
8118 | |||||
8119 | // Check if the new slice would be legal. | ||||
8120 | if (!LS.isLegal()) | ||||
8121 | return false; | ||||
8122 | |||||
8123 | // Record the slice. | ||||
8124 | LoadedSlices.push_back(LS); | ||||
8125 | } | ||||
8126 | |||||
8127 | // Abort slicing if it does not seem to be profitable. | ||||
8128 | if (!isSlicingProfitable(LoadedSlices, UsedBits, ForCodeSize)) | ||||
8129 | return false; | ||||
8130 | |||||
8131 | ++SlicedLoads; | ||||
8132 | |||||
8133 | // Rewrite each chain to use an independent load. | ||||
8134 | // By construction, each chain can be represented by a unique load. | ||||
8135 | |||||
8136 | // Prepare the argument for the new token factor for all the slices. | ||||
8137 | SmallVector<SDValue, 8> ArgChains; | ||||
8138 | for (SmallVectorImpl<LoadedSlice>::const_iterator | ||||
8139 | LSIt = LoadedSlices.begin(), | ||||
8140 | LSItEnd = LoadedSlices.end(); | ||||
8141 | LSIt != LSItEnd; ++LSIt) { | ||||
8142 | SDValue SliceInst = LSIt->loadSlice(); | ||||
8143 | CombineTo(LSIt->Inst, SliceInst, true); | ||||
8144 | if (SliceInst.getNode()->getOpcode() != ISD::LOAD) | ||||
8145 | SliceInst = SliceInst.getOperand(0); | ||||
8146 | assert(SliceInst->getOpcode() == ISD::LOAD && | ||||
8147 | "It takes more than a zext to get to the loaded slice!!"); | ||||
8148 | ArgChains.push_back(SliceInst.getValue(1)); | ||||
8149 | } | ||||
8150 | |||||
8151 | SDValue Chain = DAG.getNode(ISD::TokenFactor, SDLoc(LD), MVT::Other, | ||||
8152 | &ArgChains[0], ArgChains.size()); | ||||
8153 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain); | ||||
8154 | return true; | ||||
8155 | } | ||||
8156 | |||||
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8157 | /// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the |
8158 | /// load is having specific bytes cleared out. If so, return the byte size | ||||
8159 | /// being masked out and the shift amount. | ||||
8160 | static std::pair<unsigned, unsigned> | ||||
8161 | CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) { | ||||
8162 | std::pair<unsigned, unsigned> Result(0, 0); | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8163 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8164 | // Check for the structure we're looking for. |
8165 | if (V->getOpcode() != ISD::AND || | ||||
8166 | !isa<ConstantSDNode>(V->getOperand(1)) || | ||||
8167 | !ISD::isNormalLoad(V->getOperand(0).getNode())) | ||||
8168 | return Result; | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8169 | |
Chris Lattner | e698758 | 2010-04-15 06:10:49 +0000 | [diff] [blame] | 8170 | // Check the chain and pointer. |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8171 | LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0)); |
Chris Lattner | e698758 | 2010-04-15 06:10:49 +0000 | [diff] [blame] | 8172 | if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8173 | |
Chris Lattner | e698758 | 2010-04-15 06:10:49 +0000 | [diff] [blame] | 8174 | // The store should be chained directly to the load or be an operand of a |
8175 | // tokenfactor. | ||||
8176 | if (LD == Chain.getNode()) | ||||
8177 | ; // ok. | ||||
8178 | else if (Chain->getOpcode() != ISD::TokenFactor) | ||||
8179 | return Result; // Fail. | ||||
8180 | else { | ||||
8181 | bool isOk = false; | ||||
8182 | for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i) | ||||
8183 | if (Chain->getOperand(i).getNode() == LD) { | ||||
8184 | isOk = true; | ||||
8185 | break; | ||||
8186 | } | ||||
8187 | if (!isOk) return Result; | ||||
8188 | } | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8189 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8190 | // This only handles simple types. |
8191 | if (V.getValueType() != MVT::i16 && | ||||
8192 | V.getValueType() != MVT::i32 && | ||||
8193 | V.getValueType() != MVT::i64) | ||||
8194 | return Result; | ||||
8195 | |||||
8196 | // Check the constant mask. Invert it so that the bits being masked out are | ||||
8197 | // 0 and the bits being kept are 1. Use getSExtValue so that leading bits | ||||
8198 | // follow the sign bit for uniformity. | ||||
8199 | uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue(); | ||||
Michael J. Spencer | c6af243 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 8200 | unsigned NotMaskLZ = countLeadingZeros(NotMask); |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8201 | if (NotMaskLZ & 7) return Result; // Must be multiple of a byte. |
Michael J. Spencer | c6af243 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 8202 | unsigned NotMaskTZ = countTrailingZeros(NotMask); |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8203 | if (NotMaskTZ & 7) return Result; // Must be multiple of a byte. |
8204 | if (NotMaskLZ == 64) return Result; // All zero mask. | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8205 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8206 | // See if we have a continuous run of bits. If so, we have 0*1+0* |
8207 | if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64) | ||||
8208 | return Result; | ||||
8209 | |||||
8210 | // Adjust NotMaskLZ down to be from the actual size of the int instead of i64. | ||||
8211 | if (V.getValueType() != MVT::i64 && NotMaskLZ) | ||||
8212 | NotMaskLZ -= 64-V.getValueSizeInBits(); | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8213 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8214 | unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8; |
8215 | switch (MaskedBytes) { | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8216 | case 1: |
8217 | case 2: | ||||
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8218 | case 4: break; |
8219 | default: return Result; // All one mask, or 5-byte mask. | ||||
8220 | } | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8221 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8222 | // Verify that the first bit starts at a multiple of mask so that the access |
8223 | // is aligned the same as the access width. | ||||
8224 | if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result; | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8225 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8226 | Result.first = MaskedBytes; |
8227 | Result.second = NotMaskTZ/8; | ||||
8228 | return Result; | ||||
8229 | } | ||||
8230 | |||||
8231 | |||||
8232 | /// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that | ||||
8233 | /// provides a value as specified by MaskInfo. If so, replace the specified | ||||
8234 | /// store with a narrower store of truncated IVal. | ||||
8235 | static SDNode * | ||||
8236 | ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo, | ||||
8237 | SDValue IVal, StoreSDNode *St, | ||||
8238 | DAGCombiner *DC) { | ||||
8239 | unsigned NumBytes = MaskInfo.first; | ||||
8240 | unsigned ByteShift = MaskInfo.second; | ||||
8241 | SelectionDAG &DAG = DC->getDAG(); | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8242 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8243 | // Check to see if IVal is all zeros in the part being masked in by the 'or' |
8244 | // that uses this. If not, this is not a replacement. | ||||
8245 | APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(), | ||||
8246 | ByteShift*8, (ByteShift+NumBytes)*8); | ||||
8247 | if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0; | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8248 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8249 | // Check that it is legal on the target to do this. It is legal if the new |
8250 | // VT we're shrinking to (i8/i16/i32) is legal or we're still before type | ||||
8251 | // legalization. | ||||
8252 | MVT VT = MVT::getIntegerVT(NumBytes*8); | ||||
8253 | if (!DC->isTypeLegal(VT)) | ||||
8254 | return 0; | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8255 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8256 | // Okay, we can do this! Replace the 'St' store with a store of IVal that is |
8257 | // shifted by ByteShift and truncated down to NumBytes. | ||||
8258 | if (ByteShift) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8259 | IVal = DAG.getNode(ISD::SRL, SDLoc(IVal), IVal.getValueType(), IVal, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 8260 | DAG.getConstant(ByteShift*8, |
8261 | DC->getShiftAmountTy(IVal.getValueType()))); | ||||
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8262 | |
8263 | // Figure out the offset for the store and the alignment of the access. | ||||
8264 | unsigned StOffset; | ||||
8265 | unsigned NewAlign = St->getAlignment(); | ||||
8266 | |||||
8267 | if (DAG.getTargetLoweringInfo().isLittleEndian()) | ||||
8268 | StOffset = ByteShift; | ||||
8269 | else | ||||
8270 | StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes; | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8271 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8272 | SDValue Ptr = St->getBasePtr(); |
8273 | if (StOffset) { | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8274 | Ptr = DAG.getNode(ISD::ADD, SDLoc(IVal), Ptr.getValueType(), |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8275 | Ptr, DAG.getConstant(StOffset, Ptr.getValueType())); |
8276 | NewAlign = MinAlign(NewAlign, StOffset); | ||||
8277 | } | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8278 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8279 | // Truncate down to the new size. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8280 | IVal = DAG.getNode(ISD::TRUNCATE, SDLoc(IVal), VT, IVal); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8281 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8282 | ++OpsNarrowed; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8283 | return DAG.getStore(St->getChain(), SDLoc(St), IVal, Ptr, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 8284 | St->getPointerInfo().getWithOffset(StOffset), |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8285 | false, false, NewAlign).getNode(); |
8286 | } | ||||
8287 | |||||
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8288 | |
8289 | /// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is | ||||
8290 | /// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some | ||||
8291 | /// of the loaded bits, try narrowing the load and store if it would end up | ||||
8292 | /// being a win for performance or code size. | ||||
8293 | SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) { | ||||
8294 | StoreSDNode *ST = cast<StoreSDNode>(N); | ||||
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 8295 | if (ST->isVolatile()) |
8296 | return SDValue(); | ||||
8297 | |||||
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8298 | SDValue Chain = ST->getChain(); |
8299 | SDValue Value = ST->getValue(); | ||||
8300 | SDValue Ptr = ST->getBasePtr(); | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 8301 | EVT VT = Value.getValueType(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8302 | |
8303 | if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse()) | ||||
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 8304 | return SDValue(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8305 | |
8306 | unsigned Opc = Value.getOpcode(); | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8307 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8308 | // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst |
8309 | // is a byte mask indicating a consecutive number of bytes, check to see if | ||||
8310 | // Y is known to provide just those bytes. If so, we try to replace the | ||||
8311 | // load + replace + store sequence with a single (narrower) store, which makes | ||||
8312 | // the load dead. | ||||
8313 | if (Opc == ISD::OR) { | ||||
8314 | std::pair<unsigned, unsigned> MaskedLoad; | ||||
8315 | MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain); | ||||
8316 | if (MaskedLoad.first) | ||||
8317 | if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad, | ||||
8318 | Value.getOperand(1), ST,this)) | ||||
8319 | return SDValue(NewST, 0); | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8320 | |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 8321 | // Or is commutative, so try swapping X and Y. |
8322 | MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain); | ||||
8323 | if (MaskedLoad.first) | ||||
8324 | if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad, | ||||
8325 | Value.getOperand(0), ST,this)) | ||||
8326 | return SDValue(NewST, 0); | ||||
8327 | } | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8328 | |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8329 | if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) || |
8330 | Value.getOperand(1).getOpcode() != ISD::Constant) | ||||
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 8331 | return SDValue(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8332 | |
8333 | SDValue N0 = Value.getOperand(0); | ||||
Dan Gohman | 24bde5b | 2010-09-02 21:18:42 +0000 | [diff] [blame] | 8334 | if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() && |
8335 | Chain == SDValue(N0.getNode(), 1)) { | ||||
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8336 | LoadSDNode *LD = cast<LoadSDNode>(N0); |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 8337 | if (LD->getBasePtr() != Ptr || |
8338 | LD->getPointerInfo().getAddrSpace() != | ||||
8339 | ST->getPointerInfo().getAddrSpace()) | ||||
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 8340 | return SDValue(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8341 | |
8342 | // Find the type to narrow it the load / op / store to. | ||||
8343 | SDValue N1 = Value.getOperand(1); | ||||
8344 | unsigned BitWidth = N1.getValueSizeInBits(); | ||||
8345 | APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue(); | ||||
8346 | if (Opc == ISD::AND) | ||||
8347 | Imm ^= APInt::getAllOnesValue(BitWidth); | ||||
Evan Cheng | d3c76bb | 2009-05-28 23:52:18 +0000 | [diff] [blame] | 8348 | if (Imm == 0 || Imm.isAllOnesValue()) |
8349 | return SDValue(); | ||||
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8350 | unsigned ShAmt = Imm.countTrailingZeros(); |
8351 | unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1; | ||||
8352 | unsigned NewBW = NextPowerOf2(MSB - ShAmt); | ||||
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 8353 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8354 | while (NewBW < BitWidth && |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 8355 | !(TLI.isOperationLegalOrCustom(Opc, NewVT) && |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8356 | TLI.isNarrowingProfitable(VT, NewVT))) { |
8357 | NewBW = NextPowerOf2(NewBW); | ||||
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 8358 | NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8359 | } |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 8360 | if (NewBW >= BitWidth) |
8361 | return SDValue(); | ||||
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8362 | |
8363 | // If the lsb changed does not start at the type bitwidth boundary, | ||||
8364 | // start at the previous one. | ||||
8365 | if (ShAmt % NewBW) | ||||
8366 | ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW; | ||||
Manman Ren | 981b963 | 2012-12-12 01:13:50 +0000 | [diff] [blame] | 8367 | APInt Mask = APInt::getBitsSet(BitWidth, ShAmt, |
8368 | std::min(BitWidth, ShAmt + NewBW)); | ||||
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8369 | if ((Imm & Mask) == Imm) { |
8370 | APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW); | ||||
8371 | if (Opc == ISD::AND) | ||||
8372 | NewImm ^= APInt::getAllOnesValue(NewBW); | ||||
8373 | uint64_t PtrOff = ShAmt / 8; | ||||
8374 | // For big endian targets, we need to adjust the offset to the pointer to | ||||
8375 | // load the correct bytes. | ||||
8376 | if (TLI.isBigEndian()) | ||||
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 8377 | PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff; |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8378 | |
8379 | unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff); | ||||
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 8380 | Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext()); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 8381 | if (NewAlign < TLI.getDataLayout()->getABITypeAlignment(NewVTTy)) |
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 8382 | return SDValue(); |
8383 | |||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8384 | SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LD), |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8385 | Ptr.getValueType(), Ptr, |
8386 | DAG.getConstant(PtrOff, Ptr.getValueType())); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8387 | SDValue NewLD = DAG.getLoad(NewVT, SDLoc(N0), |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8388 | LD->getChain(), NewPtr, |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 8389 | LD->getPointerInfo().getWithOffset(PtrOff), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 8390 | LD->isVolatile(), LD->isNonTemporal(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 8391 | LD->isInvariant(), NewAlign); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8392 | SDValue NewVal = DAG.getNode(Opc, SDLoc(Value), NewVT, NewLD, |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8393 | DAG.getConstant(NewImm, NewVT)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8394 | SDValue NewST = DAG.getStore(Chain, SDLoc(N), |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8395 | NewVal, NewPtr, |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 8396 | ST->getPointerInfo().getWithOffset(PtrOff), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 8397 | false, false, NewAlign); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8398 | |
8399 | AddToWorkList(NewPtr.getNode()); | ||||
8400 | AddToWorkList(NewLD.getNode()); | ||||
8401 | AddToWorkList(NewVal.getNode()); | ||||
8402 | WorkListRemover DeadNodes(*this); | ||||
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 8403 | DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1)); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8404 | ++OpsNarrowed; |
8405 | return NewST; | ||||
8406 | } | ||||
8407 | } | ||||
8408 | |||||
Evan Cheng | cdcecc0 | 2009-05-28 18:41:02 +0000 | [diff] [blame] | 8409 | return SDValue(); |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 8410 | } |
8411 | |||||
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 8412 | /// TransformFPLoadStorePair - For a given floating point load / store pair, |
8413 | /// if the load value isn't used by any other operations, then consider | ||||
8414 | /// transforming the pair to integer load / store operations if the target | ||||
8415 | /// deems the transformation profitable. | ||||
8416 | SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) { | ||||
8417 | StoreSDNode *ST = cast<StoreSDNode>(N); | ||||
8418 | SDValue Chain = ST->getChain(); | ||||
8419 | SDValue Value = ST->getValue(); | ||||
8420 | if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) && | ||||
8421 | Value.hasOneUse() && | ||||
8422 | Chain == SDValue(Value.getNode(), 1)) { | ||||
8423 | LoadSDNode *LD = cast<LoadSDNode>(Value); | ||||
8424 | EVT VT = LD->getMemoryVT(); | ||||
8425 | if (!VT.isFloatingPoint() || | ||||
8426 | VT != ST->getMemoryVT() || | ||||
8427 | LD->isNonTemporal() || | ||||
8428 | ST->isNonTemporal() || | ||||
8429 | LD->getPointerInfo().getAddrSpace() != 0 || | ||||
8430 | ST->getPointerInfo().getAddrSpace() != 0) | ||||
8431 | return SDValue(); | ||||
8432 | |||||
8433 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits()); | ||||
8434 | if (!TLI.isOperationLegal(ISD::LOAD, IntVT) || | ||||
8435 | !TLI.isOperationLegal(ISD::STORE, IntVT) || | ||||
8436 | !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) || | ||||
8437 | !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT)) | ||||
8438 | return SDValue(); | ||||
8439 | |||||
8440 | unsigned LDAlign = LD->getAlignment(); | ||||
8441 | unsigned STAlign = ST->getAlignment(); | ||||
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 8442 | Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext()); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 8443 | unsigned ABIAlign = TLI.getDataLayout()->getABITypeAlignment(IntVTTy); |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 8444 | if (LDAlign < ABIAlign || STAlign < ABIAlign) |
8445 | return SDValue(); | ||||
8446 | |||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8447 | SDValue NewLD = DAG.getLoad(IntVT, SDLoc(Value), |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 8448 | LD->getChain(), LD->getBasePtr(), |
8449 | LD->getPointerInfo(), | ||||
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 8450 | false, false, false, LDAlign); |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 8451 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8452 | SDValue NewST = DAG.getStore(NewLD.getValue(1), SDLoc(N), |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 8453 | NewLD, ST->getBasePtr(), |
8454 | ST->getPointerInfo(), | ||||
8455 | false, false, STAlign); | ||||
8456 | |||||
8457 | AddToWorkList(NewLD.getNode()); | ||||
8458 | AddToWorkList(NewST.getNode()); | ||||
8459 | WorkListRemover DeadNodes(*this); | ||||
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 8460 | DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1)); |
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 8461 | ++LdStFP2Int; |
8462 | return NewST; | ||||
8463 | } | ||||
8464 | |||||
8465 | return SDValue(); | ||||
8466 | } | ||||
8467 | |||||
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8468 | /// Helper struct to parse and store a memory address as base + index + offset. |
8469 | /// We ignore sign extensions when it is safe to do so. | ||||
8470 | /// The following two expressions are not equivalent. To differentiate we need | ||||
8471 | /// to store whether there was a sign extension involved in the index | ||||
8472 | /// computation. | ||||
8473 | /// (load (i64 add (i64 copyfromreg %c) | ||||
8474 | /// (i64 signextend (add (i8 load %index) | ||||
8475 | /// (i8 1)))) | ||||
8476 | /// vs | ||||
8477 | /// | ||||
8478 | /// (load (i64 add (i64 copyfromreg %c) | ||||
8479 | /// (i64 signextend (i32 add (i32 signextend (i8 load %index)) | ||||
8480 | /// (i32 1))))) | ||||
8481 | struct BaseIndexOffset { | ||||
8482 | SDValue Base; | ||||
8483 | SDValue Index; | ||||
8484 | int64_t Offset; | ||||
8485 | bool IsIndexSignExt; | ||||
8486 | |||||
8487 | BaseIndexOffset() : Offset(0), IsIndexSignExt(false) {} | ||||
8488 | |||||
8489 | BaseIndexOffset(SDValue Base, SDValue Index, int64_t Offset, | ||||
8490 | bool IsIndexSignExt) : | ||||
8491 | Base(Base), Index(Index), Offset(Offset), IsIndexSignExt(IsIndexSignExt) {} | ||||
8492 | |||||
8493 | bool equalBaseIndex(const BaseIndexOffset &Other) { | ||||
8494 | return Other.Base == Base && Other.Index == Index && | ||||
8495 | Other.IsIndexSignExt == IsIndexSignExt; | ||||
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8496 | } |
8497 | |||||
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8498 | /// Parses tree in Ptr for base, index, offset addresses. |
8499 | static BaseIndexOffset match(SDValue Ptr) { | ||||
8500 | bool IsIndexSignExt = false; | ||||
8501 | |||||
Juergen Ributzka | 915e936 | 2013-08-21 21:53:38 +0000 | [diff] [blame] | 8502 | // We only can pattern match BASE + INDEX + OFFSET. If Ptr is not an ADD |
8503 | // instruction, then it could be just the BASE or everything else we don't | ||||
8504 | // know how to handle. Just use Ptr as BASE and give up. | ||||
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8505 | if (Ptr->getOpcode() != ISD::ADD) |
8506 | return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt); | ||||
8507 | |||||
Juergen Ributzka | 915e936 | 2013-08-21 21:53:38 +0000 | [diff] [blame] | 8508 | // We know that we have at least an ADD instruction. Try to pattern match |
8509 | // the simple case of BASE + OFFSET. | ||||
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8510 | if (isa<ConstantSDNode>(Ptr->getOperand(1))) { |
8511 | int64_t Offset = cast<ConstantSDNode>(Ptr->getOperand(1))->getSExtValue(); | ||||
8512 | return BaseIndexOffset(Ptr->getOperand(0), SDValue(), Offset, | ||||
8513 | IsIndexSignExt); | ||||
8514 | } | ||||
8515 | |||||
Juergen Ributzka | 915e936 | 2013-08-21 21:53:38 +0000 | [diff] [blame] | 8516 | // Inside a loop the current BASE pointer is calculated using an ADD and a |
Juergen Ributzka | 2b884bc | 2013-08-28 22:33:58 +0000 | [diff] [blame] | 8517 | // MUL instruction. In this case Ptr is the actual BASE pointer. |
Juergen Ributzka | 915e936 | 2013-08-21 21:53:38 +0000 | [diff] [blame] | 8518 | // (i64 add (i64 %array_ptr) |
8519 | // (i64 mul (i64 %induction_var) | ||||
8520 | // (i64 %element_size))) | ||||
Juergen Ributzka | 2b884bc | 2013-08-28 22:33:58 +0000 | [diff] [blame] | 8521 | if (Ptr->getOperand(1)->getOpcode() == ISD::MUL) |
Juergen Ributzka | 915e936 | 2013-08-21 21:53:38 +0000 | [diff] [blame] | 8522 | return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt); |
Juergen Ributzka | 915e936 | 2013-08-21 21:53:38 +0000 | [diff] [blame] | 8523 | |
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8524 | // Look at Base + Index + Offset cases. |
8525 | SDValue Base = Ptr->getOperand(0); | ||||
8526 | SDValue IndexOffset = Ptr->getOperand(1); | ||||
8527 | |||||
8528 | // Skip signextends. | ||||
8529 | if (IndexOffset->getOpcode() == ISD::SIGN_EXTEND) { | ||||
8530 | IndexOffset = IndexOffset->getOperand(0); | ||||
8531 | IsIndexSignExt = true; | ||||
8532 | } | ||||
8533 | |||||
8534 | // Either the case of Base + Index (no offset) or something else. | ||||
8535 | if (IndexOffset->getOpcode() != ISD::ADD) | ||||
8536 | return BaseIndexOffset(Base, IndexOffset, 0, IsIndexSignExt); | ||||
8537 | |||||
8538 | // Now we have the case of Base + Index + offset. | ||||
8539 | SDValue Index = IndexOffset->getOperand(0); | ||||
8540 | SDValue Offset = IndexOffset->getOperand(1); | ||||
8541 | |||||
8542 | if (!isa<ConstantSDNode>(Offset)) | ||||
8543 | return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt); | ||||
8544 | |||||
8545 | // Ignore signextends. | ||||
8546 | if (Index->getOpcode() == ISD::SIGN_EXTEND) { | ||||
8547 | Index = Index->getOperand(0); | ||||
8548 | IsIndexSignExt = true; | ||||
8549 | } else IsIndexSignExt = false; | ||||
8550 | |||||
8551 | int64_t Off = cast<ConstantSDNode>(Offset)->getSExtValue(); | ||||
8552 | return BaseIndexOffset(Base, Index, Off, IsIndexSignExt); | ||||
8553 | } | ||||
8554 | }; | ||||
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8555 | |
8556 | /// Holds a pointer to an LSBaseSDNode as well as information on where it | ||||
8557 | /// is located in a sequence of memory operations connected by a chain. | ||||
8558 | struct MemOpLink { | ||||
8559 | MemOpLink (LSBaseSDNode *N, int64_t Offset, unsigned Seq): | ||||
8560 | MemNode(N), OffsetFromBase(Offset), SequenceNum(Seq) { } | ||||
8561 | // Ptr to the mem node. | ||||
8562 | LSBaseSDNode *MemNode; | ||||
8563 | // Offset from the base ptr. | ||||
8564 | int64_t OffsetFromBase; | ||||
8565 | // What is the sequence number of this mem node. | ||||
8566 | // Lowest mem operand in the DAG starts at zero. | ||||
8567 | unsigned SequenceNum; | ||||
8568 | }; | ||||
8569 | |||||
8570 | /// Sorts store nodes in a link according to their offset from a shared | ||||
8571 | // base ptr. | ||||
8572 | struct ConsecutiveMemoryChainSorter { | ||||
8573 | bool operator()(MemOpLink LHS, MemOpLink RHS) { | ||||
8574 | return LHS.OffsetFromBase < RHS.OffsetFromBase; | ||||
8575 | } | ||||
8576 | }; | ||||
8577 | |||||
8578 | bool DAGCombiner::MergeConsecutiveStores(StoreSDNode* St) { | ||||
8579 | EVT MemVT = St->getMemoryVT(); | ||||
8580 | int64_t ElementSizeBytes = MemVT.getSizeInBits()/8; | ||||
Nadav Rotem | 6cc4b8d | 2013-02-14 18:28:52 +0000 | [diff] [blame] | 8581 | bool NoVectors = DAG.getMachineFunction().getFunction()->getAttributes(). |
8582 | hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat); | ||||
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8583 | |
8584 | // Don't merge vectors into wider inputs. | ||||
8585 | if (MemVT.isVector() || !MemVT.isSimple()) | ||||
8586 | return false; | ||||
8587 | |||||
8588 | // Perform an early exit check. Do not bother looking at stored values that | ||||
8589 | // are not constants or loads. | ||||
8590 | SDValue StoredVal = St->getValue(); | ||||
8591 | bool IsLoadSrc = isa<LoadSDNode>(StoredVal); | ||||
8592 | if (!isa<ConstantSDNode>(StoredVal) && !isa<ConstantFPSDNode>(StoredVal) && | ||||
8593 | !IsLoadSrc) | ||||
8594 | return false; | ||||
8595 | |||||
8596 | // Only look at ends of store sequences. | ||||
8597 | SDValue Chain = SDValue(St, 1); | ||||
8598 | if (Chain->hasOneUse() && Chain->use_begin()->getOpcode() == ISD::STORE) | ||||
8599 | return false; | ||||
8600 | |||||
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8601 | // This holds the base pointer, index, and the offset in bytes from the base |
8602 | // pointer. | ||||
8603 | BaseIndexOffset BasePtr = BaseIndexOffset::match(St->getBasePtr()); | ||||
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8604 | |
8605 | // We must have a base and an offset. | ||||
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8606 | if (!BasePtr.Base.getNode()) |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8607 | return false; |
8608 | |||||
8609 | // Do not handle stores to undef base pointers. | ||||
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8610 | if (BasePtr.Base.getOpcode() == ISD::UNDEF) |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8611 | return false; |
8612 | |||||
Nadav Rotem | 90e11dc | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 8613 | // Save the LoadSDNodes that we find in the chain. |
8614 | // We need to make sure that these nodes do not interfere with | ||||
8615 | // any of the store nodes. | ||||
8616 | SmallVector<LSBaseSDNode*, 8> AliasLoadNodes; | ||||
8617 | |||||
8618 | // Save the StoreSDNodes that we find in the chain. | ||||
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8619 | SmallVector<MemOpLink, 8> StoreNodes; |
Nadav Rotem | 90e11dc | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 8620 | |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8621 | // Walk up the chain and look for nodes with offsets from the same |
8622 | // base pointer. Stop when reaching an instruction with a different kind | ||||
8623 | // or instruction which has a different base pointer. | ||||
8624 | unsigned Seq = 0; | ||||
8625 | StoreSDNode *Index = St; | ||||
8626 | while (Index) { | ||||
8627 | // If the chain has more than one use, then we can't reorder the mem ops. | ||||
8628 | if (Index != St && !SDValue(Index, 1)->hasOneUse()) | ||||
8629 | break; | ||||
8630 | |||||
8631 | // Find the base pointer and offset for this memory node. | ||||
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8632 | BaseIndexOffset Ptr = BaseIndexOffset::match(Index->getBasePtr()); |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8633 | |
8634 | // Check that the base pointer is the same as the original one. | ||||
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8635 | if (!Ptr.equalBaseIndex(BasePtr)) |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8636 | break; |
8637 | |||||
8638 | // Check that the alignment is the same. | ||||
8639 | if (Index->getAlignment() != St->getAlignment()) | ||||
8640 | break; | ||||
8641 | |||||
8642 | // The memory operands must not be volatile. | ||||
8643 | if (Index->isVolatile() || Index->isIndexed()) | ||||
8644 | break; | ||||
8645 | |||||
8646 | // No truncation. | ||||
8647 | if (StoreSDNode *St = dyn_cast<StoreSDNode>(Index)) | ||||
8648 | if (St->isTruncatingStore()) | ||||
8649 | break; | ||||
8650 | |||||
8651 | // The stored memory type must be the same. | ||||
8652 | if (Index->getMemoryVT() != MemVT) | ||||
8653 | break; | ||||
8654 | |||||
8655 | // We do not allow unaligned stores because we want to prevent overriding | ||||
8656 | // stores. | ||||
8657 | if (Index->getAlignment()*8 != MemVT.getSizeInBits()) | ||||
8658 | break; | ||||
8659 | |||||
8660 | // We found a potential memory operand to merge. | ||||
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8661 | StoreNodes.push_back(MemOpLink(Index, Ptr.Offset, Seq++)); |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8662 | |
Nadav Rotem | 90e11dc | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 8663 | // Find the next memory operand in the chain. If the next operand in the |
8664 | // chain is a store then move up and continue the scan with the next | ||||
8665 | // memory operand. If the next operand is a load save it and use alias | ||||
8666 | // information to check if it interferes with anything. | ||||
8667 | SDNode *NextInChain = Index->getChain().getNode(); | ||||
8668 | while (1) { | ||||
Nadav Rotem | dde785c | 2012-12-06 17:34:13 +0000 | [diff] [blame] | 8669 | if (StoreSDNode *STn = dyn_cast<StoreSDNode>(NextInChain)) { |
Nadav Rotem | 90e11dc | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 8670 | // We found a store node. Use it for the next iteration. |
Nadav Rotem | dde785c | 2012-12-06 17:34:13 +0000 | [diff] [blame] | 8671 | Index = STn; |
Nadav Rotem | 90e11dc | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 8672 | break; |
8673 | } else if (LoadSDNode *Ldn = dyn_cast<LoadSDNode>(NextInChain)) { | ||||
8674 | // Save the load node for later. Continue the scan. | ||||
8675 | AliasLoadNodes.push_back(Ldn); | ||||
8676 | NextInChain = Ldn->getChain().getNode(); | ||||
8677 | continue; | ||||
8678 | } else { | ||||
8679 | Index = NULL; | ||||
8680 | break; | ||||
8681 | } | ||||
8682 | } | ||||
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8683 | } |
8684 | |||||
8685 | // Check if there is anything to merge. | ||||
8686 | if (StoreNodes.size() < 2) | ||||
8687 | return false; | ||||
8688 | |||||
8689 | // Sort the memory operands according to their distance from the base pointer. | ||||
8690 | std::sort(StoreNodes.begin(), StoreNodes.end(), | ||||
8691 | ConsecutiveMemoryChainSorter()); | ||||
8692 | |||||
8693 | // Scan the memory operations on the chain and find the first non-consecutive | ||||
8694 | // store memory address. | ||||
8695 | unsigned LastConsecutiveStore = 0; | ||||
8696 | int64_t StartAddress = StoreNodes[0].OffsetFromBase; | ||||
Nadav Rotem | dde785c | 2012-12-06 17:34:13 +0000 | [diff] [blame] | 8697 | for (unsigned i = 0, e = StoreNodes.size(); i < e; ++i) { |
8698 | |||||
8699 | // Check that the addresses are consecutive starting from the second | ||||
8700 | // element in the list of stores. | ||||
8701 | if (i > 0) { | ||||
8702 | int64_t CurrAddress = StoreNodes[i].OffsetFromBase; | ||||
8703 | if (CurrAddress - StartAddress != (ElementSizeBytes * i)) | ||||
8704 | break; | ||||
8705 | } | ||||
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8706 | |
Nadav Rotem | 90e11dc | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 8707 | bool Alias = false; |
8708 | // Check if this store interferes with any of the loads that we found. | ||||
8709 | for (unsigned ld = 0, lde = AliasLoadNodes.size(); ld < lde; ++ld) | ||||
8710 | if (isAlias(AliasLoadNodes[ld], StoreNodes[i].MemNode)) { | ||||
8711 | Alias = true; | ||||
8712 | break; | ||||
8713 | } | ||||
Nadav Rotem | 90e11dc | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 8714 | // We found a load that alias with this store. Stop the sequence. |
8715 | if (Alias) | ||||
8716 | break; | ||||
8717 | |||||
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8718 | // Mark this node as useful. |
8719 | LastConsecutiveStore = i; | ||||
8720 | } | ||||
8721 | |||||
8722 | // The node with the lowest store address. | ||||
8723 | LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode; | ||||
8724 | |||||
8725 | // Store the constants into memory as one consecutive store. | ||||
8726 | if (!IsLoadSrc) { | ||||
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8727 | unsigned LastLegalType = 0; |
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8728 | unsigned LastLegalVectorType = 0; |
8729 | bool NonZero = false; | ||||
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8730 | for (unsigned i=0; i<LastConsecutiveStore+1; ++i) { |
8731 | StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode); | ||||
8732 | SDValue StoredVal = St->getValue(); | ||||
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8733 | |
8734 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(StoredVal)) { | ||||
Benjamin Kramer | ebd7eab | 2012-10-05 18:19:44 +0000 | [diff] [blame] | 8735 | NonZero |= !C->isNullValue(); |
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8736 | } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(StoredVal)) { |
Benjamin Kramer | ebd7eab | 2012-10-05 18:19:44 +0000 | [diff] [blame] | 8737 | NonZero |= !C->getConstantFPValue()->isNullValue(); |
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8738 | } else { |
8739 | // Non constant. | ||||
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8740 | break; |
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8741 | } |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8742 | |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8743 | // Find a legal type for the constant store. |
8744 | unsigned StoreBW = (i+1) * ElementSizeBytes * 8; | ||||
8745 | EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW); | ||||
8746 | if (TLI.isTypeLegal(StoreTy)) | ||||
8747 | LastLegalType = i+1; | ||||
Arnold Schwaighofer | e737018 | 2013-04-02 15:58:51 +0000 | [diff] [blame] | 8748 | // Or check whether a truncstore is legal. |
8749 | else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) == | ||||
8750 | TargetLowering::TypePromoteInteger) { | ||||
8751 | EVT LegalizedStoredValueTy = | ||||
8752 | TLI.getTypeToTransformTo(*DAG.getContext(), StoredVal.getValueType()); | ||||
8753 | if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy)) | ||||
8754 | LastLegalType = i+1; | ||||
8755 | } | ||||
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8756 | |
8757 | // Find a legal type for the vector store. | ||||
8758 | EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1); | ||||
8759 | if (TLI.isTypeLegal(Ty)) | ||||
8760 | LastLegalVectorType = i + 1; | ||||
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8761 | } |
8762 | |||||
Bob Wilson | 99d8e76 | 2012-12-20 01:36:20 +0000 | [diff] [blame] | 8763 | // We only use vectors if the constant is known to be zero and the |
8764 | // function is not marked with the noimplicitfloat attribute. | ||||
Nadav Rotem | 6cc4b8d | 2013-02-14 18:28:52 +0000 | [diff] [blame] | 8765 | if (NonZero || NoVectors) |
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8766 | LastLegalVectorType = 0; |
8767 | |||||
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8768 | // Check if we found a legal integer type to store. |
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8769 | if (LastLegalType == 0 && LastLegalVectorType == 0) |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8770 | return false; |
8771 | |||||
Nadav Rotem | 6cc4b8d | 2013-02-14 18:28:52 +0000 | [diff] [blame] | 8772 | bool UseVector = (LastLegalVectorType > LastLegalType) && !NoVectors; |
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8773 | unsigned NumElem = UseVector ? LastLegalVectorType : LastLegalType; |
8774 | |||||
8775 | // Make sure we have something to merge. | ||||
8776 | if (NumElem < 2) | ||||
8777 | return false; | ||||
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8778 | |
8779 | unsigned EarliestNodeUsed = 0; | ||||
8780 | for (unsigned i=0; i < NumElem; ++i) { | ||||
8781 | // Find a chain for the new wide-store operand. Notice that some | ||||
8782 | // of the store nodes that we found may not be selected for inclusion | ||||
8783 | // in the wide store. The chain we use needs to be the chain of the | ||||
8784 | // earliest store node which is *used* and replaced by the wide store. | ||||
8785 | if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum) | ||||
8786 | EarliestNodeUsed = i; | ||||
8787 | } | ||||
8788 | |||||
8789 | // The earliest Node in the DAG. | ||||
8790 | LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode; | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8791 | SDLoc DL(StoreNodes[0].MemNode); |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8792 | |
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8793 | SDValue StoredVal; |
8794 | if (UseVector) { | ||||
8795 | // Find a legal type for the vector store. | ||||
8796 | EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem); | ||||
8797 | assert(TLI.isTypeLegal(Ty) && "Illegal vector store"); | ||||
8798 | StoredVal = DAG.getConstant(0, Ty); | ||||
8799 | } else { | ||||
8800 | unsigned StoreBW = NumElem * ElementSizeBytes * 8; | ||||
8801 | APInt StoreInt(StoreBW, 0); | ||||
8802 | |||||
8803 | // Construct a single integer constant which is made of the smaller | ||||
8804 | // constant inputs. | ||||
8805 | bool IsLE = TLI.isLittleEndian(); | ||||
8806 | for (unsigned i = 0; i < NumElem ; ++i) { | ||||
8807 | unsigned Idx = IsLE ?(NumElem - 1 - i) : i; | ||||
8808 | StoreSDNode *St = cast<StoreSDNode>(StoreNodes[Idx].MemNode); | ||||
8809 | SDValue Val = St->getValue(); | ||||
8810 | StoreInt<<=ElementSizeBytes*8; | ||||
8811 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) { | ||||
8812 | StoreInt|=C->getAPIntValue().zext(StoreBW); | ||||
8813 | } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Val)) { | ||||
8814 | StoreInt|= C->getValueAPF().bitcastToAPInt().zext(StoreBW); | ||||
8815 | } else { | ||||
8816 | assert(false && "Invalid constant element type"); | ||||
8817 | } | ||||
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8818 | } |
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8819 | |
8820 | // Create the new Load and Store operations. | ||||
8821 | EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW); | ||||
8822 | StoredVal = DAG.getConstant(StoreInt, StoreTy); | ||||
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8823 | } |
8824 | |||||
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 8825 | SDValue NewStore = DAG.getStore(EarliestOp->getChain(), DL, StoredVal, |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8826 | FirstInChain->getBasePtr(), |
8827 | FirstInChain->getPointerInfo(), | ||||
8828 | false, false, | ||||
8829 | FirstInChain->getAlignment()); | ||||
8830 | |||||
8831 | // Replace the first store with the new store | ||||
8832 | CombineTo(EarliestOp, NewStore); | ||||
8833 | // Erase all other stores. | ||||
8834 | for (unsigned i = 0; i < NumElem ; ++i) { | ||||
8835 | if (StoreNodes[i].MemNode == EarliestOp) | ||||
8836 | continue; | ||||
8837 | StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode); | ||||
Rafael Espindola | 8e2b8ae | 2012-11-14 05:08:56 +0000 | [diff] [blame] | 8838 | // ReplaceAllUsesWith will replace all uses that existed when it was |
8839 | // called, but graph optimizations may cause new ones to appear. For | ||||
8840 | // example, the case in pr14333 looks like | ||||
8841 | // | ||||
8842 | // St's chain -> St -> another store -> X | ||||
8843 | // | ||||
8844 | // And the only difference from St to the other store is the chain. | ||||
8845 | // When we change it's chain to be St's chain they become identical, | ||||
8846 | // get CSEed and the net result is that X is now a use of St. | ||||
8847 | // Since we know that St is redundant, just iterate. | ||||
8848 | while (!St->use_empty()) | ||||
8849 | DAG.ReplaceAllUsesWith(SDValue(St, 0), St->getChain()); | ||||
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8850 | removeFromWorkList(St); |
8851 | DAG.DeleteNode(St); | ||||
8852 | } | ||||
8853 | |||||
8854 | return true; | ||||
8855 | } | ||||
8856 | |||||
8857 | // Below we handle the case of multiple consecutive stores that | ||||
8858 | // come from multiple consecutive loads. We merge them into a single | ||||
8859 | // wide load and a single wide store. | ||||
8860 | |||||
8861 | // Look for load nodes which are used by the stored values. | ||||
8862 | SmallVector<MemOpLink, 8> LoadNodes; | ||||
8863 | |||||
8864 | // Find acceptable loads. Loads need to have the same chain (token factor), | ||||
8865 | // must not be zext, volatile, indexed, and they must be consecutive. | ||||
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8866 | BaseIndexOffset LdBasePtr; |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8867 | for (unsigned i=0; i<LastConsecutiveStore+1; ++i) { |
8868 | StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode); | ||||
8869 | LoadSDNode *Ld = dyn_cast<LoadSDNode>(St->getValue()); | ||||
8870 | if (!Ld) break; | ||||
8871 | |||||
8872 | // Loads must only have one use. | ||||
8873 | if (!Ld->hasNUsesOfValue(1, 0)) | ||||
8874 | break; | ||||
8875 | |||||
8876 | // Check that the alignment is the same as the stores. | ||||
8877 | if (Ld->getAlignment() != St->getAlignment()) | ||||
8878 | break; | ||||
8879 | |||||
8880 | // The memory operands must not be volatile. | ||||
8881 | if (Ld->isVolatile() || Ld->isIndexed()) | ||||
8882 | break; | ||||
8883 | |||||
8884 | // We do not accept ext loads. | ||||
8885 | if (Ld->getExtensionType() != ISD::NON_EXTLOAD) | ||||
8886 | break; | ||||
8887 | |||||
8888 | // The stored memory type must be the same. | ||||
8889 | if (Ld->getMemoryVT() != MemVT) | ||||
8890 | break; | ||||
8891 | |||||
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8892 | BaseIndexOffset LdPtr = BaseIndexOffset::match(Ld->getBasePtr()); |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8893 | // If this is not the first ptr that we check. |
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8894 | if (LdBasePtr.Base.getNode()) { |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8895 | // The base ptr must be the same. |
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8896 | if (!LdPtr.equalBaseIndex(LdBasePtr)) |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8897 | break; |
8898 | } else { | ||||
8899 | // Check that all other base pointers are the same as this one. | ||||
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8900 | LdBasePtr = LdPtr; |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8901 | } |
8902 | |||||
8903 | // We found a potential memory operand to merge. | ||||
Arnold Schwaighofer | f28a29b | 2013-04-01 18:12:58 +0000 | [diff] [blame] | 8904 | LoadNodes.push_back(MemOpLink(Ld, LdPtr.Offset, 0)); |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8905 | } |
8906 | |||||
8907 | if (LoadNodes.size() < 2) | ||||
8908 | return false; | ||||
8909 | |||||
8910 | // Scan the memory operations on the chain and find the first non-consecutive | ||||
8911 | // load memory address. These variables hold the index in the store node | ||||
8912 | // array. | ||||
8913 | unsigned LastConsecutiveLoad = 0; | ||||
8914 | // This variable refers to the size and not index in the array. | ||||
8915 | unsigned LastLegalVectorType = 0; | ||||
8916 | unsigned LastLegalIntegerType = 0; | ||||
8917 | StartAddress = LoadNodes[0].OffsetFromBase; | ||||
Nadav Rotem | 2e7d381 | 2012-10-03 19:30:31 +0000 | [diff] [blame] | 8918 | SDValue FirstChain = LoadNodes[0].MemNode->getChain(); |
8919 | for (unsigned i = 1; i < LoadNodes.size(); ++i) { | ||||
8920 | // All loads much share the same chain. | ||||
8921 | if (LoadNodes[i].MemNode->getChain() != FirstChain) | ||||
8922 | break; | ||||
Nadav Rotem | 6cc4b8d | 2013-02-14 18:28:52 +0000 | [diff] [blame] | 8923 | |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8924 | int64_t CurrAddress = LoadNodes[i].OffsetFromBase; |
8925 | if (CurrAddress - StartAddress != (ElementSizeBytes * i)) | ||||
8926 | break; | ||||
8927 | LastConsecutiveLoad = i; | ||||
8928 | |||||
8929 | // Find a legal type for the vector store. | ||||
8930 | EVT StoreTy = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1); | ||||
8931 | if (TLI.isTypeLegal(StoreTy)) | ||||
8932 | LastLegalVectorType = i + 1; | ||||
8933 | |||||
8934 | // Find a legal type for the integer store. | ||||
8935 | unsigned StoreBW = (i+1) * ElementSizeBytes * 8; | ||||
8936 | StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW); | ||||
8937 | if (TLI.isTypeLegal(StoreTy)) | ||||
8938 | LastLegalIntegerType = i + 1; | ||||
Arnold Schwaighofer | e737018 | 2013-04-02 15:58:51 +0000 | [diff] [blame] | 8939 | // Or check whether a truncstore and extload is legal. |
8940 | else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) == | ||||
8941 | TargetLowering::TypePromoteInteger) { | ||||
8942 | EVT LegalizedStoredValueTy = | ||||
8943 | TLI.getTypeToTransformTo(*DAG.getContext(), StoreTy); | ||||
8944 | if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy) && | ||||
8945 | TLI.isLoadExtLegal(ISD::ZEXTLOAD, StoreTy) && | ||||
8946 | TLI.isLoadExtLegal(ISD::SEXTLOAD, StoreTy) && | ||||
8947 | TLI.isLoadExtLegal(ISD::EXTLOAD, StoreTy)) | ||||
8948 | LastLegalIntegerType = i+1; | ||||
8949 | } | ||||
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8950 | } |
8951 | |||||
8952 | // Only use vector types if the vector type is larger than the integer type. | ||||
8953 | // If they are the same, use integers. | ||||
Nadav Rotem | 6cc4b8d | 2013-02-14 18:28:52 +0000 | [diff] [blame] | 8954 | bool UseVectorTy = LastLegalVectorType > LastLegalIntegerType && !NoVectors; |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8955 | unsigned LastLegalType = std::max(LastLegalVectorType, LastLegalIntegerType); |
8956 | |||||
8957 | // We add +1 here because the LastXXX variables refer to location while | ||||
8958 | // the NumElem refers to array/index size. | ||||
8959 | unsigned NumElem = std::min(LastConsecutiveStore, LastConsecutiveLoad) + 1; | ||||
8960 | NumElem = std::min(LastLegalType, NumElem); | ||||
8961 | |||||
8962 | if (NumElem < 2) | ||||
8963 | return false; | ||||
8964 | |||||
8965 | // The earliest Node in the DAG. | ||||
8966 | unsigned EarliestNodeUsed = 0; | ||||
8967 | LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode; | ||||
8968 | for (unsigned i=1; i<NumElem; ++i) { | ||||
8969 | // Find a chain for the new wide-store operand. Notice that some | ||||
8970 | // of the store nodes that we found may not be selected for inclusion | ||||
8971 | // in the wide store. The chain we use needs to be the chain of the | ||||
8972 | // earliest store node which is *used* and replaced by the wide store. | ||||
8973 | if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum) | ||||
8974 | EarliestNodeUsed = i; | ||||
8975 | } | ||||
8976 | |||||
8977 | // Find if it is better to use vectors or integers to load and store | ||||
8978 | // to memory. | ||||
8979 | EVT JointMemOpVT; | ||||
8980 | if (UseVectorTy) { | ||||
8981 | JointMemOpVT = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem); | ||||
8982 | } else { | ||||
8983 | unsigned StoreBW = NumElem * ElementSizeBytes * 8; | ||||
8984 | JointMemOpVT = EVT::getIntegerVT(*DAG.getContext(), StoreBW); | ||||
8985 | } | ||||
8986 | |||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8987 | SDLoc LoadDL(LoadNodes[0].MemNode); |
8988 | SDLoc StoreDL(StoreNodes[0].MemNode); | ||||
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 8989 | |
8990 | LoadSDNode *FirstLoad = cast<LoadSDNode>(LoadNodes[0].MemNode); | ||||
8991 | SDValue NewLoad = DAG.getLoad(JointMemOpVT, LoadDL, | ||||
8992 | FirstLoad->getChain(), | ||||
8993 | FirstLoad->getBasePtr(), | ||||
8994 | FirstLoad->getPointerInfo(), | ||||
8995 | false, false, false, | ||||
8996 | FirstLoad->getAlignment()); | ||||
8997 | |||||
8998 | SDValue NewStore = DAG.getStore(EarliestOp->getChain(), StoreDL, NewLoad, | ||||
8999 | FirstInChain->getBasePtr(), | ||||
9000 | FirstInChain->getPointerInfo(), false, false, | ||||
9001 | FirstInChain->getAlignment()); | ||||
9002 | |||||
Nadav Rotem | 2e7d381 | 2012-10-03 19:30:31 +0000 | [diff] [blame] | 9003 | // Replace one of the loads with the new load. |
9004 | LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[0].MemNode); | ||||
9005 | DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), | ||||
9006 | SDValue(NewLoad.getNode(), 1)); | ||||
9007 | |||||
9008 | // Remove the rest of the load chains. | ||||
9009 | for (unsigned i = 1; i < NumElem ; ++i) { | ||||
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9010 | // Replace all chain users of the old load nodes with the chain of the new |
9011 | // load node. | ||||
9012 | LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[i].MemNode); | ||||
Nadav Rotem | 2e7d381 | 2012-10-03 19:30:31 +0000 | [diff] [blame] | 9013 | DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), Ld->getChain()); |
9014 | } | ||||
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9015 | |
Nadav Rotem | 2e7d381 | 2012-10-03 19:30:31 +0000 | [diff] [blame] | 9016 | // Replace the first store with the new store. |
9017 | CombineTo(EarliestOp, NewStore); | ||||
9018 | // Erase all other stores. | ||||
9019 | for (unsigned i = 0; i < NumElem ; ++i) { | ||||
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9020 | // Remove all Store nodes. |
9021 | if (StoreNodes[i].MemNode == EarliestOp) | ||||
9022 | continue; | ||||
9023 | StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode); | ||||
9024 | DAG.ReplaceAllUsesOfValueWith(SDValue(St, 0), St->getChain()); | ||||
9025 | removeFromWorkList(St); | ||||
9026 | DAG.DeleteNode(St); | ||||
9027 | } | ||||
9028 | |||||
9029 | return true; | ||||
9030 | } | ||||
9031 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9032 | SDValue DAGCombiner::visitSTORE(SDNode *N) { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 9033 | StoreSDNode *ST = cast<StoreSDNode>(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9034 | SDValue Chain = ST->getChain(); |
9035 | SDValue Value = ST->getValue(); | ||||
9036 | SDValue Ptr = ST->getBasePtr(); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9037 | |
Evan Cheng | 59d5b68 | 2007-05-07 21:27:48 +0000 | [diff] [blame] | 9038 | // If this is a store of a bit convert, store the input value if the |
Evan Cheng | 2c4f943 | 2007-05-09 21:49:47 +0000 | [diff] [blame] | 9039 | // resultant store does not need a higher alignment than the original. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9040 | if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() && |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 9041 | ST->isUnindexed()) { |
Dan Gohman | 1ba519b | 2009-02-20 23:29:13 +0000 | [diff] [blame] | 9042 | unsigned OrigAlign = ST->getAlignment(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 9043 | EVT SVT = Value.getOperand(0).getValueType(); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 9044 | unsigned Align = TLI.getDataLayout()-> |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 9045 | getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext())); |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 9046 | if (Align <= OrigAlign && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 9047 | ((!LegalOperations && !ST->isVolatile()) || |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 9048 | TLI.isOperationLegalOrCustom(ISD::STORE, SVT))) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9049 | return DAG.getStore(Chain, SDLoc(N), Value.getOperand(0), |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 9050 | Ptr, ST->getPointerInfo(), ST->isVolatile(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 9051 | ST->isNonTemporal(), OrigAlign); |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 9052 | } |
Owen Anderson | a34d936 | 2011-04-14 17:30:49 +0000 | [diff] [blame] | 9053 | |
Chris Lattner | b3452ea | 2011-04-09 02:32:02 +0000 | [diff] [blame] | 9054 | // Turn 'store undef, Ptr' -> nothing. |
9055 | if (Value.getOpcode() == ISD::UNDEF && ST->isUnindexed()) | ||||
9056 | return Chain; | ||||
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 9057 | |
Nate Begeman | 2cbba89 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 9058 | // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' |
Nate Begeman | 2cbba89 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 9059 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) { |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 9060 | // NOTE: If the original store is volatile, this transform must not increase |
9061 | // the number of stores. For example, on x86-32 an f64 can be stored in one | ||||
9062 | // processor operation but an i64 (which is not legal) requires two. So the | ||||
9063 | // transform should not be done in this case. | ||||
Evan Cheng | 25ece66 | 2006-12-11 17:25:19 +0000 | [diff] [blame] | 9064 | if (Value.getOpcode() != ISD::TargetConstantFP) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9065 | SDValue Tmp; |
Craig Topper | 0ff1190 | 2013-08-15 02:44:19 +0000 | [diff] [blame] | 9066 | switch (CFP->getSimpleValueType(0).SimpleTy) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 9067 | default: llvm_unreachable("Unknown FP type"); |
Pete Cooper | 438c040 | 2012-06-21 18:00:39 +0000 | [diff] [blame] | 9068 | case MVT::f16: // We don't do this for these yet. |
9069 | case MVT::f80: | ||||
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 9070 | case MVT::f128: |
9071 | case MVT::ppcf128: | ||||
Dale Johannesen | c7b21d5 | 2007-09-18 18:36:59 +0000 | [diff] [blame] | 9072 | break; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 9073 | case MVT::f32: |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 9074 | if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) || |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 9075 | TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) { |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 9076 | Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF(). |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 9077 | bitcastToAPInt().getZExtValue(), MVT::i32); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9078 | return DAG.getStore(Chain, SDLoc(N), Tmp, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 9079 | Ptr, ST->getPointerInfo(), ST->isVolatile(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 9080 | ST->isNonTemporal(), ST->getAlignment()); |
Chris Lattner | 62be1a7 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 9081 | } |
9082 | break; | ||||
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 9083 | case MVT::f64: |
Chris Lattner | 2392ae7 | 2010-04-15 04:48:01 +0000 | [diff] [blame] | 9084 | if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations && |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 9085 | !ST->isVolatile()) || |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 9086 | TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) { |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 9087 | Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt(). |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 9088 | getZExtValue(), MVT::i64); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9089 | return DAG.getStore(Chain, SDLoc(N), Tmp, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 9090 | Ptr, ST->getPointerInfo(), ST->isVolatile(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 9091 | ST->isNonTemporal(), ST->getAlignment()); |
Chris Lattner | b3452ea | 2011-04-09 02:32:02 +0000 | [diff] [blame] | 9092 | } |
Owen Anderson | a34d936 | 2011-04-14 17:30:49 +0000 | [diff] [blame] | 9093 | |
Chris Lattner | b3452ea | 2011-04-09 02:32:02 +0000 | [diff] [blame] | 9094 | if (!ST->isVolatile() && |
9095 | TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) { | ||||
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 9096 | // Many FP stores are not made apparent until after legalize, e.g. for |
Chris Lattner | 62be1a7 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 9097 | // argument passing. Since this is so common, custom legalize the |
9098 | // 64-bit integer store into two 32-bit stores. | ||||
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 9099 | uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 9100 | SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32); |
9101 | SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32); | ||||
Duncan Sands | 0753fc1 | 2008-02-11 10:37:04 +0000 | [diff] [blame] | 9102 | if (TLI.isBigEndian()) std::swap(Lo, Hi); |
Chris Lattner | 62be1a7 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 9103 | |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 9104 | unsigned Alignment = ST->getAlignment(); |
9105 | bool isVolatile = ST->isVolatile(); | ||||
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 9106 | bool isNonTemporal = ST->isNonTemporal(); |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 9107 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9108 | SDValue St0 = DAG.getStore(Chain, SDLoc(ST), Lo, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 9109 | Ptr, ST->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 9110 | isVolatile, isNonTemporal, |
9111 | ST->getAlignment()); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9112 | Ptr = DAG.getNode(ISD::ADD, SDLoc(N), Ptr.getValueType(), Ptr, |
Chris Lattner | 62be1a7 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 9113 | DAG.getConstant(4, Ptr.getValueType())); |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 9114 | Alignment = MinAlign(Alignment, 4U); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9115 | SDValue St1 = DAG.getStore(Chain, SDLoc(ST), Hi, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 9116 | Ptr, ST->getPointerInfo().getWithOffset(4), |
9117 | isVolatile, isNonTemporal, | ||||
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 9118 | Alignment); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9119 | return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other, |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9120 | St0, St1); |
Chris Lattner | 62be1a7 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 9121 | } |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9122 | |
Chris Lattner | 62be1a7 | 2006-12-12 04:16:14 +0000 | [diff] [blame] | 9123 | break; |
Evan Cheng | 25ece66 | 2006-12-11 17:25:19 +0000 | [diff] [blame] | 9124 | } |
Nate Begeman | 2cbba89 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 9125 | } |
Nate Begeman | 2cbba89 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 9126 | } |
9127 | |||||
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 9128 | // Try to infer better alignment information than the store already has. |
9129 | if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) { | ||||
Evan Cheng | ed1c0c7 | 2011-11-28 22:37:34 +0000 | [diff] [blame] | 9130 | if (unsigned Align = DAG.InferPtrAlignment(Ptr)) { |
9131 | if (Align > ST->getAlignment()) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9132 | return DAG.getTruncStore(Chain, SDLoc(N), Value, |
Evan Cheng | ed1c0c7 | 2011-11-28 22:37:34 +0000 | [diff] [blame] | 9133 | Ptr, ST->getPointerInfo(), ST->getMemoryVT(), |
9134 | ST->isVolatile(), ST->isNonTemporal(), Align); | ||||
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 9135 | } |
9136 | } | ||||
9137 | |||||
Evan Cheng | 31959b1 | 2011-02-02 01:06:55 +0000 | [diff] [blame] | 9138 | // Try transforming a pair floating point load / store ops to integer |
9139 | // load / store ops. | ||||
9140 | SDValue NewST = TransformFPLoadStorePair(N); | ||||
9141 | if (NewST.getNode()) | ||||
9142 | return NewST; | ||||
9143 | |||||
Hal Finkel | 253acef | 2013-08-29 03:29:55 +0000 | [diff] [blame] | 9144 | bool UseAA = CombinerAA.getNumOccurrences() > 0 ? CombinerAA : |
9145 | TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA(); | ||||
9146 | if (UseAA) { | ||||
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 9147 | // Walk up chain skipping non-aliasing memory nodes. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9148 | SDValue BetterChain = FindBetterChain(N, Chain); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9149 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 9150 | // If there is a better chain. |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 9151 | if (Chain != BetterChain) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9152 | SDValue ReplStore; |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 9153 | |
9154 | // Replace the chain to avoid dependency. | ||||
Jim Laskey | d4edf2c | 2006-10-14 12:14:27 +0000 | [diff] [blame] | 9155 | if (ST->isTruncatingStore()) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9156 | ReplStore = DAG.getTruncStore(BetterChain, SDLoc(N), Value, Ptr, |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 9157 | ST->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 9158 | ST->getMemoryVT(), ST->isVolatile(), |
9159 | ST->isNonTemporal(), ST->getAlignment()); | ||||
Jim Laskey | d4edf2c | 2006-10-14 12:14:27 +0000 | [diff] [blame] | 9160 | } else { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9161 | ReplStore = DAG.getStore(BetterChain, SDLoc(N), Value, Ptr, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 9162 | ST->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 9163 | ST->isVolatile(), ST->isNonTemporal(), |
9164 | ST->getAlignment()); | ||||
Jim Laskey | d4edf2c | 2006-10-14 12:14:27 +0000 | [diff] [blame] | 9165 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9166 | |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 9167 | // Create token to keep both nodes around. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9168 | SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 9169 | MVT::Other, Chain, ReplStore); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9170 | |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 9171 | // Make sure the new and old chains are cleaned up. |
9172 | AddToWorkList(Token.getNode()); | ||||
9173 | |||||
Jim Laskey | 274062c | 2006-10-13 23:32:28 +0000 | [diff] [blame] | 9174 | // Don't add users to work list. |
9175 | return CombineTo(N, Token, false); | ||||
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 9176 | } |
Jim Laskey | d1aed7a | 2006-09-21 16:28:59 +0000 | [diff] [blame] | 9177 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9178 | |
Evan Cheng | 33dbedc | 2006-11-05 09:31:14 +0000 | [diff] [blame] | 9179 | // Try transforming N to an indexed store. |
Evan Cheng | bbd6f6e | 2006-11-07 09:03:05 +0000 | [diff] [blame] | 9180 | if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9181 | return SDValue(N, 0); |
Evan Cheng | 33dbedc | 2006-11-05 09:31:14 +0000 | [diff] [blame] | 9182 | |
Chris Lattner | 3c87285 | 2007-12-29 06:26:16 +0000 | [diff] [blame] | 9183 | // FIXME: is there such a thing as a truncating indexed store? |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 9184 | if (ST->isTruncatingStore() && ST->isUnindexed() && |
Nadav Rotem | baff46f | 2011-06-15 11:19:12 +0000 | [diff] [blame] | 9185 | Value.getValueType().isInteger()) { |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 9186 | // See if we can simplify the input to this truncstore with knowledge that |
9187 | // only the low bits are being used. For example: | ||||
9188 | // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8" | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9189 | SDValue Shorter = |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 9190 | GetDemandedBits(Value, |
Nadav Rotem | baff46f | 2011-06-15 11:19:12 +0000 | [diff] [blame] | 9191 | APInt::getLowBitsSet( |
9192 | Value.getValueType().getScalarType().getSizeInBits(), | ||||
9193 | ST->getMemoryVT().getScalarType().getSizeInBits())); | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9194 | AddToWorkList(Value.getNode()); |
9195 | if (Shorter.getNode()) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9196 | return DAG.getTruncStore(Chain, SDLoc(N), Shorter, |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 9197 | Ptr, ST->getPointerInfo(), ST->getMemoryVT(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 9198 | ST->isVolatile(), ST->isNonTemporal(), |
9199 | ST->getAlignment()); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9200 | |
Chris Lattner | e33544c | 2007-10-13 06:58:48 +0000 | [diff] [blame] | 9201 | // Otherwise, see if we can simplify the operation with |
9202 | // SimplifyDemandedBits, which only works if the value has a single use. | ||||
Dan Gohman | 7b8d4a9 | 2008-02-27 00:25:32 +0000 | [diff] [blame] | 9203 | if (SimplifyDemandedBits(Value, |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 9204 | APInt::getLowBitsSet( |
9205 | Value.getValueType().getScalarType().getSizeInBits(), | ||||
9206 | ST->getMemoryVT().getScalarType().getSizeInBits()))) | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9207 | return SDValue(N, 0); |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 9208 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9209 | |
Chris Lattner | 3c87285 | 2007-12-29 06:26:16 +0000 | [diff] [blame] | 9210 | // If this is a load followed by a store to the same location, then the store |
9211 | // is dead/noop. | ||||
9212 | if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) { | ||||
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 9213 | if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() && |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 9214 | ST->isUnindexed() && !ST->isVolatile() && |
Chris Lattner | 07649d9 | 2008-01-08 23:08:06 +0000 | [diff] [blame] | 9215 | // There can't be any side effects between the load and store, such as |
9216 | // a call or store. | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9217 | Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) { |
Chris Lattner | 3c87285 | 2007-12-29 06:26:16 +0000 | [diff] [blame] | 9218 | // The store is dead, remove it. |
9219 | return Chain; | ||||
9220 | } | ||||
9221 | } | ||||
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 9222 | |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 9223 | // If this is an FP_ROUND or TRUNC followed by a store, fold this into a |
9224 | // truncating store. We can do this even if this is already a truncstore. | ||||
9225 | if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE) | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9226 | && Value.getNode()->hasOneUse() && ST->isUnindexed() && |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 9227 | TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(), |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 9228 | ST->getMemoryVT())) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9229 | return DAG.getTruncStore(Chain, SDLoc(N), Value.getOperand(0), |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 9230 | Ptr, ST->getPointerInfo(), ST->getMemoryVT(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 9231 | ST->isVolatile(), ST->isNonTemporal(), |
9232 | ST->getAlignment()); | ||||
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 9233 | } |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 9234 | |
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9235 | // Only perform this optimization before the types are legal, because we |
Nadav Rotem | ea2c50c | 2012-10-04 22:35:15 +0000 | [diff] [blame] | 9236 | // don't want to perform this optimization on every DAGCombine invocation. |
Nadav Rotem | a569a80 | 2012-12-02 17:14:09 +0000 | [diff] [blame] | 9237 | if (!LegalTypes) { |
9238 | bool EverChanged = false; | ||||
9239 | |||||
9240 | do { | ||||
9241 | // There can be multiple store sequences on the same chain. | ||||
9242 | // Keep trying to merge store sequences until we are unable to do so | ||||
9243 | // or until we merge the last store on the chain. | ||||
9244 | bool Changed = MergeConsecutiveStores(ST); | ||||
9245 | EverChanged |= Changed; | ||||
9246 | if (!Changed) break; | ||||
9247 | } while (ST->getOpcode() != ISD::DELETED_NODE); | ||||
9248 | |||||
9249 | if (EverChanged) | ||||
9250 | return SDValue(N, 0); | ||||
9251 | } | ||||
Nadav Rotem | c653de6 | 2012-10-03 16:11:15 +0000 | [diff] [blame] | 9252 | |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 9253 | return ReduceLoadOpStoreWidth(N); |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 9254 | } |
9255 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9256 | SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) { |
9257 | SDValue InVec = N->getOperand(0); | ||||
9258 | SDValue InVal = N->getOperand(1); | ||||
9259 | SDValue EltNo = N->getOperand(2); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9260 | SDLoc dl(N); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9261 | |
Bob Wilson | 492fd45 | 2010-05-19 23:42:58 +0000 | [diff] [blame] | 9262 | // If the inserted element is an UNDEF, just use the input vector. |
9263 | if (InVal.getOpcode() == ISD::UNDEF) | ||||
9264 | return InVec; | ||||
9265 | |||||
Nadav Rotem | 609d54e | 2011-02-12 14:40:33 +0000 | [diff] [blame] | 9266 | EVT VT = InVec.getValueType(); |
9267 | |||||
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 9268 | // If we can't generate a legal BUILD_VECTOR, exit |
Nadav Rotem | 609d54e | 2011-02-12 14:40:33 +0000 | [diff] [blame] | 9269 | if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) |
9270 | return SDValue(); | ||||
9271 | |||||
Eli Friedman | 9db817f | 2011-09-09 21:04:06 +0000 | [diff] [blame] | 9272 | // Check that we know which element is being inserted |
9273 | if (!isa<ConstantSDNode>(EltNo)) | ||||
9274 | return SDValue(); | ||||
9275 | unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9276 | |
Eli Friedman | 9db817f | 2011-09-09 21:04:06 +0000 | [diff] [blame] | 9277 | // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially |
9278 | // be converted to a BUILD_VECTOR). Fill in the Ops vector with the | ||||
9279 | // vector elements. | ||||
9280 | SmallVector<SDValue, 8> Ops; | ||||
Quentin Colombet | 75c9433 | 2013-07-30 00:24:09 +0000 | [diff] [blame] | 9281 | // Do not combine these two vectors if the output vector will not replace |
9282 | // the input vector. | ||||
9283 | if (InVec.getOpcode() == ISD::BUILD_VECTOR && InVec.hasOneUse()) { | ||||
Eli Friedman | 9db817f | 2011-09-09 21:04:06 +0000 | [diff] [blame] | 9284 | Ops.append(InVec.getNode()->op_begin(), |
9285 | InVec.getNode()->op_end()); | ||||
9286 | } else if (InVec.getOpcode() == ISD::UNDEF) { | ||||
9287 | unsigned NElts = VT.getVectorNumElements(); | ||||
9288 | Ops.append(NElts, DAG.getUNDEF(InVal.getValueType())); | ||||
9289 | } else { | ||||
9290 | return SDValue(); | ||||
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 9291 | } |
Eli Friedman | 9db817f | 2011-09-09 21:04:06 +0000 | [diff] [blame] | 9292 | |
9293 | // Insert the element | ||||
9294 | if (Elt < Ops.size()) { | ||||
9295 | // All the operands of BUILD_VECTOR must have the same type; | ||||
9296 | // we enforce that here. | ||||
9297 | EVT OpVT = Ops[0].getValueType(); | ||||
9298 | if (InVal.getValueType() != OpVT) | ||||
9299 | InVal = OpVT.bitsGT(InVal.getValueType()) ? | ||||
9300 | DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) : | ||||
9301 | DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal); | ||||
9302 | Ops[Elt] = InVal; | ||||
9303 | } | ||||
9304 | |||||
9305 | // Return the new vector | ||||
9306 | return DAG.getNode(ISD::BUILD_VECTOR, dl, | ||||
9307 | VT, &Ops[0], Ops.size()); | ||||
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 9308 | } |
9309 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9310 | SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) { |
Mon P Wang | 7ac9cdf | 2009-01-17 00:07:25 +0000 | [diff] [blame] | 9311 | // (vextract (scalar_to_vector val, 0) -> val |
9312 | SDValue InVec = N->getOperand(0); | ||||
Nadav Rotem | ba05c91 | 2012-01-17 21:44:01 +0000 | [diff] [blame] | 9313 | EVT VT = InVec.getValueType(); |
9314 | EVT NVT = N->getValueType(0); | ||||
Mon P Wang | 7ac9cdf | 2009-01-17 00:07:25 +0000 | [diff] [blame] | 9315 | |
Duncan Sands | c356f33 | 2011-05-09 08:03:33 +0000 | [diff] [blame] | 9316 | if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) { |
9317 | // Check if the result type doesn't match the inserted element type. A | ||||
9318 | // SCALAR_TO_VECTOR may truncate the inserted element and the | ||||
9319 | // EXTRACT_VECTOR_ELT may widen the extracted vector. | ||||
9320 | SDValue InOp = InVec.getOperand(0); | ||||
Duncan Sands | c356f33 | 2011-05-09 08:03:33 +0000 | [diff] [blame] | 9321 | if (InOp.getValueType() != NVT) { |
9322 | assert(InOp.getValueType().isInteger() && NVT.isInteger()); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9323 | return DAG.getSExtOrTrunc(InOp, SDLoc(InVec), NVT); |
Duncan Sands | c356f33 | 2011-05-09 08:03:33 +0000 | [diff] [blame] | 9324 | } |
9325 | return InOp; | ||||
9326 | } | ||||
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9327 | |
Nadav Rotem | ba05c91 | 2012-01-17 21:44:01 +0000 | [diff] [blame] | 9328 | SDValue EltNo = N->getOperand(1); |
9329 | bool ConstEltNo = isa<ConstantSDNode>(EltNo); | ||||
9330 | |||||
9331 | // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT. | ||||
9332 | // We only perform this optimization before the op legalization phase because | ||||
Nadav Rotem | 6dfabb6 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 9333 | // we may introduce new vector instructions which are not backed by TD |
9334 | // patterns. For example on AVX, extracting elements from a wide vector | ||||
9335 | // without using extract_subvector. | ||||
Nadav Rotem | ba05c91 | 2012-01-17 21:44:01 +0000 | [diff] [blame] | 9336 | if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE |
9337 | && ConstEltNo && !LegalOperations) { | ||||
9338 | int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); | ||||
9339 | int NumElem = VT.getVectorNumElements(); | ||||
9340 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(InVec); | ||||
9341 | // Find the new index to extract from. | ||||
9342 | int OrigElt = SVOp->getMaskElt(Elt); | ||||
9343 | |||||
9344 | // Extracting an undef index is undef. | ||||
9345 | if (OrigElt == -1) | ||||
9346 | return DAG.getUNDEF(NVT); | ||||
9347 | |||||
9348 | // Select the right vector half to extract from. | ||||
9349 | if (OrigElt < NumElem) { | ||||
9350 | InVec = InVec->getOperand(0); | ||||
9351 | } else { | ||||
9352 | InVec = InVec->getOperand(1); | ||||
9353 | OrigElt -= NumElem; | ||||
9354 | } | ||||
9355 | |||||
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 9356 | EVT IndexTy = TLI.getVectorIdxTy(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9357 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N), NVT, |
Jim Grosbach | a249f7d | 2012-05-08 20:56:07 +0000 | [diff] [blame] | 9358 | InVec, DAG.getConstant(OrigElt, IndexTy)); |
Nadav Rotem | ba05c91 | 2012-01-17 21:44:01 +0000 | [diff] [blame] | 9359 | } |
9360 | |||||
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9361 | // Perform only after legalization to ensure build_vector / vector_shuffle |
9362 | // optimizations have already been done. | ||||
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 9363 | if (!LegalOperations) return SDValue(); |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9364 | |
Mon P Wang | 7ac9cdf | 2009-01-17 00:07:25 +0000 | [diff] [blame] | 9365 | // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size) |
9366 | // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size) | ||||
9367 | // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr) | ||||
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 9368 | |
Nadav Rotem | ba05c91 | 2012-01-17 21:44:01 +0000 | [diff] [blame] | 9369 | if (ConstEltNo) { |
Eric Christopher | caebdd4 | 2010-11-03 09:36:40 +0000 | [diff] [blame] | 9370 | int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 9371 | bool NewLoad = false; |
Mon P Wang | a60b523 | 2008-12-11 00:26:16 +0000 | [diff] [blame] | 9372 | bool BCNumEltsChanged = false; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 9373 | EVT ExtVT = VT.getVectorElementType(); |
9374 | EVT LVT = ExtVT; | ||||
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9375 | |
Evan Cheng | 84387ea | 2012-03-13 22:00:52 +0000 | [diff] [blame] | 9376 | // If the result of load has to be truncated, then it's not necessarily |
9377 | // profitable. | ||||
Evan Cheng | a03d366 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 9378 | if (NVT.bitsLT(LVT) && !TLI.isTruncateFree(LVT, NVT)) |
Evan Cheng | 84387ea | 2012-03-13 22:00:52 +0000 | [diff] [blame] | 9379 | return SDValue(); |
9380 | |||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9381 | if (InVec.getOpcode() == ISD::BITCAST) { |
Eli Friedman | d6e2560 | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 9382 | // Don't duplicate a load with other uses. |
9383 | if (!InVec.hasOneUse()) | ||||
9384 | return SDValue(); | ||||
9385 | |||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 9386 | EVT BCVT = InVec.getOperand(0).getValueType(); |
9387 | if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType())) | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9388 | return SDValue(); |
Mon P Wang | a60b523 | 2008-12-11 00:26:16 +0000 | [diff] [blame] | 9389 | if (VT.getVectorNumElements() != BCVT.getVectorNumElements()) |
9390 | BCNumEltsChanged = true; | ||||
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9391 | InVec = InVec.getOperand(0); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 9392 | ExtVT = BCVT.getVectorElementType(); |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9393 | NewLoad = true; |
9394 | } | ||||
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 9395 | |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9396 | LoadSDNode *LN0 = NULL; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 9397 | const ShuffleVectorSDNode *SVN = NULL; |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9398 | if (ISD::isNormalLoad(InVec.getNode())) { |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9399 | LN0 = cast<LoadSDNode>(InVec); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9400 | } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR && |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 9401 | InVec.getOperand(0).getValueType() == ExtVT && |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9402 | ISD::isNormalLoad(InVec.getOperand(0).getNode())) { |
Eli Friedman | d6e2560 | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 9403 | // Don't duplicate a load with other uses. |
9404 | if (!InVec.hasOneUse()) | ||||
9405 | return SDValue(); | ||||
9406 | |||||
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9407 | LN0 = cast<LoadSDNode>(InVec.getOperand(0)); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 9408 | } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) { |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9409 | // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1) |
9410 | // => | ||||
9411 | // (load $addr+1*size) | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9412 | |
Eli Friedman | d6e2560 | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 9413 | // Don't duplicate a load with other uses. |
9414 | if (!InVec.hasOneUse()) | ||||
9415 | return SDValue(); | ||||
9416 | |||||
Mon P Wang | a60b523 | 2008-12-11 00:26:16 +0000 | [diff] [blame] | 9417 | // If the bit convert changed the number of elements, it is unsafe |
9418 | // to examine the mask. | ||||
9419 | if (BCNumEltsChanged) | ||||
9420 | return SDValue(); | ||||
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 9421 | |
9422 | // Select the input vector, guarding against out of range extract vector. | ||||
9423 | unsigned NumElems = VT.getVectorNumElements(); | ||||
Eric Christopher | caebdd4 | 2010-11-03 09:36:40 +0000 | [diff] [blame] | 9424 | int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 9425 | InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1); |
9426 | |||||
Eli Friedman | d6e2560 | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 9427 | if (InVec.getOpcode() == ISD::BITCAST) { |
9428 | // Don't duplicate a load with other uses. | ||||
9429 | if (!InVec.hasOneUse()) | ||||
9430 | return SDValue(); | ||||
9431 | |||||
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9432 | InVec = InVec.getOperand(0); |
Eli Friedman | d6e2560 | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 9433 | } |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9434 | if (ISD::isNormalLoad(InVec.getNode())) { |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9435 | LN0 = cast<LoadSDNode>(InVec); |
Ted Kremenek | d0e88f3 | 2010-04-08 18:49:30 +0000 | [diff] [blame] | 9436 | Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems; |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 9437 | } |
9438 | } | ||||
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9439 | |
Eli Friedman | d6e2560 | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 9440 | // Make sure we found a non-volatile load and the extractelement is |
9441 | // the only use. | ||||
Nadav Rotem | 42febc6 | 2011-05-11 14:40:50 +0000 | [diff] [blame] | 9442 | if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile()) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9443 | return SDValue(); |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9444 | |
Eric Christopher | d81f17a | 2010-11-03 20:44:42 +0000 | [diff] [blame] | 9445 | // If Idx was -1 above, Elt is going to be -1, so just return undef. |
9446 | if (Elt == -1) | ||||
Eli Friedman | ed4b427 | 2011-07-25 22:25:42 +0000 | [diff] [blame] | 9447 | return DAG.getUNDEF(LVT); |
Eric Christopher | d81f17a | 2010-11-03 20:44:42 +0000 | [diff] [blame] | 9448 | |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9449 | unsigned Align = LN0->getAlignment(); |
9450 | if (NewLoad) { | ||||
9451 | // Check the resultant load doesn't need a higher alignment than the | ||||
9452 | // original load. | ||||
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9453 | unsigned NewAlign = |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 9454 | TLI.getDataLayout() |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 9455 | ->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext())); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9456 | |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 9457 | if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9458 | return SDValue(); |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9459 | |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9460 | Align = NewAlign; |
9461 | } | ||||
9462 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9463 | SDValue NewPtr = LN0->getBasePtr(); |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 9464 | unsigned PtrOff = 0; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9465 | |
Eric Christopher | d81f17a | 2010-11-03 20:44:42 +0000 | [diff] [blame] | 9466 | if (Elt) { |
Chris Lattner | fa45901 | 2010-09-21 16:08:50 +0000 | [diff] [blame] | 9467 | PtrOff = LVT.getSizeInBits() * Elt / 8; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 9468 | EVT PtrType = NewPtr.getValueType(); |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9469 | if (TLI.isBigEndian()) |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 9470 | PtrOff = VT.getSizeInBits() / 8 - PtrOff; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9471 | NewPtr = DAG.getNode(ISD::ADD, SDLoc(N), PtrType, NewPtr, |
Evan Cheng | 77f0b7a | 2008-05-13 08:35:03 +0000 | [diff] [blame] | 9472 | DAG.getConstant(PtrOff, PtrType)); |
9473 | } | ||||
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9474 | |
Eli Friedman | 4db4add | 2011-11-16 23:50:22 +0000 | [diff] [blame] | 9475 | // The replacement we need to do here is a little tricky: we need to |
9476 | // replace an extractelement of a load with a load. | ||||
9477 | // Use ReplaceAllUsesOfValuesWith to do the replacement. | ||||
Eli Friedman | d6e2560 | 2011-12-26 22:49:32 +0000 | [diff] [blame] | 9478 | // Note that this replacement assumes that the extractvalue is the only |
9479 | // use of the load; that's okay because we don't want to perform this | ||||
9480 | // transformation in other cases anyway. | ||||
Evan Cheng | 84387ea | 2012-03-13 22:00:52 +0000 | [diff] [blame] | 9481 | SDValue Load; |
Evan Cheng | a03d366 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 9482 | SDValue Chain; |
Evan Cheng | 84387ea | 2012-03-13 22:00:52 +0000 | [diff] [blame] | 9483 | if (NVT.bitsGT(LVT)) { |
9484 | // If the result type of vextract is wider than the load, then issue an | ||||
9485 | // extending load instead. | ||||
9486 | ISD::LoadExtType ExtType = TLI.isLoadExtLegal(ISD::ZEXTLOAD, LVT) | ||||
9487 | ? ISD::ZEXTLOAD : ISD::EXTLOAD; | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9488 | Load = DAG.getExtLoad(ExtType, SDLoc(N), NVT, LN0->getChain(), |
Evan Cheng | 84387ea | 2012-03-13 22:00:52 +0000 | [diff] [blame] | 9489 | NewPtr, LN0->getPointerInfo().getWithOffset(PtrOff), |
9490 | LVT, LN0->isVolatile(), LN0->isNonTemporal(),Align); | ||||
Evan Cheng | a03d366 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 9491 | Chain = Load.getValue(1); |
9492 | } else { | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9493 | Load = DAG.getLoad(LVT, SDLoc(N), LN0->getChain(), NewPtr, |
Evan Cheng | 84387ea | 2012-03-13 22:00:52 +0000 | [diff] [blame] | 9494 | LN0->getPointerInfo().getWithOffset(PtrOff), |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 9495 | LN0->isVolatile(), LN0->isNonTemporal(), |
Evan Cheng | 84387ea | 2012-03-13 22:00:52 +0000 | [diff] [blame] | 9496 | LN0->isInvariant(), Align); |
Evan Cheng | a03d366 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 9497 | Chain = Load.getValue(1); |
9498 | if (NVT.bitsLT(LVT)) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9499 | Load = DAG.getNode(ISD::TRUNCATE, SDLoc(N), NVT, Load); |
Evan Cheng | a03d366 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 9500 | else |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9501 | Load = DAG.getNode(ISD::BITCAST, SDLoc(N), NVT, Load); |
Evan Cheng | a03d366 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 9502 | } |
Eli Friedman | 4db4add | 2011-11-16 23:50:22 +0000 | [diff] [blame] | 9503 | WorkListRemover DeadNodes(*this); |
9504 | SDValue From[] = { SDValue(N, 0), SDValue(LN0,1) }; | ||||
Evan Cheng | a03d366 | 2012-03-13 22:16:11 +0000 | [diff] [blame] | 9505 | SDValue To[] = { Load, Chain }; |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 9506 | DAG.ReplaceAllUsesOfValuesWith(From, To, 2); |
Eli Friedman | 4db4add | 2011-11-16 23:50:22 +0000 | [diff] [blame] | 9507 | // Since we're explcitly calling ReplaceAllUses, add the new node to the |
9508 | // worklist explicitly as well. | ||||
9509 | AddToWorkList(Load.getNode()); | ||||
Craig Topper | 0c9da21 | 2012-03-20 05:28:39 +0000 | [diff] [blame] | 9510 | AddUsersToWorkList(Load.getNode()); // Add users too |
Eli Friedman | 4db4add | 2011-11-16 23:50:22 +0000 | [diff] [blame] | 9511 | // Make sure to revisit this node to clean it up; it will usually be dead. |
9512 | AddToWorkList(N); | ||||
9513 | return SDValue(N, 0); | ||||
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 9514 | } |
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9515 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9516 | return SDValue(); |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 9517 | } |
Evan Cheng | 513da43 | 2007-10-06 08:19:55 +0000 | [diff] [blame] | 9518 | |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9519 | // Simplify (build_vec (ext )) to (bitcast (build_vec )) |
9520 | SDValue DAGCombiner::reduceBuildVecExtToExtBuildVec(SDNode *N) { | ||||
9521 | // We perform this optimization post type-legalization because | ||||
9522 | // the type-legalizer often scalarizes integer-promoted vectors. | ||||
9523 | // Performing this optimization before may create bit-casts which | ||||
9524 | // will be type-legalized to complex code sequences. | ||||
9525 | // We perform this optimization only before the operation legalizer because we | ||||
9526 | // may introduce illegal operations. | ||||
9527 | if (Level != AfterLegalizeVectorOps && Level != AfterLegalizeTypes) | ||||
9528 | return SDValue(); | ||||
9529 | |||||
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9530 | unsigned NumInScalars = N->getNumOperands(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9531 | SDLoc dl(N); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 9532 | EVT VT = N->getValueType(0); |
Nadav Rotem | b87bdac | 2012-07-15 08:38:23 +0000 | [diff] [blame] | 9533 | |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9534 | // Check to see if this is a BUILD_VECTOR of a bunch of values |
9535 | // which come from any_extend or zero_extend nodes. If so, we can create | ||||
9536 | // a new BUILD_VECTOR using bit-casts which may enable other BUILD_VECTOR | ||||
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 9537 | // optimizations. We do not handle sign-extend because we can't fill the sign |
9538 | // using shuffles. | ||||
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9539 | EVT SourceType = MVT::Other; |
Craig Topper | d3b5889 | 2012-01-17 09:09:48 +0000 | [diff] [blame] | 9540 | bool AllAnyExt = true; |
Nadav Rotem | b87bdac | 2012-07-15 08:38:23 +0000 | [diff] [blame] | 9541 | |
Craig Topper | d3b5889 | 2012-01-17 09:09:48 +0000 | [diff] [blame] | 9542 | for (unsigned i = 0; i != NumInScalars; ++i) { |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9543 | SDValue In = N->getOperand(i); |
9544 | // Ignore undef inputs. | ||||
9545 | if (In.getOpcode() == ISD::UNDEF) continue; | ||||
9546 | |||||
9547 | bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND; | ||||
9548 | bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND; | ||||
9549 | |||||
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 9550 | // Abort if the element is not an extension. |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9551 | if (!ZeroExt && !AnyExt) { |
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 9552 | SourceType = MVT::Other; |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9553 | break; |
9554 | } | ||||
9555 | |||||
9556 | // The input is a ZeroExt or AnyExt. Check the original type. | ||||
9557 | EVT InTy = In.getOperand(0).getValueType(); | ||||
9558 | |||||
9559 | // Check that all of the widened source types are the same. | ||||
9560 | if (SourceType == MVT::Other) | ||||
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 9561 | // First time. |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9562 | SourceType = InTy; |
9563 | else if (InTy != SourceType) { | ||||
9564 | // Multiple income types. Abort. | ||||
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 9565 | SourceType = MVT::Other; |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9566 | break; |
9567 | } | ||||
9568 | |||||
9569 | // Check if all of the extends are ANY_EXTENDs. | ||||
Craig Topper | d3b5889 | 2012-01-17 09:09:48 +0000 | [diff] [blame] | 9570 | AllAnyExt &= AnyExt; |
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9571 | } |
9572 | |||||
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 9573 | // In order to have valid types, all of the inputs must be extended from the |
9574 | // same source type and all of the inputs must be any or zero extend. | ||||
9575 | // Scalar sizes must be a power of two. | ||||
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9576 | EVT OutScalarTy = VT.getScalarType(); |
Nadav Rotem | 2ee746b | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 9577 | bool ValidTypes = SourceType != MVT::Other && |
Nadav Rotem | f47368b | 2011-10-31 20:08:25 +0000 | [diff] [blame] | 9578 | isPowerOf2_32(OutScalarTy.getSizeInBits()) && |
9579 | isPowerOf2_32(SourceType.getSizeInBits()); | ||||
9580 | |||||
Nadav Rotem | 6431ff9 | 2012-03-15 08:49:06 +0000 | [diff] [blame] | 9581 | // Create a new simpler BUILD_VECTOR sequence which other optimizations can |
9582 | // turn into a single shuffle instruction. | ||||
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9583 | if (!ValidTypes) |
9584 | return SDValue(); | ||||
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9585 | |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9586 | bool isLE = TLI.isLittleEndian(); |
9587 | unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits(); | ||||
9588 | assert(ElemRatio > 1 && "Invalid element size ratio"); | ||||
9589 | SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType): | ||||
9590 | DAG.getConstant(0, SourceType); | ||||
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9591 | |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9592 | unsigned NewBVElems = ElemRatio * VT.getVectorNumElements(); |
9593 | SmallVector<SDValue, 8> Ops(NewBVElems, Filler); | ||||
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9594 | |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9595 | // Populate the new build_vector |
Jakub Staszak | adf3891 | 2012-10-24 00:38:25 +0000 | [diff] [blame] | 9596 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9597 | SDValue Cast = N->getOperand(i); |
9598 | assert((Cast.getOpcode() == ISD::ANY_EXTEND || | ||||
9599 | Cast.getOpcode() == ISD::ZERO_EXTEND || | ||||
9600 | Cast.getOpcode() == ISD::UNDEF) && "Invalid cast opcode"); | ||||
9601 | SDValue In; | ||||
9602 | if (Cast.getOpcode() == ISD::UNDEF) | ||||
9603 | In = DAG.getUNDEF(SourceType); | ||||
9604 | else | ||||
9605 | In = Cast->getOperand(0); | ||||
9606 | unsigned Index = isLE ? (i * ElemRatio) : | ||||
9607 | (i * ElemRatio + (ElemRatio - 1)); | ||||
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9608 | |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9609 | assert(Index < Ops.size() && "Invalid index"); |
9610 | Ops[Index] = In; | ||||
Nadav Rotem | b00418a | 2011-10-29 21:23:04 +0000 | [diff] [blame] | 9611 | } |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 9612 | |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9613 | // The type of the new BUILD_VECTOR node. |
9614 | EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems); | ||||
9615 | assert(VecVT.getSizeInBits() == VT.getSizeInBits() && | ||||
9616 | "Invalid vector size"); | ||||
9617 | // Check if the new vector type is legal. | ||||
9618 | if (!isTypeLegal(VecVT)) return SDValue(); | ||||
9619 | |||||
9620 | // Make the new BUILD_VECTOR. | ||||
9621 | SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], Ops.size()); | ||||
9622 | |||||
9623 | // The new BUILD_VECTOR node has the potential to be further optimized. | ||||
9624 | AddToWorkList(BV.getNode()); | ||||
9625 | // Bitcast to the desired type. | ||||
9626 | return DAG.getNode(ISD::BITCAST, dl, VT, BV); | ||||
9627 | } | ||||
9628 | |||||
Michael Liao | 1a5cc71 | 2012-10-24 04:14:18 +0000 | [diff] [blame] | 9629 | SDValue DAGCombiner::reduceBuildVecConvertToConvertBuildVec(SDNode *N) { |
9630 | EVT VT = N->getValueType(0); | ||||
9631 | |||||
9632 | unsigned NumInScalars = N->getNumOperands(); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9633 | SDLoc dl(N); |
Michael Liao | 1a5cc71 | 2012-10-24 04:14:18 +0000 | [diff] [blame] | 9634 | |
9635 | EVT SrcVT = MVT::Other; | ||||
9636 | unsigned Opcode = ISD::DELETED_NODE; | ||||
9637 | unsigned NumDefs = 0; | ||||
9638 | |||||
9639 | for (unsigned i = 0; i != NumInScalars; ++i) { | ||||
9640 | SDValue In = N->getOperand(i); | ||||
9641 | unsigned Opc = In.getOpcode(); | ||||
9642 | |||||
9643 | if (Opc == ISD::UNDEF) | ||||
9644 | continue; | ||||
9645 | |||||
9646 | // If all scalar values are floats and converted from integers. | ||||
9647 | if (Opcode == ISD::DELETED_NODE && | ||||
9648 | (Opc == ISD::UINT_TO_FP || Opc == ISD::SINT_TO_FP)) { | ||||
9649 | Opcode = Opc; | ||||
Michael Liao | 1a5cc71 | 2012-10-24 04:14:18 +0000 | [diff] [blame] | 9650 | } |
Tom Stellard | d40758b | 2013-01-02 22:13:01 +0000 | [diff] [blame] | 9651 | |
Michael Liao | 1a5cc71 | 2012-10-24 04:14:18 +0000 | [diff] [blame] | 9652 | if (Opc != Opcode) |
9653 | return SDValue(); | ||||
9654 | |||||
9655 | EVT InVT = In.getOperand(0).getValueType(); | ||||
9656 | |||||
9657 | // If all scalar values are typed differently, bail out. It's chosen to | ||||
9658 | // simplify BUILD_VECTOR of integer types. | ||||
9659 | if (SrcVT == MVT::Other) | ||||
9660 | SrcVT = InVT; | ||||
9661 | if (SrcVT != InVT) | ||||
9662 | return SDValue(); | ||||
9663 | NumDefs++; | ||||
9664 | } | ||||
9665 | |||||
9666 | // If the vector has just one element defined, it's not worth to fold it into | ||||
9667 | // a vectorized one. | ||||
9668 | if (NumDefs < 2) | ||||
9669 | return SDValue(); | ||||
9670 | |||||
9671 | assert((Opcode == ISD::UINT_TO_FP || Opcode == ISD::SINT_TO_FP) | ||||
9672 | && "Should only handle conversion from integer to float."); | ||||
9673 | assert(SrcVT != MVT::Other && "Cannot determine source type!"); | ||||
9674 | |||||
9675 | EVT NVT = EVT::getVectorVT(*DAG.getContext(), SrcVT, NumInScalars); | ||||
Tom Stellard | d40758b | 2013-01-02 22:13:01 +0000 | [diff] [blame] | 9676 | |
9677 | if (!TLI.isOperationLegalOrCustom(Opcode, NVT)) | ||||
9678 | return SDValue(); | ||||
9679 | |||||
Michael Liao | 1a5cc71 | 2012-10-24 04:14:18 +0000 | [diff] [blame] | 9680 | SmallVector<SDValue, 8> Opnds; |
9681 | for (unsigned i = 0; i != NumInScalars; ++i) { | ||||
9682 | SDValue In = N->getOperand(i); | ||||
9683 | |||||
9684 | if (In.getOpcode() == ISD::UNDEF) | ||||
9685 | Opnds.push_back(DAG.getUNDEF(SrcVT)); | ||||
9686 | else | ||||
9687 | Opnds.push_back(In.getOperand(0)); | ||||
9688 | } | ||||
9689 | SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, | ||||
9690 | &Opnds[0], Opnds.size()); | ||||
9691 | AddToWorkList(BV.getNode()); | ||||
9692 | |||||
9693 | return DAG.getNode(Opcode, dl, VT, BV); | ||||
9694 | } | ||||
9695 | |||||
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9696 | SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) { |
9697 | unsigned NumInScalars = N->getNumOperands(); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9698 | SDLoc dl(N); |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9699 | EVT VT = N->getValueType(0); |
9700 | |||||
9701 | // A vector built entirely of undefs is undef. | ||||
9702 | if (ISD::allOperandsUndef(N)) | ||||
9703 | return DAG.getUNDEF(VT); | ||||
9704 | |||||
9705 | SDValue V = reduceBuildVecExtToExtBuildVec(N); | ||||
9706 | if (V.getNode()) | ||||
9707 | return V; | ||||
9708 | |||||
Michael Liao | 1a5cc71 | 2012-10-24 04:14:18 +0000 | [diff] [blame] | 9709 | V = reduceBuildVecConvertToConvertBuildVec(N); |
9710 | if (V.getNode()) | ||||
9711 | return V; | ||||
9712 | |||||
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9713 | // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT |
9714 | // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from | ||||
9715 | // at most two distinct vectors, turn this into a shuffle node. | ||||
Duncan Sands | 00294ca | 2012-03-19 15:35:44 +0000 | [diff] [blame] | 9716 | |
9717 | // May only combine to shuffle after legalize if shuffle is legal. | ||||
9718 | if (LegalOperations && | ||||
9719 | !TLI.isOperationLegalOrCustom(ISD::VECTOR_SHUFFLE, VT)) | ||||
9720 | return SDValue(); | ||||
9721 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9722 | SDValue VecIn1, VecIn2; |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9723 | for (unsigned i = 0; i != NumInScalars; ++i) { |
9724 | // Ignore undef inputs. | ||||
9725 | if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue; | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9726 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9727 | // If this input is something other than a EXTRACT_VECTOR_ELT with a |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9728 | // constant index, bail out. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9729 | if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT || |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9730 | !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9731 | VecIn1 = VecIn2 = SDValue(0, 0); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9732 | break; |
9733 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9734 | |
Nadav Rotem | 2ee746b | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 9735 | // We allow up to two distinct input vectors. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9736 | SDValue ExtractedFromVec = N->getOperand(i).getOperand(0); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9737 | if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2) |
9738 | continue; | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9739 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9740 | if (VecIn1.getNode() == 0) { |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9741 | VecIn1 = ExtractedFromVec; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9742 | } else if (VecIn2.getNode() == 0) { |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9743 | VecIn2 = ExtractedFromVec; |
9744 | } else { | ||||
9745 | // Too many inputs. | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9746 | VecIn1 = VecIn2 = SDValue(0, 0); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9747 | break; |
9748 | } | ||||
9749 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9750 | |
Nadav Rotem | 2ee746b | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 9751 | // If everything is good, we can make a shuffle operation. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 9752 | if (VecIn1.getNode()) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 9753 | SmallVector<int, 8> Mask; |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9754 | for (unsigned i = 0; i != NumInScalars; ++i) { |
9755 | if (N->getOperand(i).getOpcode() == ISD::UNDEF) { | ||||
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 9756 | Mask.push_back(-1); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9757 | continue; |
9758 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9759 | |
Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 9760 | // If extracting from the first vector, just use the index directly. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 9761 | SDValue Extract = N->getOperand(i); |
Mon P Wang | 93b7415 | 2009-03-17 06:33:10 +0000 | [diff] [blame] | 9762 | SDValue ExtVal = Extract.getOperand(1); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9763 | if (Extract.getOperand(0) == VecIn1) { |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 9764 | unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue(); |
9765 | if (ExtIndex > VT.getVectorNumElements()) | ||||
9766 | return SDValue(); | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 9767 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 9768 | Mask.push_back(ExtIndex); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9769 | continue; |
9770 | } | ||||
9771 | |||||
9772 | // Otherwise, use InIdx + VecSize | ||||
Mon P Wang | 93b7415 | 2009-03-17 06:33:10 +0000 | [diff] [blame] | 9773 | unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 9774 | Mask.push_back(Idx+NumInScalars); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9775 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9776 | |
Nadav Rotem | 2ee746b | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 9777 | // We can't generate a shuffle node with mismatched input and output types. |
9778 | // Attempt to transform a single input vector to the correct type. | ||||
9779 | if ((VT != VecIn1.getValueType())) { | ||||
9780 | // We don't support shuffeling between TWO values of different types. | ||||
9781 | if (VecIn2.getNode() != 0) | ||||
9782 | return SDValue(); | ||||
9783 | |||||
9784 | // We only support widening of vectors which are half the size of the | ||||
9785 | // output registers. For example XMM->YMM widening on X86 with AVX. | ||||
9786 | if (VecIn1.getValueType().getSizeInBits()*2 != VT.getSizeInBits()) | ||||
9787 | return SDValue(); | ||||
9788 | |||||
James Molloy | 8cd08bf | 2012-09-10 14:01:21 +0000 | [diff] [blame] | 9789 | // If the input vector type has a different base type to the output |
9790 | // vector type, bail out. | ||||
9791 | if (VecIn1.getValueType().getVectorElementType() != | ||||
9792 | VT.getVectorElementType()) | ||||
9793 | return SDValue(); | ||||
9794 | |||||
Stepan Dyatkovskiy | fdeb9fe | 2012-08-22 09:33:55 +0000 | [diff] [blame] | 9795 | // Widen the input vector by adding undef values. |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9796 | VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, |
Stepan Dyatkovskiy | fdeb9fe | 2012-08-22 09:33:55 +0000 | [diff] [blame] | 9797 | VecIn1, DAG.getUNDEF(VecIn1.getValueType())); |
Nadav Rotem | 2ee746b | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 9798 | } |
9799 | |||||
9800 | // If VecIn2 is unused then change it to undef. | ||||
9801 | VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT); | ||||
9802 | |||||
Nadav Rotem | 6dfabb6 | 2012-09-20 08:53:31 +0000 | [diff] [blame] | 9803 | // Check that we were able to transform all incoming values to the same |
9804 | // type. | ||||
Nadav Rotem | 0877fdf | 2012-02-13 12:42:26 +0000 | [diff] [blame] | 9805 | if (VecIn2.getValueType() != VecIn1.getValueType() || |
9806 | VecIn1.getValueType() != VT) | ||||
9807 | return SDValue(); | ||||
9808 | |||||
Nadav Rotem | 2ee746b | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 9809 | // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes. |
Nadav Rotem | 0877fdf | 2012-02-13 12:42:26 +0000 | [diff] [blame] | 9810 | if (!isTypeLegal(VT)) |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 9811 | return SDValue(); |
9812 | |||||
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9813 | // Return the new VECTOR_SHUFFLE node. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 9814 | SDValue Ops[2]; |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 9815 | Ops[0] = VecIn1; |
Nadav Rotem | 2ee746b | 2012-02-12 15:05:31 +0000 | [diff] [blame] | 9816 | Ops[1] = VecIn2; |
Michael Liao | fac14ab | 2012-10-23 23:06:52 +0000 | [diff] [blame] | 9817 | return DAG.getVectorShuffle(VT, dl, Ops[0], Ops[1], &Mask[0]); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9818 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9819 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9820 | return SDValue(); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 9821 | } |
9822 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9823 | SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9824 | // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of |
9825 | // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector | ||||
9826 | // inputs come from at most two distinct vectors, turn this into a shuffle | ||||
9827 | // node. | ||||
9828 | |||||
9829 | // If we only have one input vector, we don't need to do any concatenation. | ||||
Bill Wendling | c144a57 | 2009-01-30 23:36:47 +0000 | [diff] [blame] | 9830 | if (N->getNumOperands() == 1) |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9831 | return N->getOperand(0); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9832 | |
Nadav Rotem | b7e230d | 2012-07-14 21:30:27 +0000 | [diff] [blame] | 9833 | // Check if all of the operands are undefs. |
Nadav Rotem | b87bdac | 2012-07-15 08:38:23 +0000 | [diff] [blame] | 9834 | if (ISD::allOperandsUndef(N)) |
Nadav Rotem | b7e230d | 2012-07-14 21:30:27 +0000 | [diff] [blame] | 9835 | return DAG.getUNDEF(N->getValueType(0)); |
9836 | |||||
Nadav Rotem | b2ed5fa | 2013-05-01 19:18:51 +0000 | [diff] [blame] | 9837 | // Type legalization of vectors and DAG canonicalization of SHUFFLE_VECTOR |
9838 | // nodes often generate nop CONCAT_VECTOR nodes. | ||||
9839 | // Scan the CONCAT_VECTOR operands and look for a CONCAT operations that | ||||
9840 | // place the incoming vectors at the exact same location. | ||||
9841 | SDValue SingleSource = SDValue(); | ||||
9842 | unsigned PartNumElem = N->getOperand(0).getValueType().getVectorNumElements(); | ||||
9843 | |||||
9844 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { | ||||
9845 | SDValue Op = N->getOperand(i); | ||||
9846 | |||||
9847 | if (Op.getOpcode() == ISD::UNDEF) | ||||
9848 | continue; | ||||
9849 | |||||
9850 | // Check if this is the identity extract: | ||||
9851 | if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR) | ||||
9852 | return SDValue(); | ||||
9853 | |||||
9854 | // Find the single incoming vector for the extract_subvector. | ||||
9855 | if (SingleSource.getNode()) { | ||||
9856 | if (Op.getOperand(0) != SingleSource) | ||||
9857 | return SDValue(); | ||||
9858 | } else { | ||||
9859 | SingleSource = Op.getOperand(0); | ||||
Michael Kuperstein | 2720248 | 2013-05-06 08:06:13 +0000 | [diff] [blame] | 9860 | |
9861 | // Check the source type is the same as the type of the result. | ||||
9862 | // If not, this concat may extend the vector, so we can not | ||||
9863 | // optimize it away. | ||||
9864 | if (SingleSource.getValueType() != N->getValueType(0)) | ||||
9865 | return SDValue(); | ||||
Nadav Rotem | b2ed5fa | 2013-05-01 19:18:51 +0000 | [diff] [blame] | 9866 | } |
9867 | |||||
9868 | unsigned IdentityIndex = i * PartNumElem; | ||||
9869 | ConstantSDNode *CS = dyn_cast<ConstantSDNode>(Op.getOperand(1)); | ||||
9870 | // The extract index must be constant. | ||||
9871 | if (!CS) | ||||
9872 | return SDValue(); | ||||
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 9873 | |
Nadav Rotem | b2ed5fa | 2013-05-01 19:18:51 +0000 | [diff] [blame] | 9874 | // Check that we are reading from the identity index. |
9875 | if (CS->getZExtValue() != IdentityIndex) | ||||
9876 | return SDValue(); | ||||
9877 | } | ||||
9878 | |||||
9879 | if (SingleSource.getNode()) | ||||
9880 | return SingleSource; | ||||
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 9881 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9882 | return SDValue(); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 9883 | } |
9884 | |||||
Bruno Cardoso Lopes | e97190f | 2011-09-20 23:19:33 +0000 | [diff] [blame] | 9885 | SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode* N) { |
9886 | EVT NVT = N->getValueType(0); | ||||
9887 | SDValue V = N->getOperand(0); | ||||
9888 | |||||
Michael Liao | 13429e2 | 2012-10-17 20:48:33 +0000 | [diff] [blame] | 9889 | if (V->getOpcode() == ISD::CONCAT_VECTORS) { |
9890 | // Combine: | ||||
9891 | // (extract_subvec (concat V1, V2, ...), i) | ||||
9892 | // Into: | ||||
9893 | // Vi if possible | ||||
Michael Liao | 9aecdb5 | 2012-10-19 03:17:00 +0000 | [diff] [blame] | 9894 | // Only operand 0 is checked as 'concat' assumes all inputs of the same type. |
9895 | if (V->getOperand(0).getValueType() != NVT) | ||||
9896 | return SDValue(); | ||||
Michael Liao | 13429e2 | 2012-10-17 20:48:33 +0000 | [diff] [blame] | 9897 | unsigned Idx = dyn_cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); |
9898 | unsigned NumElems = NVT.getVectorNumElements(); | ||||
9899 | assert((Idx % NumElems) == 0 && | ||||
9900 | "IDX in concat is not a multiple of the result vector length."); | ||||
9901 | return V->getOperand(Idx / NumElems); | ||||
9902 | } | ||||
9903 | |||||
Michael Liao | b4f98ea | 2013-03-25 23:47:35 +0000 | [diff] [blame] | 9904 | // Skip bitcasting |
9905 | if (V->getOpcode() == ISD::BITCAST) | ||||
9906 | V = V.getOperand(0); | ||||
9907 | |||||
9908 | if (V->getOpcode() == ISD::INSERT_SUBVECTOR) { | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9909 | SDLoc dl(N); |
Michael Liao | b4f98ea | 2013-03-25 23:47:35 +0000 | [diff] [blame] | 9910 | // Handle only simple case where vector being inserted and vector |
9911 | // being extracted are of same type, and are half size of larger vectors. | ||||
9912 | EVT BigVT = V->getOperand(0).getValueType(); | ||||
9913 | EVT SmallVT = V->getOperand(1).getValueType(); | ||||
9914 | if (!NVT.bitsEq(SmallVT) || NVT.getSizeInBits()*2 != BigVT.getSizeInBits()) | ||||
9915 | return SDValue(); | ||||
9916 | |||||
9917 | // Only handle cases where both indexes are constants with the same type. | ||||
9918 | ConstantSDNode *ExtIdx = dyn_cast<ConstantSDNode>(N->getOperand(1)); | ||||
9919 | ConstantSDNode *InsIdx = dyn_cast<ConstantSDNode>(V->getOperand(2)); | ||||
9920 | |||||
9921 | if (InsIdx && ExtIdx && | ||||
9922 | InsIdx->getValueType(0).getSizeInBits() <= 64 && | ||||
9923 | ExtIdx->getValueType(0).getSizeInBits() <= 64) { | ||||
9924 | // Combine: | ||||
9925 | // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx) | ||||
9926 | // Into: | ||||
9927 | // indices are equal or bit offsets are equal => V1 | ||||
9928 | // otherwise => (extract_subvec V1, ExtIdx) | ||||
9929 | if (InsIdx->getZExtValue() * SmallVT.getScalarType().getSizeInBits() == | ||||
9930 | ExtIdx->getZExtValue() * NVT.getScalarType().getSizeInBits()) | ||||
9931 | return DAG.getNode(ISD::BITCAST, dl, NVT, V->getOperand(1)); | ||||
9932 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT, | ||||
9933 | DAG.getNode(ISD::BITCAST, dl, | ||||
9934 | N->getOperand(0).getValueType(), | ||||
9935 | V->getOperand(0)), N->getOperand(1)); | ||||
9936 | } | ||||
9937 | } | ||||
9938 | |||||
Bruno Cardoso Lopes | e97190f | 2011-09-20 23:19:33 +0000 | [diff] [blame] | 9939 | return SDValue(); |
9940 | } | ||||
9941 | |||||
Benjamin Kramer | 6fac1fb | 2013-04-09 17:41:43 +0000 | [diff] [blame] | 9942 | // Tries to turn a shuffle of two CONCAT_VECTORS into a single concat. |
9943 | static SDValue partitionShuffleOfConcats(SDNode *N, SelectionDAG &DAG) { | ||||
9944 | EVT VT = N->getValueType(0); | ||||
9945 | unsigned NumElts = VT.getVectorNumElements(); | ||||
9946 | |||||
9947 | SDValue N0 = N->getOperand(0); | ||||
9948 | SDValue N1 = N->getOperand(1); | ||||
9949 | ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); | ||||
9950 | |||||
9951 | SmallVector<SDValue, 4> Ops; | ||||
9952 | EVT ConcatVT = N0.getOperand(0).getValueType(); | ||||
9953 | unsigned NumElemsPerConcat = ConcatVT.getVectorNumElements(); | ||||
9954 | unsigned NumConcats = NumElts / NumElemsPerConcat; | ||||
9955 | |||||
9956 | // Look at every vector that's inserted. We're looking for exact | ||||
9957 | // subvector-sized copies from a concatenated vector | ||||
9958 | for (unsigned I = 0; I != NumConcats; ++I) { | ||||
9959 | // Make sure we're dealing with a copy. | ||||
9960 | unsigned Begin = I * NumElemsPerConcat; | ||||
Hao Liu | 3778c04 | 2013-05-13 02:07:05 +0000 | [diff] [blame] | 9961 | bool AllUndef = true, NoUndef = true; |
9962 | for (unsigned J = Begin; J != Begin + NumElemsPerConcat; ++J) { | ||||
9963 | if (SVN->getMaskElt(J) >= 0) | ||||
9964 | AllUndef = false; | ||||
9965 | else | ||||
9966 | NoUndef = false; | ||||
Benjamin Kramer | 6fac1fb | 2013-04-09 17:41:43 +0000 | [diff] [blame] | 9967 | } |
9968 | |||||
Hao Liu | 3778c04 | 2013-05-13 02:07:05 +0000 | [diff] [blame] | 9969 | if (NoUndef) { |
Hao Liu | 3778c04 | 2013-05-13 02:07:05 +0000 | [diff] [blame] | 9970 | if (SVN->getMaskElt(Begin) % NumElemsPerConcat != 0) |
9971 | return SDValue(); | ||||
9972 | |||||
9973 | for (unsigned J = 1; J != NumElemsPerConcat; ++J) | ||||
9974 | if (SVN->getMaskElt(Begin + J - 1) + 1 != SVN->getMaskElt(Begin + J)) | ||||
9975 | return SDValue(); | ||||
9976 | |||||
9977 | unsigned FirstElt = SVN->getMaskElt(Begin) / NumElemsPerConcat; | ||||
9978 | if (FirstElt < N0.getNumOperands()) | ||||
9979 | Ops.push_back(N0.getOperand(FirstElt)); | ||||
9980 | else | ||||
9981 | Ops.push_back(N1.getOperand(FirstElt - N0.getNumOperands())); | ||||
9982 | |||||
9983 | } else if (AllUndef) { | ||||
9984 | Ops.push_back(DAG.getUNDEF(N0.getOperand(0).getValueType())); | ||||
9985 | } else { // Mixed with general masks and undefs, can't do optimization. | ||||
9986 | return SDValue(); | ||||
9987 | } | ||||
Benjamin Kramer | 6fac1fb | 2013-04-09 17:41:43 +0000 | [diff] [blame] | 9988 | } |
9989 | |||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9990 | return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, Ops.data(), |
Benjamin Kramer | 6fac1fb | 2013-04-09 17:41:43 +0000 | [diff] [blame] | 9991 | Ops.size()); |
9992 | } | ||||
9993 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9994 | SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 9995 | EVT VT = N->getValueType(0); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 9996 | unsigned NumElts = VT.getVectorNumElements(); |
Chris Lattner | f1d0c62 | 2006-03-31 22:16:43 +0000 | [diff] [blame] | 9997 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 9998 | SDValue N0 = N->getOperand(0); |
Craig Topper | 481b79c | 2012-01-04 08:07:43 +0000 | [diff] [blame] | 9999 | SDValue N1 = N->getOperand(1); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 10000 | |
Craig Topper | ae1bec5 | 2012-04-09 05:16:56 +0000 | [diff] [blame] | 10001 | assert(N0.getValueType() == VT && "Vector shuffle must be normalized in DAG"); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 10002 | |
Craig Topper | 481b79c | 2012-01-04 08:07:43 +0000 | [diff] [blame] | 10003 | // Canonicalize shuffle undef, undef -> undef |
10004 | if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF) | ||||
10005 | return DAG.getUNDEF(VT); | ||||
10006 | |||||
10007 | ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); | ||||
10008 | |||||
10009 | // Canonicalize shuffle v, v -> v, undef | ||||
10010 | if (N0 == N1) { | ||||
10011 | SmallVector<int, 8> NewMask; | ||||
10012 | for (unsigned i = 0; i != NumElts; ++i) { | ||||
10013 | int Idx = SVN->getMaskElt(i); | ||||
10014 | if (Idx >= (int)NumElts) Idx -= NumElts; | ||||
10015 | NewMask.push_back(Idx); | ||||
10016 | } | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10017 | return DAG.getVectorShuffle(VT, SDLoc(N), N0, DAG.getUNDEF(VT), |
Craig Topper | 481b79c | 2012-01-04 08:07:43 +0000 | [diff] [blame] | 10018 | &NewMask[0]); |
10019 | } | ||||
10020 | |||||
10021 | // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask. | ||||
10022 | if (N0.getOpcode() == ISD::UNDEF) { | ||||
10023 | SmallVector<int, 8> NewMask; | ||||
10024 | for (unsigned i = 0; i != NumElts; ++i) { | ||||
10025 | int Idx = SVN->getMaskElt(i); | ||||
Craig Topper | 4b206bd | 2012-04-09 05:55:33 +0000 | [diff] [blame] | 10026 | if (Idx >= 0) { |
Craig Topper | 01d22aa | 2013-08-08 07:38:55 +0000 | [diff] [blame] | 10027 | if (Idx >= (int)NumElts) |
Craig Topper | 4b206bd | 2012-04-09 05:55:33 +0000 | [diff] [blame] | 10028 | Idx -= NumElts; |
Craig Topper | 01d22aa | 2013-08-08 07:38:55 +0000 | [diff] [blame] | 10029 | else |
10030 | Idx = -1; // remove reference to lhs | ||||
Craig Topper | 4b206bd | 2012-04-09 05:55:33 +0000 | [diff] [blame] | 10031 | } |
10032 | NewMask.push_back(Idx); | ||||
Craig Topper | 481b79c | 2012-01-04 08:07:43 +0000 | [diff] [blame] | 10033 | } |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10034 | return DAG.getVectorShuffle(VT, SDLoc(N), N1, DAG.getUNDEF(VT), |
Craig Topper | 481b79c | 2012-01-04 08:07:43 +0000 | [diff] [blame] | 10035 | &NewMask[0]); |
10036 | } | ||||
10037 | |||||
10038 | // Remove references to rhs if it is undef | ||||
10039 | if (N1.getOpcode() == ISD::UNDEF) { | ||||
10040 | bool Changed = false; | ||||
10041 | SmallVector<int, 8> NewMask; | ||||
10042 | for (unsigned i = 0; i != NumElts; ++i) { | ||||
10043 | int Idx = SVN->getMaskElt(i); | ||||
10044 | if (Idx >= (int)NumElts) { | ||||
10045 | Idx = -1; | ||||
10046 | Changed = true; | ||||
10047 | } | ||||
10048 | NewMask.push_back(Idx); | ||||
10049 | } | ||||
10050 | if (Changed) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10051 | return DAG.getVectorShuffle(VT, SDLoc(N), N0, N1, &NewMask[0]); |
Craig Topper | 481b79c | 2012-01-04 08:07:43 +0000 | [diff] [blame] | 10052 | } |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 10053 | |
Bob Wilson | 0f1db1a | 2010-10-28 17:06:14 +0000 | [diff] [blame] | 10054 | // If it is a splat, check if the argument vector is another splat or a |
10055 | // build_vector with all scalar elements the same. | ||||
Bob Wilson | 0f1db1a | 2010-10-28 17:06:14 +0000 | [diff] [blame] | 10056 | if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10057 | SDNode *V = N0.getNode(); |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 10058 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10059 | // If this is a bit convert that changes the element type of the vector but |
Evan Cheng | 5956922 | 2006-10-16 22:49:37 +0000 | [diff] [blame] | 10060 | // not the number of vector elements, look through it. Be careful not to |
10061 | // look though conversions that change things like v4f32 to v2f64. | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10062 | if (V->getOpcode() == ISD::BITCAST) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10063 | SDValue ConvInput = V->getOperand(0); |
Evan Cheng | 2925786 | 2008-07-22 20:42:56 +0000 | [diff] [blame] | 10064 | if (ConvInput.getValueType().isVector() && |
10065 | ConvInput.getValueType().getVectorNumElements() == NumElts) | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10066 | V = ConvInput.getNode(); |
Evan Cheng | 5956922 | 2006-10-16 22:49:37 +0000 | [diff] [blame] | 10067 | } |
10068 | |||||
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10069 | if (V->getOpcode() == ISD::BUILD_VECTOR) { |
Bob Wilson | 0f1db1a | 2010-10-28 17:06:14 +0000 | [diff] [blame] | 10070 | assert(V->getNumOperands() == NumElts && |
10071 | "BUILD_VECTOR has wrong number of operands"); | ||||
10072 | SDValue Base; | ||||
10073 | bool AllSame = true; | ||||
10074 | for (unsigned i = 0; i != NumElts; ++i) { | ||||
10075 | if (V->getOperand(i).getOpcode() != ISD::UNDEF) { | ||||
10076 | Base = V->getOperand(i); | ||||
10077 | break; | ||||
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 10078 | } |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 10079 | } |
Bob Wilson | 0f1db1a | 2010-10-28 17:06:14 +0000 | [diff] [blame] | 10080 | // Splat of <u, u, u, u>, return <u, u, u, u> |
10081 | if (!Base.getNode()) | ||||
10082 | return N0; | ||||
10083 | for (unsigned i = 0; i != NumElts; ++i) { | ||||
10084 | if (V->getOperand(i) != Base) { | ||||
10085 | AllSame = false; | ||||
10086 | break; | ||||
10087 | } | ||||
10088 | } | ||||
10089 | // Splat of <x, x, x, x>, return <x, x, x, x> | ||||
10090 | if (AllSame) | ||||
10091 | return N0; | ||||
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 10092 | } |
10093 | } | ||||
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 10094 | |
Benjamin Kramer | 6fac1fb | 2013-04-09 17:41:43 +0000 | [diff] [blame] | 10095 | if (N0.getOpcode() == ISD::CONCAT_VECTORS && |
10096 | Level < AfterLegalizeVectorOps && | ||||
10097 | (N1.getOpcode() == ISD::UNDEF || | ||||
10098 | (N1.getOpcode() == ISD::CONCAT_VECTORS && | ||||
10099 | N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()))) { | ||||
10100 | SDValue V = partitionShuffleOfConcats(N, DAG); | ||||
10101 | |||||
10102 | if (V.getNode()) | ||||
10103 | return V; | ||||
10104 | } | ||||
10105 | |||||
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 10106 | // If this shuffle node is simply a swizzle of another shuffle node, |
Nadav Rotem | d16c8d0 | 2012-04-07 21:19:08 +0000 | [diff] [blame] | 10107 | // and it reverses the swizzle of the previous shuffle then we can |
10108 | // optimize shuffle(shuffle(x, undef), undef) -> x. | ||||
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 10109 | if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG && |
10110 | N1.getOpcode() == ISD::UNDEF) { | ||||
10111 | |||||
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 10112 | ShuffleVectorSDNode *OtherSV = cast<ShuffleVectorSDNode>(N0); |
10113 | |||||
Nadav Rotem | d16c8d0 | 2012-04-07 21:19:08 +0000 | [diff] [blame] | 10114 | // Shuffle nodes can only reverse shuffles with a single non-undef value. |
10115 | if (N0.getOperand(1).getOpcode() != ISD::UNDEF) | ||||
10116 | return SDValue(); | ||||
10117 | |||||
Craig Topper | ae1bec5 | 2012-04-09 05:16:56 +0000 | [diff] [blame] | 10118 | // The incoming shuffle must be of the same type as the result of the |
10119 | // current shuffle. | ||||
10120 | assert(OtherSV->getOperand(0).getValueType() == VT && | ||||
10121 | "Shuffle types don't match"); | ||||
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 10122 | |
10123 | for (unsigned i = 0; i != NumElts; ++i) { | ||||
10124 | int Idx = SVN->getMaskElt(i); | ||||
Craig Topper | ae1bec5 | 2012-04-09 05:16:56 +0000 | [diff] [blame] | 10125 | assert(Idx < (int)NumElts && "Index references undef operand"); |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 10126 | // Next, this index comes from the first value, which is the incoming |
10127 | // shuffle. Adopt the incoming index. | ||||
10128 | if (Idx >= 0) | ||||
10129 | Idx = OtherSV->getMaskElt(Idx); | ||||
10130 | |||||
Nadav Rotem | d16c8d0 | 2012-04-07 21:19:08 +0000 | [diff] [blame] | 10131 | // The combined shuffle must map each index to itself. |
Craig Topper | ae1bec5 | 2012-04-09 05:16:56 +0000 | [diff] [blame] | 10132 | if (Idx >= 0 && (unsigned)Idx != i) |
Nadav Rotem | d16c8d0 | 2012-04-07 21:19:08 +0000 | [diff] [blame] | 10133 | return SDValue(); |
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 10134 | } |
Nadav Rotem | d16c8d0 | 2012-04-07 21:19:08 +0000 | [diff] [blame] | 10135 | |
10136 | return OtherSV->getOperand(0); | ||||
Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 10137 | } |
10138 | |||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10139 | return SDValue(); |
Chris Lattner | f1d0c62 | 2006-03-31 22:16:43 +0000 | [diff] [blame] | 10140 | } |
10141 | |||||
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10142 | /// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10143 | /// an AND to a vector_shuffle with the destination vector and a zero vector. |
10144 | /// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==> | ||||
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10145 | /// vector_shuffle V, Zero, <0, 4, 2, 4> |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10146 | SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 10147 | EVT VT = N->getValueType(0); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10148 | SDLoc dl(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10149 | SDValue LHS = N->getOperand(0); |
10150 | SDValue RHS = N->getOperand(1); | ||||
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10151 | if (N->getOpcode() == ISD::AND) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10152 | if (RHS.getOpcode() == ISD::BITCAST) |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10153 | RHS = RHS.getOperand(0); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10154 | if (RHS.getOpcode() == ISD::BUILD_VECTOR) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 10155 | SmallVector<int, 8> Indices; |
10156 | unsigned NumElts = RHS.getNumOperands(); | ||||
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10157 | for (unsigned i = 0; i != NumElts; ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10158 | SDValue Elt = RHS.getOperand(i); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10159 | if (!isa<ConstantSDNode>(Elt)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10160 | return SDValue(); |
Craig Topper | b7135e5 | 2012-04-09 05:59:53 +0000 | [diff] [blame] | 10161 | |
10162 | if (cast<ConstantSDNode>(Elt)->isAllOnesValue()) | ||||
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 10163 | Indices.push_back(i); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10164 | else if (cast<ConstantSDNode>(Elt)->isNullValue()) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 10165 | Indices.push_back(NumElts); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10166 | else |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10167 | return SDValue(); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10168 | } |
10169 | |||||
10170 | // Let's see if the target supports this vector_shuffle. | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 10171 | EVT RVT = RHS.getValueType(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 10172 | if (!TLI.isVectorClearMaskLegal(Indices, RVT)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10173 | return SDValue(); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10174 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10175 | // Return the new VECTOR_SHUFFLE node. |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 10176 | EVT EltVT = RVT.getVectorElementType(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 10177 | SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(), |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 10178 | DAG.getConstant(0, EltVT)); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10179 | SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 10180 | RVT, &ZeroOps[0], ZeroOps.size()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10181 | LHS = DAG.getNode(ISD::BITCAST, dl, RVT, LHS); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 10182 | SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10183 | return DAG.getNode(ISD::BITCAST, dl, VT, Shuf); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10184 | } |
10185 | } | ||||
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10186 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10187 | return SDValue(); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10188 | } |
10189 | |||||
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10190 | /// SimplifyVBinOp - Visit a binary vector operation, like ADD. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10191 | SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) { |
Bob Wilson | d727343 | 2010-12-17 23:06:49 +0000 | [diff] [blame] | 10192 | assert(N->getValueType(0).isVector() && |
10193 | "SimplifyVBinOp only works on vectors!"); | ||||
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10194 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10195 | SDValue LHS = N->getOperand(0); |
10196 | SDValue RHS = N->getOperand(1); | ||||
10197 | SDValue Shuffle = XformToShuffleWithZero(N); | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10198 | if (Shuffle.getNode()) return Shuffle; |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 10199 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10200 | // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 10201 | // this operation. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10202 | if (LHS.getOpcode() == ISD::BUILD_VECTOR && |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10203 | RHS.getOpcode() == ISD::BUILD_VECTOR) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10204 | SmallVector<SDValue, 8> Ops; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10205 | for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10206 | SDValue LHSOp = LHS.getOperand(i); |
10207 | SDValue RHSOp = RHS.getOperand(i); | ||||
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 10208 | // If these two elements can't be folded, bail out. |
10209 | if ((LHSOp.getOpcode() != ISD::UNDEF && | ||||
10210 | LHSOp.getOpcode() != ISD::Constant && | ||||
10211 | LHSOp.getOpcode() != ISD::ConstantFP) || | ||||
10212 | (RHSOp.getOpcode() != ISD::UNDEF && | ||||
10213 | RHSOp.getOpcode() != ISD::Constant && | ||||
10214 | RHSOp.getOpcode() != ISD::ConstantFP)) | ||||
10215 | break; | ||||
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10216 | |
Evan Cheng | 7b336a8 | 2006-05-31 06:08:35 +0000 | [diff] [blame] | 10217 | // Can't fold divide by zero. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 10218 | if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV || |
10219 | N->getOpcode() == ISD::FDIV) { | ||||
Evan Cheng | 7b336a8 | 2006-05-31 06:08:35 +0000 | [diff] [blame] | 10220 | if ((RHSOp.getOpcode() == ISD::Constant && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10221 | cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) || |
Evan Cheng | 7b336a8 | 2006-05-31 06:08:35 +0000 | [diff] [blame] | 10222 | (RHSOp.getOpcode() == ISD::ConstantFP && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10223 | cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero())) |
Evan Cheng | 7b336a8 | 2006-05-31 06:08:35 +0000 | [diff] [blame] | 10224 | break; |
10225 | } | ||||
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10226 | |
Bob Wilson | d727343 | 2010-12-17 23:06:49 +0000 | [diff] [blame] | 10227 | EVT VT = LHSOp.getValueType(); |
Bob Wilson | db2b18f | 2011-10-18 17:34:47 +0000 | [diff] [blame] | 10228 | EVT RVT = RHSOp.getValueType(); |
10229 | if (RVT != VT) { | ||||
10230 | // Integer BUILD_VECTOR operands may have types larger than the element | ||||
10231 | // size (e.g., when the element type is not legal). Prior to type | ||||
10232 | // legalization, the types may not match between the two BUILD_VECTORS. | ||||
10233 | // Truncate one of the operands to make them match. | ||||
10234 | if (RVT.getSizeInBits() > VT.getSizeInBits()) { | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10235 | RHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, RHSOp); |
Bob Wilson | db2b18f | 2011-10-18 17:34:47 +0000 | [diff] [blame] | 10236 | } else { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10237 | LHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), RVT, LHSOp); |
Bob Wilson | db2b18f | 2011-10-18 17:34:47 +0000 | [diff] [blame] | 10238 | VT = RVT; |
10239 | } | ||||
10240 | } | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10241 | SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(LHS), VT, |
Evan Cheng | a083988 | 2010-05-18 00:03:40 +0000 | [diff] [blame] | 10242 | LHSOp, RHSOp); |
10243 | if (FoldOp.getOpcode() != ISD::UNDEF && | ||||
10244 | FoldOp.getOpcode() != ISD::Constant && | ||||
10245 | FoldOp.getOpcode() != ISD::ConstantFP) | ||||
10246 | break; | ||||
10247 | Ops.push_back(FoldOp); | ||||
10248 | AddToWorkList(FoldOp.getNode()); | ||||
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 10249 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10250 | |
Bob Wilson | d727343 | 2010-12-17 23:06:49 +0000 | [diff] [blame] | 10251 | if (Ops.size() == LHS.getNumOperands()) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10252 | return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), |
Bob Wilson | d727343 | 2010-12-17 23:06:49 +0000 | [diff] [blame] | 10253 | LHS.getValueType(), &Ops[0], Ops.size()); |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 10254 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10255 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10256 | return SDValue(); |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 10257 | } |
10258 | |||||
Craig Topper | dd201ff | 2012-09-11 01:45:21 +0000 | [diff] [blame] | 10259 | /// SimplifyVUnaryOp - Visit a binary vector operation, like FABS/FNEG. |
10260 | SDValue DAGCombiner::SimplifyVUnaryOp(SDNode *N) { | ||||
Craig Topper | dd201ff | 2012-09-11 01:45:21 +0000 | [diff] [blame] | 10261 | assert(N->getValueType(0).isVector() && |
10262 | "SimplifyVUnaryOp only works on vectors!"); | ||||
10263 | |||||
10264 | SDValue N0 = N->getOperand(0); | ||||
10265 | |||||
10266 | if (N0.getOpcode() != ISD::BUILD_VECTOR) | ||||
10267 | return SDValue(); | ||||
10268 | |||||
10269 | // Operand is a BUILD_VECTOR node, see if we can constant fold it. | ||||
10270 | SmallVector<SDValue, 8> Ops; | ||||
10271 | for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) { | ||||
10272 | SDValue Op = N0.getOperand(i); | ||||
10273 | if (Op.getOpcode() != ISD::UNDEF && | ||||
10274 | Op.getOpcode() != ISD::ConstantFP) | ||||
10275 | break; | ||||
10276 | EVT EltVT = Op.getValueType(); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10277 | SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(N0), EltVT, Op); |
Craig Topper | dd201ff | 2012-09-11 01:45:21 +0000 | [diff] [blame] | 10278 | if (FoldOp.getOpcode() != ISD::UNDEF && |
10279 | FoldOp.getOpcode() != ISD::ConstantFP) | ||||
10280 | break; | ||||
10281 | Ops.push_back(FoldOp); | ||||
10282 | AddToWorkList(FoldOp.getNode()); | ||||
10283 | } | ||||
10284 | |||||
10285 | if (Ops.size() != N0.getNumOperands()) | ||||
10286 | return SDValue(); | ||||
10287 | |||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10288 | return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), |
Craig Topper | dd201ff | 2012-09-11 01:45:21 +0000 | [diff] [blame] | 10289 | N0.getValueType(), &Ops[0], Ops.size()); |
10290 | } | ||||
10291 | |||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10292 | SDValue DAGCombiner::SimplifySelect(SDLoc DL, SDValue N0, |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10293 | SDValue N1, SDValue N2){ |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10294 | assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10295 | |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10296 | SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2, |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10297 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10298 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10299 | // If we got a simplified select_cc node back from SimplifySelectCC, then |
10300 | // break it down into a new SETCC node, and a new SELECT node, and then return | ||||
10301 | // the SELECT node, since we were called with a SELECT node. | ||||
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10302 | if (SCC.getNode()) { |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10303 | // Check to see if we got a select_cc back (to turn into setcc/select). |
10304 | // Otherwise, just return whatever node we got back, like fabs. | ||||
10305 | if (SCC.getOpcode() == ISD::SELECT_CC) { | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10306 | SDValue SETCC = DAG.getNode(ISD::SETCC, SDLoc(N0), |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10307 | N0.getValueType(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10308 | SCC.getOperand(0), SCC.getOperand(1), |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10309 | SCC.getOperand(4)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10310 | AddToWorkList(SETCC.getNode()); |
Matt Arsenault | b05e477 | 2013-06-14 22:04:37 +0000 | [diff] [blame] | 10311 | return DAG.getSelect(SDLoc(SCC), SCC.getValueType(), |
10312 | SCC.getOperand(2), SCC.getOperand(3), SETCC); | ||||
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10313 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10314 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10315 | return SCC; |
10316 | } | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10317 | return SDValue(); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 10318 | } |
10319 | |||||
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 10320 | /// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS |
10321 | /// are the two values being selected between, see if we can simplify the | ||||
Chris Lattner | 729c6d1 | 2006-05-27 00:43:02 +0000 | [diff] [blame] | 10322 | /// select. Callers of this should assume that TheSelect is deleted if this |
10323 | /// returns true. As such, they should return the appropriate thing (e.g. the | ||||
10324 | /// node) back to the top-level of the DAG combiner loop to avoid it being | ||||
10325 | /// looked at. | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10326 | bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10327 | SDValue RHS) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10328 | |
Nadav Rotem | f94fdb6 | 2011-02-11 19:57:47 +0000 | [diff] [blame] | 10329 | // Cannot simplify select with vector condition |
10330 | if (TheSelect->getOperand(0).getValueType().isVector()) return false; | ||||
10331 | |||||
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 10332 | // If this is a select from two identical things, try to pull the operation |
10333 | // through the select. | ||||
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 10334 | if (LHS.getOpcode() != RHS.getOpcode() || |
10335 | !LHS.hasOneUse() || !RHS.hasOneUse()) | ||||
10336 | return false; | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10337 | |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 10338 | // If this is a load and the token chain is identical, replace the select |
10339 | // of two loads with a load through a select of the address to load from. | ||||
10340 | // This triggers in things like "select bool X, 10.0, 123.0" after the FP | ||||
10341 | // constants have been dropped into the constant pool. | ||||
10342 | if (LHS.getOpcode() == ISD::LOAD) { | ||||
10343 | LoadSDNode *LLD = cast<LoadSDNode>(LHS); | ||||
10344 | LoadSDNode *RLD = cast<LoadSDNode>(RHS); | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10345 | |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 10346 | // Token chains must be identical. |
10347 | if (LHS.getOperand(0) != RHS.getOperand(0) || | ||||
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 10348 | // Do not let this transformation reduce the number of volatile loads. |
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 10349 | LLD->isVolatile() || RLD->isVolatile() || |
10350 | // If this is an EXTLOAD, the VT's must match. | ||||
10351 | LLD->getMemoryVT() != RLD->getMemoryVT() || | ||||
Duncan Sands | dcfd3a7 | 2010-11-18 20:05:18 +0000 | [diff] [blame] | 10352 | // If this is an EXTLOAD, the kind of extension must match. |
10353 | (LLD->getExtensionType() != RLD->getExtensionType() && | ||||
10354 | // The only exception is if one of the extensions is anyext. | ||||
10355 | LLD->getExtensionType() != ISD::EXTLOAD && | ||||
10356 | RLD->getExtensionType() != ISD::EXTLOAD) || | ||||
Dan Gohman | 75832d7 | 2009-10-31 14:14:04 +0000 | [diff] [blame] | 10357 | // FIXME: this discards src value information. This is |
10358 | // over-conservative. It would be beneficial to be able to remember | ||||
Mon P Wang | fe240b1 | 2010-01-11 20:12:49 +0000 | [diff] [blame] | 10359 | // both potential memory locations. Since we are discarding |
10360 | // src value info, don't do the transformation if the memory | ||||
10361 | // locations are not in the default address space. | ||||
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 10362 | LLD->getPointerInfo().getAddrSpace() != 0 || |
Pete Cooper | b0fde6d | 2013-02-12 03:14:50 +0000 | [diff] [blame] | 10363 | RLD->getPointerInfo().getAddrSpace() != 0 || |
10364 | !TLI.isOperationLegalOrCustom(TheSelect->getOpcode(), | ||||
10365 | LLD->getBasePtr().getValueType())) | ||||
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 10366 | return false; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10367 | |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 10368 | // Check that the select condition doesn't reach either load. If so, |
10369 | // folding this will induce a cycle into the DAG. If not, this is safe to | ||||
10370 | // xform, so create a select of the addresses. | ||||
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 10371 | SDValue Addr; |
10372 | if (TheSelect->getOpcode() == ISD::SELECT) { | ||||
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 10373 | SDNode *CondNode = TheSelect->getOperand(0).getNode(); |
10374 | if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) || | ||||
10375 | (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode))) | ||||
10376 | return false; | ||||
Nadav Rotem | 1c5bf3f | 2012-10-18 18:06:48 +0000 | [diff] [blame] | 10377 | // The loads must not depend on one another. |
10378 | if (LLD->isPredecessorOf(RLD) || | ||||
10379 | RLD->isPredecessorOf(LLD)) | ||||
10380 | return false; | ||||
Matt Arsenault | b05e477 | 2013-06-14 22:04:37 +0000 | [diff] [blame] | 10381 | Addr = DAG.getSelect(SDLoc(TheSelect), |
10382 | LLD->getBasePtr().getValueType(), | ||||
10383 | TheSelect->getOperand(0), LLD->getBasePtr(), | ||||
10384 | RLD->getBasePtr()); | ||||
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 10385 | } else { // Otherwise SELECT_CC |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 10386 | SDNode *CondLHS = TheSelect->getOperand(0).getNode(); |
10387 | SDNode *CondRHS = TheSelect->getOperand(1).getNode(); | ||||
10388 | |||||
10389 | if ((LLD->hasAnyUseOfValue(1) && | ||||
10390 | (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) || | ||||
Chris Lattner | 77d9521 | 2012-03-27 16:27:21 +0000 | [diff] [blame] | 10391 | (RLD->hasAnyUseOfValue(1) && |
10392 | (RLD->isPredecessorOf(CondLHS) || RLD->isPredecessorOf(CondRHS)))) | ||||
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 10393 | return false; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10394 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10395 | Addr = DAG.getNode(ISD::SELECT_CC, SDLoc(TheSelect), |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 10396 | LLD->getBasePtr().getValueType(), |
10397 | TheSelect->getOperand(0), | ||||
10398 | TheSelect->getOperand(1), | ||||
10399 | LLD->getBasePtr(), RLD->getBasePtr(), | ||||
10400 | TheSelect->getOperand(4)); | ||||
Chris Lattner | 1806161 | 2010-09-21 15:46:59 +0000 | [diff] [blame] | 10401 | } |
10402 | |||||
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 10403 | SDValue Load; |
10404 | if (LLD->getExtensionType() == ISD::NON_EXTLOAD) { | ||||
10405 | Load = DAG.getLoad(TheSelect->getValueType(0), | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10406 | SDLoc(TheSelect), |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 10407 | // FIXME: Discards pointer info. |
10408 | LLD->getChain(), Addr, MachinePointerInfo(), | ||||
10409 | LLD->isVolatile(), LLD->isNonTemporal(), | ||||
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 10410 | LLD->isInvariant(), LLD->getAlignment()); |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 10411 | } else { |
Duncan Sands | b9064bb | 2010-11-18 21:16:28 +0000 | [diff] [blame] | 10412 | Load = DAG.getExtLoad(LLD->getExtensionType() == ISD::EXTLOAD ? |
10413 | RLD->getExtensionType() : LLD->getExtensionType(), | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10414 | SDLoc(TheSelect), |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 10415 | TheSelect->getValueType(0), |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 10416 | // FIXME: Discards pointer info. |
10417 | LLD->getChain(), Addr, MachinePointerInfo(), | ||||
10418 | LLD->getMemoryVT(), LLD->isVolatile(), | ||||
10419 | LLD->isNonTemporal(), LLD->getAlignment()); | ||||
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 10420 | } |
Chris Lattner | f165806 | 2010-09-21 15:58:55 +0000 | [diff] [blame] | 10421 | |
10422 | // Users of the select now use the result of the load. | ||||
10423 | CombineTo(TheSelect, Load); | ||||
10424 | |||||
10425 | // Users of the old loads now use the new load's chain. We know the | ||||
10426 | // old-load value is dead now. | ||||
10427 | CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1)); | ||||
10428 | CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1)); | ||||
10429 | return true; | ||||
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 10430 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10431 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 10432 | return false; |
10433 | } | ||||
10434 | |||||
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10435 | /// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3 |
10436 | /// where 'cond' is the comparison specified by CC. | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10437 | SDValue DAGCombiner::SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10438 | SDValue N2, SDValue N3, |
10439 | ISD::CondCode CC, bool NotExtCompare) { | ||||
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10440 | // (x ? y : y) -> y. |
10441 | if (N2 == N3) return N2; | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10442 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 10443 | EVT VT = N2.getValueType(); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10444 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); |
10445 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode()); | ||||
10446 | ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode()); | ||||
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10447 | |
10448 | // Determine if the condition we're dealing with is constant | ||||
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 10449 | SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()), |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 10450 | N0, N1, CC, DL, false); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10451 | if (SCC.getNode()) AddToWorkList(SCC.getNode()); |
10452 | ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode()); | ||||
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10453 | |
10454 | // fold select_cc true, x, y -> x | ||||
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 10455 | if (SCCC && !SCCC->isNullValue()) |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10456 | return N2; |
10457 | // fold select_cc false, x, y -> y | ||||
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 10458 | if (SCCC && SCCC->isNullValue()) |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10459 | return N3; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10460 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10461 | // Check to see if we can simplify the select into an fabs node |
10462 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) { | ||||
10463 | // Allow either -0.0 or 0.0 | ||||
Dale Johannesen | 87503a6 | 2007-08-25 22:10:57 +0000 | [diff] [blame] | 10464 | if (CFP->getValueAPF().isZero()) { |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10465 | // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs |
10466 | if ((CC == ISD::SETGE || CC == ISD::SETGT) && | ||||
10467 | N0 == N2 && N3.getOpcode() == ISD::FNEG && | ||||
10468 | N2 == N3.getOperand(0)) | ||||
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10469 | return DAG.getNode(ISD::FABS, DL, VT, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10470 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10471 | // select (setl[te] X, +/-0.0), fneg(X), X -> fabs |
10472 | if ((CC == ISD::SETLT || CC == ISD::SETLE) && | ||||
10473 | N0 == N3 && N2.getOpcode() == ISD::FNEG && | ||||
10474 | N2.getOperand(0) == N3) | ||||
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10475 | return DAG.getNode(ISD::FABS, DL, VT, N3); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10476 | } |
10477 | } | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10478 | |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10479 | // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)" |
10480 | // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0 | ||||
10481 | // in it. This is a win when the constant is not otherwise available because | ||||
10482 | // it replaces two constant pool loads with one. We only do this if the FP | ||||
10483 | // type is known to be legal, because if it isn't, then we are before legalize | ||||
10484 | // types an we want the other legalization to happen first (e.g. to avoid | ||||
Mon P Wang | 0b7a786 | 2009-03-14 00:25:19 +0000 | [diff] [blame] | 10485 | // messing with soft float) and if the ConstantFP is not legal, because if |
10486 | // it is legal, we may not need to store the FP constant in a constant pool. | ||||
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10487 | if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2)) |
10488 | if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) { | ||||
10489 | if (TLI.isTypeLegal(N2.getValueType()) && | ||||
Mon P Wang | 0b7a786 | 2009-03-14 00:25:19 +0000 | [diff] [blame] | 10490 | (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) != |
10491 | TargetLowering::Legal) && | ||||
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10492 | // If both constants have multiple uses, then we won't need to do an |
10493 | // extra load, they are likely around in registers for other users. | ||||
10494 | (TV->hasOneUse() || FV->hasOneUse())) { | ||||
10495 | Constant *Elts[] = { | ||||
10496 | const_cast<ConstantFP*>(FV->getConstantFPValue()), | ||||
10497 | const_cast<ConstantFP*>(TV->getConstantFPValue()) | ||||
10498 | }; | ||||
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 10499 | Type *FPTy = Elts[0]->getType(); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 10500 | const DataLayout &TD = *TLI.getDataLayout(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10501 | |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10502 | // Create a ConstantArray of the two constants. |
Jay Foad | 2670108 | 2011-06-22 09:24:39 +0000 | [diff] [blame] | 10503 | Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10504 | SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(), |
10505 | TD.getPrefTypeAlignment(FPTy)); | ||||
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 10506 | unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10507 | |
10508 | // Get the offsets to the 0 and 1 element of the array so that we can | ||||
10509 | // select between them. | ||||
10510 | SDValue Zero = DAG.getIntPtrConstant(0); | ||||
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 10511 | unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType()); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10512 | SDValue One = DAG.getIntPtrConstant(EltSize); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10513 | |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10514 | SDValue Cond = DAG.getSetCC(DL, |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 10515 | getSetCCResultType(N0.getValueType()), |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10516 | N0, N1, CC); |
Dan Gohman | 7b316c9 | 2011-09-22 23:01:29 +0000 | [diff] [blame] | 10517 | AddToWorkList(Cond.getNode()); |
Matt Arsenault | b05e477 | 2013-06-14 22:04:37 +0000 | [diff] [blame] | 10518 | SDValue CstOffset = DAG.getSelect(DL, Zero.getValueType(), |
10519 | Cond, One, Zero); | ||||
Dan Gohman | 7b316c9 | 2011-09-22 23:01:29 +0000 | [diff] [blame] | 10520 | AddToWorkList(CstOffset.getNode()); |
Tom Stellard | edd08f7 | 2013-08-26 15:06:10 +0000 | [diff] [blame] | 10521 | CPIdx = DAG.getNode(ISD::ADD, DL, CPIdx.getValueType(), CPIdx, |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10522 | CstOffset); |
Dan Gohman | 7b316c9 | 2011-09-22 23:01:29 +0000 | [diff] [blame] | 10523 | AddToWorkList(CPIdx.getNode()); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10524 | return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx, |
Chris Lattner | 85ca106 | 2010-09-21 07:32:19 +0000 | [diff] [blame] | 10525 | MachinePointerInfo::getConstantPool(), false, |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 10526 | false, false, Alignment); |
Chris Lattner | 600fec3 | 2009-03-11 05:08:08 +0000 | [diff] [blame] | 10527 | |
10528 | } | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10529 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10530 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10531 | // Check to see if we can perform the "gzip trick", transforming |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10532 | // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A) |
Chris Lattner | e3152e5 | 2006-09-20 06:41:35 +0000 | [diff] [blame] | 10533 | if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT && |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 10534 | (N1C->isNullValue() || // (a < 0) ? b : 0 |
10535 | (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0 | ||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 10536 | EVT XType = N0.getValueType(); |
10537 | EVT AType = N2.getValueType(); | ||||
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 10538 | if (XType.bitsGE(AType)) { |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 10539 | // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 10540 | // single-bit constant. |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 10541 | if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) { |
10542 | unsigned ShCtV = N2C->getAPIntValue().logBase2(); | ||||
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 10543 | ShCtV = XType.getSizeInBits()-ShCtV-1; |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 10544 | SDValue ShCt = DAG.getConstant(ShCtV, |
10545 | getShiftAmountTy(N0.getValueType())); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10546 | SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0), |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10547 | XType, N0, ShCt); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10548 | AddToWorkList(Shift.getNode()); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10549 | |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 10550 | if (XType.bitsGT(AType)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 10551 | Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10552 | AddToWorkList(Shift.getNode()); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10553 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10554 | |
10555 | return DAG.getNode(ISD::AND, DL, AType, Shift, N2); | ||||
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10556 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10557 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10558 | SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0), |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10559 | XType, N0, |
10560 | DAG.getConstant(XType.getSizeInBits()-1, | ||||
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 10561 | getShiftAmountTy(N0.getValueType()))); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10562 | AddToWorkList(Shift.getNode()); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10563 | |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 10564 | if (XType.bitsGT(AType)) { |
Bill Wendling | 9729c5a | 2009-01-31 03:12:48 +0000 | [diff] [blame] | 10565 | Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10566 | AddToWorkList(Shift.getNode()); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10567 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10568 | |
10569 | return DAG.getNode(ISD::AND, DL, AType, Shift, N2); | ||||
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10570 | } |
10571 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10572 | |
Owen Anderson | ed1088a | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 10573 | // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A) |
10574 | // where y is has a single bit set. | ||||
10575 | // A plaintext description would be, we can turn the SELECT_CC into an AND | ||||
10576 | // when the condition can be materialized as an all-ones register. Any | ||||
10577 | // single bit-test can be materialized as an all-ones register with | ||||
10578 | // shift-left and shift-right-arith. | ||||
10579 | if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND && | ||||
10580 | N0->getValueType(0) == VT && | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10581 | N1C && N1C->isNullValue() && |
Owen Anderson | ed1088a | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 10582 | N2C && N2C->isNullValue()) { |
10583 | SDValue AndLHS = N0->getOperand(0); | ||||
10584 | ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1)); | ||||
10585 | if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) { | ||||
10586 | // Shift the tested bit over the sign bit. | ||||
10587 | APInt AndMask = ConstAndRHS->getAPIntValue(); | ||||
10588 | SDValue ShlAmt = | ||||
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 10589 | DAG.getConstant(AndMask.countLeadingZeros(), |
10590 | getShiftAmountTy(AndLHS.getValueType())); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10591 | SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(N0), VT, AndLHS, ShlAmt); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10592 | |
Owen Anderson | ed1088a | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 10593 | // Now arithmetic right shift it all the way over, so the result is either |
10594 | // all-ones, or zero. | ||||
10595 | SDValue ShrAmt = | ||||
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 10596 | DAG.getConstant(AndMask.getBitWidth()-1, |
10597 | getShiftAmountTy(Shl.getValueType())); | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10598 | SDValue Shr = DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, ShrAmt); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10599 | |
Owen Anderson | ed1088a | 2010-09-22 22:58:22 +0000 | [diff] [blame] | 10600 | return DAG.getNode(ISD::AND, DL, VT, Shr, N3); |
10601 | } | ||||
10602 | } | ||||
10603 | |||||
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 10604 | // fold select C, 16, 0 -> shl C, 4 |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 10605 | if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() && |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 10606 | TLI.getBooleanContents(N0.getValueType().isVector()) == |
10607 | TargetLowering::ZeroOrOneBooleanContent) { | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10608 | |
Chris Lattner | 1eba01e | 2007-04-11 06:50:51 +0000 | [diff] [blame] | 10609 | // If the caller doesn't want us to simplify this into a zext of a compare, |
10610 | // don't do it. | ||||
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 10611 | if (NotExtCompare && N2C->getAPIntValue() == 1) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10612 | return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10613 | |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 10614 | // Get a SetCC of the condition |
Owen Anderson | efcc1ae | 2012-11-03 00:17:26 +0000 | [diff] [blame] | 10615 | // NOTE: Don't create a SETCC if it's not legal on this target. |
10616 | if (!LegalOperations || | ||||
10617 | TLI.isOperationLegal(ISD::SETCC, | ||||
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 10618 | LegalTypes ? getSetCCResultType(N0.getValueType()) : MVT::i1)) { |
Owen Anderson | efcc1ae | 2012-11-03 00:17:26 +0000 | [diff] [blame] | 10619 | SDValue Temp, SCC; |
10620 | // cast from setcc result type to select result type | ||||
10621 | if (LegalTypes) { | ||||
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 10622 | SCC = DAG.getSetCC(DL, getSetCCResultType(N0.getValueType()), |
Owen Anderson | efcc1ae | 2012-11-03 00:17:26 +0000 | [diff] [blame] | 10623 | N0, N1, CC); |
10624 | if (N2.getValueType().bitsLT(SCC.getValueType())) | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10625 | Temp = DAG.getZeroExtendInReg(SCC, SDLoc(N2), |
Owen Anderson | efcc1ae | 2012-11-03 00:17:26 +0000 | [diff] [blame] | 10626 | N2.getValueType()); |
10627 | else | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10628 | Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2), |
Owen Anderson | efcc1ae | 2012-11-03 00:17:26 +0000 | [diff] [blame] | 10629 | N2.getValueType(), SCC); |
10630 | } else { | ||||
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10631 | SCC = DAG.getSetCC(SDLoc(N0), MVT::i1, N0, N1, CC); |
10632 | Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2), | ||||
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10633 | N2.getValueType(), SCC); |
Owen Anderson | efcc1ae | 2012-11-03 00:17:26 +0000 | [diff] [blame] | 10634 | } |
10635 | |||||
10636 | AddToWorkList(SCC.getNode()); | ||||
10637 | AddToWorkList(Temp.getNode()); | ||||
10638 | |||||
10639 | if (N2C->getAPIntValue() == 1) | ||||
10640 | return Temp; | ||||
10641 | |||||
10642 | // shl setcc result by log2 n2c | ||||
10643 | return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp, | ||||
10644 | DAG.getConstant(N2C->getAPIntValue().logBase2(), | ||||
10645 | getShiftAmountTy(Temp.getValueType()))); | ||||
Nate Begeman | b0d04a7 | 2006-02-18 02:40:58 +0000 | [diff] [blame] | 10646 | } |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 10647 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10648 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10649 | // Check to see if this is the equivalent of setcc |
10650 | // FIXME: Turn all of these into setcc if setcc if setcc is legal | ||||
10651 | // otherwise, go ahead with the folds. | ||||
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 10652 | if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 10653 | EVT XType = N0.getValueType(); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 10654 | if (!LegalOperations || |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 10655 | TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(XType))) { |
10656 | SDValue Res = DAG.getSetCC(DL, getSetCCResultType(XType), N0, N1, CC); | ||||
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10657 | if (Res.getValueType() != VT) |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10658 | Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10659 | return Res; |
10660 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10661 | |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10662 | // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X)))) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10663 | if (N1C && N1C->isNullValue() && CC == ISD::SETEQ && |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 10664 | (!LegalOperations || |
Duncan Sands | 184a876 | 2008-06-14 17:48:34 +0000 | [diff] [blame] | 10665 | TLI.isOperationLegal(ISD::CTLZ, XType))) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10666 | SDValue Ctlz = DAG.getNode(ISD::CTLZ, SDLoc(N0), XType, N0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10667 | return DAG.getNode(ISD::SRL, DL, XType, Ctlz, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 10668 | DAG.getConstant(Log2_32(XType.getSizeInBits()), |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 10669 | getShiftAmountTy(Ctlz.getValueType()))); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10670 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10671 | // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1)) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10672 | if (N1C && N1C->isNullValue() && CC == ISD::SETGT) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10673 | SDValue NegN0 = DAG.getNode(ISD::SUB, SDLoc(N0), |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10674 | XType, DAG.getConstant(0, XType), N0); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10675 | SDValue NotN0 = DAG.getNOT(SDLoc(N0), N0, XType); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10676 | return DAG.getNode(ISD::SRL, DL, XType, |
Bill Wendling | fc4b677 | 2009-02-01 11:19:36 +0000 | [diff] [blame] | 10677 | DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0), |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 10678 | DAG.getConstant(XType.getSizeInBits()-1, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 10679 | getShiftAmountTy(XType))); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10680 | } |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10681 | // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1)) |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10682 | if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10683 | SDValue Sign = DAG.getNode(ISD::SRL, SDLoc(N0), XType, N0, |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10684 | DAG.getConstant(XType.getSizeInBits()-1, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 10685 | getShiftAmountTy(N0.getValueType()))); |
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10686 | return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType)); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10687 | } |
10688 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10689 | |
Benjamin Kramer | cde5110 | 2010-07-08 12:09:56 +0000 | [diff] [blame] | 10690 | // Check to see if this is an integer abs. |
10691 | // select_cc setg[te] X, 0, X, -X -> | ||||
10692 | // select_cc setgt X, -1, X, -X -> | ||||
10693 | // select_cc setl[te] X, 0, -X, X -> | ||||
10694 | // select_cc setlt X, 1, -X, X -> | ||||
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10695 | // Y = sra (X, size(X)-1); xor (add (X, Y), Y) |
Benjamin Kramer | cde5110 | 2010-07-08 12:09:56 +0000 | [diff] [blame] | 10696 | if (N1C) { |
10697 | ConstantSDNode *SubC = NULL; | ||||
10698 | if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) || | ||||
10699 | (N1C->isAllOnesValue() && CC == ISD::SETGT)) && | ||||
10700 | N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) | ||||
10701 | SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0)); | ||||
10702 | else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) || | ||||
10703 | (N1C->isOne() && CC == ISD::SETLT)) && | ||||
10704 | N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) | ||||
10705 | SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0)); | ||||
10706 | |||||
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 10707 | EVT XType = N0.getValueType(); |
Benjamin Kramer | cde5110 | 2010-07-08 12:09:56 +0000 | [diff] [blame] | 10708 | if (SubC && SubC->isNullValue() && XType.isInteger()) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10709 | SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0), XType, |
Benjamin Kramer | cde5110 | 2010-07-08 12:09:56 +0000 | [diff] [blame] | 10710 | N0, |
10711 | DAG.getConstant(XType.getSizeInBits()-1, | ||||
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 10712 | getShiftAmountTy(N0.getValueType()))); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10713 | SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N0), |
Benjamin Kramer | cde5110 | 2010-07-08 12:09:56 +0000 | [diff] [blame] | 10714 | XType, N0, Shift); |
10715 | AddToWorkList(Shift.getNode()); | ||||
10716 | AddToWorkList(Add.getNode()); | ||||
10717 | return DAG.getNode(ISD::XOR, DL, XType, Add, Shift); | ||||
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 10718 | } |
10719 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10720 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10721 | return SDValue(); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 10722 | } |
10723 | |||||
Evan Cheng | fa1eb27 | 2007-02-08 22:13:59 +0000 | [diff] [blame] | 10724 | /// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 10725 | SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10726 | SDValue N1, ISD::CondCode Cond, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10727 | SDLoc DL, bool foldBooleans) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10728 | TargetLowering::DAGCombinerInfo |
Nadav Rotem | 444b4bf | 2012-12-27 06:47:41 +0000 | [diff] [blame] | 10729 | DagCombineInfo(DAG, Level, false, this); |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 10730 | return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 10731 | } |
10732 | |||||
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 10733 | /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant, |
10734 | /// return a DAG expression to select that will generate the same value by | ||||
10735 | /// multiplying by a magic number. See: | ||||
10736 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10737 | SDValue DAGCombiner::BuildSDIV(SDNode *N) { |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 10738 | std::vector<SDNode*> Built; |
Richard Osborne | 19a4daf | 2011-11-07 17:09:05 +0000 | [diff] [blame] | 10739 | SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, &Built); |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 10740 | |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 10741 | for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end(); |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 10742 | ii != ee; ++ii) |
10743 | AddToWorkList(*ii); | ||||
10744 | return S; | ||||
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 10745 | } |
10746 | |||||
10747 | /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant, | ||||
10748 | /// return a DAG expression to select that will generate the same value by | ||||
10749 | /// multiplying by a magic number. See: | ||||
10750 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10751 | SDValue DAGCombiner::BuildUDIV(SDNode *N) { |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 10752 | std::vector<SDNode*> Built; |
Richard Osborne | 19a4daf | 2011-11-07 17:09:05 +0000 | [diff] [blame] | 10753 | SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, &Built); |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 10754 | |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 10755 | for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end(); |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 10756 | ii != ee; ++ii) |
10757 | AddToWorkList(*ii); | ||||
10758 | return S; | ||||
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 10759 | } |
10760 | |||||
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 10761 | /// FindBaseOffset - Return true if base is a frame index, which is known not |
Eric Christopher | 503a64d | 2010-12-09 04:48:06 +0000 | [diff] [blame] | 10762 | // to alias with anything but itself. Provides base object and offset as |
10763 | // results. | ||||
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 10764 | static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset, |
Roman Divacky | 2943e37 | 2012-09-05 22:15:49 +0000 | [diff] [blame] | 10765 | const GlobalValue *&GV, const void *&CV) { |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 10766 | // Assume it is a primitive operation. |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 10767 | Base = Ptr; Offset = 0; GV = 0; CV = 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10768 | |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 10769 | // If it's an adding a simple constant then integrate the offset. |
10770 | if (Base.getOpcode() == ISD::ADD) { | ||||
10771 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) { | ||||
10772 | Base = Base.getOperand(0); | ||||
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 10773 | Offset += C->getZExtValue(); |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 10774 | } |
10775 | } | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10776 | |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 10777 | // Return the underlying GlobalValue, and update the Offset. Return false |
10778 | // for GlobalAddressSDNode since the same GlobalAddress may be represented | ||||
10779 | // by multiple nodes with different offsets. | ||||
10780 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) { | ||||
10781 | GV = G->getGlobal(); | ||||
10782 | Offset += G->getOffset(); | ||||
10783 | return false; | ||||
10784 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10785 | |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 10786 | // Return the underlying Constant value, and update the Offset. Return false |
10787 | // for ConstantSDNodes since the same constant pool entry may be represented | ||||
10788 | // by multiple nodes with different offsets. | ||||
10789 | if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) { | ||||
Roman Divacky | 2943e37 | 2012-09-05 22:15:49 +0000 | [diff] [blame] | 10790 | CV = C->isMachineConstantPoolEntry() ? (const void *)C->getMachineCPVal() |
10791 | : (const void *)C->getConstVal(); | ||||
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 10792 | Offset += C->getOffset(); |
10793 | return false; | ||||
10794 | } | ||||
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 10795 | // If it's any of the following then it can't alias with anything but itself. |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 10796 | return isa<FrameIndexSDNode>(Base); |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 10797 | } |
10798 | |||||
10799 | /// isAlias - Return true if there is any possibility that the two addresses | ||||
10800 | /// overlap. | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10801 | bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1, |
Jim Laskey | 096c22e | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 10802 | const Value *SrcValue1, int SrcValueOffset1, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10803 | unsigned SrcValueAlign1, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 10804 | const MDNode *TBAAInfo1, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10805 | SDValue Ptr2, int64_t Size2, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10806 | const Value *SrcValue2, int SrcValueOffset2, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 10807 | unsigned SrcValueAlign2, |
10808 | const MDNode *TBAAInfo2) const { | ||||
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 10809 | // If they are the same then they must be aliases. |
10810 | if (Ptr1 == Ptr2) return true; | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10811 | |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 10812 | // Gather base node and offset information. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10813 | SDValue Base1, Base2; |
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 10814 | int64_t Offset1, Offset2; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 10815 | const GlobalValue *GV1, *GV2; |
Roman Divacky | 2943e37 | 2012-09-05 22:15:49 +0000 | [diff] [blame] | 10816 | const void *CV1, *CV2; |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 10817 | bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1); |
10818 | bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10819 | |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 10820 | // If they have a same base address then check to see if they overlap. |
10821 | if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2))) | ||||
Bill Wendling | 836ca7d | 2009-01-30 23:59:18 +0000 | [diff] [blame] | 10822 | return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10823 | |
Owen Anderson | 4a9f150 | 2010-09-20 20:39:59 +0000 | [diff] [blame] | 10824 | // It is possible for different frame indices to alias each other, mostly |
10825 | // when tail call optimization reuses return address slots for arguments. | ||||
10826 | // To catch this case, look up the actual index of frame indices to compute | ||||
10827 | // the real alias relationship. | ||||
10828 | if (isFrameIndex1 && isFrameIndex2) { | ||||
10829 | MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); | ||||
10830 | Offset1 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex()); | ||||
10831 | Offset2 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex()); | ||||
10832 | return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1); | ||||
10833 | } | ||||
10834 | |||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10835 | // Otherwise, if we know what the bases are, and they aren't identical, then |
Owen Anderson | 4a9f150 | 2010-09-20 20:39:59 +0000 | [diff] [blame] | 10836 | // we know they cannot alias. |
Nate Begeman | cc66cdd | 2009-09-25 06:05:26 +0000 | [diff] [blame] | 10837 | if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2)) |
10838 | return false; | ||||
Jim Laskey | 096c22e | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 10839 | |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10840 | // If we know required SrcValue1 and SrcValue2 have relatively large alignment |
10841 | // compared to the size and offset of the access, we may be able to prove they | ||||
10842 | // do not alias. This check is conservative for now to catch cases created by | ||||
10843 | // splitting vector types. | ||||
10844 | if ((SrcValueAlign1 == SrcValueAlign2) && | ||||
10845 | (SrcValueOffset1 != SrcValueOffset2) && | ||||
10846 | (Size1 == Size2) && (SrcValueAlign1 > Size1)) { | ||||
10847 | int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1; | ||||
10848 | int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1; | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10849 | |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10850 | // There is no overlap between these relatively aligned accesses of similar |
10851 | // size, return no alias. | ||||
10852 | if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1) | ||||
10853 | return false; | ||||
10854 | } | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10855 | |
Hal Finkel | 253acef | 2013-08-29 03:29:55 +0000 | [diff] [blame] | 10856 | bool UseAA = CombinerGlobalAA.getNumOccurrences() > 0 ? CombinerGlobalAA : |
10857 | TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA(); | ||||
Hal Finkel | 77364b7 | 2013-09-15 02:19:49 +0000 | [diff] [blame] | 10858 | if (UseAA && SrcValue1 && SrcValue2) { |
Jim Laskey | 07a2709 | 2006-10-18 19:08:31 +0000 | [diff] [blame] | 10859 | // Use alias analysis information. |
Dan Gohman | e9c8fa0 | 2007-08-27 16:32:11 +0000 | [diff] [blame] | 10860 | int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2); |
10861 | int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset; | ||||
10862 | int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset; | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10863 | AliasAnalysis::AliasResult AAResult = |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 10864 | AA.alias(AliasAnalysis::Location(SrcValue1, Overlap1, TBAAInfo1), |
10865 | AliasAnalysis::Location(SrcValue2, Overlap2, TBAAInfo2)); | ||||
Jim Laskey | 07a2709 | 2006-10-18 19:08:31 +0000 | [diff] [blame] | 10866 | if (AAResult == AliasAnalysis::NoAlias) |
10867 | return false; | ||||
10868 | } | ||||
Jim Laskey | 096c22e | 2006-10-18 12:29:57 +0000 | [diff] [blame] | 10869 | |
10870 | // Otherwise we have to assume they alias. | ||||
10871 | return true; | ||||
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 10872 | } |
10873 | |||||
Nadav Rotem | 90e11dc | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 10874 | bool DAGCombiner::isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1) { |
10875 | SDValue Ptr0, Ptr1; | ||||
10876 | int64_t Size0, Size1; | ||||
10877 | const Value *SrcValue0, *SrcValue1; | ||||
10878 | int SrcValueOffset0, SrcValueOffset1; | ||||
10879 | unsigned SrcValueAlign0, SrcValueAlign1; | ||||
10880 | const MDNode *SrcTBAAInfo0, *SrcTBAAInfo1; | ||||
10881 | FindAliasInfo(Op0, Ptr0, Size0, SrcValue0, SrcValueOffset0, | ||||
10882 | SrcValueAlign0, SrcTBAAInfo0); | ||||
10883 | FindAliasInfo(Op1, Ptr1, Size1, SrcValue1, SrcValueOffset1, | ||||
10884 | SrcValueAlign1, SrcTBAAInfo1); | ||||
10885 | return isAlias(Ptr0, Size0, SrcValue0, SrcValueOffset0, | ||||
Nadav Rotem | dde785c | 2012-12-06 17:34:13 +0000 | [diff] [blame] | 10886 | SrcValueAlign0, SrcTBAAInfo0, |
10887 | Ptr1, Size1, SrcValue1, SrcValueOffset1, | ||||
10888 | SrcValueAlign1, SrcTBAAInfo1); | ||||
Nadav Rotem | 90e11dc | 2012-11-29 00:00:08 +0000 | [diff] [blame] | 10889 | } |
10890 | |||||
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 10891 | /// FindAliasInfo - Extracts the relevant alias information from the memory |
10892 | /// node. Returns true if the operand was a load. | ||||
Jim Laskey | 7ca56af | 2006-10-11 13:47:09 +0000 | [diff] [blame] | 10893 | bool DAGCombiner::FindAliasInfo(SDNode *N, |
Benjamin Kramer | ae4746b | 2012-01-15 11:50:43 +0000 | [diff] [blame] | 10894 | SDValue &Ptr, int64_t &Size, |
10895 | const Value *&SrcValue, | ||||
10896 | int &SrcValueOffset, | ||||
10897 | unsigned &SrcValueAlign, | ||||
10898 | const MDNode *&TBAAInfo) const { | ||||
10899 | LSBaseSDNode *LS = cast<LSBaseSDNode>(N); | ||||
10900 | |||||
10901 | Ptr = LS->getBasePtr(); | ||||
10902 | Size = LS->getMemoryVT().getSizeInBits() >> 3; | ||||
10903 | SrcValue = LS->getSrcValue(); | ||||
10904 | SrcValueOffset = LS->getSrcValueOffset(); | ||||
10905 | SrcValueAlign = LS->getOriginalAlignment(); | ||||
10906 | TBAAInfo = LS->getTBAAInfo(); | ||||
10907 | return isa<LoadSDNode>(LS); | ||||
Jim Laskey | 7138234 | 2006-10-07 23:37:56 +0000 | [diff] [blame] | 10908 | } |
10909 | |||||
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 10910 | /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes, |
10911 | /// looking for aliasing nodes and adding them to the Aliases vector. | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10912 | void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain, |
Craig Topper | a0ec3f9 | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 10913 | SmallVectorImpl<SDValue> &Aliases) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10914 | SmallVector<SDValue, 8> Chains; // List of chains to visit. |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10915 | SmallPtrSet<SDNode *, 16> Visited; // Visited node set. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10916 | |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 10917 | // Get alias information for node. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10918 | SDValue Ptr; |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10919 | int64_t Size; |
10920 | const Value *SrcValue; | ||||
10921 | int SrcValueOffset; | ||||
10922 | unsigned SrcValueAlign; | ||||
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 10923 | const MDNode *SrcTBAAInfo; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10924 | bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 10925 | SrcValueAlign, SrcTBAAInfo); |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 10926 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 10927 | // Starting off. |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10928 | Chains.push_back(OriginalChain); |
Nate Begeman | 677c89d | 2009-10-12 05:53:58 +0000 | [diff] [blame] | 10929 | unsigned Depth = 0; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10930 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10931 | // Look at each chain and determine if it is an alias. If so, add it to the |
10932 | // aliases list. If not, then continue up the chain looking for the next | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10933 | // candidate. |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10934 | while (!Chains.empty()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10935 | SDValue Chain = Chains.back(); |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10936 | Chains.pop_back(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10937 | |
10938 | // For TokenFactor nodes, look at each operand and only continue up the | ||||
10939 | // chain until we find two aliases. If we've seen two aliases, assume we'll | ||||
Nate Begeman | 677c89d | 2009-10-12 05:53:58 +0000 | [diff] [blame] | 10940 | // find more and revert to original chain since the xform is unlikely to be |
10941 | // profitable. | ||||
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 10942 | // |
10943 | // FIXME: The depth check could be made to return the last non-aliasing | ||||
Nate Begeman | 677c89d | 2009-10-12 05:53:58 +0000 | [diff] [blame] | 10944 | // chain we found before we hit a tokenfactor rather than the original |
10945 | // chain. | ||||
10946 | if (Depth > 6 || Aliases.size() == 2) { | ||||
10947 | Aliases.clear(); | ||||
10948 | Aliases.push_back(OriginalChain); | ||||
10949 | break; | ||||
10950 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10951 | |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10952 | // Don't bother if we've been before. |
10953 | if (!Visited.insert(Chain.getNode())) | ||||
10954 | continue; | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10955 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10956 | switch (Chain.getOpcode()) { |
10957 | case ISD::EntryToken: | ||||
10958 | // Entry token is ideal chain operand, but handled in FindBetterChain. | ||||
10959 | break; | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10960 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10961 | case ISD::LOAD: |
10962 | case ISD::STORE: { | ||||
10963 | // Get alias information for Chain. | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10964 | SDValue OpPtr; |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10965 | int64_t OpSize; |
10966 | const Value *OpSrcValue; | ||||
10967 | int OpSrcValueOffset; | ||||
10968 | unsigned OpSrcValueAlign; | ||||
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 10969 | const MDNode *OpSrcTBAAInfo; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 10970 | bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10971 | OpSrcValue, OpSrcValueOffset, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 10972 | OpSrcValueAlign, |
10973 | OpSrcTBAAInfo); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10974 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10975 | // If chain is alias then stop here. |
10976 | if (!(IsLoad && IsOpLoad) && | ||||
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10977 | isAlias(Ptr, Size, SrcValue, SrcValueOffset, SrcValueAlign, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 10978 | SrcTBAAInfo, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10979 | OpPtr, OpSize, OpSrcValue, OpSrcValueOffset, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 10980 | OpSrcValueAlign, OpSrcTBAAInfo)) { |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10981 | Aliases.push_back(Chain); |
10982 | } else { | ||||
10983 | // Look further up the chain. | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10984 | Chains.push_back(Chain.getOperand(0)); |
Nate Begeman | 677c89d | 2009-10-12 05:53:58 +0000 | [diff] [blame] | 10985 | ++Depth; |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 10986 | } |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10987 | break; |
10988 | } | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 10989 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10990 | case ISD::TokenFactor: |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 10991 | // We have to check each of the operands of the token factor for "small" |
10992 | // token factors, so we queue them up. Adding the operands to the queue | ||||
10993 | // (stack) in reverse order maintains the original order and increases the | ||||
10994 | // likelihood that getNode will find a matching token factor (CSE.) | ||||
10995 | if (Chain.getNumOperands() > 16) { | ||||
10996 | Aliases.push_back(Chain); | ||||
10997 | break; | ||||
10998 | } | ||||
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 10999 | for (unsigned n = Chain.getNumOperands(); n;) |
11000 | Chains.push_back(Chain.getOperand(--n)); | ||||
Nate Begeman | 677c89d | 2009-10-12 05:53:58 +0000 | [diff] [blame] | 11001 | ++Depth; |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 11002 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11003 | |
Jim Laskey | bc588b8 | 2006-10-05 15:07:25 +0000 | [diff] [blame] | 11004 | default: |
11005 | // For all other instructions we will just have to take what we can get. | ||||
11006 | Aliases.push_back(Chain); | ||||
11007 | break; | ||||
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 11008 | } |
11009 | } | ||||
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 11010 | } |
11011 | |||||
11012 | /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking | ||||
11013 | /// for a better chain (aliasing node.) | ||||
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11014 | SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) { |
11015 | SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor. | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11016 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 11017 | // Accumulate all the aliases to this node. |
11018 | GatherAllAliases(N, OldChain, Aliases); | ||||
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11019 | |
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 11020 | // If no operands then chain to entry token. |
11021 | if (Aliases.size() == 0) | ||||
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 11022 | return DAG.getEntryNode(); |
Dan Gohman | 71dc7c9 | 2011-05-17 22:20:36 +0000 | [diff] [blame] | 11023 | |
11024 | // If a single operand then chain to it. We don't need to revisit it. | ||||
11025 | if (Aliases.size() == 1) | ||||
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 11026 | return Aliases[0]; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 11027 | |
Jim Laskey | 6ff23e5 | 2006-10-04 16:53:27 +0000 | [diff] [blame] | 11028 | // Construct a custom tailored token factor. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11029 | return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other, |
Nate Begeman | b6aef5c | 2009-09-15 00:18:30 +0000 | [diff] [blame] | 11030 | &Aliases[0], Aliases.size()); |
Jim Laskey | 279f053 | 2006-09-25 16:29:54 +0000 | [diff] [blame] | 11031 | } |
11032 | |||||
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 11033 | // SelectionDAG::Combine - This is the entry point for the file. |
11034 | // | ||||
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 11035 | void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA, |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 11036 | CodeGenOpt::Level OptLevel) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 11037 | /// run - This is the main entry point to this class. |
11038 | /// | ||||
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 11039 | DAGCombiner(*this, AA, OptLevel).Run(Level); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 11040 | } |