blob: 32a81a044d88a7ebd0087e812fc19b69361e105a [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
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.
12//
13// FIXME: Missing folds
14// sdiv, udiv, srem, urem (X, const) where X is an integer can be expanded into
15// a sequence of multiplies, shifts, and adds. This should be controlled by
16// some kind of hint from the target that int div is expensive.
17// various folds of mulh[s,u] by constants such as -1, powers of 2, etc.
18//
Nate Begeman44728a72005-09-19 22:34:01 +000019// FIXME: select C, pow2, pow2 -> something smart
20// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
Nate Begeman44728a72005-09-19 22:34:01 +000021// FIXME: Dead stores -> nuke
Chris Lattner40c62d52005-10-18 06:04:22 +000022// FIXME: shr X, (and Y,31) -> shr X, Y (TRICKY!)
Nate Begeman1d4d4142005-09-01 00:19:25 +000023// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000024// FIXME: undef values
Nate Begeman646d7e22005-09-02 21:18:40 +000025// FIXME: divide by zero is currently left unfolded. do we want to turn this
26// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000027// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Nate Begeman1d4d4142005-09-01 00:19:25 +000028//
29//===----------------------------------------------------------------------===//
30
31#define DEBUG_TYPE "dagcombine"
32#include "llvm/ADT/Statistic.h"
33#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000034#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000035#include "llvm/Support/MathExtras.h"
36#include "llvm/Target/TargetLowering.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000037#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000038#include "llvm/Support/CommandLine.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000039#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000040#include <cmath>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000041#include <iostream>
Jim Laskey279f0532006-09-25 16:29:54 +000042#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000043using namespace llvm;
44
45namespace {
Andrew Lenharthae6153f2006-07-20 17:43:27 +000046 static Statistic<> NodesCombined ("dagcombiner",
47 "Number of dag nodes combined");
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000048
Nate Begeman1d4d4142005-09-01 00:19:25 +000049
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000050static cl::opt<bool>
51 CombinerAA("combiner-alias-analysis", cl::Hidden,
52 cl::desc("Turn on alias analysis turning testing"));
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000053
54class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000055 SelectionDAG &DAG;
56 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000057 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000058
59 // Worklist of all of the nodes that need to be simplified.
60 std::vector<SDNode*> WorkList;
61
62 /// AddUsersToWorkList - When an instruction is simplified, add all users of
63 /// the instruction to the work lists because they might get more simplified
64 /// now.
65 ///
66 void AddUsersToWorkList(SDNode *N) {
67 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000068 UI != UE; ++UI)
69 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000070 }
71
72 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000073 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000074 void removeFromWorkList(SDNode *N) {
75 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
76 WorkList.end());
77 }
78
Chris Lattner24664722006-03-01 04:53:38 +000079 public:
Chris Lattner5750df92006-03-01 04:03:14 +000080 void AddToWorkList(SDNode *N) {
81 WorkList.push_back(N);
82 }
83
Chris Lattner3577e382006-08-11 17:56:38 +000084 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo) {
85 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
Chris Lattner87514ca2005-10-10 22:31:19 +000086 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000087 DEBUG(std::cerr << "\nReplacing "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +000088 std::cerr << "\nWith: "; To[0].Val->dump(&DAG);
Chris Lattner3577e382006-08-11 17:56:38 +000089 std::cerr << " and " << NumTo-1 << " other values\n");
Chris Lattner01a22022005-10-10 22:04:48 +000090 std::vector<SDNode*> NowDead;
Chris Lattner3577e382006-08-11 17:56:38 +000091 DAG.ReplaceAllUsesWith(N, To, &NowDead);
Chris Lattner01a22022005-10-10 22:04:48 +000092
93 // Push the new nodes and any users onto the worklist
Chris Lattner3577e382006-08-11 17:56:38 +000094 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattner01a22022005-10-10 22:04:48 +000095 WorkList.push_back(To[i].Val);
96 AddUsersToWorkList(To[i].Val);
97 }
98
99 // Nodes can end up on the worklist more than once. Make sure we do
100 // not process a node that has been replaced.
101 removeFromWorkList(N);
102 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
103 removeFromWorkList(NowDead[i]);
104
105 // Finally, since the node is now dead, remove it from the graph.
106 DAG.DeleteNode(N);
107 return SDOperand(N, 0);
108 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000109
Chris Lattner24664722006-03-01 04:53:38 +0000110 SDOperand CombineTo(SDNode *N, SDOperand Res) {
Chris Lattner3577e382006-08-11 17:56:38 +0000111 return CombineTo(N, &Res, 1);
Chris Lattner24664722006-03-01 04:53:38 +0000112 }
113
114 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
Chris Lattner3577e382006-08-11 17:56:38 +0000115 SDOperand To[] = { Res0, Res1 };
116 return CombineTo(N, To, 2);
Chris Lattner24664722006-03-01 04:53:38 +0000117 }
118 private:
119
Chris Lattner012f2412006-02-17 21:58:01 +0000120 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000121 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000122 /// propagation. If so, return true.
123 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000124 TargetLowering::TargetLoweringOpt TLO(DAG);
125 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000126 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
127 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
128 return false;
129
130 // Revisit the node.
131 WorkList.push_back(Op.Val);
132
133 // Replace the old value with the new one.
134 ++NodesCombined;
135 DEBUG(std::cerr << "\nReplacing "; TLO.Old.Val->dump();
Jim Laskey279f0532006-09-25 16:29:54 +0000136 std::cerr << "\nWith: "; TLO.New.Val->dump(&DAG);
137 std::cerr << '\n');
Chris Lattner012f2412006-02-17 21:58:01 +0000138
139 std::vector<SDNode*> NowDead;
140 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
141
Chris Lattner7d20d392006-02-20 06:51:04 +0000142 // Push the new node and any (possibly new) users onto the worklist.
Chris Lattner012f2412006-02-17 21:58:01 +0000143 WorkList.push_back(TLO.New.Val);
144 AddUsersToWorkList(TLO.New.Val);
145
146 // Nodes can end up on the worklist more than once. Make sure we do
147 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000148 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
149 removeFromWorkList(NowDead[i]);
150
Chris Lattner7d20d392006-02-20 06:51:04 +0000151 // Finally, if the node is now dead, remove it from the graph. The node
152 // may not be dead if the replacement process recursively simplified to
153 // something else needing this node.
154 if (TLO.Old.Val->use_empty()) {
155 removeFromWorkList(TLO.Old.Val);
156 DAG.DeleteNode(TLO.Old.Val);
157 }
Chris Lattner012f2412006-02-17 21:58:01 +0000158 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000159 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000160
Nate Begeman1d4d4142005-09-01 00:19:25 +0000161 /// visit - call the node-specific routine that knows how to fold each
162 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000163 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000164
165 // Visitation implementation - Implement dag node combining for different
166 // node types. The semantics are as follows:
167 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000168 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000169 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000170 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000171 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000172 SDOperand visitTokenFactor(SDNode *N);
173 SDOperand visitADD(SDNode *N);
174 SDOperand visitSUB(SDNode *N);
175 SDOperand visitMUL(SDNode *N);
176 SDOperand visitSDIV(SDNode *N);
177 SDOperand visitUDIV(SDNode *N);
178 SDOperand visitSREM(SDNode *N);
179 SDOperand visitUREM(SDNode *N);
180 SDOperand visitMULHU(SDNode *N);
181 SDOperand visitMULHS(SDNode *N);
182 SDOperand visitAND(SDNode *N);
183 SDOperand visitOR(SDNode *N);
184 SDOperand visitXOR(SDNode *N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000185 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000186 SDOperand visitSHL(SDNode *N);
187 SDOperand visitSRA(SDNode *N);
188 SDOperand visitSRL(SDNode *N);
189 SDOperand visitCTLZ(SDNode *N);
190 SDOperand visitCTTZ(SDNode *N);
191 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000192 SDOperand visitSELECT(SDNode *N);
193 SDOperand visitSELECT_CC(SDNode *N);
194 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000195 SDOperand visitSIGN_EXTEND(SDNode *N);
196 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000197 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000198 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
199 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000200 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000201 SDOperand visitVBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000202 SDOperand visitFADD(SDNode *N);
203 SDOperand visitFSUB(SDNode *N);
204 SDOperand visitFMUL(SDNode *N);
205 SDOperand visitFDIV(SDNode *N);
206 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000207 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000208 SDOperand visitSINT_TO_FP(SDNode *N);
209 SDOperand visitUINT_TO_FP(SDNode *N);
210 SDOperand visitFP_TO_SINT(SDNode *N);
211 SDOperand visitFP_TO_UINT(SDNode *N);
212 SDOperand visitFP_ROUND(SDNode *N);
213 SDOperand visitFP_ROUND_INREG(SDNode *N);
214 SDOperand visitFP_EXTEND(SDNode *N);
215 SDOperand visitFNEG(SDNode *N);
216 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000217 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000218 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000219 SDOperand visitLOAD(SDNode *N);
Chris Lattner29cd7db2006-03-31 18:10:41 +0000220 SDOperand visitXEXTLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000221 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000222 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
223 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000224 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000225 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000226 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000227
Evan Cheng44f1f092006-04-20 08:56:16 +0000228 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000229 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
230
Chris Lattner40c62d52005-10-18 06:04:22 +0000231 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000232 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000233 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
234 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
235 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000236 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000237 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000238 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000239 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000240 SDOperand BuildUDIV(SDNode *N);
241 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Jim Laskey279f0532006-09-25 16:29:54 +0000242
Jim Laskey172585b2006-09-26 07:37:42 +0000243 /// hasChainUsers - Returns true if one of the users of a load node has the
244 /// chain result as an operand.
245 bool hasChainUsers(SDNode *Load);
246
Jim Laskey279f0532006-09-25 16:29:54 +0000247 /// FindBaseOffset - Return true if we can determine base and offset
248 /// information from a given pointer operand. Provides base and offset as a
249 /// result.
250 static bool FindBaseOffset(SDOperand Ptr,
251 SDOperand &Object, int64_t &Offset);
252
253 /// isAlias - Return true if there is the possibility that the two addresses
254 /// overlap.
255 static bool isAlias(SDOperand Ptr1, int64_t Size1, SDOperand SrcValue1,
256 SDOperand Ptr2, int64_t Size2, SDOperand SrcValue2);
257
258 /// FindAliasInfo - Extracts the relevant alias information from the memory
259 /// node.
260 static void FindAliasInfo(SDNode *N,
261 SDOperand &Ptr, int64_t &Size, SDOperand &SrcValue);
262
263 /// hasChain - Return true if Op has a chain. Provides chain if present.
264 ///
265 static bool hasChain(SDOperand Op, SDOperand &Chain);
266
267 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
268 /// looking for a better chain.
269 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
270
Nate Begeman1d4d4142005-09-01 00:19:25 +0000271public:
272 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000273 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000274
275 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000276 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000277 };
278}
279
Chris Lattner24664722006-03-01 04:53:38 +0000280//===----------------------------------------------------------------------===//
281// TargetLowering::DAGCombinerInfo implementation
282//===----------------------------------------------------------------------===//
283
284void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
285 ((DAGCombiner*)DC)->AddToWorkList(N);
286}
287
288SDOperand TargetLowering::DAGCombinerInfo::
289CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000290 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000291}
292
293SDOperand TargetLowering::DAGCombinerInfo::
294CombineTo(SDNode *N, SDOperand Res) {
295 return ((DAGCombiner*)DC)->CombineTo(N, Res);
296}
297
298
299SDOperand TargetLowering::DAGCombinerInfo::
300CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
301 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
302}
303
304
305
306
307//===----------------------------------------------------------------------===//
308
309
Nate Begeman4ebd8052005-09-01 23:24:04 +0000310// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
311// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000312// Also, set the incoming LHS, RHS, and CC references to the appropriate
313// nodes based on the type of node we are checking. This simplifies life a
314// bit for the callers.
315static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
316 SDOperand &CC) {
317 if (N.getOpcode() == ISD::SETCC) {
318 LHS = N.getOperand(0);
319 RHS = N.getOperand(1);
320 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000321 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000322 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000323 if (N.getOpcode() == ISD::SELECT_CC &&
324 N.getOperand(2).getOpcode() == ISD::Constant &&
325 N.getOperand(3).getOpcode() == ISD::Constant &&
326 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000327 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
328 LHS = N.getOperand(0);
329 RHS = N.getOperand(1);
330 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000331 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000332 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000333 return false;
334}
335
Nate Begeman99801192005-09-07 23:25:52 +0000336// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
337// one use. If this is true, it allows the users to invert the operation for
338// free when it is profitable to do so.
339static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000340 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000341 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000342 return true;
343 return false;
344}
345
Nate Begemancd4d58c2006-02-03 06:46:56 +0000346SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
347 MVT::ValueType VT = N0.getValueType();
348 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
349 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
350 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
351 if (isa<ConstantSDNode>(N1)) {
352 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000353 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000354 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
355 } else if (N0.hasOneUse()) {
356 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000357 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000358 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
359 }
360 }
361 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
362 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
363 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
364 if (isa<ConstantSDNode>(N0)) {
365 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000366 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000367 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
368 } else if (N1.hasOneUse()) {
369 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000370 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000371 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
372 }
373 }
374 return SDOperand();
375}
376
Nate Begeman4ebd8052005-09-01 23:24:04 +0000377void DAGCombiner::Run(bool RunningAfterLegalize) {
378 // set the instance variable, so that the various visit routines may use it.
379 AfterLegalize = RunningAfterLegalize;
380
Nate Begeman646d7e22005-09-02 21:18:40 +0000381 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000382 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
383 E = DAG.allnodes_end(); I != E; ++I)
384 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000385
Chris Lattner95038592005-10-05 06:35:28 +0000386 // Create a dummy node (which is not added to allnodes), that adds a reference
387 // to the root node, preventing it from being deleted, and tracking any
388 // changes of the root.
389 HandleSDNode Dummy(DAG.getRoot());
390
Chris Lattner24664722006-03-01 04:53:38 +0000391
392 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
393 TargetLowering::DAGCombinerInfo
394 DagCombineInfo(DAG, !RunningAfterLegalize, this);
395
Nate Begeman1d4d4142005-09-01 00:19:25 +0000396 // while the worklist isn't empty, inspect the node on the end of it and
397 // try and combine it.
398 while (!WorkList.empty()) {
399 SDNode *N = WorkList.back();
400 WorkList.pop_back();
401
402 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000403 // N is deleted from the DAG, since they too may now be dead or may have a
404 // reduced number of uses, allowing other xforms.
405 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000406 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
407 WorkList.push_back(N->getOperand(i).Val);
408
Nate Begeman1d4d4142005-09-01 00:19:25 +0000409 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000410 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000411 continue;
412 }
413
Nate Begeman83e75ec2005-09-06 04:43:02 +0000414 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000415
416 // If nothing happened, try a target-specific DAG combine.
417 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000418 assert(N->getOpcode() != ISD::DELETED_NODE &&
419 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000420 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
421 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
422 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
423 }
424
Nate Begeman83e75ec2005-09-06 04:43:02 +0000425 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000426 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000427 // If we get back the same node we passed in, rather than a new node or
428 // zero, we know that the node must have defined multiple values and
429 // CombineTo was used. Since CombineTo takes care of the worklist
430 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000431 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000432 assert(N->getOpcode() != ISD::DELETED_NODE &&
433 RV.Val->getOpcode() != ISD::DELETED_NODE &&
434 "Node was deleted but visit returned new node!");
435
Nate Begeman2300f552005-09-07 00:15:36 +0000436 DEBUG(std::cerr << "\nReplacing "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000437 std::cerr << "\nWith: "; RV.Val->dump(&DAG);
Nate Begeman2300f552005-09-07 00:15:36 +0000438 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000439 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000440 if (N->getNumValues() == RV.Val->getNumValues())
441 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
442 else {
443 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
444 SDOperand OpV = RV;
445 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
446 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000447
448 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000449 WorkList.push_back(RV.Val);
450 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000451
452 // Nodes can end up on the worklist more than once. Make sure we do
453 // not process a node that has been replaced.
454 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000455 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
456 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000457
458 // Finally, since the node is now dead, remove it from the graph.
459 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000460 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000461 }
462 }
Chris Lattner95038592005-10-05 06:35:28 +0000463
464 // If the root changed (e.g. it was a dead load, update the root).
465 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000466}
467
Nate Begeman83e75ec2005-09-06 04:43:02 +0000468SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000469 switch(N->getOpcode()) {
470 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000471 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000472 case ISD::ADD: return visitADD(N);
473 case ISD::SUB: return visitSUB(N);
474 case ISD::MUL: return visitMUL(N);
475 case ISD::SDIV: return visitSDIV(N);
476 case ISD::UDIV: return visitUDIV(N);
477 case ISD::SREM: return visitSREM(N);
478 case ISD::UREM: return visitUREM(N);
479 case ISD::MULHU: return visitMULHU(N);
480 case ISD::MULHS: return visitMULHS(N);
481 case ISD::AND: return visitAND(N);
482 case ISD::OR: return visitOR(N);
483 case ISD::XOR: return visitXOR(N);
484 case ISD::SHL: return visitSHL(N);
485 case ISD::SRA: return visitSRA(N);
486 case ISD::SRL: return visitSRL(N);
487 case ISD::CTLZ: return visitCTLZ(N);
488 case ISD::CTTZ: return visitCTTZ(N);
489 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000490 case ISD::SELECT: return visitSELECT(N);
491 case ISD::SELECT_CC: return visitSELECT_CC(N);
492 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000493 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
494 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000495 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000496 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
497 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000498 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000499 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000500 case ISD::FADD: return visitFADD(N);
501 case ISD::FSUB: return visitFSUB(N);
502 case ISD::FMUL: return visitFMUL(N);
503 case ISD::FDIV: return visitFDIV(N);
504 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000505 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000506 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
507 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
508 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
509 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
510 case ISD::FP_ROUND: return visitFP_ROUND(N);
511 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
512 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
513 case ISD::FNEG: return visitFNEG(N);
514 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000515 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000516 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000517 case ISD::LOAD: return visitLOAD(N);
Chris Lattner29cd7db2006-03-31 18:10:41 +0000518 case ISD::EXTLOAD:
519 case ISD::SEXTLOAD:
520 case ISD::ZEXTLOAD: return visitXEXTLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000521 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000522 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
523 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000524 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000525 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000526 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000527 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
528 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
529 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
530 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
531 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
532 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
533 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
534 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000535 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000536 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000537}
538
Nate Begeman83e75ec2005-09-06 04:43:02 +0000539SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000540 // If the token factor has two operands and one is the entry token, replace
541 // the token factor with the other operand.
542 if (N->getNumOperands() == 2) {
Chris Lattner21a57dc2006-05-12 05:01:37 +0000543 if (N->getOperand(0).getOpcode() == ISD::EntryToken ||
544 N->getOperand(0) == N->getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000545 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000546 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000547 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000548 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000549
Jim Laskey279f0532006-09-25 16:29:54 +0000550 SmallVector<SDNode *, 8> TFs; // Set of token factor nodes.
551 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
552
553 // Add this ndoe to the token factor set.
554 TFs.push_back(N);
555
556 // Separate token factors from other operands.
557 for (unsigned i = 0, ie = N->getNumOperands(); i != ie; ++i) {
Nate Begemanded49632005-10-13 03:11:28 +0000558 SDOperand Op = N->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000559 if (Op.getOpcode() == ISD::TokenFactor)
560 TFs.push_back(Op.Val);
561 else if (Op.getOpcode() != ISD::EntryToken)
Nate Begemanded49632005-10-13 03:11:28 +0000562 Ops.push_back(Op);
Jim Laskey279f0532006-09-25 16:29:54 +0000563 }
564
565 // If there are token factor operands.
566 if (TFs.size() > 1) {
567 bool Changed = false; // If we should replace this token factor.
568
569 // For each token factor.
570 for (unsigned j = 1, je = TFs.size(); j != je; ++j) {
571 SDNode *TF = TFs[j];
572 bool CanMerge = true; // Can we merge this token factor.
573
574 if (CombinerAA) {
575 if (!TF->hasOneUse()) {
576 // Check to see if all users point to members of the token factor set.
577 for (SDNode::use_iterator UI = TF->use_begin(), UE = TF->use_end();
578 CanMerge && UI != UE; ++UI) {
579 SDNode *User = *UI;
580 CanMerge = User->getOpcode() == ISD::TokenFactor &&
581 std::find(TFs.begin(), TFs.end(), User) != TFs.end();
582 }
583 }
584 } else {
585 CanMerge = TF->hasOneUse();
586 }
587
588 // If it's valid to merge.
589 if (CanMerge) {
590 // Remove dead token factor node.
591 AddToWorkList(TF);
592
593 // Make sure we don't duplicate operands.
594 unsigned m = Ops.size(); // Number of prior operands.
595 for (unsigned l = 0, le = TF->getNumOperands(); l != le; ++l) {
596 SDOperand Op = TF->getOperand(l);
597 if (std::find(Ops.begin(), Ops.end(), Op) == Ops.end())
598 Ops.push_back(Op);
599 }
600 Changed = true;
601 } else {
602 // Can't merge this token factor.
603 Ops.push_back(SDOperand(TF, 0));
604 }
605 }
606
607 // If we've change things around then replace token factor.
608 if (Changed) {
609 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000610 }
611 }
Jim Laskey279f0532006-09-25 16:29:54 +0000612
Nate Begeman83e75ec2005-09-06 04:43:02 +0000613 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000614}
615
Nate Begeman83e75ec2005-09-06 04:43:02 +0000616SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000617 SDOperand N0 = N->getOperand(0);
618 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000619 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
620 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000621 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000622
623 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000624 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000625 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000626 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000627 if (N0C && !N1C)
628 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000629 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000630 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000631 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000632 // fold ((c1-A)+c2) -> (c1+c2)-A
633 if (N1C && N0.getOpcode() == ISD::SUB)
634 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
635 return DAG.getNode(ISD::SUB, VT,
636 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
637 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000638 // reassociate add
639 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
640 if (RADD.Val != 0)
641 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000642 // fold ((0-A) + B) -> B-A
643 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
644 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000645 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000646 // fold (A + (0-B)) -> A-B
647 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
648 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000649 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000650 // fold (A+(B-A)) -> B
651 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000652 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000653
Evan Cheng860771d2006-03-01 01:09:54 +0000654 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000655 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000656
657 // fold (a+b) -> (a|b) iff a and b share no bits.
658 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
659 uint64_t LHSZero, LHSOne;
660 uint64_t RHSZero, RHSOne;
661 uint64_t Mask = MVT::getIntVTBitMask(VT);
662 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
663 if (LHSZero) {
664 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
665
666 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
667 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
668 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
669 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
670 return DAG.getNode(ISD::OR, VT, N0, N1);
671 }
672 }
673
Nate Begeman83e75ec2005-09-06 04:43:02 +0000674 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000675}
676
Nate Begeman83e75ec2005-09-06 04:43:02 +0000677SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000678 SDOperand N0 = N->getOperand(0);
679 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000680 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
681 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000682 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000683
Chris Lattner854077d2005-10-17 01:07:11 +0000684 // fold (sub x, x) -> 0
685 if (N0 == N1)
686 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000687 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000688 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000689 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000690 // fold (sub x, c) -> (add x, -c)
691 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000692 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000693 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000694 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000695 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000696 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000697 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000698 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000699 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000700}
701
Nate Begeman83e75ec2005-09-06 04:43:02 +0000702SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000703 SDOperand N0 = N->getOperand(0);
704 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000705 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
706 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000707 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000708
709 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000710 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000711 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000712 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000713 if (N0C && !N1C)
714 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000715 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000716 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000717 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000718 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000719 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000720 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000721 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000722 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000723 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000724 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000725 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000726 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
727 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
728 // FIXME: If the input is something that is easily negated (e.g. a
729 // single-use add), we should put the negate there.
730 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
731 DAG.getNode(ISD::SHL, VT, N0,
732 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
733 TLI.getShiftAmountTy())));
734 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +0000735
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000736 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
737 if (N1C && N0.getOpcode() == ISD::SHL &&
738 isa<ConstantSDNode>(N0.getOperand(1))) {
739 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000740 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000741 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
742 }
743
744 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
745 // use.
746 {
747 SDOperand Sh(0,0), Y(0,0);
748 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
749 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
750 N0.Val->hasOneUse()) {
751 Sh = N0; Y = N1;
752 } else if (N1.getOpcode() == ISD::SHL &&
753 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
754 Sh = N1; Y = N0;
755 }
756 if (Sh.Val) {
757 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
758 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
759 }
760 }
Chris Lattnera1deca32006-03-04 23:33:26 +0000761 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
762 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
763 isa<ConstantSDNode>(N0.getOperand(1))) {
764 return DAG.getNode(ISD::ADD, VT,
765 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
766 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
767 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000768
Nate Begemancd4d58c2006-02-03 06:46:56 +0000769 // reassociate mul
770 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
771 if (RMUL.Val != 0)
772 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000773 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000774}
775
Nate Begeman83e75ec2005-09-06 04:43:02 +0000776SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000777 SDOperand N0 = N->getOperand(0);
778 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000779 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
780 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000781 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000782
783 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000784 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000785 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000786 // fold (sdiv X, 1) -> X
787 if (N1C && N1C->getSignExtended() == 1LL)
788 return N0;
789 // fold (sdiv X, -1) -> 0-X
790 if (N1C && N1C->isAllOnesValue())
791 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000792 // If we know the sign bits of both operands are zero, strength reduce to a
793 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
794 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000795 if (TLI.MaskedValueIsZero(N1, SignBit) &&
796 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000797 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000798 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000799 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000800 (isPowerOf2_64(N1C->getSignExtended()) ||
801 isPowerOf2_64(-N1C->getSignExtended()))) {
802 // If dividing by powers of two is cheap, then don't perform the following
803 // fold.
804 if (TLI.isPow2DivCheap())
805 return SDOperand();
806 int64_t pow2 = N1C->getSignExtended();
807 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000808 unsigned lg2 = Log2_64(abs2);
809 // Splat the sign bit into the register
810 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000811 DAG.getConstant(MVT::getSizeInBits(VT)-1,
812 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +0000813 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000814 // Add (N0 < 0) ? abs2 - 1 : 0;
815 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
816 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000817 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000818 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +0000819 AddToWorkList(SRL.Val);
820 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +0000821 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
822 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000823 // If we're dividing by a positive value, we're done. Otherwise, we must
824 // negate the result.
825 if (pow2 > 0)
826 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +0000827 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000828 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
829 }
Nate Begeman69575232005-10-20 02:15:44 +0000830 // if integer divide is expensive and we satisfy the requirements, emit an
831 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000832 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000833 !TLI.isIntDivCheap()) {
834 SDOperand Op = BuildSDIV(N);
835 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000836 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000837 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000838}
839
Nate Begeman83e75ec2005-09-06 04:43:02 +0000840SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000841 SDOperand N0 = N->getOperand(0);
842 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000843 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
844 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000845 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000846
847 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000848 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000849 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000850 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000851 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000852 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000853 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000854 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000855 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
856 if (N1.getOpcode() == ISD::SHL) {
857 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
858 if (isPowerOf2_64(SHC->getValue())) {
859 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000860 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
861 DAG.getConstant(Log2_64(SHC->getValue()),
862 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +0000863 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000864 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000865 }
866 }
867 }
Nate Begeman69575232005-10-20 02:15:44 +0000868 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000869 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
870 SDOperand Op = BuildUDIV(N);
871 if (Op.Val) return Op;
872 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000873 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000874}
875
Nate Begeman83e75ec2005-09-06 04:43:02 +0000876SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000877 SDOperand N0 = N->getOperand(0);
878 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000879 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
880 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000881 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000882
883 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000884 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000885 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000886 // If we know the sign bits of both operands are zero, strength reduce to a
887 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
888 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000889 if (TLI.MaskedValueIsZero(N1, SignBit) &&
890 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000891 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000892 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000893}
894
Nate Begeman83e75ec2005-09-06 04:43:02 +0000895SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000896 SDOperand N0 = N->getOperand(0);
897 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000898 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
899 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000900 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000901
902 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000903 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000904 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000905 // fold (urem x, pow2) -> (and x, pow2-1)
906 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000907 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000908 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
909 if (N1.getOpcode() == ISD::SHL) {
910 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
911 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000912 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +0000913 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000914 return DAG.getNode(ISD::AND, VT, N0, Add);
915 }
916 }
917 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000918 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000919}
920
Nate Begeman83e75ec2005-09-06 04:43:02 +0000921SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000922 SDOperand N0 = N->getOperand(0);
923 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000924 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000925
926 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000927 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000928 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000929 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000930 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000931 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
932 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000933 TLI.getShiftAmountTy()));
934 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000935}
936
Nate Begeman83e75ec2005-09-06 04:43:02 +0000937SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000938 SDOperand N0 = N->getOperand(0);
939 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000940 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000941
942 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000943 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000944 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000945 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000946 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000947 return DAG.getConstant(0, N0.getValueType());
948 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000949}
950
Chris Lattner35e5c142006-05-05 05:51:50 +0000951/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
952/// two operands of the same opcode, try to simplify it.
953SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
954 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
955 MVT::ValueType VT = N0.getValueType();
956 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
957
Chris Lattner540121f2006-05-05 06:31:05 +0000958 // For each of OP in AND/OR/XOR:
959 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
960 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
961 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +0000962 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +0000963 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +0000964 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000965 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
966 SDOperand ORNode = DAG.getNode(N->getOpcode(),
967 N0.getOperand(0).getValueType(),
968 N0.getOperand(0), N1.getOperand(0));
969 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +0000970 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +0000971 }
972
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000973 // For each of OP in SHL/SRL/SRA/AND...
974 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
975 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
976 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +0000977 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000978 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000979 N0.getOperand(1) == N1.getOperand(1)) {
980 SDOperand ORNode = DAG.getNode(N->getOpcode(),
981 N0.getOperand(0).getValueType(),
982 N0.getOperand(0), N1.getOperand(0));
983 AddToWorkList(ORNode.Val);
984 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
985 }
986
987 return SDOperand();
988}
989
Nate Begeman83e75ec2005-09-06 04:43:02 +0000990SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000991 SDOperand N0 = N->getOperand(0);
992 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +0000993 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000994 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
995 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000996 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000997 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000998
999 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001000 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001001 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001002 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001003 if (N0C && !N1C)
1004 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001005 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001006 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001007 return N0;
1008 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +00001009 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001010 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001011 // reassociate and
1012 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1013 if (RAND.Val != 0)
1014 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001015 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001016 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001017 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001018 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001019 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001020 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1021 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001022 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +00001023 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001024 ~N1C->getValue() & InMask)) {
1025 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1026 N0.getOperand(0));
1027
1028 // Replace uses of the AND with uses of the Zero extend node.
1029 CombineTo(N, Zext);
1030
Chris Lattner3603cd62006-02-02 07:17:31 +00001031 // We actually want to replace all uses of the any_extend with the
1032 // zero_extend, to avoid duplicating things. This will later cause this
1033 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001034 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001035 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001036 }
1037 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001038 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1039 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1040 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1041 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1042
1043 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1044 MVT::isInteger(LL.getValueType())) {
1045 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1046 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1047 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001048 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001049 return DAG.getSetCC(VT, ORNode, LR, Op1);
1050 }
1051 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1052 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1053 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001054 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001055 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1056 }
1057 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1058 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1059 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001060 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001061 return DAG.getSetCC(VT, ORNode, LR, Op1);
1062 }
1063 }
1064 // canonicalize equivalent to ll == rl
1065 if (LL == RR && LR == RL) {
1066 Op1 = ISD::getSetCCSwappedOperands(Op1);
1067 std::swap(RL, RR);
1068 }
1069 if (LL == RL && LR == RR) {
1070 bool isInteger = MVT::isInteger(LL.getValueType());
1071 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1072 if (Result != ISD::SETCC_INVALID)
1073 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1074 }
1075 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001076
1077 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1078 if (N0.getOpcode() == N1.getOpcode()) {
1079 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1080 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001081 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001082
Nate Begemande996292006-02-03 22:24:05 +00001083 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1084 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001085 if (!MVT::isVector(VT) &&
1086 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001087 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001088 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +00001089 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +00001090 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001091 // If we zero all the possible extended bits, then we can turn this into
1092 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001093 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +00001094 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001095 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1096 N0.getOperand(1), N0.getOperand(2),
1097 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001098 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001099 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001100 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001101 }
1102 }
1103 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001104 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001105 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001106 // If we zero all the possible extended bits, then we can turn this into
1107 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001108 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001109 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001110 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1111 N0.getOperand(1), N0.getOperand(2),
1112 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001113 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001114 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001115 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001116 }
1117 }
Chris Lattner15045b62006-02-28 06:35:35 +00001118
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001119 // fold (and (load x), 255) -> (zextload x, i8)
1120 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1121 if (N1C &&
1122 (N0.getOpcode() == ISD::LOAD || N0.getOpcode() == ISD::EXTLOAD ||
1123 N0.getOpcode() == ISD::ZEXTLOAD) &&
1124 N0.hasOneUse()) {
1125 MVT::ValueType EVT, LoadedVT;
Chris Lattner15045b62006-02-28 06:35:35 +00001126 if (N1C->getValue() == 255)
1127 EVT = MVT::i8;
1128 else if (N1C->getValue() == 65535)
1129 EVT = MVT::i16;
1130 else if (N1C->getValue() == ~0U)
1131 EVT = MVT::i32;
1132 else
1133 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001134
1135 LoadedVT = N0.getOpcode() == ISD::LOAD ? VT :
1136 cast<VTSDNode>(N0.getOperand(3))->getVT();
Chris Lattnere44be602006-04-04 17:39:18 +00001137 if (EVT != MVT::Other && LoadedVT > EVT &&
1138 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Chris Lattner15045b62006-02-28 06:35:35 +00001139 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1140 // For big endian targets, we need to add an offset to the pointer to load
1141 // the correct bytes. For little endian systems, we merely need to read
1142 // fewer bytes from the same pointer.
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001143 unsigned PtrOff =
1144 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1145 SDOperand NewPtr = N0.getOperand(1);
1146 if (!TLI.isLittleEndian())
1147 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1148 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001149 AddToWorkList(NewPtr.Val);
Chris Lattner15045b62006-02-28 06:35:35 +00001150 SDOperand Load =
1151 DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
1152 N0.getOperand(2), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001153 AddToWorkList(N);
Chris Lattner15045b62006-02-28 06:35:35 +00001154 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001155 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner15045b62006-02-28 06:35:35 +00001156 }
1157 }
1158
Nate Begeman83e75ec2005-09-06 04:43:02 +00001159 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001160}
1161
Nate Begeman83e75ec2005-09-06 04:43:02 +00001162SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001163 SDOperand N0 = N->getOperand(0);
1164 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001165 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001166 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1167 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001168 MVT::ValueType VT = N1.getValueType();
1169 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001170
1171 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001172 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001173 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001174 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001175 if (N0C && !N1C)
1176 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001177 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001178 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001179 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001180 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001181 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001182 return N1;
1183 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001184 if (N1C &&
1185 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001186 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001187 // reassociate or
1188 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1189 if (ROR.Val != 0)
1190 return ROR;
1191 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1192 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001193 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001194 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1195 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1196 N1),
1197 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001198 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001199 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1200 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1201 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1202 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1203
1204 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1205 MVT::isInteger(LL.getValueType())) {
1206 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1207 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1208 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1209 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1210 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001211 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001212 return DAG.getSetCC(VT, ORNode, LR, Op1);
1213 }
1214 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1215 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1216 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1217 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1218 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001219 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001220 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1221 }
1222 }
1223 // canonicalize equivalent to ll == rl
1224 if (LL == RR && LR == RL) {
1225 Op1 = ISD::getSetCCSwappedOperands(Op1);
1226 std::swap(RL, RR);
1227 }
1228 if (LL == RL && LR == RR) {
1229 bool isInteger = MVT::isInteger(LL.getValueType());
1230 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1231 if (Result != ISD::SETCC_INVALID)
1232 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1233 }
1234 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001235
1236 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1237 if (N0.getOpcode() == N1.getOpcode()) {
1238 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1239 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001240 }
Chris Lattner516b9622006-09-14 20:50:57 +00001241
Chris Lattner1ec72732006-09-14 21:11:37 +00001242 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1243 if (N0.getOpcode() == ISD::AND &&
1244 N1.getOpcode() == ISD::AND &&
1245 N0.getOperand(1).getOpcode() == ISD::Constant &&
1246 N1.getOperand(1).getOpcode() == ISD::Constant &&
1247 // Don't increase # computations.
1248 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1249 // We can only do this xform if we know that bits from X that are set in C2
1250 // but not in C1 are already zero. Likewise for Y.
1251 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1252 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1253
1254 if (TLI.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1255 TLI.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
1256 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1257 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1258 }
1259 }
1260
1261
Chris Lattner516b9622006-09-14 20:50:57 +00001262 // See if this is some rotate idiom.
1263 if (SDNode *Rot = MatchRotate(N0, N1))
1264 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001265
Nate Begeman83e75ec2005-09-06 04:43:02 +00001266 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001267}
1268
Chris Lattner516b9622006-09-14 20:50:57 +00001269
1270/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1271static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1272 if (Op.getOpcode() == ISD::AND) {
1273 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1274 Mask = Op.getOperand(1);
1275 Op = Op.getOperand(0);
1276 } else {
1277 return false;
1278 }
1279 }
1280
1281 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1282 Shift = Op;
1283 return true;
1284 }
1285 return false;
1286}
1287
1288
1289// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1290// idioms for rotate, and if the target supports rotation instructions, generate
1291// a rot[lr].
1292SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1293 // Must be a legal type. Expanded an promoted things won't work with rotates.
1294 MVT::ValueType VT = LHS.getValueType();
1295 if (!TLI.isTypeLegal(VT)) return 0;
1296
1297 // The target must have at least one rotate flavor.
1298 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1299 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1300 if (!HasROTL && !HasROTR) return 0;
1301
1302 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1303 SDOperand LHSShift; // The shift.
1304 SDOperand LHSMask; // AND value if any.
1305 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1306 return 0; // Not part of a rotate.
1307
1308 SDOperand RHSShift; // The shift.
1309 SDOperand RHSMask; // AND value if any.
1310 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1311 return 0; // Not part of a rotate.
1312
1313 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1314 return 0; // Not shifting the same value.
1315
1316 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1317 return 0; // Shifts must disagree.
1318
1319 // Canonicalize shl to left side in a shl/srl pair.
1320 if (RHSShift.getOpcode() == ISD::SHL) {
1321 std::swap(LHS, RHS);
1322 std::swap(LHSShift, RHSShift);
1323 std::swap(LHSMask , RHSMask );
1324 }
1325
1326 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1327
1328 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1329 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
1330 if (LHSShift.getOperand(1).getOpcode() == ISD::Constant &&
1331 RHSShift.getOperand(1).getOpcode() == ISD::Constant) {
1332 uint64_t LShVal = cast<ConstantSDNode>(LHSShift.getOperand(1))->getValue();
1333 uint64_t RShVal = cast<ConstantSDNode>(RHSShift.getOperand(1))->getValue();
1334 if ((LShVal + RShVal) != OpSizeInBits)
1335 return 0;
1336
1337 SDOperand Rot;
1338 if (HasROTL)
1339 Rot = DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1340 LHSShift.getOperand(1));
1341 else
1342 Rot = DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1343 RHSShift.getOperand(1));
1344
1345 // If there is an AND of either shifted operand, apply it to the result.
1346 if (LHSMask.Val || RHSMask.Val) {
1347 uint64_t Mask = MVT::getIntVTBitMask(VT);
1348
1349 if (LHSMask.Val) {
1350 uint64_t RHSBits = (1ULL << LShVal)-1;
1351 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1352 }
1353 if (RHSMask.Val) {
1354 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1355 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1356 }
1357
1358 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1359 }
1360
1361 return Rot.Val;
1362 }
1363
1364 // If there is a mask here, and we have a variable shift, we can't be sure
1365 // that we're masking out the right stuff.
1366 if (LHSMask.Val || RHSMask.Val)
1367 return 0;
1368
1369 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1370 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
1371 if (RHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1372 LHSShift.getOperand(1) == RHSShift.getOperand(1).getOperand(1)) {
1373 if (ConstantSDNode *SUBC =
1374 dyn_cast<ConstantSDNode>(RHSShift.getOperand(1).getOperand(0))) {
1375 if (SUBC->getValue() == OpSizeInBits)
1376 if (HasROTL)
1377 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1378 LHSShift.getOperand(1)).Val;
1379 else
1380 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1381 LHSShift.getOperand(1)).Val;
1382 }
1383 }
1384
1385 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1386 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
1387 if (LHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1388 RHSShift.getOperand(1) == LHSShift.getOperand(1).getOperand(1)) {
1389 if (ConstantSDNode *SUBC =
1390 dyn_cast<ConstantSDNode>(LHSShift.getOperand(1).getOperand(0))) {
1391 if (SUBC->getValue() == OpSizeInBits)
1392 if (HasROTL)
1393 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1394 LHSShift.getOperand(1)).Val;
1395 else
1396 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1397 RHSShift.getOperand(1)).Val;
1398 }
1399 }
1400
1401 return 0;
1402}
1403
1404
Nate Begeman83e75ec2005-09-06 04:43:02 +00001405SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001406 SDOperand N0 = N->getOperand(0);
1407 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001408 SDOperand LHS, RHS, CC;
1409 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1410 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001411 MVT::ValueType VT = N0.getValueType();
1412
1413 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001414 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001415 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001416 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001417 if (N0C && !N1C)
1418 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001419 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001420 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001421 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001422 // reassociate xor
1423 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1424 if (RXOR.Val != 0)
1425 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001426 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001427 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1428 bool isInt = MVT::isInteger(LHS.getValueType());
1429 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1430 isInt);
1431 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001432 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001433 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001434 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001435 assert(0 && "Unhandled SetCC Equivalent!");
1436 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001437 }
Nate Begeman99801192005-09-07 23:25:52 +00001438 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1439 if (N1C && N1C->getValue() == 1 &&
1440 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001441 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001442 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1443 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001444 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1445 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001446 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001447 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001448 }
1449 }
Nate Begeman99801192005-09-07 23:25:52 +00001450 // fold !(x or y) -> (!x and !y) iff x or y are constants
1451 if (N1C && N1C->isAllOnesValue() &&
1452 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001453 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001454 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1455 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001456 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1457 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001458 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001459 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001460 }
1461 }
Nate Begeman223df222005-09-08 20:18:10 +00001462 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1463 if (N1C && N0.getOpcode() == ISD::XOR) {
1464 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1465 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1466 if (N00C)
1467 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1468 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1469 if (N01C)
1470 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1471 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1472 }
1473 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001474 if (N0 == N1) {
1475 if (!MVT::isVector(VT)) {
1476 return DAG.getConstant(0, VT);
1477 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1478 // Produce a vector of zeros.
1479 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1480 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001481 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001482 }
1483 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001484
1485 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1486 if (N0.getOpcode() == N1.getOpcode()) {
1487 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1488 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001489 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001490
Chris Lattner3e104b12006-04-08 04:15:24 +00001491 // Simplify the expression using non-local knowledge.
1492 if (!MVT::isVector(VT) &&
1493 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001494 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001495
Nate Begeman83e75ec2005-09-06 04:43:02 +00001496 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001497}
1498
Nate Begeman83e75ec2005-09-06 04:43:02 +00001499SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001500 SDOperand N0 = N->getOperand(0);
1501 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001502 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1503 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001504 MVT::ValueType VT = N0.getValueType();
1505 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1506
1507 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001508 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001509 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001510 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001511 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001512 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001513 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001514 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001515 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001516 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001517 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001518 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001519 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001520 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001521 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001522 if (SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001523 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001524 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001525 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001526 N0.getOperand(1).getOpcode() == ISD::Constant) {
1527 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001528 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001529 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001530 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001531 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001532 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001533 }
1534 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1535 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001536 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001537 N0.getOperand(1).getOpcode() == ISD::Constant) {
1538 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001539 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001540 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1541 DAG.getConstant(~0ULL << c1, VT));
1542 if (c2 > c1)
1543 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001544 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001545 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001546 return DAG.getNode(ISD::SRL, VT, Mask,
1547 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001548 }
1549 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001550 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001551 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001552 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnercac70592006-03-05 19:53:55 +00001553 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1554 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1555 isa<ConstantSDNode>(N0.getOperand(1))) {
1556 return DAG.getNode(ISD::ADD, VT,
1557 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1558 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1559 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001560 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001561}
1562
Nate Begeman83e75ec2005-09-06 04:43:02 +00001563SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001564 SDOperand N0 = N->getOperand(0);
1565 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001566 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1567 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001568 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001569
1570 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001571 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001572 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001573 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001574 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001575 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001576 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001577 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001578 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001579 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001580 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001581 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001582 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001583 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001584 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001585 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1586 // sext_inreg.
1587 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1588 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1589 MVT::ValueType EVT;
1590 switch (LowBits) {
1591 default: EVT = MVT::Other; break;
1592 case 1: EVT = MVT::i1; break;
1593 case 8: EVT = MVT::i8; break;
1594 case 16: EVT = MVT::i16; break;
1595 case 32: EVT = MVT::i32; break;
1596 }
1597 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1598 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1599 DAG.getValueType(EVT));
1600 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001601
1602 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1603 if (N1C && N0.getOpcode() == ISD::SRA) {
1604 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1605 unsigned Sum = N1C->getValue() + C1->getValue();
1606 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1607 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1608 DAG.getConstant(Sum, N1C->getValueType(0)));
1609 }
1610 }
1611
Chris Lattnera8504462006-05-08 20:51:54 +00001612 // Simplify, based on bits shifted out of the LHS.
1613 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
1614 return SDOperand(N, 0);
1615
1616
Nate Begeman1d4d4142005-09-01 00:19:25 +00001617 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001618 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001619 return DAG.getNode(ISD::SRL, VT, N0, N1);
1620 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001621}
1622
Nate Begeman83e75ec2005-09-06 04:43:02 +00001623SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001624 SDOperand N0 = N->getOperand(0);
1625 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001626 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1627 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001628 MVT::ValueType VT = N0.getValueType();
1629 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1630
1631 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001632 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001633 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001634 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001635 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001636 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001637 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001638 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001639 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001640 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001641 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001642 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001643 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001644 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001645 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001646 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001647 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001648 N0.getOperand(1).getOpcode() == ISD::Constant) {
1649 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001650 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001651 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001652 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001653 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001654 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001655 }
Chris Lattner350bec02006-04-02 06:11:11 +00001656
Chris Lattner06afe072006-05-05 22:53:17 +00001657 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
1658 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1659 // Shifting in all undef bits?
1660 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
1661 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
1662 return DAG.getNode(ISD::UNDEF, VT);
1663
1664 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
1665 AddToWorkList(SmallShift.Val);
1666 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
1667 }
1668
Chris Lattner350bec02006-04-02 06:11:11 +00001669 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1670 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1671 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1672 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1673 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
1674
1675 // If any of the input bits are KnownOne, then the input couldn't be all
1676 // zeros, thus the result of the srl will always be zero.
1677 if (KnownOne) return DAG.getConstant(0, VT);
1678
1679 // If all of the bits input the to ctlz node are known to be zero, then
1680 // the result of the ctlz is "32" and the result of the shift is one.
1681 uint64_t UnknownBits = ~KnownZero & Mask;
1682 if (UnknownBits == 0) return DAG.getConstant(1, VT);
1683
1684 // Otherwise, check to see if there is exactly one bit input to the ctlz.
1685 if ((UnknownBits & (UnknownBits-1)) == 0) {
1686 // Okay, we know that only that the single bit specified by UnknownBits
1687 // could be set on input to the CTLZ node. If this bit is set, the SRL
1688 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
1689 // to an SRL,XOR pair, which is likely to simplify more.
1690 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
1691 SDOperand Op = N0.getOperand(0);
1692 if (ShAmt) {
1693 Op = DAG.getNode(ISD::SRL, VT, Op,
1694 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
1695 AddToWorkList(Op.Val);
1696 }
1697 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
1698 }
1699 }
1700
Nate Begeman83e75ec2005-09-06 04:43:02 +00001701 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001702}
1703
Nate Begeman83e75ec2005-09-06 04:43:02 +00001704SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001705 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001706 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001707
1708 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001709 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001710 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001711 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001712}
1713
Nate Begeman83e75ec2005-09-06 04:43:02 +00001714SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001715 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001716 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001717
1718 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001719 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001720 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001721 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001722}
1723
Nate Begeman83e75ec2005-09-06 04:43:02 +00001724SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001725 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001726 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001727
1728 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001729 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001730 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001731 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001732}
1733
Nate Begeman452d7be2005-09-16 00:54:12 +00001734SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1735 SDOperand N0 = N->getOperand(0);
1736 SDOperand N1 = N->getOperand(1);
1737 SDOperand N2 = N->getOperand(2);
1738 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1739 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1740 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1741 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001742
Nate Begeman452d7be2005-09-16 00:54:12 +00001743 // fold select C, X, X -> X
1744 if (N1 == N2)
1745 return N1;
1746 // fold select true, X, Y -> X
1747 if (N0C && !N0C->isNullValue())
1748 return N1;
1749 // fold select false, X, Y -> Y
1750 if (N0C && N0C->isNullValue())
1751 return N2;
1752 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001753 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001754 return DAG.getNode(ISD::OR, VT, N0, N2);
1755 // fold select C, 0, X -> ~C & X
1756 // FIXME: this should check for C type == X type, not i1?
1757 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1758 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001759 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001760 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1761 }
1762 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001763 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001764 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001765 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001766 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1767 }
1768 // fold select C, X, 0 -> C & X
1769 // FIXME: this should check for C type == X type, not i1?
1770 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1771 return DAG.getNode(ISD::AND, VT, N0, N1);
1772 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1773 if (MVT::i1 == VT && N0 == N1)
1774 return DAG.getNode(ISD::OR, VT, N0, N2);
1775 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1776 if (MVT::i1 == VT && N0 == N2)
1777 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00001778
Chris Lattner40c62d52005-10-18 06:04:22 +00001779 // If we can fold this based on the true/false value, do so.
1780 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00001781 return SDOperand(N, 0); // Don't revisit N.
1782
Nate Begeman44728a72005-09-19 22:34:01 +00001783 // fold selects based on a setcc into other things, such as min/max/abs
1784 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001785 // FIXME:
1786 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1787 // having to say they don't support SELECT_CC on every type the DAG knows
1788 // about, since there is no way to mark an opcode illegal at all value types
1789 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1790 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1791 N1, N2, N0.getOperand(2));
1792 else
1793 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001794 return SDOperand();
1795}
1796
1797SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001798 SDOperand N0 = N->getOperand(0);
1799 SDOperand N1 = N->getOperand(1);
1800 SDOperand N2 = N->getOperand(2);
1801 SDOperand N3 = N->getOperand(3);
1802 SDOperand N4 = N->getOperand(4);
1803 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1804 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1805 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1806 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1807
Nate Begeman44728a72005-09-19 22:34:01 +00001808 // fold select_cc lhs, rhs, x, x, cc -> x
1809 if (N2 == N3)
1810 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001811
Chris Lattner5f42a242006-09-20 06:19:26 +00001812 // Determine if the condition we're dealing with is constant
1813 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
1814
1815 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
1816 if (SCCC->getValue())
1817 return N2; // cond always true -> true val
1818 else
1819 return N3; // cond always false -> false val
1820 }
1821
1822 // Fold to a simpler select_cc
1823 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
1824 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
1825 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
1826 SCC.getOperand(2));
1827
Chris Lattner40c62d52005-10-18 06:04:22 +00001828 // If we can fold this based on the true/false value, do so.
1829 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00001830 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00001831
Nate Begeman44728a72005-09-19 22:34:01 +00001832 // fold select_cc into other things, such as min/max/abs
1833 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001834}
1835
1836SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1837 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1838 cast<CondCodeSDNode>(N->getOperand(2))->get());
1839}
1840
Nate Begeman83e75ec2005-09-06 04:43:02 +00001841SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001842 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001843 MVT::ValueType VT = N->getValueType(0);
1844
Nate Begeman1d4d4142005-09-01 00:19:25 +00001845 // fold (sext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001846 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001847 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00001848
Nate Begeman1d4d4142005-09-01 00:19:25 +00001849 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001850 // fold (sext (aext x)) -> (sext x)
1851 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001852 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00001853
Chris Lattner6007b842006-09-21 06:00:20 +00001854 // fold (sext (truncate x)) -> (sextinreg x).
1855 if (N0.getOpcode() == ISD::TRUNCATE &&
Chris Lattnerbf370872006-09-21 06:17:39 +00001856 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
1857 N0.getValueType()))) {
Chris Lattner6007b842006-09-21 06:00:20 +00001858 SDOperand Op = N0.getOperand(0);
1859 if (Op.getValueType() < VT) {
1860 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
1861 } else if (Op.getValueType() > VT) {
1862 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
1863 }
1864 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001865 DAG.getValueType(N0.getValueType()));
Chris Lattner6007b842006-09-21 06:00:20 +00001866 }
Chris Lattner310b5782006-05-06 23:06:26 +00001867
Evan Cheng110dec22005-12-14 02:19:23 +00001868 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001869 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1870 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001871 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1872 N0.getOperand(1), N0.getOperand(2),
1873 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001874 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001875 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1876 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001877 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00001878 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001879
1880 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1881 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1882 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1883 N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001884 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1885 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1886 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001887 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001888 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1889 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001890 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001891 }
1892
Nate Begeman83e75ec2005-09-06 04:43:02 +00001893 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001894}
1895
Nate Begeman83e75ec2005-09-06 04:43:02 +00001896SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001897 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001898 MVT::ValueType VT = N->getValueType(0);
1899
Nate Begeman1d4d4142005-09-01 00:19:25 +00001900 // fold (zext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001901 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001902 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001903 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001904 // fold (zext (aext x)) -> (zext x)
1905 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001906 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00001907
1908 // fold (zext (truncate x)) -> (and x, mask)
1909 if (N0.getOpcode() == ISD::TRUNCATE &&
1910 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
1911 SDOperand Op = N0.getOperand(0);
1912 if (Op.getValueType() < VT) {
1913 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
1914 } else if (Op.getValueType() > VT) {
1915 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
1916 }
1917 return DAG.getZeroExtendInReg(Op, N0.getValueType());
1918 }
1919
Chris Lattner111c2282006-09-21 06:14:31 +00001920 // fold (zext (and (trunc x), cst)) -> (and x, cst).
1921 if (N0.getOpcode() == ISD::AND &&
1922 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1923 N0.getOperand(1).getOpcode() == ISD::Constant) {
1924 SDOperand X = N0.getOperand(0).getOperand(0);
1925 if (X.getValueType() < VT) {
1926 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
1927 } else if (X.getValueType() > VT) {
1928 X = DAG.getNode(ISD::TRUNCATE, VT, X);
1929 }
1930 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1931 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
1932 }
1933
Evan Cheng110dec22005-12-14 02:19:23 +00001934 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001935 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1936 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001937 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1938 N0.getOperand(1), N0.getOperand(2),
1939 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001940 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001941 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1942 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001943 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00001944 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001945
1946 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1947 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1948 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1949 N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001950 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1951 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1952 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001953 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001954 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1955 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001956 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001957 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001958 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001959}
1960
Chris Lattner5ffc0662006-05-05 05:58:59 +00001961SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
1962 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001963 MVT::ValueType VT = N->getValueType(0);
1964
1965 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001966 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00001967 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
1968 // fold (aext (aext x)) -> (aext x)
1969 // fold (aext (zext x)) -> (zext x)
1970 // fold (aext (sext x)) -> (sext x)
1971 if (N0.getOpcode() == ISD::ANY_EXTEND ||
1972 N0.getOpcode() == ISD::ZERO_EXTEND ||
1973 N0.getOpcode() == ISD::SIGN_EXTEND)
1974 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
1975
Chris Lattner84750582006-09-20 06:29:17 +00001976 // fold (aext (truncate x))
1977 if (N0.getOpcode() == ISD::TRUNCATE) {
1978 SDOperand TruncOp = N0.getOperand(0);
1979 if (TruncOp.getValueType() == VT)
1980 return TruncOp; // x iff x size == zext size.
1981 if (TruncOp.getValueType() > VT)
1982 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
1983 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
1984 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00001985
1986 // fold (aext (and (trunc x), cst)) -> (and x, cst).
1987 if (N0.getOpcode() == ISD::AND &&
1988 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1989 N0.getOperand(1).getOpcode() == ISD::Constant) {
1990 SDOperand X = N0.getOperand(0).getOperand(0);
1991 if (X.getValueType() < VT) {
1992 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
1993 } else if (X.getValueType() > VT) {
1994 X = DAG.getNode(ISD::TRUNCATE, VT, X);
1995 }
1996 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1997 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
1998 }
1999
Chris Lattner5ffc0662006-05-05 05:58:59 +00002000 // fold (aext (load x)) -> (aext (truncate (extload x)))
2001 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
2002 (!AfterLegalize||TLI.isOperationLegal(ISD::EXTLOAD, N0.getValueType()))) {
2003 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0),
2004 N0.getOperand(1), N0.getOperand(2),
2005 N0.getValueType());
2006 CombineTo(N, ExtLoad);
2007 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2008 ExtLoad.getValue(1));
2009 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2010 }
2011
2012 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2013 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2014 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
2015 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD ||
2016 N0.getOpcode() == ISD::SEXTLOAD) &&
2017 N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00002018 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
2019 SDOperand ExtLoad = DAG.getExtLoad(N0.getOpcode(), VT, N0.getOperand(0),
2020 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002021 CombineTo(N, ExtLoad);
2022 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2023 ExtLoad.getValue(1));
2024 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2025 }
2026 return SDOperand();
2027}
2028
2029
Nate Begeman83e75ec2005-09-06 04:43:02 +00002030SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002031 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002032 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002033 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002034 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00002035 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002036
Nate Begeman1d4d4142005-09-01 00:19:25 +00002037 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00002038 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00002039 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00002040
Chris Lattner541a24f2006-05-06 22:43:44 +00002041 // If the input is already sign extended, just drop the extension.
Chris Lattneree4ea922006-05-06 09:30:03 +00002042 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
2043 return N0;
2044
Nate Begeman646d7e22005-09-02 21:18:40 +00002045 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
2046 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2047 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00002048 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002049 }
Chris Lattner4b37e872006-05-08 21:18:59 +00002050
Nate Begeman07ed4172005-10-10 21:26:48 +00002051 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00002052 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00002053 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00002054
2055 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
2056 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
2057 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
2058 if (N0.getOpcode() == ISD::SRL) {
2059 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2060 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
2061 // We can turn this into an SRA iff the input to the SRL is already sign
2062 // extended enough.
2063 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
2064 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
2065 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
2066 }
2067 }
2068
Nate Begemanded49632005-10-13 03:11:28 +00002069 // fold (sext_inreg (extload x)) -> (sextload x)
2070 if (N0.getOpcode() == ISD::EXTLOAD &&
2071 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00002072 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00002073 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
2074 N0.getOperand(1), N0.getOperand(2),
2075 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002076 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002077 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002078 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002079 }
2080 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00002081 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00002082 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00002083 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00002084 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
2085 N0.getOperand(1), N0.getOperand(2),
2086 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002087 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002088 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002089 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002090 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002091 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002092}
2093
Nate Begeman83e75ec2005-09-06 04:43:02 +00002094SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002095 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002096 MVT::ValueType VT = N->getValueType(0);
2097
2098 // noop truncate
2099 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002100 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002101 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002102 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002103 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002104 // fold (truncate (truncate x)) -> (truncate x)
2105 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002106 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002107 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00002108 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2109 N0.getOpcode() == ISD::ANY_EXTEND) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002110 if (N0.getValueType() < VT)
2111 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00002112 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002113 else if (N0.getValueType() > VT)
2114 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002115 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002116 else
2117 // if the source and dest are the same type, we can drop both the extend
2118 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002119 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002120 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002121 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00002122 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00002123 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
2124 "Cannot truncate to larger type!");
2125 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00002126 // For big endian targets, we need to add an offset to the pointer to load
2127 // the correct bytes. For little endian systems, we merely need to read
2128 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00002129 uint64_t PtrOff =
2130 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00002131 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
2132 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
2133 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00002134 AddToWorkList(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00002135 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00002136 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00002137 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002138 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002139 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002140 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002141}
2142
Chris Lattner94683772005-12-23 05:30:37 +00002143SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2144 SDOperand N0 = N->getOperand(0);
2145 MVT::ValueType VT = N->getValueType(0);
2146
2147 // If the input is a constant, let getNode() fold it.
2148 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2149 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2150 if (Res.Val != N) return Res;
2151 }
2152
Chris Lattnerc8547d82005-12-23 05:37:50 +00002153 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2154 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00002155
Chris Lattner57104102005-12-23 05:44:41 +00002156 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002157 // FIXME: These xforms need to know that the resultant load doesn't need a
2158 // higher alignment than the original!
2159 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00002160 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
2161 N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00002162 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00002163 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2164 Load.getValue(1));
2165 return Load;
2166 }
2167
Chris Lattner94683772005-12-23 05:30:37 +00002168 return SDOperand();
2169}
2170
Chris Lattner6258fb22006-04-02 02:53:43 +00002171SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2172 SDOperand N0 = N->getOperand(0);
2173 MVT::ValueType VT = N->getValueType(0);
2174
2175 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2176 // First check to see if this is all constant.
2177 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2178 VT == MVT::Vector) {
2179 bool isSimple = true;
2180 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2181 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2182 N0.getOperand(i).getOpcode() != ISD::Constant &&
2183 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2184 isSimple = false;
2185 break;
2186 }
2187
Chris Lattner97c20732006-04-03 17:29:28 +00002188 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2189 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002190 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2191 }
2192 }
2193
2194 return SDOperand();
2195}
2196
2197/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2198/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2199/// destination element value type.
2200SDOperand DAGCombiner::
2201ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2202 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2203
2204 // If this is already the right type, we're done.
2205 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2206
2207 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2208 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2209
2210 // If this is a conversion of N elements of one type to N elements of another
2211 // type, convert each element. This handles FP<->INT cases.
2212 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002213 SmallVector<SDOperand, 8> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00002214 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002215 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002216 AddToWorkList(Ops.back().Val);
2217 }
Chris Lattner6258fb22006-04-02 02:53:43 +00002218 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2219 Ops.push_back(DAG.getValueType(DstEltVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002220 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002221 }
2222
2223 // Otherwise, we're growing or shrinking the elements. To avoid having to
2224 // handle annoying details of growing/shrinking FP values, we convert them to
2225 // int first.
2226 if (MVT::isFloatingPoint(SrcEltVT)) {
2227 // Convert the input float vector to a int vector where the elements are the
2228 // same sizes.
2229 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2230 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2231 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2232 SrcEltVT = IntVT;
2233 }
2234
2235 // Now we know the input is an integer vector. If the output is a FP type,
2236 // convert to integer first, then to FP of the right size.
2237 if (MVT::isFloatingPoint(DstEltVT)) {
2238 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2239 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2240 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2241
2242 // Next, convert to FP elements of the same size.
2243 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2244 }
2245
2246 // Okay, we know the src/dst types are both integers of differing types.
2247 // Handling growing first.
2248 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2249 if (SrcBitSize < DstBitSize) {
2250 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2251
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002252 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002253 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2254 i += NumInputsPerOutput) {
2255 bool isLE = TLI.isLittleEndian();
2256 uint64_t NewBits = 0;
2257 bool EltIsUndef = true;
2258 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2259 // Shift the previously computed bits over.
2260 NewBits <<= SrcBitSize;
2261 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2262 if (Op.getOpcode() == ISD::UNDEF) continue;
2263 EltIsUndef = false;
2264
2265 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2266 }
2267
2268 if (EltIsUndef)
2269 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2270 else
2271 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2272 }
2273
2274 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2275 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002276 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002277 }
2278
2279 // Finally, this must be the case where we are shrinking elements: each input
2280 // turns into multiple outputs.
2281 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002282 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002283 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2284 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2285 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2286 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2287 continue;
2288 }
2289 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2290
2291 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2292 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2293 OpVal >>= DstBitSize;
2294 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2295 }
2296
2297 // For big endian targets, swap the order of the pieces of each element.
2298 if (!TLI.isLittleEndian())
2299 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2300 }
2301 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2302 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002303 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002304}
2305
2306
2307
Chris Lattner01b3d732005-09-28 22:28:18 +00002308SDOperand DAGCombiner::visitFADD(SDNode *N) {
2309 SDOperand N0 = N->getOperand(0);
2310 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002311 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2312 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002313 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002314
2315 // fold (fadd c1, c2) -> c1+c2
2316 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002317 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002318 // canonicalize constant to RHS
2319 if (N0CFP && !N1CFP)
2320 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002321 // fold (A + (-B)) -> A-B
2322 if (N1.getOpcode() == ISD::FNEG)
2323 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002324 // fold ((-A) + B) -> B-A
2325 if (N0.getOpcode() == ISD::FNEG)
2326 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002327 return SDOperand();
2328}
2329
2330SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2331 SDOperand N0 = N->getOperand(0);
2332 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002333 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2334 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002335 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002336
2337 // fold (fsub c1, c2) -> c1-c2
2338 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002339 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002340 // fold (A-(-B)) -> A+B
2341 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00002342 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002343 return SDOperand();
2344}
2345
2346SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2347 SDOperand N0 = N->getOperand(0);
2348 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002349 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2350 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002351 MVT::ValueType VT = N->getValueType(0);
2352
Nate Begeman11af4ea2005-10-17 20:40:11 +00002353 // fold (fmul c1, c2) -> c1*c2
2354 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002355 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002356 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002357 if (N0CFP && !N1CFP)
2358 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002359 // fold (fmul X, 2.0) -> (fadd X, X)
2360 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2361 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002362 return SDOperand();
2363}
2364
2365SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2366 SDOperand N0 = N->getOperand(0);
2367 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002368 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2369 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002370 MVT::ValueType VT = N->getValueType(0);
2371
Nate Begemana148d982006-01-18 22:35:16 +00002372 // fold (fdiv c1, c2) -> c1/c2
2373 if (N0CFP && N1CFP)
2374 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002375 return SDOperand();
2376}
2377
2378SDOperand DAGCombiner::visitFREM(SDNode *N) {
2379 SDOperand N0 = N->getOperand(0);
2380 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002381 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2382 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002383 MVT::ValueType VT = N->getValueType(0);
2384
Nate Begemana148d982006-01-18 22:35:16 +00002385 // fold (frem c1, c2) -> fmod(c1,c2)
2386 if (N0CFP && N1CFP)
2387 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002388 return SDOperand();
2389}
2390
Chris Lattner12d83032006-03-05 05:30:57 +00002391SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2392 SDOperand N0 = N->getOperand(0);
2393 SDOperand N1 = N->getOperand(1);
2394 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2395 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2396 MVT::ValueType VT = N->getValueType(0);
2397
2398 if (N0CFP && N1CFP) // Constant fold
2399 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2400
2401 if (N1CFP) {
2402 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2403 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2404 union {
2405 double d;
2406 int64_t i;
2407 } u;
2408 u.d = N1CFP->getValue();
2409 if (u.i >= 0)
2410 return DAG.getNode(ISD::FABS, VT, N0);
2411 else
2412 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2413 }
2414
2415 // copysign(fabs(x), y) -> copysign(x, y)
2416 // copysign(fneg(x), y) -> copysign(x, y)
2417 // copysign(copysign(x,z), y) -> copysign(x, y)
2418 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2419 N0.getOpcode() == ISD::FCOPYSIGN)
2420 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2421
2422 // copysign(x, abs(y)) -> abs(x)
2423 if (N1.getOpcode() == ISD::FABS)
2424 return DAG.getNode(ISD::FABS, VT, N0);
2425
2426 // copysign(x, copysign(y,z)) -> copysign(x, z)
2427 if (N1.getOpcode() == ISD::FCOPYSIGN)
2428 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2429
2430 // copysign(x, fp_extend(y)) -> copysign(x, y)
2431 // copysign(x, fp_round(y)) -> copysign(x, y)
2432 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2433 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2434
2435 return SDOperand();
2436}
2437
2438
Chris Lattner01b3d732005-09-28 22:28:18 +00002439
Nate Begeman83e75ec2005-09-06 04:43:02 +00002440SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002441 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002442 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002443 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002444
2445 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002446 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002447 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002448 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002449}
2450
Nate Begeman83e75ec2005-09-06 04:43:02 +00002451SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002452 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002453 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002454 MVT::ValueType VT = N->getValueType(0);
2455
Nate Begeman1d4d4142005-09-01 00:19:25 +00002456 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002457 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002458 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002459 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002460}
2461
Nate Begeman83e75ec2005-09-06 04:43:02 +00002462SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002463 SDOperand N0 = N->getOperand(0);
2464 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2465 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002466
2467 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002468 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002469 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002470 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002471}
2472
Nate Begeman83e75ec2005-09-06 04:43:02 +00002473SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002474 SDOperand N0 = N->getOperand(0);
2475 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2476 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002477
2478 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002479 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002480 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002481 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002482}
2483
Nate Begeman83e75ec2005-09-06 04:43:02 +00002484SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002485 SDOperand N0 = N->getOperand(0);
2486 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2487 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002488
2489 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002490 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002491 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002492
2493 // fold (fp_round (fp_extend x)) -> x
2494 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2495 return N0.getOperand(0);
2496
2497 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2498 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2499 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2500 AddToWorkList(Tmp.Val);
2501 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2502 }
2503
Nate Begeman83e75ec2005-09-06 04:43:02 +00002504 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002505}
2506
Nate Begeman83e75ec2005-09-06 04:43:02 +00002507SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002508 SDOperand N0 = N->getOperand(0);
2509 MVT::ValueType VT = N->getValueType(0);
2510 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002511 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002512
Nate Begeman1d4d4142005-09-01 00:19:25 +00002513 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002514 if (N0CFP) {
2515 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002516 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002517 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002518 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002519}
2520
Nate Begeman83e75ec2005-09-06 04:43:02 +00002521SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002522 SDOperand N0 = N->getOperand(0);
2523 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2524 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002525
2526 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002527 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002528 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00002529
2530 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
2531 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
2532 (!AfterLegalize||TLI.isOperationLegal(ISD::EXTLOAD, N0.getValueType()))) {
2533 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0),
2534 N0.getOperand(1), N0.getOperand(2),
2535 N0.getValueType());
2536 CombineTo(N, ExtLoad);
2537 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
2538 ExtLoad.getValue(1));
2539 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2540 }
2541
2542
Nate Begeman83e75ec2005-09-06 04:43:02 +00002543 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002544}
2545
Nate Begeman83e75ec2005-09-06 04:43:02 +00002546SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002547 SDOperand N0 = N->getOperand(0);
2548 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2549 MVT::ValueType VT = N->getValueType(0);
2550
2551 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002552 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002553 return DAG.getNode(ISD::FNEG, VT, N0);
2554 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002555 if (N0.getOpcode() == ISD::SUB)
2556 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002557 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002558 if (N0.getOpcode() == ISD::FNEG)
2559 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002560 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002561}
2562
Nate Begeman83e75ec2005-09-06 04:43:02 +00002563SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002564 SDOperand N0 = N->getOperand(0);
2565 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2566 MVT::ValueType VT = N->getValueType(0);
2567
Nate Begeman1d4d4142005-09-01 00:19:25 +00002568 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002569 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002570 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002571 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002572 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002573 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002574 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002575 // fold (fabs (fcopysign x, y)) -> (fabs x)
2576 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2577 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2578
Nate Begeman83e75ec2005-09-06 04:43:02 +00002579 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002580}
2581
Nate Begeman44728a72005-09-19 22:34:01 +00002582SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2583 SDOperand Chain = N->getOperand(0);
2584 SDOperand N1 = N->getOperand(1);
2585 SDOperand N2 = N->getOperand(2);
2586 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2587
2588 // never taken branch, fold to chain
2589 if (N1C && N1C->isNullValue())
2590 return Chain;
2591 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002592 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002593 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002594 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2595 // on the target.
2596 if (N1.getOpcode() == ISD::SETCC &&
2597 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2598 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2599 N1.getOperand(0), N1.getOperand(1), N2);
2600 }
Nate Begeman44728a72005-09-19 22:34:01 +00002601 return SDOperand();
2602}
2603
Chris Lattner3ea0b472005-10-05 06:47:48 +00002604// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2605//
Nate Begeman44728a72005-09-19 22:34:01 +00002606SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002607 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2608 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2609
2610 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002611 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2612 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2613
2614 // fold br_cc true, dest -> br dest (unconditional branch)
2615 if (SCCC && SCCC->getValue())
2616 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2617 N->getOperand(4));
2618 // fold br_cc false, dest -> unconditional fall through
2619 if (SCCC && SCCC->isNullValue())
2620 return N->getOperand(0);
2621 // fold to a simpler setcc
2622 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2623 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2624 Simp.getOperand(2), Simp.getOperand(0),
2625 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002626 return SDOperand();
2627}
2628
Chris Lattner01a22022005-10-10 22:04:48 +00002629SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2630 SDOperand Chain = N->getOperand(0);
2631 SDOperand Ptr = N->getOperand(1);
2632 SDOperand SrcValue = N->getOperand(2);
Chris Lattnere4b95392006-03-31 18:06:18 +00002633
2634 // If there are no uses of the loaded value, change uses of the chain value
2635 // into uses of the chain input (i.e. delete the dead load).
2636 if (N->hasNUsesOfValue(0, 0))
2637 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
Chris Lattner01a22022005-10-10 22:04:48 +00002638
2639 // If this load is directly stored, replace the load value with the stored
2640 // value.
2641 // TODO: Handle store large -> read small portion.
2642 // TODO: Handle TRUNCSTORE/EXTLOAD
2643 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2644 Chain.getOperand(1).getValueType() == N->getValueType(0))
2645 return CombineTo(N, Chain.getOperand(1), Chain);
2646
Jim Laskey3dd11702006-09-26 08:14:06 +00002647 // We can only move the load if it has a user of it's chain result. Otherwise
2648 // there is no place to attach it's old chain.
Jim Laskey172585b2006-09-26 07:37:42 +00002649 if (CombinerAA && hasChainUsers(N)) {
Jim Laskey279f0532006-09-25 16:29:54 +00002650 // Walk up chain skipping non-aliasing memory nodes.
2651 SDOperand BetterChain = FindBetterChain(N, Chain);
2652
2653 // If the there is a better chain.
2654 if (Chain != BetterChain) {
2655 // Replace the chain to void dependency.
2656 SDOperand ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
2657 SrcValue);
2658
Jim Laskey288af5e2006-09-25 19:32:58 +00002659 // Create token factor to keep chain around.
2660 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
2661 Chain, ReplLoad.getValue(1));
2662
2663 // Replace uses with load and token factor.
2664 CombineTo(N, ReplLoad.getValue(0), Token);
2665
Jim Laskey279f0532006-09-25 16:29:54 +00002666 return SDOperand(N, 0);
2667 }
2668 }
2669
Chris Lattner01a22022005-10-10 22:04:48 +00002670 return SDOperand();
2671}
2672
Chris Lattner29cd7db2006-03-31 18:10:41 +00002673/// visitXEXTLOAD - Handle EXTLOAD/ZEXTLOAD/SEXTLOAD.
2674SDOperand DAGCombiner::visitXEXTLOAD(SDNode *N) {
2675 SDOperand Chain = N->getOperand(0);
2676 SDOperand Ptr = N->getOperand(1);
2677 SDOperand SrcValue = N->getOperand(2);
2678 SDOperand EVT = N->getOperand(3);
2679
2680 // If there are no uses of the loaded value, change uses of the chain value
2681 // into uses of the chain input (i.e. delete the dead load).
2682 if (N->hasNUsesOfValue(0, 0))
2683 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
2684
2685 return SDOperand();
2686}
2687
Chris Lattner87514ca2005-10-10 22:31:19 +00002688SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2689 SDOperand Chain = N->getOperand(0);
2690 SDOperand Value = N->getOperand(1);
2691 SDOperand Ptr = N->getOperand(2);
2692 SDOperand SrcValue = N->getOperand(3);
2693
2694 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002695 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002696 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2697 // Make sure that these stores are the same value type:
2698 // FIXME: we really care that the second store is >= size of the first.
2699 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002700 // Create a new store of Value that replaces both stores.
2701 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002702 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2703 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002704 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2705 PrevStore->getOperand(0), Value, Ptr,
2706 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002707 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002708 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002709 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002710 }
2711
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002712 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002713 // FIXME: This needs to know that the resultant store does not need a
2714 // higher alignment than the original.
Jim Laskey14fbcbf2006-09-25 21:11:32 +00002715 if (0 && Value.getOpcode() == ISD::BIT_CONVERT) {
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002716 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2717 Ptr, SrcValue);
Jim Laskey279f0532006-09-25 16:29:54 +00002718 }
2719
2720 if (CombinerAA) {
Jim Laskey288af5e2006-09-25 19:32:58 +00002721 // If the store ptr is a frame index and the frame index has a use of one
2722 // and this is a return block, then the store is redundant.
2723 if (Ptr.hasOneUse() && isa<FrameIndexSDNode>(Ptr) &&
2724 DAG.getRoot().getOpcode() == ISD::RET) {
2725 return Chain;
2726 }
2727
Jim Laskey279f0532006-09-25 16:29:54 +00002728 // Walk up chain skipping non-aliasing memory nodes.
2729 SDOperand BetterChain = FindBetterChain(N, Chain);
2730
2731 // If the there is a better chain.
2732 if (Chain != BetterChain) {
2733 // Replace the chain to void dependency.
2734 SDOperand ReplStore = DAG.getNode(ISD::STORE, MVT::Other,
2735 BetterChain, Value, Ptr,
2736 SrcValue);
2737 // Create token to keep both nodes around.
2738 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
2739 Chain, ReplStore);
2740
2741 // Make sure we merge token factors.
2742 AddUsersToWorkList(N);
2743
2744 // Old chain needs to be cleaned up.
2745 AddToWorkList(Chain.Val);
2746
2747 return Token;
2748 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00002749 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002750
Chris Lattner87514ca2005-10-10 22:31:19 +00002751 return SDOperand();
2752}
2753
Chris Lattnerca242442006-03-19 01:27:56 +00002754SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2755 SDOperand InVec = N->getOperand(0);
2756 SDOperand InVal = N->getOperand(1);
2757 SDOperand EltNo = N->getOperand(2);
2758
2759 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2760 // vector with the inserted element.
2761 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2762 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002763 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002764 if (Elt < Ops.size())
2765 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002766 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
2767 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002768 }
2769
2770 return SDOperand();
2771}
2772
2773SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2774 SDOperand InVec = N->getOperand(0);
2775 SDOperand InVal = N->getOperand(1);
2776 SDOperand EltNo = N->getOperand(2);
2777 SDOperand NumElts = N->getOperand(3);
2778 SDOperand EltType = N->getOperand(4);
2779
2780 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2781 // vector with the inserted element.
2782 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2783 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002784 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002785 if (Elt < Ops.size()-2)
2786 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002787 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
2788 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002789 }
2790
2791 return SDOperand();
2792}
2793
Chris Lattnerd7648c82006-03-28 20:28:38 +00002794SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
2795 unsigned NumInScalars = N->getNumOperands()-2;
2796 SDOperand NumElts = N->getOperand(NumInScalars);
2797 SDOperand EltType = N->getOperand(NumInScalars+1);
2798
2799 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
2800 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
2801 // two distinct vectors, turn this into a shuffle node.
2802 SDOperand VecIn1, VecIn2;
2803 for (unsigned i = 0; i != NumInScalars; ++i) {
2804 // Ignore undef inputs.
2805 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
2806
2807 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
2808 // constant index, bail out.
2809 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
2810 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
2811 VecIn1 = VecIn2 = SDOperand(0, 0);
2812 break;
2813 }
2814
2815 // If the input vector type disagrees with the result of the vbuild_vector,
2816 // we can't make a shuffle.
2817 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
2818 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
2819 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
2820 VecIn1 = VecIn2 = SDOperand(0, 0);
2821 break;
2822 }
2823
2824 // Otherwise, remember this. We allow up to two distinct input vectors.
2825 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
2826 continue;
2827
2828 if (VecIn1.Val == 0) {
2829 VecIn1 = ExtractedFromVec;
2830 } else if (VecIn2.Val == 0) {
2831 VecIn2 = ExtractedFromVec;
2832 } else {
2833 // Too many inputs.
2834 VecIn1 = VecIn2 = SDOperand(0, 0);
2835 break;
2836 }
2837 }
2838
2839 // If everything is good, we can make a shuffle operation.
2840 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002841 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00002842 for (unsigned i = 0; i != NumInScalars; ++i) {
2843 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
2844 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2845 continue;
2846 }
2847
2848 SDOperand Extract = N->getOperand(i);
2849
2850 // If extracting from the first vector, just use the index directly.
2851 if (Extract.getOperand(0) == VecIn1) {
2852 BuildVecIndices.push_back(Extract.getOperand(1));
2853 continue;
2854 }
2855
2856 // Otherwise, use InIdx + VecSize
2857 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
2858 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
2859 }
2860
2861 // Add count and size info.
2862 BuildVecIndices.push_back(NumElts);
2863 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
2864
2865 // Return the new VVECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002866 SDOperand Ops[5];
2867 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00002868 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002869 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00002870 } else {
2871 // Use an undef vbuild_vector as input for the second operand.
2872 std::vector<SDOperand> UnOps(NumInScalars,
2873 DAG.getNode(ISD::UNDEF,
2874 cast<VTSDNode>(EltType)->getVT()));
2875 UnOps.push_back(NumElts);
2876 UnOps.push_back(EltType);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002877 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2878 &UnOps[0], UnOps.size());
2879 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00002880 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002881 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2882 &BuildVecIndices[0], BuildVecIndices.size());
2883 Ops[3] = NumElts;
2884 Ops[4] = EltType;
2885 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
Chris Lattnerd7648c82006-03-28 20:28:38 +00002886 }
2887
2888 return SDOperand();
2889}
2890
Chris Lattner66445d32006-03-28 22:11:53 +00002891SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002892 SDOperand ShufMask = N->getOperand(2);
2893 unsigned NumElts = ShufMask.getNumOperands();
2894
2895 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2896 bool isIdentity = true;
2897 for (unsigned i = 0; i != NumElts; ++i) {
2898 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2899 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2900 isIdentity = false;
2901 break;
2902 }
2903 }
2904 if (isIdentity) return N->getOperand(0);
2905
2906 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2907 isIdentity = true;
2908 for (unsigned i = 0; i != NumElts; ++i) {
2909 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2910 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2911 isIdentity = false;
2912 break;
2913 }
2914 }
2915 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00002916
2917 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
2918 // needed at all.
2919 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00002920 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002921 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00002922 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002923 for (unsigned i = 0; i != NumElts; ++i)
2924 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
2925 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
2926 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00002927 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00002928 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00002929 BaseIdx = Idx;
2930 } else {
2931 if (BaseIdx != Idx)
2932 isSplat = false;
2933 if (VecNum != V) {
2934 isUnary = false;
2935 break;
2936 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00002937 }
2938 }
2939
2940 SDOperand N0 = N->getOperand(0);
2941 SDOperand N1 = N->getOperand(1);
2942 // Normalize unary shuffle so the RHS is undef.
2943 if (isUnary && VecNum == 1)
2944 std::swap(N0, N1);
2945
Evan Cheng917ec982006-07-21 08:25:53 +00002946 // If it is a splat, check if the argument vector is a build_vector with
2947 // all scalar elements the same.
2948 if (isSplat) {
2949 SDNode *V = N0.Val;
2950 if (V->getOpcode() == ISD::BIT_CONVERT)
2951 V = V->getOperand(0).Val;
2952 if (V->getOpcode() == ISD::BUILD_VECTOR) {
2953 unsigned NumElems = V->getNumOperands()-2;
2954 if (NumElems > BaseIdx) {
2955 SDOperand Base;
2956 bool AllSame = true;
2957 for (unsigned i = 0; i != NumElems; ++i) {
2958 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
2959 Base = V->getOperand(i);
2960 break;
2961 }
2962 }
2963 // Splat of <u, u, u, u>, return <u, u, u, u>
2964 if (!Base.Val)
2965 return N0;
2966 for (unsigned i = 0; i != NumElems; ++i) {
2967 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
2968 V->getOperand(i) != Base) {
2969 AllSame = false;
2970 break;
2971 }
2972 }
2973 // Splat of <x, x, x, x>, return <x, x, x, x>
2974 if (AllSame)
2975 return N0;
2976 }
2977 }
2978 }
2979
Evan Chenge7bec0d2006-07-20 22:44:41 +00002980 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
2981 // into an undef.
2982 if (isUnary || N0 == N1) {
2983 if (N0.getOpcode() == ISD::UNDEF)
Evan Chengc04766a2006-04-06 23:20:43 +00002984 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00002985 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2986 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002987 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00002988 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00002989 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2990 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2991 MappedOps.push_back(ShufMask.getOperand(i));
2992 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00002993 unsigned NewIdx =
2994 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2995 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00002996 }
2997 }
2998 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002999 &MappedOps[0], MappedOps.size());
Chris Lattner3e104b12006-04-08 04:15:24 +00003000 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00003001 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
Evan Chenge7bec0d2006-07-20 22:44:41 +00003002 N0,
Chris Lattner66445d32006-03-28 22:11:53 +00003003 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
3004 ShufMask);
3005 }
3006
3007 return SDOperand();
3008}
3009
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003010SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
3011 SDOperand ShufMask = N->getOperand(2);
3012 unsigned NumElts = ShufMask.getNumOperands()-2;
3013
3014 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3015 bool isIdentity = true;
3016 for (unsigned i = 0; i != NumElts; ++i) {
3017 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3018 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3019 isIdentity = false;
3020 break;
3021 }
3022 }
3023 if (isIdentity) return N->getOperand(0);
3024
3025 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3026 isIdentity = true;
3027 for (unsigned i = 0; i != NumElts; ++i) {
3028 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3029 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3030 isIdentity = false;
3031 break;
3032 }
3033 }
3034 if (isIdentity) return N->getOperand(1);
3035
Evan Chenge7bec0d2006-07-20 22:44:41 +00003036 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3037 // needed at all.
3038 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003039 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003040 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003041 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003042 for (unsigned i = 0; i != NumElts; ++i)
3043 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3044 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3045 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003046 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003047 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003048 BaseIdx = Idx;
3049 } else {
3050 if (BaseIdx != Idx)
3051 isSplat = false;
3052 if (VecNum != V) {
3053 isUnary = false;
3054 break;
3055 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003056 }
3057 }
3058
3059 SDOperand N0 = N->getOperand(0);
3060 SDOperand N1 = N->getOperand(1);
3061 // Normalize unary shuffle so the RHS is undef.
3062 if (isUnary && VecNum == 1)
3063 std::swap(N0, N1);
3064
Evan Cheng917ec982006-07-21 08:25:53 +00003065 // If it is a splat, check if the argument vector is a build_vector with
3066 // all scalar elements the same.
3067 if (isSplat) {
3068 SDNode *V = N0.Val;
3069 if (V->getOpcode() == ISD::VBIT_CONVERT)
3070 V = V->getOperand(0).Val;
3071 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
3072 unsigned NumElems = V->getNumOperands()-2;
3073 if (NumElems > BaseIdx) {
3074 SDOperand Base;
3075 bool AllSame = true;
3076 for (unsigned i = 0; i != NumElems; ++i) {
3077 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3078 Base = V->getOperand(i);
3079 break;
3080 }
3081 }
3082 // Splat of <u, u, u, u>, return <u, u, u, u>
3083 if (!Base.Val)
3084 return N0;
3085 for (unsigned i = 0; i != NumElems; ++i) {
3086 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3087 V->getOperand(i) != Base) {
3088 AllSame = false;
3089 break;
3090 }
3091 }
3092 // Splat of <x, x, x, x>, return <x, x, x, x>
3093 if (AllSame)
3094 return N0;
3095 }
3096 }
3097 }
3098
Evan Chenge7bec0d2006-07-20 22:44:41 +00003099 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3100 // into an undef.
3101 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00003102 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3103 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003104 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00003105 for (unsigned i = 0; i != NumElts; ++i) {
3106 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3107 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3108 MappedOps.push_back(ShufMask.getOperand(i));
3109 } else {
3110 unsigned NewIdx =
3111 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3112 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
3113 }
3114 }
3115 // Add the type/#elts values.
3116 MappedOps.push_back(ShufMask.getOperand(NumElts));
3117 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
3118
3119 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003120 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003121 AddToWorkList(ShufMask.Val);
3122
3123 // Build the undef vector.
3124 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
3125 for (unsigned i = 0; i != NumElts; ++i)
3126 MappedOps[i] = UDVal;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003127 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
3128 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003129 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3130 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003131
3132 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
Evan Chenge7bec0d2006-07-20 22:44:41 +00003133 N0, UDVal, ShufMask,
Chris Lattner17614ea2006-04-08 05:34:25 +00003134 MappedOps[NumElts], MappedOps[NumElts+1]);
3135 }
3136
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003137 return SDOperand();
3138}
3139
Evan Cheng44f1f092006-04-20 08:56:16 +00003140/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
3141/// a VAND to a vector_shuffle with the destination vector and a zero vector.
3142/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
3143/// vector_shuffle V, Zero, <0, 4, 2, 4>
3144SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
3145 SDOperand LHS = N->getOperand(0);
3146 SDOperand RHS = N->getOperand(1);
3147 if (N->getOpcode() == ISD::VAND) {
3148 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
3149 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
3150 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
3151 RHS = RHS.getOperand(0);
3152 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
3153 std::vector<SDOperand> IdxOps;
3154 unsigned NumOps = RHS.getNumOperands();
3155 unsigned NumElts = NumOps-2;
3156 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
3157 for (unsigned i = 0; i != NumElts; ++i) {
3158 SDOperand Elt = RHS.getOperand(i);
3159 if (!isa<ConstantSDNode>(Elt))
3160 return SDOperand();
3161 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
3162 IdxOps.push_back(DAG.getConstant(i, EVT));
3163 else if (cast<ConstantSDNode>(Elt)->isNullValue())
3164 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
3165 else
3166 return SDOperand();
3167 }
3168
3169 // Let's see if the target supports this vector_shuffle.
3170 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
3171 return SDOperand();
3172
3173 // Return the new VVECTOR_SHUFFLE node.
3174 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
3175 SDOperand EVTNode = DAG.getValueType(EVT);
3176 std::vector<SDOperand> Ops;
Chris Lattner516b9622006-09-14 20:50:57 +00003177 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode,
3178 EVTNode);
Evan Cheng44f1f092006-04-20 08:56:16 +00003179 Ops.push_back(LHS);
3180 AddToWorkList(LHS.Val);
3181 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
3182 ZeroOps.push_back(NumEltsNode);
3183 ZeroOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003184 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3185 &ZeroOps[0], ZeroOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003186 IdxOps.push_back(NumEltsNode);
3187 IdxOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003188 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3189 &IdxOps[0], IdxOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003190 Ops.push_back(NumEltsNode);
3191 Ops.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003192 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
3193 &Ops[0], Ops.size());
Evan Cheng44f1f092006-04-20 08:56:16 +00003194 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
3195 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
3196 DstVecSize, DstVecEVT);
3197 }
3198 return Result;
3199 }
3200 }
3201 return SDOperand();
3202}
3203
Chris Lattneredab1b92006-04-02 03:25:57 +00003204/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
3205/// the scalar operation of the vop if it is operating on an integer vector
3206/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
3207SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
3208 ISD::NodeType FPOp) {
3209 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
3210 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
3211 SDOperand LHS = N->getOperand(0);
3212 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00003213 SDOperand Shuffle = XformToShuffleWithZero(N);
3214 if (Shuffle.Val) return Shuffle;
3215
Chris Lattneredab1b92006-04-02 03:25:57 +00003216 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
3217 // this operation.
3218 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
3219 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003220 SmallVector<SDOperand, 8> Ops;
Chris Lattneredab1b92006-04-02 03:25:57 +00003221 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
3222 SDOperand LHSOp = LHS.getOperand(i);
3223 SDOperand RHSOp = RHS.getOperand(i);
3224 // If these two elements can't be folded, bail out.
3225 if ((LHSOp.getOpcode() != ISD::UNDEF &&
3226 LHSOp.getOpcode() != ISD::Constant &&
3227 LHSOp.getOpcode() != ISD::ConstantFP) ||
3228 (RHSOp.getOpcode() != ISD::UNDEF &&
3229 RHSOp.getOpcode() != ISD::Constant &&
3230 RHSOp.getOpcode() != ISD::ConstantFP))
3231 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00003232 // Can't fold divide by zero.
3233 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
3234 if ((RHSOp.getOpcode() == ISD::Constant &&
3235 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
3236 (RHSOp.getOpcode() == ISD::ConstantFP &&
3237 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
3238 break;
3239 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003240 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00003241 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00003242 assert((Ops.back().getOpcode() == ISD::UNDEF ||
3243 Ops.back().getOpcode() == ISD::Constant ||
3244 Ops.back().getOpcode() == ISD::ConstantFP) &&
3245 "Scalar binop didn't fold!");
3246 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003247
3248 if (Ops.size() == LHS.getNumOperands()-2) {
3249 Ops.push_back(*(LHS.Val->op_end()-2));
3250 Ops.push_back(*(LHS.Val->op_end()-1));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003251 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003252 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003253 }
3254
3255 return SDOperand();
3256}
3257
Nate Begeman44728a72005-09-19 22:34:01 +00003258SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00003259 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
3260
3261 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
3262 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3263 // If we got a simplified select_cc node back from SimplifySelectCC, then
3264 // break it down into a new SETCC node, and a new SELECT node, and then return
3265 // the SELECT node, since we were called with a SELECT node.
3266 if (SCC.Val) {
3267 // Check to see if we got a select_cc back (to turn into setcc/select).
3268 // Otherwise, just return whatever node we got back, like fabs.
3269 if (SCC.getOpcode() == ISD::SELECT_CC) {
3270 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
3271 SCC.getOperand(0), SCC.getOperand(1),
3272 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00003273 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003274 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
3275 SCC.getOperand(3), SETCC);
3276 }
3277 return SCC;
3278 }
Nate Begeman44728a72005-09-19 22:34:01 +00003279 return SDOperand();
3280}
3281
Chris Lattner40c62d52005-10-18 06:04:22 +00003282/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
3283/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00003284/// select. Callers of this should assume that TheSelect is deleted if this
3285/// returns true. As such, they should return the appropriate thing (e.g. the
3286/// node) back to the top-level of the DAG combiner loop to avoid it being
3287/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00003288///
3289bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
3290 SDOperand RHS) {
3291
3292 // If this is a select from two identical things, try to pull the operation
3293 // through the select.
3294 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
3295#if 0
3296 std::cerr << "SELECT: ["; LHS.Val->dump();
3297 std::cerr << "] ["; RHS.Val->dump();
3298 std::cerr << "]\n";
3299#endif
3300
3301 // If this is a load and the token chain is identical, replace the select
3302 // of two loads with a load through a select of the address to load from.
3303 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
3304 // constants have been dropped into the constant pool.
3305 if ((LHS.getOpcode() == ISD::LOAD ||
3306 LHS.getOpcode() == ISD::EXTLOAD ||
3307 LHS.getOpcode() == ISD::ZEXTLOAD ||
3308 LHS.getOpcode() == ISD::SEXTLOAD) &&
3309 // Token chains must be identical.
3310 LHS.getOperand(0) == RHS.getOperand(0) &&
3311 // If this is an EXTLOAD, the VT's must match.
3312 (LHS.getOpcode() == ISD::LOAD ||
3313 LHS.getOperand(3) == RHS.getOperand(3))) {
3314 // FIXME: this conflates two src values, discarding one. This is not
3315 // the right thing to do, but nothing uses srcvalues now. When they do,
3316 // turn SrcValue into a list of locations.
3317 SDOperand Addr;
3318 if (TheSelect->getOpcode() == ISD::SELECT)
3319 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
3320 TheSelect->getOperand(0), LHS.getOperand(1),
3321 RHS.getOperand(1));
3322 else
3323 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
3324 TheSelect->getOperand(0),
3325 TheSelect->getOperand(1),
3326 LHS.getOperand(1), RHS.getOperand(1),
3327 TheSelect->getOperand(4));
3328
3329 SDOperand Load;
3330 if (LHS.getOpcode() == ISD::LOAD)
3331 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
3332 Addr, LHS.getOperand(2));
3333 else
3334 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
3335 LHS.getOperand(0), Addr, LHS.getOperand(2),
3336 cast<VTSDNode>(LHS.getOperand(3))->getVT());
3337 // Users of the select now use the result of the load.
3338 CombineTo(TheSelect, Load);
3339
3340 // Users of the old loads now use the new load's chain. We know the
3341 // old-load value is dead now.
3342 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
3343 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
3344 return true;
3345 }
3346 }
3347
3348 return false;
3349}
3350
Nate Begeman44728a72005-09-19 22:34:01 +00003351SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
3352 SDOperand N2, SDOperand N3,
3353 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00003354
3355 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00003356 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
3357 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
3358 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
3359
3360 // Determine if the condition we're dealing with is constant
3361 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
3362 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
3363
3364 // fold select_cc true, x, y -> x
3365 if (SCCC && SCCC->getValue())
3366 return N2;
3367 // fold select_cc false, x, y -> y
3368 if (SCCC && SCCC->getValue() == 0)
3369 return N3;
3370
3371 // Check to see if we can simplify the select into an fabs node
3372 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
3373 // Allow either -0.0 or 0.0
3374 if (CFP->getValue() == 0.0) {
3375 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
3376 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
3377 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
3378 N2 == N3.getOperand(0))
3379 return DAG.getNode(ISD::FABS, VT, N0);
3380
3381 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
3382 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3383 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3384 N2.getOperand(0) == N3)
3385 return DAG.getNode(ISD::FABS, VT, N3);
3386 }
3387 }
3388
3389 // Check to see if we can perform the "gzip trick", transforming
3390 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00003391 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00003392 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00003393 MVT::isInteger(N2.getValueType()) &&
3394 (N1C->isNullValue() || // (a < 0) ? b : 0
3395 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00003396 MVT::ValueType XType = N0.getValueType();
3397 MVT::ValueType AType = N2.getValueType();
3398 if (XType >= AType) {
3399 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00003400 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00003401 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3402 unsigned ShCtV = Log2_64(N2C->getValue());
3403 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3404 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3405 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00003406 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003407 if (XType > AType) {
3408 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003409 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003410 }
3411 return DAG.getNode(ISD::AND, AType, Shift, N2);
3412 }
3413 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3414 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3415 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003416 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003417 if (XType > AType) {
3418 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003419 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003420 }
3421 return DAG.getNode(ISD::AND, AType, Shift, N2);
3422 }
3423 }
Nate Begeman07ed4172005-10-10 21:26:48 +00003424
3425 // fold select C, 16, 0 -> shl C, 4
3426 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3427 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3428 // Get a SetCC of the condition
3429 // FIXME: Should probably make sure that setcc is legal if we ever have a
3430 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00003431 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00003432 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00003433 if (AfterLegalize) {
3434 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003435 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00003436 } else {
3437 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003438 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00003439 }
Chris Lattner5750df92006-03-01 04:03:14 +00003440 AddToWorkList(SCC.Val);
3441 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00003442 // shl setcc result by log2 n2c
3443 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3444 DAG.getConstant(Log2_64(N2C->getValue()),
3445 TLI.getShiftAmountTy()));
3446 }
3447
Nate Begemanf845b452005-10-08 00:29:44 +00003448 // Check to see if this is the equivalent of setcc
3449 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3450 // otherwise, go ahead with the folds.
3451 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3452 MVT::ValueType XType = N0.getValueType();
3453 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3454 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3455 if (Res.getValueType() != VT)
3456 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3457 return Res;
3458 }
3459
3460 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3461 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3462 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3463 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3464 return DAG.getNode(ISD::SRL, XType, Ctlz,
3465 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3466 TLI.getShiftAmountTy()));
3467 }
3468 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3469 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3470 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3471 N0);
3472 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3473 DAG.getConstant(~0ULL, XType));
3474 return DAG.getNode(ISD::SRL, XType,
3475 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3476 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3477 TLI.getShiftAmountTy()));
3478 }
3479 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3480 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3481 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3482 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3483 TLI.getShiftAmountTy()));
3484 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3485 }
3486 }
3487
3488 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3489 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3490 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3491 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3492 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3493 MVT::ValueType XType = N0.getValueType();
3494 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3495 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3496 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3497 TLI.getShiftAmountTy()));
3498 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003499 AddToWorkList(Shift.Val);
3500 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003501 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3502 }
3503 }
3504 }
3505
Nate Begeman44728a72005-09-19 22:34:01 +00003506 return SDOperand();
3507}
3508
Nate Begeman452d7be2005-09-16 00:54:12 +00003509SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00003510 SDOperand N1, ISD::CondCode Cond,
3511 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003512 // These setcc operations always fold.
3513 switch (Cond) {
3514 default: break;
3515 case ISD::SETFALSE:
3516 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3517 case ISD::SETTRUE:
3518 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3519 }
3520
3521 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3522 uint64_t C1 = N1C->getValue();
3523 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
3524 uint64_t C0 = N0C->getValue();
3525
3526 // Sign extend the operands if required
3527 if (ISD::isSignedIntSetCC(Cond)) {
3528 C0 = N0C->getSignExtended();
3529 C1 = N1C->getSignExtended();
3530 }
3531
3532 switch (Cond) {
3533 default: assert(0 && "Unknown integer setcc!");
3534 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3535 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3536 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
3537 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
3538 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
3539 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
3540 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
3541 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
3542 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
3543 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
3544 }
3545 } else {
Chris Lattner5f42a242006-09-20 06:19:26 +00003546 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
3547 // equality comparison, then we're just comparing whether X itself is
3548 // zero.
3549 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
3550 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
3551 N0.getOperand(1).getOpcode() == ISD::Constant) {
3552 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
3553 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3554 ShAmt == Log2_32(MVT::getSizeInBits(N0.getValueType()))) {
3555 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
3556 // (srl (ctlz x), 5) == 0 -> X != 0
3557 // (srl (ctlz x), 5) != 1 -> X != 0
3558 Cond = ISD::SETNE;
3559 } else {
3560 // (srl (ctlz x), 5) != 0 -> X == 0
3561 // (srl (ctlz x), 5) == 1 -> X == 0
3562 Cond = ISD::SETEQ;
3563 }
3564 SDOperand Zero = DAG.getConstant(0, N0.getValueType());
3565 return DAG.getSetCC(VT, N0.getOperand(0).getOperand(0),
3566 Zero, Cond);
3567 }
3568 }
3569
Nate Begeman452d7be2005-09-16 00:54:12 +00003570 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3571 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3572 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3573
3574 // If the comparison constant has bits in the upper part, the
3575 // zero-extended value could never match.
3576 if (C1 & (~0ULL << InSize)) {
3577 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3578 switch (Cond) {
3579 case ISD::SETUGT:
3580 case ISD::SETUGE:
3581 case ISD::SETEQ: return DAG.getConstant(0, VT);
3582 case ISD::SETULT:
3583 case ISD::SETULE:
3584 case ISD::SETNE: return DAG.getConstant(1, VT);
3585 case ISD::SETGT:
3586 case ISD::SETGE:
3587 // True if the sign bit of C1 is set.
3588 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3589 case ISD::SETLT:
3590 case ISD::SETLE:
3591 // True if the sign bit of C1 isn't set.
3592 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3593 default:
3594 break;
3595 }
3596 }
3597
3598 // Otherwise, we can perform the comparison with the low bits.
3599 switch (Cond) {
3600 case ISD::SETEQ:
3601 case ISD::SETNE:
3602 case ISD::SETUGT:
3603 case ISD::SETUGE:
3604 case ISD::SETULT:
3605 case ISD::SETULE:
3606 return DAG.getSetCC(VT, N0.getOperand(0),
3607 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3608 Cond);
3609 default:
3610 break; // todo, be more careful with signed comparisons
3611 }
3612 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3613 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3614 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3615 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3616 MVT::ValueType ExtDstTy = N0.getValueType();
3617 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3618
3619 // If the extended part has any inconsistent bits, it cannot ever
3620 // compare equal. In other words, they have to be all ones or all
3621 // zeros.
3622 uint64_t ExtBits =
3623 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3624 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3625 return DAG.getConstant(Cond == ISD::SETNE, VT);
3626
3627 SDOperand ZextOp;
3628 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3629 if (Op0Ty == ExtSrcTy) {
3630 ZextOp = N0.getOperand(0);
3631 } else {
3632 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3633 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3634 DAG.getConstant(Imm, Op0Ty));
3635 }
Chris Lattner5750df92006-03-01 04:03:14 +00003636 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003637 // Otherwise, make this a use of a zext.
3638 return DAG.getSetCC(VT, ZextOp,
3639 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3640 ExtDstTy),
3641 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00003642 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
3643 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3644 (N0.getOpcode() == ISD::XOR ||
3645 (N0.getOpcode() == ISD::AND &&
3646 N0.getOperand(0).getOpcode() == ISD::XOR &&
3647 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3648 isa<ConstantSDNode>(N0.getOperand(1)) &&
3649 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
3650 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
3651 // only do this if the top bits are known zero.
3652 if (TLI.MaskedValueIsZero(N1,
3653 MVT::getIntVTBitMask(N0.getValueType())-1)) {
3654 // Okay, get the un-inverted input value.
3655 SDOperand Val;
3656 if (N0.getOpcode() == ISD::XOR)
3657 Val = N0.getOperand(0);
3658 else {
3659 assert(N0.getOpcode() == ISD::AND &&
3660 N0.getOperand(0).getOpcode() == ISD::XOR);
3661 // ((X^1)&1)^1 -> X & 1
3662 Val = DAG.getNode(ISD::AND, N0.getValueType(),
3663 N0.getOperand(0).getOperand(0), N0.getOperand(1));
3664 }
3665 return DAG.getSetCC(VT, Val, N1,
3666 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
3667 }
Nate Begeman452d7be2005-09-16 00:54:12 +00003668 }
Chris Lattner5c46f742005-10-05 06:11:08 +00003669
Nate Begeman452d7be2005-09-16 00:54:12 +00003670 uint64_t MinVal, MaxVal;
3671 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
3672 if (ISD::isSignedIntSetCC(Cond)) {
3673 MinVal = 1ULL << (OperandBitSize-1);
3674 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
3675 MaxVal = ~0ULL >> (65-OperandBitSize);
3676 else
3677 MaxVal = 0;
3678 } else {
3679 MinVal = 0;
3680 MaxVal = ~0ULL >> (64-OperandBitSize);
3681 }
3682
3683 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
3684 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
3685 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
3686 --C1; // X >= C0 --> X > (C0-1)
3687 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3688 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
3689 }
3690
3691 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
3692 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
3693 ++C1; // X <= C0 --> X < (C0+1)
3694 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3695 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
3696 }
3697
3698 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
3699 return DAG.getConstant(0, VT); // X < MIN --> false
3700
3701 // Canonicalize setgt X, Min --> setne X, Min
3702 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
3703 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00003704 // Canonicalize setlt X, Max --> setne X, Max
3705 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
3706 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00003707
3708 // If we have setult X, 1, turn it into seteq X, 0
3709 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
3710 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
3711 ISD::SETEQ);
3712 // If we have setugt X, Max-1, turn it into seteq X, Max
3713 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
3714 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
3715 ISD::SETEQ);
3716
3717 // If we have "setcc X, C0", check to see if we can shrink the immediate
3718 // by changing cc.
3719
3720 // SETUGT X, SINTMAX -> SETLT X, 0
3721 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
3722 C1 == (~0ULL >> (65-OperandBitSize)))
3723 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
3724 ISD::SETLT);
3725
3726 // FIXME: Implement the rest of these.
3727
3728 // Fold bit comparisons when we can.
3729 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3730 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
3731 if (ConstantSDNode *AndRHS =
3732 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3733 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
3734 // Perform the xform if the AND RHS is a single bit.
3735 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
3736 return DAG.getNode(ISD::SRL, VT, N0,
3737 DAG.getConstant(Log2_64(AndRHS->getValue()),
3738 TLI.getShiftAmountTy()));
3739 }
3740 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
3741 // (X & 8) == 8 --> (X & 8) >> 3
3742 // Perform the xform if C1 is a single bit.
3743 if ((C1 & (C1-1)) == 0) {
3744 return DAG.getNode(ISD::SRL, VT, N0,
Chris Lattner729c6d12006-05-27 00:43:02 +00003745 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
Nate Begeman452d7be2005-09-16 00:54:12 +00003746 }
3747 }
3748 }
3749 }
3750 } else if (isa<ConstantSDNode>(N0.Val)) {
3751 // Ensure that the constant occurs on the RHS.
3752 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3753 }
3754
3755 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
3756 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
3757 double C0 = N0C->getValue(), C1 = N1C->getValue();
3758
3759 switch (Cond) {
3760 default: break; // FIXME: Implement the rest of these!
3761 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3762 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3763 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
3764 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
3765 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
3766 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
3767 }
3768 } else {
3769 // Ensure that the constant occurs on the RHS.
3770 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3771 }
3772
3773 if (N0 == N1) {
3774 // We can always fold X == Y for integer setcc's.
3775 if (MVT::isInteger(N0.getValueType()))
3776 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3777 unsigned UOF = ISD::getUnorderedFlavor(Cond);
3778 if (UOF == 2) // FP operators that are undefined on NaNs.
3779 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3780 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
3781 return DAG.getConstant(UOF, VT);
3782 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
3783 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00003784 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00003785 if (NewCond != Cond)
3786 return DAG.getSetCC(VT, N0, N1, NewCond);
3787 }
3788
3789 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3790 MVT::isInteger(N0.getValueType())) {
3791 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
3792 N0.getOpcode() == ISD::XOR) {
3793 // Simplify (X+Y) == (X+Z) --> Y == Z
3794 if (N0.getOpcode() == N1.getOpcode()) {
3795 if (N0.getOperand(0) == N1.getOperand(0))
3796 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
3797 if (N0.getOperand(1) == N1.getOperand(1))
3798 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
Evan Cheng1efba0e2006-08-29 06:42:35 +00003799 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003800 // If X op Y == Y op X, try other combinations.
3801 if (N0.getOperand(0) == N1.getOperand(1))
3802 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
3803 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00003804 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00003805 }
3806 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003807
3808 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
3809 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3810 // Turn (X+C1) == C2 --> X == C2-C1
3811 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
3812 return DAG.getSetCC(VT, N0.getOperand(0),
3813 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
3814 N0.getValueType()), Cond);
3815 }
3816
3817 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
3818 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00003819 // If we know that all of the inverted bits are zero, don't bother
3820 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003821 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00003822 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003823 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00003824 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003825 }
3826
3827 // Turn (C1-X) == C2 --> X == C1-C2
3828 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
3829 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
3830 return DAG.getSetCC(VT, N0.getOperand(1),
3831 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
3832 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00003833 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003834 }
3835 }
3836
Nate Begeman452d7be2005-09-16 00:54:12 +00003837 // Simplify (X+Z) == X --> Z == 0
3838 if (N0.getOperand(0) == N1)
3839 return DAG.getSetCC(VT, N0.getOperand(1),
3840 DAG.getConstant(0, N0.getValueType()), Cond);
3841 if (N0.getOperand(1) == N1) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00003842 if (DAG.isCommutativeBinOp(N0.getOpcode()))
Nate Begeman452d7be2005-09-16 00:54:12 +00003843 return DAG.getSetCC(VT, N0.getOperand(0),
3844 DAG.getConstant(0, N0.getValueType()), Cond);
3845 else {
3846 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
3847 // (Z-X) == X --> Z == X<<1
3848 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
3849 N1,
3850 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003851 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003852 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
3853 }
3854 }
3855 }
3856
3857 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3858 N1.getOpcode() == ISD::XOR) {
3859 // Simplify X == (X+Z) --> Z == 0
3860 if (N1.getOperand(0) == N0) {
3861 return DAG.getSetCC(VT, N1.getOperand(1),
3862 DAG.getConstant(0, N1.getValueType()), Cond);
3863 } else if (N1.getOperand(1) == N0) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00003864 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003865 return DAG.getSetCC(VT, N1.getOperand(0),
3866 DAG.getConstant(0, N1.getValueType()), Cond);
3867 } else {
3868 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3869 // X == (Z-X) --> X<<1 == Z
3870 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
3871 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003872 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003873 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
3874 }
3875 }
3876 }
3877 }
3878
3879 // Fold away ALL boolean setcc's.
3880 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00003881 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003882 switch (Cond) {
3883 default: assert(0 && "Unknown integer setcc!");
3884 case ISD::SETEQ: // X == Y -> (X^Y)^1
3885 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3886 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00003887 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003888 break;
3889 case ISD::SETNE: // X != Y --> (X^Y)
3890 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3891 break;
3892 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
3893 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
3894 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3895 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003896 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003897 break;
3898 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
3899 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
3900 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3901 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003902 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003903 break;
3904 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
3905 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
3906 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3907 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003908 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003909 break;
3910 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
3911 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
3912 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3913 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
3914 break;
3915 }
3916 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00003917 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003918 // FIXME: If running after legalize, we probably can't do this.
3919 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3920 }
3921 return N0;
3922 }
3923
3924 // Could not fold it.
3925 return SDOperand();
3926}
3927
Nate Begeman69575232005-10-20 02:15:44 +00003928/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3929/// return a DAG expression to select that will generate the same value by
3930/// multiplying by a magic number. See:
3931/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3932SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003933 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003934 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
3935
Andrew Lenharth232c9102006-06-12 16:07:18 +00003936 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003937 ii != ee; ++ii)
3938 AddToWorkList(*ii);
3939 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003940}
3941
3942/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3943/// return a DAG expression to select that will generate the same value by
3944/// multiplying by a magic number. See:
3945/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3946SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003947 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003948 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00003949
Andrew Lenharth232c9102006-06-12 16:07:18 +00003950 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003951 ii != ee; ++ii)
3952 AddToWorkList(*ii);
3953 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003954}
3955
Jim Laskey172585b2006-09-26 07:37:42 +00003956/// hasChainUsers - Returns true if one of the users of a load node has the
3957/// chain result as an operand.
3958bool DAGCombiner::hasChainUsers(SDNode *Load) {
Jim Laskey79597d22006-09-26 09:32:41 +00003959 SDOperand Chain(Load, 1); // The load's chain result.
3960
3961 // For each user of the load.
3962 for (SDNode::use_iterator UI = Load->use_begin(), UE = Load->use_end();
3963 UI != UE; ++UI) {
3964 const SDNode *User = *UI;
3965
3966 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
3967 if (User->getOperand(i) == Chain) return true;
Jim Laskey172585b2006-09-26 07:37:42 +00003968 }
3969 }
3970
Jim Laskey3dd11702006-09-26 08:14:06 +00003971 // No luck.
Jim Laskey172585b2006-09-26 07:37:42 +00003972 return false;
3973}
3974
Jim Laskey279f0532006-09-25 16:29:54 +00003975/// FindBaseOffset - Return true if we can determine base and offset information
3976/// from a given pointer operand. Provides base and offset as a result.
3977bool DAGCombiner::FindBaseOffset(SDOperand Ptr,
3978 SDOperand &Object, int64_t &Offset) {
3979
3980 // Is it a frame variable, global or constant.
3981 if (isa<FrameIndexSDNode>(Ptr) ||
3982 isa<ConstantPoolSDNode>(Ptr) ||
3983 isa<GlobalAddressSDNode>(Ptr)) {
3984 Object = Ptr; Offset = 0;
3985 return true;
3986 } else if (Ptr.getOpcode() == ISD::ADD &&
3987 FindBaseOffset(Ptr.getOperand(0), Object, Offset)) {
3988 // If it's an add of an simple constant then include it in the offset.
3989 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Ptr.getOperand(1))) {
3990 Offset += C->getValue();
3991 return true;
3992 }
3993 }
3994
3995 return false;
3996}
3997
3998/// isAlias - Return true if there is the possibility that the two addresses
3999/// overlap.
4000bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
4001 SDOperand SrcValue1,
4002 SDOperand Ptr2, int64_t Size2,
4003 SDOperand SrcValue2) {
4004 // If they are the same then they must be aliases.
4005 if (Ptr1 == Ptr2) return true;
4006
4007 // Gather base offset information. Objects can be frame variables, globals
4008 // or constants.
4009 SDOperand Object1, Object2;
4010 int64_t Offset1, Offset2;
4011 if (FindBaseOffset(Ptr1, Object1, Offset1) &&
4012 FindBaseOffset(Ptr2, Object2, Offset2)) {
4013 // If they have a different base address, then they can't alias.
4014 if (Object1 != Object2) return false;
4015
4016 // Check to see if the addresses overlap.
4017 if ((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1)
4018 return false;
4019 }
4020
4021 // Otherwise we don't know and have to play it safe.
4022 return true;
4023}
4024
4025/// FindAliasInfo - Extracts the relevant alias information from the memory
4026/// node.
4027void DAGCombiner::FindAliasInfo(SDNode *N,
4028 SDOperand &Ptr, int64_t &Size, SDOperand &SrcValue) {
4029 switch (N->getOpcode()) {
4030 case ISD::LOAD:
4031 Ptr = N->getOperand(1);
Jim Laskey3dd11702006-09-26 08:14:06 +00004032 Size = MVT::getSizeInBits(N->getValueType(0)) >> 3;
Jim Laskey279f0532006-09-25 16:29:54 +00004033 SrcValue = N->getOperand(2);
4034 break;
4035 case ISD::STORE:
4036 Ptr = N->getOperand(2);
4037 Size = MVT::getSizeInBits(N->getOperand(1).getValueType()) >> 3;
4038 SrcValue = N->getOperand(3);
4039 break;
4040 default:
4041 assert(0 && "getAliasInfo expected a memory op");
4042 }
4043}
4044
4045/// hasChain - Return true if Op has a chain. Provides chain if present.
4046///
4047bool DAGCombiner::hasChain(SDOperand Op, SDOperand &Chain) {
4048 if (Op.getNumOperands() == 0) return false;
4049 Chain = Op.getOperand(0);
4050 return Chain.getValueType() == MVT::Other;
4051}
4052
4053/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
4054/// for a better chain.
4055SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand Chain) {
4056 // Get alias information for node.
4057 SDOperand Ptr;
4058 int64_t Size;
4059 SDOperand SrcValue;
4060 FindAliasInfo(N, Ptr, Size, SrcValue);
4061
4062 // While we don't encounter any aliasing memory nodes walk up chain.
4063 while (true) {
4064 switch (Chain.getOpcode()) {
4065 case ISD::EntryToken:
4066 // Entry token is ideal chain operand.
4067 return Chain;
4068 case ISD::LOAD:
4069 case ISD::STORE: {
4070 // Get alias information for chain.
4071 SDOperand ChainPtr;
4072 int64_t ChainSize;
4073 SDOperand ChainSrcValue;
4074 FindAliasInfo(Chain.Val, ChainPtr, ChainSize, ChainSrcValue);
4075
4076 // If chain is alias then stop here, otherwise continue up chain.
4077 if (isAlias(Ptr, Size, SrcValue, ChainPtr, ChainSize, ChainSrcValue))
4078 return Chain;
4079 else
4080 Chain = Chain.getOperand(0);
4081
4082 break;
4083 }
4084 case ISD::TokenFactor: {
4085 // Continue up each of token factor operand and accumulate results in
4086 // a new token factor. CSE will handle duplicate elimination.
4087 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
4088 bool Change = false;
4089
4090 // For each token factor operand.
4091 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i) {
4092 SDOperand Op = Chain.getOperand(i);
4093 SDOperand OpChain = FindBetterChain(N, Op);
4094
4095 // Make sure we don't duplicate an operand.
4096 if (OpChain.getOpcode() != ISD::EntryToken &&
4097 std::find(Ops.begin(), Ops.end(), OpChain) == Ops.end()) {
4098 Ops.push_back(OpChain);
4099 }
4100
4101 // If we added a new operand.
4102 Change = Change || Op != OpChain;
4103 }
4104
4105 // If we have new operands.
4106 if (Change) {
4107 // Create a specialized token factor for this chain. getNode CSE will
4108 // handle duplicates. If it's a single operand, getNode will just
4109 // return the opernand instead of a new token factor.
4110 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
4111 }
4112
4113 // Leave things alone.
4114 return Chain;
4115 }
4116 // For all other instructions we will just have to take what we can get.
4117 default: return Chain;
4118 }
4119 }
4120
4121 return Chain;
4122}
4123
Nate Begeman1d4d4142005-09-01 00:19:25 +00004124// SelectionDAG::Combine - This is the entry point for the file.
4125//
Nate Begeman4ebd8052005-09-01 23:24:04 +00004126void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00004127 /// run - This is the main entry point to this class.
4128 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00004129 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004130}