blob: f2b23d7516992615c5f11dd6a084894a8d79224e [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);
Evan Chengc5484282006-10-04 00:56:09 +0000224 SDOperand visitLOADX(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000225 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000226 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
227 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000228 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000229 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000230 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000231
Evan Cheng44f1f092006-04-20 08:56:16 +0000232 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000233 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
234
Chris Lattner40c62d52005-10-18 06:04:22 +0000235 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000236 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000237 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
238 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
239 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000240 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000241 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000242 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000243 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000244 SDOperand BuildUDIV(SDNode *N);
245 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Jim Laskey279f0532006-09-25 16:29:54 +0000246
Jim Laskey6ff23e52006-10-04 16:53:27 +0000247 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
248 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000249 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000250 SmallVector<SDOperand, 8> &Aliases);
251
Jim Laskey279f0532006-09-25 16:29:54 +0000252 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000253 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000254 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
255
Nate Begeman1d4d4142005-09-01 00:19:25 +0000256public:
257 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000258 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000259
260 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000261 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000262 };
263}
264
Chris Lattner24664722006-03-01 04:53:38 +0000265//===----------------------------------------------------------------------===//
266// TargetLowering::DAGCombinerInfo implementation
267//===----------------------------------------------------------------------===//
268
269void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
270 ((DAGCombiner*)DC)->AddToWorkList(N);
271}
272
273SDOperand TargetLowering::DAGCombinerInfo::
274CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000275 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000276}
277
278SDOperand TargetLowering::DAGCombinerInfo::
279CombineTo(SDNode *N, SDOperand Res) {
280 return ((DAGCombiner*)DC)->CombineTo(N, Res);
281}
282
283
284SDOperand TargetLowering::DAGCombinerInfo::
285CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
286 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
287}
288
289
290
291
292//===----------------------------------------------------------------------===//
293
294
Nate Begeman4ebd8052005-09-01 23:24:04 +0000295// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
296// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000297// Also, set the incoming LHS, RHS, and CC references to the appropriate
298// nodes based on the type of node we are checking. This simplifies life a
299// bit for the callers.
300static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
301 SDOperand &CC) {
302 if (N.getOpcode() == ISD::SETCC) {
303 LHS = N.getOperand(0);
304 RHS = N.getOperand(1);
305 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000306 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000307 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000308 if (N.getOpcode() == ISD::SELECT_CC &&
309 N.getOperand(2).getOpcode() == ISD::Constant &&
310 N.getOperand(3).getOpcode() == ISD::Constant &&
311 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000312 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
313 LHS = N.getOperand(0);
314 RHS = N.getOperand(1);
315 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000316 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000317 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000318 return false;
319}
320
Nate Begeman99801192005-09-07 23:25:52 +0000321// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
322// one use. If this is true, it allows the users to invert the operation for
323// free when it is profitable to do so.
324static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000325 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000326 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000327 return true;
328 return false;
329}
330
Nate Begemancd4d58c2006-02-03 06:46:56 +0000331SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
332 MVT::ValueType VT = N0.getValueType();
333 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
334 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
335 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
336 if (isa<ConstantSDNode>(N1)) {
337 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000338 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000339 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
340 } else if (N0.hasOneUse()) {
341 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000342 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000343 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
344 }
345 }
346 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
347 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
348 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
349 if (isa<ConstantSDNode>(N0)) {
350 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000351 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000352 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
353 } else if (N1.hasOneUse()) {
354 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000355 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000356 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
357 }
358 }
359 return SDOperand();
360}
361
Nate Begeman4ebd8052005-09-01 23:24:04 +0000362void DAGCombiner::Run(bool RunningAfterLegalize) {
363 // set the instance variable, so that the various visit routines may use it.
364 AfterLegalize = RunningAfterLegalize;
365
Nate Begeman646d7e22005-09-02 21:18:40 +0000366 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000367 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
368 E = DAG.allnodes_end(); I != E; ++I)
369 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000370
Chris Lattner95038592005-10-05 06:35:28 +0000371 // Create a dummy node (which is not added to allnodes), that adds a reference
372 // to the root node, preventing it from being deleted, and tracking any
373 // changes of the root.
374 HandleSDNode Dummy(DAG.getRoot());
375
Chris Lattner24664722006-03-01 04:53:38 +0000376
377 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
378 TargetLowering::DAGCombinerInfo
379 DagCombineInfo(DAG, !RunningAfterLegalize, this);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000380
Nate Begeman1d4d4142005-09-01 00:19:25 +0000381 // while the worklist isn't empty, inspect the node on the end of it and
382 // try and combine it.
383 while (!WorkList.empty()) {
384 SDNode *N = WorkList.back();
385 WorkList.pop_back();
386
387 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000388 // N is deleted from the DAG, since they too may now be dead or may have a
389 // reduced number of uses, allowing other xforms.
390 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000391 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000392 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000393
Chris Lattner95038592005-10-05 06:35:28 +0000394 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000395 continue;
396 }
397
Nate Begeman83e75ec2005-09-06 04:43:02 +0000398 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000399
400 // If nothing happened, try a target-specific DAG combine.
401 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000402 assert(N->getOpcode() != ISD::DELETED_NODE &&
403 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000404 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
405 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
406 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
407 }
408
Nate Begeman83e75ec2005-09-06 04:43:02 +0000409 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000410 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000411 // If we get back the same node we passed in, rather than a new node or
412 // zero, we know that the node must have defined multiple values and
413 // CombineTo was used. Since CombineTo takes care of the worklist
414 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000415 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000416 assert(N->getOpcode() != ISD::DELETED_NODE &&
417 RV.Val->getOpcode() != ISD::DELETED_NODE &&
418 "Node was deleted but visit returned new node!");
419
Jim Laskey6ff23e52006-10-04 16:53:27 +0000420 DEBUG(std::cerr << "\nReplacing.3 "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000421 std::cerr << "\nWith: "; RV.Val->dump(&DAG);
Nate Begeman2300f552005-09-07 00:15:36 +0000422 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000423 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000424 if (N->getNumValues() == RV.Val->getNumValues())
425 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
426 else {
427 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
428 SDOperand OpV = RV;
429 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
430 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000431
432 // Push the new node and any users onto the worklist
Jim Laskey6ff23e52006-10-04 16:53:27 +0000433 AddToWorkList(RV.Val);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000434 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000435
Jim Laskey6ff23e52006-10-04 16:53:27 +0000436 // Nodes can be reintroduced into the worklist. Make sure we do not
437 // process a node that has been replaced.
Nate Begeman646d7e22005-09-02 21:18:40 +0000438 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000439 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
440 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000441
442 // Finally, since the node is now dead, remove it from the graph.
443 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000444 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000445 }
446 }
Chris Lattner95038592005-10-05 06:35:28 +0000447
448 // If the root changed (e.g. it was a dead load, update the root).
449 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000450}
451
Nate Begeman83e75ec2005-09-06 04:43:02 +0000452SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000453 switch(N->getOpcode()) {
454 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000455 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000456 case ISD::ADD: return visitADD(N);
457 case ISD::SUB: return visitSUB(N);
458 case ISD::MUL: return visitMUL(N);
459 case ISD::SDIV: return visitSDIV(N);
460 case ISD::UDIV: return visitUDIV(N);
461 case ISD::SREM: return visitSREM(N);
462 case ISD::UREM: return visitUREM(N);
463 case ISD::MULHU: return visitMULHU(N);
464 case ISD::MULHS: return visitMULHS(N);
465 case ISD::AND: return visitAND(N);
466 case ISD::OR: return visitOR(N);
467 case ISD::XOR: return visitXOR(N);
468 case ISD::SHL: return visitSHL(N);
469 case ISD::SRA: return visitSRA(N);
470 case ISD::SRL: return visitSRL(N);
471 case ISD::CTLZ: return visitCTLZ(N);
472 case ISD::CTTZ: return visitCTTZ(N);
473 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000474 case ISD::SELECT: return visitSELECT(N);
475 case ISD::SELECT_CC: return visitSELECT_CC(N);
476 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000477 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
478 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000479 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000480 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
481 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000482 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000483 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000484 case ISD::FADD: return visitFADD(N);
485 case ISD::FSUB: return visitFSUB(N);
486 case ISD::FMUL: return visitFMUL(N);
487 case ISD::FDIV: return visitFDIV(N);
488 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000489 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000490 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
491 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
492 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
493 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
494 case ISD::FP_ROUND: return visitFP_ROUND(N);
495 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
496 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
497 case ISD::FNEG: return visitFNEG(N);
498 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000499 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000500 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000501 case ISD::LOAD: return visitLOAD(N);
Evan Chengc5484282006-10-04 00:56:09 +0000502 case ISD::LOADX: return visitLOADX(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000503 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000504 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
505 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000506 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000507 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000508 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000509 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
510 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
511 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
512 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
513 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
514 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
515 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
516 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000517 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000518 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000519}
520
Chris Lattner6270f682006-10-08 22:57:01 +0000521/// getInputChainForNode - Given a node, return its input chain if it has one,
522/// otherwise return a null sd operand.
523static SDOperand getInputChainForNode(SDNode *N) {
524 if (unsigned NumOps = N->getNumOperands()) {
525 if (N->getOperand(0).getValueType() == MVT::Other)
526 return N->getOperand(0);
527 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
528 return N->getOperand(NumOps-1);
529 for (unsigned i = 1; i < NumOps-1; ++i)
530 if (N->getOperand(i).getValueType() == MVT::Other)
531 return N->getOperand(i);
532 }
533 return SDOperand(0, 0);
534}
535
Nate Begeman83e75ec2005-09-06 04:43:02 +0000536SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000537 // If N has two operands, where one has an input chain equal to the other,
538 // the 'other' chain is redundant.
539 if (N->getNumOperands() == 2) {
540 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
541 return N->getOperand(0);
542 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
543 return N->getOperand(1);
544 }
545
546
Jim Laskey6ff23e52006-10-04 16:53:27 +0000547 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Jim Laskey279f0532006-09-25 16:29:54 +0000548 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000549 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000550
551 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000552 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000553
Jim Laskey71382342006-10-07 23:37:56 +0000554 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000555 // encountered.
556 for (unsigned i = 0; i < TFs.size(); ++i) {
557 SDNode *TF = TFs[i];
558
Jim Laskey6ff23e52006-10-04 16:53:27 +0000559 // Check each of the operands.
560 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
561 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000562
Jim Laskey6ff23e52006-10-04 16:53:27 +0000563 switch (Op.getOpcode()) {
564 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000565 // Entry tokens don't need to be added to the list. They are
566 // rededundant.
567 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000568 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000569
Jim Laskey6ff23e52006-10-04 16:53:27 +0000570 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000571 if ((CombinerAA || Op.hasOneUse()) &&
572 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000573 // Queue up for processing.
574 TFs.push_back(Op.Val);
575 // Clean up in case the token factor is removed.
576 AddToWorkList(Op.Val);
577 Changed = true;
578 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000579 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000580 // Fall thru
581
582 default:
Jim Laskeybc588b82006-10-05 15:07:25 +0000583 // Only add if not there prior.
584 if (std::find(Ops.begin(), Ops.end(), Op) == Ops.end())
585 Ops.push_back(Op);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000586 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000587 }
588 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000589 }
590
591 SDOperand Result;
592
593 // If we've change things around then replace token factor.
594 if (Changed) {
595 if (Ops.size() == 0) {
596 // The entry token is the only possible outcome.
597 Result = DAG.getEntryNode();
598 } else {
599 // New and improved token factor.
600 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000601 }
602 }
Jim Laskey279f0532006-09-25 16:29:54 +0000603
Jim Laskey6ff23e52006-10-04 16:53:27 +0000604 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000605}
606
Nate Begeman83e75ec2005-09-06 04:43:02 +0000607SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000608 SDOperand N0 = N->getOperand(0);
609 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000610 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
611 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000612 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000613
614 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000615 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000616 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000617 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000618 if (N0C && !N1C)
619 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000620 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000621 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000622 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000623 // fold ((c1-A)+c2) -> (c1+c2)-A
624 if (N1C && N0.getOpcode() == ISD::SUB)
625 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
626 return DAG.getNode(ISD::SUB, VT,
627 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
628 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000629 // reassociate add
630 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
631 if (RADD.Val != 0)
632 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000633 // fold ((0-A) + B) -> B-A
634 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
635 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000636 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000637 // fold (A + (0-B)) -> A-B
638 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
639 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000640 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000641 // fold (A+(B-A)) -> B
642 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000643 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000644
Evan Cheng860771d2006-03-01 01:09:54 +0000645 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000646 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000647
648 // fold (a+b) -> (a|b) iff a and b share no bits.
649 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
650 uint64_t LHSZero, LHSOne;
651 uint64_t RHSZero, RHSOne;
652 uint64_t Mask = MVT::getIntVTBitMask(VT);
653 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
654 if (LHSZero) {
655 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
656
657 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
658 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
659 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
660 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
661 return DAG.getNode(ISD::OR, VT, N0, N1);
662 }
663 }
664
Nate Begeman83e75ec2005-09-06 04:43:02 +0000665 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000666}
667
Nate Begeman83e75ec2005-09-06 04:43:02 +0000668SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000669 SDOperand N0 = N->getOperand(0);
670 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000671 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
672 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000673 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000674
Chris Lattner854077d2005-10-17 01:07:11 +0000675 // fold (sub x, x) -> 0
676 if (N0 == N1)
677 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000678 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000679 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000680 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000681 // fold (sub x, c) -> (add x, -c)
682 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000683 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000684 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000685 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000686 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000687 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000688 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000689 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000690 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000691}
692
Nate Begeman83e75ec2005-09-06 04:43:02 +0000693SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000694 SDOperand N0 = N->getOperand(0);
695 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000696 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
697 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000698 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000699
700 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000701 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000702 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000703 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000704 if (N0C && !N1C)
705 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000706 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000707 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000708 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000709 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000710 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000711 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000712 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000713 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000714 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000715 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000716 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000717 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
718 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
719 // FIXME: If the input is something that is easily negated (e.g. a
720 // single-use add), we should put the negate there.
721 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
722 DAG.getNode(ISD::SHL, VT, N0,
723 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
724 TLI.getShiftAmountTy())));
725 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +0000726
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000727 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
728 if (N1C && N0.getOpcode() == ISD::SHL &&
729 isa<ConstantSDNode>(N0.getOperand(1))) {
730 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000731 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000732 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
733 }
734
735 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
736 // use.
737 {
738 SDOperand Sh(0,0), Y(0,0);
739 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
740 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
741 N0.Val->hasOneUse()) {
742 Sh = N0; Y = N1;
743 } else if (N1.getOpcode() == ISD::SHL &&
744 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
745 Sh = N1; Y = N0;
746 }
747 if (Sh.Val) {
748 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
749 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
750 }
751 }
Chris Lattnera1deca32006-03-04 23:33:26 +0000752 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
753 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
754 isa<ConstantSDNode>(N0.getOperand(1))) {
755 return DAG.getNode(ISD::ADD, VT,
756 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
757 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
758 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000759
Nate Begemancd4d58c2006-02-03 06:46:56 +0000760 // reassociate mul
761 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
762 if (RMUL.Val != 0)
763 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000764 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000765}
766
Nate Begeman83e75ec2005-09-06 04:43:02 +0000767SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000768 SDOperand N0 = N->getOperand(0);
769 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000770 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
771 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000772 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000773
774 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000775 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000776 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000777 // fold (sdiv X, 1) -> X
778 if (N1C && N1C->getSignExtended() == 1LL)
779 return N0;
780 // fold (sdiv X, -1) -> 0-X
781 if (N1C && N1C->isAllOnesValue())
782 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000783 // If we know the sign bits of both operands are zero, strength reduce to a
784 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
785 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000786 if (TLI.MaskedValueIsZero(N1, SignBit) &&
787 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000788 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000789 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000790 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000791 (isPowerOf2_64(N1C->getSignExtended()) ||
792 isPowerOf2_64(-N1C->getSignExtended()))) {
793 // If dividing by powers of two is cheap, then don't perform the following
794 // fold.
795 if (TLI.isPow2DivCheap())
796 return SDOperand();
797 int64_t pow2 = N1C->getSignExtended();
798 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000799 unsigned lg2 = Log2_64(abs2);
800 // Splat the sign bit into the register
801 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000802 DAG.getConstant(MVT::getSizeInBits(VT)-1,
803 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +0000804 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000805 // Add (N0 < 0) ? abs2 - 1 : 0;
806 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
807 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000808 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000809 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +0000810 AddToWorkList(SRL.Val);
811 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +0000812 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
813 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000814 // If we're dividing by a positive value, we're done. Otherwise, we must
815 // negate the result.
816 if (pow2 > 0)
817 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +0000818 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000819 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
820 }
Nate Begeman69575232005-10-20 02:15:44 +0000821 // if integer divide is expensive and we satisfy the requirements, emit an
822 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000823 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000824 !TLI.isIntDivCheap()) {
825 SDOperand Op = BuildSDIV(N);
826 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000827 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000828 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000829}
830
Nate Begeman83e75ec2005-09-06 04:43:02 +0000831SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000832 SDOperand N0 = N->getOperand(0);
833 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000834 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
835 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000836 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000837
838 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000839 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000840 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000841 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000842 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000843 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000844 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000845 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000846 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
847 if (N1.getOpcode() == ISD::SHL) {
848 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
849 if (isPowerOf2_64(SHC->getValue())) {
850 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000851 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
852 DAG.getConstant(Log2_64(SHC->getValue()),
853 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +0000854 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000855 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000856 }
857 }
858 }
Nate Begeman69575232005-10-20 02:15:44 +0000859 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000860 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
861 SDOperand Op = BuildUDIV(N);
862 if (Op.Val) return Op;
863 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000864 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000865}
866
Nate Begeman83e75ec2005-09-06 04:43:02 +0000867SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000868 SDOperand N0 = N->getOperand(0);
869 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000870 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
871 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000872 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000873
874 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000875 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000876 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000877 // If we know the sign bits of both operands are zero, strength reduce to a
878 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
879 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000880 if (TLI.MaskedValueIsZero(N1, SignBit) &&
881 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000882 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000883 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000884}
885
Nate Begeman83e75ec2005-09-06 04:43:02 +0000886SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000887 SDOperand N0 = N->getOperand(0);
888 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000889 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
890 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000891 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000892
893 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000894 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000895 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000896 // fold (urem x, pow2) -> (and x, pow2-1)
897 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000898 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000899 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
900 if (N1.getOpcode() == ISD::SHL) {
901 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
902 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000903 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +0000904 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000905 return DAG.getNode(ISD::AND, VT, N0, Add);
906 }
907 }
908 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000909 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000910}
911
Nate Begeman83e75ec2005-09-06 04:43:02 +0000912SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000913 SDOperand N0 = N->getOperand(0);
914 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000915 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000916
917 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000918 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000919 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000920 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000921 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000922 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
923 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000924 TLI.getShiftAmountTy()));
925 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000926}
927
Nate Begeman83e75ec2005-09-06 04:43:02 +0000928SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000929 SDOperand N0 = N->getOperand(0);
930 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000931 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000932
933 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000934 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000935 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000936 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000937 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000938 return DAG.getConstant(0, N0.getValueType());
939 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000940}
941
Chris Lattner35e5c142006-05-05 05:51:50 +0000942/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
943/// two operands of the same opcode, try to simplify it.
944SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
945 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
946 MVT::ValueType VT = N0.getValueType();
947 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
948
Chris Lattner540121f2006-05-05 06:31:05 +0000949 // For each of OP in AND/OR/XOR:
950 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
951 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
952 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +0000953 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +0000954 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +0000955 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000956 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
957 SDOperand ORNode = DAG.getNode(N->getOpcode(),
958 N0.getOperand(0).getValueType(),
959 N0.getOperand(0), N1.getOperand(0));
960 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +0000961 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +0000962 }
963
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000964 // For each of OP in SHL/SRL/SRA/AND...
965 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
966 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
967 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +0000968 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000969 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000970 N0.getOperand(1) == N1.getOperand(1)) {
971 SDOperand ORNode = DAG.getNode(N->getOpcode(),
972 N0.getOperand(0).getValueType(),
973 N0.getOperand(0), N1.getOperand(0));
974 AddToWorkList(ORNode.Val);
975 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
976 }
977
978 return SDOperand();
979}
980
Nate Begeman83e75ec2005-09-06 04:43:02 +0000981SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000982 SDOperand N0 = N->getOperand(0);
983 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +0000984 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000985 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
986 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000987 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000988 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000989
990 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000991 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000992 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000993 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000994 if (N0C && !N1C)
995 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000996 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000997 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000998 return N0;
999 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +00001000 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001001 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001002 // reassociate and
1003 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1004 if (RAND.Val != 0)
1005 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001006 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001007 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001008 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001009 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001010 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001011 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1012 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001013 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +00001014 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001015 ~N1C->getValue() & InMask)) {
1016 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1017 N0.getOperand(0));
1018
1019 // Replace uses of the AND with uses of the Zero extend node.
1020 CombineTo(N, Zext);
1021
Chris Lattner3603cd62006-02-02 07:17:31 +00001022 // We actually want to replace all uses of the any_extend with the
1023 // zero_extend, to avoid duplicating things. This will later cause this
1024 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001025 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001026 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001027 }
1028 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001029 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1030 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1031 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1032 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1033
1034 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1035 MVT::isInteger(LL.getValueType())) {
1036 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1037 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1038 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001039 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001040 return DAG.getSetCC(VT, ORNode, LR, Op1);
1041 }
1042 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1043 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1044 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001045 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001046 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1047 }
1048 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1049 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1050 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001051 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001052 return DAG.getSetCC(VT, ORNode, LR, Op1);
1053 }
1054 }
1055 // canonicalize equivalent to ll == rl
1056 if (LL == RR && LR == RL) {
1057 Op1 = ISD::getSetCCSwappedOperands(Op1);
1058 std::swap(RL, RR);
1059 }
1060 if (LL == RL && LR == RR) {
1061 bool isInteger = MVT::isInteger(LL.getValueType());
1062 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1063 if (Result != ISD::SETCC_INVALID)
1064 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1065 }
1066 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001067
1068 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1069 if (N0.getOpcode() == N1.getOpcode()) {
1070 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1071 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001072 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001073
Nate Begemande996292006-02-03 22:24:05 +00001074 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1075 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001076 if (!MVT::isVector(VT) &&
1077 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001078 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001079 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Chengc5484282006-10-04 00:56:09 +00001080 if (ISD::isEXTLoad(N0.Val)) {
Nate Begemanded49632005-10-13 03:11:28 +00001081 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001082 // If we zero all the possible extended bits, then we can turn this into
1083 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001084 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001085 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001086 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1087 N0.getOperand(1), N0.getOperand(2),
1088 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001089 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001090 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001091 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001092 }
1093 }
1094 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Chengc5484282006-10-04 00:56:09 +00001095 if (ISD::isSEXTLoad(N0.Val) && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001096 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
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))) {
Nate Begemanded49632005-10-13 03:11:28 +00001101 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1102 N0.getOperand(1), N0.getOperand(2),
1103 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)
1112 if (N1C &&
Evan Chengc5484282006-10-04 00:56:09 +00001113 (N0.getOpcode() == ISD::LOAD || ISD::isEXTLoad(N0.Val) ||
1114 ISD::isZEXTLoad(N0.Val)) &&
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001115 N0.hasOneUse()) {
1116 MVT::ValueType EVT, LoadedVT;
Chris Lattner15045b62006-02-28 06:35:35 +00001117 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
1126 LoadedVT = N0.getOpcode() == ISD::LOAD ? VT :
1127 cast<VTSDNode>(N0.getOperand(3))->getVT();
Chris Lattnere44be602006-04-04 17:39:18 +00001128 if (EVT != MVT::Other && LoadedVT > EVT &&
Evan Chengc5484282006-10-04 00:56:09 +00001129 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Chris Lattner15045b62006-02-28 06:35:35 +00001130 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1131 // For big endian targets, we need to add an offset to the pointer to load
1132 // the correct bytes. For little endian systems, we merely need to read
1133 // fewer bytes from the same pointer.
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001134 unsigned PtrOff =
1135 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1136 SDOperand NewPtr = N0.getOperand(1);
1137 if (!TLI.isLittleEndian())
1138 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1139 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001140 AddToWorkList(NewPtr.Val);
Chris Lattner15045b62006-02-28 06:35:35 +00001141 SDOperand Load =
1142 DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
1143 N0.getOperand(2), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001144 AddToWorkList(N);
Chris Lattner15045b62006-02-28 06:35:35 +00001145 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001146 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
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)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001860 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00001861 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001862 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1863 N0.getOperand(1), N0.getOperand(2),
1864 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001865 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001866 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1867 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001868 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00001869 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001870
1871 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1872 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Chengc5484282006-10-04 00:56:09 +00001873 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001874 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1875 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1876 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001877 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001878 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1879 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001880 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001881 }
1882
Nate Begeman83e75ec2005-09-06 04:43:02 +00001883 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001884}
1885
Nate Begeman83e75ec2005-09-06 04:43:02 +00001886SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001887 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001888 MVT::ValueType VT = N->getValueType(0);
1889
Nate Begeman1d4d4142005-09-01 00:19:25 +00001890 // fold (zext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001891 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001892 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001893 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001894 // fold (zext (aext x)) -> (zext x)
1895 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001896 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00001897
1898 // fold (zext (truncate x)) -> (and x, mask)
1899 if (N0.getOpcode() == ISD::TRUNCATE &&
1900 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
1901 SDOperand Op = N0.getOperand(0);
1902 if (Op.getValueType() < VT) {
1903 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
1904 } else if (Op.getValueType() > VT) {
1905 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
1906 }
1907 return DAG.getZeroExtendInReg(Op, N0.getValueType());
1908 }
1909
Chris Lattner111c2282006-09-21 06:14:31 +00001910 // fold (zext (and (trunc x), cst)) -> (and x, cst).
1911 if (N0.getOpcode() == ISD::AND &&
1912 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1913 N0.getOperand(1).getOpcode() == ISD::Constant) {
1914 SDOperand X = N0.getOperand(0).getOperand(0);
1915 if (X.getValueType() < VT) {
1916 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
1917 } else if (X.getValueType() > VT) {
1918 X = DAG.getNode(ISD::TRUNCATE, VT, X);
1919 }
1920 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1921 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
1922 }
1923
Evan Cheng110dec22005-12-14 02:19:23 +00001924 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001925 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00001926 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng110dec22005-12-14 02:19:23 +00001927 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1928 N0.getOperand(1), N0.getOperand(2),
1929 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001930 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001931 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1932 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001933 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00001934 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001935
1936 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1937 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Chengc5484282006-10-04 00:56:09 +00001938 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001939 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1940 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1941 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001942 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001943 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1944 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001945 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001946 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001947 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001948}
1949
Chris Lattner5ffc0662006-05-05 05:58:59 +00001950SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
1951 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001952 MVT::ValueType VT = N->getValueType(0);
1953
1954 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001955 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00001956 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
1957 // fold (aext (aext x)) -> (aext x)
1958 // fold (aext (zext x)) -> (zext x)
1959 // fold (aext (sext x)) -> (sext x)
1960 if (N0.getOpcode() == ISD::ANY_EXTEND ||
1961 N0.getOpcode() == ISD::ZERO_EXTEND ||
1962 N0.getOpcode() == ISD::SIGN_EXTEND)
1963 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
1964
Chris Lattner84750582006-09-20 06:29:17 +00001965 // fold (aext (truncate x))
1966 if (N0.getOpcode() == ISD::TRUNCATE) {
1967 SDOperand TruncOp = N0.getOperand(0);
1968 if (TruncOp.getValueType() == VT)
1969 return TruncOp; // x iff x size == zext size.
1970 if (TruncOp.getValueType() > VT)
1971 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
1972 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
1973 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00001974
1975 // fold (aext (and (trunc x), cst)) -> (and x, cst).
1976 if (N0.getOpcode() == ISD::AND &&
1977 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1978 N0.getOperand(1).getOpcode() == ISD::Constant) {
1979 SDOperand X = N0.getOperand(0).getOperand(0);
1980 if (X.getValueType() < VT) {
1981 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
1982 } else if (X.getValueType() > VT) {
1983 X = DAG.getNode(ISD::TRUNCATE, VT, X);
1984 }
1985 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1986 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
1987 }
1988
Chris Lattner5ffc0662006-05-05 05:58:59 +00001989 // fold (aext (load x)) -> (aext (truncate (extload x)))
1990 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00001991 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Chris Lattner5ffc0662006-05-05 05:58:59 +00001992 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0),
1993 N0.getOperand(1), N0.getOperand(2),
1994 N0.getValueType());
1995 CombineTo(N, ExtLoad);
1996 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1997 ExtLoad.getValue(1));
1998 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1999 }
2000
2001 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2002 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2003 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Chengc5484282006-10-04 00:56:09 +00002004 if (N0.getOpcode() == ISD::LOADX && N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00002005 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Evan Chengc5484282006-10-04 00:56:09 +00002006 unsigned LType = N0.getConstantOperandVal(4);
2007 SDOperand ExtLoad = DAG.getExtLoad((ISD::LoadExtType)LType, VT,
2008 N0.getOperand(0), N0.getOperand(1),
2009 N0.getOperand(2), EVT);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002010 CombineTo(N, ExtLoad);
2011 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2012 ExtLoad.getValue(1));
2013 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2014 }
2015 return SDOperand();
2016}
2017
2018
Nate Begeman83e75ec2005-09-06 04:43:02 +00002019SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002020 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002021 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002022 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002023 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00002024 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002025
Nate Begeman1d4d4142005-09-01 00:19:25 +00002026 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00002027 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00002028 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00002029
Chris Lattner541a24f2006-05-06 22:43:44 +00002030 // If the input is already sign extended, just drop the extension.
Chris Lattneree4ea922006-05-06 09:30:03 +00002031 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
2032 return N0;
2033
Nate Begeman646d7e22005-09-02 21:18:40 +00002034 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
2035 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2036 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00002037 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002038 }
Chris Lattner4b37e872006-05-08 21:18:59 +00002039
Nate Begeman07ed4172005-10-10 21:26:48 +00002040 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00002041 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00002042 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00002043
2044 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
2045 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
2046 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
2047 if (N0.getOpcode() == ISD::SRL) {
2048 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2049 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
2050 // We can turn this into an SRA iff the input to the SRL is already sign
2051 // extended enough.
2052 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
2053 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
2054 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
2055 }
2056 }
2057
Nate Begemanded49632005-10-13 03:11:28 +00002058 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00002059 if (ISD::isEXTLoad(N0.Val) &&
Nate Begemanded49632005-10-13 03:11:28 +00002060 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002061 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00002062 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
2063 N0.getOperand(1), N0.getOperand(2),
2064 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002065 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002066 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002067 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002068 }
2069 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Chengc5484282006-10-04 00:56:09 +00002070 if (ISD::isZEXTLoad(N0.Val) && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00002071 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002072 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00002073 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
2074 N0.getOperand(1), N0.getOperand(2),
2075 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002076 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002077 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002078 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002079 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002080 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002081}
2082
Nate Begeman83e75ec2005-09-06 04:43:02 +00002083SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002084 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002085 MVT::ValueType VT = N->getValueType(0);
2086
2087 // noop truncate
2088 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002089 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002090 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002091 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002092 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002093 // fold (truncate (truncate x)) -> (truncate x)
2094 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002095 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002096 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00002097 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2098 N0.getOpcode() == ISD::ANY_EXTEND) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002099 if (N0.getValueType() < VT)
2100 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00002101 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002102 else if (N0.getValueType() > VT)
2103 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002104 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002105 else
2106 // if the source and dest are the same type, we can drop both the extend
2107 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002108 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002109 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002110 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00002111 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00002112 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
2113 "Cannot truncate to larger type!");
2114 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00002115 // For big endian targets, we need to add an offset to the pointer to load
2116 // the correct bytes. For little endian systems, we merely need to read
2117 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00002118 uint64_t PtrOff =
2119 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00002120 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
2121 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
2122 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00002123 AddToWorkList(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00002124 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00002125 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00002126 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002127 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002128 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002129 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002130}
2131
Chris Lattner94683772005-12-23 05:30:37 +00002132SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2133 SDOperand N0 = N->getOperand(0);
2134 MVT::ValueType VT = N->getValueType(0);
2135
2136 // If the input is a constant, let getNode() fold it.
2137 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2138 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2139 if (Res.Val != N) return Res;
2140 }
2141
Chris Lattnerc8547d82005-12-23 05:37:50 +00002142 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2143 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00002144
Chris Lattner57104102005-12-23 05:44:41 +00002145 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002146 // FIXME: These xforms need to know that the resultant load doesn't need a
2147 // higher alignment than the original!
2148 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00002149 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
2150 N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00002151 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00002152 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2153 Load.getValue(1));
2154 return Load;
2155 }
2156
Chris Lattner94683772005-12-23 05:30:37 +00002157 return SDOperand();
2158}
2159
Chris Lattner6258fb22006-04-02 02:53:43 +00002160SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2161 SDOperand N0 = N->getOperand(0);
2162 MVT::ValueType VT = N->getValueType(0);
2163
2164 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2165 // First check to see if this is all constant.
2166 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2167 VT == MVT::Vector) {
2168 bool isSimple = true;
2169 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2170 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2171 N0.getOperand(i).getOpcode() != ISD::Constant &&
2172 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2173 isSimple = false;
2174 break;
2175 }
2176
Chris Lattner97c20732006-04-03 17:29:28 +00002177 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2178 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002179 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2180 }
2181 }
2182
2183 return SDOperand();
2184}
2185
2186/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2187/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2188/// destination element value type.
2189SDOperand DAGCombiner::
2190ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2191 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2192
2193 // If this is already the right type, we're done.
2194 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2195
2196 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2197 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2198
2199 // If this is a conversion of N elements of one type to N elements of another
2200 // type, convert each element. This handles FP<->INT cases.
2201 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002202 SmallVector<SDOperand, 8> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00002203 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002204 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002205 AddToWorkList(Ops.back().Val);
2206 }
Chris Lattner6258fb22006-04-02 02:53:43 +00002207 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2208 Ops.push_back(DAG.getValueType(DstEltVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002209 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002210 }
2211
2212 // Otherwise, we're growing or shrinking the elements. To avoid having to
2213 // handle annoying details of growing/shrinking FP values, we convert them to
2214 // int first.
2215 if (MVT::isFloatingPoint(SrcEltVT)) {
2216 // Convert the input float vector to a int vector where the elements are the
2217 // same sizes.
2218 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2219 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2220 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2221 SrcEltVT = IntVT;
2222 }
2223
2224 // Now we know the input is an integer vector. If the output is a FP type,
2225 // convert to integer first, then to FP of the right size.
2226 if (MVT::isFloatingPoint(DstEltVT)) {
2227 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2228 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2229 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2230
2231 // Next, convert to FP elements of the same size.
2232 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2233 }
2234
2235 // Okay, we know the src/dst types are both integers of differing types.
2236 // Handling growing first.
2237 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2238 if (SrcBitSize < DstBitSize) {
2239 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2240
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002241 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002242 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2243 i += NumInputsPerOutput) {
2244 bool isLE = TLI.isLittleEndian();
2245 uint64_t NewBits = 0;
2246 bool EltIsUndef = true;
2247 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2248 // Shift the previously computed bits over.
2249 NewBits <<= SrcBitSize;
2250 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2251 if (Op.getOpcode() == ISD::UNDEF) continue;
2252 EltIsUndef = false;
2253
2254 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2255 }
2256
2257 if (EltIsUndef)
2258 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2259 else
2260 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2261 }
2262
2263 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2264 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002265 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002266 }
2267
2268 // Finally, this must be the case where we are shrinking elements: each input
2269 // turns into multiple outputs.
2270 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002271 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002272 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2273 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2274 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2275 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2276 continue;
2277 }
2278 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2279
2280 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2281 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2282 OpVal >>= DstBitSize;
2283 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2284 }
2285
2286 // For big endian targets, swap the order of the pieces of each element.
2287 if (!TLI.isLittleEndian())
2288 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2289 }
2290 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2291 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002292 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002293}
2294
2295
2296
Chris Lattner01b3d732005-09-28 22:28:18 +00002297SDOperand DAGCombiner::visitFADD(SDNode *N) {
2298 SDOperand N0 = N->getOperand(0);
2299 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002300 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2301 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002302 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002303
2304 // fold (fadd c1, c2) -> c1+c2
2305 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002306 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002307 // canonicalize constant to RHS
2308 if (N0CFP && !N1CFP)
2309 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002310 // fold (A + (-B)) -> A-B
2311 if (N1.getOpcode() == ISD::FNEG)
2312 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002313 // fold ((-A) + B) -> B-A
2314 if (N0.getOpcode() == ISD::FNEG)
2315 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002316 return SDOperand();
2317}
2318
2319SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2320 SDOperand N0 = N->getOperand(0);
2321 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002322 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2323 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002324 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002325
2326 // fold (fsub c1, c2) -> c1-c2
2327 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002328 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002329 // fold (A-(-B)) -> A+B
2330 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00002331 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002332 return SDOperand();
2333}
2334
2335SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2336 SDOperand N0 = N->getOperand(0);
2337 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002338 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2339 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002340 MVT::ValueType VT = N->getValueType(0);
2341
Nate Begeman11af4ea2005-10-17 20:40:11 +00002342 // fold (fmul c1, c2) -> c1*c2
2343 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002344 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002345 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002346 if (N0CFP && !N1CFP)
2347 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002348 // fold (fmul X, 2.0) -> (fadd X, X)
2349 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2350 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002351 return SDOperand();
2352}
2353
2354SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2355 SDOperand N0 = N->getOperand(0);
2356 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002357 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2358 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002359 MVT::ValueType VT = N->getValueType(0);
2360
Nate Begemana148d982006-01-18 22:35:16 +00002361 // fold (fdiv c1, c2) -> c1/c2
2362 if (N0CFP && N1CFP)
2363 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002364 return SDOperand();
2365}
2366
2367SDOperand DAGCombiner::visitFREM(SDNode *N) {
2368 SDOperand N0 = N->getOperand(0);
2369 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002370 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2371 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002372 MVT::ValueType VT = N->getValueType(0);
2373
Nate Begemana148d982006-01-18 22:35:16 +00002374 // fold (frem c1, c2) -> fmod(c1,c2)
2375 if (N0CFP && N1CFP)
2376 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002377 return SDOperand();
2378}
2379
Chris Lattner12d83032006-03-05 05:30:57 +00002380SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2381 SDOperand N0 = N->getOperand(0);
2382 SDOperand N1 = N->getOperand(1);
2383 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2384 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2385 MVT::ValueType VT = N->getValueType(0);
2386
2387 if (N0CFP && N1CFP) // Constant fold
2388 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2389
2390 if (N1CFP) {
2391 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2392 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2393 union {
2394 double d;
2395 int64_t i;
2396 } u;
2397 u.d = N1CFP->getValue();
2398 if (u.i >= 0)
2399 return DAG.getNode(ISD::FABS, VT, N0);
2400 else
2401 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2402 }
2403
2404 // copysign(fabs(x), y) -> copysign(x, y)
2405 // copysign(fneg(x), y) -> copysign(x, y)
2406 // copysign(copysign(x,z), y) -> copysign(x, y)
2407 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2408 N0.getOpcode() == ISD::FCOPYSIGN)
2409 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2410
2411 // copysign(x, abs(y)) -> abs(x)
2412 if (N1.getOpcode() == ISD::FABS)
2413 return DAG.getNode(ISD::FABS, VT, N0);
2414
2415 // copysign(x, copysign(y,z)) -> copysign(x, z)
2416 if (N1.getOpcode() == ISD::FCOPYSIGN)
2417 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2418
2419 // copysign(x, fp_extend(y)) -> copysign(x, y)
2420 // copysign(x, fp_round(y)) -> copysign(x, y)
2421 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2422 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2423
2424 return SDOperand();
2425}
2426
2427
Chris Lattner01b3d732005-09-28 22:28:18 +00002428
Nate Begeman83e75ec2005-09-06 04:43:02 +00002429SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002430 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002431 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002432 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002433
2434 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002435 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002436 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002437 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002438}
2439
Nate Begeman83e75ec2005-09-06 04:43:02 +00002440SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002441 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002442 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002443 MVT::ValueType VT = N->getValueType(0);
2444
Nate Begeman1d4d4142005-09-01 00:19:25 +00002445 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002446 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002447 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002448 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002449}
2450
Nate Begeman83e75ec2005-09-06 04:43:02 +00002451SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002452 SDOperand N0 = N->getOperand(0);
2453 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2454 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002455
2456 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002457 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002458 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002459 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002460}
2461
Nate Begeman83e75ec2005-09-06 04:43:02 +00002462SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002463 SDOperand N0 = N->getOperand(0);
2464 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2465 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002466
2467 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002468 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002469 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002470 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002471}
2472
Nate Begeman83e75ec2005-09-06 04:43:02 +00002473SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002474 SDOperand N0 = N->getOperand(0);
2475 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2476 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002477
2478 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002479 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002480 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002481
2482 // fold (fp_round (fp_extend x)) -> x
2483 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2484 return N0.getOperand(0);
2485
2486 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2487 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2488 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2489 AddToWorkList(Tmp.Val);
2490 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2491 }
2492
Nate Begeman83e75ec2005-09-06 04:43:02 +00002493 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002494}
2495
Nate Begeman83e75ec2005-09-06 04:43:02 +00002496SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002497 SDOperand N0 = N->getOperand(0);
2498 MVT::ValueType VT = N->getValueType(0);
2499 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002500 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002501
Nate Begeman1d4d4142005-09-01 00:19:25 +00002502 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002503 if (N0CFP) {
2504 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002505 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002506 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002507 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002508}
2509
Nate Begeman83e75ec2005-09-06 04:43:02 +00002510SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002511 SDOperand N0 = N->getOperand(0);
2512 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2513 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002514
2515 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002516 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002517 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00002518
2519 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
2520 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002521 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Chris Lattnere564dbb2006-05-05 21:34:35 +00002522 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0),
2523 N0.getOperand(1), N0.getOperand(2),
2524 N0.getValueType());
2525 CombineTo(N, ExtLoad);
2526 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
2527 ExtLoad.getValue(1));
2528 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2529 }
2530
2531
Nate Begeman83e75ec2005-09-06 04:43:02 +00002532 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002533}
2534
Nate Begeman83e75ec2005-09-06 04:43:02 +00002535SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002536 SDOperand N0 = N->getOperand(0);
2537 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2538 MVT::ValueType VT = N->getValueType(0);
2539
2540 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002541 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002542 return DAG.getNode(ISD::FNEG, VT, N0);
2543 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002544 if (N0.getOpcode() == ISD::SUB)
2545 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002546 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002547 if (N0.getOpcode() == ISD::FNEG)
2548 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002549 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002550}
2551
Nate Begeman83e75ec2005-09-06 04:43:02 +00002552SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002553 SDOperand N0 = N->getOperand(0);
2554 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2555 MVT::ValueType VT = N->getValueType(0);
2556
Nate Begeman1d4d4142005-09-01 00:19:25 +00002557 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002558 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002559 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002560 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002561 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002562 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002563 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002564 // fold (fabs (fcopysign x, y)) -> (fabs x)
2565 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2566 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2567
Nate Begeman83e75ec2005-09-06 04:43:02 +00002568 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002569}
2570
Nate Begeman44728a72005-09-19 22:34:01 +00002571SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2572 SDOperand Chain = N->getOperand(0);
2573 SDOperand N1 = N->getOperand(1);
2574 SDOperand N2 = N->getOperand(2);
2575 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2576
2577 // never taken branch, fold to chain
2578 if (N1C && N1C->isNullValue())
2579 return Chain;
2580 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002581 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002582 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002583 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2584 // on the target.
2585 if (N1.getOpcode() == ISD::SETCC &&
2586 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2587 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2588 N1.getOperand(0), N1.getOperand(1), N2);
2589 }
Nate Begeman44728a72005-09-19 22:34:01 +00002590 return SDOperand();
2591}
2592
Chris Lattner3ea0b472005-10-05 06:47:48 +00002593// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2594//
Nate Begeman44728a72005-09-19 22:34:01 +00002595SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002596 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2597 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2598
2599 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002600 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2601 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2602
2603 // fold br_cc true, dest -> br dest (unconditional branch)
2604 if (SCCC && SCCC->getValue())
2605 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2606 N->getOperand(4));
2607 // fold br_cc false, dest -> unconditional fall through
2608 if (SCCC && SCCC->isNullValue())
2609 return N->getOperand(0);
2610 // fold to a simpler setcc
2611 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2612 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2613 Simp.getOperand(2), Simp.getOperand(0),
2614 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002615 return SDOperand();
2616}
2617
Chris Lattner01a22022005-10-10 22:04:48 +00002618SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2619 SDOperand Chain = N->getOperand(0);
2620 SDOperand Ptr = N->getOperand(1);
2621 SDOperand SrcValue = N->getOperand(2);
Jim Laskey6ff23e52006-10-04 16:53:27 +00002622
Chris Lattnere4b95392006-03-31 18:06:18 +00002623 // If there are no uses of the loaded value, change uses of the chain value
2624 // into uses of the chain input (i.e. delete the dead load).
2625 if (N->hasNUsesOfValue(0, 0))
2626 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
Chris Lattner01a22022005-10-10 22:04:48 +00002627
2628 // If this load is directly stored, replace the load value with the stored
2629 // value.
2630 // TODO: Handle store large -> read small portion.
2631 // TODO: Handle TRUNCSTORE/EXTLOAD
2632 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2633 Chain.getOperand(1).getValueType() == N->getValueType(0))
2634 return CombineTo(N, Chain.getOperand(1), Chain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00002635
Jim Laskeybb151852006-09-26 17:44:58 +00002636 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00002637 // Walk up chain skipping non-aliasing memory nodes.
2638 SDOperand BetterChain = FindBetterChain(N, Chain);
2639
Jim Laskey6ff23e52006-10-04 16:53:27 +00002640 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00002641 if (Chain != BetterChain) {
2642 // Replace the chain to void dependency.
2643 SDOperand ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
2644 SrcValue);
2645
Jim Laskey6ff23e52006-10-04 16:53:27 +00002646 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00002647 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
2648 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00002649
2650 // Replace uses with load result and token factor.
2651 return CombineTo(N, ReplLoad.getValue(0), Token);
Jim Laskey279f0532006-09-25 16:29:54 +00002652 }
2653 }
2654
Chris Lattner01a22022005-10-10 22:04:48 +00002655 return SDOperand();
2656}
2657
Evan Chengc5484282006-10-04 00:56:09 +00002658/// visitLOADX - Handle EXTLOAD/ZEXTLOAD/SEXTLOAD.
2659SDOperand DAGCombiner::visitLOADX(SDNode *N) {
Chris Lattner29cd7db2006-03-31 18:10:41 +00002660 SDOperand Chain = N->getOperand(0);
2661 SDOperand Ptr = N->getOperand(1);
2662 SDOperand SrcValue = N->getOperand(2);
2663 SDOperand EVT = N->getOperand(3);
2664
2665 // If there are no uses of the loaded value, change uses of the chain value
2666 // into uses of the chain input (i.e. delete the dead load).
2667 if (N->hasNUsesOfValue(0, 0))
2668 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
2669
2670 return SDOperand();
2671}
2672
Chris Lattner87514ca2005-10-10 22:31:19 +00002673SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2674 SDOperand Chain = N->getOperand(0);
2675 SDOperand Value = N->getOperand(1);
2676 SDOperand Ptr = N->getOperand(2);
2677 SDOperand SrcValue = N->getOperand(3);
2678
2679 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002680 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002681 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2682 // Make sure that these stores are the same value type:
2683 // FIXME: we really care that the second store is >= size of the first.
2684 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002685 // Create a new store of Value that replaces both stores.
2686 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002687 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2688 return Chain;
Evan Cheng786225a2006-10-05 23:01:46 +00002689 SDOperand NewStore = DAG.getStore(PrevStore->getOperand(0), Value, Ptr,
2690 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002691 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002692 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002693 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002694 }
2695
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002696 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002697 // FIXME: This needs to know that the resultant store does not need a
2698 // higher alignment than the original.
Jim Laskey14fbcbf2006-09-25 21:11:32 +00002699 if (0 && Value.getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng786225a2006-10-05 23:01:46 +00002700 return DAG.getStore(Chain, Value.getOperand(0), Ptr, SrcValue);
Jim Laskey279f0532006-09-25 16:29:54 +00002701 }
2702
2703 if (CombinerAA) {
Jim Laskey288af5e2006-09-25 19:32:58 +00002704 // If the store ptr is a frame index and the frame index has a use of one
2705 // and this is a return block, then the store is redundant.
2706 if (Ptr.hasOneUse() && isa<FrameIndexSDNode>(Ptr) &&
2707 DAG.getRoot().getOpcode() == ISD::RET) {
2708 return Chain;
2709 }
2710
Jim Laskey279f0532006-09-25 16:29:54 +00002711 // Walk up chain skipping non-aliasing memory nodes.
2712 SDOperand BetterChain = FindBetterChain(N, Chain);
2713
Jim Laskey6ff23e52006-10-04 16:53:27 +00002714 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00002715 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00002716 // Replace the chain to avoid dependency.
Evan Cheng786225a2006-10-05 23:01:46 +00002717 SDOperand ReplStore = DAG.getStore(BetterChain, Value, Ptr, SrcValue);
Jim Laskey279f0532006-09-25 16:29:54 +00002718 // Create token to keep both nodes around.
Jim Laskey6ff23e52006-10-04 16:53:27 +00002719 return DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
Jim Laskey279f0532006-09-25 16:29:54 +00002720 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00002721 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002722
Chris Lattner87514ca2005-10-10 22:31:19 +00002723 return SDOperand();
2724}
2725
Chris Lattnerca242442006-03-19 01:27:56 +00002726SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2727 SDOperand InVec = N->getOperand(0);
2728 SDOperand InVal = N->getOperand(1);
2729 SDOperand EltNo = N->getOperand(2);
2730
2731 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2732 // vector with the inserted element.
2733 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2734 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002735 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002736 if (Elt < Ops.size())
2737 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002738 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
2739 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002740 }
2741
2742 return SDOperand();
2743}
2744
2745SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2746 SDOperand InVec = N->getOperand(0);
2747 SDOperand InVal = N->getOperand(1);
2748 SDOperand EltNo = N->getOperand(2);
2749 SDOperand NumElts = N->getOperand(3);
2750 SDOperand EltType = N->getOperand(4);
2751
2752 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2753 // vector with the inserted element.
2754 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2755 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002756 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002757 if (Elt < Ops.size()-2)
2758 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002759 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
2760 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002761 }
2762
2763 return SDOperand();
2764}
2765
Chris Lattnerd7648c82006-03-28 20:28:38 +00002766SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
2767 unsigned NumInScalars = N->getNumOperands()-2;
2768 SDOperand NumElts = N->getOperand(NumInScalars);
2769 SDOperand EltType = N->getOperand(NumInScalars+1);
2770
2771 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
2772 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
2773 // two distinct vectors, turn this into a shuffle node.
2774 SDOperand VecIn1, VecIn2;
2775 for (unsigned i = 0; i != NumInScalars; ++i) {
2776 // Ignore undef inputs.
2777 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
2778
2779 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
2780 // constant index, bail out.
2781 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
2782 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
2783 VecIn1 = VecIn2 = SDOperand(0, 0);
2784 break;
2785 }
2786
2787 // If the input vector type disagrees with the result of the vbuild_vector,
2788 // we can't make a shuffle.
2789 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
2790 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
2791 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
2792 VecIn1 = VecIn2 = SDOperand(0, 0);
2793 break;
2794 }
2795
2796 // Otherwise, remember this. We allow up to two distinct input vectors.
2797 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
2798 continue;
2799
2800 if (VecIn1.Val == 0) {
2801 VecIn1 = ExtractedFromVec;
2802 } else if (VecIn2.Val == 0) {
2803 VecIn2 = ExtractedFromVec;
2804 } else {
2805 // Too many inputs.
2806 VecIn1 = VecIn2 = SDOperand(0, 0);
2807 break;
2808 }
2809 }
2810
2811 // If everything is good, we can make a shuffle operation.
2812 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002813 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00002814 for (unsigned i = 0; i != NumInScalars; ++i) {
2815 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
2816 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2817 continue;
2818 }
2819
2820 SDOperand Extract = N->getOperand(i);
2821
2822 // If extracting from the first vector, just use the index directly.
2823 if (Extract.getOperand(0) == VecIn1) {
2824 BuildVecIndices.push_back(Extract.getOperand(1));
2825 continue;
2826 }
2827
2828 // Otherwise, use InIdx + VecSize
2829 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
2830 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
2831 }
2832
2833 // Add count and size info.
2834 BuildVecIndices.push_back(NumElts);
2835 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
2836
2837 // Return the new VVECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002838 SDOperand Ops[5];
2839 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00002840 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002841 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00002842 } else {
2843 // Use an undef vbuild_vector as input for the second operand.
2844 std::vector<SDOperand> UnOps(NumInScalars,
2845 DAG.getNode(ISD::UNDEF,
2846 cast<VTSDNode>(EltType)->getVT()));
2847 UnOps.push_back(NumElts);
2848 UnOps.push_back(EltType);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002849 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2850 &UnOps[0], UnOps.size());
2851 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00002852 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002853 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2854 &BuildVecIndices[0], BuildVecIndices.size());
2855 Ops[3] = NumElts;
2856 Ops[4] = EltType;
2857 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
Chris Lattnerd7648c82006-03-28 20:28:38 +00002858 }
2859
2860 return SDOperand();
2861}
2862
Chris Lattner66445d32006-03-28 22:11:53 +00002863SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002864 SDOperand ShufMask = N->getOperand(2);
2865 unsigned NumElts = ShufMask.getNumOperands();
2866
2867 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2868 bool isIdentity = true;
2869 for (unsigned i = 0; i != NumElts; ++i) {
2870 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2871 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2872 isIdentity = false;
2873 break;
2874 }
2875 }
2876 if (isIdentity) return N->getOperand(0);
2877
2878 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2879 isIdentity = true;
2880 for (unsigned i = 0; i != NumElts; ++i) {
2881 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2882 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2883 isIdentity = false;
2884 break;
2885 }
2886 }
2887 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00002888
2889 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
2890 // needed at all.
2891 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00002892 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002893 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00002894 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002895 for (unsigned i = 0; i != NumElts; ++i)
2896 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
2897 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
2898 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00002899 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00002900 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00002901 BaseIdx = Idx;
2902 } else {
2903 if (BaseIdx != Idx)
2904 isSplat = false;
2905 if (VecNum != V) {
2906 isUnary = false;
2907 break;
2908 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00002909 }
2910 }
2911
2912 SDOperand N0 = N->getOperand(0);
2913 SDOperand N1 = N->getOperand(1);
2914 // Normalize unary shuffle so the RHS is undef.
2915 if (isUnary && VecNum == 1)
2916 std::swap(N0, N1);
2917
Evan Cheng917ec982006-07-21 08:25:53 +00002918 // If it is a splat, check if the argument vector is a build_vector with
2919 // all scalar elements the same.
2920 if (isSplat) {
2921 SDNode *V = N0.Val;
2922 if (V->getOpcode() == ISD::BIT_CONVERT)
2923 V = V->getOperand(0).Val;
2924 if (V->getOpcode() == ISD::BUILD_VECTOR) {
2925 unsigned NumElems = V->getNumOperands()-2;
2926 if (NumElems > BaseIdx) {
2927 SDOperand Base;
2928 bool AllSame = true;
2929 for (unsigned i = 0; i != NumElems; ++i) {
2930 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
2931 Base = V->getOperand(i);
2932 break;
2933 }
2934 }
2935 // Splat of <u, u, u, u>, return <u, u, u, u>
2936 if (!Base.Val)
2937 return N0;
2938 for (unsigned i = 0; i != NumElems; ++i) {
2939 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
2940 V->getOperand(i) != Base) {
2941 AllSame = false;
2942 break;
2943 }
2944 }
2945 // Splat of <x, x, x, x>, return <x, x, x, x>
2946 if (AllSame)
2947 return N0;
2948 }
2949 }
2950 }
2951
Evan Chenge7bec0d2006-07-20 22:44:41 +00002952 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
2953 // into an undef.
2954 if (isUnary || N0 == N1) {
2955 if (N0.getOpcode() == ISD::UNDEF)
Evan Chengc04766a2006-04-06 23:20:43 +00002956 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00002957 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2958 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002959 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00002960 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00002961 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2962 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2963 MappedOps.push_back(ShufMask.getOperand(i));
2964 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00002965 unsigned NewIdx =
2966 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2967 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00002968 }
2969 }
2970 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002971 &MappedOps[0], MappedOps.size());
Chris Lattner3e104b12006-04-08 04:15:24 +00002972 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00002973 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
Evan Chenge7bec0d2006-07-20 22:44:41 +00002974 N0,
Chris Lattner66445d32006-03-28 22:11:53 +00002975 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
2976 ShufMask);
2977 }
2978
2979 return SDOperand();
2980}
2981
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002982SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
2983 SDOperand ShufMask = N->getOperand(2);
2984 unsigned NumElts = ShufMask.getNumOperands()-2;
2985
2986 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2987 bool isIdentity = true;
2988 for (unsigned i = 0; i != NumElts; ++i) {
2989 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2990 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2991 isIdentity = false;
2992 break;
2993 }
2994 }
2995 if (isIdentity) return N->getOperand(0);
2996
2997 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2998 isIdentity = true;
2999 for (unsigned i = 0; i != NumElts; ++i) {
3000 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3001 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3002 isIdentity = false;
3003 break;
3004 }
3005 }
3006 if (isIdentity) return N->getOperand(1);
3007
Evan Chenge7bec0d2006-07-20 22:44:41 +00003008 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3009 // needed at all.
3010 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003011 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003012 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003013 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003014 for (unsigned i = 0; i != NumElts; ++i)
3015 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3016 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3017 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003018 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003019 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003020 BaseIdx = Idx;
3021 } else {
3022 if (BaseIdx != Idx)
3023 isSplat = false;
3024 if (VecNum != V) {
3025 isUnary = false;
3026 break;
3027 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003028 }
3029 }
3030
3031 SDOperand N0 = N->getOperand(0);
3032 SDOperand N1 = N->getOperand(1);
3033 // Normalize unary shuffle so the RHS is undef.
3034 if (isUnary && VecNum == 1)
3035 std::swap(N0, N1);
3036
Evan Cheng917ec982006-07-21 08:25:53 +00003037 // If it is a splat, check if the argument vector is a build_vector with
3038 // all scalar elements the same.
3039 if (isSplat) {
3040 SDNode *V = N0.Val;
3041 if (V->getOpcode() == ISD::VBIT_CONVERT)
3042 V = V->getOperand(0).Val;
3043 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
3044 unsigned NumElems = V->getNumOperands()-2;
3045 if (NumElems > BaseIdx) {
3046 SDOperand Base;
3047 bool AllSame = true;
3048 for (unsigned i = 0; i != NumElems; ++i) {
3049 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3050 Base = V->getOperand(i);
3051 break;
3052 }
3053 }
3054 // Splat of <u, u, u, u>, return <u, u, u, u>
3055 if (!Base.Val)
3056 return N0;
3057 for (unsigned i = 0; i != NumElems; ++i) {
3058 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3059 V->getOperand(i) != Base) {
3060 AllSame = false;
3061 break;
3062 }
3063 }
3064 // Splat of <x, x, x, x>, return <x, x, x, x>
3065 if (AllSame)
3066 return N0;
3067 }
3068 }
3069 }
3070
Evan Chenge7bec0d2006-07-20 22:44:41 +00003071 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3072 // into an undef.
3073 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00003074 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3075 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003076 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00003077 for (unsigned i = 0; i != NumElts; ++i) {
3078 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3079 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3080 MappedOps.push_back(ShufMask.getOperand(i));
3081 } else {
3082 unsigned NewIdx =
3083 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3084 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
3085 }
3086 }
3087 // Add the type/#elts values.
3088 MappedOps.push_back(ShufMask.getOperand(NumElts));
3089 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
3090
3091 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003092 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003093 AddToWorkList(ShufMask.Val);
3094
3095 // Build the undef vector.
3096 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
3097 for (unsigned i = 0; i != NumElts; ++i)
3098 MappedOps[i] = UDVal;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003099 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
3100 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003101 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3102 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003103
3104 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
Evan Chenge7bec0d2006-07-20 22:44:41 +00003105 N0, UDVal, ShufMask,
Chris Lattner17614ea2006-04-08 05:34:25 +00003106 MappedOps[NumElts], MappedOps[NumElts+1]);
3107 }
3108
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003109 return SDOperand();
3110}
3111
Evan Cheng44f1f092006-04-20 08:56:16 +00003112/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
3113/// a VAND to a vector_shuffle with the destination vector and a zero vector.
3114/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
3115/// vector_shuffle V, Zero, <0, 4, 2, 4>
3116SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
3117 SDOperand LHS = N->getOperand(0);
3118 SDOperand RHS = N->getOperand(1);
3119 if (N->getOpcode() == ISD::VAND) {
3120 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
3121 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
3122 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
3123 RHS = RHS.getOperand(0);
3124 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
3125 std::vector<SDOperand> IdxOps;
3126 unsigned NumOps = RHS.getNumOperands();
3127 unsigned NumElts = NumOps-2;
3128 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
3129 for (unsigned i = 0; i != NumElts; ++i) {
3130 SDOperand Elt = RHS.getOperand(i);
3131 if (!isa<ConstantSDNode>(Elt))
3132 return SDOperand();
3133 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
3134 IdxOps.push_back(DAG.getConstant(i, EVT));
3135 else if (cast<ConstantSDNode>(Elt)->isNullValue())
3136 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
3137 else
3138 return SDOperand();
3139 }
3140
3141 // Let's see if the target supports this vector_shuffle.
3142 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
3143 return SDOperand();
3144
3145 // Return the new VVECTOR_SHUFFLE node.
3146 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
3147 SDOperand EVTNode = DAG.getValueType(EVT);
3148 std::vector<SDOperand> Ops;
Chris Lattner516b9622006-09-14 20:50:57 +00003149 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode,
3150 EVTNode);
Evan Cheng44f1f092006-04-20 08:56:16 +00003151 Ops.push_back(LHS);
3152 AddToWorkList(LHS.Val);
3153 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
3154 ZeroOps.push_back(NumEltsNode);
3155 ZeroOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003156 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3157 &ZeroOps[0], ZeroOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003158 IdxOps.push_back(NumEltsNode);
3159 IdxOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003160 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3161 &IdxOps[0], IdxOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003162 Ops.push_back(NumEltsNode);
3163 Ops.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003164 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
3165 &Ops[0], Ops.size());
Evan Cheng44f1f092006-04-20 08:56:16 +00003166 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
3167 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
3168 DstVecSize, DstVecEVT);
3169 }
3170 return Result;
3171 }
3172 }
3173 return SDOperand();
3174}
3175
Chris Lattneredab1b92006-04-02 03:25:57 +00003176/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
3177/// the scalar operation of the vop if it is operating on an integer vector
3178/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
3179SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
3180 ISD::NodeType FPOp) {
3181 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
3182 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
3183 SDOperand LHS = N->getOperand(0);
3184 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00003185 SDOperand Shuffle = XformToShuffleWithZero(N);
3186 if (Shuffle.Val) return Shuffle;
3187
Chris Lattneredab1b92006-04-02 03:25:57 +00003188 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
3189 // this operation.
3190 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
3191 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003192 SmallVector<SDOperand, 8> Ops;
Chris Lattneredab1b92006-04-02 03:25:57 +00003193 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
3194 SDOperand LHSOp = LHS.getOperand(i);
3195 SDOperand RHSOp = RHS.getOperand(i);
3196 // If these two elements can't be folded, bail out.
3197 if ((LHSOp.getOpcode() != ISD::UNDEF &&
3198 LHSOp.getOpcode() != ISD::Constant &&
3199 LHSOp.getOpcode() != ISD::ConstantFP) ||
3200 (RHSOp.getOpcode() != ISD::UNDEF &&
3201 RHSOp.getOpcode() != ISD::Constant &&
3202 RHSOp.getOpcode() != ISD::ConstantFP))
3203 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00003204 // Can't fold divide by zero.
3205 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
3206 if ((RHSOp.getOpcode() == ISD::Constant &&
3207 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
3208 (RHSOp.getOpcode() == ISD::ConstantFP &&
3209 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
3210 break;
3211 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003212 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00003213 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00003214 assert((Ops.back().getOpcode() == ISD::UNDEF ||
3215 Ops.back().getOpcode() == ISD::Constant ||
3216 Ops.back().getOpcode() == ISD::ConstantFP) &&
3217 "Scalar binop didn't fold!");
3218 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003219
3220 if (Ops.size() == LHS.getNumOperands()-2) {
3221 Ops.push_back(*(LHS.Val->op_end()-2));
3222 Ops.push_back(*(LHS.Val->op_end()-1));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003223 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003224 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003225 }
3226
3227 return SDOperand();
3228}
3229
Nate Begeman44728a72005-09-19 22:34:01 +00003230SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00003231 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
3232
3233 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
3234 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3235 // If we got a simplified select_cc node back from SimplifySelectCC, then
3236 // break it down into a new SETCC node, and a new SELECT node, and then return
3237 // the SELECT node, since we were called with a SELECT node.
3238 if (SCC.Val) {
3239 // Check to see if we got a select_cc back (to turn into setcc/select).
3240 // Otherwise, just return whatever node we got back, like fabs.
3241 if (SCC.getOpcode() == ISD::SELECT_CC) {
3242 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
3243 SCC.getOperand(0), SCC.getOperand(1),
3244 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00003245 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003246 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
3247 SCC.getOperand(3), SETCC);
3248 }
3249 return SCC;
3250 }
Nate Begeman44728a72005-09-19 22:34:01 +00003251 return SDOperand();
3252}
3253
Chris Lattner40c62d52005-10-18 06:04:22 +00003254/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
3255/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00003256/// select. Callers of this should assume that TheSelect is deleted if this
3257/// returns true. As such, they should return the appropriate thing (e.g. the
3258/// node) back to the top-level of the DAG combiner loop to avoid it being
3259/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00003260///
3261bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
3262 SDOperand RHS) {
3263
3264 // If this is a select from two identical things, try to pull the operation
3265 // through the select.
3266 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00003267 // If this is a load and the token chain is identical, replace the select
3268 // of two loads with a load through a select of the address to load from.
3269 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
3270 // constants have been dropped into the constant pool.
3271 if ((LHS.getOpcode() == ISD::LOAD ||
Evan Chengc5484282006-10-04 00:56:09 +00003272 LHS.getOpcode() == ISD::LOADX ) &&
Chris Lattner40c62d52005-10-18 06:04:22 +00003273 // Token chains must be identical.
3274 LHS.getOperand(0) == RHS.getOperand(0) &&
3275 // If this is an EXTLOAD, the VT's must match.
3276 (LHS.getOpcode() == ISD::LOAD ||
3277 LHS.getOperand(3) == RHS.getOperand(3))) {
3278 // FIXME: this conflates two src values, discarding one. This is not
3279 // the right thing to do, but nothing uses srcvalues now. When they do,
3280 // turn SrcValue into a list of locations.
3281 SDOperand Addr;
3282 if (TheSelect->getOpcode() == ISD::SELECT)
3283 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
3284 TheSelect->getOperand(0), LHS.getOperand(1),
3285 RHS.getOperand(1));
3286 else
3287 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
3288 TheSelect->getOperand(0),
3289 TheSelect->getOperand(1),
3290 LHS.getOperand(1), RHS.getOperand(1),
3291 TheSelect->getOperand(4));
3292
3293 SDOperand Load;
3294 if (LHS.getOpcode() == ISD::LOAD)
3295 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
3296 Addr, LHS.getOperand(2));
Evan Chengc5484282006-10-04 00:56:09 +00003297 else {
3298 unsigned LType = LHS.getConstantOperandVal(4);
3299 Load = DAG.getExtLoad((ISD::LoadExtType)LType,
3300 TheSelect->getValueType(0),
Chris Lattner40c62d52005-10-18 06:04:22 +00003301 LHS.getOperand(0), Addr, LHS.getOperand(2),
3302 cast<VTSDNode>(LHS.getOperand(3))->getVT());
Evan Chengc5484282006-10-04 00:56:09 +00003303 }
Chris Lattner40c62d52005-10-18 06:04:22 +00003304 // Users of the select now use the result of the load.
3305 CombineTo(TheSelect, Load);
3306
3307 // Users of the old loads now use the new load's chain. We know the
3308 // old-load value is dead now.
3309 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
3310 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
3311 return true;
3312 }
3313 }
3314
3315 return false;
3316}
3317
Nate Begeman44728a72005-09-19 22:34:01 +00003318SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
3319 SDOperand N2, SDOperand N3,
3320 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00003321
3322 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00003323 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
3324 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
3325 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
3326
3327 // Determine if the condition we're dealing with is constant
3328 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
3329 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
3330
3331 // fold select_cc true, x, y -> x
3332 if (SCCC && SCCC->getValue())
3333 return N2;
3334 // fold select_cc false, x, y -> y
3335 if (SCCC && SCCC->getValue() == 0)
3336 return N3;
3337
3338 // Check to see if we can simplify the select into an fabs node
3339 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
3340 // Allow either -0.0 or 0.0
3341 if (CFP->getValue() == 0.0) {
3342 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
3343 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
3344 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
3345 N2 == N3.getOperand(0))
3346 return DAG.getNode(ISD::FABS, VT, N0);
3347
3348 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
3349 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3350 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3351 N2.getOperand(0) == N3)
3352 return DAG.getNode(ISD::FABS, VT, N3);
3353 }
3354 }
3355
3356 // Check to see if we can perform the "gzip trick", transforming
3357 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00003358 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00003359 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00003360 MVT::isInteger(N2.getValueType()) &&
3361 (N1C->isNullValue() || // (a < 0) ? b : 0
3362 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00003363 MVT::ValueType XType = N0.getValueType();
3364 MVT::ValueType AType = N2.getValueType();
3365 if (XType >= AType) {
3366 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00003367 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00003368 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3369 unsigned ShCtV = Log2_64(N2C->getValue());
3370 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3371 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3372 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00003373 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003374 if (XType > AType) {
3375 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003376 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003377 }
3378 return DAG.getNode(ISD::AND, AType, Shift, N2);
3379 }
3380 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3381 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3382 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003383 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003384 if (XType > AType) {
3385 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003386 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003387 }
3388 return DAG.getNode(ISD::AND, AType, Shift, N2);
3389 }
3390 }
Nate Begeman07ed4172005-10-10 21:26:48 +00003391
3392 // fold select C, 16, 0 -> shl C, 4
3393 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3394 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3395 // Get a SetCC of the condition
3396 // FIXME: Should probably make sure that setcc is legal if we ever have a
3397 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00003398 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00003399 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00003400 if (AfterLegalize) {
3401 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003402 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00003403 } else {
3404 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003405 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00003406 }
Chris Lattner5750df92006-03-01 04:03:14 +00003407 AddToWorkList(SCC.Val);
3408 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00003409 // shl setcc result by log2 n2c
3410 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3411 DAG.getConstant(Log2_64(N2C->getValue()),
3412 TLI.getShiftAmountTy()));
3413 }
3414
Nate Begemanf845b452005-10-08 00:29:44 +00003415 // Check to see if this is the equivalent of setcc
3416 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3417 // otherwise, go ahead with the folds.
3418 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3419 MVT::ValueType XType = N0.getValueType();
3420 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3421 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3422 if (Res.getValueType() != VT)
3423 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3424 return Res;
3425 }
3426
3427 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3428 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3429 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3430 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3431 return DAG.getNode(ISD::SRL, XType, Ctlz,
3432 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3433 TLI.getShiftAmountTy()));
3434 }
3435 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3436 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3437 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3438 N0);
3439 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3440 DAG.getConstant(~0ULL, XType));
3441 return DAG.getNode(ISD::SRL, XType,
3442 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3443 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3444 TLI.getShiftAmountTy()));
3445 }
3446 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3447 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3448 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3449 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3450 TLI.getShiftAmountTy()));
3451 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3452 }
3453 }
3454
3455 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3456 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3457 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3458 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3459 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3460 MVT::ValueType XType = N0.getValueType();
3461 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3462 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3463 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3464 TLI.getShiftAmountTy()));
3465 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003466 AddToWorkList(Shift.Val);
3467 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003468 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3469 }
3470 }
3471 }
3472
Nate Begeman44728a72005-09-19 22:34:01 +00003473 return SDOperand();
3474}
3475
Nate Begeman452d7be2005-09-16 00:54:12 +00003476SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00003477 SDOperand N1, ISD::CondCode Cond,
3478 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003479 // These setcc operations always fold.
3480 switch (Cond) {
3481 default: break;
3482 case ISD::SETFALSE:
3483 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3484 case ISD::SETTRUE:
3485 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3486 }
3487
3488 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3489 uint64_t C1 = N1C->getValue();
3490 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
3491 uint64_t C0 = N0C->getValue();
3492
3493 // Sign extend the operands if required
3494 if (ISD::isSignedIntSetCC(Cond)) {
3495 C0 = N0C->getSignExtended();
3496 C1 = N1C->getSignExtended();
3497 }
3498
3499 switch (Cond) {
3500 default: assert(0 && "Unknown integer setcc!");
3501 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3502 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3503 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
3504 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
3505 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
3506 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
3507 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
3508 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
3509 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
3510 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
3511 }
3512 } else {
Chris Lattner5f42a242006-09-20 06:19:26 +00003513 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
3514 // equality comparison, then we're just comparing whether X itself is
3515 // zero.
3516 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
3517 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
3518 N0.getOperand(1).getOpcode() == ISD::Constant) {
3519 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
3520 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3521 ShAmt == Log2_32(MVT::getSizeInBits(N0.getValueType()))) {
3522 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
3523 // (srl (ctlz x), 5) == 0 -> X != 0
3524 // (srl (ctlz x), 5) != 1 -> X != 0
3525 Cond = ISD::SETNE;
3526 } else {
3527 // (srl (ctlz x), 5) != 0 -> X == 0
3528 // (srl (ctlz x), 5) == 1 -> X == 0
3529 Cond = ISD::SETEQ;
3530 }
3531 SDOperand Zero = DAG.getConstant(0, N0.getValueType());
3532 return DAG.getSetCC(VT, N0.getOperand(0).getOperand(0),
3533 Zero, Cond);
3534 }
3535 }
3536
Nate Begeman452d7be2005-09-16 00:54:12 +00003537 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3538 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3539 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3540
3541 // If the comparison constant has bits in the upper part, the
3542 // zero-extended value could never match.
3543 if (C1 & (~0ULL << InSize)) {
3544 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3545 switch (Cond) {
3546 case ISD::SETUGT:
3547 case ISD::SETUGE:
3548 case ISD::SETEQ: return DAG.getConstant(0, VT);
3549 case ISD::SETULT:
3550 case ISD::SETULE:
3551 case ISD::SETNE: return DAG.getConstant(1, VT);
3552 case ISD::SETGT:
3553 case ISD::SETGE:
3554 // True if the sign bit of C1 is set.
3555 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3556 case ISD::SETLT:
3557 case ISD::SETLE:
3558 // True if the sign bit of C1 isn't set.
3559 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3560 default:
3561 break;
3562 }
3563 }
3564
3565 // Otherwise, we can perform the comparison with the low bits.
3566 switch (Cond) {
3567 case ISD::SETEQ:
3568 case ISD::SETNE:
3569 case ISD::SETUGT:
3570 case ISD::SETUGE:
3571 case ISD::SETULT:
3572 case ISD::SETULE:
3573 return DAG.getSetCC(VT, N0.getOperand(0),
3574 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3575 Cond);
3576 default:
3577 break; // todo, be more careful with signed comparisons
3578 }
3579 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3580 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3581 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3582 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3583 MVT::ValueType ExtDstTy = N0.getValueType();
3584 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3585
3586 // If the extended part has any inconsistent bits, it cannot ever
3587 // compare equal. In other words, they have to be all ones or all
3588 // zeros.
3589 uint64_t ExtBits =
3590 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3591 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3592 return DAG.getConstant(Cond == ISD::SETNE, VT);
3593
3594 SDOperand ZextOp;
3595 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3596 if (Op0Ty == ExtSrcTy) {
3597 ZextOp = N0.getOperand(0);
3598 } else {
3599 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3600 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3601 DAG.getConstant(Imm, Op0Ty));
3602 }
Chris Lattner5750df92006-03-01 04:03:14 +00003603 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003604 // Otherwise, make this a use of a zext.
3605 return DAG.getSetCC(VT, ZextOp,
3606 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3607 ExtDstTy),
3608 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00003609 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
3610 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3611 (N0.getOpcode() == ISD::XOR ||
3612 (N0.getOpcode() == ISD::AND &&
3613 N0.getOperand(0).getOpcode() == ISD::XOR &&
3614 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3615 isa<ConstantSDNode>(N0.getOperand(1)) &&
3616 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
3617 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
3618 // only do this if the top bits are known zero.
3619 if (TLI.MaskedValueIsZero(N1,
3620 MVT::getIntVTBitMask(N0.getValueType())-1)) {
3621 // Okay, get the un-inverted input value.
3622 SDOperand Val;
3623 if (N0.getOpcode() == ISD::XOR)
3624 Val = N0.getOperand(0);
3625 else {
3626 assert(N0.getOpcode() == ISD::AND &&
3627 N0.getOperand(0).getOpcode() == ISD::XOR);
3628 // ((X^1)&1)^1 -> X & 1
3629 Val = DAG.getNode(ISD::AND, N0.getValueType(),
3630 N0.getOperand(0).getOperand(0), N0.getOperand(1));
3631 }
3632 return DAG.getSetCC(VT, Val, N1,
3633 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
3634 }
Nate Begeman452d7be2005-09-16 00:54:12 +00003635 }
Chris Lattner5c46f742005-10-05 06:11:08 +00003636
Nate Begeman452d7be2005-09-16 00:54:12 +00003637 uint64_t MinVal, MaxVal;
3638 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
3639 if (ISD::isSignedIntSetCC(Cond)) {
3640 MinVal = 1ULL << (OperandBitSize-1);
3641 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
3642 MaxVal = ~0ULL >> (65-OperandBitSize);
3643 else
3644 MaxVal = 0;
3645 } else {
3646 MinVal = 0;
3647 MaxVal = ~0ULL >> (64-OperandBitSize);
3648 }
3649
3650 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
3651 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
3652 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
3653 --C1; // X >= C0 --> X > (C0-1)
3654 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3655 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
3656 }
3657
3658 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
3659 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
3660 ++C1; // X <= C0 --> X < (C0+1)
3661 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3662 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
3663 }
3664
3665 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
3666 return DAG.getConstant(0, VT); // X < MIN --> false
3667
3668 // Canonicalize setgt X, Min --> setne X, Min
3669 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
3670 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00003671 // Canonicalize setlt X, Max --> setne X, Max
3672 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
3673 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00003674
3675 // If we have setult X, 1, turn it into seteq X, 0
3676 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
3677 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
3678 ISD::SETEQ);
3679 // If we have setugt X, Max-1, turn it into seteq X, Max
3680 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
3681 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
3682 ISD::SETEQ);
3683
3684 // If we have "setcc X, C0", check to see if we can shrink the immediate
3685 // by changing cc.
3686
3687 // SETUGT X, SINTMAX -> SETLT X, 0
3688 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
3689 C1 == (~0ULL >> (65-OperandBitSize)))
3690 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
3691 ISD::SETLT);
3692
3693 // FIXME: Implement the rest of these.
3694
3695 // Fold bit comparisons when we can.
3696 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3697 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
3698 if (ConstantSDNode *AndRHS =
3699 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3700 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
3701 // Perform the xform if the AND RHS is a single bit.
3702 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
3703 return DAG.getNode(ISD::SRL, VT, N0,
3704 DAG.getConstant(Log2_64(AndRHS->getValue()),
3705 TLI.getShiftAmountTy()));
3706 }
3707 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
3708 // (X & 8) == 8 --> (X & 8) >> 3
3709 // Perform the xform if C1 is a single bit.
3710 if ((C1 & (C1-1)) == 0) {
3711 return DAG.getNode(ISD::SRL, VT, N0,
Chris Lattner729c6d12006-05-27 00:43:02 +00003712 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
Nate Begeman452d7be2005-09-16 00:54:12 +00003713 }
3714 }
3715 }
3716 }
3717 } else if (isa<ConstantSDNode>(N0.Val)) {
3718 // Ensure that the constant occurs on the RHS.
3719 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3720 }
3721
3722 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
3723 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
3724 double C0 = N0C->getValue(), C1 = N1C->getValue();
3725
3726 switch (Cond) {
3727 default: break; // FIXME: Implement the rest of these!
3728 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3729 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3730 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
3731 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
3732 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
3733 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
3734 }
3735 } else {
3736 // Ensure that the constant occurs on the RHS.
3737 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3738 }
3739
3740 if (N0 == N1) {
3741 // We can always fold X == Y for integer setcc's.
3742 if (MVT::isInteger(N0.getValueType()))
3743 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3744 unsigned UOF = ISD::getUnorderedFlavor(Cond);
3745 if (UOF == 2) // FP operators that are undefined on NaNs.
3746 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3747 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
3748 return DAG.getConstant(UOF, VT);
3749 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
3750 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00003751 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00003752 if (NewCond != Cond)
3753 return DAG.getSetCC(VT, N0, N1, NewCond);
3754 }
3755
3756 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3757 MVT::isInteger(N0.getValueType())) {
3758 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
3759 N0.getOpcode() == ISD::XOR) {
3760 // Simplify (X+Y) == (X+Z) --> Y == Z
3761 if (N0.getOpcode() == N1.getOpcode()) {
3762 if (N0.getOperand(0) == N1.getOperand(0))
3763 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
3764 if (N0.getOperand(1) == N1.getOperand(1))
3765 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
Evan Cheng1efba0e2006-08-29 06:42:35 +00003766 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003767 // If X op Y == Y op X, try other combinations.
3768 if (N0.getOperand(0) == N1.getOperand(1))
3769 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
3770 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00003771 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00003772 }
3773 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003774
3775 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
3776 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3777 // Turn (X+C1) == C2 --> X == C2-C1
3778 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
3779 return DAG.getSetCC(VT, N0.getOperand(0),
3780 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
3781 N0.getValueType()), Cond);
3782 }
3783
3784 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
3785 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00003786 // If we know that all of the inverted bits are zero, don't bother
3787 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003788 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00003789 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003790 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00003791 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003792 }
3793
3794 // Turn (C1-X) == C2 --> X == C1-C2
3795 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
3796 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
3797 return DAG.getSetCC(VT, N0.getOperand(1),
3798 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
3799 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00003800 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003801 }
3802 }
3803
Nate Begeman452d7be2005-09-16 00:54:12 +00003804 // Simplify (X+Z) == X --> Z == 0
3805 if (N0.getOperand(0) == N1)
3806 return DAG.getSetCC(VT, N0.getOperand(1),
3807 DAG.getConstant(0, N0.getValueType()), Cond);
3808 if (N0.getOperand(1) == N1) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00003809 if (DAG.isCommutativeBinOp(N0.getOpcode()))
Nate Begeman452d7be2005-09-16 00:54:12 +00003810 return DAG.getSetCC(VT, N0.getOperand(0),
3811 DAG.getConstant(0, N0.getValueType()), Cond);
3812 else {
3813 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
3814 // (Z-X) == X --> Z == X<<1
3815 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
3816 N1,
3817 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003818 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003819 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
3820 }
3821 }
3822 }
3823
3824 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3825 N1.getOpcode() == ISD::XOR) {
3826 // Simplify X == (X+Z) --> Z == 0
3827 if (N1.getOperand(0) == N0) {
3828 return DAG.getSetCC(VT, N1.getOperand(1),
3829 DAG.getConstant(0, N1.getValueType()), Cond);
3830 } else if (N1.getOperand(1) == N0) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00003831 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003832 return DAG.getSetCC(VT, N1.getOperand(0),
3833 DAG.getConstant(0, N1.getValueType()), Cond);
3834 } else {
3835 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3836 // X == (Z-X) --> X<<1 == Z
3837 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
3838 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003839 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003840 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
3841 }
3842 }
3843 }
3844 }
3845
3846 // Fold away ALL boolean setcc's.
3847 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00003848 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003849 switch (Cond) {
3850 default: assert(0 && "Unknown integer setcc!");
3851 case ISD::SETEQ: // X == Y -> (X^Y)^1
3852 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3853 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00003854 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003855 break;
3856 case ISD::SETNE: // X != Y --> (X^Y)
3857 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3858 break;
3859 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
3860 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
3861 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3862 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003863 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003864 break;
3865 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
3866 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
3867 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3868 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003869 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003870 break;
3871 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
3872 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
3873 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3874 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003875 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003876 break;
3877 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
3878 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
3879 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3880 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
3881 break;
3882 }
3883 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00003884 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003885 // FIXME: If running after legalize, we probably can't do this.
3886 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3887 }
3888 return N0;
3889 }
3890
3891 // Could not fold it.
3892 return SDOperand();
3893}
3894
Nate Begeman69575232005-10-20 02:15:44 +00003895/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3896/// return a DAG expression to select that will generate the same value by
3897/// multiplying by a magic number. See:
3898/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3899SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003900 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003901 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
3902
Andrew Lenharth232c9102006-06-12 16:07:18 +00003903 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003904 ii != ee; ++ii)
3905 AddToWorkList(*ii);
3906 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003907}
3908
3909/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3910/// return a DAG expression to select that will generate the same value by
3911/// multiplying by a magic number. See:
3912/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3913SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003914 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003915 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00003916
Andrew Lenharth232c9102006-06-12 16:07:18 +00003917 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003918 ii != ee; ++ii)
3919 AddToWorkList(*ii);
3920 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003921}
3922
Jim Laskey71382342006-10-07 23:37:56 +00003923/// FindBaseOffset - Return true if base is known not to alias with anything
3924/// but itself. Provides base object and offset as results.
3925static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
3926 // Assume it is a primitive operation.
3927 Base = Ptr; Offset = 0;
3928
3929 // If it's an adding a simple constant then integrate the offset.
3930 if (Base.getOpcode() == ISD::ADD) {
3931 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
3932 Base = Base.getOperand(0);
3933 Offset += C->getValue();
3934 }
3935 }
3936
3937 // If it's any of the following then it can't alias with anything but itself.
3938 return isa<FrameIndexSDNode>(Base) ||
3939 isa<ConstantPoolSDNode>(Base) ||
3940 isa<GlobalAddressSDNode>(Base);
3941}
3942
3943/// isAlias - Return true if there is any possibility that the two addresses
3944/// overlap.
3945static bool isAlias(SDOperand Ptr1, int64_t Size1, SDOperand SrcValue1,
3946 SDOperand Ptr2, int64_t Size2, SDOperand SrcValue2) {
3947 // If they are the same then they must be aliases.
3948 if (Ptr1 == Ptr2) return true;
3949
3950 // Gather base node and offset information.
3951 SDOperand Base1, Base2;
3952 int64_t Offset1, Offset2;
3953 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
3954 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
3955
3956 // If they have a same base address then...
3957 if (Base1 == Base2) {
3958 // Check to see if the addresses overlap.
3959 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
3960 }
3961
3962 // Otherwise they alias if either is unknown.
3963 return !KnownBase1 || !KnownBase2;
3964}
3965
3966/// FindAliasInfo - Extracts the relevant alias information from the memory
3967/// node. Returns true if the operand was a load.
3968static bool FindAliasInfo(SDNode *N,
3969 SDOperand &Ptr, int64_t &Size, SDOperand &SrcValue) {
3970 switch (N->getOpcode()) {
3971 case ISD::LOAD:
3972 Ptr = N->getOperand(1);
3973 Size = MVT::getSizeInBits(N->getValueType(0)) >> 3;
3974 SrcValue = N->getOperand(2);
3975 return true;
3976 case ISD::STORE:
3977 Ptr = N->getOperand(2);
3978 Size = MVT::getSizeInBits(N->getOperand(1).getValueType()) >> 3;
3979 SrcValue = N->getOperand(3);
3980 break;
3981 default:
3982 assert(0 && "FindAliasInfo expected a memory operand");
3983 break;
3984 }
3985
3986 return false;
3987}
3988
Jim Laskey6ff23e52006-10-04 16:53:27 +00003989/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
3990/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00003991void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00003992 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00003993 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00003994 std::set<SDNode *> Visited; // Visited node set.
3995
Jim Laskey279f0532006-09-25 16:29:54 +00003996 // Get alias information for node.
3997 SDOperand Ptr;
3998 int64_t Size;
3999 SDOperand SrcValue;
Jim Laskeybc588b82006-10-05 15:07:25 +00004000 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue);
Jim Laskey279f0532006-09-25 16:29:54 +00004001
Jim Laskey6ff23e52006-10-04 16:53:27 +00004002 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00004003 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00004004
Jim Laskeybc588b82006-10-05 15:07:25 +00004005 // Look at each chain and determine if it is an alias. If so, add it to the
4006 // aliases list. If not, then continue up the chain looking for the next
4007 // candidate.
4008 while (!Chains.empty()) {
4009 SDOperand Chain = Chains.back();
4010 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00004011
Jim Laskeybc588b82006-10-05 15:07:25 +00004012 // Don't bother if we've been before.
4013 if (Visited.find(Chain.Val) != Visited.end()) continue;
4014 Visited.insert(Chain.Val);
4015
4016 switch (Chain.getOpcode()) {
4017 case ISD::EntryToken:
4018 // Entry token is ideal chain operand, but handled in FindBetterChain.
4019 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00004020
Jim Laskeybc588b82006-10-05 15:07:25 +00004021 case ISD::LOAD:
4022 case ISD::STORE: {
4023 // Get alias information for Chain.
4024 SDOperand OpPtr;
4025 int64_t OpSize;
4026 SDOperand OpSrcValue;
4027 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize, OpSrcValue);
4028
4029 // If chain is alias then stop here.
4030 if (!(IsLoad && IsOpLoad) &&
4031 isAlias(Ptr, Size, SrcValue, OpPtr, OpSize, OpSrcValue)) {
4032 Aliases.push_back(Chain);
4033 } else {
4034 // Look further up the chain.
4035 Chains.push_back(Chain.getOperand(0));
4036 // Clean up old chain.
4037 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00004038 }
Jim Laskeybc588b82006-10-05 15:07:25 +00004039 break;
4040 }
4041
4042 case ISD::TokenFactor:
4043 // We have to check each of the operands of the token factor, so we queue
4044 // then up. Adding the operands to the queue (stack) in reverse order
4045 // maintains the original order and increases the likelihood that getNode
4046 // will find a matching token factor (CSE.)
4047 for (unsigned n = Chain.getNumOperands(); n;)
4048 Chains.push_back(Chain.getOperand(--n));
4049 // Eliminate the token factor if we can.
4050 AddToWorkList(Chain.Val);
4051 break;
4052
4053 default:
4054 // For all other instructions we will just have to take what we can get.
4055 Aliases.push_back(Chain);
4056 break;
Jim Laskey279f0532006-09-25 16:29:54 +00004057 }
4058 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004059}
4060
4061/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
4062/// for a better chain (aliasing node.)
4063SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
4064 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00004065
Jim Laskey6ff23e52006-10-04 16:53:27 +00004066 // Accumulate all the aliases to this node.
4067 GatherAllAliases(N, OldChain, Aliases);
4068
4069 if (Aliases.size() == 0) {
4070 // If no operands then chain to entry token.
4071 return DAG.getEntryNode();
4072 } else if (Aliases.size() == 1) {
4073 // If a single operand then chain to it. We don't need to revisit it.
4074 return Aliases[0];
4075 }
4076
4077 // Construct a custom tailored token factor.
4078 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4079 &Aliases[0], Aliases.size());
4080
4081 // Make sure the old chain gets cleaned up.
4082 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
4083
4084 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00004085}
4086
Nate Begeman1d4d4142005-09-01 00:19:25 +00004087// SelectionDAG::Combine - This is the entry point for the file.
4088//
Nate Begeman4ebd8052005-09-01 23:24:04 +00004089void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00004090 /// run - This is the main entry point to this class.
4091 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00004092 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004093}