blob: 96456aa5f05219bac4264c1999b1a90fd0d03fc8 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
12//
13// FIXME: Missing folds
14// sdiv, udiv, srem, urem (X, const) where X is an integer can be expanded into
15// a sequence of multiplies, shifts, and adds. This should be controlled by
16// some kind of hint from the target that int div is expensive.
17// various folds of mulh[s,u] by constants such as -1, powers of 2, etc.
18//
Nate Begeman44728a72005-09-19 22:34:01 +000019// FIXME: select C, pow2, pow2 -> something smart
20// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
Nate Begeman44728a72005-09-19 22:34:01 +000021// FIXME: Dead stores -> nuke
Chris Lattner40c62d52005-10-18 06:04:22 +000022// FIXME: shr X, (and Y,31) -> shr X, Y (TRICKY!)
Nate Begeman1d4d4142005-09-01 00:19:25 +000023// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000024// FIXME: undef values
Nate Begeman646d7e22005-09-02 21:18:40 +000025// FIXME: divide by zero is currently left unfolded. do we want to turn this
26// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000027// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Nate Begeman1d4d4142005-09-01 00:19:25 +000028//
29//===----------------------------------------------------------------------===//
30
31#define DEBUG_TYPE "dagcombine"
32#include "llvm/ADT/Statistic.h"
33#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000034#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000035#include "llvm/Support/MathExtras.h"
36#include "llvm/Target/TargetLowering.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000037#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000038#include "llvm/Support/CommandLine.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000039#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000040#include <cmath>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000041#include <iostream>
Jim Laskey279f0532006-09-25 16:29:54 +000042#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000043using namespace llvm;
44
45namespace {
Andrew Lenharthae6153f2006-07-20 17:43:27 +000046 static Statistic<> NodesCombined ("dagcombiner",
47 "Number of dag nodes combined");
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000048
Jim Laskey71382342006-10-07 23:37:56 +000049 static cl::opt<bool>
50 CombinerAA("combiner-alias-analysis", cl::Hidden,
51 cl::desc("Turn on alias analysis turning testing"));
Jim Laskey6ff23e52006-10-04 16:53:27 +000052
Jim Laskeybc588b82006-10-05 15:07:25 +000053//------------------------------ DAGCombiner ---------------------------------//
54
Jim Laskey71382342006-10-07 23:37:56 +000055 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000056 SelectionDAG &DAG;
57 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000058 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000059
60 // Worklist of all of the nodes that need to be simplified.
61 std::vector<SDNode*> WorkList;
62
63 /// AddUsersToWorkList - When an instruction is simplified, add all users of
64 /// the instruction to the work lists because they might get more simplified
65 /// now.
66 ///
67 void AddUsersToWorkList(SDNode *N) {
68 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000069 UI != UE; ++UI)
Jim Laskey6ff23e52006-10-04 16:53:27 +000070 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000071 }
72
73 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000074 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000075 void removeFromWorkList(SDNode *N) {
76 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
77 WorkList.end());
78 }
79
Chris Lattner24664722006-03-01 04:53:38 +000080 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000081 /// AddToWorkList - Add to the work list making sure it's instance is at the
82 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000083 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000084 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000085 WorkList.push_back(N);
86 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000087
Chris Lattner3577e382006-08-11 17:56:38 +000088 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo) {
89 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
Chris Lattner87514ca2005-10-10 22:31:19 +000090 ++NodesCombined;
Jim Laskey6ff23e52006-10-04 16:53:27 +000091 DEBUG(std::cerr << "\nReplacing.1 "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +000092 std::cerr << "\nWith: "; To[0].Val->dump(&DAG);
Chris Lattner3577e382006-08-11 17:56:38 +000093 std::cerr << " and " << NumTo-1 << " other values\n");
Chris Lattner01a22022005-10-10 22:04:48 +000094 std::vector<SDNode*> NowDead;
Chris Lattner3577e382006-08-11 17:56:38 +000095 DAG.ReplaceAllUsesWith(N, To, &NowDead);
Chris Lattner01a22022005-10-10 22:04:48 +000096
97 // Push the new nodes and any users onto the worklist
Chris Lattner3577e382006-08-11 17:56:38 +000098 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000099 AddToWorkList(To[i].Val);
Chris Lattner01a22022005-10-10 22:04:48 +0000100 AddUsersToWorkList(To[i].Val);
101 }
102
Jim Laskey6ff23e52006-10-04 16:53:27 +0000103 // Nodes can be reintroduced into the worklist. Make sure we do not
104 // process a node that has been replaced.
Chris Lattner01a22022005-10-10 22:04:48 +0000105 removeFromWorkList(N);
106 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
107 removeFromWorkList(NowDead[i]);
108
109 // Finally, since the node is now dead, remove it from the graph.
110 DAG.DeleteNode(N);
111 return SDOperand(N, 0);
112 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000113
Chris Lattner24664722006-03-01 04:53:38 +0000114 SDOperand CombineTo(SDNode *N, SDOperand Res) {
Chris Lattner3577e382006-08-11 17:56:38 +0000115 return CombineTo(N, &Res, 1);
Chris Lattner24664722006-03-01 04:53:38 +0000116 }
117
118 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
Chris Lattner3577e382006-08-11 17:56:38 +0000119 SDOperand To[] = { Res0, Res1 };
120 return CombineTo(N, To, 2);
Chris Lattner24664722006-03-01 04:53:38 +0000121 }
122 private:
123
Chris Lattner012f2412006-02-17 21:58:01 +0000124 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000125 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000126 /// propagation. If so, return true.
127 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000128 TargetLowering::TargetLoweringOpt TLO(DAG);
129 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000130 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
131 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
132 return false;
133
134 // Revisit the node.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000135 AddToWorkList(Op.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000136
137 // Replace the old value with the new one.
138 ++NodesCombined;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000139 DEBUG(std::cerr << "\nReplacing.2 "; TLO.Old.Val->dump();
Jim Laskey279f0532006-09-25 16:29:54 +0000140 std::cerr << "\nWith: "; TLO.New.Val->dump(&DAG);
141 std::cerr << '\n');
Chris Lattner012f2412006-02-17 21:58:01 +0000142
143 std::vector<SDNode*> NowDead;
144 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
145
Chris Lattner7d20d392006-02-20 06:51:04 +0000146 // Push the new node and any (possibly new) users onto the worklist.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000147 AddToWorkList(TLO.New.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000148 AddUsersToWorkList(TLO.New.Val);
149
150 // Nodes can end up on the worklist more than once. Make sure we do
151 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000152 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
153 removeFromWorkList(NowDead[i]);
154
Chris Lattner7d20d392006-02-20 06:51:04 +0000155 // Finally, if the node is now dead, remove it from the graph. The node
156 // may not be dead if the replacement process recursively simplified to
157 // something else needing this node.
158 if (TLO.Old.Val->use_empty()) {
159 removeFromWorkList(TLO.Old.Val);
160 DAG.DeleteNode(TLO.Old.Val);
161 }
Chris Lattner012f2412006-02-17 21:58:01 +0000162 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000163 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000164
Nate Begeman1d4d4142005-09-01 00:19:25 +0000165 /// visit - call the node-specific routine that knows how to fold each
166 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000167 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000168
169 // Visitation implementation - Implement dag node combining for different
170 // node types. The semantics are as follows:
171 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000172 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000173 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000174 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000175 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000176 SDOperand visitTokenFactor(SDNode *N);
177 SDOperand visitADD(SDNode *N);
178 SDOperand visitSUB(SDNode *N);
179 SDOperand visitMUL(SDNode *N);
180 SDOperand visitSDIV(SDNode *N);
181 SDOperand visitUDIV(SDNode *N);
182 SDOperand visitSREM(SDNode *N);
183 SDOperand visitUREM(SDNode *N);
184 SDOperand visitMULHU(SDNode *N);
185 SDOperand visitMULHS(SDNode *N);
186 SDOperand visitAND(SDNode *N);
187 SDOperand visitOR(SDNode *N);
188 SDOperand visitXOR(SDNode *N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000189 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000190 SDOperand visitSHL(SDNode *N);
191 SDOperand visitSRA(SDNode *N);
192 SDOperand visitSRL(SDNode *N);
193 SDOperand visitCTLZ(SDNode *N);
194 SDOperand visitCTTZ(SDNode *N);
195 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000196 SDOperand visitSELECT(SDNode *N);
197 SDOperand visitSELECT_CC(SDNode *N);
198 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000199 SDOperand visitSIGN_EXTEND(SDNode *N);
200 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000201 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000202 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
203 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000204 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000205 SDOperand visitVBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000206 SDOperand visitFADD(SDNode *N);
207 SDOperand visitFSUB(SDNode *N);
208 SDOperand visitFMUL(SDNode *N);
209 SDOperand visitFDIV(SDNode *N);
210 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000211 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000212 SDOperand visitSINT_TO_FP(SDNode *N);
213 SDOperand visitUINT_TO_FP(SDNode *N);
214 SDOperand visitFP_TO_SINT(SDNode *N);
215 SDOperand visitFP_TO_UINT(SDNode *N);
216 SDOperand visitFP_ROUND(SDNode *N);
217 SDOperand visitFP_ROUND_INREG(SDNode *N);
218 SDOperand visitFP_EXTEND(SDNode *N);
219 SDOperand visitFNEG(SDNode *N);
220 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000221 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000222 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000223 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000224 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000225 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
226 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000227 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000228 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000229 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000230
Evan Cheng44f1f092006-04-20 08:56:16 +0000231 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000232 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
233
Chris Lattner40c62d52005-10-18 06:04:22 +0000234 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000235 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000236 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
237 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
238 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000239 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000240 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000241 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000242 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000243 SDOperand BuildUDIV(SDNode *N);
244 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Jim Laskey279f0532006-09-25 16:29:54 +0000245
Jim Laskey6ff23e52006-10-04 16:53:27 +0000246 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
247 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000248 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000249 SmallVector<SDOperand, 8> &Aliases);
250
Jim Laskey279f0532006-09-25 16:29:54 +0000251 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000252 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000253 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
254
Nate Begeman1d4d4142005-09-01 00:19:25 +0000255public:
256 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000257 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000258
259 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000260 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000261 };
262}
263
Chris Lattner24664722006-03-01 04:53:38 +0000264//===----------------------------------------------------------------------===//
265// TargetLowering::DAGCombinerInfo implementation
266//===----------------------------------------------------------------------===//
267
268void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
269 ((DAGCombiner*)DC)->AddToWorkList(N);
270}
271
272SDOperand TargetLowering::DAGCombinerInfo::
273CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000274 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000275}
276
277SDOperand TargetLowering::DAGCombinerInfo::
278CombineTo(SDNode *N, SDOperand Res) {
279 return ((DAGCombiner*)DC)->CombineTo(N, Res);
280}
281
282
283SDOperand TargetLowering::DAGCombinerInfo::
284CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
285 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
286}
287
288
289
290
291//===----------------------------------------------------------------------===//
292
293
Nate Begeman4ebd8052005-09-01 23:24:04 +0000294// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
295// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000296// Also, set the incoming LHS, RHS, and CC references to the appropriate
297// nodes based on the type of node we are checking. This simplifies life a
298// bit for the callers.
299static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
300 SDOperand &CC) {
301 if (N.getOpcode() == ISD::SETCC) {
302 LHS = N.getOperand(0);
303 RHS = N.getOperand(1);
304 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000305 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000306 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000307 if (N.getOpcode() == ISD::SELECT_CC &&
308 N.getOperand(2).getOpcode() == ISD::Constant &&
309 N.getOperand(3).getOpcode() == ISD::Constant &&
310 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000311 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
312 LHS = N.getOperand(0);
313 RHS = N.getOperand(1);
314 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000315 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000316 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000317 return false;
318}
319
Nate Begeman99801192005-09-07 23:25:52 +0000320// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
321// one use. If this is true, it allows the users to invert the operation for
322// free when it is profitable to do so.
323static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000324 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000325 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000326 return true;
327 return false;
328}
329
Nate Begemancd4d58c2006-02-03 06:46:56 +0000330SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
331 MVT::ValueType VT = N0.getValueType();
332 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
333 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
334 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
335 if (isa<ConstantSDNode>(N1)) {
336 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000337 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000338 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
339 } else if (N0.hasOneUse()) {
340 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000341 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000342 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
343 }
344 }
345 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
346 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
347 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
348 if (isa<ConstantSDNode>(N0)) {
349 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000350 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000351 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
352 } else if (N1.hasOneUse()) {
353 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000354 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000355 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
356 }
357 }
358 return SDOperand();
359}
360
Nate Begeman4ebd8052005-09-01 23:24:04 +0000361void DAGCombiner::Run(bool RunningAfterLegalize) {
362 // set the instance variable, so that the various visit routines may use it.
363 AfterLegalize = RunningAfterLegalize;
364
Nate Begeman646d7e22005-09-02 21:18:40 +0000365 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000366 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
367 E = DAG.allnodes_end(); I != E; ++I)
368 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000369
Chris Lattner95038592005-10-05 06:35:28 +0000370 // Create a dummy node (which is not added to allnodes), that adds a reference
371 // to the root node, preventing it from being deleted, and tracking any
372 // changes of the root.
373 HandleSDNode Dummy(DAG.getRoot());
374
Chris Lattner24664722006-03-01 04:53:38 +0000375
376 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
377 TargetLowering::DAGCombinerInfo
378 DagCombineInfo(DAG, !RunningAfterLegalize, this);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000379
Nate Begeman1d4d4142005-09-01 00:19:25 +0000380 // while the worklist isn't empty, inspect the node on the end of it and
381 // try and combine it.
382 while (!WorkList.empty()) {
383 SDNode *N = WorkList.back();
384 WorkList.pop_back();
385
386 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000387 // N is deleted from the DAG, since they too may now be dead or may have a
388 // reduced number of uses, allowing other xforms.
389 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000390 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000391 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000392
Chris Lattner95038592005-10-05 06:35:28 +0000393 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000394 continue;
395 }
396
Nate Begeman83e75ec2005-09-06 04:43:02 +0000397 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000398
399 // If nothing happened, try a target-specific DAG combine.
400 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000401 assert(N->getOpcode() != ISD::DELETED_NODE &&
402 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000403 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
404 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
405 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
406 }
407
Nate Begeman83e75ec2005-09-06 04:43:02 +0000408 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000409 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000410 // If we get back the same node we passed in, rather than a new node or
411 // zero, we know that the node must have defined multiple values and
412 // CombineTo was used. Since CombineTo takes care of the worklist
413 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000414 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000415 assert(N->getOpcode() != ISD::DELETED_NODE &&
416 RV.Val->getOpcode() != ISD::DELETED_NODE &&
417 "Node was deleted but visit returned new node!");
418
Jim Laskey6ff23e52006-10-04 16:53:27 +0000419 DEBUG(std::cerr << "\nReplacing.3 "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000420 std::cerr << "\nWith: "; RV.Val->dump(&DAG);
Nate Begeman2300f552005-09-07 00:15:36 +0000421 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000422 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000423 if (N->getNumValues() == RV.Val->getNumValues())
424 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
425 else {
426 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
427 SDOperand OpV = RV;
428 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
429 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000430
431 // Push the new node and any users onto the worklist
Jim Laskey6ff23e52006-10-04 16:53:27 +0000432 AddToWorkList(RV.Val);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000433 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000434
Jim Laskey6ff23e52006-10-04 16:53:27 +0000435 // Nodes can be reintroduced into the worklist. Make sure we do not
436 // process a node that has been replaced.
Nate Begeman646d7e22005-09-02 21:18:40 +0000437 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000438 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
439 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000440
441 // Finally, since the node is now dead, remove it from the graph.
442 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000443 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000444 }
445 }
Chris Lattner95038592005-10-05 06:35:28 +0000446
447 // If the root changed (e.g. it was a dead load, update the root).
448 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000449}
450
Nate Begeman83e75ec2005-09-06 04:43:02 +0000451SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000452 switch(N->getOpcode()) {
453 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000454 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000455 case ISD::ADD: return visitADD(N);
456 case ISD::SUB: return visitSUB(N);
457 case ISD::MUL: return visitMUL(N);
458 case ISD::SDIV: return visitSDIV(N);
459 case ISD::UDIV: return visitUDIV(N);
460 case ISD::SREM: return visitSREM(N);
461 case ISD::UREM: return visitUREM(N);
462 case ISD::MULHU: return visitMULHU(N);
463 case ISD::MULHS: return visitMULHS(N);
464 case ISD::AND: return visitAND(N);
465 case ISD::OR: return visitOR(N);
466 case ISD::XOR: return visitXOR(N);
467 case ISD::SHL: return visitSHL(N);
468 case ISD::SRA: return visitSRA(N);
469 case ISD::SRL: return visitSRL(N);
470 case ISD::CTLZ: return visitCTLZ(N);
471 case ISD::CTTZ: return visitCTTZ(N);
472 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000473 case ISD::SELECT: return visitSELECT(N);
474 case ISD::SELECT_CC: return visitSELECT_CC(N);
475 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000476 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
477 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000478 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000479 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
480 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000481 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000482 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000483 case ISD::FADD: return visitFADD(N);
484 case ISD::FSUB: return visitFSUB(N);
485 case ISD::FMUL: return visitFMUL(N);
486 case ISD::FDIV: return visitFDIV(N);
487 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000488 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000489 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
490 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
491 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
492 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
493 case ISD::FP_ROUND: return visitFP_ROUND(N);
494 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
495 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
496 case ISD::FNEG: return visitFNEG(N);
497 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000498 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000499 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000500 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000501 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000502 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
503 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000504 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000505 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000506 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000507 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
508 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
509 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
510 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
511 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
512 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
513 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
514 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000515 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000516 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000517}
518
Chris Lattner6270f682006-10-08 22:57:01 +0000519/// getInputChainForNode - Given a node, return its input chain if it has one,
520/// otherwise return a null sd operand.
521static SDOperand getInputChainForNode(SDNode *N) {
522 if (unsigned NumOps = N->getNumOperands()) {
523 if (N->getOperand(0).getValueType() == MVT::Other)
524 return N->getOperand(0);
525 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
526 return N->getOperand(NumOps-1);
527 for (unsigned i = 1; i < NumOps-1; ++i)
528 if (N->getOperand(i).getValueType() == MVT::Other)
529 return N->getOperand(i);
530 }
531 return SDOperand(0, 0);
532}
533
Nate Begeman83e75ec2005-09-06 04:43:02 +0000534SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000535 // If N has two operands, where one has an input chain equal to the other,
536 // the 'other' chain is redundant.
537 if (N->getNumOperands() == 2) {
538 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
539 return N->getOperand(0);
540 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
541 return N->getOperand(1);
542 }
543
544
Jim Laskey6ff23e52006-10-04 16:53:27 +0000545 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Jim Laskey279f0532006-09-25 16:29:54 +0000546 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000547 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000548
549 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000550 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000551
Jim Laskey71382342006-10-07 23:37:56 +0000552 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000553 // encountered.
554 for (unsigned i = 0; i < TFs.size(); ++i) {
555 SDNode *TF = TFs[i];
556
Jim Laskey6ff23e52006-10-04 16:53:27 +0000557 // Check each of the operands.
558 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
559 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000560
Jim Laskey6ff23e52006-10-04 16:53:27 +0000561 switch (Op.getOpcode()) {
562 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000563 // Entry tokens don't need to be added to the list. They are
564 // rededundant.
565 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000566 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000567
Jim Laskey6ff23e52006-10-04 16:53:27 +0000568 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000569 if ((CombinerAA || Op.hasOneUse()) &&
570 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000571 // Queue up for processing.
572 TFs.push_back(Op.Val);
573 // Clean up in case the token factor is removed.
574 AddToWorkList(Op.Val);
575 Changed = true;
576 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000577 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000578 // Fall thru
579
580 default:
Jim Laskeybc588b82006-10-05 15:07:25 +0000581 // Only add if not there prior.
582 if (std::find(Ops.begin(), Ops.end(), Op) == Ops.end())
583 Ops.push_back(Op);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000584 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000585 }
586 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000587 }
588
589 SDOperand Result;
590
591 // If we've change things around then replace token factor.
592 if (Changed) {
593 if (Ops.size() == 0) {
594 // The entry token is the only possible outcome.
595 Result = DAG.getEntryNode();
596 } else {
597 // New and improved token factor.
598 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000599 }
600 }
Jim Laskey279f0532006-09-25 16:29:54 +0000601
Jim Laskey6ff23e52006-10-04 16:53:27 +0000602 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000603}
604
Nate Begeman83e75ec2005-09-06 04:43:02 +0000605SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000606 SDOperand N0 = N->getOperand(0);
607 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000608 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
609 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000610 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000611
612 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000613 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000614 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000615 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000616 if (N0C && !N1C)
617 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000618 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000619 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000620 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000621 // fold ((c1-A)+c2) -> (c1+c2)-A
622 if (N1C && N0.getOpcode() == ISD::SUB)
623 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
624 return DAG.getNode(ISD::SUB, VT,
625 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
626 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000627 // reassociate add
628 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
629 if (RADD.Val != 0)
630 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000631 // fold ((0-A) + B) -> B-A
632 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
633 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000634 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000635 // fold (A + (0-B)) -> A-B
636 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
637 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000638 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000639 // fold (A+(B-A)) -> B
640 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000641 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000642
Evan Cheng860771d2006-03-01 01:09:54 +0000643 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000644 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000645
646 // fold (a+b) -> (a|b) iff a and b share no bits.
647 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
648 uint64_t LHSZero, LHSOne;
649 uint64_t RHSZero, RHSOne;
650 uint64_t Mask = MVT::getIntVTBitMask(VT);
651 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
652 if (LHSZero) {
653 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
654
655 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
656 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
657 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
658 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
659 return DAG.getNode(ISD::OR, VT, N0, N1);
660 }
661 }
662
Nate Begeman83e75ec2005-09-06 04:43:02 +0000663 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000664}
665
Nate Begeman83e75ec2005-09-06 04:43:02 +0000666SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000667 SDOperand N0 = N->getOperand(0);
668 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000669 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
670 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000671 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000672
Chris Lattner854077d2005-10-17 01:07:11 +0000673 // fold (sub x, x) -> 0
674 if (N0 == N1)
675 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000676 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000677 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000678 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000679 // fold (sub x, c) -> (add x, -c)
680 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000681 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000682 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000683 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000684 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000685 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000686 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000687 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000688 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000689}
690
Nate Begeman83e75ec2005-09-06 04:43:02 +0000691SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000692 SDOperand N0 = N->getOperand(0);
693 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000694 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
695 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000696 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000697
698 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000699 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000700 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000701 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000702 if (N0C && !N1C)
703 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000704 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000705 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000706 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000707 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000708 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000709 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000710 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000711 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000712 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000713 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000714 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000715 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
716 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
717 // FIXME: If the input is something that is easily negated (e.g. a
718 // single-use add), we should put the negate there.
719 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
720 DAG.getNode(ISD::SHL, VT, N0,
721 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
722 TLI.getShiftAmountTy())));
723 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +0000724
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000725 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
726 if (N1C && N0.getOpcode() == ISD::SHL &&
727 isa<ConstantSDNode>(N0.getOperand(1))) {
728 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000729 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000730 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
731 }
732
733 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
734 // use.
735 {
736 SDOperand Sh(0,0), Y(0,0);
737 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
738 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
739 N0.Val->hasOneUse()) {
740 Sh = N0; Y = N1;
741 } else if (N1.getOpcode() == ISD::SHL &&
742 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
743 Sh = N1; Y = N0;
744 }
745 if (Sh.Val) {
746 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
747 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
748 }
749 }
Chris Lattnera1deca32006-03-04 23:33:26 +0000750 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
751 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
752 isa<ConstantSDNode>(N0.getOperand(1))) {
753 return DAG.getNode(ISD::ADD, VT,
754 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
755 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
756 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000757
Nate Begemancd4d58c2006-02-03 06:46:56 +0000758 // reassociate mul
759 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
760 if (RMUL.Val != 0)
761 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000762 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000763}
764
Nate Begeman83e75ec2005-09-06 04:43:02 +0000765SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000766 SDOperand N0 = N->getOperand(0);
767 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000768 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
769 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000770 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000771
772 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000773 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000774 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000775 // fold (sdiv X, 1) -> X
776 if (N1C && N1C->getSignExtended() == 1LL)
777 return N0;
778 // fold (sdiv X, -1) -> 0-X
779 if (N1C && N1C->isAllOnesValue())
780 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000781 // If we know the sign bits of both operands are zero, strength reduce to a
782 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
783 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000784 if (TLI.MaskedValueIsZero(N1, SignBit) &&
785 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000786 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000787 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000788 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000789 (isPowerOf2_64(N1C->getSignExtended()) ||
790 isPowerOf2_64(-N1C->getSignExtended()))) {
791 // If dividing by powers of two is cheap, then don't perform the following
792 // fold.
793 if (TLI.isPow2DivCheap())
794 return SDOperand();
795 int64_t pow2 = N1C->getSignExtended();
796 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000797 unsigned lg2 = Log2_64(abs2);
798 // Splat the sign bit into the register
799 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000800 DAG.getConstant(MVT::getSizeInBits(VT)-1,
801 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +0000802 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000803 // Add (N0 < 0) ? abs2 - 1 : 0;
804 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
805 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000806 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000807 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +0000808 AddToWorkList(SRL.Val);
809 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +0000810 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
811 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000812 // If we're dividing by a positive value, we're done. Otherwise, we must
813 // negate the result.
814 if (pow2 > 0)
815 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +0000816 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000817 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
818 }
Nate Begeman69575232005-10-20 02:15:44 +0000819 // if integer divide is expensive and we satisfy the requirements, emit an
820 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000821 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000822 !TLI.isIntDivCheap()) {
823 SDOperand Op = BuildSDIV(N);
824 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000825 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000826 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000827}
828
Nate Begeman83e75ec2005-09-06 04:43:02 +0000829SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000830 SDOperand N0 = N->getOperand(0);
831 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000832 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
833 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000834 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000835
836 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000837 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000838 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000839 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000840 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000841 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000842 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000843 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000844 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
845 if (N1.getOpcode() == ISD::SHL) {
846 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
847 if (isPowerOf2_64(SHC->getValue())) {
848 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000849 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
850 DAG.getConstant(Log2_64(SHC->getValue()),
851 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +0000852 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000853 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000854 }
855 }
856 }
Nate Begeman69575232005-10-20 02:15:44 +0000857 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000858 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
859 SDOperand Op = BuildUDIV(N);
860 if (Op.Val) return Op;
861 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000862 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000863}
864
Nate Begeman83e75ec2005-09-06 04:43:02 +0000865SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000866 SDOperand N0 = N->getOperand(0);
867 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000868 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
869 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000870 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000871
872 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000873 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000874 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000875 // If we know the sign bits of both operands are zero, strength reduce to a
876 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
877 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000878 if (TLI.MaskedValueIsZero(N1, SignBit) &&
879 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000880 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000881 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000882}
883
Nate Begeman83e75ec2005-09-06 04:43:02 +0000884SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000885 SDOperand N0 = N->getOperand(0);
886 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000887 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
888 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000889 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000890
891 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000892 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000893 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000894 // fold (urem x, pow2) -> (and x, pow2-1)
895 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000896 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000897 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
898 if (N1.getOpcode() == ISD::SHL) {
899 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
900 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000901 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +0000902 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000903 return DAG.getNode(ISD::AND, VT, N0, Add);
904 }
905 }
906 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000907 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000908}
909
Nate Begeman83e75ec2005-09-06 04:43:02 +0000910SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000911 SDOperand N0 = N->getOperand(0);
912 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000913 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000914
915 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000916 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000917 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000918 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000919 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000920 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
921 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000922 TLI.getShiftAmountTy()));
923 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000924}
925
Nate Begeman83e75ec2005-09-06 04:43:02 +0000926SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000927 SDOperand N0 = N->getOperand(0);
928 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000929 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000930
931 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000932 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000933 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000934 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000935 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000936 return DAG.getConstant(0, N0.getValueType());
937 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000938}
939
Chris Lattner35e5c142006-05-05 05:51:50 +0000940/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
941/// two operands of the same opcode, try to simplify it.
942SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
943 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
944 MVT::ValueType VT = N0.getValueType();
945 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
946
Chris Lattner540121f2006-05-05 06:31:05 +0000947 // For each of OP in AND/OR/XOR:
948 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
949 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
950 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +0000951 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +0000952 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +0000953 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000954 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
955 SDOperand ORNode = DAG.getNode(N->getOpcode(),
956 N0.getOperand(0).getValueType(),
957 N0.getOperand(0), N1.getOperand(0));
958 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +0000959 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +0000960 }
961
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000962 // For each of OP in SHL/SRL/SRA/AND...
963 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
964 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
965 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +0000966 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000967 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000968 N0.getOperand(1) == N1.getOperand(1)) {
969 SDOperand ORNode = DAG.getNode(N->getOpcode(),
970 N0.getOperand(0).getValueType(),
971 N0.getOperand(0), N1.getOperand(0));
972 AddToWorkList(ORNode.Val);
973 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
974 }
975
976 return SDOperand();
977}
978
Nate Begeman83e75ec2005-09-06 04:43:02 +0000979SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000980 SDOperand N0 = N->getOperand(0);
981 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +0000982 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000983 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
984 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000985 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000986 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000987
988 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000989 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000990 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000991 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000992 if (N0C && !N1C)
993 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000994 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000995 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000996 return N0;
997 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +0000998 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000999 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001000 // reassociate and
1001 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1002 if (RAND.Val != 0)
1003 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001004 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001005 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001006 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001007 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001008 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001009 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1010 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001011 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +00001012 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001013 ~N1C->getValue() & InMask)) {
1014 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1015 N0.getOperand(0));
1016
1017 // Replace uses of the AND with uses of the Zero extend node.
1018 CombineTo(N, Zext);
1019
Chris Lattner3603cd62006-02-02 07:17:31 +00001020 // We actually want to replace all uses of the any_extend with the
1021 // zero_extend, to avoid duplicating things. This will later cause this
1022 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001023 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001024 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001025 }
1026 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001027 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1028 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1029 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1030 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1031
1032 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1033 MVT::isInteger(LL.getValueType())) {
1034 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1035 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1036 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001037 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001038 return DAG.getSetCC(VT, ORNode, LR, Op1);
1039 }
1040 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1041 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1042 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001043 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001044 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1045 }
1046 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1047 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1048 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001049 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001050 return DAG.getSetCC(VT, ORNode, LR, Op1);
1051 }
1052 }
1053 // canonicalize equivalent to ll == rl
1054 if (LL == RR && LR == RL) {
1055 Op1 = ISD::getSetCCSwappedOperands(Op1);
1056 std::swap(RL, RR);
1057 }
1058 if (LL == RL && LR == RR) {
1059 bool isInteger = MVT::isInteger(LL.getValueType());
1060 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1061 if (Result != ISD::SETCC_INVALID)
1062 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1063 }
1064 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001065
1066 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1067 if (N0.getOpcode() == N1.getOpcode()) {
1068 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1069 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001070 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001071
Nate Begemande996292006-02-03 22:24:05 +00001072 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1073 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001074 if (!MVT::isVector(VT) &&
1075 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001076 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001077 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Chengc5484282006-10-04 00:56:09 +00001078 if (ISD::isEXTLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001079 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1080 MVT::ValueType EVT = LN0->getLoadVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001081 // If we zero all the possible extended bits, then we can turn this into
1082 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001083 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001084 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001085 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1086 LN0->getBasePtr(), LN0->getSrcValue(),
1087 LN0->getSrcValueOffset(), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001088 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001089 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001090 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001091 }
1092 }
1093 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Chengc5484282006-10-04 00:56:09 +00001094 if (ISD::isSEXTLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001095 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1096 MVT::ValueType EVT = LN0->getLoadVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001097 // If we zero all the possible extended bits, then we can turn this into
1098 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001099 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001100 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001101 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1102 LN0->getBasePtr(), LN0->getSrcValue(),
1103 LN0->getSrcValueOffset(), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001104 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001105 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001106 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001107 }
1108 }
Chris Lattner15045b62006-02-28 06:35:35 +00001109
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001110 // fold (and (load x), 255) -> (zextload x, i8)
1111 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001112 if (N1C && N0.getOpcode() == ISD::LOAD) {
1113 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1114 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
1115 N0.hasOneUse()) {
1116 MVT::ValueType EVT, LoadedVT;
1117 if (N1C->getValue() == 255)
1118 EVT = MVT::i8;
1119 else if (N1C->getValue() == 65535)
1120 EVT = MVT::i16;
1121 else if (N1C->getValue() == ~0U)
1122 EVT = MVT::i32;
1123 else
1124 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001125
Evan Cheng466685d2006-10-09 20:57:25 +00001126 LoadedVT = LN0->getLoadVT();
1127 if (EVT != MVT::Other && LoadedVT > EVT &&
1128 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1129 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1130 // For big endian targets, we need to add an offset to the pointer to
1131 // load the correct bytes. For little endian systems, we merely need to
1132 // read fewer bytes from the same pointer.
1133 unsigned PtrOff =
1134 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1135 SDOperand NewPtr = LN0->getBasePtr();
1136 if (!TLI.isLittleEndian())
1137 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1138 DAG.getConstant(PtrOff, PtrType));
1139 AddToWorkList(NewPtr.Val);
1140 SDOperand Load =
1141 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
1142 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT);
1143 AddToWorkList(N);
1144 CombineTo(N0.Val, Load, Load.getValue(1));
1145 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1146 }
Chris Lattner15045b62006-02-28 06:35:35 +00001147 }
1148 }
1149
Nate Begeman83e75ec2005-09-06 04:43:02 +00001150 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001151}
1152
Nate Begeman83e75ec2005-09-06 04:43:02 +00001153SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001154 SDOperand N0 = N->getOperand(0);
1155 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001156 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001157 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1158 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001159 MVT::ValueType VT = N1.getValueType();
1160 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001161
1162 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001163 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001164 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001165 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001166 if (N0C && !N1C)
1167 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001168 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001169 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001170 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001171 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001172 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001173 return N1;
1174 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001175 if (N1C &&
1176 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001177 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001178 // reassociate or
1179 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1180 if (ROR.Val != 0)
1181 return ROR;
1182 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1183 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001184 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001185 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1186 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1187 N1),
1188 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001189 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001190 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1191 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1192 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1193 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1194
1195 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1196 MVT::isInteger(LL.getValueType())) {
1197 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1198 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1199 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1200 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1201 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001202 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001203 return DAG.getSetCC(VT, ORNode, LR, Op1);
1204 }
1205 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1206 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1207 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1208 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1209 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001210 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001211 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1212 }
1213 }
1214 // canonicalize equivalent to ll == rl
1215 if (LL == RR && LR == RL) {
1216 Op1 = ISD::getSetCCSwappedOperands(Op1);
1217 std::swap(RL, RR);
1218 }
1219 if (LL == RL && LR == RR) {
1220 bool isInteger = MVT::isInteger(LL.getValueType());
1221 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1222 if (Result != ISD::SETCC_INVALID)
1223 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1224 }
1225 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001226
1227 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1228 if (N0.getOpcode() == N1.getOpcode()) {
1229 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1230 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001231 }
Chris Lattner516b9622006-09-14 20:50:57 +00001232
Chris Lattner1ec72732006-09-14 21:11:37 +00001233 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1234 if (N0.getOpcode() == ISD::AND &&
1235 N1.getOpcode() == ISD::AND &&
1236 N0.getOperand(1).getOpcode() == ISD::Constant &&
1237 N1.getOperand(1).getOpcode() == ISD::Constant &&
1238 // Don't increase # computations.
1239 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1240 // We can only do this xform if we know that bits from X that are set in C2
1241 // but not in C1 are already zero. Likewise for Y.
1242 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1243 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1244
1245 if (TLI.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1246 TLI.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
1247 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1248 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1249 }
1250 }
1251
1252
Chris Lattner516b9622006-09-14 20:50:57 +00001253 // See if this is some rotate idiom.
1254 if (SDNode *Rot = MatchRotate(N0, N1))
1255 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001256
Nate Begeman83e75ec2005-09-06 04:43:02 +00001257 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001258}
1259
Chris Lattner516b9622006-09-14 20:50:57 +00001260
1261/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1262static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1263 if (Op.getOpcode() == ISD::AND) {
1264 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1265 Mask = Op.getOperand(1);
1266 Op = Op.getOperand(0);
1267 } else {
1268 return false;
1269 }
1270 }
1271
1272 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1273 Shift = Op;
1274 return true;
1275 }
1276 return false;
1277}
1278
1279
1280// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1281// idioms for rotate, and if the target supports rotation instructions, generate
1282// a rot[lr].
1283SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1284 // Must be a legal type. Expanded an promoted things won't work with rotates.
1285 MVT::ValueType VT = LHS.getValueType();
1286 if (!TLI.isTypeLegal(VT)) return 0;
1287
1288 // The target must have at least one rotate flavor.
1289 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1290 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1291 if (!HasROTL && !HasROTR) return 0;
1292
1293 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1294 SDOperand LHSShift; // The shift.
1295 SDOperand LHSMask; // AND value if any.
1296 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1297 return 0; // Not part of a rotate.
1298
1299 SDOperand RHSShift; // The shift.
1300 SDOperand RHSMask; // AND value if any.
1301 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1302 return 0; // Not part of a rotate.
1303
1304 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1305 return 0; // Not shifting the same value.
1306
1307 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1308 return 0; // Shifts must disagree.
1309
1310 // Canonicalize shl to left side in a shl/srl pair.
1311 if (RHSShift.getOpcode() == ISD::SHL) {
1312 std::swap(LHS, RHS);
1313 std::swap(LHSShift, RHSShift);
1314 std::swap(LHSMask , RHSMask );
1315 }
1316
1317 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1318
1319 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1320 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
1321 if (LHSShift.getOperand(1).getOpcode() == ISD::Constant &&
1322 RHSShift.getOperand(1).getOpcode() == ISD::Constant) {
1323 uint64_t LShVal = cast<ConstantSDNode>(LHSShift.getOperand(1))->getValue();
1324 uint64_t RShVal = cast<ConstantSDNode>(RHSShift.getOperand(1))->getValue();
1325 if ((LShVal + RShVal) != OpSizeInBits)
1326 return 0;
1327
1328 SDOperand Rot;
1329 if (HasROTL)
1330 Rot = DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1331 LHSShift.getOperand(1));
1332 else
1333 Rot = DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1334 RHSShift.getOperand(1));
1335
1336 // If there is an AND of either shifted operand, apply it to the result.
1337 if (LHSMask.Val || RHSMask.Val) {
1338 uint64_t Mask = MVT::getIntVTBitMask(VT);
1339
1340 if (LHSMask.Val) {
1341 uint64_t RHSBits = (1ULL << LShVal)-1;
1342 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1343 }
1344 if (RHSMask.Val) {
1345 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1346 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1347 }
1348
1349 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1350 }
1351
1352 return Rot.Val;
1353 }
1354
1355 // If there is a mask here, and we have a variable shift, we can't be sure
1356 // that we're masking out the right stuff.
1357 if (LHSMask.Val || RHSMask.Val)
1358 return 0;
1359
1360 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1361 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
1362 if (RHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1363 LHSShift.getOperand(1) == RHSShift.getOperand(1).getOperand(1)) {
1364 if (ConstantSDNode *SUBC =
1365 dyn_cast<ConstantSDNode>(RHSShift.getOperand(1).getOperand(0))) {
1366 if (SUBC->getValue() == OpSizeInBits)
1367 if (HasROTL)
1368 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1369 LHSShift.getOperand(1)).Val;
1370 else
1371 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1372 LHSShift.getOperand(1)).Val;
1373 }
1374 }
1375
1376 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1377 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
1378 if (LHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1379 RHSShift.getOperand(1) == LHSShift.getOperand(1).getOperand(1)) {
1380 if (ConstantSDNode *SUBC =
1381 dyn_cast<ConstantSDNode>(LHSShift.getOperand(1).getOperand(0))) {
1382 if (SUBC->getValue() == OpSizeInBits)
1383 if (HasROTL)
1384 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1385 LHSShift.getOperand(1)).Val;
1386 else
1387 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1388 RHSShift.getOperand(1)).Val;
1389 }
1390 }
1391
1392 return 0;
1393}
1394
1395
Nate Begeman83e75ec2005-09-06 04:43:02 +00001396SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001397 SDOperand N0 = N->getOperand(0);
1398 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001399 SDOperand LHS, RHS, CC;
1400 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1401 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001402 MVT::ValueType VT = N0.getValueType();
1403
1404 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001405 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001406 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001407 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001408 if (N0C && !N1C)
1409 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001410 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001411 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001412 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001413 // reassociate xor
1414 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1415 if (RXOR.Val != 0)
1416 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001417 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001418 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1419 bool isInt = MVT::isInteger(LHS.getValueType());
1420 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1421 isInt);
1422 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001423 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001424 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001425 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001426 assert(0 && "Unhandled SetCC Equivalent!");
1427 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001428 }
Nate Begeman99801192005-09-07 23:25:52 +00001429 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1430 if (N1C && N1C->getValue() == 1 &&
1431 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001432 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001433 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1434 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001435 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1436 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001437 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001438 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001439 }
1440 }
Nate Begeman99801192005-09-07 23:25:52 +00001441 // fold !(x or y) -> (!x and !y) iff x or y are constants
1442 if (N1C && N1C->isAllOnesValue() &&
1443 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001444 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001445 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1446 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001447 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1448 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001449 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001450 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001451 }
1452 }
Nate Begeman223df222005-09-08 20:18:10 +00001453 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1454 if (N1C && N0.getOpcode() == ISD::XOR) {
1455 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1456 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1457 if (N00C)
1458 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1459 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1460 if (N01C)
1461 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1462 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1463 }
1464 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001465 if (N0 == N1) {
1466 if (!MVT::isVector(VT)) {
1467 return DAG.getConstant(0, VT);
1468 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1469 // Produce a vector of zeros.
1470 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1471 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001472 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001473 }
1474 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001475
1476 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1477 if (N0.getOpcode() == N1.getOpcode()) {
1478 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1479 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001480 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001481
Chris Lattner3e104b12006-04-08 04:15:24 +00001482 // Simplify the expression using non-local knowledge.
1483 if (!MVT::isVector(VT) &&
1484 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001485 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001486
Nate Begeman83e75ec2005-09-06 04:43:02 +00001487 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001488}
1489
Nate Begeman83e75ec2005-09-06 04:43:02 +00001490SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001491 SDOperand N0 = N->getOperand(0);
1492 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001493 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1494 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001495 MVT::ValueType VT = N0.getValueType();
1496 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1497
1498 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001499 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001500 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001501 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001502 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001503 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001504 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001505 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001506 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001507 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001508 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001509 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001510 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001511 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001512 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001513 if (SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001514 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001515 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001516 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001517 N0.getOperand(1).getOpcode() == ISD::Constant) {
1518 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001519 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001520 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001521 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001522 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001523 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001524 }
1525 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1526 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001527 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001528 N0.getOperand(1).getOpcode() == ISD::Constant) {
1529 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001530 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001531 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1532 DAG.getConstant(~0ULL << c1, VT));
1533 if (c2 > c1)
1534 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001535 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001536 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001537 return DAG.getNode(ISD::SRL, VT, Mask,
1538 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001539 }
1540 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001541 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001542 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001543 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnercac70592006-03-05 19:53:55 +00001544 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1545 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1546 isa<ConstantSDNode>(N0.getOperand(1))) {
1547 return DAG.getNode(ISD::ADD, VT,
1548 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1549 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1550 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001551 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001552}
1553
Nate Begeman83e75ec2005-09-06 04:43:02 +00001554SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001555 SDOperand N0 = N->getOperand(0);
1556 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001557 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1558 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001559 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001560
1561 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001562 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001563 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001564 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001565 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001566 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001567 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001568 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001569 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001570 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001571 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001572 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001573 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001574 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001575 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001576 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1577 // sext_inreg.
1578 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1579 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1580 MVT::ValueType EVT;
1581 switch (LowBits) {
1582 default: EVT = MVT::Other; break;
1583 case 1: EVT = MVT::i1; break;
1584 case 8: EVT = MVT::i8; break;
1585 case 16: EVT = MVT::i16; break;
1586 case 32: EVT = MVT::i32; break;
1587 }
1588 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1589 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1590 DAG.getValueType(EVT));
1591 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001592
1593 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1594 if (N1C && N0.getOpcode() == ISD::SRA) {
1595 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1596 unsigned Sum = N1C->getValue() + C1->getValue();
1597 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1598 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1599 DAG.getConstant(Sum, N1C->getValueType(0)));
1600 }
1601 }
1602
Chris Lattnera8504462006-05-08 20:51:54 +00001603 // Simplify, based on bits shifted out of the LHS.
1604 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
1605 return SDOperand(N, 0);
1606
1607
Nate Begeman1d4d4142005-09-01 00:19:25 +00001608 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001609 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001610 return DAG.getNode(ISD::SRL, VT, N0, N1);
1611 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001612}
1613
Nate Begeman83e75ec2005-09-06 04:43:02 +00001614SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001615 SDOperand N0 = N->getOperand(0);
1616 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001617 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1618 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001619 MVT::ValueType VT = N0.getValueType();
1620 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1621
1622 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001623 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001624 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001625 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001626 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001627 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001628 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001629 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001630 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001631 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001632 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001633 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001634 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001635 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001636 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001637 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001638 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001639 N0.getOperand(1).getOpcode() == ISD::Constant) {
1640 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001641 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001642 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001643 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001644 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001645 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001646 }
Chris Lattner350bec02006-04-02 06:11:11 +00001647
Chris Lattner06afe072006-05-05 22:53:17 +00001648 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
1649 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1650 // Shifting in all undef bits?
1651 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
1652 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
1653 return DAG.getNode(ISD::UNDEF, VT);
1654
1655 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
1656 AddToWorkList(SmallShift.Val);
1657 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
1658 }
1659
Chris Lattner350bec02006-04-02 06:11:11 +00001660 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1661 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1662 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1663 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1664 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
1665
1666 // If any of the input bits are KnownOne, then the input couldn't be all
1667 // zeros, thus the result of the srl will always be zero.
1668 if (KnownOne) return DAG.getConstant(0, VT);
1669
1670 // If all of the bits input the to ctlz node are known to be zero, then
1671 // the result of the ctlz is "32" and the result of the shift is one.
1672 uint64_t UnknownBits = ~KnownZero & Mask;
1673 if (UnknownBits == 0) return DAG.getConstant(1, VT);
1674
1675 // Otherwise, check to see if there is exactly one bit input to the ctlz.
1676 if ((UnknownBits & (UnknownBits-1)) == 0) {
1677 // Okay, we know that only that the single bit specified by UnknownBits
1678 // could be set on input to the CTLZ node. If this bit is set, the SRL
1679 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
1680 // to an SRL,XOR pair, which is likely to simplify more.
1681 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
1682 SDOperand Op = N0.getOperand(0);
1683 if (ShAmt) {
1684 Op = DAG.getNode(ISD::SRL, VT, Op,
1685 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
1686 AddToWorkList(Op.Val);
1687 }
1688 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
1689 }
1690 }
1691
Nate Begeman83e75ec2005-09-06 04:43:02 +00001692 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001693}
1694
Nate Begeman83e75ec2005-09-06 04:43:02 +00001695SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001696 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001697 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001698
1699 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001700 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001701 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001702 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001703}
1704
Nate Begeman83e75ec2005-09-06 04:43:02 +00001705SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001706 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001707 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001708
1709 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001710 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001711 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001712 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001713}
1714
Nate Begeman83e75ec2005-09-06 04:43:02 +00001715SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001716 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001717 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001718
1719 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001720 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001721 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001722 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001723}
1724
Nate Begeman452d7be2005-09-16 00:54:12 +00001725SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1726 SDOperand N0 = N->getOperand(0);
1727 SDOperand N1 = N->getOperand(1);
1728 SDOperand N2 = N->getOperand(2);
1729 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1730 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1731 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1732 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001733
Nate Begeman452d7be2005-09-16 00:54:12 +00001734 // fold select C, X, X -> X
1735 if (N1 == N2)
1736 return N1;
1737 // fold select true, X, Y -> X
1738 if (N0C && !N0C->isNullValue())
1739 return N1;
1740 // fold select false, X, Y -> Y
1741 if (N0C && N0C->isNullValue())
1742 return N2;
1743 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001744 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001745 return DAG.getNode(ISD::OR, VT, N0, N2);
1746 // fold select C, 0, X -> ~C & X
1747 // FIXME: this should check for C type == X type, not i1?
1748 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1749 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001750 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001751 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1752 }
1753 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001754 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001755 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001756 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001757 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1758 }
1759 // fold select C, X, 0 -> C & X
1760 // FIXME: this should check for C type == X type, not i1?
1761 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1762 return DAG.getNode(ISD::AND, VT, N0, N1);
1763 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1764 if (MVT::i1 == VT && N0 == N1)
1765 return DAG.getNode(ISD::OR, VT, N0, N2);
1766 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1767 if (MVT::i1 == VT && N0 == N2)
1768 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00001769
Chris Lattner40c62d52005-10-18 06:04:22 +00001770 // If we can fold this based on the true/false value, do so.
1771 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00001772 return SDOperand(N, 0); // Don't revisit N.
1773
Nate Begeman44728a72005-09-19 22:34:01 +00001774 // fold selects based on a setcc into other things, such as min/max/abs
1775 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001776 // FIXME:
1777 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1778 // having to say they don't support SELECT_CC on every type the DAG knows
1779 // about, since there is no way to mark an opcode illegal at all value types
1780 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1781 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1782 N1, N2, N0.getOperand(2));
1783 else
1784 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001785 return SDOperand();
1786}
1787
1788SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001789 SDOperand N0 = N->getOperand(0);
1790 SDOperand N1 = N->getOperand(1);
1791 SDOperand N2 = N->getOperand(2);
1792 SDOperand N3 = N->getOperand(3);
1793 SDOperand N4 = N->getOperand(4);
1794 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1795 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1796 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1797 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1798
Nate Begeman44728a72005-09-19 22:34:01 +00001799 // fold select_cc lhs, rhs, x, x, cc -> x
1800 if (N2 == N3)
1801 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001802
Chris Lattner5f42a242006-09-20 06:19:26 +00001803 // Determine if the condition we're dealing with is constant
1804 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
1805
1806 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
1807 if (SCCC->getValue())
1808 return N2; // cond always true -> true val
1809 else
1810 return N3; // cond always false -> false val
1811 }
1812
1813 // Fold to a simpler select_cc
1814 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
1815 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
1816 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
1817 SCC.getOperand(2));
1818
Chris Lattner40c62d52005-10-18 06:04:22 +00001819 // If we can fold this based on the true/false value, do so.
1820 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00001821 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00001822
Nate Begeman44728a72005-09-19 22:34:01 +00001823 // fold select_cc into other things, such as min/max/abs
1824 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001825}
1826
1827SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1828 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1829 cast<CondCodeSDNode>(N->getOperand(2))->get());
1830}
1831
Nate Begeman83e75ec2005-09-06 04:43:02 +00001832SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001833 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001834 MVT::ValueType VT = N->getValueType(0);
1835
Nate Begeman1d4d4142005-09-01 00:19:25 +00001836 // fold (sext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001837 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001838 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00001839
Nate Begeman1d4d4142005-09-01 00:19:25 +00001840 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001841 // fold (sext (aext x)) -> (sext x)
1842 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001843 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00001844
Chris Lattner6007b842006-09-21 06:00:20 +00001845 // fold (sext (truncate x)) -> (sextinreg x).
1846 if (N0.getOpcode() == ISD::TRUNCATE &&
Chris Lattnerbf370872006-09-21 06:17:39 +00001847 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
1848 N0.getValueType()))) {
Chris Lattner6007b842006-09-21 06:00:20 +00001849 SDOperand Op = N0.getOperand(0);
1850 if (Op.getValueType() < VT) {
1851 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
1852 } else if (Op.getValueType() > VT) {
1853 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
1854 }
1855 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001856 DAG.getValueType(N0.getValueType()));
Chris Lattner6007b842006-09-21 06:00:20 +00001857 }
Chris Lattner310b5782006-05-06 23:06:26 +00001858
Evan Cheng110dec22005-12-14 02:19:23 +00001859 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00001860 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00001861 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng466685d2006-10-09 20:57:25 +00001862 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1863 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
1864 LN0->getBasePtr(), LN0->getSrcValue(),
1865 LN0->getSrcValueOffset(),
Nate Begeman3df4d522005-10-12 20:40:40 +00001866 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001867 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001868 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1869 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001870 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00001871 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001872
1873 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1874 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Chengc5484282006-10-04 00:56:09 +00001875 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001876 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1877 MVT::ValueType EVT = LN0->getLoadVT();
1878 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
1879 LN0->getBasePtr(), LN0->getSrcValue(),
1880 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001881 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001882 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1883 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001884 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001885 }
1886
Nate Begeman83e75ec2005-09-06 04:43:02 +00001887 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001888}
1889
Nate Begeman83e75ec2005-09-06 04:43:02 +00001890SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001891 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001892 MVT::ValueType VT = N->getValueType(0);
1893
Nate Begeman1d4d4142005-09-01 00:19:25 +00001894 // fold (zext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001895 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001896 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001897 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001898 // fold (zext (aext x)) -> (zext x)
1899 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001900 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00001901
1902 // fold (zext (truncate x)) -> (and x, mask)
1903 if (N0.getOpcode() == ISD::TRUNCATE &&
1904 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
1905 SDOperand Op = N0.getOperand(0);
1906 if (Op.getValueType() < VT) {
1907 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
1908 } else if (Op.getValueType() > VT) {
1909 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
1910 }
1911 return DAG.getZeroExtendInReg(Op, N0.getValueType());
1912 }
1913
Chris Lattner111c2282006-09-21 06:14:31 +00001914 // fold (zext (and (trunc x), cst)) -> (and x, cst).
1915 if (N0.getOpcode() == ISD::AND &&
1916 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1917 N0.getOperand(1).getOpcode() == ISD::Constant) {
1918 SDOperand X = N0.getOperand(0).getOperand(0);
1919 if (X.getValueType() < VT) {
1920 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
1921 } else if (X.getValueType() > VT) {
1922 X = DAG.getNode(ISD::TRUNCATE, VT, X);
1923 }
1924 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1925 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
1926 }
1927
Evan Cheng110dec22005-12-14 02:19:23 +00001928 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00001929 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00001930 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001931 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1932 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1933 LN0->getBasePtr(), LN0->getSrcValue(),
1934 LN0->getSrcValueOffset(),
Evan Cheng110dec22005-12-14 02:19:23 +00001935 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001936 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001937 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1938 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001939 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00001940 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001941
1942 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1943 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Chengc5484282006-10-04 00:56:09 +00001944 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001945 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1946 MVT::ValueType EVT = LN0->getLoadVT();
1947 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1948 LN0->getBasePtr(), LN0->getSrcValue(),
1949 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001950 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001951 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1952 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001953 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001954 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001955 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001956}
1957
Chris Lattner5ffc0662006-05-05 05:58:59 +00001958SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
1959 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001960 MVT::ValueType VT = N->getValueType(0);
1961
1962 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001963 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00001964 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
1965 // fold (aext (aext x)) -> (aext x)
1966 // fold (aext (zext x)) -> (zext x)
1967 // fold (aext (sext x)) -> (sext x)
1968 if (N0.getOpcode() == ISD::ANY_EXTEND ||
1969 N0.getOpcode() == ISD::ZERO_EXTEND ||
1970 N0.getOpcode() == ISD::SIGN_EXTEND)
1971 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
1972
Chris Lattner84750582006-09-20 06:29:17 +00001973 // fold (aext (truncate x))
1974 if (N0.getOpcode() == ISD::TRUNCATE) {
1975 SDOperand TruncOp = N0.getOperand(0);
1976 if (TruncOp.getValueType() == VT)
1977 return TruncOp; // x iff x size == zext size.
1978 if (TruncOp.getValueType() > VT)
1979 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
1980 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
1981 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00001982
1983 // fold (aext (and (trunc x), cst)) -> (and x, cst).
1984 if (N0.getOpcode() == ISD::AND &&
1985 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1986 N0.getOperand(1).getOpcode() == ISD::Constant) {
1987 SDOperand X = N0.getOperand(0).getOperand(0);
1988 if (X.getValueType() < VT) {
1989 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
1990 } else if (X.getValueType() > VT) {
1991 X = DAG.getNode(ISD::TRUNCATE, VT, X);
1992 }
1993 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1994 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
1995 }
1996
Chris Lattner5ffc0662006-05-05 05:58:59 +00001997 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00001998 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00001999 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002000 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2001 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2002 LN0->getBasePtr(), LN0->getSrcValue(),
2003 LN0->getSrcValueOffset(),
Chris Lattner5ffc0662006-05-05 05:58:59 +00002004 N0.getValueType());
2005 CombineTo(N, ExtLoad);
2006 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2007 ExtLoad.getValue(1));
2008 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2009 }
2010
2011 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2012 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2013 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002014 if (N0.getOpcode() == ISD::LOAD && !ISD::isNON_EXTLoad(N0.Val) &&
2015 N0.hasOneUse()) {
2016 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2017 MVT::ValueType EVT = LN0->getLoadVT();
2018 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
2019 LN0->getChain(), LN0->getBasePtr(),
2020 LN0->getSrcValue(),
2021 LN0->getSrcValueOffset(), EVT);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002022 CombineTo(N, ExtLoad);
2023 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2024 ExtLoad.getValue(1));
2025 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2026 }
2027 return SDOperand();
2028}
2029
2030
Nate Begeman83e75ec2005-09-06 04:43:02 +00002031SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002032 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002033 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002034 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002035 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00002036 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002037
Nate Begeman1d4d4142005-09-01 00:19:25 +00002038 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00002039 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00002040 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00002041
Chris Lattner541a24f2006-05-06 22:43:44 +00002042 // If the input is already sign extended, just drop the extension.
Chris Lattneree4ea922006-05-06 09:30:03 +00002043 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
2044 return N0;
2045
Nate Begeman646d7e22005-09-02 21:18:40 +00002046 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
2047 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2048 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00002049 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002050 }
Chris Lattner4b37e872006-05-08 21:18:59 +00002051
Nate Begeman07ed4172005-10-10 21:26:48 +00002052 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00002053 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00002054 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00002055
2056 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
2057 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
2058 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
2059 if (N0.getOpcode() == ISD::SRL) {
2060 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2061 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
2062 // We can turn this into an SRA iff the input to the SRL is already sign
2063 // extended enough.
2064 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
2065 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
2066 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
2067 }
2068 }
2069
Nate Begemanded49632005-10-13 03:11:28 +00002070 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00002071 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00002072 EVT == cast<LoadSDNode>(N0)->getLoadVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002073 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002074 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2075 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2076 LN0->getBasePtr(), LN0->getSrcValue(),
2077 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002078 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002079 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002080 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002081 }
2082 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Chengc5484282006-10-04 00:56:09 +00002083 if (ISD::isZEXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Cheng466685d2006-10-09 20:57:25 +00002084 EVT == cast<LoadSDNode>(N0)->getLoadVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002085 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002086 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2087 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2088 LN0->getBasePtr(), LN0->getSrcValue(),
2089 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002090 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002091 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002092 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002093 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002094 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002095}
2096
Nate Begeman83e75ec2005-09-06 04:43:02 +00002097SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002098 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002099 MVT::ValueType VT = N->getValueType(0);
2100
2101 // noop truncate
2102 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002103 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002104 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002105 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002106 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002107 // fold (truncate (truncate x)) -> (truncate x)
2108 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002109 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002110 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00002111 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2112 N0.getOpcode() == ISD::ANY_EXTEND) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002113 if (N0.getValueType() < VT)
2114 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00002115 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002116 else if (N0.getValueType() > VT)
2117 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002118 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002119 else
2120 // if the source and dest are the same type, we can drop both the extend
2121 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002122 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002123 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002124 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng466685d2006-10-09 20:57:25 +00002125 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00002126 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
2127 "Cannot truncate to larger type!");
Evan Cheng466685d2006-10-09 20:57:25 +00002128 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Nate Begeman3df4d522005-10-12 20:40:40 +00002129 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00002130 // For big endian targets, we need to add an offset to the pointer to load
2131 // the correct bytes. For little endian systems, we merely need to read
2132 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00002133 uint64_t PtrOff =
2134 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Evan Cheng466685d2006-10-09 20:57:25 +00002135 SDOperand NewPtr = TLI.isLittleEndian() ? LN0->getBasePtr() :
2136 DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Nate Begeman765784a2005-10-12 23:18:53 +00002137 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00002138 AddToWorkList(NewPtr.Val);
Evan Cheng466685d2006-10-09 20:57:25 +00002139 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), NewPtr,
2140 LN0->getSrcValue(), LN0->getSrcValueOffset());
Chris Lattner5750df92006-03-01 04:03:14 +00002141 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00002142 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002143 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002144 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002145 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002146}
2147
Chris Lattner94683772005-12-23 05:30:37 +00002148SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2149 SDOperand N0 = N->getOperand(0);
2150 MVT::ValueType VT = N->getValueType(0);
2151
2152 // If the input is a constant, let getNode() fold it.
2153 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2154 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2155 if (Res.Val != N) return Res;
2156 }
2157
Chris Lattnerc8547d82005-12-23 05:37:50 +00002158 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2159 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00002160
Chris Lattner57104102005-12-23 05:44:41 +00002161 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002162 // FIXME: These xforms need to know that the resultant load doesn't need a
2163 // higher alignment than the original!
Evan Cheng466685d2006-10-09 20:57:25 +00002164 if (0 && ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse()) {
2165 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2166 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
2167 LN0->getSrcValue(), LN0->getSrcValueOffset());
Chris Lattner5750df92006-03-01 04:03:14 +00002168 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00002169 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2170 Load.getValue(1));
2171 return Load;
2172 }
2173
Chris Lattner94683772005-12-23 05:30:37 +00002174 return SDOperand();
2175}
2176
Chris Lattner6258fb22006-04-02 02:53:43 +00002177SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2178 SDOperand N0 = N->getOperand(0);
2179 MVT::ValueType VT = N->getValueType(0);
2180
2181 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2182 // First check to see if this is all constant.
2183 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2184 VT == MVT::Vector) {
2185 bool isSimple = true;
2186 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2187 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2188 N0.getOperand(i).getOpcode() != ISD::Constant &&
2189 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2190 isSimple = false;
2191 break;
2192 }
2193
Chris Lattner97c20732006-04-03 17:29:28 +00002194 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2195 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002196 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2197 }
2198 }
2199
2200 return SDOperand();
2201}
2202
2203/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2204/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2205/// destination element value type.
2206SDOperand DAGCombiner::
2207ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2208 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2209
2210 // If this is already the right type, we're done.
2211 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2212
2213 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2214 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2215
2216 // If this is a conversion of N elements of one type to N elements of another
2217 // type, convert each element. This handles FP<->INT cases.
2218 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002219 SmallVector<SDOperand, 8> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00002220 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002221 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002222 AddToWorkList(Ops.back().Val);
2223 }
Chris Lattner6258fb22006-04-02 02:53:43 +00002224 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2225 Ops.push_back(DAG.getValueType(DstEltVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002226 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002227 }
2228
2229 // Otherwise, we're growing or shrinking the elements. To avoid having to
2230 // handle annoying details of growing/shrinking FP values, we convert them to
2231 // int first.
2232 if (MVT::isFloatingPoint(SrcEltVT)) {
2233 // Convert the input float vector to a int vector where the elements are the
2234 // same sizes.
2235 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2236 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2237 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2238 SrcEltVT = IntVT;
2239 }
2240
2241 // Now we know the input is an integer vector. If the output is a FP type,
2242 // convert to integer first, then to FP of the right size.
2243 if (MVT::isFloatingPoint(DstEltVT)) {
2244 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2245 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2246 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2247
2248 // Next, convert to FP elements of the same size.
2249 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2250 }
2251
2252 // Okay, we know the src/dst types are both integers of differing types.
2253 // Handling growing first.
2254 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2255 if (SrcBitSize < DstBitSize) {
2256 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2257
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002258 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002259 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2260 i += NumInputsPerOutput) {
2261 bool isLE = TLI.isLittleEndian();
2262 uint64_t NewBits = 0;
2263 bool EltIsUndef = true;
2264 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2265 // Shift the previously computed bits over.
2266 NewBits <<= SrcBitSize;
2267 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2268 if (Op.getOpcode() == ISD::UNDEF) continue;
2269 EltIsUndef = false;
2270
2271 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2272 }
2273
2274 if (EltIsUndef)
2275 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2276 else
2277 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2278 }
2279
2280 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2281 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002282 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002283 }
2284
2285 // Finally, this must be the case where we are shrinking elements: each input
2286 // turns into multiple outputs.
2287 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002288 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002289 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2290 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2291 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2292 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2293 continue;
2294 }
2295 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2296
2297 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2298 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2299 OpVal >>= DstBitSize;
2300 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2301 }
2302
2303 // For big endian targets, swap the order of the pieces of each element.
2304 if (!TLI.isLittleEndian())
2305 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2306 }
2307 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2308 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002309 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002310}
2311
2312
2313
Chris Lattner01b3d732005-09-28 22:28:18 +00002314SDOperand DAGCombiner::visitFADD(SDNode *N) {
2315 SDOperand N0 = N->getOperand(0);
2316 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002317 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2318 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002319 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002320
2321 // fold (fadd c1, c2) -> c1+c2
2322 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002323 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002324 // canonicalize constant to RHS
2325 if (N0CFP && !N1CFP)
2326 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002327 // fold (A + (-B)) -> A-B
2328 if (N1.getOpcode() == ISD::FNEG)
2329 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002330 // fold ((-A) + B) -> B-A
2331 if (N0.getOpcode() == ISD::FNEG)
2332 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002333 return SDOperand();
2334}
2335
2336SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2337 SDOperand N0 = N->getOperand(0);
2338 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002339 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2340 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002341 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002342
2343 // fold (fsub c1, c2) -> c1-c2
2344 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002345 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002346 // fold (A-(-B)) -> A+B
2347 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00002348 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002349 return SDOperand();
2350}
2351
2352SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2353 SDOperand N0 = N->getOperand(0);
2354 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002355 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2356 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002357 MVT::ValueType VT = N->getValueType(0);
2358
Nate Begeman11af4ea2005-10-17 20:40:11 +00002359 // fold (fmul c1, c2) -> c1*c2
2360 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002361 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002362 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002363 if (N0CFP && !N1CFP)
2364 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002365 // fold (fmul X, 2.0) -> (fadd X, X)
2366 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2367 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002368 return SDOperand();
2369}
2370
2371SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2372 SDOperand N0 = N->getOperand(0);
2373 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002374 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2375 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002376 MVT::ValueType VT = N->getValueType(0);
2377
Nate Begemana148d982006-01-18 22:35:16 +00002378 // fold (fdiv c1, c2) -> c1/c2
2379 if (N0CFP && N1CFP)
2380 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002381 return SDOperand();
2382}
2383
2384SDOperand DAGCombiner::visitFREM(SDNode *N) {
2385 SDOperand N0 = N->getOperand(0);
2386 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002387 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2388 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002389 MVT::ValueType VT = N->getValueType(0);
2390
Nate Begemana148d982006-01-18 22:35:16 +00002391 // fold (frem c1, c2) -> fmod(c1,c2)
2392 if (N0CFP && N1CFP)
2393 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002394 return SDOperand();
2395}
2396
Chris Lattner12d83032006-03-05 05:30:57 +00002397SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2398 SDOperand N0 = N->getOperand(0);
2399 SDOperand N1 = N->getOperand(1);
2400 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2401 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2402 MVT::ValueType VT = N->getValueType(0);
2403
2404 if (N0CFP && N1CFP) // Constant fold
2405 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2406
2407 if (N1CFP) {
2408 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2409 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2410 union {
2411 double d;
2412 int64_t i;
2413 } u;
2414 u.d = N1CFP->getValue();
2415 if (u.i >= 0)
2416 return DAG.getNode(ISD::FABS, VT, N0);
2417 else
2418 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2419 }
2420
2421 // copysign(fabs(x), y) -> copysign(x, y)
2422 // copysign(fneg(x), y) -> copysign(x, y)
2423 // copysign(copysign(x,z), y) -> copysign(x, y)
2424 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2425 N0.getOpcode() == ISD::FCOPYSIGN)
2426 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2427
2428 // copysign(x, abs(y)) -> abs(x)
2429 if (N1.getOpcode() == ISD::FABS)
2430 return DAG.getNode(ISD::FABS, VT, N0);
2431
2432 // copysign(x, copysign(y,z)) -> copysign(x, z)
2433 if (N1.getOpcode() == ISD::FCOPYSIGN)
2434 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2435
2436 // copysign(x, fp_extend(y)) -> copysign(x, y)
2437 // copysign(x, fp_round(y)) -> copysign(x, y)
2438 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2439 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2440
2441 return SDOperand();
2442}
2443
2444
Chris Lattner01b3d732005-09-28 22:28:18 +00002445
Nate Begeman83e75ec2005-09-06 04:43:02 +00002446SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002447 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002448 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002449 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002450
2451 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002452 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002453 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002454 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002455}
2456
Nate Begeman83e75ec2005-09-06 04:43:02 +00002457SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002458 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002459 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002460 MVT::ValueType VT = N->getValueType(0);
2461
Nate Begeman1d4d4142005-09-01 00:19:25 +00002462 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002463 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002464 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002465 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002466}
2467
Nate Begeman83e75ec2005-09-06 04:43:02 +00002468SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002469 SDOperand N0 = N->getOperand(0);
2470 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2471 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002472
2473 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002474 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002475 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002476 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002477}
2478
Nate Begeman83e75ec2005-09-06 04:43:02 +00002479SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002480 SDOperand N0 = N->getOperand(0);
2481 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2482 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002483
2484 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002485 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002486 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002487 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002488}
2489
Nate Begeman83e75ec2005-09-06 04:43:02 +00002490SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002491 SDOperand N0 = N->getOperand(0);
2492 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2493 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002494
2495 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002496 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002497 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002498
2499 // fold (fp_round (fp_extend x)) -> x
2500 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2501 return N0.getOperand(0);
2502
2503 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2504 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2505 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2506 AddToWorkList(Tmp.Val);
2507 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2508 }
2509
Nate Begeman83e75ec2005-09-06 04:43:02 +00002510 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002511}
2512
Nate Begeman83e75ec2005-09-06 04:43:02 +00002513SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002514 SDOperand N0 = N->getOperand(0);
2515 MVT::ValueType VT = N->getValueType(0);
2516 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002517 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002518
Nate Begeman1d4d4142005-09-01 00:19:25 +00002519 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002520 if (N0CFP) {
2521 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002522 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002523 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002524 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002525}
2526
Nate Begeman83e75ec2005-09-06 04:43:02 +00002527SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002528 SDOperand N0 = N->getOperand(0);
2529 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2530 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002531
2532 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002533 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002534 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00002535
2536 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002537 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002538 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002539 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2540 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2541 LN0->getBasePtr(), LN0->getSrcValue(),
2542 LN0->getSrcValueOffset(),
Chris Lattnere564dbb2006-05-05 21:34:35 +00002543 N0.getValueType());
2544 CombineTo(N, ExtLoad);
2545 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
2546 ExtLoad.getValue(1));
2547 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2548 }
2549
2550
Nate Begeman83e75ec2005-09-06 04:43:02 +00002551 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002552}
2553
Nate Begeman83e75ec2005-09-06 04:43:02 +00002554SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002555 SDOperand N0 = N->getOperand(0);
2556 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2557 MVT::ValueType VT = N->getValueType(0);
2558
2559 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002560 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002561 return DAG.getNode(ISD::FNEG, VT, N0);
2562 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002563 if (N0.getOpcode() == ISD::SUB)
2564 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002565 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002566 if (N0.getOpcode() == ISD::FNEG)
2567 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002568 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002569}
2570
Nate Begeman83e75ec2005-09-06 04:43:02 +00002571SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002572 SDOperand N0 = N->getOperand(0);
2573 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2574 MVT::ValueType VT = N->getValueType(0);
2575
Nate Begeman1d4d4142005-09-01 00:19:25 +00002576 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002577 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002578 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002579 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002580 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002581 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002582 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002583 // fold (fabs (fcopysign x, y)) -> (fabs x)
2584 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2585 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2586
Nate Begeman83e75ec2005-09-06 04:43:02 +00002587 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002588}
2589
Nate Begeman44728a72005-09-19 22:34:01 +00002590SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2591 SDOperand Chain = N->getOperand(0);
2592 SDOperand N1 = N->getOperand(1);
2593 SDOperand N2 = N->getOperand(2);
2594 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2595
2596 // never taken branch, fold to chain
2597 if (N1C && N1C->isNullValue())
2598 return Chain;
2599 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002600 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002601 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002602 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2603 // on the target.
2604 if (N1.getOpcode() == ISD::SETCC &&
2605 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2606 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2607 N1.getOperand(0), N1.getOperand(1), N2);
2608 }
Nate Begeman44728a72005-09-19 22:34:01 +00002609 return SDOperand();
2610}
2611
Chris Lattner3ea0b472005-10-05 06:47:48 +00002612// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2613//
Nate Begeman44728a72005-09-19 22:34:01 +00002614SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002615 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2616 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2617
2618 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002619 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2620 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2621
2622 // fold br_cc true, dest -> br dest (unconditional branch)
2623 if (SCCC && SCCC->getValue())
2624 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2625 N->getOperand(4));
2626 // fold br_cc false, dest -> unconditional fall through
2627 if (SCCC && SCCC->isNullValue())
2628 return N->getOperand(0);
2629 // fold to a simpler setcc
2630 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2631 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2632 Simp.getOperand(2), Simp.getOperand(0),
2633 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002634 return SDOperand();
2635}
2636
Chris Lattner01a22022005-10-10 22:04:48 +00002637SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00002638 LoadSDNode *LD = cast<LoadSDNode>(N);
2639 SDOperand Chain = LD->getChain();
2640 SDOperand Ptr = LD->getBasePtr();
Jim Laskey6ff23e52006-10-04 16:53:27 +00002641
Chris Lattnere4b95392006-03-31 18:06:18 +00002642 // If there are no uses of the loaded value, change uses of the chain value
2643 // into uses of the chain input (i.e. delete the dead load).
2644 if (N->hasNUsesOfValue(0, 0))
2645 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
Chris Lattner01a22022005-10-10 22:04:48 +00002646
Evan Cheng466685d2006-10-09 20:57:25 +00002647 if (!ISD::isNON_EXTLoad(N))
2648 return SDOperand();
2649
Chris Lattner01a22022005-10-10 22:04:48 +00002650 // If this load is directly stored, replace the load value with the stored
2651 // value.
2652 // TODO: Handle store large -> read small portion.
2653 // TODO: Handle TRUNCSTORE/EXTLOAD
2654 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2655 Chain.getOperand(1).getValueType() == N->getValueType(0))
2656 return CombineTo(N, Chain.getOperand(1), Chain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00002657
Jim Laskeybb151852006-09-26 17:44:58 +00002658 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00002659 // Walk up chain skipping non-aliasing memory nodes.
2660 SDOperand BetterChain = FindBetterChain(N, Chain);
2661
Jim Laskey6ff23e52006-10-04 16:53:27 +00002662 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00002663 if (Chain != BetterChain) {
2664 // Replace the chain to void dependency.
2665 SDOperand ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00002666 LD->getSrcValue(), LD->getSrcValueOffset());
Jim Laskey279f0532006-09-25 16:29:54 +00002667
Jim Laskey6ff23e52006-10-04 16:53:27 +00002668 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00002669 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
2670 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00002671
2672 // Replace uses with load result and token factor.
2673 return CombineTo(N, ReplLoad.getValue(0), Token);
Jim Laskey279f0532006-09-25 16:29:54 +00002674 }
2675 }
2676
Chris Lattner01a22022005-10-10 22:04:48 +00002677 return SDOperand();
2678}
2679
Chris Lattner87514ca2005-10-10 22:31:19 +00002680SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2681 SDOperand Chain = N->getOperand(0);
2682 SDOperand Value = N->getOperand(1);
2683 SDOperand Ptr = N->getOperand(2);
2684 SDOperand SrcValue = N->getOperand(3);
2685
2686 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002687 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002688 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2689 // Make sure that these stores are the same value type:
2690 // FIXME: we really care that the second store is >= size of the first.
2691 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002692 // Create a new store of Value that replaces both stores.
2693 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002694 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2695 return Chain;
Evan Cheng786225a2006-10-05 23:01:46 +00002696 SDOperand NewStore = DAG.getStore(PrevStore->getOperand(0), Value, Ptr,
2697 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002698 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002699 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002700 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002701 }
2702
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002703 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002704 // FIXME: This needs to know that the resultant store does not need a
2705 // higher alignment than the original.
Jim Laskey14fbcbf2006-09-25 21:11:32 +00002706 if (0 && Value.getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng786225a2006-10-05 23:01:46 +00002707 return DAG.getStore(Chain, Value.getOperand(0), Ptr, SrcValue);
Jim Laskey279f0532006-09-25 16:29:54 +00002708 }
2709
2710 if (CombinerAA) {
Jim Laskey288af5e2006-09-25 19:32:58 +00002711 // If the store ptr is a frame index and the frame index has a use of one
2712 // and this is a return block, then the store is redundant.
2713 if (Ptr.hasOneUse() && isa<FrameIndexSDNode>(Ptr) &&
2714 DAG.getRoot().getOpcode() == ISD::RET) {
2715 return Chain;
2716 }
2717
Jim Laskey279f0532006-09-25 16:29:54 +00002718 // Walk up chain skipping non-aliasing memory nodes.
2719 SDOperand BetterChain = FindBetterChain(N, Chain);
2720
Jim Laskey6ff23e52006-10-04 16:53:27 +00002721 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00002722 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00002723 // Replace the chain to avoid dependency.
Evan Cheng786225a2006-10-05 23:01:46 +00002724 SDOperand ReplStore = DAG.getStore(BetterChain, Value, Ptr, SrcValue);
Jim Laskey279f0532006-09-25 16:29:54 +00002725 // Create token to keep both nodes around.
Jim Laskey6ff23e52006-10-04 16:53:27 +00002726 return DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
Jim Laskey279f0532006-09-25 16:29:54 +00002727 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00002728 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002729
Chris Lattner87514ca2005-10-10 22:31:19 +00002730 return SDOperand();
2731}
2732
Chris Lattnerca242442006-03-19 01:27:56 +00002733SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2734 SDOperand InVec = N->getOperand(0);
2735 SDOperand InVal = N->getOperand(1);
2736 SDOperand EltNo = N->getOperand(2);
2737
2738 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2739 // vector with the inserted element.
2740 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2741 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002742 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002743 if (Elt < Ops.size())
2744 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002745 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
2746 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002747 }
2748
2749 return SDOperand();
2750}
2751
2752SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2753 SDOperand InVec = N->getOperand(0);
2754 SDOperand InVal = N->getOperand(1);
2755 SDOperand EltNo = N->getOperand(2);
2756 SDOperand NumElts = N->getOperand(3);
2757 SDOperand EltType = N->getOperand(4);
2758
2759 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2760 // vector with the inserted element.
2761 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2762 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002763 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002764 if (Elt < Ops.size()-2)
2765 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002766 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
2767 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002768 }
2769
2770 return SDOperand();
2771}
2772
Chris Lattnerd7648c82006-03-28 20:28:38 +00002773SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
2774 unsigned NumInScalars = N->getNumOperands()-2;
2775 SDOperand NumElts = N->getOperand(NumInScalars);
2776 SDOperand EltType = N->getOperand(NumInScalars+1);
2777
2778 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
2779 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
2780 // two distinct vectors, turn this into a shuffle node.
2781 SDOperand VecIn1, VecIn2;
2782 for (unsigned i = 0; i != NumInScalars; ++i) {
2783 // Ignore undef inputs.
2784 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
2785
2786 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
2787 // constant index, bail out.
2788 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
2789 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
2790 VecIn1 = VecIn2 = SDOperand(0, 0);
2791 break;
2792 }
2793
2794 // If the input vector type disagrees with the result of the vbuild_vector,
2795 // we can't make a shuffle.
2796 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
2797 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
2798 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
2799 VecIn1 = VecIn2 = SDOperand(0, 0);
2800 break;
2801 }
2802
2803 // Otherwise, remember this. We allow up to two distinct input vectors.
2804 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
2805 continue;
2806
2807 if (VecIn1.Val == 0) {
2808 VecIn1 = ExtractedFromVec;
2809 } else if (VecIn2.Val == 0) {
2810 VecIn2 = ExtractedFromVec;
2811 } else {
2812 // Too many inputs.
2813 VecIn1 = VecIn2 = SDOperand(0, 0);
2814 break;
2815 }
2816 }
2817
2818 // If everything is good, we can make a shuffle operation.
2819 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002820 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00002821 for (unsigned i = 0; i != NumInScalars; ++i) {
2822 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
2823 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2824 continue;
2825 }
2826
2827 SDOperand Extract = N->getOperand(i);
2828
2829 // If extracting from the first vector, just use the index directly.
2830 if (Extract.getOperand(0) == VecIn1) {
2831 BuildVecIndices.push_back(Extract.getOperand(1));
2832 continue;
2833 }
2834
2835 // Otherwise, use InIdx + VecSize
2836 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
2837 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
2838 }
2839
2840 // Add count and size info.
2841 BuildVecIndices.push_back(NumElts);
2842 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
2843
2844 // Return the new VVECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002845 SDOperand Ops[5];
2846 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00002847 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002848 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00002849 } else {
2850 // Use an undef vbuild_vector as input for the second operand.
2851 std::vector<SDOperand> UnOps(NumInScalars,
2852 DAG.getNode(ISD::UNDEF,
2853 cast<VTSDNode>(EltType)->getVT()));
2854 UnOps.push_back(NumElts);
2855 UnOps.push_back(EltType);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002856 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2857 &UnOps[0], UnOps.size());
2858 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00002859 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002860 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2861 &BuildVecIndices[0], BuildVecIndices.size());
2862 Ops[3] = NumElts;
2863 Ops[4] = EltType;
2864 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
Chris Lattnerd7648c82006-03-28 20:28:38 +00002865 }
2866
2867 return SDOperand();
2868}
2869
Chris Lattner66445d32006-03-28 22:11:53 +00002870SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002871 SDOperand ShufMask = N->getOperand(2);
2872 unsigned NumElts = ShufMask.getNumOperands();
2873
2874 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2875 bool isIdentity = true;
2876 for (unsigned i = 0; i != NumElts; ++i) {
2877 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2878 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2879 isIdentity = false;
2880 break;
2881 }
2882 }
2883 if (isIdentity) return N->getOperand(0);
2884
2885 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2886 isIdentity = true;
2887 for (unsigned i = 0; i != NumElts; ++i) {
2888 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2889 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2890 isIdentity = false;
2891 break;
2892 }
2893 }
2894 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00002895
2896 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
2897 // needed at all.
2898 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00002899 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002900 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00002901 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002902 for (unsigned i = 0; i != NumElts; ++i)
2903 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
2904 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
2905 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00002906 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00002907 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00002908 BaseIdx = Idx;
2909 } else {
2910 if (BaseIdx != Idx)
2911 isSplat = false;
2912 if (VecNum != V) {
2913 isUnary = false;
2914 break;
2915 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00002916 }
2917 }
2918
2919 SDOperand N0 = N->getOperand(0);
2920 SDOperand N1 = N->getOperand(1);
2921 // Normalize unary shuffle so the RHS is undef.
2922 if (isUnary && VecNum == 1)
2923 std::swap(N0, N1);
2924
Evan Cheng917ec982006-07-21 08:25:53 +00002925 // If it is a splat, check if the argument vector is a build_vector with
2926 // all scalar elements the same.
2927 if (isSplat) {
2928 SDNode *V = N0.Val;
2929 if (V->getOpcode() == ISD::BIT_CONVERT)
2930 V = V->getOperand(0).Val;
2931 if (V->getOpcode() == ISD::BUILD_VECTOR) {
2932 unsigned NumElems = V->getNumOperands()-2;
2933 if (NumElems > BaseIdx) {
2934 SDOperand Base;
2935 bool AllSame = true;
2936 for (unsigned i = 0; i != NumElems; ++i) {
2937 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
2938 Base = V->getOperand(i);
2939 break;
2940 }
2941 }
2942 // Splat of <u, u, u, u>, return <u, u, u, u>
2943 if (!Base.Val)
2944 return N0;
2945 for (unsigned i = 0; i != NumElems; ++i) {
2946 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
2947 V->getOperand(i) != Base) {
2948 AllSame = false;
2949 break;
2950 }
2951 }
2952 // Splat of <x, x, x, x>, return <x, x, x, x>
2953 if (AllSame)
2954 return N0;
2955 }
2956 }
2957 }
2958
Evan Chenge7bec0d2006-07-20 22:44:41 +00002959 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
2960 // into an undef.
2961 if (isUnary || N0 == N1) {
2962 if (N0.getOpcode() == ISD::UNDEF)
Evan Chengc04766a2006-04-06 23:20:43 +00002963 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00002964 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2965 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002966 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00002967 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00002968 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2969 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2970 MappedOps.push_back(ShufMask.getOperand(i));
2971 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00002972 unsigned NewIdx =
2973 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2974 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00002975 }
2976 }
2977 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002978 &MappedOps[0], MappedOps.size());
Chris Lattner3e104b12006-04-08 04:15:24 +00002979 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00002980 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
Evan Chenge7bec0d2006-07-20 22:44:41 +00002981 N0,
Chris Lattner66445d32006-03-28 22:11:53 +00002982 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
2983 ShufMask);
2984 }
2985
2986 return SDOperand();
2987}
2988
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002989SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
2990 SDOperand ShufMask = N->getOperand(2);
2991 unsigned NumElts = ShufMask.getNumOperands()-2;
2992
2993 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2994 bool isIdentity = true;
2995 for (unsigned i = 0; i != NumElts; ++i) {
2996 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2997 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2998 isIdentity = false;
2999 break;
3000 }
3001 }
3002 if (isIdentity) return N->getOperand(0);
3003
3004 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3005 isIdentity = true;
3006 for (unsigned i = 0; i != NumElts; ++i) {
3007 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3008 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3009 isIdentity = false;
3010 break;
3011 }
3012 }
3013 if (isIdentity) return N->getOperand(1);
3014
Evan Chenge7bec0d2006-07-20 22:44:41 +00003015 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3016 // needed at all.
3017 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003018 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003019 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003020 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003021 for (unsigned i = 0; i != NumElts; ++i)
3022 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3023 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3024 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003025 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003026 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003027 BaseIdx = Idx;
3028 } else {
3029 if (BaseIdx != Idx)
3030 isSplat = false;
3031 if (VecNum != V) {
3032 isUnary = false;
3033 break;
3034 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003035 }
3036 }
3037
3038 SDOperand N0 = N->getOperand(0);
3039 SDOperand N1 = N->getOperand(1);
3040 // Normalize unary shuffle so the RHS is undef.
3041 if (isUnary && VecNum == 1)
3042 std::swap(N0, N1);
3043
Evan Cheng917ec982006-07-21 08:25:53 +00003044 // If it is a splat, check if the argument vector is a build_vector with
3045 // all scalar elements the same.
3046 if (isSplat) {
3047 SDNode *V = N0.Val;
3048 if (V->getOpcode() == ISD::VBIT_CONVERT)
3049 V = V->getOperand(0).Val;
3050 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
3051 unsigned NumElems = V->getNumOperands()-2;
3052 if (NumElems > BaseIdx) {
3053 SDOperand Base;
3054 bool AllSame = true;
3055 for (unsigned i = 0; i != NumElems; ++i) {
3056 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3057 Base = V->getOperand(i);
3058 break;
3059 }
3060 }
3061 // Splat of <u, u, u, u>, return <u, u, u, u>
3062 if (!Base.Val)
3063 return N0;
3064 for (unsigned i = 0; i != NumElems; ++i) {
3065 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3066 V->getOperand(i) != Base) {
3067 AllSame = false;
3068 break;
3069 }
3070 }
3071 // Splat of <x, x, x, x>, return <x, x, x, x>
3072 if (AllSame)
3073 return N0;
3074 }
3075 }
3076 }
3077
Evan Chenge7bec0d2006-07-20 22:44:41 +00003078 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3079 // into an undef.
3080 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00003081 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3082 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003083 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00003084 for (unsigned i = 0; i != NumElts; ++i) {
3085 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3086 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3087 MappedOps.push_back(ShufMask.getOperand(i));
3088 } else {
3089 unsigned NewIdx =
3090 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3091 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
3092 }
3093 }
3094 // Add the type/#elts values.
3095 MappedOps.push_back(ShufMask.getOperand(NumElts));
3096 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
3097
3098 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003099 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003100 AddToWorkList(ShufMask.Val);
3101
3102 // Build the undef vector.
3103 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
3104 for (unsigned i = 0; i != NumElts; ++i)
3105 MappedOps[i] = UDVal;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003106 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
3107 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003108 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3109 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003110
3111 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
Evan Chenge7bec0d2006-07-20 22:44:41 +00003112 N0, UDVal, ShufMask,
Chris Lattner17614ea2006-04-08 05:34:25 +00003113 MappedOps[NumElts], MappedOps[NumElts+1]);
3114 }
3115
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003116 return SDOperand();
3117}
3118
Evan Cheng44f1f092006-04-20 08:56:16 +00003119/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
3120/// a VAND to a vector_shuffle with the destination vector and a zero vector.
3121/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
3122/// vector_shuffle V, Zero, <0, 4, 2, 4>
3123SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
3124 SDOperand LHS = N->getOperand(0);
3125 SDOperand RHS = N->getOperand(1);
3126 if (N->getOpcode() == ISD::VAND) {
3127 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
3128 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
3129 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
3130 RHS = RHS.getOperand(0);
3131 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
3132 std::vector<SDOperand> IdxOps;
3133 unsigned NumOps = RHS.getNumOperands();
3134 unsigned NumElts = NumOps-2;
3135 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
3136 for (unsigned i = 0; i != NumElts; ++i) {
3137 SDOperand Elt = RHS.getOperand(i);
3138 if (!isa<ConstantSDNode>(Elt))
3139 return SDOperand();
3140 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
3141 IdxOps.push_back(DAG.getConstant(i, EVT));
3142 else if (cast<ConstantSDNode>(Elt)->isNullValue())
3143 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
3144 else
3145 return SDOperand();
3146 }
3147
3148 // Let's see if the target supports this vector_shuffle.
3149 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
3150 return SDOperand();
3151
3152 // Return the new VVECTOR_SHUFFLE node.
3153 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
3154 SDOperand EVTNode = DAG.getValueType(EVT);
3155 std::vector<SDOperand> Ops;
Chris Lattner516b9622006-09-14 20:50:57 +00003156 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode,
3157 EVTNode);
Evan Cheng44f1f092006-04-20 08:56:16 +00003158 Ops.push_back(LHS);
3159 AddToWorkList(LHS.Val);
3160 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
3161 ZeroOps.push_back(NumEltsNode);
3162 ZeroOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003163 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3164 &ZeroOps[0], ZeroOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003165 IdxOps.push_back(NumEltsNode);
3166 IdxOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003167 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3168 &IdxOps[0], IdxOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003169 Ops.push_back(NumEltsNode);
3170 Ops.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003171 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
3172 &Ops[0], Ops.size());
Evan Cheng44f1f092006-04-20 08:56:16 +00003173 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
3174 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
3175 DstVecSize, DstVecEVT);
3176 }
3177 return Result;
3178 }
3179 }
3180 return SDOperand();
3181}
3182
Chris Lattneredab1b92006-04-02 03:25:57 +00003183/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
3184/// the scalar operation of the vop if it is operating on an integer vector
3185/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
3186SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
3187 ISD::NodeType FPOp) {
3188 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
3189 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
3190 SDOperand LHS = N->getOperand(0);
3191 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00003192 SDOperand Shuffle = XformToShuffleWithZero(N);
3193 if (Shuffle.Val) return Shuffle;
3194
Chris Lattneredab1b92006-04-02 03:25:57 +00003195 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
3196 // this operation.
3197 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
3198 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003199 SmallVector<SDOperand, 8> Ops;
Chris Lattneredab1b92006-04-02 03:25:57 +00003200 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
3201 SDOperand LHSOp = LHS.getOperand(i);
3202 SDOperand RHSOp = RHS.getOperand(i);
3203 // If these two elements can't be folded, bail out.
3204 if ((LHSOp.getOpcode() != ISD::UNDEF &&
3205 LHSOp.getOpcode() != ISD::Constant &&
3206 LHSOp.getOpcode() != ISD::ConstantFP) ||
3207 (RHSOp.getOpcode() != ISD::UNDEF &&
3208 RHSOp.getOpcode() != ISD::Constant &&
3209 RHSOp.getOpcode() != ISD::ConstantFP))
3210 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00003211 // Can't fold divide by zero.
3212 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
3213 if ((RHSOp.getOpcode() == ISD::Constant &&
3214 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
3215 (RHSOp.getOpcode() == ISD::ConstantFP &&
3216 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
3217 break;
3218 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003219 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00003220 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00003221 assert((Ops.back().getOpcode() == ISD::UNDEF ||
3222 Ops.back().getOpcode() == ISD::Constant ||
3223 Ops.back().getOpcode() == ISD::ConstantFP) &&
3224 "Scalar binop didn't fold!");
3225 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003226
3227 if (Ops.size() == LHS.getNumOperands()-2) {
3228 Ops.push_back(*(LHS.Val->op_end()-2));
3229 Ops.push_back(*(LHS.Val->op_end()-1));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003230 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003231 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003232 }
3233
3234 return SDOperand();
3235}
3236
Nate Begeman44728a72005-09-19 22:34:01 +00003237SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00003238 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
3239
3240 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
3241 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3242 // If we got a simplified select_cc node back from SimplifySelectCC, then
3243 // break it down into a new SETCC node, and a new SELECT node, and then return
3244 // the SELECT node, since we were called with a SELECT node.
3245 if (SCC.Val) {
3246 // Check to see if we got a select_cc back (to turn into setcc/select).
3247 // Otherwise, just return whatever node we got back, like fabs.
3248 if (SCC.getOpcode() == ISD::SELECT_CC) {
3249 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
3250 SCC.getOperand(0), SCC.getOperand(1),
3251 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00003252 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003253 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
3254 SCC.getOperand(3), SETCC);
3255 }
3256 return SCC;
3257 }
Nate Begeman44728a72005-09-19 22:34:01 +00003258 return SDOperand();
3259}
3260
Chris Lattner40c62d52005-10-18 06:04:22 +00003261/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
3262/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00003263/// select. Callers of this should assume that TheSelect is deleted if this
3264/// returns true. As such, they should return the appropriate thing (e.g. the
3265/// node) back to the top-level of the DAG combiner loop to avoid it being
3266/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00003267///
3268bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
3269 SDOperand RHS) {
3270
3271 // If this is a select from two identical things, try to pull the operation
3272 // through the select.
3273 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00003274 // If this is a load and the token chain is identical, replace the select
3275 // of two loads with a load through a select of the address to load from.
3276 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
3277 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00003278 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00003279 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00003280 LHS.getOperand(0) == RHS.getOperand(0)) {
3281 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
3282 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
3283
3284 // If this is an EXTLOAD, the VT's must match.
3285 if (LLD->getLoadVT() == RLD->getLoadVT()) {
3286 // FIXME: this conflates two src values, discarding one. This is not
3287 // the right thing to do, but nothing uses srcvalues now. When they do,
3288 // turn SrcValue into a list of locations.
3289 SDOperand Addr;
3290 if (TheSelect->getOpcode() == ISD::SELECT)
3291 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
3292 TheSelect->getOperand(0), LLD->getBasePtr(),
3293 RLD->getBasePtr());
3294 else
3295 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
3296 TheSelect->getOperand(0),
3297 TheSelect->getOperand(1),
3298 LLD->getBasePtr(), RLD->getBasePtr(),
3299 TheSelect->getOperand(4));
Chris Lattner40c62d52005-10-18 06:04:22 +00003300
Evan Cheng466685d2006-10-09 20:57:25 +00003301 SDOperand Load;
3302 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
3303 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
3304 Addr,LLD->getSrcValue(), LLD->getSrcValueOffset());
3305 else {
3306 Load = DAG.getExtLoad(LLD->getExtensionType(),
3307 TheSelect->getValueType(0),
3308 LLD->getChain(), Addr, LLD->getSrcValue(),
3309 LLD->getSrcValueOffset(),
3310 LLD->getLoadVT());
3311 }
3312 // Users of the select now use the result of the load.
3313 CombineTo(TheSelect, Load);
3314
3315 // Users of the old loads now use the new load's chain. We know the
3316 // old-load value is dead now.
3317 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
3318 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
3319 return true;
Evan Chengc5484282006-10-04 00:56:09 +00003320 }
Chris Lattner40c62d52005-10-18 06:04:22 +00003321 }
3322 }
3323
3324 return false;
3325}
3326
Nate Begeman44728a72005-09-19 22:34:01 +00003327SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
3328 SDOperand N2, SDOperand N3,
3329 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00003330
3331 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00003332 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
3333 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
3334 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
3335
3336 // Determine if the condition we're dealing with is constant
3337 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
3338 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
3339
3340 // fold select_cc true, x, y -> x
3341 if (SCCC && SCCC->getValue())
3342 return N2;
3343 // fold select_cc false, x, y -> y
3344 if (SCCC && SCCC->getValue() == 0)
3345 return N3;
3346
3347 // Check to see if we can simplify the select into an fabs node
3348 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
3349 // Allow either -0.0 or 0.0
3350 if (CFP->getValue() == 0.0) {
3351 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
3352 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
3353 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
3354 N2 == N3.getOperand(0))
3355 return DAG.getNode(ISD::FABS, VT, N0);
3356
3357 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
3358 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3359 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3360 N2.getOperand(0) == N3)
3361 return DAG.getNode(ISD::FABS, VT, N3);
3362 }
3363 }
3364
3365 // Check to see if we can perform the "gzip trick", transforming
3366 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00003367 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00003368 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00003369 MVT::isInteger(N2.getValueType()) &&
3370 (N1C->isNullValue() || // (a < 0) ? b : 0
3371 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00003372 MVT::ValueType XType = N0.getValueType();
3373 MVT::ValueType AType = N2.getValueType();
3374 if (XType >= AType) {
3375 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00003376 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00003377 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3378 unsigned ShCtV = Log2_64(N2C->getValue());
3379 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3380 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3381 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00003382 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003383 if (XType > AType) {
3384 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003385 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003386 }
3387 return DAG.getNode(ISD::AND, AType, Shift, N2);
3388 }
3389 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3390 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3391 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003392 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003393 if (XType > AType) {
3394 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003395 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003396 }
3397 return DAG.getNode(ISD::AND, AType, Shift, N2);
3398 }
3399 }
Nate Begeman07ed4172005-10-10 21:26:48 +00003400
3401 // fold select C, 16, 0 -> shl C, 4
3402 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3403 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3404 // Get a SetCC of the condition
3405 // FIXME: Should probably make sure that setcc is legal if we ever have a
3406 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00003407 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00003408 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00003409 if (AfterLegalize) {
3410 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003411 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00003412 } else {
3413 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003414 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00003415 }
Chris Lattner5750df92006-03-01 04:03:14 +00003416 AddToWorkList(SCC.Val);
3417 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00003418 // shl setcc result by log2 n2c
3419 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3420 DAG.getConstant(Log2_64(N2C->getValue()),
3421 TLI.getShiftAmountTy()));
3422 }
3423
Nate Begemanf845b452005-10-08 00:29:44 +00003424 // Check to see if this is the equivalent of setcc
3425 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3426 // otherwise, go ahead with the folds.
3427 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3428 MVT::ValueType XType = N0.getValueType();
3429 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3430 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3431 if (Res.getValueType() != VT)
3432 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3433 return Res;
3434 }
3435
3436 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3437 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3438 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3439 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3440 return DAG.getNode(ISD::SRL, XType, Ctlz,
3441 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3442 TLI.getShiftAmountTy()));
3443 }
3444 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3445 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3446 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3447 N0);
3448 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3449 DAG.getConstant(~0ULL, XType));
3450 return DAG.getNode(ISD::SRL, XType,
3451 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3452 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3453 TLI.getShiftAmountTy()));
3454 }
3455 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3456 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3457 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3458 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3459 TLI.getShiftAmountTy()));
3460 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3461 }
3462 }
3463
3464 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3465 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3466 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3467 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3468 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3469 MVT::ValueType XType = N0.getValueType();
3470 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3471 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3472 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3473 TLI.getShiftAmountTy()));
3474 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003475 AddToWorkList(Shift.Val);
3476 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003477 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3478 }
3479 }
3480 }
3481
Nate Begeman44728a72005-09-19 22:34:01 +00003482 return SDOperand();
3483}
3484
Nate Begeman452d7be2005-09-16 00:54:12 +00003485SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00003486 SDOperand N1, ISD::CondCode Cond,
3487 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003488 // These setcc operations always fold.
3489 switch (Cond) {
3490 default: break;
3491 case ISD::SETFALSE:
3492 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3493 case ISD::SETTRUE:
3494 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3495 }
3496
3497 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3498 uint64_t C1 = N1C->getValue();
3499 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
3500 uint64_t C0 = N0C->getValue();
3501
3502 // Sign extend the operands if required
3503 if (ISD::isSignedIntSetCC(Cond)) {
3504 C0 = N0C->getSignExtended();
3505 C1 = N1C->getSignExtended();
3506 }
3507
3508 switch (Cond) {
3509 default: assert(0 && "Unknown integer setcc!");
3510 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3511 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3512 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
3513 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
3514 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
3515 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
3516 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
3517 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
3518 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
3519 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
3520 }
3521 } else {
Chris Lattner5f42a242006-09-20 06:19:26 +00003522 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
3523 // equality comparison, then we're just comparing whether X itself is
3524 // zero.
3525 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
3526 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
3527 N0.getOperand(1).getOpcode() == ISD::Constant) {
3528 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
3529 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3530 ShAmt == Log2_32(MVT::getSizeInBits(N0.getValueType()))) {
3531 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
3532 // (srl (ctlz x), 5) == 0 -> X != 0
3533 // (srl (ctlz x), 5) != 1 -> X != 0
3534 Cond = ISD::SETNE;
3535 } else {
3536 // (srl (ctlz x), 5) != 0 -> X == 0
3537 // (srl (ctlz x), 5) == 1 -> X == 0
3538 Cond = ISD::SETEQ;
3539 }
3540 SDOperand Zero = DAG.getConstant(0, N0.getValueType());
3541 return DAG.getSetCC(VT, N0.getOperand(0).getOperand(0),
3542 Zero, Cond);
3543 }
3544 }
3545
Nate Begeman452d7be2005-09-16 00:54:12 +00003546 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3547 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3548 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3549
3550 // If the comparison constant has bits in the upper part, the
3551 // zero-extended value could never match.
3552 if (C1 & (~0ULL << InSize)) {
3553 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3554 switch (Cond) {
3555 case ISD::SETUGT:
3556 case ISD::SETUGE:
3557 case ISD::SETEQ: return DAG.getConstant(0, VT);
3558 case ISD::SETULT:
3559 case ISD::SETULE:
3560 case ISD::SETNE: return DAG.getConstant(1, VT);
3561 case ISD::SETGT:
3562 case ISD::SETGE:
3563 // True if the sign bit of C1 is set.
3564 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3565 case ISD::SETLT:
3566 case ISD::SETLE:
3567 // True if the sign bit of C1 isn't set.
3568 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3569 default:
3570 break;
3571 }
3572 }
3573
3574 // Otherwise, we can perform the comparison with the low bits.
3575 switch (Cond) {
3576 case ISD::SETEQ:
3577 case ISD::SETNE:
3578 case ISD::SETUGT:
3579 case ISD::SETUGE:
3580 case ISD::SETULT:
3581 case ISD::SETULE:
3582 return DAG.getSetCC(VT, N0.getOperand(0),
3583 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3584 Cond);
3585 default:
3586 break; // todo, be more careful with signed comparisons
3587 }
3588 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3589 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3590 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3591 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3592 MVT::ValueType ExtDstTy = N0.getValueType();
3593 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3594
3595 // If the extended part has any inconsistent bits, it cannot ever
3596 // compare equal. In other words, they have to be all ones or all
3597 // zeros.
3598 uint64_t ExtBits =
3599 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3600 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3601 return DAG.getConstant(Cond == ISD::SETNE, VT);
3602
3603 SDOperand ZextOp;
3604 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3605 if (Op0Ty == ExtSrcTy) {
3606 ZextOp = N0.getOperand(0);
3607 } else {
3608 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3609 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3610 DAG.getConstant(Imm, Op0Ty));
3611 }
Chris Lattner5750df92006-03-01 04:03:14 +00003612 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003613 // Otherwise, make this a use of a zext.
3614 return DAG.getSetCC(VT, ZextOp,
3615 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3616 ExtDstTy),
3617 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00003618 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
3619 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3620 (N0.getOpcode() == ISD::XOR ||
3621 (N0.getOpcode() == ISD::AND &&
3622 N0.getOperand(0).getOpcode() == ISD::XOR &&
3623 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3624 isa<ConstantSDNode>(N0.getOperand(1)) &&
3625 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
3626 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
3627 // only do this if the top bits are known zero.
3628 if (TLI.MaskedValueIsZero(N1,
3629 MVT::getIntVTBitMask(N0.getValueType())-1)) {
3630 // Okay, get the un-inverted input value.
3631 SDOperand Val;
3632 if (N0.getOpcode() == ISD::XOR)
3633 Val = N0.getOperand(0);
3634 else {
3635 assert(N0.getOpcode() == ISD::AND &&
3636 N0.getOperand(0).getOpcode() == ISD::XOR);
3637 // ((X^1)&1)^1 -> X & 1
3638 Val = DAG.getNode(ISD::AND, N0.getValueType(),
3639 N0.getOperand(0).getOperand(0), N0.getOperand(1));
3640 }
3641 return DAG.getSetCC(VT, Val, N1,
3642 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
3643 }
Nate Begeman452d7be2005-09-16 00:54:12 +00003644 }
Chris Lattner5c46f742005-10-05 06:11:08 +00003645
Nate Begeman452d7be2005-09-16 00:54:12 +00003646 uint64_t MinVal, MaxVal;
3647 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
3648 if (ISD::isSignedIntSetCC(Cond)) {
3649 MinVal = 1ULL << (OperandBitSize-1);
3650 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
3651 MaxVal = ~0ULL >> (65-OperandBitSize);
3652 else
3653 MaxVal = 0;
3654 } else {
3655 MinVal = 0;
3656 MaxVal = ~0ULL >> (64-OperandBitSize);
3657 }
3658
3659 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
3660 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
3661 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
3662 --C1; // X >= C0 --> X > (C0-1)
3663 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3664 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
3665 }
3666
3667 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
3668 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
3669 ++C1; // X <= C0 --> X < (C0+1)
3670 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3671 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
3672 }
3673
3674 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
3675 return DAG.getConstant(0, VT); // X < MIN --> false
3676
3677 // Canonicalize setgt X, Min --> setne X, Min
3678 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
3679 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00003680 // Canonicalize setlt X, Max --> setne X, Max
3681 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
3682 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00003683
3684 // If we have setult X, 1, turn it into seteq X, 0
3685 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
3686 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
3687 ISD::SETEQ);
3688 // If we have setugt X, Max-1, turn it into seteq X, Max
3689 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
3690 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
3691 ISD::SETEQ);
3692
3693 // If we have "setcc X, C0", check to see if we can shrink the immediate
3694 // by changing cc.
3695
3696 // SETUGT X, SINTMAX -> SETLT X, 0
3697 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
3698 C1 == (~0ULL >> (65-OperandBitSize)))
3699 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
3700 ISD::SETLT);
3701
3702 // FIXME: Implement the rest of these.
3703
3704 // Fold bit comparisons when we can.
3705 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3706 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
3707 if (ConstantSDNode *AndRHS =
3708 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3709 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
3710 // Perform the xform if the AND RHS is a single bit.
3711 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
3712 return DAG.getNode(ISD::SRL, VT, N0,
3713 DAG.getConstant(Log2_64(AndRHS->getValue()),
3714 TLI.getShiftAmountTy()));
3715 }
3716 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
3717 // (X & 8) == 8 --> (X & 8) >> 3
3718 // Perform the xform if C1 is a single bit.
3719 if ((C1 & (C1-1)) == 0) {
3720 return DAG.getNode(ISD::SRL, VT, N0,
Chris Lattner729c6d12006-05-27 00:43:02 +00003721 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
Nate Begeman452d7be2005-09-16 00:54:12 +00003722 }
3723 }
3724 }
3725 }
3726 } else if (isa<ConstantSDNode>(N0.Val)) {
3727 // Ensure that the constant occurs on the RHS.
3728 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3729 }
3730
3731 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
3732 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
3733 double C0 = N0C->getValue(), C1 = N1C->getValue();
3734
3735 switch (Cond) {
3736 default: break; // FIXME: Implement the rest of these!
3737 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3738 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3739 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
3740 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
3741 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
3742 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
3743 }
3744 } else {
3745 // Ensure that the constant occurs on the RHS.
3746 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3747 }
3748
3749 if (N0 == N1) {
3750 // We can always fold X == Y for integer setcc's.
3751 if (MVT::isInteger(N0.getValueType()))
3752 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3753 unsigned UOF = ISD::getUnorderedFlavor(Cond);
3754 if (UOF == 2) // FP operators that are undefined on NaNs.
3755 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3756 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
3757 return DAG.getConstant(UOF, VT);
3758 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
3759 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00003760 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00003761 if (NewCond != Cond)
3762 return DAG.getSetCC(VT, N0, N1, NewCond);
3763 }
3764
3765 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3766 MVT::isInteger(N0.getValueType())) {
3767 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
3768 N0.getOpcode() == ISD::XOR) {
3769 // Simplify (X+Y) == (X+Z) --> Y == Z
3770 if (N0.getOpcode() == N1.getOpcode()) {
3771 if (N0.getOperand(0) == N1.getOperand(0))
3772 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
3773 if (N0.getOperand(1) == N1.getOperand(1))
3774 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
Evan Cheng1efba0e2006-08-29 06:42:35 +00003775 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003776 // If X op Y == Y op X, try other combinations.
3777 if (N0.getOperand(0) == N1.getOperand(1))
3778 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
3779 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00003780 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00003781 }
3782 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003783
3784 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
3785 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3786 // Turn (X+C1) == C2 --> X == C2-C1
3787 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
3788 return DAG.getSetCC(VT, N0.getOperand(0),
3789 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
3790 N0.getValueType()), Cond);
3791 }
3792
3793 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
3794 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00003795 // If we know that all of the inverted bits are zero, don't bother
3796 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003797 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00003798 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003799 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00003800 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003801 }
3802
3803 // Turn (C1-X) == C2 --> X == C1-C2
3804 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
3805 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
3806 return DAG.getSetCC(VT, N0.getOperand(1),
3807 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
3808 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00003809 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003810 }
3811 }
3812
Nate Begeman452d7be2005-09-16 00:54:12 +00003813 // Simplify (X+Z) == X --> Z == 0
3814 if (N0.getOperand(0) == N1)
3815 return DAG.getSetCC(VT, N0.getOperand(1),
3816 DAG.getConstant(0, N0.getValueType()), Cond);
3817 if (N0.getOperand(1) == N1) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00003818 if (DAG.isCommutativeBinOp(N0.getOpcode()))
Nate Begeman452d7be2005-09-16 00:54:12 +00003819 return DAG.getSetCC(VT, N0.getOperand(0),
3820 DAG.getConstant(0, N0.getValueType()), Cond);
3821 else {
3822 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
3823 // (Z-X) == X --> Z == X<<1
3824 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
3825 N1,
3826 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003827 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003828 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
3829 }
3830 }
3831 }
3832
3833 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3834 N1.getOpcode() == ISD::XOR) {
3835 // Simplify X == (X+Z) --> Z == 0
3836 if (N1.getOperand(0) == N0) {
3837 return DAG.getSetCC(VT, N1.getOperand(1),
3838 DAG.getConstant(0, N1.getValueType()), Cond);
3839 } else if (N1.getOperand(1) == N0) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00003840 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003841 return DAG.getSetCC(VT, N1.getOperand(0),
3842 DAG.getConstant(0, N1.getValueType()), Cond);
3843 } else {
3844 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3845 // X == (Z-X) --> X<<1 == Z
3846 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
3847 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003848 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003849 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
3850 }
3851 }
3852 }
3853 }
3854
3855 // Fold away ALL boolean setcc's.
3856 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00003857 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003858 switch (Cond) {
3859 default: assert(0 && "Unknown integer setcc!");
3860 case ISD::SETEQ: // X == Y -> (X^Y)^1
3861 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3862 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00003863 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003864 break;
3865 case ISD::SETNE: // X != Y --> (X^Y)
3866 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3867 break;
3868 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
3869 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
3870 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3871 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003872 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003873 break;
3874 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
3875 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
3876 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3877 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003878 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003879 break;
3880 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
3881 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
3882 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3883 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003884 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003885 break;
3886 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
3887 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
3888 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3889 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
3890 break;
3891 }
3892 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00003893 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003894 // FIXME: If running after legalize, we probably can't do this.
3895 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3896 }
3897 return N0;
3898 }
3899
3900 // Could not fold it.
3901 return SDOperand();
3902}
3903
Nate Begeman69575232005-10-20 02:15:44 +00003904/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3905/// return a DAG expression to select that will generate the same value by
3906/// multiplying by a magic number. See:
3907/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3908SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003909 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003910 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
3911
Andrew Lenharth232c9102006-06-12 16:07:18 +00003912 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003913 ii != ee; ++ii)
3914 AddToWorkList(*ii);
3915 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003916}
3917
3918/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3919/// return a DAG expression to select that will generate the same value by
3920/// multiplying by a magic number. See:
3921/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3922SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003923 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003924 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00003925
Andrew Lenharth232c9102006-06-12 16:07:18 +00003926 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003927 ii != ee; ++ii)
3928 AddToWorkList(*ii);
3929 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003930}
3931
Jim Laskey71382342006-10-07 23:37:56 +00003932/// FindBaseOffset - Return true if base is known not to alias with anything
3933/// but itself. Provides base object and offset as results.
3934static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
3935 // Assume it is a primitive operation.
3936 Base = Ptr; Offset = 0;
3937
3938 // If it's an adding a simple constant then integrate the offset.
3939 if (Base.getOpcode() == ISD::ADD) {
3940 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
3941 Base = Base.getOperand(0);
3942 Offset += C->getValue();
3943 }
3944 }
3945
3946 // If it's any of the following then it can't alias with anything but itself.
3947 return isa<FrameIndexSDNode>(Base) ||
3948 isa<ConstantPoolSDNode>(Base) ||
3949 isa<GlobalAddressSDNode>(Base);
3950}
3951
3952/// isAlias - Return true if there is any possibility that the two addresses
3953/// overlap.
3954static bool isAlias(SDOperand Ptr1, int64_t Size1, SDOperand SrcValue1,
3955 SDOperand Ptr2, int64_t Size2, SDOperand SrcValue2) {
3956 // If they are the same then they must be aliases.
3957 if (Ptr1 == Ptr2) return true;
3958
3959 // Gather base node and offset information.
3960 SDOperand Base1, Base2;
3961 int64_t Offset1, Offset2;
3962 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
3963 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
3964
3965 // If they have a same base address then...
3966 if (Base1 == Base2) {
3967 // Check to see if the addresses overlap.
3968 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
3969 }
3970
3971 // Otherwise they alias if either is unknown.
3972 return !KnownBase1 || !KnownBase2;
3973}
3974
3975/// FindAliasInfo - Extracts the relevant alias information from the memory
3976/// node. Returns true if the operand was a load.
Evan Cheng466685d2006-10-09 20:57:25 +00003977static bool FindAliasInfo(SDNode *N, SDOperand &Ptr, int64_t &Size,
3978 SDOperand &SrcValue, SelectionDAG &DAG) {
Jim Laskey71382342006-10-07 23:37:56 +00003979 switch (N->getOpcode()) {
3980 case ISD::LOAD:
Evan Cheng466685d2006-10-09 20:57:25 +00003981 if (!ISD::isNON_EXTLoad(N))
3982 return false;
Jim Laskey71382342006-10-07 23:37:56 +00003983 Ptr = N->getOperand(1);
3984 Size = MVT::getSizeInBits(N->getValueType(0)) >> 3;
3985 SrcValue = N->getOperand(2);
3986 return true;
3987 case ISD::STORE:
3988 Ptr = N->getOperand(2);
3989 Size = MVT::getSizeInBits(N->getOperand(1).getValueType()) >> 3;
3990 SrcValue = N->getOperand(3);
3991 break;
3992 default:
3993 assert(0 && "FindAliasInfo expected a memory operand");
3994 break;
3995 }
3996
3997 return false;
3998}
3999
Jim Laskey6ff23e52006-10-04 16:53:27 +00004000/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
4001/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00004002void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00004003 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004004 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00004005 std::set<SDNode *> Visited; // Visited node set.
4006
Jim Laskey279f0532006-09-25 16:29:54 +00004007 // Get alias information for node.
4008 SDOperand Ptr;
4009 int64_t Size;
4010 SDOperand SrcValue;
Evan Cheng466685d2006-10-09 20:57:25 +00004011 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, DAG);
Jim Laskey279f0532006-09-25 16:29:54 +00004012
Jim Laskey6ff23e52006-10-04 16:53:27 +00004013 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00004014 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00004015
Jim Laskeybc588b82006-10-05 15:07:25 +00004016 // Look at each chain and determine if it is an alias. If so, add it to the
4017 // aliases list. If not, then continue up the chain looking for the next
4018 // candidate.
4019 while (!Chains.empty()) {
4020 SDOperand Chain = Chains.back();
4021 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00004022
Jim Laskeybc588b82006-10-05 15:07:25 +00004023 // Don't bother if we've been before.
4024 if (Visited.find(Chain.Val) != Visited.end()) continue;
4025 Visited.insert(Chain.Val);
4026
4027 switch (Chain.getOpcode()) {
4028 case ISD::EntryToken:
4029 // Entry token is ideal chain operand, but handled in FindBetterChain.
4030 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00004031
Jim Laskeybc588b82006-10-05 15:07:25 +00004032 case ISD::LOAD:
4033 case ISD::STORE: {
4034 // Get alias information for Chain.
4035 SDOperand OpPtr;
4036 int64_t OpSize;
4037 SDOperand OpSrcValue;
Evan Cheng466685d2006-10-09 20:57:25 +00004038 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize, OpSrcValue, DAG);
Jim Laskeybc588b82006-10-05 15:07:25 +00004039
4040 // If chain is alias then stop here.
4041 if (!(IsLoad && IsOpLoad) &&
4042 isAlias(Ptr, Size, SrcValue, OpPtr, OpSize, OpSrcValue)) {
4043 Aliases.push_back(Chain);
4044 } else {
4045 // Look further up the chain.
4046 Chains.push_back(Chain.getOperand(0));
4047 // Clean up old chain.
4048 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00004049 }
Jim Laskeybc588b82006-10-05 15:07:25 +00004050 break;
4051 }
4052
4053 case ISD::TokenFactor:
4054 // We have to check each of the operands of the token factor, so we queue
4055 // then up. Adding the operands to the queue (stack) in reverse order
4056 // maintains the original order and increases the likelihood that getNode
4057 // will find a matching token factor (CSE.)
4058 for (unsigned n = Chain.getNumOperands(); n;)
4059 Chains.push_back(Chain.getOperand(--n));
4060 // Eliminate the token factor if we can.
4061 AddToWorkList(Chain.Val);
4062 break;
4063
4064 default:
4065 // For all other instructions we will just have to take what we can get.
4066 Aliases.push_back(Chain);
4067 break;
Jim Laskey279f0532006-09-25 16:29:54 +00004068 }
4069 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004070}
4071
4072/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
4073/// for a better chain (aliasing node.)
4074SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
4075 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00004076
Jim Laskey6ff23e52006-10-04 16:53:27 +00004077 // Accumulate all the aliases to this node.
4078 GatherAllAliases(N, OldChain, Aliases);
4079
4080 if (Aliases.size() == 0) {
4081 // If no operands then chain to entry token.
4082 return DAG.getEntryNode();
4083 } else if (Aliases.size() == 1) {
4084 // If a single operand then chain to it. We don't need to revisit it.
4085 return Aliases[0];
4086 }
4087
4088 // Construct a custom tailored token factor.
4089 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4090 &Aliases[0], Aliases.size());
4091
4092 // Make sure the old chain gets cleaned up.
4093 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
4094
4095 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00004096}
4097
Nate Begeman1d4d4142005-09-01 00:19:25 +00004098// SelectionDAG::Combine - This is the entry point for the file.
4099//
Nate Begeman4ebd8052005-09-01 23:24:04 +00004100void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00004101 /// run - This is the main entry point to this class.
4102 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00004103 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004104}