blob: c30a222b4acc3ff154ede07d74d084339c0303c4 [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>
Nate Begeman1d4d4142005-09-01 00:19:25 +000042using namespace llvm;
43
44namespace {
Andrew Lenharthae6153f2006-07-20 17:43:27 +000045 static Statistic<> NodesCombined ("dagcombiner",
46 "Number of dag nodes combined");
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000047
Nate Begeman1d4d4142005-09-01 00:19:25 +000048
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000049static cl::opt<bool>
50 CombinerAA("combiner-alias-analysis", cl::Hidden,
51 cl::desc("Turn on alias analysis turning testing"));
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000052
53class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000054 SelectionDAG &DAG;
55 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000056 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000057
58 // Worklist of all of the nodes that need to be simplified.
59 std::vector<SDNode*> WorkList;
60
61 /// AddUsersToWorkList - When an instruction is simplified, add all users of
62 /// the instruction to the work lists because they might get more simplified
63 /// now.
64 ///
65 void AddUsersToWorkList(SDNode *N) {
66 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000067 UI != UE; ++UI)
68 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000069 }
70
71 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000072 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000073 void removeFromWorkList(SDNode *N) {
74 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
75 WorkList.end());
76 }
77
Chris Lattner24664722006-03-01 04:53:38 +000078 public:
Chris Lattner5750df92006-03-01 04:03:14 +000079 void AddToWorkList(SDNode *N) {
80 WorkList.push_back(N);
81 }
82
Chris Lattner3577e382006-08-11 17:56:38 +000083 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo) {
84 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
Chris Lattner87514ca2005-10-10 22:31:19 +000085 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000086 DEBUG(std::cerr << "\nReplacing "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +000087 std::cerr << "\nWith: "; To[0].Val->dump(&DAG);
Chris Lattner3577e382006-08-11 17:56:38 +000088 std::cerr << " and " << NumTo-1 << " other values\n");
Chris Lattner01a22022005-10-10 22:04:48 +000089 std::vector<SDNode*> NowDead;
Chris Lattner3577e382006-08-11 17:56:38 +000090 DAG.ReplaceAllUsesWith(N, To, &NowDead);
Chris Lattner01a22022005-10-10 22:04:48 +000091
92 // Push the new nodes and any users onto the worklist
Chris Lattner3577e382006-08-11 17:56:38 +000093 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattner01a22022005-10-10 22:04:48 +000094 WorkList.push_back(To[i].Val);
95 AddUsersToWorkList(To[i].Val);
96 }
97
98 // Nodes can end up on the worklist more than once. Make sure we do
99 // not process a node that has been replaced.
100 removeFromWorkList(N);
101 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
102 removeFromWorkList(NowDead[i]);
103
104 // Finally, since the node is now dead, remove it from the graph.
105 DAG.DeleteNode(N);
106 return SDOperand(N, 0);
107 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000108
Chris Lattner24664722006-03-01 04:53:38 +0000109 SDOperand CombineTo(SDNode *N, SDOperand Res) {
Chris Lattner3577e382006-08-11 17:56:38 +0000110 return CombineTo(N, &Res, 1);
Chris Lattner24664722006-03-01 04:53:38 +0000111 }
112
113 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
Chris Lattner3577e382006-08-11 17:56:38 +0000114 SDOperand To[] = { Res0, Res1 };
115 return CombineTo(N, To, 2);
Chris Lattner24664722006-03-01 04:53:38 +0000116 }
117 private:
118
Chris Lattner012f2412006-02-17 21:58:01 +0000119 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000120 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000121 /// propagation. If so, return true.
122 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000123 TargetLowering::TargetLoweringOpt TLO(DAG);
124 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000125 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
126 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
127 return false;
128
129 // Revisit the node.
130 WorkList.push_back(Op.Val);
131
132 // Replace the old value with the new one.
133 ++NodesCombined;
134 DEBUG(std::cerr << "\nReplacing "; TLO.Old.Val->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000135 std::cerr << "\nWith: "; TLO.New.Val->dump(&DAG));
Chris Lattner012f2412006-02-17 21:58:01 +0000136
137 std::vector<SDNode*> NowDead;
138 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
139
Chris Lattner7d20d392006-02-20 06:51:04 +0000140 // Push the new node and any (possibly new) users onto the worklist.
Chris Lattner012f2412006-02-17 21:58:01 +0000141 WorkList.push_back(TLO.New.Val);
142 AddUsersToWorkList(TLO.New.Val);
143
144 // Nodes can end up on the worklist more than once. Make sure we do
145 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000146 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
147 removeFromWorkList(NowDead[i]);
148
Chris Lattner7d20d392006-02-20 06:51:04 +0000149 // Finally, if the node is now dead, remove it from the graph. The node
150 // may not be dead if the replacement process recursively simplified to
151 // something else needing this node.
152 if (TLO.Old.Val->use_empty()) {
153 removeFromWorkList(TLO.Old.Val);
154 DAG.DeleteNode(TLO.Old.Val);
155 }
Chris Lattner012f2412006-02-17 21:58:01 +0000156 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000157 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000158
Nate Begeman1d4d4142005-09-01 00:19:25 +0000159 /// visit - call the node-specific routine that knows how to fold each
160 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000161 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000162
163 // Visitation implementation - Implement dag node combining for different
164 // node types. The semantics are as follows:
165 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000166 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000167 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000168 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000169 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000170 SDOperand visitTokenFactor(SDNode *N);
171 SDOperand visitADD(SDNode *N);
172 SDOperand visitSUB(SDNode *N);
173 SDOperand visitMUL(SDNode *N);
174 SDOperand visitSDIV(SDNode *N);
175 SDOperand visitUDIV(SDNode *N);
176 SDOperand visitSREM(SDNode *N);
177 SDOperand visitUREM(SDNode *N);
178 SDOperand visitMULHU(SDNode *N);
179 SDOperand visitMULHS(SDNode *N);
180 SDOperand visitAND(SDNode *N);
181 SDOperand visitOR(SDNode *N);
182 SDOperand visitXOR(SDNode *N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000183 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000184 SDOperand visitSHL(SDNode *N);
185 SDOperand visitSRA(SDNode *N);
186 SDOperand visitSRL(SDNode *N);
187 SDOperand visitCTLZ(SDNode *N);
188 SDOperand visitCTTZ(SDNode *N);
189 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000190 SDOperand visitSELECT(SDNode *N);
191 SDOperand visitSELECT_CC(SDNode *N);
192 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000193 SDOperand visitSIGN_EXTEND(SDNode *N);
194 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000195 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000196 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
197 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000198 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000199 SDOperand visitVBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000200 SDOperand visitFADD(SDNode *N);
201 SDOperand visitFSUB(SDNode *N);
202 SDOperand visitFMUL(SDNode *N);
203 SDOperand visitFDIV(SDNode *N);
204 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000205 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000206 SDOperand visitSINT_TO_FP(SDNode *N);
207 SDOperand visitUINT_TO_FP(SDNode *N);
208 SDOperand visitFP_TO_SINT(SDNode *N);
209 SDOperand visitFP_TO_UINT(SDNode *N);
210 SDOperand visitFP_ROUND(SDNode *N);
211 SDOperand visitFP_ROUND_INREG(SDNode *N);
212 SDOperand visitFP_EXTEND(SDNode *N);
213 SDOperand visitFNEG(SDNode *N);
214 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000215 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000216 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000217 SDOperand visitLOAD(SDNode *N);
Chris Lattner29cd7db2006-03-31 18:10:41 +0000218 SDOperand visitXEXTLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000219 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000220 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
221 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000222 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000223 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000224 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000225
Evan Cheng44f1f092006-04-20 08:56:16 +0000226 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000227 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
228
Chris Lattner40c62d52005-10-18 06:04:22 +0000229 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000230 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000231 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
232 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
233 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000234 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000235 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000236 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000237 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000238 SDOperand BuildUDIV(SDNode *N);
239 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Jim Laskeyd1aed7a2006-09-21 16:28:59 +0000240 bool isNotAlias(SDOperand Ptr1, SDOperand Ptr2);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000241public:
242 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000243 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000244
245 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000246 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000247 };
248}
249
Chris Lattner24664722006-03-01 04:53:38 +0000250//===----------------------------------------------------------------------===//
251// TargetLowering::DAGCombinerInfo implementation
252//===----------------------------------------------------------------------===//
253
254void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
255 ((DAGCombiner*)DC)->AddToWorkList(N);
256}
257
258SDOperand TargetLowering::DAGCombinerInfo::
259CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000260 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000261}
262
263SDOperand TargetLowering::DAGCombinerInfo::
264CombineTo(SDNode *N, SDOperand Res) {
265 return ((DAGCombiner*)DC)->CombineTo(N, Res);
266}
267
268
269SDOperand TargetLowering::DAGCombinerInfo::
270CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
271 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
272}
273
274
275
276
277//===----------------------------------------------------------------------===//
278
279
Nate Begeman4ebd8052005-09-01 23:24:04 +0000280// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
281// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000282// Also, set the incoming LHS, RHS, and CC references to the appropriate
283// nodes based on the type of node we are checking. This simplifies life a
284// bit for the callers.
285static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
286 SDOperand &CC) {
287 if (N.getOpcode() == ISD::SETCC) {
288 LHS = N.getOperand(0);
289 RHS = N.getOperand(1);
290 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000291 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000292 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000293 if (N.getOpcode() == ISD::SELECT_CC &&
294 N.getOperand(2).getOpcode() == ISD::Constant &&
295 N.getOperand(3).getOpcode() == ISD::Constant &&
296 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000297 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
298 LHS = N.getOperand(0);
299 RHS = N.getOperand(1);
300 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000301 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000302 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000303 return false;
304}
305
Nate Begeman99801192005-09-07 23:25:52 +0000306// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
307// one use. If this is true, it allows the users to invert the operation for
308// free when it is profitable to do so.
309static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000310 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000311 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000312 return true;
313 return false;
314}
315
Nate Begemancd4d58c2006-02-03 06:46:56 +0000316SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
317 MVT::ValueType VT = N0.getValueType();
318 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
319 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
320 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
321 if (isa<ConstantSDNode>(N1)) {
322 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000323 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000324 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
325 } else if (N0.hasOneUse()) {
326 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000327 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000328 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
329 }
330 }
331 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
332 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
333 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
334 if (isa<ConstantSDNode>(N0)) {
335 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000336 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000337 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
338 } else if (N1.hasOneUse()) {
339 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000340 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000341 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
342 }
343 }
344 return SDOperand();
345}
346
Nate Begeman4ebd8052005-09-01 23:24:04 +0000347void DAGCombiner::Run(bool RunningAfterLegalize) {
348 // set the instance variable, so that the various visit routines may use it.
349 AfterLegalize = RunningAfterLegalize;
350
Nate Begeman646d7e22005-09-02 21:18:40 +0000351 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000352 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
353 E = DAG.allnodes_end(); I != E; ++I)
354 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000355
Chris Lattner95038592005-10-05 06:35:28 +0000356 // Create a dummy node (which is not added to allnodes), that adds a reference
357 // to the root node, preventing it from being deleted, and tracking any
358 // changes of the root.
359 HandleSDNode Dummy(DAG.getRoot());
360
Chris Lattner24664722006-03-01 04:53:38 +0000361
362 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
363 TargetLowering::DAGCombinerInfo
364 DagCombineInfo(DAG, !RunningAfterLegalize, this);
365
Nate Begeman1d4d4142005-09-01 00:19:25 +0000366 // while the worklist isn't empty, inspect the node on the end of it and
367 // try and combine it.
368 while (!WorkList.empty()) {
369 SDNode *N = WorkList.back();
370 WorkList.pop_back();
371
372 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000373 // N is deleted from the DAG, since they too may now be dead or may have a
374 // reduced number of uses, allowing other xforms.
375 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000376 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
377 WorkList.push_back(N->getOperand(i).Val);
378
Nate Begeman1d4d4142005-09-01 00:19:25 +0000379 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000380 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000381 continue;
382 }
383
Nate Begeman83e75ec2005-09-06 04:43:02 +0000384 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000385
386 // If nothing happened, try a target-specific DAG combine.
387 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000388 assert(N->getOpcode() != ISD::DELETED_NODE &&
389 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000390 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
391 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
392 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
393 }
394
Nate Begeman83e75ec2005-09-06 04:43:02 +0000395 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000396 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000397 // If we get back the same node we passed in, rather than a new node or
398 // zero, we know that the node must have defined multiple values and
399 // CombineTo was used. Since CombineTo takes care of the worklist
400 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000401 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000402 assert(N->getOpcode() != ISD::DELETED_NODE &&
403 RV.Val->getOpcode() != ISD::DELETED_NODE &&
404 "Node was deleted but visit returned new node!");
405
Nate Begeman2300f552005-09-07 00:15:36 +0000406 DEBUG(std::cerr << "\nReplacing "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000407 std::cerr << "\nWith: "; RV.Val->dump(&DAG);
Nate Begeman2300f552005-09-07 00:15:36 +0000408 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000409 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000410 if (N->getNumValues() == RV.Val->getNumValues())
411 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
412 else {
413 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
414 SDOperand OpV = RV;
415 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
416 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000417
418 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000419 WorkList.push_back(RV.Val);
420 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000421
422 // Nodes can end up on the worklist more than once. Make sure we do
423 // not process a node that has been replaced.
424 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000425 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
426 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000427
428 // Finally, since the node is now dead, remove it from the graph.
429 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000430 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000431 }
432 }
Chris Lattner95038592005-10-05 06:35:28 +0000433
434 // If the root changed (e.g. it was a dead load, update the root).
435 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000436}
437
Nate Begeman83e75ec2005-09-06 04:43:02 +0000438SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000439 switch(N->getOpcode()) {
440 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000441 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000442 case ISD::ADD: return visitADD(N);
443 case ISD::SUB: return visitSUB(N);
444 case ISD::MUL: return visitMUL(N);
445 case ISD::SDIV: return visitSDIV(N);
446 case ISD::UDIV: return visitUDIV(N);
447 case ISD::SREM: return visitSREM(N);
448 case ISD::UREM: return visitUREM(N);
449 case ISD::MULHU: return visitMULHU(N);
450 case ISD::MULHS: return visitMULHS(N);
451 case ISD::AND: return visitAND(N);
452 case ISD::OR: return visitOR(N);
453 case ISD::XOR: return visitXOR(N);
454 case ISD::SHL: return visitSHL(N);
455 case ISD::SRA: return visitSRA(N);
456 case ISD::SRL: return visitSRL(N);
457 case ISD::CTLZ: return visitCTLZ(N);
458 case ISD::CTTZ: return visitCTTZ(N);
459 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000460 case ISD::SELECT: return visitSELECT(N);
461 case ISD::SELECT_CC: return visitSELECT_CC(N);
462 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000463 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
464 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000465 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000466 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
467 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000468 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000469 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000470 case ISD::FADD: return visitFADD(N);
471 case ISD::FSUB: return visitFSUB(N);
472 case ISD::FMUL: return visitFMUL(N);
473 case ISD::FDIV: return visitFDIV(N);
474 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000475 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000476 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
477 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
478 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
479 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
480 case ISD::FP_ROUND: return visitFP_ROUND(N);
481 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
482 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
483 case ISD::FNEG: return visitFNEG(N);
484 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000485 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000486 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000487 case ISD::LOAD: return visitLOAD(N);
Chris Lattner29cd7db2006-03-31 18:10:41 +0000488 case ISD::EXTLOAD:
489 case ISD::SEXTLOAD:
490 case ISD::ZEXTLOAD: return visitXEXTLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000491 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000492 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
493 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000494 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000495 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000496 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000497 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
498 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
499 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
500 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
501 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
502 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
503 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
504 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000505 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000506 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000507}
508
Nate Begeman83e75ec2005-09-06 04:43:02 +0000509SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000510 SmallVector<SDOperand, 8> Ops;
Nate Begemanded49632005-10-13 03:11:28 +0000511 bool Changed = false;
512
Nate Begeman1d4d4142005-09-01 00:19:25 +0000513 // If the token factor has two operands and one is the entry token, replace
514 // the token factor with the other operand.
515 if (N->getNumOperands() == 2) {
Chris Lattner21a57dc2006-05-12 05:01:37 +0000516 if (N->getOperand(0).getOpcode() == ISD::EntryToken ||
517 N->getOperand(0) == N->getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000518 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000519 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000520 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000521 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000522
Nate Begemanded49632005-10-13 03:11:28 +0000523 // fold (tokenfactor (tokenfactor)) -> tokenfactor
524 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
525 SDOperand Op = N->getOperand(i);
526 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
Chris Lattnerac0f8f22006-03-13 18:37:30 +0000527 AddToWorkList(Op.Val); // Remove dead node.
Nate Begemanded49632005-10-13 03:11:28 +0000528 Changed = true;
529 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
530 Ops.push_back(Op.getOperand(j));
Chris Lattner21a57dc2006-05-12 05:01:37 +0000531 } else if (i == 0 || N->getOperand(i) != N->getOperand(i-1)) {
Nate Begemanded49632005-10-13 03:11:28 +0000532 Ops.push_back(Op);
Chris Lattner21a57dc2006-05-12 05:01:37 +0000533 } else {
534 // Deleted an operand that was the same as the last one.
535 Changed = true;
Nate Begemanded49632005-10-13 03:11:28 +0000536 }
537 }
538 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000539 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begeman83e75ec2005-09-06 04:43:02 +0000540 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000541}
542
Nate Begeman83e75ec2005-09-06 04:43:02 +0000543SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000544 SDOperand N0 = N->getOperand(0);
545 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000546 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
547 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000548 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000549
550 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000551 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000552 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000553 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000554 if (N0C && !N1C)
555 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000556 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000557 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000558 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000559 // fold ((c1-A)+c2) -> (c1+c2)-A
560 if (N1C && N0.getOpcode() == ISD::SUB)
561 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
562 return DAG.getNode(ISD::SUB, VT,
563 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
564 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000565 // reassociate add
566 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
567 if (RADD.Val != 0)
568 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000569 // fold ((0-A) + B) -> B-A
570 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
571 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000572 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000573 // fold (A + (0-B)) -> A-B
574 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
575 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000576 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000577 // fold (A+(B-A)) -> B
578 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000579 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000580
Evan Cheng860771d2006-03-01 01:09:54 +0000581 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000582 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000583
584 // fold (a+b) -> (a|b) iff a and b share no bits.
585 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
586 uint64_t LHSZero, LHSOne;
587 uint64_t RHSZero, RHSOne;
588 uint64_t Mask = MVT::getIntVTBitMask(VT);
589 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
590 if (LHSZero) {
591 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
592
593 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
594 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
595 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
596 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
597 return DAG.getNode(ISD::OR, VT, N0, N1);
598 }
599 }
600
Nate Begeman83e75ec2005-09-06 04:43:02 +0000601 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000602}
603
Nate Begeman83e75ec2005-09-06 04:43:02 +0000604SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000605 SDOperand N0 = N->getOperand(0);
606 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000607 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
608 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000609 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000610
Chris Lattner854077d2005-10-17 01:07:11 +0000611 // fold (sub x, x) -> 0
612 if (N0 == N1)
613 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000614 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000615 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000616 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000617 // fold (sub x, c) -> (add x, -c)
618 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000619 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000620 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000621 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000622 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000623 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000624 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000625 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000626 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000627}
628
Nate Begeman83e75ec2005-09-06 04:43:02 +0000629SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000630 SDOperand N0 = N->getOperand(0);
631 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000632 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
633 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000634 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000635
636 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000637 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000638 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000639 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000640 if (N0C && !N1C)
641 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000642 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000643 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000644 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000645 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000646 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000647 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000648 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000649 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000650 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000651 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000652 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000653 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
654 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
655 // FIXME: If the input is something that is easily negated (e.g. a
656 // single-use add), we should put the negate there.
657 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
658 DAG.getNode(ISD::SHL, VT, N0,
659 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
660 TLI.getShiftAmountTy())));
661 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +0000662
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000663 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
664 if (N1C && N0.getOpcode() == ISD::SHL &&
665 isa<ConstantSDNode>(N0.getOperand(1))) {
666 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000667 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000668 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
669 }
670
671 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
672 // use.
673 {
674 SDOperand Sh(0,0), Y(0,0);
675 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
676 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
677 N0.Val->hasOneUse()) {
678 Sh = N0; Y = N1;
679 } else if (N1.getOpcode() == ISD::SHL &&
680 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
681 Sh = N1; Y = N0;
682 }
683 if (Sh.Val) {
684 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
685 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
686 }
687 }
Chris Lattnera1deca32006-03-04 23:33:26 +0000688 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
689 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
690 isa<ConstantSDNode>(N0.getOperand(1))) {
691 return DAG.getNode(ISD::ADD, VT,
692 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
693 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
694 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000695
Nate Begemancd4d58c2006-02-03 06:46:56 +0000696 // reassociate mul
697 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
698 if (RMUL.Val != 0)
699 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000700 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000701}
702
Nate Begeman83e75ec2005-09-06 04:43:02 +0000703SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000704 SDOperand N0 = N->getOperand(0);
705 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000706 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
707 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000708 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000709
710 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000711 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000712 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000713 // fold (sdiv X, 1) -> X
714 if (N1C && N1C->getSignExtended() == 1LL)
715 return N0;
716 // fold (sdiv X, -1) -> 0-X
717 if (N1C && N1C->isAllOnesValue())
718 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000719 // If we know the sign bits of both operands are zero, strength reduce to a
720 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
721 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000722 if (TLI.MaskedValueIsZero(N1, SignBit) &&
723 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000724 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000725 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000726 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000727 (isPowerOf2_64(N1C->getSignExtended()) ||
728 isPowerOf2_64(-N1C->getSignExtended()))) {
729 // If dividing by powers of two is cheap, then don't perform the following
730 // fold.
731 if (TLI.isPow2DivCheap())
732 return SDOperand();
733 int64_t pow2 = N1C->getSignExtended();
734 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000735 unsigned lg2 = Log2_64(abs2);
736 // Splat the sign bit into the register
737 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000738 DAG.getConstant(MVT::getSizeInBits(VT)-1,
739 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +0000740 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000741 // Add (N0 < 0) ? abs2 - 1 : 0;
742 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
743 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000744 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000745 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +0000746 AddToWorkList(SRL.Val);
747 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +0000748 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
749 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000750 // If we're dividing by a positive value, we're done. Otherwise, we must
751 // negate the result.
752 if (pow2 > 0)
753 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +0000754 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000755 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
756 }
Nate Begeman69575232005-10-20 02:15:44 +0000757 // if integer divide is expensive and we satisfy the requirements, emit an
758 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000759 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000760 !TLI.isIntDivCheap()) {
761 SDOperand Op = BuildSDIV(N);
762 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000763 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000764 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000765}
766
Nate Begeman83e75ec2005-09-06 04:43:02 +0000767SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000768 SDOperand N0 = N->getOperand(0);
769 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000770 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
771 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000772 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000773
774 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000775 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000776 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000777 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000778 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000779 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000780 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000781 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000782 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
783 if (N1.getOpcode() == ISD::SHL) {
784 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
785 if (isPowerOf2_64(SHC->getValue())) {
786 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000787 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
788 DAG.getConstant(Log2_64(SHC->getValue()),
789 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +0000790 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000791 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000792 }
793 }
794 }
Nate Begeman69575232005-10-20 02:15:44 +0000795 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000796 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
797 SDOperand Op = BuildUDIV(N);
798 if (Op.Val) return Op;
799 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000800 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000801}
802
Nate Begeman83e75ec2005-09-06 04:43:02 +0000803SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000804 SDOperand N0 = N->getOperand(0);
805 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000806 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
807 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000808 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000809
810 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000811 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000812 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000813 // If we know the sign bits of both operands are zero, strength reduce to a
814 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
815 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000816 if (TLI.MaskedValueIsZero(N1, SignBit) &&
817 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000818 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000819 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000820}
821
Nate Begeman83e75ec2005-09-06 04:43:02 +0000822SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000823 SDOperand N0 = N->getOperand(0);
824 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000825 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
826 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000827 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000828
829 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000830 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000831 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000832 // fold (urem x, pow2) -> (and x, pow2-1)
833 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000834 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000835 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
836 if (N1.getOpcode() == ISD::SHL) {
837 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
838 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000839 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +0000840 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000841 return DAG.getNode(ISD::AND, VT, N0, Add);
842 }
843 }
844 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000845 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000846}
847
Nate Begeman83e75ec2005-09-06 04:43:02 +0000848SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000849 SDOperand N0 = N->getOperand(0);
850 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000851 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000852
853 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000854 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000855 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000856 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000857 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000858 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
859 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000860 TLI.getShiftAmountTy()));
861 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000862}
863
Nate Begeman83e75ec2005-09-06 04:43:02 +0000864SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000865 SDOperand N0 = N->getOperand(0);
866 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000867 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000868
869 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000870 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000871 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000872 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000873 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000874 return DAG.getConstant(0, N0.getValueType());
875 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000876}
877
Chris Lattner35e5c142006-05-05 05:51:50 +0000878/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
879/// two operands of the same opcode, try to simplify it.
880SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
881 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
882 MVT::ValueType VT = N0.getValueType();
883 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
884
Chris Lattner540121f2006-05-05 06:31:05 +0000885 // For each of OP in AND/OR/XOR:
886 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
887 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
888 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +0000889 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +0000890 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +0000891 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000892 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
893 SDOperand ORNode = DAG.getNode(N->getOpcode(),
894 N0.getOperand(0).getValueType(),
895 N0.getOperand(0), N1.getOperand(0));
896 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +0000897 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +0000898 }
899
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000900 // For each of OP in SHL/SRL/SRA/AND...
901 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
902 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
903 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +0000904 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000905 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000906 N0.getOperand(1) == N1.getOperand(1)) {
907 SDOperand ORNode = DAG.getNode(N->getOpcode(),
908 N0.getOperand(0).getValueType(),
909 N0.getOperand(0), N1.getOperand(0));
910 AddToWorkList(ORNode.Val);
911 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
912 }
913
914 return SDOperand();
915}
916
Nate Begeman83e75ec2005-09-06 04:43:02 +0000917SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000918 SDOperand N0 = N->getOperand(0);
919 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +0000920 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000921 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
922 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000923 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000924 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000925
926 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000927 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000928 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000929 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000930 if (N0C && !N1C)
931 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000932 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000933 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000934 return N0;
935 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +0000936 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000937 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000938 // reassociate and
939 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
940 if (RAND.Val != 0)
941 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000942 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +0000943 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000944 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000945 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000946 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +0000947 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
948 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +0000949 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +0000950 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +0000951 ~N1C->getValue() & InMask)) {
952 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
953 N0.getOperand(0));
954
955 // Replace uses of the AND with uses of the Zero extend node.
956 CombineTo(N, Zext);
957
Chris Lattner3603cd62006-02-02 07:17:31 +0000958 // We actually want to replace all uses of the any_extend with the
959 // zero_extend, to avoid duplicating things. This will later cause this
960 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +0000961 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +0000962 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +0000963 }
964 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000965 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
966 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
967 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
968 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
969
970 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
971 MVT::isInteger(LL.getValueType())) {
972 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
973 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
974 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +0000975 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000976 return DAG.getSetCC(VT, ORNode, LR, Op1);
977 }
978 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
979 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
980 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +0000981 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000982 return DAG.getSetCC(VT, ANDNode, LR, Op1);
983 }
984 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
985 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
986 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +0000987 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000988 return DAG.getSetCC(VT, ORNode, LR, Op1);
989 }
990 }
991 // canonicalize equivalent to ll == rl
992 if (LL == RR && LR == RL) {
993 Op1 = ISD::getSetCCSwappedOperands(Op1);
994 std::swap(RL, RR);
995 }
996 if (LL == RL && LR == RR) {
997 bool isInteger = MVT::isInteger(LL.getValueType());
998 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
999 if (Result != ISD::SETCC_INVALID)
1000 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1001 }
1002 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001003
1004 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1005 if (N0.getOpcode() == N1.getOpcode()) {
1006 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1007 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001008 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001009
Nate Begemande996292006-02-03 22:24:05 +00001010 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1011 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001012 if (!MVT::isVector(VT) &&
1013 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001014 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001015 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +00001016 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +00001017 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001018 // If we zero all the possible extended bits, then we can turn this into
1019 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001020 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +00001021 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001022 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1023 N0.getOperand(1), N0.getOperand(2),
1024 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001025 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001026 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001027 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001028 }
1029 }
1030 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001031 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001032 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001033 // If we zero all the possible extended bits, then we can turn this into
1034 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001035 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001036 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001037 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1038 N0.getOperand(1), N0.getOperand(2),
1039 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001040 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001041 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001042 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001043 }
1044 }
Chris Lattner15045b62006-02-28 06:35:35 +00001045
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001046 // fold (and (load x), 255) -> (zextload x, i8)
1047 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1048 if (N1C &&
1049 (N0.getOpcode() == ISD::LOAD || N0.getOpcode() == ISD::EXTLOAD ||
1050 N0.getOpcode() == ISD::ZEXTLOAD) &&
1051 N0.hasOneUse()) {
1052 MVT::ValueType EVT, LoadedVT;
Chris Lattner15045b62006-02-28 06:35:35 +00001053 if (N1C->getValue() == 255)
1054 EVT = MVT::i8;
1055 else if (N1C->getValue() == 65535)
1056 EVT = MVT::i16;
1057 else if (N1C->getValue() == ~0U)
1058 EVT = MVT::i32;
1059 else
1060 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001061
1062 LoadedVT = N0.getOpcode() == ISD::LOAD ? VT :
1063 cast<VTSDNode>(N0.getOperand(3))->getVT();
Chris Lattnere44be602006-04-04 17:39:18 +00001064 if (EVT != MVT::Other && LoadedVT > EVT &&
1065 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Chris Lattner15045b62006-02-28 06:35:35 +00001066 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1067 // For big endian targets, we need to add an offset to the pointer to load
1068 // the correct bytes. For little endian systems, we merely need to read
1069 // fewer bytes from the same pointer.
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001070 unsigned PtrOff =
1071 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1072 SDOperand NewPtr = N0.getOperand(1);
1073 if (!TLI.isLittleEndian())
1074 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1075 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001076 AddToWorkList(NewPtr.Val);
Chris Lattner15045b62006-02-28 06:35:35 +00001077 SDOperand Load =
1078 DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
1079 N0.getOperand(2), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001080 AddToWorkList(N);
Chris Lattner15045b62006-02-28 06:35:35 +00001081 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001082 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner15045b62006-02-28 06:35:35 +00001083 }
1084 }
1085
Nate Begeman83e75ec2005-09-06 04:43:02 +00001086 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001087}
1088
Nate Begeman83e75ec2005-09-06 04:43:02 +00001089SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001090 SDOperand N0 = N->getOperand(0);
1091 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001092 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001093 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1094 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001095 MVT::ValueType VT = N1.getValueType();
1096 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001097
1098 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001099 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001100 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001101 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001102 if (N0C && !N1C)
1103 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001104 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001105 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001106 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001107 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001108 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001109 return N1;
1110 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001111 if (N1C &&
1112 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001113 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001114 // reassociate or
1115 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1116 if (ROR.Val != 0)
1117 return ROR;
1118 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1119 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001120 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001121 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1122 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1123 N1),
1124 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001125 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001126 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1127 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1128 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1129 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1130
1131 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1132 MVT::isInteger(LL.getValueType())) {
1133 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1134 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1135 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1136 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1137 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001138 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001139 return DAG.getSetCC(VT, ORNode, LR, Op1);
1140 }
1141 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1142 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1143 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1144 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1145 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001146 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001147 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1148 }
1149 }
1150 // canonicalize equivalent to ll == rl
1151 if (LL == RR && LR == RL) {
1152 Op1 = ISD::getSetCCSwappedOperands(Op1);
1153 std::swap(RL, RR);
1154 }
1155 if (LL == RL && LR == RR) {
1156 bool isInteger = MVT::isInteger(LL.getValueType());
1157 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1158 if (Result != ISD::SETCC_INVALID)
1159 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1160 }
1161 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001162
1163 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1164 if (N0.getOpcode() == N1.getOpcode()) {
1165 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1166 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001167 }
Chris Lattner516b9622006-09-14 20:50:57 +00001168
Chris Lattner1ec72732006-09-14 21:11:37 +00001169 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1170 if (N0.getOpcode() == ISD::AND &&
1171 N1.getOpcode() == ISD::AND &&
1172 N0.getOperand(1).getOpcode() == ISD::Constant &&
1173 N1.getOperand(1).getOpcode() == ISD::Constant &&
1174 // Don't increase # computations.
1175 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1176 // We can only do this xform if we know that bits from X that are set in C2
1177 // but not in C1 are already zero. Likewise for Y.
1178 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1179 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1180
1181 if (TLI.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1182 TLI.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
1183 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1184 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1185 }
1186 }
1187
1188
Chris Lattner516b9622006-09-14 20:50:57 +00001189 // See if this is some rotate idiom.
1190 if (SDNode *Rot = MatchRotate(N0, N1))
1191 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001192
Nate Begeman83e75ec2005-09-06 04:43:02 +00001193 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001194}
1195
Chris Lattner516b9622006-09-14 20:50:57 +00001196
1197/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1198static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1199 if (Op.getOpcode() == ISD::AND) {
1200 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1201 Mask = Op.getOperand(1);
1202 Op = Op.getOperand(0);
1203 } else {
1204 return false;
1205 }
1206 }
1207
1208 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1209 Shift = Op;
1210 return true;
1211 }
1212 return false;
1213}
1214
1215
1216// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1217// idioms for rotate, and if the target supports rotation instructions, generate
1218// a rot[lr].
1219SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1220 // Must be a legal type. Expanded an promoted things won't work with rotates.
1221 MVT::ValueType VT = LHS.getValueType();
1222 if (!TLI.isTypeLegal(VT)) return 0;
1223
1224 // The target must have at least one rotate flavor.
1225 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1226 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1227 if (!HasROTL && !HasROTR) return 0;
1228
1229 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1230 SDOperand LHSShift; // The shift.
1231 SDOperand LHSMask; // AND value if any.
1232 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1233 return 0; // Not part of a rotate.
1234
1235 SDOperand RHSShift; // The shift.
1236 SDOperand RHSMask; // AND value if any.
1237 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1238 return 0; // Not part of a rotate.
1239
1240 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1241 return 0; // Not shifting the same value.
1242
1243 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1244 return 0; // Shifts must disagree.
1245
1246 // Canonicalize shl to left side in a shl/srl pair.
1247 if (RHSShift.getOpcode() == ISD::SHL) {
1248 std::swap(LHS, RHS);
1249 std::swap(LHSShift, RHSShift);
1250 std::swap(LHSMask , RHSMask );
1251 }
1252
1253 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1254
1255 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1256 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
1257 if (LHSShift.getOperand(1).getOpcode() == ISD::Constant &&
1258 RHSShift.getOperand(1).getOpcode() == ISD::Constant) {
1259 uint64_t LShVal = cast<ConstantSDNode>(LHSShift.getOperand(1))->getValue();
1260 uint64_t RShVal = cast<ConstantSDNode>(RHSShift.getOperand(1))->getValue();
1261 if ((LShVal + RShVal) != OpSizeInBits)
1262 return 0;
1263
1264 SDOperand Rot;
1265 if (HasROTL)
1266 Rot = DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1267 LHSShift.getOperand(1));
1268 else
1269 Rot = DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1270 RHSShift.getOperand(1));
1271
1272 // If there is an AND of either shifted operand, apply it to the result.
1273 if (LHSMask.Val || RHSMask.Val) {
1274 uint64_t Mask = MVT::getIntVTBitMask(VT);
1275
1276 if (LHSMask.Val) {
1277 uint64_t RHSBits = (1ULL << LShVal)-1;
1278 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1279 }
1280 if (RHSMask.Val) {
1281 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1282 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1283 }
1284
1285 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1286 }
1287
1288 return Rot.Val;
1289 }
1290
1291 // If there is a mask here, and we have a variable shift, we can't be sure
1292 // that we're masking out the right stuff.
1293 if (LHSMask.Val || RHSMask.Val)
1294 return 0;
1295
1296 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1297 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
1298 if (RHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1299 LHSShift.getOperand(1) == RHSShift.getOperand(1).getOperand(1)) {
1300 if (ConstantSDNode *SUBC =
1301 dyn_cast<ConstantSDNode>(RHSShift.getOperand(1).getOperand(0))) {
1302 if (SUBC->getValue() == OpSizeInBits)
1303 if (HasROTL)
1304 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1305 LHSShift.getOperand(1)).Val;
1306 else
1307 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1308 LHSShift.getOperand(1)).Val;
1309 }
1310 }
1311
1312 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1313 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
1314 if (LHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1315 RHSShift.getOperand(1) == LHSShift.getOperand(1).getOperand(1)) {
1316 if (ConstantSDNode *SUBC =
1317 dyn_cast<ConstantSDNode>(LHSShift.getOperand(1).getOperand(0))) {
1318 if (SUBC->getValue() == OpSizeInBits)
1319 if (HasROTL)
1320 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1321 LHSShift.getOperand(1)).Val;
1322 else
1323 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1324 RHSShift.getOperand(1)).Val;
1325 }
1326 }
1327
1328 return 0;
1329}
1330
1331
Nate Begeman83e75ec2005-09-06 04:43:02 +00001332SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001333 SDOperand N0 = N->getOperand(0);
1334 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001335 SDOperand LHS, RHS, CC;
1336 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1337 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001338 MVT::ValueType VT = N0.getValueType();
1339
1340 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001341 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001342 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001343 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001344 if (N0C && !N1C)
1345 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001346 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001347 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001348 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001349 // reassociate xor
1350 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1351 if (RXOR.Val != 0)
1352 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001353 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001354 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1355 bool isInt = MVT::isInteger(LHS.getValueType());
1356 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1357 isInt);
1358 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001359 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001360 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001361 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001362 assert(0 && "Unhandled SetCC Equivalent!");
1363 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001364 }
Nate Begeman99801192005-09-07 23:25:52 +00001365 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1366 if (N1C && N1C->getValue() == 1 &&
1367 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001368 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001369 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1370 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001371 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1372 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001373 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001374 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001375 }
1376 }
Nate Begeman99801192005-09-07 23:25:52 +00001377 // fold !(x or y) -> (!x and !y) iff x or y are constants
1378 if (N1C && N1C->isAllOnesValue() &&
1379 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001380 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001381 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1382 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001383 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1384 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001385 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001386 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001387 }
1388 }
Nate Begeman223df222005-09-08 20:18:10 +00001389 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1390 if (N1C && N0.getOpcode() == ISD::XOR) {
1391 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1392 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1393 if (N00C)
1394 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1395 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1396 if (N01C)
1397 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1398 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1399 }
1400 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001401 if (N0 == N1) {
1402 if (!MVT::isVector(VT)) {
1403 return DAG.getConstant(0, VT);
1404 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1405 // Produce a vector of zeros.
1406 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1407 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001408 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001409 }
1410 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001411
1412 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1413 if (N0.getOpcode() == N1.getOpcode()) {
1414 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1415 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001416 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001417
Chris Lattner3e104b12006-04-08 04:15:24 +00001418 // Simplify the expression using non-local knowledge.
1419 if (!MVT::isVector(VT) &&
1420 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001421 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001422
Nate Begeman83e75ec2005-09-06 04:43:02 +00001423 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001424}
1425
Nate Begeman83e75ec2005-09-06 04:43:02 +00001426SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001427 SDOperand N0 = N->getOperand(0);
1428 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001429 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1430 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001431 MVT::ValueType VT = N0.getValueType();
1432 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1433
1434 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001435 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001436 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001437 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001438 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001439 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001440 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001441 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001442 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001443 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001444 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001445 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001446 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001447 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001448 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001449 if (SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001450 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001451 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001452 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001453 N0.getOperand(1).getOpcode() == ISD::Constant) {
1454 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001455 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001456 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001457 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001458 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001459 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001460 }
1461 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1462 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001463 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001464 N0.getOperand(1).getOpcode() == ISD::Constant) {
1465 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001466 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001467 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1468 DAG.getConstant(~0ULL << c1, VT));
1469 if (c2 > c1)
1470 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001471 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001472 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001473 return DAG.getNode(ISD::SRL, VT, Mask,
1474 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001475 }
1476 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001477 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001478 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001479 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnercac70592006-03-05 19:53:55 +00001480 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1481 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1482 isa<ConstantSDNode>(N0.getOperand(1))) {
1483 return DAG.getNode(ISD::ADD, VT,
1484 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1485 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1486 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001487 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001488}
1489
Nate Begeman83e75ec2005-09-06 04:43:02 +00001490SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001491 SDOperand N0 = N->getOperand(0);
1492 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001493 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1494 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001495 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001496
1497 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001498 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001499 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001500 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001501 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001502 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001503 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001504 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001505 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001506 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001507 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001508 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001509 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001510 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001511 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001512 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1513 // sext_inreg.
1514 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1515 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1516 MVT::ValueType EVT;
1517 switch (LowBits) {
1518 default: EVT = MVT::Other; break;
1519 case 1: EVT = MVT::i1; break;
1520 case 8: EVT = MVT::i8; break;
1521 case 16: EVT = MVT::i16; break;
1522 case 32: EVT = MVT::i32; break;
1523 }
1524 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1525 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1526 DAG.getValueType(EVT));
1527 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001528
1529 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1530 if (N1C && N0.getOpcode() == ISD::SRA) {
1531 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1532 unsigned Sum = N1C->getValue() + C1->getValue();
1533 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1534 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1535 DAG.getConstant(Sum, N1C->getValueType(0)));
1536 }
1537 }
1538
Chris Lattnera8504462006-05-08 20:51:54 +00001539 // Simplify, based on bits shifted out of the LHS.
1540 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
1541 return SDOperand(N, 0);
1542
1543
Nate Begeman1d4d4142005-09-01 00:19:25 +00001544 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001545 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001546 return DAG.getNode(ISD::SRL, VT, N0, N1);
1547 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001548}
1549
Nate Begeman83e75ec2005-09-06 04:43:02 +00001550SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001551 SDOperand N0 = N->getOperand(0);
1552 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001553 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1554 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001555 MVT::ValueType VT = N0.getValueType();
1556 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1557
1558 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001559 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001560 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001561 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001562 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001563 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001564 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001565 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001566 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001567 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001568 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001569 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001570 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001571 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001572 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001573 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001574 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001575 N0.getOperand(1).getOpcode() == ISD::Constant) {
1576 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001577 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001578 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001579 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001580 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001581 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001582 }
Chris Lattner350bec02006-04-02 06:11:11 +00001583
Chris Lattner06afe072006-05-05 22:53:17 +00001584 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
1585 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1586 // Shifting in all undef bits?
1587 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
1588 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
1589 return DAG.getNode(ISD::UNDEF, VT);
1590
1591 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
1592 AddToWorkList(SmallShift.Val);
1593 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
1594 }
1595
Chris Lattner350bec02006-04-02 06:11:11 +00001596 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1597 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1598 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1599 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1600 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
1601
1602 // If any of the input bits are KnownOne, then the input couldn't be all
1603 // zeros, thus the result of the srl will always be zero.
1604 if (KnownOne) return DAG.getConstant(0, VT);
1605
1606 // If all of the bits input the to ctlz node are known to be zero, then
1607 // the result of the ctlz is "32" and the result of the shift is one.
1608 uint64_t UnknownBits = ~KnownZero & Mask;
1609 if (UnknownBits == 0) return DAG.getConstant(1, VT);
1610
1611 // Otherwise, check to see if there is exactly one bit input to the ctlz.
1612 if ((UnknownBits & (UnknownBits-1)) == 0) {
1613 // Okay, we know that only that the single bit specified by UnknownBits
1614 // could be set on input to the CTLZ node. If this bit is set, the SRL
1615 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
1616 // to an SRL,XOR pair, which is likely to simplify more.
1617 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
1618 SDOperand Op = N0.getOperand(0);
1619 if (ShAmt) {
1620 Op = DAG.getNode(ISD::SRL, VT, Op,
1621 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
1622 AddToWorkList(Op.Val);
1623 }
1624 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
1625 }
1626 }
1627
Nate Begeman83e75ec2005-09-06 04:43:02 +00001628 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001629}
1630
Nate Begeman83e75ec2005-09-06 04:43:02 +00001631SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001632 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001633 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001634
1635 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001636 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001637 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001638 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001639}
1640
Nate Begeman83e75ec2005-09-06 04:43:02 +00001641SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001642 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001643 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001644
1645 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001646 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001647 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001648 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001649}
1650
Nate Begeman83e75ec2005-09-06 04:43:02 +00001651SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001652 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001653 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001654
1655 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001656 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001657 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001658 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001659}
1660
Nate Begeman452d7be2005-09-16 00:54:12 +00001661SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1662 SDOperand N0 = N->getOperand(0);
1663 SDOperand N1 = N->getOperand(1);
1664 SDOperand N2 = N->getOperand(2);
1665 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1666 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1667 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1668 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001669
Nate Begeman452d7be2005-09-16 00:54:12 +00001670 // fold select C, X, X -> X
1671 if (N1 == N2)
1672 return N1;
1673 // fold select true, X, Y -> X
1674 if (N0C && !N0C->isNullValue())
1675 return N1;
1676 // fold select false, X, Y -> Y
1677 if (N0C && N0C->isNullValue())
1678 return N2;
1679 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001680 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001681 return DAG.getNode(ISD::OR, VT, N0, N2);
1682 // fold select C, 0, X -> ~C & X
1683 // FIXME: this should check for C type == X type, not i1?
1684 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1685 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001686 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001687 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1688 }
1689 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001690 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001691 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001692 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001693 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1694 }
1695 // fold select C, X, 0 -> C & X
1696 // FIXME: this should check for C type == X type, not i1?
1697 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1698 return DAG.getNode(ISD::AND, VT, N0, N1);
1699 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1700 if (MVT::i1 == VT && N0 == N1)
1701 return DAG.getNode(ISD::OR, VT, N0, N2);
1702 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1703 if (MVT::i1 == VT && N0 == N2)
1704 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00001705
Chris Lattner40c62d52005-10-18 06:04:22 +00001706 // If we can fold this based on the true/false value, do so.
1707 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00001708 return SDOperand(N, 0); // Don't revisit N.
1709
Nate Begeman44728a72005-09-19 22:34:01 +00001710 // fold selects based on a setcc into other things, such as min/max/abs
1711 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001712 // FIXME:
1713 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1714 // having to say they don't support SELECT_CC on every type the DAG knows
1715 // about, since there is no way to mark an opcode illegal at all value types
1716 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1717 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1718 N1, N2, N0.getOperand(2));
1719 else
1720 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001721 return SDOperand();
1722}
1723
1724SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001725 SDOperand N0 = N->getOperand(0);
1726 SDOperand N1 = N->getOperand(1);
1727 SDOperand N2 = N->getOperand(2);
1728 SDOperand N3 = N->getOperand(3);
1729 SDOperand N4 = N->getOperand(4);
1730 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1731 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1732 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1733 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1734
Nate Begeman44728a72005-09-19 22:34:01 +00001735 // fold select_cc lhs, rhs, x, x, cc -> x
1736 if (N2 == N3)
1737 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001738
Chris Lattner5f42a242006-09-20 06:19:26 +00001739 // Determine if the condition we're dealing with is constant
1740 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
1741
1742 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
1743 if (SCCC->getValue())
1744 return N2; // cond always true -> true val
1745 else
1746 return N3; // cond always false -> false val
1747 }
1748
1749 // Fold to a simpler select_cc
1750 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
1751 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
1752 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
1753 SCC.getOperand(2));
1754
Chris Lattner40c62d52005-10-18 06:04:22 +00001755 // If we can fold this based on the true/false value, do so.
1756 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00001757 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00001758
Nate Begeman44728a72005-09-19 22:34:01 +00001759 // fold select_cc into other things, such as min/max/abs
1760 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001761}
1762
1763SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1764 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1765 cast<CondCodeSDNode>(N->getOperand(2))->get());
1766}
1767
Nate Begeman83e75ec2005-09-06 04:43:02 +00001768SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001769 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001770 MVT::ValueType VT = N->getValueType(0);
1771
Nate Begeman1d4d4142005-09-01 00:19:25 +00001772 // fold (sext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001773 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001774 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00001775
Nate Begeman1d4d4142005-09-01 00:19:25 +00001776 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001777 // fold (sext (aext x)) -> (sext x)
1778 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001779 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00001780
Chris Lattner6007b842006-09-21 06:00:20 +00001781 // fold (sext (truncate x)) -> (sextinreg x).
1782 if (N0.getOpcode() == ISD::TRUNCATE &&
Chris Lattnerbf370872006-09-21 06:17:39 +00001783 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
1784 N0.getValueType()))) {
Chris Lattner6007b842006-09-21 06:00:20 +00001785 SDOperand Op = N0.getOperand(0);
1786 if (Op.getValueType() < VT) {
1787 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
1788 } else if (Op.getValueType() > VT) {
1789 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
1790 }
1791 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001792 DAG.getValueType(N0.getValueType()));
Chris Lattner6007b842006-09-21 06:00:20 +00001793 }
Chris Lattner310b5782006-05-06 23:06:26 +00001794
Evan Cheng110dec22005-12-14 02:19:23 +00001795 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001796 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1797 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001798 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1799 N0.getOperand(1), N0.getOperand(2),
1800 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001801 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001802 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1803 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001804 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00001805 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001806
1807 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1808 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1809 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1810 N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001811 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1812 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1813 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001814 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001815 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1816 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001817 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001818 }
1819
Nate Begeman83e75ec2005-09-06 04:43:02 +00001820 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001821}
1822
Nate Begeman83e75ec2005-09-06 04:43:02 +00001823SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001824 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001825 MVT::ValueType VT = N->getValueType(0);
1826
Nate Begeman1d4d4142005-09-01 00:19:25 +00001827 // fold (zext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001828 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001829 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001830 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001831 // fold (zext (aext x)) -> (zext x)
1832 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001833 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00001834
1835 // fold (zext (truncate x)) -> (and x, mask)
1836 if (N0.getOpcode() == ISD::TRUNCATE &&
1837 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
1838 SDOperand Op = N0.getOperand(0);
1839 if (Op.getValueType() < VT) {
1840 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
1841 } else if (Op.getValueType() > VT) {
1842 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
1843 }
1844 return DAG.getZeroExtendInReg(Op, N0.getValueType());
1845 }
1846
Chris Lattner111c2282006-09-21 06:14:31 +00001847 // fold (zext (and (trunc x), cst)) -> (and x, cst).
1848 if (N0.getOpcode() == ISD::AND &&
1849 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1850 N0.getOperand(1).getOpcode() == ISD::Constant) {
1851 SDOperand X = N0.getOperand(0).getOperand(0);
1852 if (X.getValueType() < VT) {
1853 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
1854 } else if (X.getValueType() > VT) {
1855 X = DAG.getNode(ISD::TRUNCATE, VT, X);
1856 }
1857 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1858 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
1859 }
1860
Evan Cheng110dec22005-12-14 02:19:23 +00001861 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001862 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1863 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001864 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1865 N0.getOperand(1), N0.getOperand(2),
1866 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001867 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001868 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1869 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001870 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00001871 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001872
1873 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1874 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1875 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1876 N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001877 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1878 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1879 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001880 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001881 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1882 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001883 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001884 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001885 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001886}
1887
Chris Lattner5ffc0662006-05-05 05:58:59 +00001888SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
1889 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001890 MVT::ValueType VT = N->getValueType(0);
1891
1892 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001893 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00001894 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
1895 // fold (aext (aext x)) -> (aext x)
1896 // fold (aext (zext x)) -> (zext x)
1897 // fold (aext (sext x)) -> (sext x)
1898 if (N0.getOpcode() == ISD::ANY_EXTEND ||
1899 N0.getOpcode() == ISD::ZERO_EXTEND ||
1900 N0.getOpcode() == ISD::SIGN_EXTEND)
1901 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
1902
Chris Lattner84750582006-09-20 06:29:17 +00001903 // fold (aext (truncate x))
1904 if (N0.getOpcode() == ISD::TRUNCATE) {
1905 SDOperand TruncOp = N0.getOperand(0);
1906 if (TruncOp.getValueType() == VT)
1907 return TruncOp; // x iff x size == zext size.
1908 if (TruncOp.getValueType() > VT)
1909 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
1910 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
1911 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00001912
1913 // fold (aext (and (trunc x), cst)) -> (and x, cst).
1914 if (N0.getOpcode() == ISD::AND &&
1915 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1916 N0.getOperand(1).getOpcode() == ISD::Constant) {
1917 SDOperand X = N0.getOperand(0).getOperand(0);
1918 if (X.getValueType() < VT) {
1919 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
1920 } else if (X.getValueType() > VT) {
1921 X = DAG.getNode(ISD::TRUNCATE, VT, X);
1922 }
1923 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1924 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
1925 }
1926
Chris Lattner5ffc0662006-05-05 05:58:59 +00001927 // fold (aext (load x)) -> (aext (truncate (extload x)))
1928 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1929 (!AfterLegalize||TLI.isOperationLegal(ISD::EXTLOAD, N0.getValueType()))) {
1930 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0),
1931 N0.getOperand(1), N0.getOperand(2),
1932 N0.getValueType());
1933 CombineTo(N, ExtLoad);
1934 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1935 ExtLoad.getValue(1));
1936 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1937 }
1938
1939 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
1940 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
1941 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
1942 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD ||
1943 N0.getOpcode() == ISD::SEXTLOAD) &&
1944 N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001945 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1946 SDOperand ExtLoad = DAG.getExtLoad(N0.getOpcode(), VT, N0.getOperand(0),
1947 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001948 CombineTo(N, ExtLoad);
1949 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1950 ExtLoad.getValue(1));
1951 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1952 }
1953 return SDOperand();
1954}
1955
1956
Nate Begeman83e75ec2005-09-06 04:43:02 +00001957SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001958 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001959 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001960 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001961 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001962 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001963
Nate Begeman1d4d4142005-09-01 00:19:25 +00001964 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00001965 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00001966 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00001967
Chris Lattner541a24f2006-05-06 22:43:44 +00001968 // If the input is already sign extended, just drop the extension.
Chris Lattneree4ea922006-05-06 09:30:03 +00001969 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
1970 return N0;
1971
Nate Begeman646d7e22005-09-02 21:18:40 +00001972 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1973 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1974 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001975 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001976 }
Chris Lattner4b37e872006-05-08 21:18:59 +00001977
Nate Begeman07ed4172005-10-10 21:26:48 +00001978 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001979 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00001980 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00001981
1982 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
1983 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
1984 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
1985 if (N0.getOpcode() == ISD::SRL) {
1986 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
1987 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
1988 // We can turn this into an SRA iff the input to the SRL is already sign
1989 // extended enough.
1990 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
1991 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
1992 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
1993 }
1994 }
1995
Nate Begemanded49632005-10-13 03:11:28 +00001996 // fold (sext_inreg (extload x)) -> (sextload x)
1997 if (N0.getOpcode() == ISD::EXTLOAD &&
1998 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001999 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00002000 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
2001 N0.getOperand(1), N0.getOperand(2),
2002 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002003 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002004 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002005 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002006 }
2007 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00002008 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00002009 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00002010 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00002011 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
2012 N0.getOperand(1), N0.getOperand(2),
2013 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002014 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002015 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002016 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002017 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002018 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002019}
2020
Nate Begeman83e75ec2005-09-06 04:43:02 +00002021SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002022 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002023 MVT::ValueType VT = N->getValueType(0);
2024
2025 // noop truncate
2026 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002027 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002028 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002029 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002030 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002031 // fold (truncate (truncate x)) -> (truncate x)
2032 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002033 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002034 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00002035 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2036 N0.getOpcode() == ISD::ANY_EXTEND) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002037 if (N0.getValueType() < VT)
2038 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00002039 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002040 else if (N0.getValueType() > VT)
2041 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002042 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002043 else
2044 // if the source and dest are the same type, we can drop both the extend
2045 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002046 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002047 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002048 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00002049 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00002050 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
2051 "Cannot truncate to larger type!");
2052 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00002053 // For big endian targets, we need to add an offset to the pointer to load
2054 // the correct bytes. For little endian systems, we merely need to read
2055 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00002056 uint64_t PtrOff =
2057 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00002058 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
2059 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
2060 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00002061 AddToWorkList(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00002062 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00002063 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00002064 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002065 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002066 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002067 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002068}
2069
Chris Lattner94683772005-12-23 05:30:37 +00002070SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2071 SDOperand N0 = N->getOperand(0);
2072 MVT::ValueType VT = N->getValueType(0);
2073
2074 // If the input is a constant, let getNode() fold it.
2075 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2076 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2077 if (Res.Val != N) return Res;
2078 }
2079
Chris Lattnerc8547d82005-12-23 05:37:50 +00002080 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2081 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00002082
Chris Lattner57104102005-12-23 05:44:41 +00002083 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002084 // FIXME: These xforms need to know that the resultant load doesn't need a
2085 // higher alignment than the original!
2086 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00002087 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
2088 N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00002089 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00002090 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2091 Load.getValue(1));
2092 return Load;
2093 }
2094
Chris Lattner94683772005-12-23 05:30:37 +00002095 return SDOperand();
2096}
2097
Chris Lattner6258fb22006-04-02 02:53:43 +00002098SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2099 SDOperand N0 = N->getOperand(0);
2100 MVT::ValueType VT = N->getValueType(0);
2101
2102 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2103 // First check to see if this is all constant.
2104 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2105 VT == MVT::Vector) {
2106 bool isSimple = true;
2107 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2108 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2109 N0.getOperand(i).getOpcode() != ISD::Constant &&
2110 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2111 isSimple = false;
2112 break;
2113 }
2114
Chris Lattner97c20732006-04-03 17:29:28 +00002115 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2116 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002117 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2118 }
2119 }
2120
2121 return SDOperand();
2122}
2123
2124/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2125/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2126/// destination element value type.
2127SDOperand DAGCombiner::
2128ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2129 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2130
2131 // If this is already the right type, we're done.
2132 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2133
2134 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2135 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2136
2137 // If this is a conversion of N elements of one type to N elements of another
2138 // type, convert each element. This handles FP<->INT cases.
2139 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002140 SmallVector<SDOperand, 8> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00002141 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002142 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002143 AddToWorkList(Ops.back().Val);
2144 }
Chris Lattner6258fb22006-04-02 02:53:43 +00002145 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2146 Ops.push_back(DAG.getValueType(DstEltVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002147 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002148 }
2149
2150 // Otherwise, we're growing or shrinking the elements. To avoid having to
2151 // handle annoying details of growing/shrinking FP values, we convert them to
2152 // int first.
2153 if (MVT::isFloatingPoint(SrcEltVT)) {
2154 // Convert the input float vector to a int vector where the elements are the
2155 // same sizes.
2156 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2157 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2158 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2159 SrcEltVT = IntVT;
2160 }
2161
2162 // Now we know the input is an integer vector. If the output is a FP type,
2163 // convert to integer first, then to FP of the right size.
2164 if (MVT::isFloatingPoint(DstEltVT)) {
2165 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2166 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2167 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2168
2169 // Next, convert to FP elements of the same size.
2170 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2171 }
2172
2173 // Okay, we know the src/dst types are both integers of differing types.
2174 // Handling growing first.
2175 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2176 if (SrcBitSize < DstBitSize) {
2177 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2178
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002179 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002180 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2181 i += NumInputsPerOutput) {
2182 bool isLE = TLI.isLittleEndian();
2183 uint64_t NewBits = 0;
2184 bool EltIsUndef = true;
2185 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2186 // Shift the previously computed bits over.
2187 NewBits <<= SrcBitSize;
2188 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2189 if (Op.getOpcode() == ISD::UNDEF) continue;
2190 EltIsUndef = false;
2191
2192 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2193 }
2194
2195 if (EltIsUndef)
2196 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2197 else
2198 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2199 }
2200
2201 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2202 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002203 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002204 }
2205
2206 // Finally, this must be the case where we are shrinking elements: each input
2207 // turns into multiple outputs.
2208 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002209 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002210 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2211 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2212 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2213 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2214 continue;
2215 }
2216 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2217
2218 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2219 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2220 OpVal >>= DstBitSize;
2221 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2222 }
2223
2224 // For big endian targets, swap the order of the pieces of each element.
2225 if (!TLI.isLittleEndian())
2226 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2227 }
2228 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2229 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002230 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002231}
2232
2233
2234
Chris Lattner01b3d732005-09-28 22:28:18 +00002235SDOperand DAGCombiner::visitFADD(SDNode *N) {
2236 SDOperand N0 = N->getOperand(0);
2237 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002238 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2239 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002240 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002241
2242 // fold (fadd c1, c2) -> c1+c2
2243 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002244 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002245 // canonicalize constant to RHS
2246 if (N0CFP && !N1CFP)
2247 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002248 // fold (A + (-B)) -> A-B
2249 if (N1.getOpcode() == ISD::FNEG)
2250 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002251 // fold ((-A) + B) -> B-A
2252 if (N0.getOpcode() == ISD::FNEG)
2253 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002254 return SDOperand();
2255}
2256
2257SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2258 SDOperand N0 = N->getOperand(0);
2259 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002260 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2261 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002262 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002263
2264 // fold (fsub c1, c2) -> c1-c2
2265 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002266 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002267 // fold (A-(-B)) -> A+B
2268 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00002269 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002270 return SDOperand();
2271}
2272
2273SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2274 SDOperand N0 = N->getOperand(0);
2275 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002276 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2277 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002278 MVT::ValueType VT = N->getValueType(0);
2279
Nate Begeman11af4ea2005-10-17 20:40:11 +00002280 // fold (fmul c1, c2) -> c1*c2
2281 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002282 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002283 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002284 if (N0CFP && !N1CFP)
2285 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002286 // fold (fmul X, 2.0) -> (fadd X, X)
2287 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2288 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002289 return SDOperand();
2290}
2291
2292SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2293 SDOperand N0 = N->getOperand(0);
2294 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002295 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2296 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002297 MVT::ValueType VT = N->getValueType(0);
2298
Nate Begemana148d982006-01-18 22:35:16 +00002299 // fold (fdiv c1, c2) -> c1/c2
2300 if (N0CFP && N1CFP)
2301 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002302 return SDOperand();
2303}
2304
2305SDOperand DAGCombiner::visitFREM(SDNode *N) {
2306 SDOperand N0 = N->getOperand(0);
2307 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002308 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2309 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002310 MVT::ValueType VT = N->getValueType(0);
2311
Nate Begemana148d982006-01-18 22:35:16 +00002312 // fold (frem c1, c2) -> fmod(c1,c2)
2313 if (N0CFP && N1CFP)
2314 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002315 return SDOperand();
2316}
2317
Chris Lattner12d83032006-03-05 05:30:57 +00002318SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2319 SDOperand N0 = N->getOperand(0);
2320 SDOperand N1 = N->getOperand(1);
2321 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2322 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2323 MVT::ValueType VT = N->getValueType(0);
2324
2325 if (N0CFP && N1CFP) // Constant fold
2326 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2327
2328 if (N1CFP) {
2329 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2330 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2331 union {
2332 double d;
2333 int64_t i;
2334 } u;
2335 u.d = N1CFP->getValue();
2336 if (u.i >= 0)
2337 return DAG.getNode(ISD::FABS, VT, N0);
2338 else
2339 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2340 }
2341
2342 // copysign(fabs(x), y) -> copysign(x, y)
2343 // copysign(fneg(x), y) -> copysign(x, y)
2344 // copysign(copysign(x,z), y) -> copysign(x, y)
2345 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2346 N0.getOpcode() == ISD::FCOPYSIGN)
2347 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2348
2349 // copysign(x, abs(y)) -> abs(x)
2350 if (N1.getOpcode() == ISD::FABS)
2351 return DAG.getNode(ISD::FABS, VT, N0);
2352
2353 // copysign(x, copysign(y,z)) -> copysign(x, z)
2354 if (N1.getOpcode() == ISD::FCOPYSIGN)
2355 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2356
2357 // copysign(x, fp_extend(y)) -> copysign(x, y)
2358 // copysign(x, fp_round(y)) -> copysign(x, y)
2359 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2360 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2361
2362 return SDOperand();
2363}
2364
2365
Chris Lattner01b3d732005-09-28 22:28:18 +00002366
Nate Begeman83e75ec2005-09-06 04:43:02 +00002367SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002368 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002369 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002370 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002371
2372 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002373 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002374 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002375 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002376}
2377
Nate Begeman83e75ec2005-09-06 04:43:02 +00002378SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002379 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002380 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002381 MVT::ValueType VT = N->getValueType(0);
2382
Nate Begeman1d4d4142005-09-01 00:19:25 +00002383 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002384 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002385 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002386 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002387}
2388
Nate Begeman83e75ec2005-09-06 04:43:02 +00002389SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002390 SDOperand N0 = N->getOperand(0);
2391 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2392 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002393
2394 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002395 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002396 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002397 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002398}
2399
Nate Begeman83e75ec2005-09-06 04:43:02 +00002400SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002401 SDOperand N0 = N->getOperand(0);
2402 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2403 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002404
2405 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002406 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002407 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002408 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002409}
2410
Nate Begeman83e75ec2005-09-06 04:43:02 +00002411SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002412 SDOperand N0 = N->getOperand(0);
2413 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2414 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002415
2416 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002417 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002418 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002419
2420 // fold (fp_round (fp_extend x)) -> x
2421 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2422 return N0.getOperand(0);
2423
2424 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2425 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2426 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2427 AddToWorkList(Tmp.Val);
2428 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2429 }
2430
Nate Begeman83e75ec2005-09-06 04:43:02 +00002431 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002432}
2433
Nate Begeman83e75ec2005-09-06 04:43:02 +00002434SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002435 SDOperand N0 = N->getOperand(0);
2436 MVT::ValueType VT = N->getValueType(0);
2437 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002438 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002439
Nate Begeman1d4d4142005-09-01 00:19:25 +00002440 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002441 if (N0CFP) {
2442 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002443 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002444 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002445 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002446}
2447
Nate Begeman83e75ec2005-09-06 04:43:02 +00002448SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002449 SDOperand N0 = N->getOperand(0);
2450 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2451 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002452
2453 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002454 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002455 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00002456
2457 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
2458 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
2459 (!AfterLegalize||TLI.isOperationLegal(ISD::EXTLOAD, N0.getValueType()))) {
2460 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0),
2461 N0.getOperand(1), N0.getOperand(2),
2462 N0.getValueType());
2463 CombineTo(N, ExtLoad);
2464 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
2465 ExtLoad.getValue(1));
2466 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2467 }
2468
2469
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::visitFNEG(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);
2477
2478 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002479 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002480 return DAG.getNode(ISD::FNEG, VT, N0);
2481 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002482 if (N0.getOpcode() == ISD::SUB)
2483 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002484 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002485 if (N0.getOpcode() == ISD::FNEG)
2486 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002487 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002488}
2489
Nate Begeman83e75ec2005-09-06 04:43:02 +00002490SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002491 SDOperand N0 = N->getOperand(0);
2492 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2493 MVT::ValueType VT = N->getValueType(0);
2494
Nate Begeman1d4d4142005-09-01 00:19:25 +00002495 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002496 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002497 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002498 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002499 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002500 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002501 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002502 // fold (fabs (fcopysign x, y)) -> (fabs x)
2503 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2504 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2505
Nate Begeman83e75ec2005-09-06 04:43:02 +00002506 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002507}
2508
Nate Begeman44728a72005-09-19 22:34:01 +00002509SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2510 SDOperand Chain = N->getOperand(0);
2511 SDOperand N1 = N->getOperand(1);
2512 SDOperand N2 = N->getOperand(2);
2513 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2514
2515 // never taken branch, fold to chain
2516 if (N1C && N1C->isNullValue())
2517 return Chain;
2518 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002519 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002520 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002521 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2522 // on the target.
2523 if (N1.getOpcode() == ISD::SETCC &&
2524 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2525 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2526 N1.getOperand(0), N1.getOperand(1), N2);
2527 }
Nate Begeman44728a72005-09-19 22:34:01 +00002528 return SDOperand();
2529}
2530
Chris Lattner3ea0b472005-10-05 06:47:48 +00002531// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2532//
Nate Begeman44728a72005-09-19 22:34:01 +00002533SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002534 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2535 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2536
2537 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002538 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2539 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2540
2541 // fold br_cc true, dest -> br dest (unconditional branch)
2542 if (SCCC && SCCC->getValue())
2543 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2544 N->getOperand(4));
2545 // fold br_cc false, dest -> unconditional fall through
2546 if (SCCC && SCCC->isNullValue())
2547 return N->getOperand(0);
2548 // fold to a simpler setcc
2549 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2550 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2551 Simp.getOperand(2), Simp.getOperand(0),
2552 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002553 return SDOperand();
2554}
2555
Chris Lattner01a22022005-10-10 22:04:48 +00002556SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2557 SDOperand Chain = N->getOperand(0);
2558 SDOperand Ptr = N->getOperand(1);
2559 SDOperand SrcValue = N->getOperand(2);
Chris Lattnere4b95392006-03-31 18:06:18 +00002560
2561 // If there are no uses of the loaded value, change uses of the chain value
2562 // into uses of the chain input (i.e. delete the dead load).
2563 if (N->hasNUsesOfValue(0, 0))
2564 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
Chris Lattner01a22022005-10-10 22:04:48 +00002565
2566 // If this load is directly stored, replace the load value with the stored
2567 // value.
2568 // TODO: Handle store large -> read small portion.
2569 // TODO: Handle TRUNCSTORE/EXTLOAD
2570 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2571 Chain.getOperand(1).getValueType() == N->getValueType(0))
2572 return CombineTo(N, Chain.getOperand(1), Chain);
2573
2574 return SDOperand();
2575}
2576
Chris Lattner29cd7db2006-03-31 18:10:41 +00002577/// visitXEXTLOAD - Handle EXTLOAD/ZEXTLOAD/SEXTLOAD.
2578SDOperand DAGCombiner::visitXEXTLOAD(SDNode *N) {
2579 SDOperand Chain = N->getOperand(0);
2580 SDOperand Ptr = N->getOperand(1);
2581 SDOperand SrcValue = N->getOperand(2);
2582 SDOperand EVT = N->getOperand(3);
2583
2584 // If there are no uses of the loaded value, change uses of the chain value
2585 // into uses of the chain input (i.e. delete the dead load).
2586 if (N->hasNUsesOfValue(0, 0))
2587 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
2588
2589 return SDOperand();
2590}
2591
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00002592/// isNotAlias - Return true if we have definitive knowlege that the two
2593/// addresses don't overlap.
2594bool DAGCombiner::isNotAlias(SDOperand Ptr1, SDOperand Ptr2) {
2595 // Mind the flag.
2596 if (!CombinerAA) return false;
2597
Jim Laskey516b0ea2006-09-21 17:35:47 +00002598 // If they are the same then they must be aliases.
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00002599 if (Ptr1 == Ptr2) return false;
2600
Jim Laskey516b0ea2006-09-21 17:35:47 +00002601 // If both operands are frame values (not the same location from above test)
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00002602 // then they can't alias.
2603 FrameIndexSDNode *FI1 = dyn_cast<FrameIndexSDNode>(Ptr1);
2604 FrameIndexSDNode *FI2 = dyn_cast<FrameIndexSDNode>(Ptr2);
Jim Laskey516b0ea2006-09-21 17:35:47 +00002605 if (FI1 && FI2) {
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00002606 return true;
2607 }
2608
2609 // Otherwise we don't know and have to play it safe.
2610 return false;
2611}
2612
Chris Lattner87514ca2005-10-10 22:31:19 +00002613SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2614 SDOperand Chain = N->getOperand(0);
2615 SDOperand Value = N->getOperand(1);
2616 SDOperand Ptr = N->getOperand(2);
2617 SDOperand SrcValue = N->getOperand(3);
2618
2619 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002620 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002621 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2622 // Make sure that these stores are the same value type:
2623 // FIXME: we really care that the second store is >= size of the first.
2624 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002625 // Create a new store of Value that replaces both stores.
2626 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002627 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2628 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002629 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2630 PrevStore->getOperand(0), Value, Ptr,
2631 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002632 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002633 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002634 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002635 }
2636
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002637 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002638 // FIXME: This needs to know that the resultant store does not need a
2639 // higher alignment than the original.
2640 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002641 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2642 Ptr, SrcValue);
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00002643
2644 // If the previous store is not an alias then break artificial chain.
2645 if (Chain.getOpcode() == ISD::STORE && isNotAlias(Ptr, Chain.getOperand(2))) {
2646 // Replace the chain to void dependency.
2647 SDNode *PrevStore = Chain.Val;
2648 SDOperand ReplStore = DAG.getNode(ISD::STORE, MVT::Other,
2649 PrevStore->getOperand(0), Value, Ptr,
2650 SrcValue);
2651 // Create token to keep both stores around.
2652 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
2653 Chain, ReplStore);
2654 // Replace uses with token.
Jim Laskey516b0ea2006-09-21 17:35:47 +00002655 return Token;
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00002656 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002657
Chris Lattner87514ca2005-10-10 22:31:19 +00002658 return SDOperand();
2659}
2660
Chris Lattnerca242442006-03-19 01:27:56 +00002661SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2662 SDOperand InVec = N->getOperand(0);
2663 SDOperand InVal = N->getOperand(1);
2664 SDOperand EltNo = N->getOperand(2);
2665
2666 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2667 // vector with the inserted element.
2668 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2669 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002670 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002671 if (Elt < Ops.size())
2672 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002673 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
2674 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002675 }
2676
2677 return SDOperand();
2678}
2679
2680SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2681 SDOperand InVec = N->getOperand(0);
2682 SDOperand InVal = N->getOperand(1);
2683 SDOperand EltNo = N->getOperand(2);
2684 SDOperand NumElts = N->getOperand(3);
2685 SDOperand EltType = N->getOperand(4);
2686
2687 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2688 // vector with the inserted element.
2689 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2690 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002691 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002692 if (Elt < Ops.size()-2)
2693 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002694 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
2695 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002696 }
2697
2698 return SDOperand();
2699}
2700
Chris Lattnerd7648c82006-03-28 20:28:38 +00002701SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
2702 unsigned NumInScalars = N->getNumOperands()-2;
2703 SDOperand NumElts = N->getOperand(NumInScalars);
2704 SDOperand EltType = N->getOperand(NumInScalars+1);
2705
2706 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
2707 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
2708 // two distinct vectors, turn this into a shuffle node.
2709 SDOperand VecIn1, VecIn2;
2710 for (unsigned i = 0; i != NumInScalars; ++i) {
2711 // Ignore undef inputs.
2712 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
2713
2714 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
2715 // constant index, bail out.
2716 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
2717 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
2718 VecIn1 = VecIn2 = SDOperand(0, 0);
2719 break;
2720 }
2721
2722 // If the input vector type disagrees with the result of the vbuild_vector,
2723 // we can't make a shuffle.
2724 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
2725 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
2726 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
2727 VecIn1 = VecIn2 = SDOperand(0, 0);
2728 break;
2729 }
2730
2731 // Otherwise, remember this. We allow up to two distinct input vectors.
2732 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
2733 continue;
2734
2735 if (VecIn1.Val == 0) {
2736 VecIn1 = ExtractedFromVec;
2737 } else if (VecIn2.Val == 0) {
2738 VecIn2 = ExtractedFromVec;
2739 } else {
2740 // Too many inputs.
2741 VecIn1 = VecIn2 = SDOperand(0, 0);
2742 break;
2743 }
2744 }
2745
2746 // If everything is good, we can make a shuffle operation.
2747 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002748 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00002749 for (unsigned i = 0; i != NumInScalars; ++i) {
2750 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
2751 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2752 continue;
2753 }
2754
2755 SDOperand Extract = N->getOperand(i);
2756
2757 // If extracting from the first vector, just use the index directly.
2758 if (Extract.getOperand(0) == VecIn1) {
2759 BuildVecIndices.push_back(Extract.getOperand(1));
2760 continue;
2761 }
2762
2763 // Otherwise, use InIdx + VecSize
2764 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
2765 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
2766 }
2767
2768 // Add count and size info.
2769 BuildVecIndices.push_back(NumElts);
2770 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
2771
2772 // Return the new VVECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002773 SDOperand Ops[5];
2774 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00002775 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002776 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00002777 } else {
2778 // Use an undef vbuild_vector as input for the second operand.
2779 std::vector<SDOperand> UnOps(NumInScalars,
2780 DAG.getNode(ISD::UNDEF,
2781 cast<VTSDNode>(EltType)->getVT()));
2782 UnOps.push_back(NumElts);
2783 UnOps.push_back(EltType);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002784 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2785 &UnOps[0], UnOps.size());
2786 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00002787 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002788 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2789 &BuildVecIndices[0], BuildVecIndices.size());
2790 Ops[3] = NumElts;
2791 Ops[4] = EltType;
2792 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
Chris Lattnerd7648c82006-03-28 20:28:38 +00002793 }
2794
2795 return SDOperand();
2796}
2797
Chris Lattner66445d32006-03-28 22:11:53 +00002798SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002799 SDOperand ShufMask = N->getOperand(2);
2800 unsigned NumElts = ShufMask.getNumOperands();
2801
2802 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2803 bool isIdentity = true;
2804 for (unsigned i = 0; i != NumElts; ++i) {
2805 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2806 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2807 isIdentity = false;
2808 break;
2809 }
2810 }
2811 if (isIdentity) return N->getOperand(0);
2812
2813 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2814 isIdentity = true;
2815 for (unsigned i = 0; i != NumElts; ++i) {
2816 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2817 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2818 isIdentity = false;
2819 break;
2820 }
2821 }
2822 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00002823
2824 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
2825 // needed at all.
2826 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00002827 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002828 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00002829 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002830 for (unsigned i = 0; i != NumElts; ++i)
2831 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
2832 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
2833 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00002834 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00002835 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00002836 BaseIdx = Idx;
2837 } else {
2838 if (BaseIdx != Idx)
2839 isSplat = false;
2840 if (VecNum != V) {
2841 isUnary = false;
2842 break;
2843 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00002844 }
2845 }
2846
2847 SDOperand N0 = N->getOperand(0);
2848 SDOperand N1 = N->getOperand(1);
2849 // Normalize unary shuffle so the RHS is undef.
2850 if (isUnary && VecNum == 1)
2851 std::swap(N0, N1);
2852
Evan Cheng917ec982006-07-21 08:25:53 +00002853 // If it is a splat, check if the argument vector is a build_vector with
2854 // all scalar elements the same.
2855 if (isSplat) {
2856 SDNode *V = N0.Val;
2857 if (V->getOpcode() == ISD::BIT_CONVERT)
2858 V = V->getOperand(0).Val;
2859 if (V->getOpcode() == ISD::BUILD_VECTOR) {
2860 unsigned NumElems = V->getNumOperands()-2;
2861 if (NumElems > BaseIdx) {
2862 SDOperand Base;
2863 bool AllSame = true;
2864 for (unsigned i = 0; i != NumElems; ++i) {
2865 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
2866 Base = V->getOperand(i);
2867 break;
2868 }
2869 }
2870 // Splat of <u, u, u, u>, return <u, u, u, u>
2871 if (!Base.Val)
2872 return N0;
2873 for (unsigned i = 0; i != NumElems; ++i) {
2874 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
2875 V->getOperand(i) != Base) {
2876 AllSame = false;
2877 break;
2878 }
2879 }
2880 // Splat of <x, x, x, x>, return <x, x, x, x>
2881 if (AllSame)
2882 return N0;
2883 }
2884 }
2885 }
2886
Evan Chenge7bec0d2006-07-20 22:44:41 +00002887 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
2888 // into an undef.
2889 if (isUnary || N0 == N1) {
2890 if (N0.getOpcode() == ISD::UNDEF)
Evan Chengc04766a2006-04-06 23:20:43 +00002891 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00002892 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2893 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002894 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00002895 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00002896 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2897 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2898 MappedOps.push_back(ShufMask.getOperand(i));
2899 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00002900 unsigned NewIdx =
2901 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2902 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00002903 }
2904 }
2905 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002906 &MappedOps[0], MappedOps.size());
Chris Lattner3e104b12006-04-08 04:15:24 +00002907 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00002908 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
Evan Chenge7bec0d2006-07-20 22:44:41 +00002909 N0,
Chris Lattner66445d32006-03-28 22:11:53 +00002910 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
2911 ShufMask);
2912 }
2913
2914 return SDOperand();
2915}
2916
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002917SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
2918 SDOperand ShufMask = N->getOperand(2);
2919 unsigned NumElts = ShufMask.getNumOperands()-2;
2920
2921 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2922 bool isIdentity = true;
2923 for (unsigned i = 0; i != NumElts; ++i) {
2924 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2925 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2926 isIdentity = false;
2927 break;
2928 }
2929 }
2930 if (isIdentity) return N->getOperand(0);
2931
2932 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2933 isIdentity = true;
2934 for (unsigned i = 0; i != NumElts; ++i) {
2935 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2936 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2937 isIdentity = false;
2938 break;
2939 }
2940 }
2941 if (isIdentity) return N->getOperand(1);
2942
Evan Chenge7bec0d2006-07-20 22:44:41 +00002943 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
2944 // needed at all.
2945 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00002946 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002947 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00002948 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002949 for (unsigned i = 0; i != NumElts; ++i)
2950 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
2951 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
2952 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00002953 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00002954 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00002955 BaseIdx = Idx;
2956 } else {
2957 if (BaseIdx != Idx)
2958 isSplat = false;
2959 if (VecNum != V) {
2960 isUnary = false;
2961 break;
2962 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00002963 }
2964 }
2965
2966 SDOperand N0 = N->getOperand(0);
2967 SDOperand N1 = N->getOperand(1);
2968 // Normalize unary shuffle so the RHS is undef.
2969 if (isUnary && VecNum == 1)
2970 std::swap(N0, N1);
2971
Evan Cheng917ec982006-07-21 08:25:53 +00002972 // If it is a splat, check if the argument vector is a build_vector with
2973 // all scalar elements the same.
2974 if (isSplat) {
2975 SDNode *V = N0.Val;
2976 if (V->getOpcode() == ISD::VBIT_CONVERT)
2977 V = V->getOperand(0).Val;
2978 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
2979 unsigned NumElems = V->getNumOperands()-2;
2980 if (NumElems > BaseIdx) {
2981 SDOperand Base;
2982 bool AllSame = true;
2983 for (unsigned i = 0; i != NumElems; ++i) {
2984 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
2985 Base = V->getOperand(i);
2986 break;
2987 }
2988 }
2989 // Splat of <u, u, u, u>, return <u, u, u, u>
2990 if (!Base.Val)
2991 return N0;
2992 for (unsigned i = 0; i != NumElems; ++i) {
2993 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
2994 V->getOperand(i) != Base) {
2995 AllSame = false;
2996 break;
2997 }
2998 }
2999 // Splat of <x, x, x, x>, return <x, x, x, x>
3000 if (AllSame)
3001 return N0;
3002 }
3003 }
3004 }
3005
Evan Chenge7bec0d2006-07-20 22:44:41 +00003006 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3007 // into an undef.
3008 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00003009 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3010 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003011 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00003012 for (unsigned i = 0; i != NumElts; ++i) {
3013 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3014 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3015 MappedOps.push_back(ShufMask.getOperand(i));
3016 } else {
3017 unsigned NewIdx =
3018 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3019 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
3020 }
3021 }
3022 // Add the type/#elts values.
3023 MappedOps.push_back(ShufMask.getOperand(NumElts));
3024 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
3025
3026 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003027 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003028 AddToWorkList(ShufMask.Val);
3029
3030 // Build the undef vector.
3031 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
3032 for (unsigned i = 0; i != NumElts; ++i)
3033 MappedOps[i] = UDVal;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003034 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
3035 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003036 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3037 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003038
3039 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
Evan Chenge7bec0d2006-07-20 22:44:41 +00003040 N0, UDVal, ShufMask,
Chris Lattner17614ea2006-04-08 05:34:25 +00003041 MappedOps[NumElts], MappedOps[NumElts+1]);
3042 }
3043
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003044 return SDOperand();
3045}
3046
Evan Cheng44f1f092006-04-20 08:56:16 +00003047/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
3048/// a VAND to a vector_shuffle with the destination vector and a zero vector.
3049/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
3050/// vector_shuffle V, Zero, <0, 4, 2, 4>
3051SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
3052 SDOperand LHS = N->getOperand(0);
3053 SDOperand RHS = N->getOperand(1);
3054 if (N->getOpcode() == ISD::VAND) {
3055 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
3056 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
3057 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
3058 RHS = RHS.getOperand(0);
3059 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
3060 std::vector<SDOperand> IdxOps;
3061 unsigned NumOps = RHS.getNumOperands();
3062 unsigned NumElts = NumOps-2;
3063 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
3064 for (unsigned i = 0; i != NumElts; ++i) {
3065 SDOperand Elt = RHS.getOperand(i);
3066 if (!isa<ConstantSDNode>(Elt))
3067 return SDOperand();
3068 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
3069 IdxOps.push_back(DAG.getConstant(i, EVT));
3070 else if (cast<ConstantSDNode>(Elt)->isNullValue())
3071 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
3072 else
3073 return SDOperand();
3074 }
3075
3076 // Let's see if the target supports this vector_shuffle.
3077 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
3078 return SDOperand();
3079
3080 // Return the new VVECTOR_SHUFFLE node.
3081 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
3082 SDOperand EVTNode = DAG.getValueType(EVT);
3083 std::vector<SDOperand> Ops;
Chris Lattner516b9622006-09-14 20:50:57 +00003084 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode,
3085 EVTNode);
Evan Cheng44f1f092006-04-20 08:56:16 +00003086 Ops.push_back(LHS);
3087 AddToWorkList(LHS.Val);
3088 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
3089 ZeroOps.push_back(NumEltsNode);
3090 ZeroOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003091 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3092 &ZeroOps[0], ZeroOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003093 IdxOps.push_back(NumEltsNode);
3094 IdxOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003095 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3096 &IdxOps[0], IdxOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003097 Ops.push_back(NumEltsNode);
3098 Ops.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003099 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
3100 &Ops[0], Ops.size());
Evan Cheng44f1f092006-04-20 08:56:16 +00003101 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
3102 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
3103 DstVecSize, DstVecEVT);
3104 }
3105 return Result;
3106 }
3107 }
3108 return SDOperand();
3109}
3110
Chris Lattneredab1b92006-04-02 03:25:57 +00003111/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
3112/// the scalar operation of the vop if it is operating on an integer vector
3113/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
3114SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
3115 ISD::NodeType FPOp) {
3116 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
3117 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
3118 SDOperand LHS = N->getOperand(0);
3119 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00003120 SDOperand Shuffle = XformToShuffleWithZero(N);
3121 if (Shuffle.Val) return Shuffle;
3122
Chris Lattneredab1b92006-04-02 03:25:57 +00003123 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
3124 // this operation.
3125 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
3126 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003127 SmallVector<SDOperand, 8> Ops;
Chris Lattneredab1b92006-04-02 03:25:57 +00003128 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
3129 SDOperand LHSOp = LHS.getOperand(i);
3130 SDOperand RHSOp = RHS.getOperand(i);
3131 // If these two elements can't be folded, bail out.
3132 if ((LHSOp.getOpcode() != ISD::UNDEF &&
3133 LHSOp.getOpcode() != ISD::Constant &&
3134 LHSOp.getOpcode() != ISD::ConstantFP) ||
3135 (RHSOp.getOpcode() != ISD::UNDEF &&
3136 RHSOp.getOpcode() != ISD::Constant &&
3137 RHSOp.getOpcode() != ISD::ConstantFP))
3138 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00003139 // Can't fold divide by zero.
3140 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
3141 if ((RHSOp.getOpcode() == ISD::Constant &&
3142 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
3143 (RHSOp.getOpcode() == ISD::ConstantFP &&
3144 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
3145 break;
3146 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003147 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00003148 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00003149 assert((Ops.back().getOpcode() == ISD::UNDEF ||
3150 Ops.back().getOpcode() == ISD::Constant ||
3151 Ops.back().getOpcode() == ISD::ConstantFP) &&
3152 "Scalar binop didn't fold!");
3153 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003154
3155 if (Ops.size() == LHS.getNumOperands()-2) {
3156 Ops.push_back(*(LHS.Val->op_end()-2));
3157 Ops.push_back(*(LHS.Val->op_end()-1));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003158 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003159 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003160 }
3161
3162 return SDOperand();
3163}
3164
Nate Begeman44728a72005-09-19 22:34:01 +00003165SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00003166 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
3167
3168 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
3169 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3170 // If we got a simplified select_cc node back from SimplifySelectCC, then
3171 // break it down into a new SETCC node, and a new SELECT node, and then return
3172 // the SELECT node, since we were called with a SELECT node.
3173 if (SCC.Val) {
3174 // Check to see if we got a select_cc back (to turn into setcc/select).
3175 // Otherwise, just return whatever node we got back, like fabs.
3176 if (SCC.getOpcode() == ISD::SELECT_CC) {
3177 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
3178 SCC.getOperand(0), SCC.getOperand(1),
3179 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00003180 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003181 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
3182 SCC.getOperand(3), SETCC);
3183 }
3184 return SCC;
3185 }
Nate Begeman44728a72005-09-19 22:34:01 +00003186 return SDOperand();
3187}
3188
Chris Lattner40c62d52005-10-18 06:04:22 +00003189/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
3190/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00003191/// select. Callers of this should assume that TheSelect is deleted if this
3192/// returns true. As such, they should return the appropriate thing (e.g. the
3193/// node) back to the top-level of the DAG combiner loop to avoid it being
3194/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00003195///
3196bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
3197 SDOperand RHS) {
3198
3199 // If this is a select from two identical things, try to pull the operation
3200 // through the select.
3201 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
3202#if 0
3203 std::cerr << "SELECT: ["; LHS.Val->dump();
3204 std::cerr << "] ["; RHS.Val->dump();
3205 std::cerr << "]\n";
3206#endif
3207
3208 // If this is a load and the token chain is identical, replace the select
3209 // of two loads with a load through a select of the address to load from.
3210 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
3211 // constants have been dropped into the constant pool.
3212 if ((LHS.getOpcode() == ISD::LOAD ||
3213 LHS.getOpcode() == ISD::EXTLOAD ||
3214 LHS.getOpcode() == ISD::ZEXTLOAD ||
3215 LHS.getOpcode() == ISD::SEXTLOAD) &&
3216 // Token chains must be identical.
3217 LHS.getOperand(0) == RHS.getOperand(0) &&
3218 // If this is an EXTLOAD, the VT's must match.
3219 (LHS.getOpcode() == ISD::LOAD ||
3220 LHS.getOperand(3) == RHS.getOperand(3))) {
3221 // FIXME: this conflates two src values, discarding one. This is not
3222 // the right thing to do, but nothing uses srcvalues now. When they do,
3223 // turn SrcValue into a list of locations.
3224 SDOperand Addr;
3225 if (TheSelect->getOpcode() == ISD::SELECT)
3226 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
3227 TheSelect->getOperand(0), LHS.getOperand(1),
3228 RHS.getOperand(1));
3229 else
3230 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
3231 TheSelect->getOperand(0),
3232 TheSelect->getOperand(1),
3233 LHS.getOperand(1), RHS.getOperand(1),
3234 TheSelect->getOperand(4));
3235
3236 SDOperand Load;
3237 if (LHS.getOpcode() == ISD::LOAD)
3238 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
3239 Addr, LHS.getOperand(2));
3240 else
3241 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
3242 LHS.getOperand(0), Addr, LHS.getOperand(2),
3243 cast<VTSDNode>(LHS.getOperand(3))->getVT());
3244 // Users of the select now use the result of the load.
3245 CombineTo(TheSelect, Load);
3246
3247 // Users of the old loads now use the new load's chain. We know the
3248 // old-load value is dead now.
3249 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
3250 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
3251 return true;
3252 }
3253 }
3254
3255 return false;
3256}
3257
Nate Begeman44728a72005-09-19 22:34:01 +00003258SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
3259 SDOperand N2, SDOperand N3,
3260 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00003261
3262 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00003263 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
3264 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
3265 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
3266
3267 // Determine if the condition we're dealing with is constant
3268 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
3269 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
3270
3271 // fold select_cc true, x, y -> x
3272 if (SCCC && SCCC->getValue())
3273 return N2;
3274 // fold select_cc false, x, y -> y
3275 if (SCCC && SCCC->getValue() == 0)
3276 return N3;
3277
3278 // Check to see if we can simplify the select into an fabs node
3279 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
3280 // Allow either -0.0 or 0.0
3281 if (CFP->getValue() == 0.0) {
3282 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
3283 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
3284 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
3285 N2 == N3.getOperand(0))
3286 return DAG.getNode(ISD::FABS, VT, N0);
3287
3288 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
3289 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3290 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3291 N2.getOperand(0) == N3)
3292 return DAG.getNode(ISD::FABS, VT, N3);
3293 }
3294 }
3295
3296 // Check to see if we can perform the "gzip trick", transforming
3297 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00003298 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00003299 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00003300 MVT::isInteger(N2.getValueType()) &&
3301 (N1C->isNullValue() || // (a < 0) ? b : 0
3302 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00003303 MVT::ValueType XType = N0.getValueType();
3304 MVT::ValueType AType = N2.getValueType();
3305 if (XType >= AType) {
3306 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00003307 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00003308 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3309 unsigned ShCtV = Log2_64(N2C->getValue());
3310 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3311 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3312 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00003313 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003314 if (XType > AType) {
3315 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003316 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003317 }
3318 return DAG.getNode(ISD::AND, AType, Shift, N2);
3319 }
3320 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3321 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3322 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003323 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003324 if (XType > AType) {
3325 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003326 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003327 }
3328 return DAG.getNode(ISD::AND, AType, Shift, N2);
3329 }
3330 }
Nate Begeman07ed4172005-10-10 21:26:48 +00003331
3332 // fold select C, 16, 0 -> shl C, 4
3333 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3334 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3335 // Get a SetCC of the condition
3336 // FIXME: Should probably make sure that setcc is legal if we ever have a
3337 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00003338 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00003339 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00003340 if (AfterLegalize) {
3341 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003342 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00003343 } else {
3344 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003345 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00003346 }
Chris Lattner5750df92006-03-01 04:03:14 +00003347 AddToWorkList(SCC.Val);
3348 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00003349 // shl setcc result by log2 n2c
3350 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3351 DAG.getConstant(Log2_64(N2C->getValue()),
3352 TLI.getShiftAmountTy()));
3353 }
3354
Nate Begemanf845b452005-10-08 00:29:44 +00003355 // Check to see if this is the equivalent of setcc
3356 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3357 // otherwise, go ahead with the folds.
3358 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3359 MVT::ValueType XType = N0.getValueType();
3360 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3361 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3362 if (Res.getValueType() != VT)
3363 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3364 return Res;
3365 }
3366
3367 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3368 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3369 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3370 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3371 return DAG.getNode(ISD::SRL, XType, Ctlz,
3372 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3373 TLI.getShiftAmountTy()));
3374 }
3375 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3376 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3377 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3378 N0);
3379 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3380 DAG.getConstant(~0ULL, XType));
3381 return DAG.getNode(ISD::SRL, XType,
3382 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3383 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3384 TLI.getShiftAmountTy()));
3385 }
3386 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3387 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3388 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3389 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3390 TLI.getShiftAmountTy()));
3391 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3392 }
3393 }
3394
3395 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3396 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3397 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3398 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3399 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3400 MVT::ValueType XType = N0.getValueType();
3401 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3402 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3403 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3404 TLI.getShiftAmountTy()));
3405 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003406 AddToWorkList(Shift.Val);
3407 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003408 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3409 }
3410 }
3411 }
3412
Nate Begeman44728a72005-09-19 22:34:01 +00003413 return SDOperand();
3414}
3415
Nate Begeman452d7be2005-09-16 00:54:12 +00003416SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00003417 SDOperand N1, ISD::CondCode Cond,
3418 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003419 // These setcc operations always fold.
3420 switch (Cond) {
3421 default: break;
3422 case ISD::SETFALSE:
3423 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3424 case ISD::SETTRUE:
3425 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3426 }
3427
3428 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3429 uint64_t C1 = N1C->getValue();
3430 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
3431 uint64_t C0 = N0C->getValue();
3432
3433 // Sign extend the operands if required
3434 if (ISD::isSignedIntSetCC(Cond)) {
3435 C0 = N0C->getSignExtended();
3436 C1 = N1C->getSignExtended();
3437 }
3438
3439 switch (Cond) {
3440 default: assert(0 && "Unknown integer setcc!");
3441 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3442 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3443 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
3444 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
3445 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
3446 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
3447 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
3448 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
3449 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
3450 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
3451 }
3452 } else {
Chris Lattner5f42a242006-09-20 06:19:26 +00003453 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
3454 // equality comparison, then we're just comparing whether X itself is
3455 // zero.
3456 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
3457 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
3458 N0.getOperand(1).getOpcode() == ISD::Constant) {
3459 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
3460 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3461 ShAmt == Log2_32(MVT::getSizeInBits(N0.getValueType()))) {
3462 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
3463 // (srl (ctlz x), 5) == 0 -> X != 0
3464 // (srl (ctlz x), 5) != 1 -> X != 0
3465 Cond = ISD::SETNE;
3466 } else {
3467 // (srl (ctlz x), 5) != 0 -> X == 0
3468 // (srl (ctlz x), 5) == 1 -> X == 0
3469 Cond = ISD::SETEQ;
3470 }
3471 SDOperand Zero = DAG.getConstant(0, N0.getValueType());
3472 return DAG.getSetCC(VT, N0.getOperand(0).getOperand(0),
3473 Zero, Cond);
3474 }
3475 }
3476
Nate Begeman452d7be2005-09-16 00:54:12 +00003477 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3478 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3479 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3480
3481 // If the comparison constant has bits in the upper part, the
3482 // zero-extended value could never match.
3483 if (C1 & (~0ULL << InSize)) {
3484 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3485 switch (Cond) {
3486 case ISD::SETUGT:
3487 case ISD::SETUGE:
3488 case ISD::SETEQ: return DAG.getConstant(0, VT);
3489 case ISD::SETULT:
3490 case ISD::SETULE:
3491 case ISD::SETNE: return DAG.getConstant(1, VT);
3492 case ISD::SETGT:
3493 case ISD::SETGE:
3494 // True if the sign bit of C1 is set.
3495 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3496 case ISD::SETLT:
3497 case ISD::SETLE:
3498 // True if the sign bit of C1 isn't set.
3499 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3500 default:
3501 break;
3502 }
3503 }
3504
3505 // Otherwise, we can perform the comparison with the low bits.
3506 switch (Cond) {
3507 case ISD::SETEQ:
3508 case ISD::SETNE:
3509 case ISD::SETUGT:
3510 case ISD::SETUGE:
3511 case ISD::SETULT:
3512 case ISD::SETULE:
3513 return DAG.getSetCC(VT, N0.getOperand(0),
3514 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3515 Cond);
3516 default:
3517 break; // todo, be more careful with signed comparisons
3518 }
3519 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3520 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3521 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3522 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3523 MVT::ValueType ExtDstTy = N0.getValueType();
3524 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3525
3526 // If the extended part has any inconsistent bits, it cannot ever
3527 // compare equal. In other words, they have to be all ones or all
3528 // zeros.
3529 uint64_t ExtBits =
3530 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3531 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3532 return DAG.getConstant(Cond == ISD::SETNE, VT);
3533
3534 SDOperand ZextOp;
3535 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3536 if (Op0Ty == ExtSrcTy) {
3537 ZextOp = N0.getOperand(0);
3538 } else {
3539 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3540 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3541 DAG.getConstant(Imm, Op0Ty));
3542 }
Chris Lattner5750df92006-03-01 04:03:14 +00003543 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003544 // Otherwise, make this a use of a zext.
3545 return DAG.getSetCC(VT, ZextOp,
3546 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3547 ExtDstTy),
3548 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00003549 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
3550 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3551 (N0.getOpcode() == ISD::XOR ||
3552 (N0.getOpcode() == ISD::AND &&
3553 N0.getOperand(0).getOpcode() == ISD::XOR &&
3554 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3555 isa<ConstantSDNode>(N0.getOperand(1)) &&
3556 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
3557 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
3558 // only do this if the top bits are known zero.
3559 if (TLI.MaskedValueIsZero(N1,
3560 MVT::getIntVTBitMask(N0.getValueType())-1)) {
3561 // Okay, get the un-inverted input value.
3562 SDOperand Val;
3563 if (N0.getOpcode() == ISD::XOR)
3564 Val = N0.getOperand(0);
3565 else {
3566 assert(N0.getOpcode() == ISD::AND &&
3567 N0.getOperand(0).getOpcode() == ISD::XOR);
3568 // ((X^1)&1)^1 -> X & 1
3569 Val = DAG.getNode(ISD::AND, N0.getValueType(),
3570 N0.getOperand(0).getOperand(0), N0.getOperand(1));
3571 }
3572 return DAG.getSetCC(VT, Val, N1,
3573 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
3574 }
Nate Begeman452d7be2005-09-16 00:54:12 +00003575 }
Chris Lattner5c46f742005-10-05 06:11:08 +00003576
Nate Begeman452d7be2005-09-16 00:54:12 +00003577 uint64_t MinVal, MaxVal;
3578 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
3579 if (ISD::isSignedIntSetCC(Cond)) {
3580 MinVal = 1ULL << (OperandBitSize-1);
3581 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
3582 MaxVal = ~0ULL >> (65-OperandBitSize);
3583 else
3584 MaxVal = 0;
3585 } else {
3586 MinVal = 0;
3587 MaxVal = ~0ULL >> (64-OperandBitSize);
3588 }
3589
3590 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
3591 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
3592 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
3593 --C1; // X >= C0 --> X > (C0-1)
3594 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3595 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
3596 }
3597
3598 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
3599 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
3600 ++C1; // X <= C0 --> X < (C0+1)
3601 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3602 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
3603 }
3604
3605 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
3606 return DAG.getConstant(0, VT); // X < MIN --> false
3607
3608 // Canonicalize setgt X, Min --> setne X, Min
3609 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
3610 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00003611 // Canonicalize setlt X, Max --> setne X, Max
3612 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
3613 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00003614
3615 // If we have setult X, 1, turn it into seteq X, 0
3616 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
3617 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
3618 ISD::SETEQ);
3619 // If we have setugt X, Max-1, turn it into seteq X, Max
3620 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
3621 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
3622 ISD::SETEQ);
3623
3624 // If we have "setcc X, C0", check to see if we can shrink the immediate
3625 // by changing cc.
3626
3627 // SETUGT X, SINTMAX -> SETLT X, 0
3628 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
3629 C1 == (~0ULL >> (65-OperandBitSize)))
3630 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
3631 ISD::SETLT);
3632
3633 // FIXME: Implement the rest of these.
3634
3635 // Fold bit comparisons when we can.
3636 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3637 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
3638 if (ConstantSDNode *AndRHS =
3639 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3640 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
3641 // Perform the xform if the AND RHS is a single bit.
3642 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
3643 return DAG.getNode(ISD::SRL, VT, N0,
3644 DAG.getConstant(Log2_64(AndRHS->getValue()),
3645 TLI.getShiftAmountTy()));
3646 }
3647 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
3648 // (X & 8) == 8 --> (X & 8) >> 3
3649 // Perform the xform if C1 is a single bit.
3650 if ((C1 & (C1-1)) == 0) {
3651 return DAG.getNode(ISD::SRL, VT, N0,
Chris Lattner729c6d12006-05-27 00:43:02 +00003652 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
Nate Begeman452d7be2005-09-16 00:54:12 +00003653 }
3654 }
3655 }
3656 }
3657 } else if (isa<ConstantSDNode>(N0.Val)) {
3658 // Ensure that the constant occurs on the RHS.
3659 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3660 }
3661
3662 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
3663 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
3664 double C0 = N0C->getValue(), C1 = N1C->getValue();
3665
3666 switch (Cond) {
3667 default: break; // FIXME: Implement the rest of these!
3668 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3669 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3670 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
3671 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
3672 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
3673 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
3674 }
3675 } else {
3676 // Ensure that the constant occurs on the RHS.
3677 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3678 }
3679
3680 if (N0 == N1) {
3681 // We can always fold X == Y for integer setcc's.
3682 if (MVT::isInteger(N0.getValueType()))
3683 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3684 unsigned UOF = ISD::getUnorderedFlavor(Cond);
3685 if (UOF == 2) // FP operators that are undefined on NaNs.
3686 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3687 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
3688 return DAG.getConstant(UOF, VT);
3689 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
3690 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00003691 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00003692 if (NewCond != Cond)
3693 return DAG.getSetCC(VT, N0, N1, NewCond);
3694 }
3695
3696 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3697 MVT::isInteger(N0.getValueType())) {
3698 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
3699 N0.getOpcode() == ISD::XOR) {
3700 // Simplify (X+Y) == (X+Z) --> Y == Z
3701 if (N0.getOpcode() == N1.getOpcode()) {
3702 if (N0.getOperand(0) == N1.getOperand(0))
3703 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
3704 if (N0.getOperand(1) == N1.getOperand(1))
3705 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
Evan Cheng1efba0e2006-08-29 06:42:35 +00003706 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003707 // If X op Y == Y op X, try other combinations.
3708 if (N0.getOperand(0) == N1.getOperand(1))
3709 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
3710 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00003711 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00003712 }
3713 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003714
3715 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
3716 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3717 // Turn (X+C1) == C2 --> X == C2-C1
3718 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
3719 return DAG.getSetCC(VT, N0.getOperand(0),
3720 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
3721 N0.getValueType()), Cond);
3722 }
3723
3724 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
3725 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00003726 // If we know that all of the inverted bits are zero, don't bother
3727 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003728 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00003729 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003730 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00003731 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003732 }
3733
3734 // Turn (C1-X) == C2 --> X == C1-C2
3735 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
3736 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
3737 return DAG.getSetCC(VT, N0.getOperand(1),
3738 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
3739 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00003740 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003741 }
3742 }
3743
Nate Begeman452d7be2005-09-16 00:54:12 +00003744 // Simplify (X+Z) == X --> Z == 0
3745 if (N0.getOperand(0) == N1)
3746 return DAG.getSetCC(VT, N0.getOperand(1),
3747 DAG.getConstant(0, N0.getValueType()), Cond);
3748 if (N0.getOperand(1) == N1) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00003749 if (DAG.isCommutativeBinOp(N0.getOpcode()))
Nate Begeman452d7be2005-09-16 00:54:12 +00003750 return DAG.getSetCC(VT, N0.getOperand(0),
3751 DAG.getConstant(0, N0.getValueType()), Cond);
3752 else {
3753 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
3754 // (Z-X) == X --> Z == X<<1
3755 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
3756 N1,
3757 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003758 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003759 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
3760 }
3761 }
3762 }
3763
3764 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3765 N1.getOpcode() == ISD::XOR) {
3766 // Simplify X == (X+Z) --> Z == 0
3767 if (N1.getOperand(0) == N0) {
3768 return DAG.getSetCC(VT, N1.getOperand(1),
3769 DAG.getConstant(0, N1.getValueType()), Cond);
3770 } else if (N1.getOperand(1) == N0) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00003771 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003772 return DAG.getSetCC(VT, N1.getOperand(0),
3773 DAG.getConstant(0, N1.getValueType()), Cond);
3774 } else {
3775 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3776 // X == (Z-X) --> X<<1 == Z
3777 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
3778 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003779 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003780 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
3781 }
3782 }
3783 }
3784 }
3785
3786 // Fold away ALL boolean setcc's.
3787 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00003788 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003789 switch (Cond) {
3790 default: assert(0 && "Unknown integer setcc!");
3791 case ISD::SETEQ: // X == Y -> (X^Y)^1
3792 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3793 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00003794 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003795 break;
3796 case ISD::SETNE: // X != Y --> (X^Y)
3797 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3798 break;
3799 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
3800 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
3801 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3802 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003803 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003804 break;
3805 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
3806 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
3807 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3808 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003809 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003810 break;
3811 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
3812 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
3813 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3814 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003815 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003816 break;
3817 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
3818 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
3819 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3820 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
3821 break;
3822 }
3823 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00003824 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003825 // FIXME: If running after legalize, we probably can't do this.
3826 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3827 }
3828 return N0;
3829 }
3830
3831 // Could not fold it.
3832 return SDOperand();
3833}
3834
Nate Begeman69575232005-10-20 02:15:44 +00003835/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3836/// return a DAG expression to select that will generate the same value by
3837/// multiplying by a magic number. See:
3838/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3839SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003840 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003841 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
3842
Andrew Lenharth232c9102006-06-12 16:07:18 +00003843 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003844 ii != ee; ++ii)
3845 AddToWorkList(*ii);
3846 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003847}
3848
3849/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3850/// return a DAG expression to select that will generate the same value by
3851/// multiplying by a magic number. See:
3852/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3853SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003854 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003855 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00003856
Andrew Lenharth232c9102006-06-12 16:07:18 +00003857 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003858 ii != ee; ++ii)
3859 AddToWorkList(*ii);
3860 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003861}
3862
Nate Begeman1d4d4142005-09-01 00:19:25 +00003863// SelectionDAG::Combine - This is the entry point for the file.
3864//
Nate Begeman4ebd8052005-09-01 23:24:04 +00003865void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003866 /// run - This is the main entry point to this class.
3867 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00003868 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003869}