blob: 0e7cc2ee1d50dfc06ef198f90ff09420a11b558d [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 Begeman4ebd8052005-09-01 23:24:04 +000025// FIXME: make truncate see through SIGN_EXTEND and AND
Nate Begeman646d7e22005-09-02 21:18:40 +000026// FIXME: divide by zero is currently left unfolded. do we want to turn this
27// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000028// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Nate Begeman1d4d4142005-09-01 00:19:25 +000029//
30//===----------------------------------------------------------------------===//
31
32#define DEBUG_TYPE "dagcombine"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000035#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000036#include "llvm/Support/MathExtras.h"
37#include "llvm/Target/TargetLowering.h"
Chris Lattner360e8202006-06-28 21:58:30 +000038#include "llvm/Support/Visibility.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");
Nate Begeman1d4d4142005-09-01 00:19:25 +000047
Chris Lattner360e8202006-06-28 21:58:30 +000048 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000049 SelectionDAG &DAG;
50 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000051 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000052
53 // Worklist of all of the nodes that need to be simplified.
54 std::vector<SDNode*> WorkList;
55
56 /// AddUsersToWorkList - When an instruction is simplified, add all users of
57 /// the instruction to the work lists because they might get more simplified
58 /// now.
59 ///
60 void AddUsersToWorkList(SDNode *N) {
61 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000062 UI != UE; ++UI)
63 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000064 }
65
66 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000067 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000068 void removeFromWorkList(SDNode *N) {
69 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
70 WorkList.end());
71 }
72
Chris Lattner24664722006-03-01 04:53:38 +000073 public:
Chris Lattner5750df92006-03-01 04:03:14 +000074 void AddToWorkList(SDNode *N) {
75 WorkList.push_back(N);
76 }
77
Chris Lattner01a22022005-10-10 22:04:48 +000078 SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner87514ca2005-10-10 22:31:19 +000079 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000080 DEBUG(std::cerr << "\nReplacing "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +000081 std::cerr << "\nWith: "; To[0].Val->dump(&DAG);
Chris Lattner01a22022005-10-10 22:04:48 +000082 std::cerr << " and " << To.size()-1 << " other values\n");
83 std::vector<SDNode*> NowDead;
Chris Lattnerb9ea4a32006-08-11 17:46:28 +000084 DAG.ReplaceAllUsesWith(N, &To[0], &NowDead);
Chris Lattner01a22022005-10-10 22:04:48 +000085
86 // Push the new nodes and any users onto the worklist
87 for (unsigned i = 0, e = To.size(); i != e; ++i) {
88 WorkList.push_back(To[i].Val);
89 AddUsersToWorkList(To[i].Val);
90 }
91
92 // Nodes can end up on the worklist more than once. Make sure we do
93 // not process a node that has been replaced.
94 removeFromWorkList(N);
95 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
96 removeFromWorkList(NowDead[i]);
97
98 // Finally, since the node is now dead, remove it from the graph.
99 DAG.DeleteNode(N);
100 return SDOperand(N, 0);
101 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000102
Chris Lattner24664722006-03-01 04:53:38 +0000103 SDOperand CombineTo(SDNode *N, SDOperand Res) {
104 std::vector<SDOperand> To;
105 To.push_back(Res);
106 return CombineTo(N, To);
107 }
108
109 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
110 std::vector<SDOperand> To;
111 To.push_back(Res0);
112 To.push_back(Res1);
113 return CombineTo(N, To);
114 }
115 private:
116
Chris Lattner012f2412006-02-17 21:58:01 +0000117 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000118 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000119 /// propagation. If so, return true.
120 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000121 TargetLowering::TargetLoweringOpt TLO(DAG);
122 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000123 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
124 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
125 return false;
126
127 // Revisit the node.
128 WorkList.push_back(Op.Val);
129
130 // Replace the old value with the new one.
131 ++NodesCombined;
132 DEBUG(std::cerr << "\nReplacing "; TLO.Old.Val->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000133 std::cerr << "\nWith: "; TLO.New.Val->dump(&DAG));
Chris Lattner012f2412006-02-17 21:58:01 +0000134
135 std::vector<SDNode*> NowDead;
136 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
137
Chris Lattner7d20d392006-02-20 06:51:04 +0000138 // Push the new node and any (possibly new) users onto the worklist.
Chris Lattner012f2412006-02-17 21:58:01 +0000139 WorkList.push_back(TLO.New.Val);
140 AddUsersToWorkList(TLO.New.Val);
141
142 // Nodes can end up on the worklist more than once. Make sure we do
143 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000144 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
145 removeFromWorkList(NowDead[i]);
146
Chris Lattner7d20d392006-02-20 06:51:04 +0000147 // Finally, if the node is now dead, remove it from the graph. The node
148 // may not be dead if the replacement process recursively simplified to
149 // something else needing this node.
150 if (TLO.Old.Val->use_empty()) {
151 removeFromWorkList(TLO.Old.Val);
152 DAG.DeleteNode(TLO.Old.Val);
153 }
Chris Lattner012f2412006-02-17 21:58:01 +0000154 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000155 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000156
Nate Begeman1d4d4142005-09-01 00:19:25 +0000157 /// visit - call the node-specific routine that knows how to fold each
158 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000159 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000160
161 // Visitation implementation - Implement dag node combining for different
162 // node types. The semantics are as follows:
163 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000164 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000165 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000166 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000167 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000168 SDOperand visitTokenFactor(SDNode *N);
169 SDOperand visitADD(SDNode *N);
170 SDOperand visitSUB(SDNode *N);
171 SDOperand visitMUL(SDNode *N);
172 SDOperand visitSDIV(SDNode *N);
173 SDOperand visitUDIV(SDNode *N);
174 SDOperand visitSREM(SDNode *N);
175 SDOperand visitUREM(SDNode *N);
176 SDOperand visitMULHU(SDNode *N);
177 SDOperand visitMULHS(SDNode *N);
178 SDOperand visitAND(SDNode *N);
179 SDOperand visitOR(SDNode *N);
180 SDOperand visitXOR(SDNode *N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000181 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000182 SDOperand visitSHL(SDNode *N);
183 SDOperand visitSRA(SDNode *N);
184 SDOperand visitSRL(SDNode *N);
185 SDOperand visitCTLZ(SDNode *N);
186 SDOperand visitCTTZ(SDNode *N);
187 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000188 SDOperand visitSELECT(SDNode *N);
189 SDOperand visitSELECT_CC(SDNode *N);
190 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000191 SDOperand visitSIGN_EXTEND(SDNode *N);
192 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000193 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000194 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
195 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000196 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000197 SDOperand visitVBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000198 SDOperand visitFADD(SDNode *N);
199 SDOperand visitFSUB(SDNode *N);
200 SDOperand visitFMUL(SDNode *N);
201 SDOperand visitFDIV(SDNode *N);
202 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000203 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000204 SDOperand visitSINT_TO_FP(SDNode *N);
205 SDOperand visitUINT_TO_FP(SDNode *N);
206 SDOperand visitFP_TO_SINT(SDNode *N);
207 SDOperand visitFP_TO_UINT(SDNode *N);
208 SDOperand visitFP_ROUND(SDNode *N);
209 SDOperand visitFP_ROUND_INREG(SDNode *N);
210 SDOperand visitFP_EXTEND(SDNode *N);
211 SDOperand visitFNEG(SDNode *N);
212 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000213 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000214 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000215 SDOperand visitLOAD(SDNode *N);
Chris Lattner29cd7db2006-03-31 18:10:41 +0000216 SDOperand visitXEXTLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000217 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000218 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
219 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000220 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000221 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000222 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000223
Evan Cheng44f1f092006-04-20 08:56:16 +0000224 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000225 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
226
Chris Lattner40c62d52005-10-18 06:04:22 +0000227 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000228 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000229 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
230 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
231 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000232 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000233 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000234 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000235 SDOperand BuildSDIV(SDNode *N);
236 SDOperand BuildUDIV(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000237public:
238 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000239 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000240
241 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000242 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000243 };
244}
245
Chris Lattner24664722006-03-01 04:53:38 +0000246//===----------------------------------------------------------------------===//
247// TargetLowering::DAGCombinerInfo implementation
248//===----------------------------------------------------------------------===//
249
250void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
251 ((DAGCombiner*)DC)->AddToWorkList(N);
252}
253
254SDOperand TargetLowering::DAGCombinerInfo::
255CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
256 return ((DAGCombiner*)DC)->CombineTo(N, To);
257}
258
259SDOperand TargetLowering::DAGCombinerInfo::
260CombineTo(SDNode *N, SDOperand Res) {
261 return ((DAGCombiner*)DC)->CombineTo(N, Res);
262}
263
264
265SDOperand TargetLowering::DAGCombinerInfo::
266CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
267 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
268}
269
270
271
272
273//===----------------------------------------------------------------------===//
274
275
Nate Begeman4ebd8052005-09-01 23:24:04 +0000276// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
277// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000278// Also, set the incoming LHS, RHS, and CC references to the appropriate
279// nodes based on the type of node we are checking. This simplifies life a
280// bit for the callers.
281static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
282 SDOperand &CC) {
283 if (N.getOpcode() == ISD::SETCC) {
284 LHS = N.getOperand(0);
285 RHS = N.getOperand(1);
286 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000287 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000288 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000289 if (N.getOpcode() == ISD::SELECT_CC &&
290 N.getOperand(2).getOpcode() == ISD::Constant &&
291 N.getOperand(3).getOpcode() == ISD::Constant &&
292 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000293 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
294 LHS = N.getOperand(0);
295 RHS = N.getOperand(1);
296 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000297 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000298 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000299 return false;
300}
301
Nate Begeman99801192005-09-07 23:25:52 +0000302// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
303// one use. If this is true, it allows the users to invert the operation for
304// free when it is profitable to do so.
305static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000306 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000307 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000308 return true;
309 return false;
310}
311
Nate Begeman452d7be2005-09-16 00:54:12 +0000312// FIXME: This should probably go in the ISD class rather than being duplicated
313// in several files.
314static bool isCommutativeBinOp(unsigned Opcode) {
315 switch (Opcode) {
316 case ISD::ADD:
317 case ISD::MUL:
318 case ISD::AND:
319 case ISD::OR:
320 case ISD::XOR: return true;
321 default: return false; // FIXME: Need commutative info for user ops!
322 }
323}
324
Nate Begemancd4d58c2006-02-03 06:46:56 +0000325SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
326 MVT::ValueType VT = N0.getValueType();
327 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
328 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
329 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
330 if (isa<ConstantSDNode>(N1)) {
331 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000332 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000333 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
334 } else if (N0.hasOneUse()) {
335 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
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, N0.getOperand(1));
338 }
339 }
340 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
341 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
342 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
343 if (isa<ConstantSDNode>(N0)) {
344 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000345 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000346 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
347 } else if (N1.hasOneUse()) {
348 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000349 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000350 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
351 }
352 }
353 return SDOperand();
354}
355
Nate Begeman4ebd8052005-09-01 23:24:04 +0000356void DAGCombiner::Run(bool RunningAfterLegalize) {
357 // set the instance variable, so that the various visit routines may use it.
358 AfterLegalize = RunningAfterLegalize;
359
Nate Begeman646d7e22005-09-02 21:18:40 +0000360 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000361 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
362 E = DAG.allnodes_end(); I != E; ++I)
363 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000364
Chris Lattner95038592005-10-05 06:35:28 +0000365 // Create a dummy node (which is not added to allnodes), that adds a reference
366 // to the root node, preventing it from being deleted, and tracking any
367 // changes of the root.
368 HandleSDNode Dummy(DAG.getRoot());
369
Chris Lattner24664722006-03-01 04:53:38 +0000370
371 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
372 TargetLowering::DAGCombinerInfo
373 DagCombineInfo(DAG, !RunningAfterLegalize, this);
374
Nate Begeman1d4d4142005-09-01 00:19:25 +0000375 // while the worklist isn't empty, inspect the node on the end of it and
376 // try and combine it.
377 while (!WorkList.empty()) {
378 SDNode *N = WorkList.back();
379 WorkList.pop_back();
380
381 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000382 // N is deleted from the DAG, since they too may now be dead or may have a
383 // reduced number of uses, allowing other xforms.
384 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000385 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
386 WorkList.push_back(N->getOperand(i).Val);
387
Nate Begeman1d4d4142005-09-01 00:19:25 +0000388 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000389 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000390 continue;
391 }
392
Nate Begeman83e75ec2005-09-06 04:43:02 +0000393 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000394
395 // If nothing happened, try a target-specific DAG combine.
396 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000397 assert(N->getOpcode() != ISD::DELETED_NODE &&
398 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000399 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
400 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
401 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
402 }
403
Nate Begeman83e75ec2005-09-06 04:43:02 +0000404 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000405 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000406 // If we get back the same node we passed in, rather than a new node or
407 // zero, we know that the node must have defined multiple values and
408 // CombineTo was used. Since CombineTo takes care of the worklist
409 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000410 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000411 assert(N->getOpcode() != ISD::DELETED_NODE &&
412 RV.Val->getOpcode() != ISD::DELETED_NODE &&
413 "Node was deleted but visit returned new node!");
414
Nate Begeman2300f552005-09-07 00:15:36 +0000415 DEBUG(std::cerr << "\nReplacing "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000416 std::cerr << "\nWith: "; RV.Val->dump(&DAG);
Nate Begeman2300f552005-09-07 00:15:36 +0000417 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000418 std::vector<SDNode*> NowDead;
Chris Lattnerb9ea4a32006-08-11 17:46:28 +0000419 SDOperand OpV = RV;
420 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000421
422 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000423 WorkList.push_back(RV.Val);
424 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000425
426 // Nodes can end up on the worklist more than once. Make sure we do
427 // not process a node that has been replaced.
428 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000429 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
430 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000431
432 // Finally, since the node is now dead, remove it from the graph.
433 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000434 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000435 }
436 }
Chris Lattner95038592005-10-05 06:35:28 +0000437
438 // If the root changed (e.g. it was a dead load, update the root).
439 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000440}
441
Nate Begeman83e75ec2005-09-06 04:43:02 +0000442SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000443 switch(N->getOpcode()) {
444 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000445 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000446 case ISD::ADD: return visitADD(N);
447 case ISD::SUB: return visitSUB(N);
448 case ISD::MUL: return visitMUL(N);
449 case ISD::SDIV: return visitSDIV(N);
450 case ISD::UDIV: return visitUDIV(N);
451 case ISD::SREM: return visitSREM(N);
452 case ISD::UREM: return visitUREM(N);
453 case ISD::MULHU: return visitMULHU(N);
454 case ISD::MULHS: return visitMULHS(N);
455 case ISD::AND: return visitAND(N);
456 case ISD::OR: return visitOR(N);
457 case ISD::XOR: return visitXOR(N);
458 case ISD::SHL: return visitSHL(N);
459 case ISD::SRA: return visitSRA(N);
460 case ISD::SRL: return visitSRL(N);
461 case ISD::CTLZ: return visitCTLZ(N);
462 case ISD::CTTZ: return visitCTTZ(N);
463 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000464 case ISD::SELECT: return visitSELECT(N);
465 case ISD::SELECT_CC: return visitSELECT_CC(N);
466 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000467 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
468 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000469 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000470 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
471 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000472 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000473 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000474 case ISD::FADD: return visitFADD(N);
475 case ISD::FSUB: return visitFSUB(N);
476 case ISD::FMUL: return visitFMUL(N);
477 case ISD::FDIV: return visitFDIV(N);
478 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000479 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000480 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
481 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
482 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
483 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
484 case ISD::FP_ROUND: return visitFP_ROUND(N);
485 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
486 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
487 case ISD::FNEG: return visitFNEG(N);
488 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000489 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000490 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000491 case ISD::LOAD: return visitLOAD(N);
Chris Lattner29cd7db2006-03-31 18:10:41 +0000492 case ISD::EXTLOAD:
493 case ISD::SEXTLOAD:
494 case ISD::ZEXTLOAD: return visitXEXTLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000495 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000496 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
497 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000498 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000499 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000500 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000501 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
502 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
503 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
504 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
505 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
506 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
507 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
508 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000509 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000510 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000511}
512
Nate Begeman83e75ec2005-09-06 04:43:02 +0000513SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000514 SmallVector<SDOperand, 8> Ops;
Nate Begemanded49632005-10-13 03:11:28 +0000515 bool Changed = false;
516
Nate Begeman1d4d4142005-09-01 00:19:25 +0000517 // If the token factor has two operands and one is the entry token, replace
518 // the token factor with the other operand.
519 if (N->getNumOperands() == 2) {
Chris Lattner21a57dc2006-05-12 05:01:37 +0000520 if (N->getOperand(0).getOpcode() == ISD::EntryToken ||
521 N->getOperand(0) == N->getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000522 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000523 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000524 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000525 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000526
Nate Begemanded49632005-10-13 03:11:28 +0000527 // fold (tokenfactor (tokenfactor)) -> tokenfactor
528 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
529 SDOperand Op = N->getOperand(i);
530 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
Chris Lattnerac0f8f22006-03-13 18:37:30 +0000531 AddToWorkList(Op.Val); // Remove dead node.
Nate Begemanded49632005-10-13 03:11:28 +0000532 Changed = true;
533 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
534 Ops.push_back(Op.getOperand(j));
Chris Lattner21a57dc2006-05-12 05:01:37 +0000535 } else if (i == 0 || N->getOperand(i) != N->getOperand(i-1)) {
Nate Begemanded49632005-10-13 03:11:28 +0000536 Ops.push_back(Op);
Chris Lattner21a57dc2006-05-12 05:01:37 +0000537 } else {
538 // Deleted an operand that was the same as the last one.
539 Changed = true;
Nate Begemanded49632005-10-13 03:11:28 +0000540 }
541 }
542 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000543 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begeman83e75ec2005-09-06 04:43:02 +0000544 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000545}
546
Nate Begeman83e75ec2005-09-06 04:43:02 +0000547SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000548 SDOperand N0 = N->getOperand(0);
549 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000550 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
551 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000552 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000553
554 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000555 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000556 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000557 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000558 if (N0C && !N1C)
559 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000560 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000561 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000562 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000563 // fold ((c1-A)+c2) -> (c1+c2)-A
564 if (N1C && N0.getOpcode() == ISD::SUB)
565 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
566 return DAG.getNode(ISD::SUB, VT,
567 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
568 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000569 // reassociate add
570 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
571 if (RADD.Val != 0)
572 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000573 // fold ((0-A) + B) -> B-A
574 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
575 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000576 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000577 // fold (A + (0-B)) -> A-B
578 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
579 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000580 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000581 // fold (A+(B-A)) -> B
582 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000583 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000584
Evan Cheng860771d2006-03-01 01:09:54 +0000585 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000586 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000587
588 // fold (a+b) -> (a|b) iff a and b share no bits.
589 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
590 uint64_t LHSZero, LHSOne;
591 uint64_t RHSZero, RHSOne;
592 uint64_t Mask = MVT::getIntVTBitMask(VT);
593 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
594 if (LHSZero) {
595 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
596
597 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
598 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
599 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
600 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
601 return DAG.getNode(ISD::OR, VT, N0, N1);
602 }
603 }
604
Nate Begeman83e75ec2005-09-06 04:43:02 +0000605 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000606}
607
Nate Begeman83e75ec2005-09-06 04:43:02 +0000608SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000609 SDOperand N0 = N->getOperand(0);
610 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000611 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
612 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000613 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000614
Chris Lattner854077d2005-10-17 01:07:11 +0000615 // fold (sub x, x) -> 0
616 if (N0 == N1)
617 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000618 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000619 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000620 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000621 // fold (sub x, c) -> (add x, -c)
622 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000623 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000624 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000625 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000626 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000627 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000628 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000629 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000630 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000631}
632
Nate Begeman83e75ec2005-09-06 04:43:02 +0000633SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000634 SDOperand N0 = N->getOperand(0);
635 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000636 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
637 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000638 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000639
640 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000641 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000642 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000643 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000644 if (N0C && !N1C)
645 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000646 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000647 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000648 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000649 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000650 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000651 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000652 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000653 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000654 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000655 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000656 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000657 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
658 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
659 // FIXME: If the input is something that is easily negated (e.g. a
660 // single-use add), we should put the negate there.
661 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
662 DAG.getNode(ISD::SHL, VT, N0,
663 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
664 TLI.getShiftAmountTy())));
665 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +0000666
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000667 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
668 if (N1C && N0.getOpcode() == ISD::SHL &&
669 isa<ConstantSDNode>(N0.getOperand(1))) {
670 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000671 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000672 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
673 }
674
675 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
676 // use.
677 {
678 SDOperand Sh(0,0), Y(0,0);
679 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
680 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
681 N0.Val->hasOneUse()) {
682 Sh = N0; Y = N1;
683 } else if (N1.getOpcode() == ISD::SHL &&
684 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
685 Sh = N1; Y = N0;
686 }
687 if (Sh.Val) {
688 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
689 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
690 }
691 }
Chris Lattnera1deca32006-03-04 23:33:26 +0000692 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
693 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
694 isa<ConstantSDNode>(N0.getOperand(1))) {
695 return DAG.getNode(ISD::ADD, VT,
696 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
697 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
698 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000699
Nate Begemancd4d58c2006-02-03 06:46:56 +0000700 // reassociate mul
701 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
702 if (RMUL.Val != 0)
703 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000704 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000705}
706
Nate Begeman83e75ec2005-09-06 04:43:02 +0000707SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000708 SDOperand N0 = N->getOperand(0);
709 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000710 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
711 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000712 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000713
714 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000715 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000716 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000717 // fold (sdiv X, 1) -> X
718 if (N1C && N1C->getSignExtended() == 1LL)
719 return N0;
720 // fold (sdiv X, -1) -> 0-X
721 if (N1C && N1C->isAllOnesValue())
722 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000723 // If we know the sign bits of both operands are zero, strength reduce to a
724 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
725 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000726 if (TLI.MaskedValueIsZero(N1, SignBit) &&
727 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000728 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000729 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000730 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000731 (isPowerOf2_64(N1C->getSignExtended()) ||
732 isPowerOf2_64(-N1C->getSignExtended()))) {
733 // If dividing by powers of two is cheap, then don't perform the following
734 // fold.
735 if (TLI.isPow2DivCheap())
736 return SDOperand();
737 int64_t pow2 = N1C->getSignExtended();
738 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000739 unsigned lg2 = Log2_64(abs2);
740 // Splat the sign bit into the register
741 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000742 DAG.getConstant(MVT::getSizeInBits(VT)-1,
743 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +0000744 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000745 // Add (N0 < 0) ? abs2 - 1 : 0;
746 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
747 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000748 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000749 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +0000750 AddToWorkList(SRL.Val);
751 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +0000752 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
753 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000754 // If we're dividing by a positive value, we're done. Otherwise, we must
755 // negate the result.
756 if (pow2 > 0)
757 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +0000758 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000759 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
760 }
Nate Begeman69575232005-10-20 02:15:44 +0000761 // if integer divide is expensive and we satisfy the requirements, emit an
762 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000763 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000764 !TLI.isIntDivCheap()) {
765 SDOperand Op = BuildSDIV(N);
766 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000767 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000768 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000769}
770
Nate Begeman83e75ec2005-09-06 04:43:02 +0000771SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000772 SDOperand N0 = N->getOperand(0);
773 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000774 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
775 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000776 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000777
778 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000779 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000780 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000781 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000782 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000783 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000784 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000785 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000786 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
787 if (N1.getOpcode() == ISD::SHL) {
788 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
789 if (isPowerOf2_64(SHC->getValue())) {
790 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000791 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
792 DAG.getConstant(Log2_64(SHC->getValue()),
793 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +0000794 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000795 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000796 }
797 }
798 }
Nate Begeman69575232005-10-20 02:15:44 +0000799 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000800 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
801 SDOperand Op = BuildUDIV(N);
802 if (Op.Val) return Op;
803 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000804 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000805}
806
Nate Begeman83e75ec2005-09-06 04:43:02 +0000807SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000808 SDOperand N0 = N->getOperand(0);
809 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000810 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
811 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000812 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000813
814 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000815 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000816 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000817 // If we know the sign bits of both operands are zero, strength reduce to a
818 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
819 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000820 if (TLI.MaskedValueIsZero(N1, SignBit) &&
821 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000822 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000823 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000824}
825
Nate Begeman83e75ec2005-09-06 04:43:02 +0000826SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000827 SDOperand N0 = N->getOperand(0);
828 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000829 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
830 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000831 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000832
833 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000834 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000835 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000836 // fold (urem x, pow2) -> (and x, pow2-1)
837 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000838 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000839 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
840 if (N1.getOpcode() == ISD::SHL) {
841 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
842 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000843 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +0000844 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000845 return DAG.getNode(ISD::AND, VT, N0, Add);
846 }
847 }
848 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000849 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000850}
851
Nate Begeman83e75ec2005-09-06 04:43:02 +0000852SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000853 SDOperand N0 = N->getOperand(0);
854 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000855 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000856
857 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000858 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000859 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000860 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000861 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000862 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
863 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000864 TLI.getShiftAmountTy()));
865 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000866}
867
Nate Begeman83e75ec2005-09-06 04:43:02 +0000868SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000869 SDOperand N0 = N->getOperand(0);
870 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000871 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000872
873 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000874 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000875 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000876 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000877 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000878 return DAG.getConstant(0, N0.getValueType());
879 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000880}
881
Chris Lattner35e5c142006-05-05 05:51:50 +0000882/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
883/// two operands of the same opcode, try to simplify it.
884SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
885 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
886 MVT::ValueType VT = N0.getValueType();
887 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
888
Chris Lattner540121f2006-05-05 06:31:05 +0000889 // For each of OP in AND/OR/XOR:
890 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
891 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
892 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +0000893 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +0000894 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +0000895 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000896 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
897 SDOperand ORNode = DAG.getNode(N->getOpcode(),
898 N0.getOperand(0).getValueType(),
899 N0.getOperand(0), N1.getOperand(0));
900 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +0000901 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +0000902 }
903
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000904 // For each of OP in SHL/SRL/SRA/AND...
905 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
906 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
907 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +0000908 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000909 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000910 N0.getOperand(1) == N1.getOperand(1)) {
911 SDOperand ORNode = DAG.getNode(N->getOpcode(),
912 N0.getOperand(0).getValueType(),
913 N0.getOperand(0), N1.getOperand(0));
914 AddToWorkList(ORNode.Val);
915 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
916 }
917
918 return SDOperand();
919}
920
Nate Begeman83e75ec2005-09-06 04:43:02 +0000921SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000922 SDOperand N0 = N->getOperand(0);
923 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +0000924 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000925 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
926 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000927 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000928 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000929
930 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000931 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000932 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000933 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000934 if (N0C && !N1C)
935 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000936 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000937 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000938 return N0;
939 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +0000940 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000941 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000942 // reassociate and
943 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
944 if (RAND.Val != 0)
945 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000946 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +0000947 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000948 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000949 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000950 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +0000951 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
952 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +0000953 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +0000954 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +0000955 ~N1C->getValue() & InMask)) {
956 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
957 N0.getOperand(0));
958
959 // Replace uses of the AND with uses of the Zero extend node.
960 CombineTo(N, Zext);
961
Chris Lattner3603cd62006-02-02 07:17:31 +0000962 // We actually want to replace all uses of the any_extend with the
963 // zero_extend, to avoid duplicating things. This will later cause this
964 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +0000965 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +0000966 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +0000967 }
968 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000969 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
970 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
971 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
972 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
973
974 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
975 MVT::isInteger(LL.getValueType())) {
976 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
977 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
978 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +0000979 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000980 return DAG.getSetCC(VT, ORNode, LR, Op1);
981 }
982 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
983 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
984 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +0000985 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000986 return DAG.getSetCC(VT, ANDNode, LR, Op1);
987 }
988 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
989 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
990 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +0000991 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000992 return DAG.getSetCC(VT, ORNode, LR, Op1);
993 }
994 }
995 // canonicalize equivalent to ll == rl
996 if (LL == RR && LR == RL) {
997 Op1 = ISD::getSetCCSwappedOperands(Op1);
998 std::swap(RL, RR);
999 }
1000 if (LL == RL && LR == RR) {
1001 bool isInteger = MVT::isInteger(LL.getValueType());
1002 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1003 if (Result != ISD::SETCC_INVALID)
1004 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1005 }
1006 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001007
1008 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1009 if (N0.getOpcode() == N1.getOpcode()) {
1010 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1011 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001012 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001013
Nate Begemande996292006-02-03 22:24:05 +00001014 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1015 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001016 if (!MVT::isVector(VT) &&
1017 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001018 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001019 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +00001020 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +00001021 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001022 // If we zero all the possible extended bits, then we can turn this into
1023 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001024 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +00001025 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001026 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1027 N0.getOperand(1), N0.getOperand(2),
1028 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001029 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001030 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001031 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001032 }
1033 }
1034 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001035 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001036 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001037 // If we zero all the possible extended bits, then we can turn this into
1038 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001039 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001040 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001041 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1042 N0.getOperand(1), N0.getOperand(2),
1043 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001044 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001045 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001046 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001047 }
1048 }
Chris Lattner15045b62006-02-28 06:35:35 +00001049
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001050 // fold (and (load x), 255) -> (zextload x, i8)
1051 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1052 if (N1C &&
1053 (N0.getOpcode() == ISD::LOAD || N0.getOpcode() == ISD::EXTLOAD ||
1054 N0.getOpcode() == ISD::ZEXTLOAD) &&
1055 N0.hasOneUse()) {
1056 MVT::ValueType EVT, LoadedVT;
Chris Lattner15045b62006-02-28 06:35:35 +00001057 if (N1C->getValue() == 255)
1058 EVT = MVT::i8;
1059 else if (N1C->getValue() == 65535)
1060 EVT = MVT::i16;
1061 else if (N1C->getValue() == ~0U)
1062 EVT = MVT::i32;
1063 else
1064 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001065
1066 LoadedVT = N0.getOpcode() == ISD::LOAD ? VT :
1067 cast<VTSDNode>(N0.getOperand(3))->getVT();
Chris Lattnere44be602006-04-04 17:39:18 +00001068 if (EVT != MVT::Other && LoadedVT > EVT &&
1069 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Chris Lattner15045b62006-02-28 06:35:35 +00001070 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1071 // For big endian targets, we need to add an offset to the pointer to load
1072 // the correct bytes. For little endian systems, we merely need to read
1073 // fewer bytes from the same pointer.
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001074 unsigned PtrOff =
1075 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1076 SDOperand NewPtr = N0.getOperand(1);
1077 if (!TLI.isLittleEndian())
1078 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1079 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001080 AddToWorkList(NewPtr.Val);
Chris Lattner15045b62006-02-28 06:35:35 +00001081 SDOperand Load =
1082 DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
1083 N0.getOperand(2), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001084 AddToWorkList(N);
Chris Lattner15045b62006-02-28 06:35:35 +00001085 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001086 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner15045b62006-02-28 06:35:35 +00001087 }
1088 }
1089
Nate Begeman83e75ec2005-09-06 04:43:02 +00001090 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001091}
1092
Nate Begeman83e75ec2005-09-06 04:43:02 +00001093SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001094 SDOperand N0 = N->getOperand(0);
1095 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001096 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001097 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1098 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001099 MVT::ValueType VT = N1.getValueType();
1100 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001101
1102 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001103 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001104 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001105 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001106 if (N0C && !N1C)
1107 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001108 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001109 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001110 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001111 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001112 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001113 return N1;
1114 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001115 if (N1C &&
1116 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001117 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001118 // reassociate or
1119 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1120 if (ROR.Val != 0)
1121 return ROR;
1122 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1123 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001124 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001125 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1126 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1127 N1),
1128 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001129 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001130 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1131 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1132 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1133 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1134
1135 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1136 MVT::isInteger(LL.getValueType())) {
1137 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1138 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1139 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1140 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1141 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001142 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001143 return DAG.getSetCC(VT, ORNode, LR, Op1);
1144 }
1145 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1146 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1147 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1148 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1149 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001150 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001151 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1152 }
1153 }
1154 // canonicalize equivalent to ll == rl
1155 if (LL == RR && LR == RL) {
1156 Op1 = ISD::getSetCCSwappedOperands(Op1);
1157 std::swap(RL, RR);
1158 }
1159 if (LL == RL && LR == RR) {
1160 bool isInteger = MVT::isInteger(LL.getValueType());
1161 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1162 if (Result != ISD::SETCC_INVALID)
1163 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1164 }
1165 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001166
1167 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1168 if (N0.getOpcode() == N1.getOpcode()) {
1169 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1170 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001171 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001172
Nate Begeman35ef9132006-01-11 21:21:00 +00001173 // canonicalize shl to left side in a shl/srl pair, to match rotate
1174 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
1175 std::swap(N0, N1);
1176 // check for rotl, rotr
1177 if (N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SRL &&
1178 N0.getOperand(0) == N1.getOperand(0) &&
Chris Lattneraf551bc2006-01-12 18:57:33 +00001179 TLI.isOperationLegal(ISD::ROTL, VT) && TLI.isTypeLegal(VT)) {
Nate Begeman35ef9132006-01-11 21:21:00 +00001180 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1181 if (N0.getOperand(1).getOpcode() == ISD::Constant &&
1182 N1.getOperand(1).getOpcode() == ISD::Constant) {
1183 uint64_t c1val = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1184 uint64_t c2val = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1185 if ((c1val + c2val) == OpSizeInBits)
1186 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1187 }
1188 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1189 if (N1.getOperand(1).getOpcode() == ISD::SUB &&
1190 N0.getOperand(1) == N1.getOperand(1).getOperand(1))
1191 if (ConstantSDNode *SUBC =
1192 dyn_cast<ConstantSDNode>(N1.getOperand(1).getOperand(0)))
1193 if (SUBC->getValue() == OpSizeInBits)
1194 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1195 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1196 if (N0.getOperand(1).getOpcode() == ISD::SUB &&
1197 N1.getOperand(1) == N0.getOperand(1).getOperand(1))
1198 if (ConstantSDNode *SUBC =
1199 dyn_cast<ConstantSDNode>(N0.getOperand(1).getOperand(0)))
1200 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattneraf551bc2006-01-12 18:57:33 +00001201 if (TLI.isOperationLegal(ISD::ROTR, VT) && TLI.isTypeLegal(VT))
Nate Begeman35ef9132006-01-11 21:21:00 +00001202 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1203 N1.getOperand(1));
1204 else
1205 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1206 N0.getOperand(1));
1207 }
1208 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001209 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001210}
1211
Nate Begeman83e75ec2005-09-06 04:43:02 +00001212SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001213 SDOperand N0 = N->getOperand(0);
1214 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001215 SDOperand LHS, RHS, CC;
1216 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1217 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001218 MVT::ValueType VT = N0.getValueType();
1219
1220 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001221 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001222 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001223 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001224 if (N0C && !N1C)
1225 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001226 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001227 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001228 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001229 // reassociate xor
1230 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1231 if (RXOR.Val != 0)
1232 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001233 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001234 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1235 bool isInt = MVT::isInteger(LHS.getValueType());
1236 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1237 isInt);
1238 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001239 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001240 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001241 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001242 assert(0 && "Unhandled SetCC Equivalent!");
1243 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001244 }
Nate Begeman99801192005-09-07 23:25:52 +00001245 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1246 if (N1C && N1C->getValue() == 1 &&
1247 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001248 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001249 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1250 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001251 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1252 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001253 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001254 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001255 }
1256 }
Nate Begeman99801192005-09-07 23:25:52 +00001257 // fold !(x or y) -> (!x and !y) iff x or y are constants
1258 if (N1C && N1C->isAllOnesValue() &&
1259 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001260 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001261 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1262 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001263 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1264 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001265 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001266 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001267 }
1268 }
Nate Begeman223df222005-09-08 20:18:10 +00001269 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1270 if (N1C && N0.getOpcode() == ISD::XOR) {
1271 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1272 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1273 if (N00C)
1274 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1275 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1276 if (N01C)
1277 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1278 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1279 }
1280 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001281 if (N0 == N1) {
1282 if (!MVT::isVector(VT)) {
1283 return DAG.getConstant(0, VT);
1284 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1285 // Produce a vector of zeros.
1286 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1287 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001288 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001289 }
1290 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001291
1292 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1293 if (N0.getOpcode() == N1.getOpcode()) {
1294 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1295 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001296 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001297
Chris Lattner3e104b12006-04-08 04:15:24 +00001298 // Simplify the expression using non-local knowledge.
1299 if (!MVT::isVector(VT) &&
1300 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001301 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001302
Nate Begeman83e75ec2005-09-06 04:43:02 +00001303 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001304}
1305
Nate Begeman83e75ec2005-09-06 04:43:02 +00001306SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001307 SDOperand N0 = N->getOperand(0);
1308 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001309 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1310 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001311 MVT::ValueType VT = N0.getValueType();
1312 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1313
1314 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001315 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001316 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001317 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001318 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001319 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001320 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001321 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001322 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001323 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001324 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001325 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001326 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001327 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001328 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001329 if (SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001330 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001331 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001332 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001333 N0.getOperand(1).getOpcode() == ISD::Constant) {
1334 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001335 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001336 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001337 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001338 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001339 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001340 }
1341 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1342 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001343 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001344 N0.getOperand(1).getOpcode() == ISD::Constant) {
1345 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001346 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001347 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1348 DAG.getConstant(~0ULL << c1, VT));
1349 if (c2 > c1)
1350 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001351 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001352 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001353 return DAG.getNode(ISD::SRL, VT, Mask,
1354 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001355 }
1356 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001357 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001358 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001359 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnercac70592006-03-05 19:53:55 +00001360 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1361 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1362 isa<ConstantSDNode>(N0.getOperand(1))) {
1363 return DAG.getNode(ISD::ADD, VT,
1364 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1365 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1366 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001367 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001368}
1369
Nate Begeman83e75ec2005-09-06 04:43:02 +00001370SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001371 SDOperand N0 = N->getOperand(0);
1372 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001373 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1374 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001375 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001376
1377 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001378 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001379 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001380 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001381 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001382 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001383 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001384 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001385 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001386 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001387 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001388 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001389 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001390 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001391 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001392 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1393 // sext_inreg.
1394 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1395 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1396 MVT::ValueType EVT;
1397 switch (LowBits) {
1398 default: EVT = MVT::Other; break;
1399 case 1: EVT = MVT::i1; break;
1400 case 8: EVT = MVT::i8; break;
1401 case 16: EVT = MVT::i16; break;
1402 case 32: EVT = MVT::i32; break;
1403 }
1404 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1405 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1406 DAG.getValueType(EVT));
1407 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001408
1409 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1410 if (N1C && N0.getOpcode() == ISD::SRA) {
1411 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1412 unsigned Sum = N1C->getValue() + C1->getValue();
1413 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1414 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1415 DAG.getConstant(Sum, N1C->getValueType(0)));
1416 }
1417 }
1418
Chris Lattnera8504462006-05-08 20:51:54 +00001419 // Simplify, based on bits shifted out of the LHS.
1420 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
1421 return SDOperand(N, 0);
1422
1423
Nate Begeman1d4d4142005-09-01 00:19:25 +00001424 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001425 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001426 return DAG.getNode(ISD::SRL, VT, N0, N1);
1427 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001428}
1429
Nate Begeman83e75ec2005-09-06 04:43:02 +00001430SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001431 SDOperand N0 = N->getOperand(0);
1432 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001433 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1434 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001435 MVT::ValueType VT = N0.getValueType();
1436 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1437
1438 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001439 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001440 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001441 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001442 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001443 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001444 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001445 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001446 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001447 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001448 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001449 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001450 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001451 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001452 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001453 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001454 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001455 N0.getOperand(1).getOpcode() == ISD::Constant) {
1456 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001457 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001458 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001459 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001460 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001461 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001462 }
Chris Lattner350bec02006-04-02 06:11:11 +00001463
Chris Lattner06afe072006-05-05 22:53:17 +00001464 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
1465 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1466 // Shifting in all undef bits?
1467 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
1468 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
1469 return DAG.getNode(ISD::UNDEF, VT);
1470
1471 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
1472 AddToWorkList(SmallShift.Val);
1473 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
1474 }
1475
Chris Lattner350bec02006-04-02 06:11:11 +00001476 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1477 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1478 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1479 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1480 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
1481
1482 // If any of the input bits are KnownOne, then the input couldn't be all
1483 // zeros, thus the result of the srl will always be zero.
1484 if (KnownOne) return DAG.getConstant(0, VT);
1485
1486 // If all of the bits input the to ctlz node are known to be zero, then
1487 // the result of the ctlz is "32" and the result of the shift is one.
1488 uint64_t UnknownBits = ~KnownZero & Mask;
1489 if (UnknownBits == 0) return DAG.getConstant(1, VT);
1490
1491 // Otherwise, check to see if there is exactly one bit input to the ctlz.
1492 if ((UnknownBits & (UnknownBits-1)) == 0) {
1493 // Okay, we know that only that the single bit specified by UnknownBits
1494 // could be set on input to the CTLZ node. If this bit is set, the SRL
1495 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
1496 // to an SRL,XOR pair, which is likely to simplify more.
1497 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
1498 SDOperand Op = N0.getOperand(0);
1499 if (ShAmt) {
1500 Op = DAG.getNode(ISD::SRL, VT, Op,
1501 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
1502 AddToWorkList(Op.Val);
1503 }
1504 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
1505 }
1506 }
1507
Nate Begeman83e75ec2005-09-06 04:43:02 +00001508 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001509}
1510
Nate Begeman83e75ec2005-09-06 04:43:02 +00001511SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001512 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001513 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001514
1515 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001516 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001517 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001518 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001519}
1520
Nate Begeman83e75ec2005-09-06 04:43:02 +00001521SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001522 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001523 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001524
1525 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001526 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001527 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001528 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001529}
1530
Nate Begeman83e75ec2005-09-06 04:43:02 +00001531SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001532 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001533 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001534
1535 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001536 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001537 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001538 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001539}
1540
Nate Begeman452d7be2005-09-16 00:54:12 +00001541SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1542 SDOperand N0 = N->getOperand(0);
1543 SDOperand N1 = N->getOperand(1);
1544 SDOperand N2 = N->getOperand(2);
1545 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1546 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1547 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1548 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001549
Nate Begeman452d7be2005-09-16 00:54:12 +00001550 // fold select C, X, X -> X
1551 if (N1 == N2)
1552 return N1;
1553 // fold select true, X, Y -> X
1554 if (N0C && !N0C->isNullValue())
1555 return N1;
1556 // fold select false, X, Y -> Y
1557 if (N0C && N0C->isNullValue())
1558 return N2;
1559 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001560 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001561 return DAG.getNode(ISD::OR, VT, N0, N2);
1562 // fold select C, 0, X -> ~C & X
1563 // FIXME: this should check for C type == X type, not i1?
1564 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1565 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001566 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001567 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1568 }
1569 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001570 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001571 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001572 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001573 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1574 }
1575 // fold select C, X, 0 -> C & X
1576 // FIXME: this should check for C type == X type, not i1?
1577 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1578 return DAG.getNode(ISD::AND, VT, N0, N1);
1579 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1580 if (MVT::i1 == VT && N0 == N1)
1581 return DAG.getNode(ISD::OR, VT, N0, N2);
1582 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1583 if (MVT::i1 == VT && N0 == N2)
1584 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00001585
Chris Lattner40c62d52005-10-18 06:04:22 +00001586 // If we can fold this based on the true/false value, do so.
1587 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00001588 return SDOperand(N, 0); // Don't revisit N.
1589
Nate Begeman44728a72005-09-19 22:34:01 +00001590 // fold selects based on a setcc into other things, such as min/max/abs
1591 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001592 // FIXME:
1593 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1594 // having to say they don't support SELECT_CC on every type the DAG knows
1595 // about, since there is no way to mark an opcode illegal at all value types
1596 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1597 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1598 N1, N2, N0.getOperand(2));
1599 else
1600 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001601 return SDOperand();
1602}
1603
1604SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001605 SDOperand N0 = N->getOperand(0);
1606 SDOperand N1 = N->getOperand(1);
1607 SDOperand N2 = N->getOperand(2);
1608 SDOperand N3 = N->getOperand(3);
1609 SDOperand N4 = N->getOperand(4);
1610 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1611 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1612 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1613 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1614
1615 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001616 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner5eed34d2006-05-12 17:57:54 +00001617 //ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
Chris Lattner91559022005-10-05 04:45:43 +00001618
Nate Begeman44728a72005-09-19 22:34:01 +00001619 // fold select_cc lhs, rhs, x, x, cc -> x
1620 if (N2 == N3)
1621 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001622
1623 // If we can fold this based on the true/false value, do so.
1624 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00001625 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00001626
Nate Begeman44728a72005-09-19 22:34:01 +00001627 // fold select_cc into other things, such as min/max/abs
1628 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001629}
1630
1631SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1632 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1633 cast<CondCodeSDNode>(N->getOperand(2))->get());
1634}
1635
Nate Begeman83e75ec2005-09-06 04:43:02 +00001636SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001637 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001638 MVT::ValueType VT = N->getValueType(0);
1639
Nate Begeman1d4d4142005-09-01 00:19:25 +00001640 // fold (sext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001641 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001642 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00001643
Nate Begeman1d4d4142005-09-01 00:19:25 +00001644 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001645 // fold (sext (aext x)) -> (sext x)
1646 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001647 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00001648
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001649 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
Chris Lattnercc2210b2005-12-07 18:02:05 +00001650 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1651 (!AfterLegalize ||
1652 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001653 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1654 DAG.getValueType(N0.getValueType()));
Chris Lattner310b5782006-05-06 23:06:26 +00001655
Evan Cheng110dec22005-12-14 02:19:23 +00001656 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001657 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1658 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001659 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1660 N0.getOperand(1), N0.getOperand(2),
1661 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001662 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001663 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1664 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001665 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00001666 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001667
1668 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1669 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1670 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1671 N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001672 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1673 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1674 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001675 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001676 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1677 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001678 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001679 }
1680
Nate Begeman83e75ec2005-09-06 04:43:02 +00001681 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001682}
1683
Nate Begeman83e75ec2005-09-06 04:43:02 +00001684SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001685 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001686 MVT::ValueType VT = N->getValueType(0);
1687
Nate Begeman1d4d4142005-09-01 00:19:25 +00001688 // fold (zext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001689 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001690 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001691 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001692 // fold (zext (aext x)) -> (zext x)
1693 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001694 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Evan Cheng110dec22005-12-14 02:19:23 +00001695 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1696 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001697 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
Chris Lattner00cb95c2005-12-14 07:58:38 +00001698 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
Evan Cheng110dec22005-12-14 02:19:23 +00001699 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001700 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1701 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001702 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1703 N0.getOperand(1), N0.getOperand(2),
1704 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001705 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001706 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1707 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001708 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00001709 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001710
1711 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1712 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1713 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1714 N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001715 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1716 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1717 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001718 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001719 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1720 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001721 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001722 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001723 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001724}
1725
Chris Lattner5ffc0662006-05-05 05:58:59 +00001726SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
1727 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001728 MVT::ValueType VT = N->getValueType(0);
1729
1730 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001731 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00001732 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
1733 // fold (aext (aext x)) -> (aext x)
1734 // fold (aext (zext x)) -> (zext x)
1735 // fold (aext (sext x)) -> (sext x)
1736 if (N0.getOpcode() == ISD::ANY_EXTEND ||
1737 N0.getOpcode() == ISD::ZERO_EXTEND ||
1738 N0.getOpcode() == ISD::SIGN_EXTEND)
1739 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
1740
1741 // fold (aext (truncate x)) -> x iff x size == zext size.
1742 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT)
1743 return N0.getOperand(0);
1744 // fold (aext (load x)) -> (aext (truncate (extload x)))
1745 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1746 (!AfterLegalize||TLI.isOperationLegal(ISD::EXTLOAD, N0.getValueType()))) {
1747 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0),
1748 N0.getOperand(1), N0.getOperand(2),
1749 N0.getValueType());
1750 CombineTo(N, ExtLoad);
1751 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1752 ExtLoad.getValue(1));
1753 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1754 }
1755
1756 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
1757 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
1758 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
1759 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD ||
1760 N0.getOpcode() == ISD::SEXTLOAD) &&
1761 N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001762 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1763 SDOperand ExtLoad = DAG.getExtLoad(N0.getOpcode(), VT, N0.getOperand(0),
1764 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001765 CombineTo(N, ExtLoad);
1766 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1767 ExtLoad.getValue(1));
1768 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1769 }
1770 return SDOperand();
1771}
1772
1773
Nate Begeman83e75ec2005-09-06 04:43:02 +00001774SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001775 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001776 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001777 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001778 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001779 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001780
Nate Begeman1d4d4142005-09-01 00:19:25 +00001781 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00001782 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00001783 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00001784
Chris Lattner541a24f2006-05-06 22:43:44 +00001785 // If the input is already sign extended, just drop the extension.
Chris Lattneree4ea922006-05-06 09:30:03 +00001786 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
1787 return N0;
1788
Nate Begeman646d7e22005-09-02 21:18:40 +00001789 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1790 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1791 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001792 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001793 }
Chris Lattner4b37e872006-05-08 21:18:59 +00001794
Nate Begeman07ed4172005-10-10 21:26:48 +00001795 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001796 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00001797 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00001798
1799 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
1800 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
1801 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
1802 if (N0.getOpcode() == ISD::SRL) {
1803 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
1804 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
1805 // We can turn this into an SRA iff the input to the SRL is already sign
1806 // extended enough.
1807 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
1808 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
1809 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
1810 }
1811 }
1812
Nate Begemanded49632005-10-13 03:11:28 +00001813 // fold (sext_inreg (extload x)) -> (sextload x)
1814 if (N0.getOpcode() == ISD::EXTLOAD &&
1815 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001816 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001817 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1818 N0.getOperand(1), N0.getOperand(2),
1819 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001820 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001821 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001822 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001823 }
1824 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001825 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00001826 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001827 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001828 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1829 N0.getOperand(1), N0.getOperand(2),
1830 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001831 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001832 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001833 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001834 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001835 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001836}
1837
Nate Begeman83e75ec2005-09-06 04:43:02 +00001838SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001839 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001840 MVT::ValueType VT = N->getValueType(0);
1841
1842 // noop truncate
1843 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001844 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001845 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001846 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001847 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001848 // fold (truncate (truncate x)) -> (truncate x)
1849 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001850 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001851 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00001852 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
1853 N0.getOpcode() == ISD::ANY_EXTEND) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001854 if (N0.getValueType() < VT)
1855 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001856 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001857 else if (N0.getValueType() > VT)
1858 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001859 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001860 else
1861 // if the source and dest are the same type, we can drop both the extend
1862 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001863 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001864 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001865 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00001866 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00001867 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1868 "Cannot truncate to larger type!");
1869 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001870 // For big endian targets, we need to add an offset to the pointer to load
1871 // the correct bytes. For little endian systems, we merely need to read
1872 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001873 uint64_t PtrOff =
1874 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001875 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1876 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1877 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001878 AddToWorkList(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001879 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00001880 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001881 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001882 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00001883 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001884 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001885}
1886
Chris Lattner94683772005-12-23 05:30:37 +00001887SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
1888 SDOperand N0 = N->getOperand(0);
1889 MVT::ValueType VT = N->getValueType(0);
1890
1891 // If the input is a constant, let getNode() fold it.
1892 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
1893 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
1894 if (Res.Val != N) return Res;
1895 }
1896
Chris Lattnerc8547d82005-12-23 05:37:50 +00001897 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
1898 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00001899
Chris Lattner57104102005-12-23 05:44:41 +00001900 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00001901 // FIXME: These xforms need to know that the resultant load doesn't need a
1902 // higher alignment than the original!
1903 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00001904 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
1905 N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00001906 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00001907 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
1908 Load.getValue(1));
1909 return Load;
1910 }
1911
Chris Lattner94683772005-12-23 05:30:37 +00001912 return SDOperand();
1913}
1914
Chris Lattner6258fb22006-04-02 02:53:43 +00001915SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
1916 SDOperand N0 = N->getOperand(0);
1917 MVT::ValueType VT = N->getValueType(0);
1918
1919 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
1920 // First check to see if this is all constant.
1921 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
1922 VT == MVT::Vector) {
1923 bool isSimple = true;
1924 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
1925 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
1926 N0.getOperand(i).getOpcode() != ISD::Constant &&
1927 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
1928 isSimple = false;
1929 break;
1930 }
1931
Chris Lattner97c20732006-04-03 17:29:28 +00001932 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
1933 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00001934 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
1935 }
1936 }
1937
1938 return SDOperand();
1939}
1940
1941/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
1942/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
1943/// destination element value type.
1944SDOperand DAGCombiner::
1945ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
1946 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
1947
1948 // If this is already the right type, we're done.
1949 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
1950
1951 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
1952 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
1953
1954 // If this is a conversion of N elements of one type to N elements of another
1955 // type, convert each element. This handles FP<->INT cases.
1956 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001957 SmallVector<SDOperand, 8> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00001958 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00001959 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00001960 AddToWorkList(Ops.back().Val);
1961 }
Chris Lattner6258fb22006-04-02 02:53:43 +00001962 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
1963 Ops.push_back(DAG.getValueType(DstEltVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001964 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00001965 }
1966
1967 // Otherwise, we're growing or shrinking the elements. To avoid having to
1968 // handle annoying details of growing/shrinking FP values, we convert them to
1969 // int first.
1970 if (MVT::isFloatingPoint(SrcEltVT)) {
1971 // Convert the input float vector to a int vector where the elements are the
1972 // same sizes.
1973 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
1974 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
1975 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
1976 SrcEltVT = IntVT;
1977 }
1978
1979 // Now we know the input is an integer vector. If the output is a FP type,
1980 // convert to integer first, then to FP of the right size.
1981 if (MVT::isFloatingPoint(DstEltVT)) {
1982 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
1983 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
1984 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
1985
1986 // Next, convert to FP elements of the same size.
1987 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
1988 }
1989
1990 // Okay, we know the src/dst types are both integers of differing types.
1991 // Handling growing first.
1992 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
1993 if (SrcBitSize < DstBitSize) {
1994 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
1995
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001996 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00001997 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
1998 i += NumInputsPerOutput) {
1999 bool isLE = TLI.isLittleEndian();
2000 uint64_t NewBits = 0;
2001 bool EltIsUndef = true;
2002 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2003 // Shift the previously computed bits over.
2004 NewBits <<= SrcBitSize;
2005 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2006 if (Op.getOpcode() == ISD::UNDEF) continue;
2007 EltIsUndef = false;
2008
2009 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2010 }
2011
2012 if (EltIsUndef)
2013 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2014 else
2015 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2016 }
2017
2018 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2019 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002020 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002021 }
2022
2023 // Finally, this must be the case where we are shrinking elements: each input
2024 // turns into multiple outputs.
2025 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002026 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002027 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2028 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2029 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2030 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2031 continue;
2032 }
2033 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2034
2035 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2036 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2037 OpVal >>= DstBitSize;
2038 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2039 }
2040
2041 // For big endian targets, swap the order of the pieces of each element.
2042 if (!TLI.isLittleEndian())
2043 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2044 }
2045 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2046 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002047 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002048}
2049
2050
2051
Chris Lattner01b3d732005-09-28 22:28:18 +00002052SDOperand DAGCombiner::visitFADD(SDNode *N) {
2053 SDOperand N0 = N->getOperand(0);
2054 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002055 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2056 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002057 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002058
2059 // fold (fadd c1, c2) -> c1+c2
2060 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002061 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002062 // canonicalize constant to RHS
2063 if (N0CFP && !N1CFP)
2064 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002065 // fold (A + (-B)) -> A-B
2066 if (N1.getOpcode() == ISD::FNEG)
2067 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002068 // fold ((-A) + B) -> B-A
2069 if (N0.getOpcode() == ISD::FNEG)
2070 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002071 return SDOperand();
2072}
2073
2074SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2075 SDOperand N0 = N->getOperand(0);
2076 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002077 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2078 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002079 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002080
2081 // fold (fsub c1, c2) -> c1-c2
2082 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002083 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002084 // fold (A-(-B)) -> A+B
2085 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00002086 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002087 return SDOperand();
2088}
2089
2090SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2091 SDOperand N0 = N->getOperand(0);
2092 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002093 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2094 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002095 MVT::ValueType VT = N->getValueType(0);
2096
Nate Begeman11af4ea2005-10-17 20:40:11 +00002097 // fold (fmul c1, c2) -> c1*c2
2098 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002099 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002100 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002101 if (N0CFP && !N1CFP)
2102 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002103 // fold (fmul X, 2.0) -> (fadd X, X)
2104 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2105 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002106 return SDOperand();
2107}
2108
2109SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2110 SDOperand N0 = N->getOperand(0);
2111 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002112 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2113 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002114 MVT::ValueType VT = N->getValueType(0);
2115
Nate Begemana148d982006-01-18 22:35:16 +00002116 // fold (fdiv c1, c2) -> c1/c2
2117 if (N0CFP && N1CFP)
2118 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002119 return SDOperand();
2120}
2121
2122SDOperand DAGCombiner::visitFREM(SDNode *N) {
2123 SDOperand N0 = N->getOperand(0);
2124 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002125 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2126 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002127 MVT::ValueType VT = N->getValueType(0);
2128
Nate Begemana148d982006-01-18 22:35:16 +00002129 // fold (frem c1, c2) -> fmod(c1,c2)
2130 if (N0CFP && N1CFP)
2131 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002132 return SDOperand();
2133}
2134
Chris Lattner12d83032006-03-05 05:30:57 +00002135SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2136 SDOperand N0 = N->getOperand(0);
2137 SDOperand N1 = N->getOperand(1);
2138 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2139 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2140 MVT::ValueType VT = N->getValueType(0);
2141
2142 if (N0CFP && N1CFP) // Constant fold
2143 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2144
2145 if (N1CFP) {
2146 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2147 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2148 union {
2149 double d;
2150 int64_t i;
2151 } u;
2152 u.d = N1CFP->getValue();
2153 if (u.i >= 0)
2154 return DAG.getNode(ISD::FABS, VT, N0);
2155 else
2156 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2157 }
2158
2159 // copysign(fabs(x), y) -> copysign(x, y)
2160 // copysign(fneg(x), y) -> copysign(x, y)
2161 // copysign(copysign(x,z), y) -> copysign(x, y)
2162 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2163 N0.getOpcode() == ISD::FCOPYSIGN)
2164 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2165
2166 // copysign(x, abs(y)) -> abs(x)
2167 if (N1.getOpcode() == ISD::FABS)
2168 return DAG.getNode(ISD::FABS, VT, N0);
2169
2170 // copysign(x, copysign(y,z)) -> copysign(x, z)
2171 if (N1.getOpcode() == ISD::FCOPYSIGN)
2172 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2173
2174 // copysign(x, fp_extend(y)) -> copysign(x, y)
2175 // copysign(x, fp_round(y)) -> copysign(x, y)
2176 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2177 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2178
2179 return SDOperand();
2180}
2181
2182
Chris Lattner01b3d732005-09-28 22:28:18 +00002183
Nate Begeman83e75ec2005-09-06 04:43:02 +00002184SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002185 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002186 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002187 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002188
2189 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002190 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002191 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002192 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002193}
2194
Nate Begeman83e75ec2005-09-06 04:43:02 +00002195SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002196 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002197 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002198 MVT::ValueType VT = N->getValueType(0);
2199
Nate Begeman1d4d4142005-09-01 00:19:25 +00002200 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002201 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002202 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002203 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002204}
2205
Nate Begeman83e75ec2005-09-06 04:43:02 +00002206SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002207 SDOperand N0 = N->getOperand(0);
2208 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2209 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002210
2211 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002212 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002213 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002214 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002215}
2216
Nate Begeman83e75ec2005-09-06 04:43:02 +00002217SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002218 SDOperand N0 = N->getOperand(0);
2219 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2220 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002221
2222 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002223 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002224 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002225 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002226}
2227
Nate Begeman83e75ec2005-09-06 04:43:02 +00002228SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002229 SDOperand N0 = N->getOperand(0);
2230 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2231 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002232
2233 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002234 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002235 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002236
2237 // fold (fp_round (fp_extend x)) -> x
2238 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2239 return N0.getOperand(0);
2240
2241 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2242 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2243 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2244 AddToWorkList(Tmp.Val);
2245 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2246 }
2247
Nate Begeman83e75ec2005-09-06 04:43:02 +00002248 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002249}
2250
Nate Begeman83e75ec2005-09-06 04:43:02 +00002251SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002252 SDOperand N0 = N->getOperand(0);
2253 MVT::ValueType VT = N->getValueType(0);
2254 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002255 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002256
Nate Begeman1d4d4142005-09-01 00:19:25 +00002257 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002258 if (N0CFP) {
2259 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002260 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002261 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002262 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002263}
2264
Nate Begeman83e75ec2005-09-06 04:43:02 +00002265SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002266 SDOperand N0 = N->getOperand(0);
2267 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2268 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002269
2270 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002271 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002272 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00002273
2274 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
2275 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
2276 (!AfterLegalize||TLI.isOperationLegal(ISD::EXTLOAD, N0.getValueType()))) {
2277 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0),
2278 N0.getOperand(1), N0.getOperand(2),
2279 N0.getValueType());
2280 CombineTo(N, ExtLoad);
2281 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
2282 ExtLoad.getValue(1));
2283 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2284 }
2285
2286
Nate Begeman83e75ec2005-09-06 04:43:02 +00002287 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002288}
2289
Nate Begeman83e75ec2005-09-06 04:43:02 +00002290SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002291 SDOperand N0 = N->getOperand(0);
2292 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2293 MVT::ValueType VT = N->getValueType(0);
2294
2295 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002296 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002297 return DAG.getNode(ISD::FNEG, VT, N0);
2298 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002299 if (N0.getOpcode() == ISD::SUB)
2300 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002301 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002302 if (N0.getOpcode() == ISD::FNEG)
2303 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002304 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002305}
2306
Nate Begeman83e75ec2005-09-06 04:43:02 +00002307SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002308 SDOperand N0 = N->getOperand(0);
2309 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2310 MVT::ValueType VT = N->getValueType(0);
2311
Nate Begeman1d4d4142005-09-01 00:19:25 +00002312 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002313 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002314 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002315 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002316 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002317 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002318 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002319 // fold (fabs (fcopysign x, y)) -> (fabs x)
2320 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2321 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2322
Nate Begeman83e75ec2005-09-06 04:43:02 +00002323 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002324}
2325
Nate Begeman44728a72005-09-19 22:34:01 +00002326SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2327 SDOperand Chain = N->getOperand(0);
2328 SDOperand N1 = N->getOperand(1);
2329 SDOperand N2 = N->getOperand(2);
2330 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2331
2332 // never taken branch, fold to chain
2333 if (N1C && N1C->isNullValue())
2334 return Chain;
2335 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002336 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002337 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002338 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2339 // on the target.
2340 if (N1.getOpcode() == ISD::SETCC &&
2341 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2342 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2343 N1.getOperand(0), N1.getOperand(1), N2);
2344 }
Nate Begeman44728a72005-09-19 22:34:01 +00002345 return SDOperand();
2346}
2347
Chris Lattner3ea0b472005-10-05 06:47:48 +00002348// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2349//
Nate Begeman44728a72005-09-19 22:34:01 +00002350SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002351 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2352 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2353
2354 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002355 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2356 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2357
2358 // fold br_cc true, dest -> br dest (unconditional branch)
2359 if (SCCC && SCCC->getValue())
2360 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2361 N->getOperand(4));
2362 // fold br_cc false, dest -> unconditional fall through
2363 if (SCCC && SCCC->isNullValue())
2364 return N->getOperand(0);
2365 // fold to a simpler setcc
2366 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2367 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2368 Simp.getOperand(2), Simp.getOperand(0),
2369 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002370 return SDOperand();
2371}
2372
Chris Lattner01a22022005-10-10 22:04:48 +00002373SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2374 SDOperand Chain = N->getOperand(0);
2375 SDOperand Ptr = N->getOperand(1);
2376 SDOperand SrcValue = N->getOperand(2);
Chris Lattnere4b95392006-03-31 18:06:18 +00002377
2378 // If there are no uses of the loaded value, change uses of the chain value
2379 // into uses of the chain input (i.e. delete the dead load).
2380 if (N->hasNUsesOfValue(0, 0))
2381 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
Chris Lattner01a22022005-10-10 22:04:48 +00002382
2383 // If this load is directly stored, replace the load value with the stored
2384 // value.
2385 // TODO: Handle store large -> read small portion.
2386 // TODO: Handle TRUNCSTORE/EXTLOAD
2387 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2388 Chain.getOperand(1).getValueType() == N->getValueType(0))
2389 return CombineTo(N, Chain.getOperand(1), Chain);
2390
2391 return SDOperand();
2392}
2393
Chris Lattner29cd7db2006-03-31 18:10:41 +00002394/// visitXEXTLOAD - Handle EXTLOAD/ZEXTLOAD/SEXTLOAD.
2395SDOperand DAGCombiner::visitXEXTLOAD(SDNode *N) {
2396 SDOperand Chain = N->getOperand(0);
2397 SDOperand Ptr = N->getOperand(1);
2398 SDOperand SrcValue = N->getOperand(2);
2399 SDOperand EVT = N->getOperand(3);
2400
2401 // If there are no uses of the loaded value, change uses of the chain value
2402 // into uses of the chain input (i.e. delete the dead load).
2403 if (N->hasNUsesOfValue(0, 0))
2404 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
2405
2406 return SDOperand();
2407}
2408
Chris Lattner87514ca2005-10-10 22:31:19 +00002409SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2410 SDOperand Chain = N->getOperand(0);
2411 SDOperand Value = N->getOperand(1);
2412 SDOperand Ptr = N->getOperand(2);
2413 SDOperand SrcValue = N->getOperand(3);
2414
2415 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002416 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002417 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2418 // Make sure that these stores are the same value type:
2419 // FIXME: we really care that the second store is >= size of the first.
2420 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002421 // Create a new store of Value that replaces both stores.
2422 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002423 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2424 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002425 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2426 PrevStore->getOperand(0), Value, Ptr,
2427 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002428 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002429 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002430 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002431 }
2432
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002433 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002434 // FIXME: This needs to know that the resultant store does not need a
2435 // higher alignment than the original.
2436 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002437 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2438 Ptr, SrcValue);
2439
Chris Lattner87514ca2005-10-10 22:31:19 +00002440 return SDOperand();
2441}
2442
Chris Lattnerca242442006-03-19 01:27:56 +00002443SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2444 SDOperand InVec = N->getOperand(0);
2445 SDOperand InVal = N->getOperand(1);
2446 SDOperand EltNo = N->getOperand(2);
2447
2448 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2449 // vector with the inserted element.
2450 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2451 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002452 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002453 if (Elt < Ops.size())
2454 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002455 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
2456 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002457 }
2458
2459 return SDOperand();
2460}
2461
2462SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2463 SDOperand InVec = N->getOperand(0);
2464 SDOperand InVal = N->getOperand(1);
2465 SDOperand EltNo = N->getOperand(2);
2466 SDOperand NumElts = N->getOperand(3);
2467 SDOperand EltType = N->getOperand(4);
2468
2469 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2470 // vector with the inserted element.
2471 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2472 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002473 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002474 if (Elt < Ops.size()-2)
2475 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002476 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
2477 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002478 }
2479
2480 return SDOperand();
2481}
2482
Chris Lattnerd7648c82006-03-28 20:28:38 +00002483SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
2484 unsigned NumInScalars = N->getNumOperands()-2;
2485 SDOperand NumElts = N->getOperand(NumInScalars);
2486 SDOperand EltType = N->getOperand(NumInScalars+1);
2487
2488 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
2489 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
2490 // two distinct vectors, turn this into a shuffle node.
2491 SDOperand VecIn1, VecIn2;
2492 for (unsigned i = 0; i != NumInScalars; ++i) {
2493 // Ignore undef inputs.
2494 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
2495
2496 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
2497 // constant index, bail out.
2498 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
2499 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
2500 VecIn1 = VecIn2 = SDOperand(0, 0);
2501 break;
2502 }
2503
2504 // If the input vector type disagrees with the result of the vbuild_vector,
2505 // we can't make a shuffle.
2506 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
2507 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
2508 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
2509 VecIn1 = VecIn2 = SDOperand(0, 0);
2510 break;
2511 }
2512
2513 // Otherwise, remember this. We allow up to two distinct input vectors.
2514 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
2515 continue;
2516
2517 if (VecIn1.Val == 0) {
2518 VecIn1 = ExtractedFromVec;
2519 } else if (VecIn2.Val == 0) {
2520 VecIn2 = ExtractedFromVec;
2521 } else {
2522 // Too many inputs.
2523 VecIn1 = VecIn2 = SDOperand(0, 0);
2524 break;
2525 }
2526 }
2527
2528 // If everything is good, we can make a shuffle operation.
2529 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002530 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00002531 for (unsigned i = 0; i != NumInScalars; ++i) {
2532 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
2533 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2534 continue;
2535 }
2536
2537 SDOperand Extract = N->getOperand(i);
2538
2539 // If extracting from the first vector, just use the index directly.
2540 if (Extract.getOperand(0) == VecIn1) {
2541 BuildVecIndices.push_back(Extract.getOperand(1));
2542 continue;
2543 }
2544
2545 // Otherwise, use InIdx + VecSize
2546 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
2547 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
2548 }
2549
2550 // Add count and size info.
2551 BuildVecIndices.push_back(NumElts);
2552 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
2553
2554 // Return the new VVECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002555 SDOperand Ops[5];
2556 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00002557 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002558 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00002559 } else {
2560 // Use an undef vbuild_vector as input for the second operand.
2561 std::vector<SDOperand> UnOps(NumInScalars,
2562 DAG.getNode(ISD::UNDEF,
2563 cast<VTSDNode>(EltType)->getVT()));
2564 UnOps.push_back(NumElts);
2565 UnOps.push_back(EltType);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002566 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2567 &UnOps[0], UnOps.size());
2568 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00002569 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002570 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2571 &BuildVecIndices[0], BuildVecIndices.size());
2572 Ops[3] = NumElts;
2573 Ops[4] = EltType;
2574 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
Chris Lattnerd7648c82006-03-28 20:28:38 +00002575 }
2576
2577 return SDOperand();
2578}
2579
Chris Lattner66445d32006-03-28 22:11:53 +00002580SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002581 SDOperand ShufMask = N->getOperand(2);
2582 unsigned NumElts = ShufMask.getNumOperands();
2583
2584 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2585 bool isIdentity = true;
2586 for (unsigned i = 0; i != NumElts; ++i) {
2587 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2588 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2589 isIdentity = false;
2590 break;
2591 }
2592 }
2593 if (isIdentity) return N->getOperand(0);
2594
2595 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2596 isIdentity = true;
2597 for (unsigned i = 0; i != NumElts; ++i) {
2598 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2599 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2600 isIdentity = false;
2601 break;
2602 }
2603 }
2604 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00002605
2606 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
2607 // needed at all.
2608 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00002609 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002610 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00002611 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002612 for (unsigned i = 0; i != NumElts; ++i)
2613 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
2614 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
2615 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00002616 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00002617 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00002618 BaseIdx = Idx;
2619 } else {
2620 if (BaseIdx != Idx)
2621 isSplat = false;
2622 if (VecNum != V) {
2623 isUnary = false;
2624 break;
2625 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00002626 }
2627 }
2628
2629 SDOperand N0 = N->getOperand(0);
2630 SDOperand N1 = N->getOperand(1);
2631 // Normalize unary shuffle so the RHS is undef.
2632 if (isUnary && VecNum == 1)
2633 std::swap(N0, N1);
2634
Evan Cheng917ec982006-07-21 08:25:53 +00002635 // If it is a splat, check if the argument vector is a build_vector with
2636 // all scalar elements the same.
2637 if (isSplat) {
2638 SDNode *V = N0.Val;
2639 if (V->getOpcode() == ISD::BIT_CONVERT)
2640 V = V->getOperand(0).Val;
2641 if (V->getOpcode() == ISD::BUILD_VECTOR) {
2642 unsigned NumElems = V->getNumOperands()-2;
2643 if (NumElems > BaseIdx) {
2644 SDOperand Base;
2645 bool AllSame = true;
2646 for (unsigned i = 0; i != NumElems; ++i) {
2647 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
2648 Base = V->getOperand(i);
2649 break;
2650 }
2651 }
2652 // Splat of <u, u, u, u>, return <u, u, u, u>
2653 if (!Base.Val)
2654 return N0;
2655 for (unsigned i = 0; i != NumElems; ++i) {
2656 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
2657 V->getOperand(i) != Base) {
2658 AllSame = false;
2659 break;
2660 }
2661 }
2662 // Splat of <x, x, x, x>, return <x, x, x, x>
2663 if (AllSame)
2664 return N0;
2665 }
2666 }
2667 }
2668
Evan Chenge7bec0d2006-07-20 22:44:41 +00002669 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
2670 // into an undef.
2671 if (isUnary || N0 == N1) {
2672 if (N0.getOpcode() == ISD::UNDEF)
Evan Chengc04766a2006-04-06 23:20:43 +00002673 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00002674 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2675 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002676 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00002677 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00002678 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2679 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2680 MappedOps.push_back(ShufMask.getOperand(i));
2681 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00002682 unsigned NewIdx =
2683 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2684 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00002685 }
2686 }
2687 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002688 &MappedOps[0], MappedOps.size());
Chris Lattner3e104b12006-04-08 04:15:24 +00002689 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00002690 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
Evan Chenge7bec0d2006-07-20 22:44:41 +00002691 N0,
Chris Lattner66445d32006-03-28 22:11:53 +00002692 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
2693 ShufMask);
2694 }
2695
2696 return SDOperand();
2697}
2698
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002699SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
2700 SDOperand ShufMask = N->getOperand(2);
2701 unsigned NumElts = ShufMask.getNumOperands()-2;
2702
2703 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2704 bool isIdentity = true;
2705 for (unsigned i = 0; i != NumElts; ++i) {
2706 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2707 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2708 isIdentity = false;
2709 break;
2710 }
2711 }
2712 if (isIdentity) return N->getOperand(0);
2713
2714 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2715 isIdentity = true;
2716 for (unsigned i = 0; i != NumElts; ++i) {
2717 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2718 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2719 isIdentity = false;
2720 break;
2721 }
2722 }
2723 if (isIdentity) return N->getOperand(1);
2724
Evan Chenge7bec0d2006-07-20 22:44:41 +00002725 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
2726 // needed at all.
2727 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00002728 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002729 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00002730 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002731 for (unsigned i = 0; i != NumElts; ++i)
2732 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
2733 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
2734 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00002735 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00002736 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00002737 BaseIdx = Idx;
2738 } else {
2739 if (BaseIdx != Idx)
2740 isSplat = false;
2741 if (VecNum != V) {
2742 isUnary = false;
2743 break;
2744 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00002745 }
2746 }
2747
2748 SDOperand N0 = N->getOperand(0);
2749 SDOperand N1 = N->getOperand(1);
2750 // Normalize unary shuffle so the RHS is undef.
2751 if (isUnary && VecNum == 1)
2752 std::swap(N0, N1);
2753
Evan Cheng917ec982006-07-21 08:25:53 +00002754 // If it is a splat, check if the argument vector is a build_vector with
2755 // all scalar elements the same.
2756 if (isSplat) {
2757 SDNode *V = N0.Val;
2758 if (V->getOpcode() == ISD::VBIT_CONVERT)
2759 V = V->getOperand(0).Val;
2760 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
2761 unsigned NumElems = V->getNumOperands()-2;
2762 if (NumElems > BaseIdx) {
2763 SDOperand Base;
2764 bool AllSame = true;
2765 for (unsigned i = 0; i != NumElems; ++i) {
2766 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
2767 Base = V->getOperand(i);
2768 break;
2769 }
2770 }
2771 // Splat of <u, u, u, u>, return <u, u, u, u>
2772 if (!Base.Val)
2773 return N0;
2774 for (unsigned i = 0; i != NumElems; ++i) {
2775 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
2776 V->getOperand(i) != Base) {
2777 AllSame = false;
2778 break;
2779 }
2780 }
2781 // Splat of <x, x, x, x>, return <x, x, x, x>
2782 if (AllSame)
2783 return N0;
2784 }
2785 }
2786 }
2787
Evan Chenge7bec0d2006-07-20 22:44:41 +00002788 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
2789 // into an undef.
2790 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00002791 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2792 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002793 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00002794 for (unsigned i = 0; i != NumElts; ++i) {
2795 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2796 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2797 MappedOps.push_back(ShufMask.getOperand(i));
2798 } else {
2799 unsigned NewIdx =
2800 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2801 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
2802 }
2803 }
2804 // Add the type/#elts values.
2805 MappedOps.push_back(ShufMask.getOperand(NumElts));
2806 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
2807
2808 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002809 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00002810 AddToWorkList(ShufMask.Val);
2811
2812 // Build the undef vector.
2813 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
2814 for (unsigned i = 0; i != NumElts; ++i)
2815 MappedOps[i] = UDVal;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002816 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
2817 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002818 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2819 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00002820
2821 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
Evan Chenge7bec0d2006-07-20 22:44:41 +00002822 N0, UDVal, ShufMask,
Chris Lattner17614ea2006-04-08 05:34:25 +00002823 MappedOps[NumElts], MappedOps[NumElts+1]);
2824 }
2825
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002826 return SDOperand();
2827}
2828
Evan Cheng44f1f092006-04-20 08:56:16 +00002829/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
2830/// a VAND to a vector_shuffle with the destination vector and a zero vector.
2831/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
2832/// vector_shuffle V, Zero, <0, 4, 2, 4>
2833SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
2834 SDOperand LHS = N->getOperand(0);
2835 SDOperand RHS = N->getOperand(1);
2836 if (N->getOpcode() == ISD::VAND) {
2837 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
2838 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
2839 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
2840 RHS = RHS.getOperand(0);
2841 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
2842 std::vector<SDOperand> IdxOps;
2843 unsigned NumOps = RHS.getNumOperands();
2844 unsigned NumElts = NumOps-2;
2845 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
2846 for (unsigned i = 0; i != NumElts; ++i) {
2847 SDOperand Elt = RHS.getOperand(i);
2848 if (!isa<ConstantSDNode>(Elt))
2849 return SDOperand();
2850 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
2851 IdxOps.push_back(DAG.getConstant(i, EVT));
2852 else if (cast<ConstantSDNode>(Elt)->isNullValue())
2853 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
2854 else
2855 return SDOperand();
2856 }
2857
2858 // Let's see if the target supports this vector_shuffle.
2859 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
2860 return SDOperand();
2861
2862 // Return the new VVECTOR_SHUFFLE node.
2863 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
2864 SDOperand EVTNode = DAG.getValueType(EVT);
2865 std::vector<SDOperand> Ops;
2866 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode, EVTNode);
2867 Ops.push_back(LHS);
2868 AddToWorkList(LHS.Val);
2869 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
2870 ZeroOps.push_back(NumEltsNode);
2871 ZeroOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002872 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2873 &ZeroOps[0], ZeroOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00002874 IdxOps.push_back(NumEltsNode);
2875 IdxOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002876 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2877 &IdxOps[0], IdxOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00002878 Ops.push_back(NumEltsNode);
2879 Ops.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002880 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
2881 &Ops[0], Ops.size());
Evan Cheng44f1f092006-04-20 08:56:16 +00002882 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
2883 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
2884 DstVecSize, DstVecEVT);
2885 }
2886 return Result;
2887 }
2888 }
2889 return SDOperand();
2890}
2891
Chris Lattneredab1b92006-04-02 03:25:57 +00002892/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
2893/// the scalar operation of the vop if it is operating on an integer vector
2894/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
2895SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
2896 ISD::NodeType FPOp) {
2897 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
2898 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
2899 SDOperand LHS = N->getOperand(0);
2900 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00002901 SDOperand Shuffle = XformToShuffleWithZero(N);
2902 if (Shuffle.Val) return Shuffle;
2903
Chris Lattneredab1b92006-04-02 03:25:57 +00002904 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
2905 // this operation.
2906 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
2907 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002908 SmallVector<SDOperand, 8> Ops;
Chris Lattneredab1b92006-04-02 03:25:57 +00002909 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
2910 SDOperand LHSOp = LHS.getOperand(i);
2911 SDOperand RHSOp = RHS.getOperand(i);
2912 // If these two elements can't be folded, bail out.
2913 if ((LHSOp.getOpcode() != ISD::UNDEF &&
2914 LHSOp.getOpcode() != ISD::Constant &&
2915 LHSOp.getOpcode() != ISD::ConstantFP) ||
2916 (RHSOp.getOpcode() != ISD::UNDEF &&
2917 RHSOp.getOpcode() != ISD::Constant &&
2918 RHSOp.getOpcode() != ISD::ConstantFP))
2919 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00002920 // Can't fold divide by zero.
2921 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
2922 if ((RHSOp.getOpcode() == ISD::Constant &&
2923 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
2924 (RHSOp.getOpcode() == ISD::ConstantFP &&
2925 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
2926 break;
2927 }
Chris Lattneredab1b92006-04-02 03:25:57 +00002928 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00002929 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00002930 assert((Ops.back().getOpcode() == ISD::UNDEF ||
2931 Ops.back().getOpcode() == ISD::Constant ||
2932 Ops.back().getOpcode() == ISD::ConstantFP) &&
2933 "Scalar binop didn't fold!");
2934 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00002935
2936 if (Ops.size() == LHS.getNumOperands()-2) {
2937 Ops.push_back(*(LHS.Val->op_end()-2));
2938 Ops.push_back(*(LHS.Val->op_end()-1));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002939 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00002940 }
Chris Lattneredab1b92006-04-02 03:25:57 +00002941 }
2942
2943 return SDOperand();
2944}
2945
Nate Begeman44728a72005-09-19 22:34:01 +00002946SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00002947 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
2948
2949 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
2950 cast<CondCodeSDNode>(N0.getOperand(2))->get());
2951 // If we got a simplified select_cc node back from SimplifySelectCC, then
2952 // break it down into a new SETCC node, and a new SELECT node, and then return
2953 // the SELECT node, since we were called with a SELECT node.
2954 if (SCC.Val) {
2955 // Check to see if we got a select_cc back (to turn into setcc/select).
2956 // Otherwise, just return whatever node we got back, like fabs.
2957 if (SCC.getOpcode() == ISD::SELECT_CC) {
2958 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
2959 SCC.getOperand(0), SCC.getOperand(1),
2960 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00002961 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002962 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
2963 SCC.getOperand(3), SETCC);
2964 }
2965 return SCC;
2966 }
Nate Begeman44728a72005-09-19 22:34:01 +00002967 return SDOperand();
2968}
2969
Chris Lattner40c62d52005-10-18 06:04:22 +00002970/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
2971/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00002972/// select. Callers of this should assume that TheSelect is deleted if this
2973/// returns true. As such, they should return the appropriate thing (e.g. the
2974/// node) back to the top-level of the DAG combiner loop to avoid it being
2975/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00002976///
2977bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
2978 SDOperand RHS) {
2979
2980 // If this is a select from two identical things, try to pull the operation
2981 // through the select.
2982 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
2983#if 0
2984 std::cerr << "SELECT: ["; LHS.Val->dump();
2985 std::cerr << "] ["; RHS.Val->dump();
2986 std::cerr << "]\n";
2987#endif
2988
2989 // If this is a load and the token chain is identical, replace the select
2990 // of two loads with a load through a select of the address to load from.
2991 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
2992 // constants have been dropped into the constant pool.
2993 if ((LHS.getOpcode() == ISD::LOAD ||
2994 LHS.getOpcode() == ISD::EXTLOAD ||
2995 LHS.getOpcode() == ISD::ZEXTLOAD ||
2996 LHS.getOpcode() == ISD::SEXTLOAD) &&
2997 // Token chains must be identical.
2998 LHS.getOperand(0) == RHS.getOperand(0) &&
2999 // If this is an EXTLOAD, the VT's must match.
3000 (LHS.getOpcode() == ISD::LOAD ||
3001 LHS.getOperand(3) == RHS.getOperand(3))) {
3002 // FIXME: this conflates two src values, discarding one. This is not
3003 // the right thing to do, but nothing uses srcvalues now. When they do,
3004 // turn SrcValue into a list of locations.
3005 SDOperand Addr;
3006 if (TheSelect->getOpcode() == ISD::SELECT)
3007 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
3008 TheSelect->getOperand(0), LHS.getOperand(1),
3009 RHS.getOperand(1));
3010 else
3011 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
3012 TheSelect->getOperand(0),
3013 TheSelect->getOperand(1),
3014 LHS.getOperand(1), RHS.getOperand(1),
3015 TheSelect->getOperand(4));
3016
3017 SDOperand Load;
3018 if (LHS.getOpcode() == ISD::LOAD)
3019 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
3020 Addr, LHS.getOperand(2));
3021 else
3022 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
3023 LHS.getOperand(0), Addr, LHS.getOperand(2),
3024 cast<VTSDNode>(LHS.getOperand(3))->getVT());
3025 // Users of the select now use the result of the load.
3026 CombineTo(TheSelect, Load);
3027
3028 // Users of the old loads now use the new load's chain. We know the
3029 // old-load value is dead now.
3030 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
3031 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
3032 return true;
3033 }
3034 }
3035
3036 return false;
3037}
3038
Nate Begeman44728a72005-09-19 22:34:01 +00003039SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
3040 SDOperand N2, SDOperand N3,
3041 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00003042
3043 MVT::ValueType VT = N2.getValueType();
Chris Lattner5eed34d2006-05-12 17:57:54 +00003044 //ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003045 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
3046 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
3047 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
3048
3049 // Determine if the condition we're dealing with is constant
3050 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
3051 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
3052
3053 // fold select_cc true, x, y -> x
3054 if (SCCC && SCCC->getValue())
3055 return N2;
3056 // fold select_cc false, x, y -> y
3057 if (SCCC && SCCC->getValue() == 0)
3058 return N3;
3059
3060 // Check to see if we can simplify the select into an fabs node
3061 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
3062 // Allow either -0.0 or 0.0
3063 if (CFP->getValue() == 0.0) {
3064 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
3065 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
3066 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
3067 N2 == N3.getOperand(0))
3068 return DAG.getNode(ISD::FABS, VT, N0);
3069
3070 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
3071 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3072 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3073 N2.getOperand(0) == N3)
3074 return DAG.getNode(ISD::FABS, VT, N3);
3075 }
3076 }
3077
3078 // Check to see if we can perform the "gzip trick", transforming
3079 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
3080 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
3081 MVT::isInteger(N0.getValueType()) &&
3082 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
3083 MVT::ValueType XType = N0.getValueType();
3084 MVT::ValueType AType = N2.getValueType();
3085 if (XType >= AType) {
3086 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00003087 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00003088 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3089 unsigned ShCtV = Log2_64(N2C->getValue());
3090 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3091 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3092 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00003093 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003094 if (XType > AType) {
3095 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003096 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003097 }
3098 return DAG.getNode(ISD::AND, AType, Shift, N2);
3099 }
3100 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3101 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3102 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003103 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003104 if (XType > AType) {
3105 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003106 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003107 }
3108 return DAG.getNode(ISD::AND, AType, Shift, N2);
3109 }
3110 }
Nate Begeman07ed4172005-10-10 21:26:48 +00003111
3112 // fold select C, 16, 0 -> shl C, 4
3113 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3114 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3115 // Get a SetCC of the condition
3116 // FIXME: Should probably make sure that setcc is legal if we ever have a
3117 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00003118 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00003119 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00003120 if (AfterLegalize) {
3121 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003122 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00003123 } else {
3124 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003125 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00003126 }
Chris Lattner5750df92006-03-01 04:03:14 +00003127 AddToWorkList(SCC.Val);
3128 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00003129 // shl setcc result by log2 n2c
3130 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3131 DAG.getConstant(Log2_64(N2C->getValue()),
3132 TLI.getShiftAmountTy()));
3133 }
3134
Nate Begemanf845b452005-10-08 00:29:44 +00003135 // Check to see if this is the equivalent of setcc
3136 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3137 // otherwise, go ahead with the folds.
3138 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3139 MVT::ValueType XType = N0.getValueType();
3140 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3141 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3142 if (Res.getValueType() != VT)
3143 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3144 return Res;
3145 }
3146
3147 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3148 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3149 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3150 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3151 return DAG.getNode(ISD::SRL, XType, Ctlz,
3152 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3153 TLI.getShiftAmountTy()));
3154 }
3155 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3156 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3157 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3158 N0);
3159 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3160 DAG.getConstant(~0ULL, XType));
3161 return DAG.getNode(ISD::SRL, XType,
3162 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3163 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3164 TLI.getShiftAmountTy()));
3165 }
3166 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3167 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3168 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3169 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3170 TLI.getShiftAmountTy()));
3171 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3172 }
3173 }
3174
3175 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3176 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3177 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3178 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3179 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3180 MVT::ValueType XType = N0.getValueType();
3181 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3182 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3183 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3184 TLI.getShiftAmountTy()));
3185 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003186 AddToWorkList(Shift.Val);
3187 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003188 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3189 }
3190 }
3191 }
3192
Nate Begeman44728a72005-09-19 22:34:01 +00003193 return SDOperand();
3194}
3195
Nate Begeman452d7be2005-09-16 00:54:12 +00003196SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00003197 SDOperand N1, ISD::CondCode Cond,
3198 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003199 // These setcc operations always fold.
3200 switch (Cond) {
3201 default: break;
3202 case ISD::SETFALSE:
3203 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3204 case ISD::SETTRUE:
3205 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3206 }
3207
3208 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3209 uint64_t C1 = N1C->getValue();
3210 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
3211 uint64_t C0 = N0C->getValue();
3212
3213 // Sign extend the operands if required
3214 if (ISD::isSignedIntSetCC(Cond)) {
3215 C0 = N0C->getSignExtended();
3216 C1 = N1C->getSignExtended();
3217 }
3218
3219 switch (Cond) {
3220 default: assert(0 && "Unknown integer setcc!");
3221 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3222 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3223 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
3224 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
3225 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
3226 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
3227 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
3228 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
3229 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
3230 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
3231 }
3232 } else {
3233 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3234 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3235 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3236
3237 // If the comparison constant has bits in the upper part, the
3238 // zero-extended value could never match.
3239 if (C1 & (~0ULL << InSize)) {
3240 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3241 switch (Cond) {
3242 case ISD::SETUGT:
3243 case ISD::SETUGE:
3244 case ISD::SETEQ: return DAG.getConstant(0, VT);
3245 case ISD::SETULT:
3246 case ISD::SETULE:
3247 case ISD::SETNE: return DAG.getConstant(1, VT);
3248 case ISD::SETGT:
3249 case ISD::SETGE:
3250 // True if the sign bit of C1 is set.
3251 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3252 case ISD::SETLT:
3253 case ISD::SETLE:
3254 // True if the sign bit of C1 isn't set.
3255 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3256 default:
3257 break;
3258 }
3259 }
3260
3261 // Otherwise, we can perform the comparison with the low bits.
3262 switch (Cond) {
3263 case ISD::SETEQ:
3264 case ISD::SETNE:
3265 case ISD::SETUGT:
3266 case ISD::SETUGE:
3267 case ISD::SETULT:
3268 case ISD::SETULE:
3269 return DAG.getSetCC(VT, N0.getOperand(0),
3270 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3271 Cond);
3272 default:
3273 break; // todo, be more careful with signed comparisons
3274 }
3275 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3276 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3277 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3278 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3279 MVT::ValueType ExtDstTy = N0.getValueType();
3280 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3281
3282 // If the extended part has any inconsistent bits, it cannot ever
3283 // compare equal. In other words, they have to be all ones or all
3284 // zeros.
3285 uint64_t ExtBits =
3286 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3287 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3288 return DAG.getConstant(Cond == ISD::SETNE, VT);
3289
3290 SDOperand ZextOp;
3291 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3292 if (Op0Ty == ExtSrcTy) {
3293 ZextOp = N0.getOperand(0);
3294 } else {
3295 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3296 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3297 DAG.getConstant(Imm, Op0Ty));
3298 }
Chris Lattner5750df92006-03-01 04:03:14 +00003299 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003300 // Otherwise, make this a use of a zext.
3301 return DAG.getSetCC(VT, ZextOp,
3302 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3303 ExtDstTy),
3304 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00003305 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
3306 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3307 (N0.getOpcode() == ISD::XOR ||
3308 (N0.getOpcode() == ISD::AND &&
3309 N0.getOperand(0).getOpcode() == ISD::XOR &&
3310 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3311 isa<ConstantSDNode>(N0.getOperand(1)) &&
3312 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
3313 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
3314 // only do this if the top bits are known zero.
3315 if (TLI.MaskedValueIsZero(N1,
3316 MVT::getIntVTBitMask(N0.getValueType())-1)) {
3317 // Okay, get the un-inverted input value.
3318 SDOperand Val;
3319 if (N0.getOpcode() == ISD::XOR)
3320 Val = N0.getOperand(0);
3321 else {
3322 assert(N0.getOpcode() == ISD::AND &&
3323 N0.getOperand(0).getOpcode() == ISD::XOR);
3324 // ((X^1)&1)^1 -> X & 1
3325 Val = DAG.getNode(ISD::AND, N0.getValueType(),
3326 N0.getOperand(0).getOperand(0), N0.getOperand(1));
3327 }
3328 return DAG.getSetCC(VT, Val, N1,
3329 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
3330 }
Nate Begeman452d7be2005-09-16 00:54:12 +00003331 }
Chris Lattner5c46f742005-10-05 06:11:08 +00003332
Nate Begeman452d7be2005-09-16 00:54:12 +00003333 uint64_t MinVal, MaxVal;
3334 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
3335 if (ISD::isSignedIntSetCC(Cond)) {
3336 MinVal = 1ULL << (OperandBitSize-1);
3337 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
3338 MaxVal = ~0ULL >> (65-OperandBitSize);
3339 else
3340 MaxVal = 0;
3341 } else {
3342 MinVal = 0;
3343 MaxVal = ~0ULL >> (64-OperandBitSize);
3344 }
3345
3346 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
3347 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
3348 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
3349 --C1; // X >= C0 --> X > (C0-1)
3350 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3351 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
3352 }
3353
3354 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
3355 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
3356 ++C1; // X <= C0 --> X < (C0+1)
3357 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3358 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
3359 }
3360
3361 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
3362 return DAG.getConstant(0, VT); // X < MIN --> false
3363
3364 // Canonicalize setgt X, Min --> setne X, Min
3365 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
3366 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00003367 // Canonicalize setlt X, Max --> setne X, Max
3368 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
3369 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00003370
3371 // If we have setult X, 1, turn it into seteq X, 0
3372 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
3373 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
3374 ISD::SETEQ);
3375 // If we have setugt X, Max-1, turn it into seteq X, Max
3376 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
3377 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
3378 ISD::SETEQ);
3379
3380 // If we have "setcc X, C0", check to see if we can shrink the immediate
3381 // by changing cc.
3382
3383 // SETUGT X, SINTMAX -> SETLT X, 0
3384 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
3385 C1 == (~0ULL >> (65-OperandBitSize)))
3386 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
3387 ISD::SETLT);
3388
3389 // FIXME: Implement the rest of these.
3390
3391 // Fold bit comparisons when we can.
3392 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3393 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
3394 if (ConstantSDNode *AndRHS =
3395 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3396 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
3397 // Perform the xform if the AND RHS is a single bit.
3398 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
3399 return DAG.getNode(ISD::SRL, VT, N0,
3400 DAG.getConstant(Log2_64(AndRHS->getValue()),
3401 TLI.getShiftAmountTy()));
3402 }
3403 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
3404 // (X & 8) == 8 --> (X & 8) >> 3
3405 // Perform the xform if C1 is a single bit.
3406 if ((C1 & (C1-1)) == 0) {
3407 return DAG.getNode(ISD::SRL, VT, N0,
Chris Lattner729c6d12006-05-27 00:43:02 +00003408 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
Nate Begeman452d7be2005-09-16 00:54:12 +00003409 }
3410 }
3411 }
3412 }
3413 } else if (isa<ConstantSDNode>(N0.Val)) {
3414 // Ensure that the constant occurs on the RHS.
3415 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3416 }
3417
3418 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
3419 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
3420 double C0 = N0C->getValue(), C1 = N1C->getValue();
3421
3422 switch (Cond) {
3423 default: break; // FIXME: Implement the rest of these!
3424 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3425 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3426 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
3427 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
3428 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
3429 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
3430 }
3431 } else {
3432 // Ensure that the constant occurs on the RHS.
3433 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3434 }
3435
3436 if (N0 == N1) {
3437 // We can always fold X == Y for integer setcc's.
3438 if (MVT::isInteger(N0.getValueType()))
3439 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3440 unsigned UOF = ISD::getUnorderedFlavor(Cond);
3441 if (UOF == 2) // FP operators that are undefined on NaNs.
3442 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3443 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
3444 return DAG.getConstant(UOF, VT);
3445 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
3446 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00003447 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00003448 if (NewCond != Cond)
3449 return DAG.getSetCC(VT, N0, N1, NewCond);
3450 }
3451
3452 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3453 MVT::isInteger(N0.getValueType())) {
3454 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
3455 N0.getOpcode() == ISD::XOR) {
3456 // Simplify (X+Y) == (X+Z) --> Y == Z
3457 if (N0.getOpcode() == N1.getOpcode()) {
3458 if (N0.getOperand(0) == N1.getOperand(0))
3459 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
3460 if (N0.getOperand(1) == N1.getOperand(1))
3461 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
3462 if (isCommutativeBinOp(N0.getOpcode())) {
3463 // If X op Y == Y op X, try other combinations.
3464 if (N0.getOperand(0) == N1.getOperand(1))
3465 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
3466 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00003467 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00003468 }
3469 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003470
3471 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
3472 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3473 // Turn (X+C1) == C2 --> X == C2-C1
3474 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
3475 return DAG.getSetCC(VT, N0.getOperand(0),
3476 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
3477 N0.getValueType()), Cond);
3478 }
3479
3480 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
3481 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00003482 // If we know that all of the inverted bits are zero, don't bother
3483 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003484 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00003485 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003486 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00003487 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003488 }
3489
3490 // Turn (C1-X) == C2 --> X == C1-C2
3491 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
3492 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
3493 return DAG.getSetCC(VT, N0.getOperand(1),
3494 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
3495 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00003496 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003497 }
3498 }
3499
Nate Begeman452d7be2005-09-16 00:54:12 +00003500 // Simplify (X+Z) == X --> Z == 0
3501 if (N0.getOperand(0) == N1)
3502 return DAG.getSetCC(VT, N0.getOperand(1),
3503 DAG.getConstant(0, N0.getValueType()), Cond);
3504 if (N0.getOperand(1) == N1) {
3505 if (isCommutativeBinOp(N0.getOpcode()))
3506 return DAG.getSetCC(VT, N0.getOperand(0),
3507 DAG.getConstant(0, N0.getValueType()), Cond);
3508 else {
3509 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
3510 // (Z-X) == X --> Z == X<<1
3511 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
3512 N1,
3513 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003514 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003515 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
3516 }
3517 }
3518 }
3519
3520 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3521 N1.getOpcode() == ISD::XOR) {
3522 // Simplify X == (X+Z) --> Z == 0
3523 if (N1.getOperand(0) == N0) {
3524 return DAG.getSetCC(VT, N1.getOperand(1),
3525 DAG.getConstant(0, N1.getValueType()), Cond);
3526 } else if (N1.getOperand(1) == N0) {
3527 if (isCommutativeBinOp(N1.getOpcode())) {
3528 return DAG.getSetCC(VT, N1.getOperand(0),
3529 DAG.getConstant(0, N1.getValueType()), Cond);
3530 } else {
3531 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3532 // X == (Z-X) --> X<<1 == Z
3533 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
3534 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003535 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003536 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
3537 }
3538 }
3539 }
3540 }
3541
3542 // Fold away ALL boolean setcc's.
3543 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00003544 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003545 switch (Cond) {
3546 default: assert(0 && "Unknown integer setcc!");
3547 case ISD::SETEQ: // X == Y -> (X^Y)^1
3548 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3549 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00003550 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003551 break;
3552 case ISD::SETNE: // X != Y --> (X^Y)
3553 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3554 break;
3555 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
3556 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
3557 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3558 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003559 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003560 break;
3561 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
3562 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
3563 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3564 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003565 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003566 break;
3567 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
3568 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
3569 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3570 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003571 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003572 break;
3573 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
3574 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
3575 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3576 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
3577 break;
3578 }
3579 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00003580 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003581 // FIXME: If running after legalize, we probably can't do this.
3582 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3583 }
3584 return N0;
3585 }
3586
3587 // Could not fold it.
3588 return SDOperand();
3589}
3590
Nate Begeman69575232005-10-20 02:15:44 +00003591/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3592/// return a DAG expression to select that will generate the same value by
3593/// multiplying by a magic number. See:
3594/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3595SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003596 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003597 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
3598
Andrew Lenharth232c9102006-06-12 16:07:18 +00003599 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003600 ii != ee; ++ii)
3601 AddToWorkList(*ii);
3602 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003603}
3604
3605/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3606/// return a DAG expression to select that will generate the same value by
3607/// multiplying by a magic number. See:
3608/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3609SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003610 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003611 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00003612
Andrew Lenharth232c9102006-06-12 16:07:18 +00003613 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003614 ii != ee; ++ii)
3615 AddToWorkList(*ii);
3616 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003617}
3618
Nate Begeman1d4d4142005-09-01 00:19:25 +00003619// SelectionDAG::Combine - This is the entry point for the file.
3620//
Nate Begeman4ebd8052005-09-01 23:24:04 +00003621void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003622 /// run - This is the main entry point to this class.
3623 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00003624 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003625}