blob: e098bd98439d2cbbdedbaa437314df9d52698f58 [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 Lattnera4f0b3a2006-08-27 12:54:02 +000038#include "llvm/Support/Compiler.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 Lattner3577e382006-08-11 17:56:38 +000078 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo) {
79 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
Chris Lattner87514ca2005-10-10 22:31:19 +000080 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000081 DEBUG(std::cerr << "\nReplacing "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +000082 std::cerr << "\nWith: "; To[0].Val->dump(&DAG);
Chris Lattner3577e382006-08-11 17:56:38 +000083 std::cerr << " and " << NumTo-1 << " other values\n");
Chris Lattner01a22022005-10-10 22:04:48 +000084 std::vector<SDNode*> NowDead;
Chris Lattner3577e382006-08-11 17:56:38 +000085 DAG.ReplaceAllUsesWith(N, To, &NowDead);
Chris Lattner01a22022005-10-10 22:04:48 +000086
87 // Push the new nodes and any users onto the worklist
Chris Lattner3577e382006-08-11 17:56:38 +000088 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattner01a22022005-10-10 22:04:48 +000089 WorkList.push_back(To[i].Val);
90 AddUsersToWorkList(To[i].Val);
91 }
92
93 // Nodes can end up on the worklist more than once. Make sure we do
94 // not process a node that has been replaced.
95 removeFromWorkList(N);
96 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
97 removeFromWorkList(NowDead[i]);
98
99 // Finally, since the node is now dead, remove it from the graph.
100 DAG.DeleteNode(N);
101 return SDOperand(N, 0);
102 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000103
Chris Lattner24664722006-03-01 04:53:38 +0000104 SDOperand CombineTo(SDNode *N, SDOperand Res) {
Chris Lattner3577e382006-08-11 17:56:38 +0000105 return CombineTo(N, &Res, 1);
Chris Lattner24664722006-03-01 04:53:38 +0000106 }
107
108 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
Chris Lattner3577e382006-08-11 17:56:38 +0000109 SDOperand To[] = { Res0, Res1 };
110 return CombineTo(N, To, 2);
Chris Lattner24664722006-03-01 04:53:38 +0000111 }
112 private:
113
Chris Lattner012f2412006-02-17 21:58:01 +0000114 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000115 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000116 /// propagation. If so, return true.
117 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000118 TargetLowering::TargetLoweringOpt TLO(DAG);
119 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000120 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
121 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
122 return false;
123
124 // Revisit the node.
125 WorkList.push_back(Op.Val);
126
127 // Replace the old value with the new one.
128 ++NodesCombined;
129 DEBUG(std::cerr << "\nReplacing "; TLO.Old.Val->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000130 std::cerr << "\nWith: "; TLO.New.Val->dump(&DAG));
Chris Lattner012f2412006-02-17 21:58:01 +0000131
132 std::vector<SDNode*> NowDead;
133 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
134
Chris Lattner7d20d392006-02-20 06:51:04 +0000135 // Push the new node and any (possibly new) users onto the worklist.
Chris Lattner012f2412006-02-17 21:58:01 +0000136 WorkList.push_back(TLO.New.Val);
137 AddUsersToWorkList(TLO.New.Val);
138
139 // Nodes can end up on the worklist more than once. Make sure we do
140 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000141 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
142 removeFromWorkList(NowDead[i]);
143
Chris Lattner7d20d392006-02-20 06:51:04 +0000144 // Finally, if the node is now dead, remove it from the graph. The node
145 // may not be dead if the replacement process recursively simplified to
146 // something else needing this node.
147 if (TLO.Old.Val->use_empty()) {
148 removeFromWorkList(TLO.Old.Val);
149 DAG.DeleteNode(TLO.Old.Val);
150 }
Chris Lattner012f2412006-02-17 21:58:01 +0000151 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000152 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000153
Nate Begeman1d4d4142005-09-01 00:19:25 +0000154 /// visit - call the node-specific routine that knows how to fold each
155 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000156 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000157
158 // Visitation implementation - Implement dag node combining for different
159 // node types. The semantics are as follows:
160 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000161 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000162 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000163 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000164 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000165 SDOperand visitTokenFactor(SDNode *N);
166 SDOperand visitADD(SDNode *N);
167 SDOperand visitSUB(SDNode *N);
168 SDOperand visitMUL(SDNode *N);
169 SDOperand visitSDIV(SDNode *N);
170 SDOperand visitUDIV(SDNode *N);
171 SDOperand visitSREM(SDNode *N);
172 SDOperand visitUREM(SDNode *N);
173 SDOperand visitMULHU(SDNode *N);
174 SDOperand visitMULHS(SDNode *N);
175 SDOperand visitAND(SDNode *N);
176 SDOperand visitOR(SDNode *N);
177 SDOperand visitXOR(SDNode *N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000178 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000179 SDOperand visitSHL(SDNode *N);
180 SDOperand visitSRA(SDNode *N);
181 SDOperand visitSRL(SDNode *N);
182 SDOperand visitCTLZ(SDNode *N);
183 SDOperand visitCTTZ(SDNode *N);
184 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000185 SDOperand visitSELECT(SDNode *N);
186 SDOperand visitSELECT_CC(SDNode *N);
187 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000188 SDOperand visitSIGN_EXTEND(SDNode *N);
189 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000190 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000191 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
192 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000193 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000194 SDOperand visitVBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000195 SDOperand visitFADD(SDNode *N);
196 SDOperand visitFSUB(SDNode *N);
197 SDOperand visitFMUL(SDNode *N);
198 SDOperand visitFDIV(SDNode *N);
199 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000200 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000201 SDOperand visitSINT_TO_FP(SDNode *N);
202 SDOperand visitUINT_TO_FP(SDNode *N);
203 SDOperand visitFP_TO_SINT(SDNode *N);
204 SDOperand visitFP_TO_UINT(SDNode *N);
205 SDOperand visitFP_ROUND(SDNode *N);
206 SDOperand visitFP_ROUND_INREG(SDNode *N);
207 SDOperand visitFP_EXTEND(SDNode *N);
208 SDOperand visitFNEG(SDNode *N);
209 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000210 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000211 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000212 SDOperand visitLOAD(SDNode *N);
Chris Lattner29cd7db2006-03-31 18:10:41 +0000213 SDOperand visitXEXTLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000214 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000215 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
216 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000217 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000218 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000219 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000220
Evan Cheng44f1f092006-04-20 08:56:16 +0000221 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000222 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
223
Chris Lattner40c62d52005-10-18 06:04:22 +0000224 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000225 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000226 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
227 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
228 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000229 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000230 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000231 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000232 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000233 SDOperand BuildUDIV(SDNode *N);
234 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000235public:
236 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000237 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000238
239 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000240 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000241 };
242}
243
Chris Lattner24664722006-03-01 04:53:38 +0000244//===----------------------------------------------------------------------===//
245// TargetLowering::DAGCombinerInfo implementation
246//===----------------------------------------------------------------------===//
247
248void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
249 ((DAGCombiner*)DC)->AddToWorkList(N);
250}
251
252SDOperand TargetLowering::DAGCombinerInfo::
253CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000254 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000255}
256
257SDOperand TargetLowering::DAGCombinerInfo::
258CombineTo(SDNode *N, SDOperand Res) {
259 return ((DAGCombiner*)DC)->CombineTo(N, Res);
260}
261
262
263SDOperand TargetLowering::DAGCombinerInfo::
264CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
265 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
266}
267
268
269
270
271//===----------------------------------------------------------------------===//
272
273
Nate Begeman4ebd8052005-09-01 23:24:04 +0000274// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
275// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000276// Also, set the incoming LHS, RHS, and CC references to the appropriate
277// nodes based on the type of node we are checking. This simplifies life a
278// bit for the callers.
279static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
280 SDOperand &CC) {
281 if (N.getOpcode() == ISD::SETCC) {
282 LHS = N.getOperand(0);
283 RHS = N.getOperand(1);
284 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000285 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000286 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000287 if (N.getOpcode() == ISD::SELECT_CC &&
288 N.getOperand(2).getOpcode() == ISD::Constant &&
289 N.getOperand(3).getOpcode() == ISD::Constant &&
290 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000291 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
292 LHS = N.getOperand(0);
293 RHS = N.getOperand(1);
294 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000295 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000296 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000297 return false;
298}
299
Nate Begeman99801192005-09-07 23:25:52 +0000300// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
301// one use. If this is true, it allows the users to invert the operation for
302// free when it is profitable to do so.
303static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000304 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000305 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000306 return true;
307 return false;
308}
309
Nate Begemancd4d58c2006-02-03 06:46:56 +0000310SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
311 MVT::ValueType VT = N0.getValueType();
312 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
313 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
314 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
315 if (isa<ConstantSDNode>(N1)) {
316 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000317 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000318 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
319 } else if (N0.hasOneUse()) {
320 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000321 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000322 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
323 }
324 }
325 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
326 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
327 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
328 if (isa<ConstantSDNode>(N0)) {
329 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000330 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000331 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
332 } else if (N1.hasOneUse()) {
333 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000334 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000335 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
336 }
337 }
338 return SDOperand();
339}
340
Nate Begeman4ebd8052005-09-01 23:24:04 +0000341void DAGCombiner::Run(bool RunningAfterLegalize) {
342 // set the instance variable, so that the various visit routines may use it.
343 AfterLegalize = RunningAfterLegalize;
344
Nate Begeman646d7e22005-09-02 21:18:40 +0000345 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000346 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
347 E = DAG.allnodes_end(); I != E; ++I)
348 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000349
Chris Lattner95038592005-10-05 06:35:28 +0000350 // Create a dummy node (which is not added to allnodes), that adds a reference
351 // to the root node, preventing it from being deleted, and tracking any
352 // changes of the root.
353 HandleSDNode Dummy(DAG.getRoot());
354
Chris Lattner24664722006-03-01 04:53:38 +0000355
356 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
357 TargetLowering::DAGCombinerInfo
358 DagCombineInfo(DAG, !RunningAfterLegalize, this);
359
Nate Begeman1d4d4142005-09-01 00:19:25 +0000360 // while the worklist isn't empty, inspect the node on the end of it and
361 // try and combine it.
362 while (!WorkList.empty()) {
363 SDNode *N = WorkList.back();
364 WorkList.pop_back();
365
366 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000367 // N is deleted from the DAG, since they too may now be dead or may have a
368 // reduced number of uses, allowing other xforms.
369 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000370 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
371 WorkList.push_back(N->getOperand(i).Val);
372
Nate Begeman1d4d4142005-09-01 00:19:25 +0000373 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000374 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000375 continue;
376 }
377
Nate Begeman83e75ec2005-09-06 04:43:02 +0000378 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000379
380 // If nothing happened, try a target-specific DAG combine.
381 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000382 assert(N->getOpcode() != ISD::DELETED_NODE &&
383 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000384 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
385 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
386 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
387 }
388
Nate Begeman83e75ec2005-09-06 04:43:02 +0000389 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000390 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000391 // If we get back the same node we passed in, rather than a new node or
392 // zero, we know that the node must have defined multiple values and
393 // CombineTo was used. Since CombineTo takes care of the worklist
394 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000395 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000396 assert(N->getOpcode() != ISD::DELETED_NODE &&
397 RV.Val->getOpcode() != ISD::DELETED_NODE &&
398 "Node was deleted but visit returned new node!");
399
Nate Begeman2300f552005-09-07 00:15:36 +0000400 DEBUG(std::cerr << "\nReplacing "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000401 std::cerr << "\nWith: "; RV.Val->dump(&DAG);
Nate Begeman2300f552005-09-07 00:15:36 +0000402 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000403 std::vector<SDNode*> NowDead;
Chris Lattnerb9ea4a32006-08-11 17:46:28 +0000404 SDOperand OpV = RV;
405 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000406
407 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000408 WorkList.push_back(RV.Val);
409 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000410
411 // Nodes can end up on the worklist more than once. Make sure we do
412 // not process a node that has been replaced.
413 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000414 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
415 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000416
417 // Finally, since the node is now dead, remove it from the graph.
418 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000419 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000420 }
421 }
Chris Lattner95038592005-10-05 06:35:28 +0000422
423 // If the root changed (e.g. it was a dead load, update the root).
424 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000425}
426
Nate Begeman83e75ec2005-09-06 04:43:02 +0000427SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000428 switch(N->getOpcode()) {
429 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000430 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000431 case ISD::ADD: return visitADD(N);
432 case ISD::SUB: return visitSUB(N);
433 case ISD::MUL: return visitMUL(N);
434 case ISD::SDIV: return visitSDIV(N);
435 case ISD::UDIV: return visitUDIV(N);
436 case ISD::SREM: return visitSREM(N);
437 case ISD::UREM: return visitUREM(N);
438 case ISD::MULHU: return visitMULHU(N);
439 case ISD::MULHS: return visitMULHS(N);
440 case ISD::AND: return visitAND(N);
441 case ISD::OR: return visitOR(N);
442 case ISD::XOR: return visitXOR(N);
443 case ISD::SHL: return visitSHL(N);
444 case ISD::SRA: return visitSRA(N);
445 case ISD::SRL: return visitSRL(N);
446 case ISD::CTLZ: return visitCTLZ(N);
447 case ISD::CTTZ: return visitCTTZ(N);
448 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000449 case ISD::SELECT: return visitSELECT(N);
450 case ISD::SELECT_CC: return visitSELECT_CC(N);
451 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000452 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
453 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000454 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000455 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
456 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000457 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000458 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000459 case ISD::FADD: return visitFADD(N);
460 case ISD::FSUB: return visitFSUB(N);
461 case ISD::FMUL: return visitFMUL(N);
462 case ISD::FDIV: return visitFDIV(N);
463 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000464 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000465 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
466 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
467 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
468 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
469 case ISD::FP_ROUND: return visitFP_ROUND(N);
470 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
471 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
472 case ISD::FNEG: return visitFNEG(N);
473 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000474 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000475 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000476 case ISD::LOAD: return visitLOAD(N);
Chris Lattner29cd7db2006-03-31 18:10:41 +0000477 case ISD::EXTLOAD:
478 case ISD::SEXTLOAD:
479 case ISD::ZEXTLOAD: return visitXEXTLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000480 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000481 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
482 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000483 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000484 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000485 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000486 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
487 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
488 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
489 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
490 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
491 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
492 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
493 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000494 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000495 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000496}
497
Nate Begeman83e75ec2005-09-06 04:43:02 +0000498SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000499 SmallVector<SDOperand, 8> Ops;
Nate Begemanded49632005-10-13 03:11:28 +0000500 bool Changed = false;
501
Nate Begeman1d4d4142005-09-01 00:19:25 +0000502 // If the token factor has two operands and one is the entry token, replace
503 // the token factor with the other operand.
504 if (N->getNumOperands() == 2) {
Chris Lattner21a57dc2006-05-12 05:01:37 +0000505 if (N->getOperand(0).getOpcode() == ISD::EntryToken ||
506 N->getOperand(0) == N->getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000507 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000508 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000509 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000510 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000511
Nate Begemanded49632005-10-13 03:11:28 +0000512 // fold (tokenfactor (tokenfactor)) -> tokenfactor
513 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
514 SDOperand Op = N->getOperand(i);
515 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
Chris Lattnerac0f8f22006-03-13 18:37:30 +0000516 AddToWorkList(Op.Val); // Remove dead node.
Nate Begemanded49632005-10-13 03:11:28 +0000517 Changed = true;
518 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
519 Ops.push_back(Op.getOperand(j));
Chris Lattner21a57dc2006-05-12 05:01:37 +0000520 } else if (i == 0 || N->getOperand(i) != N->getOperand(i-1)) {
Nate Begemanded49632005-10-13 03:11:28 +0000521 Ops.push_back(Op);
Chris Lattner21a57dc2006-05-12 05:01:37 +0000522 } else {
523 // Deleted an operand that was the same as the last one.
524 Changed = true;
Nate Begemanded49632005-10-13 03:11:28 +0000525 }
526 }
527 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000528 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begeman83e75ec2005-09-06 04:43:02 +0000529 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000530}
531
Nate Begeman83e75ec2005-09-06 04:43:02 +0000532SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000533 SDOperand N0 = N->getOperand(0);
534 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000535 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
536 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000537 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000538
539 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000540 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000541 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000542 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000543 if (N0C && !N1C)
544 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000545 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000546 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000547 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000548 // fold ((c1-A)+c2) -> (c1+c2)-A
549 if (N1C && N0.getOpcode() == ISD::SUB)
550 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
551 return DAG.getNode(ISD::SUB, VT,
552 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
553 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000554 // reassociate add
555 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
556 if (RADD.Val != 0)
557 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000558 // fold ((0-A) + B) -> B-A
559 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
560 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000561 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000562 // fold (A + (0-B)) -> A-B
563 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
564 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000565 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000566 // fold (A+(B-A)) -> B
567 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000568 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000569
Evan Cheng860771d2006-03-01 01:09:54 +0000570 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000571 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000572
573 // fold (a+b) -> (a|b) iff a and b share no bits.
574 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
575 uint64_t LHSZero, LHSOne;
576 uint64_t RHSZero, RHSOne;
577 uint64_t Mask = MVT::getIntVTBitMask(VT);
578 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
579 if (LHSZero) {
580 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
581
582 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
583 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
584 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
585 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
586 return DAG.getNode(ISD::OR, VT, N0, N1);
587 }
588 }
589
Nate Begeman83e75ec2005-09-06 04:43:02 +0000590 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000591}
592
Nate Begeman83e75ec2005-09-06 04:43:02 +0000593SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000594 SDOperand N0 = N->getOperand(0);
595 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000596 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
597 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000598 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000599
Chris Lattner854077d2005-10-17 01:07:11 +0000600 // fold (sub x, x) -> 0
601 if (N0 == N1)
602 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000603 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000604 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000605 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000606 // fold (sub x, c) -> (add x, -c)
607 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000608 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000609 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000610 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000611 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000612 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000613 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000614 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000615 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000616}
617
Nate Begeman83e75ec2005-09-06 04:43:02 +0000618SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000619 SDOperand N0 = N->getOperand(0);
620 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000621 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
622 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000623 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000624
625 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000626 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000627 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000628 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000629 if (N0C && !N1C)
630 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000631 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000632 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000633 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000634 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000635 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000636 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000637 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000638 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000639 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000640 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000641 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000642 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
643 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
644 // FIXME: If the input is something that is easily negated (e.g. a
645 // single-use add), we should put the negate there.
646 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
647 DAG.getNode(ISD::SHL, VT, N0,
648 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
649 TLI.getShiftAmountTy())));
650 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +0000651
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000652 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
653 if (N1C && N0.getOpcode() == ISD::SHL &&
654 isa<ConstantSDNode>(N0.getOperand(1))) {
655 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000656 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000657 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
658 }
659
660 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
661 // use.
662 {
663 SDOperand Sh(0,0), Y(0,0);
664 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
665 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
666 N0.Val->hasOneUse()) {
667 Sh = N0; Y = N1;
668 } else if (N1.getOpcode() == ISD::SHL &&
669 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
670 Sh = N1; Y = N0;
671 }
672 if (Sh.Val) {
673 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
674 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
675 }
676 }
Chris Lattnera1deca32006-03-04 23:33:26 +0000677 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
678 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
679 isa<ConstantSDNode>(N0.getOperand(1))) {
680 return DAG.getNode(ISD::ADD, VT,
681 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
682 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
683 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000684
Nate Begemancd4d58c2006-02-03 06:46:56 +0000685 // reassociate mul
686 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
687 if (RMUL.Val != 0)
688 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000689 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000690}
691
Nate Begeman83e75ec2005-09-06 04:43:02 +0000692SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000693 SDOperand N0 = N->getOperand(0);
694 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000695 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
696 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000697 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000698
699 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000700 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000701 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000702 // fold (sdiv X, 1) -> X
703 if (N1C && N1C->getSignExtended() == 1LL)
704 return N0;
705 // fold (sdiv X, -1) -> 0-X
706 if (N1C && N1C->isAllOnesValue())
707 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000708 // If we know the sign bits of both operands are zero, strength reduce to a
709 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
710 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000711 if (TLI.MaskedValueIsZero(N1, SignBit) &&
712 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000713 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000714 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000715 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000716 (isPowerOf2_64(N1C->getSignExtended()) ||
717 isPowerOf2_64(-N1C->getSignExtended()))) {
718 // If dividing by powers of two is cheap, then don't perform the following
719 // fold.
720 if (TLI.isPow2DivCheap())
721 return SDOperand();
722 int64_t pow2 = N1C->getSignExtended();
723 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000724 unsigned lg2 = Log2_64(abs2);
725 // Splat the sign bit into the register
726 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000727 DAG.getConstant(MVT::getSizeInBits(VT)-1,
728 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +0000729 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000730 // Add (N0 < 0) ? abs2 - 1 : 0;
731 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
732 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000733 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000734 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +0000735 AddToWorkList(SRL.Val);
736 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +0000737 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
738 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000739 // If we're dividing by a positive value, we're done. Otherwise, we must
740 // negate the result.
741 if (pow2 > 0)
742 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +0000743 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000744 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
745 }
Nate Begeman69575232005-10-20 02:15:44 +0000746 // if integer divide is expensive and we satisfy the requirements, emit an
747 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000748 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000749 !TLI.isIntDivCheap()) {
750 SDOperand Op = BuildSDIV(N);
751 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000752 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000753 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000754}
755
Nate Begeman83e75ec2005-09-06 04:43:02 +0000756SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000757 SDOperand N0 = N->getOperand(0);
758 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000759 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
760 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000761 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000762
763 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000764 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000765 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000766 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000767 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000768 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000769 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000770 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000771 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
772 if (N1.getOpcode() == ISD::SHL) {
773 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
774 if (isPowerOf2_64(SHC->getValue())) {
775 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000776 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
777 DAG.getConstant(Log2_64(SHC->getValue()),
778 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +0000779 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000780 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000781 }
782 }
783 }
Nate Begeman69575232005-10-20 02:15:44 +0000784 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000785 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
786 SDOperand Op = BuildUDIV(N);
787 if (Op.Val) return Op;
788 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000789 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000790}
791
Nate Begeman83e75ec2005-09-06 04:43:02 +0000792SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000793 SDOperand N0 = N->getOperand(0);
794 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000795 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
796 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000797 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000798
799 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000800 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000801 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000802 // If we know the sign bits of both operands are zero, strength reduce to a
803 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
804 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000805 if (TLI.MaskedValueIsZero(N1, SignBit) &&
806 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000807 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000808 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000809}
810
Nate Begeman83e75ec2005-09-06 04:43:02 +0000811SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000812 SDOperand N0 = N->getOperand(0);
813 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000814 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
815 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000816 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000817
818 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000819 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000820 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000821 // fold (urem x, pow2) -> (and x, pow2-1)
822 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000823 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000824 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
825 if (N1.getOpcode() == ISD::SHL) {
826 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
827 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000828 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +0000829 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000830 return DAG.getNode(ISD::AND, VT, N0, Add);
831 }
832 }
833 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000834 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000835}
836
Nate Begeman83e75ec2005-09-06 04:43:02 +0000837SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000838 SDOperand N0 = N->getOperand(0);
839 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000840 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000841
842 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000843 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000844 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000845 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000846 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000847 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
848 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000849 TLI.getShiftAmountTy()));
850 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000851}
852
Nate Begeman83e75ec2005-09-06 04:43:02 +0000853SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000854 SDOperand N0 = N->getOperand(0);
855 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000856 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000857
858 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000859 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000860 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000861 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000862 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000863 return DAG.getConstant(0, N0.getValueType());
864 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000865}
866
Chris Lattner35e5c142006-05-05 05:51:50 +0000867/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
868/// two operands of the same opcode, try to simplify it.
869SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
870 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
871 MVT::ValueType VT = N0.getValueType();
872 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
873
Chris Lattner540121f2006-05-05 06:31:05 +0000874 // For each of OP in AND/OR/XOR:
875 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
876 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
877 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +0000878 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +0000879 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +0000880 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000881 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
882 SDOperand ORNode = DAG.getNode(N->getOpcode(),
883 N0.getOperand(0).getValueType(),
884 N0.getOperand(0), N1.getOperand(0));
885 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +0000886 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +0000887 }
888
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000889 // For each of OP in SHL/SRL/SRA/AND...
890 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
891 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
892 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +0000893 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000894 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000895 N0.getOperand(1) == N1.getOperand(1)) {
896 SDOperand ORNode = DAG.getNode(N->getOpcode(),
897 N0.getOperand(0).getValueType(),
898 N0.getOperand(0), N1.getOperand(0));
899 AddToWorkList(ORNode.Val);
900 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
901 }
902
903 return SDOperand();
904}
905
Nate Begeman83e75ec2005-09-06 04:43:02 +0000906SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000907 SDOperand N0 = N->getOperand(0);
908 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +0000909 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000910 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
911 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000912 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000913 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000914
915 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000916 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000917 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000918 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000919 if (N0C && !N1C)
920 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000921 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000922 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000923 return N0;
924 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +0000925 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000926 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000927 // reassociate and
928 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
929 if (RAND.Val != 0)
930 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000931 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +0000932 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000933 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000934 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000935 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +0000936 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
937 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +0000938 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +0000939 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +0000940 ~N1C->getValue() & InMask)) {
941 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
942 N0.getOperand(0));
943
944 // Replace uses of the AND with uses of the Zero extend node.
945 CombineTo(N, Zext);
946
Chris Lattner3603cd62006-02-02 07:17:31 +0000947 // We actually want to replace all uses of the any_extend with the
948 // zero_extend, to avoid duplicating things. This will later cause this
949 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +0000950 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +0000951 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +0000952 }
953 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000954 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
955 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
956 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
957 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
958
959 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
960 MVT::isInteger(LL.getValueType())) {
961 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
962 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
963 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +0000964 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000965 return DAG.getSetCC(VT, ORNode, LR, Op1);
966 }
967 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
968 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
969 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +0000970 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000971 return DAG.getSetCC(VT, ANDNode, LR, Op1);
972 }
973 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
974 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
975 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +0000976 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000977 return DAG.getSetCC(VT, ORNode, LR, Op1);
978 }
979 }
980 // canonicalize equivalent to ll == rl
981 if (LL == RR && LR == RL) {
982 Op1 = ISD::getSetCCSwappedOperands(Op1);
983 std::swap(RL, RR);
984 }
985 if (LL == RL && LR == RR) {
986 bool isInteger = MVT::isInteger(LL.getValueType());
987 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
988 if (Result != ISD::SETCC_INVALID)
989 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
990 }
991 }
Chris Lattner35e5c142006-05-05 05:51:50 +0000992
993 // Simplify: and (op x...), (op y...) -> (op (and x, y))
994 if (N0.getOpcode() == N1.getOpcode()) {
995 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
996 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000997 }
Chris Lattner35e5c142006-05-05 05:51:50 +0000998
Nate Begemande996292006-02-03 22:24:05 +0000999 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1000 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001001 if (!MVT::isVector(VT) &&
1002 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001003 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001004 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +00001005 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +00001006 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001007 // If we zero all the possible extended bits, then we can turn this into
1008 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001009 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +00001010 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001011 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1012 N0.getOperand(1), N0.getOperand(2),
1013 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001014 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001015 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001016 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001017 }
1018 }
1019 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001020 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
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)) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +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 }
Chris Lattner15045b62006-02-28 06:35:35 +00001034
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001035 // fold (and (load x), 255) -> (zextload x, i8)
1036 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1037 if (N1C &&
1038 (N0.getOpcode() == ISD::LOAD || N0.getOpcode() == ISD::EXTLOAD ||
1039 N0.getOpcode() == ISD::ZEXTLOAD) &&
1040 N0.hasOneUse()) {
1041 MVT::ValueType EVT, LoadedVT;
Chris Lattner15045b62006-02-28 06:35:35 +00001042 if (N1C->getValue() == 255)
1043 EVT = MVT::i8;
1044 else if (N1C->getValue() == 65535)
1045 EVT = MVT::i16;
1046 else if (N1C->getValue() == ~0U)
1047 EVT = MVT::i32;
1048 else
1049 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001050
1051 LoadedVT = N0.getOpcode() == ISD::LOAD ? VT :
1052 cast<VTSDNode>(N0.getOperand(3))->getVT();
Chris Lattnere44be602006-04-04 17:39:18 +00001053 if (EVT != MVT::Other && LoadedVT > EVT &&
1054 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Chris Lattner15045b62006-02-28 06:35:35 +00001055 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1056 // For big endian targets, we need to add an offset to the pointer to load
1057 // the correct bytes. For little endian systems, we merely need to read
1058 // fewer bytes from the same pointer.
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001059 unsigned PtrOff =
1060 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1061 SDOperand NewPtr = N0.getOperand(1);
1062 if (!TLI.isLittleEndian())
1063 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1064 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001065 AddToWorkList(NewPtr.Val);
Chris Lattner15045b62006-02-28 06:35:35 +00001066 SDOperand Load =
1067 DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
1068 N0.getOperand(2), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001069 AddToWorkList(N);
Chris Lattner15045b62006-02-28 06:35:35 +00001070 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001071 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner15045b62006-02-28 06:35:35 +00001072 }
1073 }
1074
Nate Begeman83e75ec2005-09-06 04:43:02 +00001075 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001076}
1077
Nate Begeman83e75ec2005-09-06 04:43:02 +00001078SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001079 SDOperand N0 = N->getOperand(0);
1080 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001081 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001082 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1083 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001084 MVT::ValueType VT = N1.getValueType();
1085 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001086
1087 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001088 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001089 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001090 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001091 if (N0C && !N1C)
1092 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001093 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001094 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001095 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001096 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001097 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001098 return N1;
1099 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001100 if (N1C &&
1101 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001102 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001103 // reassociate or
1104 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1105 if (ROR.Val != 0)
1106 return ROR;
1107 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1108 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001109 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001110 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1111 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1112 N1),
1113 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001114 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001115 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1116 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1117 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1118 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1119
1120 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1121 MVT::isInteger(LL.getValueType())) {
1122 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1123 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1124 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1125 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1126 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001127 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001128 return DAG.getSetCC(VT, ORNode, LR, Op1);
1129 }
1130 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1131 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1132 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1133 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1134 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001135 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001136 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1137 }
1138 }
1139 // canonicalize equivalent to ll == rl
1140 if (LL == RR && LR == RL) {
1141 Op1 = ISD::getSetCCSwappedOperands(Op1);
1142 std::swap(RL, RR);
1143 }
1144 if (LL == RL && LR == RR) {
1145 bool isInteger = MVT::isInteger(LL.getValueType());
1146 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1147 if (Result != ISD::SETCC_INVALID)
1148 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1149 }
1150 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001151
1152 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1153 if (N0.getOpcode() == N1.getOpcode()) {
1154 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1155 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001156 }
Chris Lattner516b9622006-09-14 20:50:57 +00001157
Chris Lattner1ec72732006-09-14 21:11:37 +00001158 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1159 if (N0.getOpcode() == ISD::AND &&
1160 N1.getOpcode() == ISD::AND &&
1161 N0.getOperand(1).getOpcode() == ISD::Constant &&
1162 N1.getOperand(1).getOpcode() == ISD::Constant &&
1163 // Don't increase # computations.
1164 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1165 // We can only do this xform if we know that bits from X that are set in C2
1166 // but not in C1 are already zero. Likewise for Y.
1167 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1168 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1169
1170 if (TLI.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1171 TLI.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
1172 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1173 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1174 }
1175 }
1176
1177
Chris Lattner516b9622006-09-14 20:50:57 +00001178 // See if this is some rotate idiom.
1179 if (SDNode *Rot = MatchRotate(N0, N1))
1180 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001181
Nate Begeman83e75ec2005-09-06 04:43:02 +00001182 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001183}
1184
Chris Lattner516b9622006-09-14 20:50:57 +00001185
1186/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1187static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1188 if (Op.getOpcode() == ISD::AND) {
1189 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1190 Mask = Op.getOperand(1);
1191 Op = Op.getOperand(0);
1192 } else {
1193 return false;
1194 }
1195 }
1196
1197 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1198 Shift = Op;
1199 return true;
1200 }
1201 return false;
1202}
1203
1204
1205// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1206// idioms for rotate, and if the target supports rotation instructions, generate
1207// a rot[lr].
1208SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1209 // Must be a legal type. Expanded an promoted things won't work with rotates.
1210 MVT::ValueType VT = LHS.getValueType();
1211 if (!TLI.isTypeLegal(VT)) return 0;
1212
1213 // The target must have at least one rotate flavor.
1214 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1215 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1216 if (!HasROTL && !HasROTR) return 0;
1217
1218 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1219 SDOperand LHSShift; // The shift.
1220 SDOperand LHSMask; // AND value if any.
1221 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1222 return 0; // Not part of a rotate.
1223
1224 SDOperand RHSShift; // The shift.
1225 SDOperand RHSMask; // AND value if any.
1226 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1227 return 0; // Not part of a rotate.
1228
1229 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1230 return 0; // Not shifting the same value.
1231
1232 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1233 return 0; // Shifts must disagree.
1234
1235 // Canonicalize shl to left side in a shl/srl pair.
1236 if (RHSShift.getOpcode() == ISD::SHL) {
1237 std::swap(LHS, RHS);
1238 std::swap(LHSShift, RHSShift);
1239 std::swap(LHSMask , RHSMask );
1240 }
1241
1242 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1243
1244 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1245 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
1246 if (LHSShift.getOperand(1).getOpcode() == ISD::Constant &&
1247 RHSShift.getOperand(1).getOpcode() == ISD::Constant) {
1248 uint64_t LShVal = cast<ConstantSDNode>(LHSShift.getOperand(1))->getValue();
1249 uint64_t RShVal = cast<ConstantSDNode>(RHSShift.getOperand(1))->getValue();
1250 if ((LShVal + RShVal) != OpSizeInBits)
1251 return 0;
1252
1253 SDOperand Rot;
1254 if (HasROTL)
1255 Rot = DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1256 LHSShift.getOperand(1));
1257 else
1258 Rot = DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1259 RHSShift.getOperand(1));
1260
1261 // If there is an AND of either shifted operand, apply it to the result.
1262 if (LHSMask.Val || RHSMask.Val) {
1263 uint64_t Mask = MVT::getIntVTBitMask(VT);
1264
1265 if (LHSMask.Val) {
1266 uint64_t RHSBits = (1ULL << LShVal)-1;
1267 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1268 }
1269 if (RHSMask.Val) {
1270 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1271 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1272 }
1273
1274 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1275 }
1276
1277 return Rot.Val;
1278 }
1279
1280 // If there is a mask here, and we have a variable shift, we can't be sure
1281 // that we're masking out the right stuff.
1282 if (LHSMask.Val || RHSMask.Val)
1283 return 0;
1284
1285 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1286 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
1287 if (RHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1288 LHSShift.getOperand(1) == RHSShift.getOperand(1).getOperand(1)) {
1289 if (ConstantSDNode *SUBC =
1290 dyn_cast<ConstantSDNode>(RHSShift.getOperand(1).getOperand(0))) {
1291 if (SUBC->getValue() == OpSizeInBits)
1292 if (HasROTL)
1293 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1294 LHSShift.getOperand(1)).Val;
1295 else
1296 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1297 LHSShift.getOperand(1)).Val;
1298 }
1299 }
1300
1301 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1302 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
1303 if (LHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1304 RHSShift.getOperand(1) == LHSShift.getOperand(1).getOperand(1)) {
1305 if (ConstantSDNode *SUBC =
1306 dyn_cast<ConstantSDNode>(LHSShift.getOperand(1).getOperand(0))) {
1307 if (SUBC->getValue() == OpSizeInBits)
1308 if (HasROTL)
1309 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1310 LHSShift.getOperand(1)).Val;
1311 else
1312 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1313 RHSShift.getOperand(1)).Val;
1314 }
1315 }
1316
1317 return 0;
1318}
1319
1320
Nate Begeman83e75ec2005-09-06 04:43:02 +00001321SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001322 SDOperand N0 = N->getOperand(0);
1323 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001324 SDOperand LHS, RHS, CC;
1325 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1326 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001327 MVT::ValueType VT = N0.getValueType();
1328
1329 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001330 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001331 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001332 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001333 if (N0C && !N1C)
1334 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001335 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001336 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001337 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001338 // reassociate xor
1339 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1340 if (RXOR.Val != 0)
1341 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001342 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001343 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1344 bool isInt = MVT::isInteger(LHS.getValueType());
1345 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1346 isInt);
1347 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001348 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001349 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001350 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001351 assert(0 && "Unhandled SetCC Equivalent!");
1352 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001353 }
Nate Begeman99801192005-09-07 23:25:52 +00001354 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1355 if (N1C && N1C->getValue() == 1 &&
1356 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001357 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001358 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1359 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001360 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1361 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001362 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001363 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001364 }
1365 }
Nate Begeman99801192005-09-07 23:25:52 +00001366 // fold !(x or y) -> (!x and !y) iff x or y are constants
1367 if (N1C && N1C->isAllOnesValue() &&
1368 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001369 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001370 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1371 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001372 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1373 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001374 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001375 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001376 }
1377 }
Nate Begeman223df222005-09-08 20:18:10 +00001378 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1379 if (N1C && N0.getOpcode() == ISD::XOR) {
1380 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1381 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1382 if (N00C)
1383 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1384 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1385 if (N01C)
1386 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1387 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1388 }
1389 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001390 if (N0 == N1) {
1391 if (!MVT::isVector(VT)) {
1392 return DAG.getConstant(0, VT);
1393 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1394 // Produce a vector of zeros.
1395 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1396 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001397 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001398 }
1399 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001400
1401 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1402 if (N0.getOpcode() == N1.getOpcode()) {
1403 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1404 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001405 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001406
Chris Lattner3e104b12006-04-08 04:15:24 +00001407 // Simplify the expression using non-local knowledge.
1408 if (!MVT::isVector(VT) &&
1409 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001410 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001411
Nate Begeman83e75ec2005-09-06 04:43:02 +00001412 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001413}
1414
Nate Begeman83e75ec2005-09-06 04:43:02 +00001415SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001416 SDOperand N0 = N->getOperand(0);
1417 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001418 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1419 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001420 MVT::ValueType VT = N0.getValueType();
1421 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1422
1423 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001424 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001425 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001426 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001427 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001428 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001429 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001430 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001431 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001432 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001433 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001434 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001435 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001436 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001437 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001438 if (SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001439 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001440 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001441 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001442 N0.getOperand(1).getOpcode() == ISD::Constant) {
1443 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001444 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001445 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001446 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001447 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001448 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001449 }
1450 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1451 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001452 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001453 N0.getOperand(1).getOpcode() == ISD::Constant) {
1454 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001455 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001456 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1457 DAG.getConstant(~0ULL << c1, VT));
1458 if (c2 > c1)
1459 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001460 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001461 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001462 return DAG.getNode(ISD::SRL, VT, Mask,
1463 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001464 }
1465 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001466 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001467 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001468 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnercac70592006-03-05 19:53:55 +00001469 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1470 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1471 isa<ConstantSDNode>(N0.getOperand(1))) {
1472 return DAG.getNode(ISD::ADD, VT,
1473 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1474 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1475 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001476 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001477}
1478
Nate Begeman83e75ec2005-09-06 04:43:02 +00001479SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001480 SDOperand N0 = N->getOperand(0);
1481 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001482 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1483 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001484 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001485
1486 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001487 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001488 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001489 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001490 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001491 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001492 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001493 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001494 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001495 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001496 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001497 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001498 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001499 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001500 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001501 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1502 // sext_inreg.
1503 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1504 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1505 MVT::ValueType EVT;
1506 switch (LowBits) {
1507 default: EVT = MVT::Other; break;
1508 case 1: EVT = MVT::i1; break;
1509 case 8: EVT = MVT::i8; break;
1510 case 16: EVT = MVT::i16; break;
1511 case 32: EVT = MVT::i32; break;
1512 }
1513 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1514 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1515 DAG.getValueType(EVT));
1516 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001517
1518 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1519 if (N1C && N0.getOpcode() == ISD::SRA) {
1520 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1521 unsigned Sum = N1C->getValue() + C1->getValue();
1522 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1523 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1524 DAG.getConstant(Sum, N1C->getValueType(0)));
1525 }
1526 }
1527
Chris Lattnera8504462006-05-08 20:51:54 +00001528 // Simplify, based on bits shifted out of the LHS.
1529 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
1530 return SDOperand(N, 0);
1531
1532
Nate Begeman1d4d4142005-09-01 00:19:25 +00001533 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001534 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001535 return DAG.getNode(ISD::SRL, VT, N0, N1);
1536 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001537}
1538
Nate Begeman83e75ec2005-09-06 04:43:02 +00001539SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001540 SDOperand N0 = N->getOperand(0);
1541 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001542 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1543 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001544 MVT::ValueType VT = N0.getValueType();
1545 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1546
1547 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001548 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001549 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001550 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001551 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001552 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001553 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001554 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001555 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001556 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001557 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001558 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001559 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001560 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001561 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001562 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001563 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001564 N0.getOperand(1).getOpcode() == ISD::Constant) {
1565 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001566 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001567 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001568 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001569 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001570 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001571 }
Chris Lattner350bec02006-04-02 06:11:11 +00001572
Chris Lattner06afe072006-05-05 22:53:17 +00001573 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
1574 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1575 // Shifting in all undef bits?
1576 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
1577 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
1578 return DAG.getNode(ISD::UNDEF, VT);
1579
1580 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
1581 AddToWorkList(SmallShift.Val);
1582 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
1583 }
1584
Chris Lattner350bec02006-04-02 06:11:11 +00001585 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1586 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1587 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1588 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1589 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
1590
1591 // If any of the input bits are KnownOne, then the input couldn't be all
1592 // zeros, thus the result of the srl will always be zero.
1593 if (KnownOne) return DAG.getConstant(0, VT);
1594
1595 // If all of the bits input the to ctlz node are known to be zero, then
1596 // the result of the ctlz is "32" and the result of the shift is one.
1597 uint64_t UnknownBits = ~KnownZero & Mask;
1598 if (UnknownBits == 0) return DAG.getConstant(1, VT);
1599
1600 // Otherwise, check to see if there is exactly one bit input to the ctlz.
1601 if ((UnknownBits & (UnknownBits-1)) == 0) {
1602 // Okay, we know that only that the single bit specified by UnknownBits
1603 // could be set on input to the CTLZ node. If this bit is set, the SRL
1604 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
1605 // to an SRL,XOR pair, which is likely to simplify more.
1606 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
1607 SDOperand Op = N0.getOperand(0);
1608 if (ShAmt) {
1609 Op = DAG.getNode(ISD::SRL, VT, Op,
1610 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
1611 AddToWorkList(Op.Val);
1612 }
1613 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
1614 }
1615 }
1616
Nate Begeman83e75ec2005-09-06 04:43:02 +00001617 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001618}
1619
Nate Begeman83e75ec2005-09-06 04:43:02 +00001620SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001621 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001622 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001623
1624 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001625 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001626 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001627 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001628}
1629
Nate Begeman83e75ec2005-09-06 04:43:02 +00001630SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001631 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001632 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001633
1634 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001635 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001636 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001637 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001638}
1639
Nate Begeman83e75ec2005-09-06 04:43:02 +00001640SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001641 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001642 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001643
1644 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001645 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001646 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001647 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001648}
1649
Nate Begeman452d7be2005-09-16 00:54:12 +00001650SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1651 SDOperand N0 = N->getOperand(0);
1652 SDOperand N1 = N->getOperand(1);
1653 SDOperand N2 = N->getOperand(2);
1654 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1655 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1656 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1657 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001658
Nate Begeman452d7be2005-09-16 00:54:12 +00001659 // fold select C, X, X -> X
1660 if (N1 == N2)
1661 return N1;
1662 // fold select true, X, Y -> X
1663 if (N0C && !N0C->isNullValue())
1664 return N1;
1665 // fold select false, X, Y -> Y
1666 if (N0C && N0C->isNullValue())
1667 return N2;
1668 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001669 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001670 return DAG.getNode(ISD::OR, VT, N0, N2);
1671 // fold select C, 0, X -> ~C & X
1672 // FIXME: this should check for C type == X type, not i1?
1673 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1674 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001675 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001676 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1677 }
1678 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001679 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001680 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001681 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001682 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1683 }
1684 // fold select C, X, 0 -> C & X
1685 // FIXME: this should check for C type == X type, not i1?
1686 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1687 return DAG.getNode(ISD::AND, VT, N0, N1);
1688 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1689 if (MVT::i1 == VT && N0 == N1)
1690 return DAG.getNode(ISD::OR, VT, N0, N2);
1691 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1692 if (MVT::i1 == VT && N0 == N2)
1693 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00001694
Chris Lattner40c62d52005-10-18 06:04:22 +00001695 // If we can fold this based on the true/false value, do so.
1696 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00001697 return SDOperand(N, 0); // Don't revisit N.
1698
Nate Begeman44728a72005-09-19 22:34:01 +00001699 // fold selects based on a setcc into other things, such as min/max/abs
1700 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001701 // FIXME:
1702 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1703 // having to say they don't support SELECT_CC on every type the DAG knows
1704 // about, since there is no way to mark an opcode illegal at all value types
1705 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1706 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1707 N1, N2, N0.getOperand(2));
1708 else
1709 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001710 return SDOperand();
1711}
1712
1713SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001714 SDOperand N0 = N->getOperand(0);
1715 SDOperand N1 = N->getOperand(1);
1716 SDOperand N2 = N->getOperand(2);
1717 SDOperand N3 = N->getOperand(3);
1718 SDOperand N4 = N->getOperand(4);
1719 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1720 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1721 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1722 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1723
1724 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001725 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner5eed34d2006-05-12 17:57:54 +00001726 //ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
Chris Lattner91559022005-10-05 04:45:43 +00001727
Nate Begeman44728a72005-09-19 22:34:01 +00001728 // fold select_cc lhs, rhs, x, x, cc -> x
1729 if (N2 == N3)
1730 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001731
1732 // If we can fold this based on the true/false value, do so.
1733 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00001734 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00001735
Nate Begeman44728a72005-09-19 22:34:01 +00001736 // fold select_cc into other things, such as min/max/abs
1737 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001738}
1739
1740SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1741 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1742 cast<CondCodeSDNode>(N->getOperand(2))->get());
1743}
1744
Nate Begeman83e75ec2005-09-06 04:43:02 +00001745SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001746 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001747 MVT::ValueType VT = N->getValueType(0);
1748
Nate Begeman1d4d4142005-09-01 00:19:25 +00001749 // fold (sext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001750 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001751 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00001752
Nate Begeman1d4d4142005-09-01 00:19:25 +00001753 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001754 // fold (sext (aext x)) -> (sext x)
1755 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001756 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00001757
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001758 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
Chris Lattnercc2210b2005-12-07 18:02:05 +00001759 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1760 (!AfterLegalize ||
1761 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001762 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1763 DAG.getValueType(N0.getValueType()));
Chris Lattner310b5782006-05-06 23:06:26 +00001764
Evan Cheng110dec22005-12-14 02:19:23 +00001765 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001766 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1767 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001768 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1769 N0.getOperand(1), N0.getOperand(2),
1770 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001771 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001772 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1773 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001774 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00001775 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001776
1777 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1778 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1779 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1780 N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001781 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1782 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1783 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001784 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001785 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1786 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001787 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001788 }
1789
Nate Begeman83e75ec2005-09-06 04:43:02 +00001790 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001791}
1792
Nate Begeman83e75ec2005-09-06 04:43:02 +00001793SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001794 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001795 MVT::ValueType VT = N->getValueType(0);
1796
Nate Begeman1d4d4142005-09-01 00:19:25 +00001797 // fold (zext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001798 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001799 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001800 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001801 // fold (zext (aext x)) -> (zext x)
1802 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001803 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Evan Cheng110dec22005-12-14 02:19:23 +00001804 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1805 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001806 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
Chris Lattner00cb95c2005-12-14 07:58:38 +00001807 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
Evan Cheng110dec22005-12-14 02:19:23 +00001808 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001809 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1810 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001811 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1812 N0.getOperand(1), N0.getOperand(2),
1813 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001814 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001815 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1816 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001817 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00001818 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001819
1820 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1821 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1822 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1823 N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001824 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1825 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1826 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001827 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001828 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1829 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001830 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001831 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001832 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001833}
1834
Chris Lattner5ffc0662006-05-05 05:58:59 +00001835SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
1836 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001837 MVT::ValueType VT = N->getValueType(0);
1838
1839 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001840 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00001841 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
1842 // fold (aext (aext x)) -> (aext x)
1843 // fold (aext (zext x)) -> (zext x)
1844 // fold (aext (sext x)) -> (sext x)
1845 if (N0.getOpcode() == ISD::ANY_EXTEND ||
1846 N0.getOpcode() == ISD::ZERO_EXTEND ||
1847 N0.getOpcode() == ISD::SIGN_EXTEND)
1848 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
1849
1850 // fold (aext (truncate x)) -> x iff x size == zext size.
1851 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT)
1852 return N0.getOperand(0);
1853 // fold (aext (load x)) -> (aext (truncate (extload x)))
1854 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1855 (!AfterLegalize||TLI.isOperationLegal(ISD::EXTLOAD, N0.getValueType()))) {
1856 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0),
1857 N0.getOperand(1), N0.getOperand(2),
1858 N0.getValueType());
1859 CombineTo(N, ExtLoad);
1860 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1861 ExtLoad.getValue(1));
1862 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1863 }
1864
1865 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
1866 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
1867 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
1868 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD ||
1869 N0.getOpcode() == ISD::SEXTLOAD) &&
1870 N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001871 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1872 SDOperand ExtLoad = DAG.getExtLoad(N0.getOpcode(), VT, N0.getOperand(0),
1873 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001874 CombineTo(N, ExtLoad);
1875 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1876 ExtLoad.getValue(1));
1877 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1878 }
1879 return SDOperand();
1880}
1881
1882
Nate Begeman83e75ec2005-09-06 04:43:02 +00001883SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001884 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001885 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001886 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001887 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001888 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001889
Nate Begeman1d4d4142005-09-01 00:19:25 +00001890 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00001891 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00001892 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00001893
Chris Lattner541a24f2006-05-06 22:43:44 +00001894 // If the input is already sign extended, just drop the extension.
Chris Lattneree4ea922006-05-06 09:30:03 +00001895 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
1896 return N0;
1897
Nate Begeman646d7e22005-09-02 21:18:40 +00001898 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1899 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1900 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001901 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001902 }
Chris Lattner4b37e872006-05-08 21:18:59 +00001903
Nate Begeman07ed4172005-10-10 21:26:48 +00001904 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001905 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00001906 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00001907
1908 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
1909 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
1910 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
1911 if (N0.getOpcode() == ISD::SRL) {
1912 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
1913 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
1914 // We can turn this into an SRA iff the input to the SRL is already sign
1915 // extended enough.
1916 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
1917 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
1918 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
1919 }
1920 }
1921
Nate Begemanded49632005-10-13 03:11:28 +00001922 // fold (sext_inreg (extload x)) -> (sextload x)
1923 if (N0.getOpcode() == ISD::EXTLOAD &&
1924 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001925 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001926 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1927 N0.getOperand(1), N0.getOperand(2),
1928 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001929 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001930 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001931 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001932 }
1933 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001934 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00001935 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001936 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001937 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1938 N0.getOperand(1), N0.getOperand(2),
1939 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001940 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001941 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001942 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001943 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001944 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001945}
1946
Nate Begeman83e75ec2005-09-06 04:43:02 +00001947SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001948 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001949 MVT::ValueType VT = N->getValueType(0);
1950
1951 // noop truncate
1952 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001953 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001954 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001955 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001956 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001957 // fold (truncate (truncate x)) -> (truncate x)
1958 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001959 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001960 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00001961 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
1962 N0.getOpcode() == ISD::ANY_EXTEND) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001963 if (N0.getValueType() < VT)
1964 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001965 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001966 else if (N0.getValueType() > VT)
1967 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001968 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001969 else
1970 // if the source and dest are the same type, we can drop both the extend
1971 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001972 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001973 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001974 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00001975 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00001976 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1977 "Cannot truncate to larger type!");
1978 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001979 // For big endian targets, we need to add an offset to the pointer to load
1980 // the correct bytes. For little endian systems, we merely need to read
1981 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001982 uint64_t PtrOff =
1983 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001984 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1985 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1986 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001987 AddToWorkList(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001988 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00001989 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001990 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001991 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00001992 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001993 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001994}
1995
Chris Lattner94683772005-12-23 05:30:37 +00001996SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
1997 SDOperand N0 = N->getOperand(0);
1998 MVT::ValueType VT = N->getValueType(0);
1999
2000 // If the input is a constant, let getNode() fold it.
2001 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2002 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2003 if (Res.Val != N) return Res;
2004 }
2005
Chris Lattnerc8547d82005-12-23 05:37:50 +00002006 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2007 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00002008
Chris Lattner57104102005-12-23 05:44:41 +00002009 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002010 // FIXME: These xforms need to know that the resultant load doesn't need a
2011 // higher alignment than the original!
2012 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00002013 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
2014 N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00002015 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00002016 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2017 Load.getValue(1));
2018 return Load;
2019 }
2020
Chris Lattner94683772005-12-23 05:30:37 +00002021 return SDOperand();
2022}
2023
Chris Lattner6258fb22006-04-02 02:53:43 +00002024SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2025 SDOperand N0 = N->getOperand(0);
2026 MVT::ValueType VT = N->getValueType(0);
2027
2028 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2029 // First check to see if this is all constant.
2030 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2031 VT == MVT::Vector) {
2032 bool isSimple = true;
2033 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2034 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2035 N0.getOperand(i).getOpcode() != ISD::Constant &&
2036 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2037 isSimple = false;
2038 break;
2039 }
2040
Chris Lattner97c20732006-04-03 17:29:28 +00002041 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2042 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002043 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2044 }
2045 }
2046
2047 return SDOperand();
2048}
2049
2050/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2051/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2052/// destination element value type.
2053SDOperand DAGCombiner::
2054ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2055 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2056
2057 // If this is already the right type, we're done.
2058 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2059
2060 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2061 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2062
2063 // If this is a conversion of N elements of one type to N elements of another
2064 // type, convert each element. This handles FP<->INT cases.
2065 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002066 SmallVector<SDOperand, 8> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00002067 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002068 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002069 AddToWorkList(Ops.back().Val);
2070 }
Chris Lattner6258fb22006-04-02 02:53:43 +00002071 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2072 Ops.push_back(DAG.getValueType(DstEltVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002073 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002074 }
2075
2076 // Otherwise, we're growing or shrinking the elements. To avoid having to
2077 // handle annoying details of growing/shrinking FP values, we convert them to
2078 // int first.
2079 if (MVT::isFloatingPoint(SrcEltVT)) {
2080 // Convert the input float vector to a int vector where the elements are the
2081 // same sizes.
2082 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2083 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2084 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2085 SrcEltVT = IntVT;
2086 }
2087
2088 // Now we know the input is an integer vector. If the output is a FP type,
2089 // convert to integer first, then to FP of the right size.
2090 if (MVT::isFloatingPoint(DstEltVT)) {
2091 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2092 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2093 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2094
2095 // Next, convert to FP elements of the same size.
2096 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2097 }
2098
2099 // Okay, we know the src/dst types are both integers of differing types.
2100 // Handling growing first.
2101 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2102 if (SrcBitSize < DstBitSize) {
2103 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2104
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002105 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002106 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2107 i += NumInputsPerOutput) {
2108 bool isLE = TLI.isLittleEndian();
2109 uint64_t NewBits = 0;
2110 bool EltIsUndef = true;
2111 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2112 // Shift the previously computed bits over.
2113 NewBits <<= SrcBitSize;
2114 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2115 if (Op.getOpcode() == ISD::UNDEF) continue;
2116 EltIsUndef = false;
2117
2118 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2119 }
2120
2121 if (EltIsUndef)
2122 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2123 else
2124 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2125 }
2126
2127 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2128 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002129 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002130 }
2131
2132 // Finally, this must be the case where we are shrinking elements: each input
2133 // turns into multiple outputs.
2134 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002135 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002136 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2137 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2138 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2139 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2140 continue;
2141 }
2142 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2143
2144 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2145 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2146 OpVal >>= DstBitSize;
2147 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2148 }
2149
2150 // For big endian targets, swap the order of the pieces of each element.
2151 if (!TLI.isLittleEndian())
2152 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2153 }
2154 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2155 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002156 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002157}
2158
2159
2160
Chris Lattner01b3d732005-09-28 22:28:18 +00002161SDOperand DAGCombiner::visitFADD(SDNode *N) {
2162 SDOperand N0 = N->getOperand(0);
2163 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002164 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2165 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002166 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002167
2168 // fold (fadd c1, c2) -> c1+c2
2169 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002170 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002171 // canonicalize constant to RHS
2172 if (N0CFP && !N1CFP)
2173 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002174 // fold (A + (-B)) -> A-B
2175 if (N1.getOpcode() == ISD::FNEG)
2176 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002177 // fold ((-A) + B) -> B-A
2178 if (N0.getOpcode() == ISD::FNEG)
2179 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002180 return SDOperand();
2181}
2182
2183SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2184 SDOperand N0 = N->getOperand(0);
2185 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002186 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2187 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002188 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002189
2190 // fold (fsub c1, c2) -> c1-c2
2191 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002192 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002193 // fold (A-(-B)) -> A+B
2194 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00002195 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002196 return SDOperand();
2197}
2198
2199SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2200 SDOperand N0 = N->getOperand(0);
2201 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002202 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2203 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002204 MVT::ValueType VT = N->getValueType(0);
2205
Nate Begeman11af4ea2005-10-17 20:40:11 +00002206 // fold (fmul c1, c2) -> c1*c2
2207 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002208 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002209 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002210 if (N0CFP && !N1CFP)
2211 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002212 // fold (fmul X, 2.0) -> (fadd X, X)
2213 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2214 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002215 return SDOperand();
2216}
2217
2218SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2219 SDOperand N0 = N->getOperand(0);
2220 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002221 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2222 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002223 MVT::ValueType VT = N->getValueType(0);
2224
Nate Begemana148d982006-01-18 22:35:16 +00002225 // fold (fdiv c1, c2) -> c1/c2
2226 if (N0CFP && N1CFP)
2227 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002228 return SDOperand();
2229}
2230
2231SDOperand DAGCombiner::visitFREM(SDNode *N) {
2232 SDOperand N0 = N->getOperand(0);
2233 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002234 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2235 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002236 MVT::ValueType VT = N->getValueType(0);
2237
Nate Begemana148d982006-01-18 22:35:16 +00002238 // fold (frem c1, c2) -> fmod(c1,c2)
2239 if (N0CFP && N1CFP)
2240 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002241 return SDOperand();
2242}
2243
Chris Lattner12d83032006-03-05 05:30:57 +00002244SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2245 SDOperand N0 = N->getOperand(0);
2246 SDOperand N1 = N->getOperand(1);
2247 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2248 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2249 MVT::ValueType VT = N->getValueType(0);
2250
2251 if (N0CFP && N1CFP) // Constant fold
2252 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2253
2254 if (N1CFP) {
2255 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2256 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2257 union {
2258 double d;
2259 int64_t i;
2260 } u;
2261 u.d = N1CFP->getValue();
2262 if (u.i >= 0)
2263 return DAG.getNode(ISD::FABS, VT, N0);
2264 else
2265 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2266 }
2267
2268 // copysign(fabs(x), y) -> copysign(x, y)
2269 // copysign(fneg(x), y) -> copysign(x, y)
2270 // copysign(copysign(x,z), y) -> copysign(x, y)
2271 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2272 N0.getOpcode() == ISD::FCOPYSIGN)
2273 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2274
2275 // copysign(x, abs(y)) -> abs(x)
2276 if (N1.getOpcode() == ISD::FABS)
2277 return DAG.getNode(ISD::FABS, VT, N0);
2278
2279 // copysign(x, copysign(y,z)) -> copysign(x, z)
2280 if (N1.getOpcode() == ISD::FCOPYSIGN)
2281 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2282
2283 // copysign(x, fp_extend(y)) -> copysign(x, y)
2284 // copysign(x, fp_round(y)) -> copysign(x, y)
2285 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2286 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2287
2288 return SDOperand();
2289}
2290
2291
Chris Lattner01b3d732005-09-28 22:28:18 +00002292
Nate Begeman83e75ec2005-09-06 04:43:02 +00002293SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002294 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002295 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002296 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002297
2298 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002299 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002300 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002301 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002302}
2303
Nate Begeman83e75ec2005-09-06 04:43:02 +00002304SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002305 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002306 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002307 MVT::ValueType VT = N->getValueType(0);
2308
Nate Begeman1d4d4142005-09-01 00:19:25 +00002309 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002310 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002311 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002312 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002313}
2314
Nate Begeman83e75ec2005-09-06 04:43:02 +00002315SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002316 SDOperand N0 = N->getOperand(0);
2317 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2318 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002319
2320 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002321 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002322 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002323 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002324}
2325
Nate Begeman83e75ec2005-09-06 04:43:02 +00002326SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002327 SDOperand N0 = N->getOperand(0);
2328 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2329 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002330
2331 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002332 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002333 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002334 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002335}
2336
Nate Begeman83e75ec2005-09-06 04:43:02 +00002337SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002338 SDOperand N0 = N->getOperand(0);
2339 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2340 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002341
2342 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002343 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002344 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002345
2346 // fold (fp_round (fp_extend x)) -> x
2347 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2348 return N0.getOperand(0);
2349
2350 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2351 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2352 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2353 AddToWorkList(Tmp.Val);
2354 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2355 }
2356
Nate Begeman83e75ec2005-09-06 04:43:02 +00002357 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002358}
2359
Nate Begeman83e75ec2005-09-06 04:43:02 +00002360SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002361 SDOperand N0 = N->getOperand(0);
2362 MVT::ValueType VT = N->getValueType(0);
2363 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002364 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002365
Nate Begeman1d4d4142005-09-01 00:19:25 +00002366 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002367 if (N0CFP) {
2368 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002369 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002370 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002371 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002372}
2373
Nate Begeman83e75ec2005-09-06 04:43:02 +00002374SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002375 SDOperand N0 = N->getOperand(0);
2376 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2377 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002378
2379 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002380 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002381 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00002382
2383 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
2384 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
2385 (!AfterLegalize||TLI.isOperationLegal(ISD::EXTLOAD, N0.getValueType()))) {
2386 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0),
2387 N0.getOperand(1), N0.getOperand(2),
2388 N0.getValueType());
2389 CombineTo(N, ExtLoad);
2390 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
2391 ExtLoad.getValue(1));
2392 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2393 }
2394
2395
Nate Begeman83e75ec2005-09-06 04:43:02 +00002396 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002397}
2398
Nate Begeman83e75ec2005-09-06 04:43:02 +00002399SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002400 SDOperand N0 = N->getOperand(0);
2401 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2402 MVT::ValueType VT = N->getValueType(0);
2403
2404 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002405 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002406 return DAG.getNode(ISD::FNEG, VT, N0);
2407 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002408 if (N0.getOpcode() == ISD::SUB)
2409 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002410 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002411 if (N0.getOpcode() == ISD::FNEG)
2412 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002413 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002414}
2415
Nate Begeman83e75ec2005-09-06 04:43:02 +00002416SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002417 SDOperand N0 = N->getOperand(0);
2418 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2419 MVT::ValueType VT = N->getValueType(0);
2420
Nate Begeman1d4d4142005-09-01 00:19:25 +00002421 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002422 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002423 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002424 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002425 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002426 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002427 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002428 // fold (fabs (fcopysign x, y)) -> (fabs x)
2429 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2430 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2431
Nate Begeman83e75ec2005-09-06 04:43:02 +00002432 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002433}
2434
Nate Begeman44728a72005-09-19 22:34:01 +00002435SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2436 SDOperand Chain = N->getOperand(0);
2437 SDOperand N1 = N->getOperand(1);
2438 SDOperand N2 = N->getOperand(2);
2439 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2440
2441 // never taken branch, fold to chain
2442 if (N1C && N1C->isNullValue())
2443 return Chain;
2444 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002445 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002446 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002447 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2448 // on the target.
2449 if (N1.getOpcode() == ISD::SETCC &&
2450 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2451 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2452 N1.getOperand(0), N1.getOperand(1), N2);
2453 }
Nate Begeman44728a72005-09-19 22:34:01 +00002454 return SDOperand();
2455}
2456
Chris Lattner3ea0b472005-10-05 06:47:48 +00002457// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2458//
Nate Begeman44728a72005-09-19 22:34:01 +00002459SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002460 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2461 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2462
2463 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002464 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2465 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2466
2467 // fold br_cc true, dest -> br dest (unconditional branch)
2468 if (SCCC && SCCC->getValue())
2469 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2470 N->getOperand(4));
2471 // fold br_cc false, dest -> unconditional fall through
2472 if (SCCC && SCCC->isNullValue())
2473 return N->getOperand(0);
2474 // fold to a simpler setcc
2475 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2476 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2477 Simp.getOperand(2), Simp.getOperand(0),
2478 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002479 return SDOperand();
2480}
2481
Chris Lattner01a22022005-10-10 22:04:48 +00002482SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2483 SDOperand Chain = N->getOperand(0);
2484 SDOperand Ptr = N->getOperand(1);
2485 SDOperand SrcValue = N->getOperand(2);
Chris Lattnere4b95392006-03-31 18:06:18 +00002486
2487 // If there are no uses of the loaded value, change uses of the chain value
2488 // into uses of the chain input (i.e. delete the dead load).
2489 if (N->hasNUsesOfValue(0, 0))
2490 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
Chris Lattner01a22022005-10-10 22:04:48 +00002491
2492 // If this load is directly stored, replace the load value with the stored
2493 // value.
2494 // TODO: Handle store large -> read small portion.
2495 // TODO: Handle TRUNCSTORE/EXTLOAD
2496 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2497 Chain.getOperand(1).getValueType() == N->getValueType(0))
2498 return CombineTo(N, Chain.getOperand(1), Chain);
2499
2500 return SDOperand();
2501}
2502
Chris Lattner29cd7db2006-03-31 18:10:41 +00002503/// visitXEXTLOAD - Handle EXTLOAD/ZEXTLOAD/SEXTLOAD.
2504SDOperand DAGCombiner::visitXEXTLOAD(SDNode *N) {
2505 SDOperand Chain = N->getOperand(0);
2506 SDOperand Ptr = N->getOperand(1);
2507 SDOperand SrcValue = N->getOperand(2);
2508 SDOperand EVT = N->getOperand(3);
2509
2510 // If there are no uses of the loaded value, change uses of the chain value
2511 // into uses of the chain input (i.e. delete the dead load).
2512 if (N->hasNUsesOfValue(0, 0))
2513 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
2514
2515 return SDOperand();
2516}
2517
Chris Lattner87514ca2005-10-10 22:31:19 +00002518SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2519 SDOperand Chain = N->getOperand(0);
2520 SDOperand Value = N->getOperand(1);
2521 SDOperand Ptr = N->getOperand(2);
2522 SDOperand SrcValue = N->getOperand(3);
2523
2524 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002525 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002526 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2527 // Make sure that these stores are the same value type:
2528 // FIXME: we really care that the second store is >= size of the first.
2529 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002530 // Create a new store of Value that replaces both stores.
2531 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002532 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2533 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002534 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2535 PrevStore->getOperand(0), Value, Ptr,
2536 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002537 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002538 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002539 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002540 }
2541
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002542 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002543 // FIXME: This needs to know that the resultant store does not need a
2544 // higher alignment than the original.
2545 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002546 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2547 Ptr, SrcValue);
2548
Chris Lattner87514ca2005-10-10 22:31:19 +00002549 return SDOperand();
2550}
2551
Chris Lattnerca242442006-03-19 01:27:56 +00002552SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2553 SDOperand InVec = N->getOperand(0);
2554 SDOperand InVal = N->getOperand(1);
2555 SDOperand EltNo = N->getOperand(2);
2556
2557 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2558 // vector with the inserted element.
2559 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2560 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002561 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002562 if (Elt < Ops.size())
2563 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002564 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
2565 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002566 }
2567
2568 return SDOperand();
2569}
2570
2571SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2572 SDOperand InVec = N->getOperand(0);
2573 SDOperand InVal = N->getOperand(1);
2574 SDOperand EltNo = N->getOperand(2);
2575 SDOperand NumElts = N->getOperand(3);
2576 SDOperand EltType = N->getOperand(4);
2577
2578 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2579 // vector with the inserted element.
2580 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2581 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002582 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002583 if (Elt < Ops.size()-2)
2584 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002585 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
2586 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002587 }
2588
2589 return SDOperand();
2590}
2591
Chris Lattnerd7648c82006-03-28 20:28:38 +00002592SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
2593 unsigned NumInScalars = N->getNumOperands()-2;
2594 SDOperand NumElts = N->getOperand(NumInScalars);
2595 SDOperand EltType = N->getOperand(NumInScalars+1);
2596
2597 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
2598 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
2599 // two distinct vectors, turn this into a shuffle node.
2600 SDOperand VecIn1, VecIn2;
2601 for (unsigned i = 0; i != NumInScalars; ++i) {
2602 // Ignore undef inputs.
2603 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
2604
2605 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
2606 // constant index, bail out.
2607 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
2608 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
2609 VecIn1 = VecIn2 = SDOperand(0, 0);
2610 break;
2611 }
2612
2613 // If the input vector type disagrees with the result of the vbuild_vector,
2614 // we can't make a shuffle.
2615 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
2616 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
2617 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
2618 VecIn1 = VecIn2 = SDOperand(0, 0);
2619 break;
2620 }
2621
2622 // Otherwise, remember this. We allow up to two distinct input vectors.
2623 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
2624 continue;
2625
2626 if (VecIn1.Val == 0) {
2627 VecIn1 = ExtractedFromVec;
2628 } else if (VecIn2.Val == 0) {
2629 VecIn2 = ExtractedFromVec;
2630 } else {
2631 // Too many inputs.
2632 VecIn1 = VecIn2 = SDOperand(0, 0);
2633 break;
2634 }
2635 }
2636
2637 // If everything is good, we can make a shuffle operation.
2638 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002639 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00002640 for (unsigned i = 0; i != NumInScalars; ++i) {
2641 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
2642 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2643 continue;
2644 }
2645
2646 SDOperand Extract = N->getOperand(i);
2647
2648 // If extracting from the first vector, just use the index directly.
2649 if (Extract.getOperand(0) == VecIn1) {
2650 BuildVecIndices.push_back(Extract.getOperand(1));
2651 continue;
2652 }
2653
2654 // Otherwise, use InIdx + VecSize
2655 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
2656 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
2657 }
2658
2659 // Add count and size info.
2660 BuildVecIndices.push_back(NumElts);
2661 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
2662
2663 // Return the new VVECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002664 SDOperand Ops[5];
2665 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00002666 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002667 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00002668 } else {
2669 // Use an undef vbuild_vector as input for the second operand.
2670 std::vector<SDOperand> UnOps(NumInScalars,
2671 DAG.getNode(ISD::UNDEF,
2672 cast<VTSDNode>(EltType)->getVT()));
2673 UnOps.push_back(NumElts);
2674 UnOps.push_back(EltType);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002675 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2676 &UnOps[0], UnOps.size());
2677 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00002678 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002679 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2680 &BuildVecIndices[0], BuildVecIndices.size());
2681 Ops[3] = NumElts;
2682 Ops[4] = EltType;
2683 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
Chris Lattnerd7648c82006-03-28 20:28:38 +00002684 }
2685
2686 return SDOperand();
2687}
2688
Chris Lattner66445d32006-03-28 22:11:53 +00002689SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002690 SDOperand ShufMask = N->getOperand(2);
2691 unsigned NumElts = ShufMask.getNumOperands();
2692
2693 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2694 bool isIdentity = true;
2695 for (unsigned i = 0; i != NumElts; ++i) {
2696 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2697 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2698 isIdentity = false;
2699 break;
2700 }
2701 }
2702 if (isIdentity) return N->getOperand(0);
2703
2704 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2705 isIdentity = true;
2706 for (unsigned i = 0; i != NumElts; ++i) {
2707 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2708 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2709 isIdentity = false;
2710 break;
2711 }
2712 }
2713 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00002714
2715 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
2716 // needed at all.
2717 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00002718 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002719 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00002720 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002721 for (unsigned i = 0; i != NumElts; ++i)
2722 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
2723 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
2724 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00002725 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00002726 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00002727 BaseIdx = Idx;
2728 } else {
2729 if (BaseIdx != Idx)
2730 isSplat = false;
2731 if (VecNum != V) {
2732 isUnary = false;
2733 break;
2734 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00002735 }
2736 }
2737
2738 SDOperand N0 = N->getOperand(0);
2739 SDOperand N1 = N->getOperand(1);
2740 // Normalize unary shuffle so the RHS is undef.
2741 if (isUnary && VecNum == 1)
2742 std::swap(N0, N1);
2743
Evan Cheng917ec982006-07-21 08:25:53 +00002744 // If it is a splat, check if the argument vector is a build_vector with
2745 // all scalar elements the same.
2746 if (isSplat) {
2747 SDNode *V = N0.Val;
2748 if (V->getOpcode() == ISD::BIT_CONVERT)
2749 V = V->getOperand(0).Val;
2750 if (V->getOpcode() == ISD::BUILD_VECTOR) {
2751 unsigned NumElems = V->getNumOperands()-2;
2752 if (NumElems > BaseIdx) {
2753 SDOperand Base;
2754 bool AllSame = true;
2755 for (unsigned i = 0; i != NumElems; ++i) {
2756 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
2757 Base = V->getOperand(i);
2758 break;
2759 }
2760 }
2761 // Splat of <u, u, u, u>, return <u, u, u, u>
2762 if (!Base.Val)
2763 return N0;
2764 for (unsigned i = 0; i != NumElems; ++i) {
2765 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
2766 V->getOperand(i) != Base) {
2767 AllSame = false;
2768 break;
2769 }
2770 }
2771 // Splat of <x, x, x, x>, return <x, x, x, x>
2772 if (AllSame)
2773 return N0;
2774 }
2775 }
2776 }
2777
Evan Chenge7bec0d2006-07-20 22:44:41 +00002778 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
2779 // into an undef.
2780 if (isUnary || N0 == N1) {
2781 if (N0.getOpcode() == ISD::UNDEF)
Evan Chengc04766a2006-04-06 23:20:43 +00002782 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00002783 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2784 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002785 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00002786 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00002787 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2788 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2789 MappedOps.push_back(ShufMask.getOperand(i));
2790 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00002791 unsigned NewIdx =
2792 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2793 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00002794 }
2795 }
2796 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002797 &MappedOps[0], MappedOps.size());
Chris Lattner3e104b12006-04-08 04:15:24 +00002798 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00002799 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
Evan Chenge7bec0d2006-07-20 22:44:41 +00002800 N0,
Chris Lattner66445d32006-03-28 22:11:53 +00002801 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
2802 ShufMask);
2803 }
2804
2805 return SDOperand();
2806}
2807
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002808SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
2809 SDOperand ShufMask = N->getOperand(2);
2810 unsigned NumElts = ShufMask.getNumOperands()-2;
2811
2812 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2813 bool isIdentity = true;
2814 for (unsigned i = 0; i != NumElts; ++i) {
2815 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2816 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2817 isIdentity = false;
2818 break;
2819 }
2820 }
2821 if (isIdentity) return N->getOperand(0);
2822
2823 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2824 isIdentity = true;
2825 for (unsigned i = 0; i != NumElts; ++i) {
2826 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2827 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2828 isIdentity = false;
2829 break;
2830 }
2831 }
2832 if (isIdentity) return N->getOperand(1);
2833
Evan Chenge7bec0d2006-07-20 22:44:41 +00002834 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
2835 // needed at all.
2836 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00002837 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002838 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00002839 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002840 for (unsigned i = 0; i != NumElts; ++i)
2841 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
2842 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
2843 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00002844 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00002845 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00002846 BaseIdx = Idx;
2847 } else {
2848 if (BaseIdx != Idx)
2849 isSplat = false;
2850 if (VecNum != V) {
2851 isUnary = false;
2852 break;
2853 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00002854 }
2855 }
2856
2857 SDOperand N0 = N->getOperand(0);
2858 SDOperand N1 = N->getOperand(1);
2859 // Normalize unary shuffle so the RHS is undef.
2860 if (isUnary && VecNum == 1)
2861 std::swap(N0, N1);
2862
Evan Cheng917ec982006-07-21 08:25:53 +00002863 // If it is a splat, check if the argument vector is a build_vector with
2864 // all scalar elements the same.
2865 if (isSplat) {
2866 SDNode *V = N0.Val;
2867 if (V->getOpcode() == ISD::VBIT_CONVERT)
2868 V = V->getOperand(0).Val;
2869 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
2870 unsigned NumElems = V->getNumOperands()-2;
2871 if (NumElems > BaseIdx) {
2872 SDOperand Base;
2873 bool AllSame = true;
2874 for (unsigned i = 0; i != NumElems; ++i) {
2875 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
2876 Base = V->getOperand(i);
2877 break;
2878 }
2879 }
2880 // Splat of <u, u, u, u>, return <u, u, u, u>
2881 if (!Base.Val)
2882 return N0;
2883 for (unsigned i = 0; i != NumElems; ++i) {
2884 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
2885 V->getOperand(i) != Base) {
2886 AllSame = false;
2887 break;
2888 }
2889 }
2890 // Splat of <x, x, x, x>, return <x, x, x, x>
2891 if (AllSame)
2892 return N0;
2893 }
2894 }
2895 }
2896
Evan Chenge7bec0d2006-07-20 22:44:41 +00002897 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
2898 // into an undef.
2899 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00002900 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2901 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002902 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00002903 for (unsigned i = 0; i != NumElts; ++i) {
2904 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2905 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2906 MappedOps.push_back(ShufMask.getOperand(i));
2907 } else {
2908 unsigned NewIdx =
2909 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2910 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
2911 }
2912 }
2913 // Add the type/#elts values.
2914 MappedOps.push_back(ShufMask.getOperand(NumElts));
2915 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
2916
2917 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002918 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00002919 AddToWorkList(ShufMask.Val);
2920
2921 // Build the undef vector.
2922 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
2923 for (unsigned i = 0; i != NumElts; ++i)
2924 MappedOps[i] = UDVal;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002925 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
2926 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002927 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2928 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00002929
2930 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
Evan Chenge7bec0d2006-07-20 22:44:41 +00002931 N0, UDVal, ShufMask,
Chris Lattner17614ea2006-04-08 05:34:25 +00002932 MappedOps[NumElts], MappedOps[NumElts+1]);
2933 }
2934
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002935 return SDOperand();
2936}
2937
Evan Cheng44f1f092006-04-20 08:56:16 +00002938/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
2939/// a VAND to a vector_shuffle with the destination vector and a zero vector.
2940/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
2941/// vector_shuffle V, Zero, <0, 4, 2, 4>
2942SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
2943 SDOperand LHS = N->getOperand(0);
2944 SDOperand RHS = N->getOperand(1);
2945 if (N->getOpcode() == ISD::VAND) {
2946 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
2947 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
2948 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
2949 RHS = RHS.getOperand(0);
2950 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
2951 std::vector<SDOperand> IdxOps;
2952 unsigned NumOps = RHS.getNumOperands();
2953 unsigned NumElts = NumOps-2;
2954 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
2955 for (unsigned i = 0; i != NumElts; ++i) {
2956 SDOperand Elt = RHS.getOperand(i);
2957 if (!isa<ConstantSDNode>(Elt))
2958 return SDOperand();
2959 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
2960 IdxOps.push_back(DAG.getConstant(i, EVT));
2961 else if (cast<ConstantSDNode>(Elt)->isNullValue())
2962 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
2963 else
2964 return SDOperand();
2965 }
2966
2967 // Let's see if the target supports this vector_shuffle.
2968 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
2969 return SDOperand();
2970
2971 // Return the new VVECTOR_SHUFFLE node.
2972 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
2973 SDOperand EVTNode = DAG.getValueType(EVT);
2974 std::vector<SDOperand> Ops;
Chris Lattner516b9622006-09-14 20:50:57 +00002975 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode,
2976 EVTNode);
Evan Cheng44f1f092006-04-20 08:56:16 +00002977 Ops.push_back(LHS);
2978 AddToWorkList(LHS.Val);
2979 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
2980 ZeroOps.push_back(NumEltsNode);
2981 ZeroOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002982 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2983 &ZeroOps[0], ZeroOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00002984 IdxOps.push_back(NumEltsNode);
2985 IdxOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002986 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2987 &IdxOps[0], IdxOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00002988 Ops.push_back(NumEltsNode);
2989 Ops.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002990 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
2991 &Ops[0], Ops.size());
Evan Cheng44f1f092006-04-20 08:56:16 +00002992 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
2993 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
2994 DstVecSize, DstVecEVT);
2995 }
2996 return Result;
2997 }
2998 }
2999 return SDOperand();
3000}
3001
Chris Lattneredab1b92006-04-02 03:25:57 +00003002/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
3003/// the scalar operation of the vop if it is operating on an integer vector
3004/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
3005SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
3006 ISD::NodeType FPOp) {
3007 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
3008 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
3009 SDOperand LHS = N->getOperand(0);
3010 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00003011 SDOperand Shuffle = XformToShuffleWithZero(N);
3012 if (Shuffle.Val) return Shuffle;
3013
Chris Lattneredab1b92006-04-02 03:25:57 +00003014 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
3015 // this operation.
3016 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
3017 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003018 SmallVector<SDOperand, 8> Ops;
Chris Lattneredab1b92006-04-02 03:25:57 +00003019 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
3020 SDOperand LHSOp = LHS.getOperand(i);
3021 SDOperand RHSOp = RHS.getOperand(i);
3022 // If these two elements can't be folded, bail out.
3023 if ((LHSOp.getOpcode() != ISD::UNDEF &&
3024 LHSOp.getOpcode() != ISD::Constant &&
3025 LHSOp.getOpcode() != ISD::ConstantFP) ||
3026 (RHSOp.getOpcode() != ISD::UNDEF &&
3027 RHSOp.getOpcode() != ISD::Constant &&
3028 RHSOp.getOpcode() != ISD::ConstantFP))
3029 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00003030 // Can't fold divide by zero.
3031 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
3032 if ((RHSOp.getOpcode() == ISD::Constant &&
3033 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
3034 (RHSOp.getOpcode() == ISD::ConstantFP &&
3035 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
3036 break;
3037 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003038 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00003039 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00003040 assert((Ops.back().getOpcode() == ISD::UNDEF ||
3041 Ops.back().getOpcode() == ISD::Constant ||
3042 Ops.back().getOpcode() == ISD::ConstantFP) &&
3043 "Scalar binop didn't fold!");
3044 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003045
3046 if (Ops.size() == LHS.getNumOperands()-2) {
3047 Ops.push_back(*(LHS.Val->op_end()-2));
3048 Ops.push_back(*(LHS.Val->op_end()-1));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003049 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003050 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003051 }
3052
3053 return SDOperand();
3054}
3055
Nate Begeman44728a72005-09-19 22:34:01 +00003056SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00003057 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
3058
3059 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
3060 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3061 // If we got a simplified select_cc node back from SimplifySelectCC, then
3062 // break it down into a new SETCC node, and a new SELECT node, and then return
3063 // the SELECT node, since we were called with a SELECT node.
3064 if (SCC.Val) {
3065 // Check to see if we got a select_cc back (to turn into setcc/select).
3066 // Otherwise, just return whatever node we got back, like fabs.
3067 if (SCC.getOpcode() == ISD::SELECT_CC) {
3068 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
3069 SCC.getOperand(0), SCC.getOperand(1),
3070 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00003071 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003072 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
3073 SCC.getOperand(3), SETCC);
3074 }
3075 return SCC;
3076 }
Nate Begeman44728a72005-09-19 22:34:01 +00003077 return SDOperand();
3078}
3079
Chris Lattner40c62d52005-10-18 06:04:22 +00003080/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
3081/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00003082/// select. Callers of this should assume that TheSelect is deleted if this
3083/// returns true. As such, they should return the appropriate thing (e.g. the
3084/// node) back to the top-level of the DAG combiner loop to avoid it being
3085/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00003086///
3087bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
3088 SDOperand RHS) {
3089
3090 // If this is a select from two identical things, try to pull the operation
3091 // through the select.
3092 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
3093#if 0
3094 std::cerr << "SELECT: ["; LHS.Val->dump();
3095 std::cerr << "] ["; RHS.Val->dump();
3096 std::cerr << "]\n";
3097#endif
3098
3099 // If this is a load and the token chain is identical, replace the select
3100 // of two loads with a load through a select of the address to load from.
3101 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
3102 // constants have been dropped into the constant pool.
3103 if ((LHS.getOpcode() == ISD::LOAD ||
3104 LHS.getOpcode() == ISD::EXTLOAD ||
3105 LHS.getOpcode() == ISD::ZEXTLOAD ||
3106 LHS.getOpcode() == ISD::SEXTLOAD) &&
3107 // Token chains must be identical.
3108 LHS.getOperand(0) == RHS.getOperand(0) &&
3109 // If this is an EXTLOAD, the VT's must match.
3110 (LHS.getOpcode() == ISD::LOAD ||
3111 LHS.getOperand(3) == RHS.getOperand(3))) {
3112 // FIXME: this conflates two src values, discarding one. This is not
3113 // the right thing to do, but nothing uses srcvalues now. When they do,
3114 // turn SrcValue into a list of locations.
3115 SDOperand Addr;
3116 if (TheSelect->getOpcode() == ISD::SELECT)
3117 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
3118 TheSelect->getOperand(0), LHS.getOperand(1),
3119 RHS.getOperand(1));
3120 else
3121 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
3122 TheSelect->getOperand(0),
3123 TheSelect->getOperand(1),
3124 LHS.getOperand(1), RHS.getOperand(1),
3125 TheSelect->getOperand(4));
3126
3127 SDOperand Load;
3128 if (LHS.getOpcode() == ISD::LOAD)
3129 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
3130 Addr, LHS.getOperand(2));
3131 else
3132 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
3133 LHS.getOperand(0), Addr, LHS.getOperand(2),
3134 cast<VTSDNode>(LHS.getOperand(3))->getVT());
3135 // Users of the select now use the result of the load.
3136 CombineTo(TheSelect, Load);
3137
3138 // Users of the old loads now use the new load's chain. We know the
3139 // old-load value is dead now.
3140 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
3141 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
3142 return true;
3143 }
3144 }
3145
3146 return false;
3147}
3148
Nate Begeman44728a72005-09-19 22:34:01 +00003149SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
3150 SDOperand N2, SDOperand N3,
3151 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00003152
3153 MVT::ValueType VT = N2.getValueType();
Chris Lattner5eed34d2006-05-12 17:57:54 +00003154 //ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003155 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
3156 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
3157 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
3158
3159 // Determine if the condition we're dealing with is constant
3160 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
3161 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
3162
3163 // fold select_cc true, x, y -> x
3164 if (SCCC && SCCC->getValue())
3165 return N2;
3166 // fold select_cc false, x, y -> y
3167 if (SCCC && SCCC->getValue() == 0)
3168 return N3;
3169
3170 // Check to see if we can simplify the select into an fabs node
3171 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
3172 // Allow either -0.0 or 0.0
3173 if (CFP->getValue() == 0.0) {
3174 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
3175 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
3176 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
3177 N2 == N3.getOperand(0))
3178 return DAG.getNode(ISD::FABS, VT, N0);
3179
3180 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
3181 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3182 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3183 N2.getOperand(0) == N3)
3184 return DAG.getNode(ISD::FABS, VT, N3);
3185 }
3186 }
3187
3188 // Check to see if we can perform the "gzip trick", transforming
3189 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
3190 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
3191 MVT::isInteger(N0.getValueType()) &&
3192 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
3193 MVT::ValueType XType = N0.getValueType();
3194 MVT::ValueType AType = N2.getValueType();
3195 if (XType >= AType) {
3196 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00003197 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00003198 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3199 unsigned ShCtV = Log2_64(N2C->getValue());
3200 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3201 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3202 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00003203 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003204 if (XType > AType) {
3205 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003206 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003207 }
3208 return DAG.getNode(ISD::AND, AType, Shift, N2);
3209 }
3210 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3211 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3212 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003213 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003214 if (XType > AType) {
3215 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003216 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003217 }
3218 return DAG.getNode(ISD::AND, AType, Shift, N2);
3219 }
3220 }
Nate Begeman07ed4172005-10-10 21:26:48 +00003221
3222 // fold select C, 16, 0 -> shl C, 4
3223 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3224 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3225 // Get a SetCC of the condition
3226 // FIXME: Should probably make sure that setcc is legal if we ever have a
3227 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00003228 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00003229 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00003230 if (AfterLegalize) {
3231 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003232 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00003233 } else {
3234 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003235 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00003236 }
Chris Lattner5750df92006-03-01 04:03:14 +00003237 AddToWorkList(SCC.Val);
3238 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00003239 // shl setcc result by log2 n2c
3240 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3241 DAG.getConstant(Log2_64(N2C->getValue()),
3242 TLI.getShiftAmountTy()));
3243 }
3244
Nate Begemanf845b452005-10-08 00:29:44 +00003245 // Check to see if this is the equivalent of setcc
3246 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3247 // otherwise, go ahead with the folds.
3248 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3249 MVT::ValueType XType = N0.getValueType();
3250 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3251 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3252 if (Res.getValueType() != VT)
3253 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3254 return Res;
3255 }
3256
3257 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3258 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3259 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3260 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3261 return DAG.getNode(ISD::SRL, XType, Ctlz,
3262 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3263 TLI.getShiftAmountTy()));
3264 }
3265 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3266 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3267 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3268 N0);
3269 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3270 DAG.getConstant(~0ULL, XType));
3271 return DAG.getNode(ISD::SRL, XType,
3272 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3273 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3274 TLI.getShiftAmountTy()));
3275 }
3276 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3277 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3278 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3279 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3280 TLI.getShiftAmountTy()));
3281 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3282 }
3283 }
3284
3285 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3286 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3287 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3288 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3289 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3290 MVT::ValueType XType = N0.getValueType();
3291 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3292 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3293 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3294 TLI.getShiftAmountTy()));
3295 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003296 AddToWorkList(Shift.Val);
3297 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003298 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3299 }
3300 }
3301 }
3302
Nate Begeman44728a72005-09-19 22:34:01 +00003303 return SDOperand();
3304}
3305
Nate Begeman452d7be2005-09-16 00:54:12 +00003306SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00003307 SDOperand N1, ISD::CondCode Cond,
3308 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003309 // These setcc operations always fold.
3310 switch (Cond) {
3311 default: break;
3312 case ISD::SETFALSE:
3313 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3314 case ISD::SETTRUE:
3315 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3316 }
3317
3318 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3319 uint64_t C1 = N1C->getValue();
3320 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
3321 uint64_t C0 = N0C->getValue();
3322
3323 // Sign extend the operands if required
3324 if (ISD::isSignedIntSetCC(Cond)) {
3325 C0 = N0C->getSignExtended();
3326 C1 = N1C->getSignExtended();
3327 }
3328
3329 switch (Cond) {
3330 default: assert(0 && "Unknown integer setcc!");
3331 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3332 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3333 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
3334 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
3335 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
3336 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
3337 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
3338 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
3339 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
3340 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
3341 }
3342 } else {
3343 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3344 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3345 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3346
3347 // If the comparison constant has bits in the upper part, the
3348 // zero-extended value could never match.
3349 if (C1 & (~0ULL << InSize)) {
3350 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3351 switch (Cond) {
3352 case ISD::SETUGT:
3353 case ISD::SETUGE:
3354 case ISD::SETEQ: return DAG.getConstant(0, VT);
3355 case ISD::SETULT:
3356 case ISD::SETULE:
3357 case ISD::SETNE: return DAG.getConstant(1, VT);
3358 case ISD::SETGT:
3359 case ISD::SETGE:
3360 // True if the sign bit of C1 is set.
3361 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3362 case ISD::SETLT:
3363 case ISD::SETLE:
3364 // True if the sign bit of C1 isn't set.
3365 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3366 default:
3367 break;
3368 }
3369 }
3370
3371 // Otherwise, we can perform the comparison with the low bits.
3372 switch (Cond) {
3373 case ISD::SETEQ:
3374 case ISD::SETNE:
3375 case ISD::SETUGT:
3376 case ISD::SETUGE:
3377 case ISD::SETULT:
3378 case ISD::SETULE:
3379 return DAG.getSetCC(VT, N0.getOperand(0),
3380 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3381 Cond);
3382 default:
3383 break; // todo, be more careful with signed comparisons
3384 }
3385 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3386 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3387 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3388 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3389 MVT::ValueType ExtDstTy = N0.getValueType();
3390 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3391
3392 // If the extended part has any inconsistent bits, it cannot ever
3393 // compare equal. In other words, they have to be all ones or all
3394 // zeros.
3395 uint64_t ExtBits =
3396 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3397 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3398 return DAG.getConstant(Cond == ISD::SETNE, VT);
3399
3400 SDOperand ZextOp;
3401 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3402 if (Op0Ty == ExtSrcTy) {
3403 ZextOp = N0.getOperand(0);
3404 } else {
3405 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3406 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3407 DAG.getConstant(Imm, Op0Ty));
3408 }
Chris Lattner5750df92006-03-01 04:03:14 +00003409 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003410 // Otherwise, make this a use of a zext.
3411 return DAG.getSetCC(VT, ZextOp,
3412 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3413 ExtDstTy),
3414 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00003415 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
3416 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3417 (N0.getOpcode() == ISD::XOR ||
3418 (N0.getOpcode() == ISD::AND &&
3419 N0.getOperand(0).getOpcode() == ISD::XOR &&
3420 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3421 isa<ConstantSDNode>(N0.getOperand(1)) &&
3422 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
3423 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
3424 // only do this if the top bits are known zero.
3425 if (TLI.MaskedValueIsZero(N1,
3426 MVT::getIntVTBitMask(N0.getValueType())-1)) {
3427 // Okay, get the un-inverted input value.
3428 SDOperand Val;
3429 if (N0.getOpcode() == ISD::XOR)
3430 Val = N0.getOperand(0);
3431 else {
3432 assert(N0.getOpcode() == ISD::AND &&
3433 N0.getOperand(0).getOpcode() == ISD::XOR);
3434 // ((X^1)&1)^1 -> X & 1
3435 Val = DAG.getNode(ISD::AND, N0.getValueType(),
3436 N0.getOperand(0).getOperand(0), N0.getOperand(1));
3437 }
3438 return DAG.getSetCC(VT, Val, N1,
3439 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
3440 }
Nate Begeman452d7be2005-09-16 00:54:12 +00003441 }
Chris Lattner5c46f742005-10-05 06:11:08 +00003442
Nate Begeman452d7be2005-09-16 00:54:12 +00003443 uint64_t MinVal, MaxVal;
3444 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
3445 if (ISD::isSignedIntSetCC(Cond)) {
3446 MinVal = 1ULL << (OperandBitSize-1);
3447 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
3448 MaxVal = ~0ULL >> (65-OperandBitSize);
3449 else
3450 MaxVal = 0;
3451 } else {
3452 MinVal = 0;
3453 MaxVal = ~0ULL >> (64-OperandBitSize);
3454 }
3455
3456 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
3457 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
3458 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
3459 --C1; // X >= C0 --> X > (C0-1)
3460 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3461 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
3462 }
3463
3464 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
3465 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
3466 ++C1; // X <= C0 --> X < (C0+1)
3467 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3468 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
3469 }
3470
3471 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
3472 return DAG.getConstant(0, VT); // X < MIN --> false
3473
3474 // Canonicalize setgt X, Min --> setne X, Min
3475 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
3476 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00003477 // Canonicalize setlt X, Max --> setne X, Max
3478 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
3479 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00003480
3481 // If we have setult X, 1, turn it into seteq X, 0
3482 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
3483 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
3484 ISD::SETEQ);
3485 // If we have setugt X, Max-1, turn it into seteq X, Max
3486 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
3487 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
3488 ISD::SETEQ);
3489
3490 // If we have "setcc X, C0", check to see if we can shrink the immediate
3491 // by changing cc.
3492
3493 // SETUGT X, SINTMAX -> SETLT X, 0
3494 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
3495 C1 == (~0ULL >> (65-OperandBitSize)))
3496 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
3497 ISD::SETLT);
3498
3499 // FIXME: Implement the rest of these.
3500
3501 // Fold bit comparisons when we can.
3502 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3503 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
3504 if (ConstantSDNode *AndRHS =
3505 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3506 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
3507 // Perform the xform if the AND RHS is a single bit.
3508 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
3509 return DAG.getNode(ISD::SRL, VT, N0,
3510 DAG.getConstant(Log2_64(AndRHS->getValue()),
3511 TLI.getShiftAmountTy()));
3512 }
3513 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
3514 // (X & 8) == 8 --> (X & 8) >> 3
3515 // Perform the xform if C1 is a single bit.
3516 if ((C1 & (C1-1)) == 0) {
3517 return DAG.getNode(ISD::SRL, VT, N0,
Chris Lattner729c6d12006-05-27 00:43:02 +00003518 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
Nate Begeman452d7be2005-09-16 00:54:12 +00003519 }
3520 }
3521 }
3522 }
3523 } else if (isa<ConstantSDNode>(N0.Val)) {
3524 // Ensure that the constant occurs on the RHS.
3525 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3526 }
3527
3528 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
3529 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
3530 double C0 = N0C->getValue(), C1 = N1C->getValue();
3531
3532 switch (Cond) {
3533 default: break; // FIXME: Implement the rest of these!
3534 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3535 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3536 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
3537 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
3538 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
3539 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
3540 }
3541 } else {
3542 // Ensure that the constant occurs on the RHS.
3543 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3544 }
3545
3546 if (N0 == N1) {
3547 // We can always fold X == Y for integer setcc's.
3548 if (MVT::isInteger(N0.getValueType()))
3549 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3550 unsigned UOF = ISD::getUnorderedFlavor(Cond);
3551 if (UOF == 2) // FP operators that are undefined on NaNs.
3552 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3553 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
3554 return DAG.getConstant(UOF, VT);
3555 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
3556 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00003557 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00003558 if (NewCond != Cond)
3559 return DAG.getSetCC(VT, N0, N1, NewCond);
3560 }
3561
3562 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3563 MVT::isInteger(N0.getValueType())) {
3564 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
3565 N0.getOpcode() == ISD::XOR) {
3566 // Simplify (X+Y) == (X+Z) --> Y == Z
3567 if (N0.getOpcode() == N1.getOpcode()) {
3568 if (N0.getOperand(0) == N1.getOperand(0))
3569 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
3570 if (N0.getOperand(1) == N1.getOperand(1))
3571 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
Evan Cheng1efba0e2006-08-29 06:42:35 +00003572 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003573 // If X op Y == Y op X, try other combinations.
3574 if (N0.getOperand(0) == N1.getOperand(1))
3575 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
3576 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00003577 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00003578 }
3579 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003580
3581 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
3582 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3583 // Turn (X+C1) == C2 --> X == C2-C1
3584 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
3585 return DAG.getSetCC(VT, N0.getOperand(0),
3586 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
3587 N0.getValueType()), Cond);
3588 }
3589
3590 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
3591 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00003592 // If we know that all of the inverted bits are zero, don't bother
3593 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003594 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00003595 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003596 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00003597 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003598 }
3599
3600 // Turn (C1-X) == C2 --> X == C1-C2
3601 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
3602 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
3603 return DAG.getSetCC(VT, N0.getOperand(1),
3604 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
3605 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00003606 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003607 }
3608 }
3609
Nate Begeman452d7be2005-09-16 00:54:12 +00003610 // Simplify (X+Z) == X --> Z == 0
3611 if (N0.getOperand(0) == N1)
3612 return DAG.getSetCC(VT, N0.getOperand(1),
3613 DAG.getConstant(0, N0.getValueType()), Cond);
3614 if (N0.getOperand(1) == N1) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00003615 if (DAG.isCommutativeBinOp(N0.getOpcode()))
Nate Begeman452d7be2005-09-16 00:54:12 +00003616 return DAG.getSetCC(VT, N0.getOperand(0),
3617 DAG.getConstant(0, N0.getValueType()), Cond);
3618 else {
3619 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
3620 // (Z-X) == X --> Z == X<<1
3621 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
3622 N1,
3623 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003624 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003625 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
3626 }
3627 }
3628 }
3629
3630 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3631 N1.getOpcode() == ISD::XOR) {
3632 // Simplify X == (X+Z) --> Z == 0
3633 if (N1.getOperand(0) == N0) {
3634 return DAG.getSetCC(VT, N1.getOperand(1),
3635 DAG.getConstant(0, N1.getValueType()), Cond);
3636 } else if (N1.getOperand(1) == N0) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00003637 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003638 return DAG.getSetCC(VT, N1.getOperand(0),
3639 DAG.getConstant(0, N1.getValueType()), Cond);
3640 } else {
3641 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3642 // X == (Z-X) --> X<<1 == Z
3643 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
3644 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003645 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003646 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
3647 }
3648 }
3649 }
3650 }
3651
3652 // Fold away ALL boolean setcc's.
3653 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00003654 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003655 switch (Cond) {
3656 default: assert(0 && "Unknown integer setcc!");
3657 case ISD::SETEQ: // X == Y -> (X^Y)^1
3658 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3659 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00003660 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003661 break;
3662 case ISD::SETNE: // X != Y --> (X^Y)
3663 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3664 break;
3665 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
3666 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
3667 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3668 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003669 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003670 break;
3671 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
3672 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
3673 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3674 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003675 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003676 break;
3677 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
3678 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
3679 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3680 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003681 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003682 break;
3683 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
3684 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
3685 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3686 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
3687 break;
3688 }
3689 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00003690 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003691 // FIXME: If running after legalize, we probably can't do this.
3692 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3693 }
3694 return N0;
3695 }
3696
3697 // Could not fold it.
3698 return SDOperand();
3699}
3700
Nate Begeman69575232005-10-20 02:15:44 +00003701/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3702/// return a DAG expression to select that will generate the same value by
3703/// multiplying by a magic number. See:
3704/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3705SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003706 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003707 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
3708
Andrew Lenharth232c9102006-06-12 16:07:18 +00003709 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003710 ii != ee; ++ii)
3711 AddToWorkList(*ii);
3712 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003713}
3714
3715/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3716/// return a DAG expression to select that will generate the same value by
3717/// multiplying by a magic number. See:
3718/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3719SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003720 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003721 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00003722
Andrew Lenharth232c9102006-06-12 16:07:18 +00003723 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003724 ii != ee; ++ii)
3725 AddToWorkList(*ii);
3726 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003727}
3728
Nate Begeman1d4d4142005-09-01 00:19:25 +00003729// SelectionDAG::Combine - This is the entry point for the file.
3730//
Nate Begeman4ebd8052005-09-01 23:24:04 +00003731void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003732 /// run - This is the main entry point to this class.
3733 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00003734 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003735}