blob: 8bbcd2651ca2cce5af6eb886858fad55c68f4489 [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"
Jim Laskeyc7c3f112006-10-16 20:52:31 +000033#include "llvm/Analysis/AliasAnalysis.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000034#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000035#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000036#include "llvm/Support/MathExtras.h"
37#include "llvm/Target/TargetLowering.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000038#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000039#include "llvm/Support/CommandLine.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000040#include <algorithm>
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
Evan Chengbbd6f6e2006-11-07 09:03:05 +000049 static Statistic<> PreIndexedNodes ("pre_indexed_ops",
50 "Number of pre-indexed nodes created");
51 static Statistic<> PostIndexedNodes ("post_indexed_ops",
52 "Number of post-indexed nodes created");
53
Jim Laskey71382342006-10-07 23:37:56 +000054 static cl::opt<bool>
55 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000056 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000057
Jim Laskey07a27092006-10-18 19:08:31 +000058 static cl::opt<bool>
59 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
60 cl::desc("Include global information in alias analysis"));
61
Jim Laskeybc588b82006-10-05 15:07:25 +000062//------------------------------ DAGCombiner ---------------------------------//
63
Jim Laskey71382342006-10-07 23:37:56 +000064 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000065 SelectionDAG &DAG;
66 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000067 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000068
69 // Worklist of all of the nodes that need to be simplified.
70 std::vector<SDNode*> WorkList;
71
Jim Laskeyc7c3f112006-10-16 20:52:31 +000072 // AA - Used for DAG load/store alias analysis.
73 AliasAnalysis &AA;
74
Nate Begeman1d4d4142005-09-01 00:19:25 +000075 /// AddUsersToWorkList - When an instruction is simplified, add all users of
76 /// the instruction to the work lists because they might get more simplified
77 /// now.
78 ///
79 void AddUsersToWorkList(SDNode *N) {
80 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000081 UI != UE; ++UI)
Jim Laskey6ff23e52006-10-04 16:53:27 +000082 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000083 }
84
85 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000086 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000087 void removeFromWorkList(SDNode *N) {
88 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
89 WorkList.end());
90 }
91
Chris Lattner24664722006-03-01 04:53:38 +000092 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000093 /// AddToWorkList - Add to the work list making sure it's instance is at the
94 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000095 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000096 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000097 WorkList.push_back(N);
98 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000099
Jim Laskey274062c2006-10-13 23:32:28 +0000100 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
101 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000102 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
Chris Lattner87514ca2005-10-10 22:31:19 +0000103 ++NodesCombined;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000104 DEBUG(std::cerr << "\nReplacing.1 "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000105 std::cerr << "\nWith: "; To[0].Val->dump(&DAG);
Chris Lattner3577e382006-08-11 17:56:38 +0000106 std::cerr << " and " << NumTo-1 << " other values\n");
Chris Lattner01a22022005-10-10 22:04:48 +0000107 std::vector<SDNode*> NowDead;
Chris Lattner3577e382006-08-11 17:56:38 +0000108 DAG.ReplaceAllUsesWith(N, To, &NowDead);
Chris Lattner01a22022005-10-10 22:04:48 +0000109
Jim Laskey274062c2006-10-13 23:32:28 +0000110 if (AddTo) {
111 // Push the new nodes and any users onto the worklist
112 for (unsigned i = 0, e = NumTo; i != e; ++i) {
113 AddToWorkList(To[i].Val);
114 AddUsersToWorkList(To[i].Val);
115 }
Chris Lattner01a22022005-10-10 22:04:48 +0000116 }
117
Jim Laskey6ff23e52006-10-04 16:53:27 +0000118 // Nodes can be reintroduced into the worklist. Make sure we do not
119 // process a node that has been replaced.
Chris Lattner01a22022005-10-10 22:04:48 +0000120 removeFromWorkList(N);
121 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
122 removeFromWorkList(NowDead[i]);
123
124 // Finally, since the node is now dead, remove it from the graph.
125 DAG.DeleteNode(N);
126 return SDOperand(N, 0);
127 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000128
Jim Laskey274062c2006-10-13 23:32:28 +0000129 SDOperand CombineTo(SDNode *N, SDOperand Res, bool AddTo = true) {
130 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000131 }
132
Jim Laskey274062c2006-10-13 23:32:28 +0000133 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1,
134 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000135 SDOperand To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000136 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000137 }
138 private:
139
Chris Lattner012f2412006-02-17 21:58:01 +0000140 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000141 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000142 /// propagation. If so, return true.
143 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000144 TargetLowering::TargetLoweringOpt TLO(DAG);
145 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000146 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
147 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
148 return false;
149
150 // Revisit the node.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000151 AddToWorkList(Op.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000152
153 // Replace the old value with the new one.
154 ++NodesCombined;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000155 DEBUG(std::cerr << "\nReplacing.2 "; TLO.Old.Val->dump();
Jim Laskey279f0532006-09-25 16:29:54 +0000156 std::cerr << "\nWith: "; TLO.New.Val->dump(&DAG);
157 std::cerr << '\n');
Chris Lattner012f2412006-02-17 21:58:01 +0000158
159 std::vector<SDNode*> NowDead;
160 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
161
Chris Lattner7d20d392006-02-20 06:51:04 +0000162 // Push the new node and any (possibly new) users onto the worklist.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000163 AddToWorkList(TLO.New.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000164 AddUsersToWorkList(TLO.New.Val);
165
166 // Nodes can end up on the worklist more than once. Make sure we do
167 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000168 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
169 removeFromWorkList(NowDead[i]);
170
Chris Lattner7d20d392006-02-20 06:51:04 +0000171 // Finally, if the node is now dead, remove it from the graph. The node
172 // may not be dead if the replacement process recursively simplified to
173 // something else needing this node.
174 if (TLO.Old.Val->use_empty()) {
175 removeFromWorkList(TLO.Old.Val);
176 DAG.DeleteNode(TLO.Old.Val);
177 }
Chris Lattner012f2412006-02-17 21:58:01 +0000178 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000179 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000180
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000181 /// CombineToPreIndexedLoadStore - Try turning a load / store and a
182 /// pre-indexed load / store when the base pointer is a add or subtract
183 /// and it has other uses besides the load / store. After the
184 /// transformation, the new indexed load / store has effectively folded
185 /// the add / subtract in and all of its other uses are redirected to the
186 /// new load / store.
Evan Cheng3ef554d2006-11-06 08:14:30 +0000187 bool CombineToPreIndexedLoadStore(SDNode *N) {
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000188 if (!AfterLegalize)
189 return false;
190
Evan Cheng33dbedc2006-11-05 09:31:14 +0000191 bool isLoad = true;
Evan Cheng7fc033a2006-11-03 03:06:21 +0000192 SDOperand Ptr;
Evan Cheng7fc033a2006-11-03 03:06:21 +0000193 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
194 Ptr = LD->getBasePtr();
Evan Cheng33dbedc2006-11-05 09:31:14 +0000195 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
196 Ptr = ST->getBasePtr();
197 isLoad = false;
Evan Cheng7fc033a2006-11-03 03:06:21 +0000198 } else
199 return false;
200
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000201 if ((Ptr.getOpcode() == ISD::ADD || Ptr.getOpcode() == ISD::SUB) &&
Evan Cheng7fc033a2006-11-03 03:06:21 +0000202 Ptr.Val->use_size() > 1) {
203 SDOperand BasePtr;
204 SDOperand Offset;
Evan Cheng144d8f02006-11-09 17:55:04 +0000205 ISD::MemIndexedMode AM = ISD::UNINDEXED;
Evan Cheng1a854be2006-11-03 07:21:16 +0000206 if (TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG)) {
Evan Cheng7fc033a2006-11-03 03:06:21 +0000207 // Try turning it into a pre-indexed load / store except when
208 // 1) Another use of base ptr is a predecessor of N. If ptr is folded
209 // that would create a cycle.
Evan Cheng03fa6ea2006-11-08 08:30:28 +0000210 // 2) All uses are load / store ops that use it as base ptr.
Evan Cheng7fc033a2006-11-03 03:06:21 +0000211
212 // Now check for #1 and #2.
Evan Cheng03fa6ea2006-11-08 08:30:28 +0000213 bool RealUse = false;
214 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
215 E = Ptr.Val->use_end(); I != E; ++I) {
216 SDNode *Use = *I;
217 if (Use == N)
218 continue;
219 if (Use->isPredecessor(N))
Evan Chenga4f53ef2006-11-08 06:56:05 +0000220 return false;
Evan Cheng03fa6ea2006-11-08 08:30:28 +0000221
222 if (!((Use->getOpcode() == ISD::LOAD &&
223 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
224 (Use->getOpcode() == ISD::STORE) &&
225 cast<StoreSDNode>(Use)->getBasePtr() == Ptr))
226 RealUse = true;
Evan Cheng7fc033a2006-11-03 03:06:21 +0000227 }
Evan Cheng03fa6ea2006-11-08 08:30:28 +0000228 if (!RealUse)
229 return false;
Evan Cheng7fc033a2006-11-03 03:06:21 +0000230
Evan Cheng33dbedc2006-11-05 09:31:14 +0000231 SDOperand Result = isLoad
232 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
233 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000234 ++PreIndexedNodes;
Evan Cheng7fc033a2006-11-03 03:06:21 +0000235 ++NodesCombined;
236 DEBUG(std::cerr << "\nReplacing.4 "; N->dump();
237 std::cerr << "\nWith: "; Result.Val->dump(&DAG);
238 std::cerr << '\n');
239 std::vector<SDNode*> NowDead;
Evan Cheng33dbedc2006-11-05 09:31:14 +0000240 if (isLoad) {
241 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
242 NowDead);
243 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
244 NowDead);
245 } else {
246 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
247 NowDead);
248 }
Evan Cheng7fc033a2006-11-03 03:06:21 +0000249
250 // Nodes can end up on the worklist more than once. Make sure we do
251 // not process a node that has been replaced.
252 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
253 removeFromWorkList(NowDead[i]);
254 // Finally, since the node is now dead, remove it from the graph.
255 DAG.DeleteNode(N);
256
257 // Replace the uses of Ptr with uses of the updated base value.
Evan Cheng33dbedc2006-11-05 09:31:14 +0000258 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
259 NowDead);
Evan Cheng7fc033a2006-11-03 03:06:21 +0000260 removeFromWorkList(Ptr.Val);
261 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
262 removeFromWorkList(NowDead[i]);
263 DAG.DeleteNode(Ptr.Val);
264
265 return true;
266 }
267 }
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000268 return false;
269 }
Evan Cheng7fc033a2006-11-03 03:06:21 +0000270
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000271 /// CombineToPostIndexedLoadStore - Try combine a load / store with a
272 /// add / sub of the base pointer node into a post-indexed load / store.
273 /// The transformation folded the add / subtract into the new indexed
274 /// load / store effectively and all of its uses are redirected to the
275 /// new load / store.
276 bool CombineToPostIndexedLoadStore(SDNode *N) {
277 if (!AfterLegalize)
278 return false;
279
280 bool isLoad = true;
281 SDOperand Ptr;
282 MVT::ValueType VT;
283 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
284 Ptr = LD->getBasePtr();
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000285 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
286 Ptr = ST->getBasePtr();
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000287 isLoad = false;
288 } else
289 return false;
290
291 if (Ptr.Val->use_size() > 1) {
292 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
293 E = Ptr.Val->use_end(); I != E; ++I) {
294 SDNode *Op = *I;
295 if (Op == N ||
296 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
297 continue;
298
299 SDOperand BasePtr;
300 SDOperand Offset;
Evan Cheng144d8f02006-11-09 17:55:04 +0000301 ISD::MemIndexedMode AM = ISD::UNINDEXED;
Evan Chengd258efa2006-11-09 04:29:46 +0000302 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM,DAG)) {
Evan Chengb00dddd2006-11-08 20:27:27 +0000303 if (Ptr == Offset)
304 std::swap(BasePtr, Offset);
305 if (Ptr != BasePtr)
306 continue;
307
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000308 // Try turning it into a post-indexed load / store except when
309 // 1) Op must be independent of N, i.e. Op is neither a predecessor
310 // nor a successor of N. Otherwise, if Op is folded that would
311 // create a cycle.
Evan Cheng03fa6ea2006-11-08 08:30:28 +0000312 // 2) All uses are load / store ops that use it as base ptr.
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000313
314 // Check for #3.
315 bool TryNext = false;
316 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
317 EE = BasePtr.Val->use_end(); II != EE; ++II) {
318 SDNode *Use = *II;
319 if (Use == Ptr.Val)
320 continue;
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000321
Evan Cheng03fa6ea2006-11-08 08:30:28 +0000322 // If all the uses are load / store addresses, then don't do the
323 // transformation.
324 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
325 bool RealUse = false;
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000326 for (SDNode::use_iterator III = Use->use_begin(),
327 EEE = Use->use_end(); III != EEE; ++III) {
328 SDNode *UseUse = *III;
Evan Cheng03fa6ea2006-11-08 08:30:28 +0000329 if (!((UseUse->getOpcode() == ISD::LOAD &&
330 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
331 (UseUse->getOpcode() == ISD::STORE) &&
332 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use))
333 RealUse = true;
334 }
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000335
Evan Cheng03fa6ea2006-11-08 08:30:28 +0000336 if (!RealUse) {
337 TryNext = true;
338 break;
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000339 }
340 }
341 }
342 if (TryNext)
343 continue;
344
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000345 // Check for #1
346 if (!Op->isPredecessor(N) && !N->isPredecessor(Op)) {
347 SDOperand Result = isLoad
348 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
349 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
350 ++PostIndexedNodes;
351 ++NodesCombined;
352 DEBUG(std::cerr << "\nReplacing.5 "; N->dump();
353 std::cerr << "\nWith: "; Result.Val->dump(&DAG);
354 std::cerr << '\n');
355 std::vector<SDNode*> NowDead;
356 if (isLoad) {
357 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
358 NowDead);
359 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
360 NowDead);
361 } else {
362 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
363 NowDead);
364 }
365
366 // Nodes can end up on the worklist more than once. Make sure we do
367 // not process a node that has been replaced.
368 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
369 removeFromWorkList(NowDead[i]);
370 // Finally, since the node is now dead, remove it from the graph.
371 DAG.DeleteNode(N);
372
373 // Replace the uses of Use with uses of the updated base value.
374 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
375 Result.getValue(isLoad ? 1 : 0),
376 NowDead);
377 removeFromWorkList(Op);
378 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
379 removeFromWorkList(NowDead[i]);
380 DAG.DeleteNode(Op);
381
382 return true;
383 }
384 }
385 }
386 }
Evan Cheng7fc033a2006-11-03 03:06:21 +0000387 return false;
388 }
389
Nate Begeman1d4d4142005-09-01 00:19:25 +0000390 /// visit - call the node-specific routine that knows how to fold each
391 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000392 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000393
394 // Visitation implementation - Implement dag node combining for different
395 // node types. The semantics are as follows:
396 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000397 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000398 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000399 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000400 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000401 SDOperand visitTokenFactor(SDNode *N);
402 SDOperand visitADD(SDNode *N);
403 SDOperand visitSUB(SDNode *N);
404 SDOperand visitMUL(SDNode *N);
405 SDOperand visitSDIV(SDNode *N);
406 SDOperand visitUDIV(SDNode *N);
407 SDOperand visitSREM(SDNode *N);
408 SDOperand visitUREM(SDNode *N);
409 SDOperand visitMULHU(SDNode *N);
410 SDOperand visitMULHS(SDNode *N);
411 SDOperand visitAND(SDNode *N);
412 SDOperand visitOR(SDNode *N);
413 SDOperand visitXOR(SDNode *N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000414 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000415 SDOperand visitSHL(SDNode *N);
416 SDOperand visitSRA(SDNode *N);
417 SDOperand visitSRL(SDNode *N);
418 SDOperand visitCTLZ(SDNode *N);
419 SDOperand visitCTTZ(SDNode *N);
420 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000421 SDOperand visitSELECT(SDNode *N);
422 SDOperand visitSELECT_CC(SDNode *N);
423 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000424 SDOperand visitSIGN_EXTEND(SDNode *N);
425 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000426 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000427 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
428 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000429 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000430 SDOperand visitVBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000431 SDOperand visitFADD(SDNode *N);
432 SDOperand visitFSUB(SDNode *N);
433 SDOperand visitFMUL(SDNode *N);
434 SDOperand visitFDIV(SDNode *N);
435 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000436 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000437 SDOperand visitSINT_TO_FP(SDNode *N);
438 SDOperand visitUINT_TO_FP(SDNode *N);
439 SDOperand visitFP_TO_SINT(SDNode *N);
440 SDOperand visitFP_TO_UINT(SDNode *N);
441 SDOperand visitFP_ROUND(SDNode *N);
442 SDOperand visitFP_ROUND_INREG(SDNode *N);
443 SDOperand visitFP_EXTEND(SDNode *N);
444 SDOperand visitFNEG(SDNode *N);
445 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000446 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000447 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000448 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000449 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000450 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
451 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000452 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000453 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000454 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000455
Evan Cheng44f1f092006-04-20 08:56:16 +0000456 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000457 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
458
Chris Lattner40c62d52005-10-18 06:04:22 +0000459 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000460 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000461 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
462 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
463 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000464 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000465 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000466 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000467 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000468 SDOperand BuildUDIV(SDNode *N);
469 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Jim Laskey279f0532006-09-25 16:29:54 +0000470
Jim Laskey6ff23e52006-10-04 16:53:27 +0000471 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
472 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000473 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000474 SmallVector<SDOperand, 8> &Aliases);
475
Jim Laskey096c22e2006-10-18 12:29:57 +0000476 /// isAlias - Return true if there is any possibility that the two addresses
477 /// overlap.
478 bool isAlias(SDOperand Ptr1, int64_t Size1,
479 const Value *SrcValue1, int SrcValueOffset1,
480 SDOperand Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000481 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000482
Jim Laskey7ca56af2006-10-11 13:47:09 +0000483 /// FindAliasInfo - Extracts the relevant alias information from the memory
484 /// node. Returns true if the operand was a load.
485 bool FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +0000486 SDOperand &Ptr, int64_t &Size,
487 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000488
Jim Laskey279f0532006-09-25 16:29:54 +0000489 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000490 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000491 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
492
Nate Begeman1d4d4142005-09-01 00:19:25 +0000493public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000494 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
495 : DAG(D),
496 TLI(D.getTargetLoweringInfo()),
497 AfterLegalize(false),
498 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000499
500 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000501 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000502 };
503}
504
Chris Lattner24664722006-03-01 04:53:38 +0000505//===----------------------------------------------------------------------===//
506// TargetLowering::DAGCombinerInfo implementation
507//===----------------------------------------------------------------------===//
508
509void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
510 ((DAGCombiner*)DC)->AddToWorkList(N);
511}
512
513SDOperand TargetLowering::DAGCombinerInfo::
514CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000515 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000516}
517
518SDOperand TargetLowering::DAGCombinerInfo::
519CombineTo(SDNode *N, SDOperand Res) {
520 return ((DAGCombiner*)DC)->CombineTo(N, Res);
521}
522
523
524SDOperand TargetLowering::DAGCombinerInfo::
525CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
526 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
527}
528
529
530
531
532//===----------------------------------------------------------------------===//
533
534
Nate Begeman4ebd8052005-09-01 23:24:04 +0000535// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
536// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000537// Also, set the incoming LHS, RHS, and CC references to the appropriate
538// nodes based on the type of node we are checking. This simplifies life a
539// bit for the callers.
540static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
541 SDOperand &CC) {
542 if (N.getOpcode() == ISD::SETCC) {
543 LHS = N.getOperand(0);
544 RHS = N.getOperand(1);
545 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000546 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000547 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000548 if (N.getOpcode() == ISD::SELECT_CC &&
549 N.getOperand(2).getOpcode() == ISD::Constant &&
550 N.getOperand(3).getOpcode() == ISD::Constant &&
551 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000552 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
553 LHS = N.getOperand(0);
554 RHS = N.getOperand(1);
555 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000556 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000557 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000558 return false;
559}
560
Nate Begeman99801192005-09-07 23:25:52 +0000561// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
562// one use. If this is true, it allows the users to invert the operation for
563// free when it is profitable to do so.
564static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000565 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000566 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000567 return true;
568 return false;
569}
570
Nate Begemancd4d58c2006-02-03 06:46:56 +0000571SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
572 MVT::ValueType VT = N0.getValueType();
573 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
574 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
575 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
576 if (isa<ConstantSDNode>(N1)) {
577 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000578 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000579 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
580 } else if (N0.hasOneUse()) {
581 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000582 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000583 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
584 }
585 }
586 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
587 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
588 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
589 if (isa<ConstantSDNode>(N0)) {
590 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000591 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000592 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
593 } else if (N1.hasOneUse()) {
594 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000595 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000596 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
597 }
598 }
599 return SDOperand();
600}
601
Nate Begeman4ebd8052005-09-01 23:24:04 +0000602void DAGCombiner::Run(bool RunningAfterLegalize) {
603 // set the instance variable, so that the various visit routines may use it.
604 AfterLegalize = RunningAfterLegalize;
605
Nate Begeman646d7e22005-09-02 21:18:40 +0000606 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000607 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
608 E = DAG.allnodes_end(); I != E; ++I)
609 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000610
Chris Lattner95038592005-10-05 06:35:28 +0000611 // Create a dummy node (which is not added to allnodes), that adds a reference
612 // to the root node, preventing it from being deleted, and tracking any
613 // changes of the root.
614 HandleSDNode Dummy(DAG.getRoot());
615
Jim Laskey26f7fa72006-10-17 19:33:52 +0000616 // The root of the dag may dangle to deleted nodes until the dag combiner is
617 // done. Set it to null to avoid confusion.
618 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000619
620 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
621 TargetLowering::DAGCombinerInfo
622 DagCombineInfo(DAG, !RunningAfterLegalize, this);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000623
Nate Begeman1d4d4142005-09-01 00:19:25 +0000624 // while the worklist isn't empty, inspect the node on the end of it and
625 // try and combine it.
626 while (!WorkList.empty()) {
627 SDNode *N = WorkList.back();
628 WorkList.pop_back();
629
630 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000631 // N is deleted from the DAG, since they too may now be dead or may have a
632 // reduced number of uses, allowing other xforms.
633 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000634 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000635 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000636
Chris Lattner95038592005-10-05 06:35:28 +0000637 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000638 continue;
639 }
640
Nate Begeman83e75ec2005-09-06 04:43:02 +0000641 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000642
643 // If nothing happened, try a target-specific DAG combine.
644 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000645 assert(N->getOpcode() != ISD::DELETED_NODE &&
646 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000647 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
648 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
649 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
650 }
651
Nate Begeman83e75ec2005-09-06 04:43:02 +0000652 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000653 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000654 // If we get back the same node we passed in, rather than a new node or
655 // zero, we know that the node must have defined multiple values and
656 // CombineTo was used. Since CombineTo takes care of the worklist
657 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000658 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000659 assert(N->getOpcode() != ISD::DELETED_NODE &&
660 RV.Val->getOpcode() != ISD::DELETED_NODE &&
661 "Node was deleted but visit returned new node!");
662
Jim Laskey6ff23e52006-10-04 16:53:27 +0000663 DEBUG(std::cerr << "\nReplacing.3 "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000664 std::cerr << "\nWith: "; RV.Val->dump(&DAG);
Nate Begeman2300f552005-09-07 00:15:36 +0000665 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000666 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000667 if (N->getNumValues() == RV.Val->getNumValues())
668 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
669 else {
670 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
671 SDOperand OpV = RV;
672 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
673 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000674
675 // Push the new node and any users onto the worklist
Jim Laskey6ff23e52006-10-04 16:53:27 +0000676 AddToWorkList(RV.Val);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000677 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000678
Jim Laskey6ff23e52006-10-04 16:53:27 +0000679 // Nodes can be reintroduced into the worklist. Make sure we do not
680 // process a node that has been replaced.
Nate Begeman646d7e22005-09-02 21:18:40 +0000681 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000682 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
683 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000684
685 // Finally, since the node is now dead, remove it from the graph.
686 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000687 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000688 }
689 }
Chris Lattner95038592005-10-05 06:35:28 +0000690
691 // If the root changed (e.g. it was a dead load, update the root).
692 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000693}
694
Nate Begeman83e75ec2005-09-06 04:43:02 +0000695SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000696 switch(N->getOpcode()) {
697 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000698 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000699 case ISD::ADD: return visitADD(N);
700 case ISD::SUB: return visitSUB(N);
701 case ISD::MUL: return visitMUL(N);
702 case ISD::SDIV: return visitSDIV(N);
703 case ISD::UDIV: return visitUDIV(N);
704 case ISD::SREM: return visitSREM(N);
705 case ISD::UREM: return visitUREM(N);
706 case ISD::MULHU: return visitMULHU(N);
707 case ISD::MULHS: return visitMULHS(N);
708 case ISD::AND: return visitAND(N);
709 case ISD::OR: return visitOR(N);
710 case ISD::XOR: return visitXOR(N);
711 case ISD::SHL: return visitSHL(N);
712 case ISD::SRA: return visitSRA(N);
713 case ISD::SRL: return visitSRL(N);
714 case ISD::CTLZ: return visitCTLZ(N);
715 case ISD::CTTZ: return visitCTTZ(N);
716 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000717 case ISD::SELECT: return visitSELECT(N);
718 case ISD::SELECT_CC: return visitSELECT_CC(N);
719 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000720 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
721 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000722 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000723 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
724 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000725 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000726 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000727 case ISD::FADD: return visitFADD(N);
728 case ISD::FSUB: return visitFSUB(N);
729 case ISD::FMUL: return visitFMUL(N);
730 case ISD::FDIV: return visitFDIV(N);
731 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000732 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000733 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
734 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
735 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
736 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
737 case ISD::FP_ROUND: return visitFP_ROUND(N);
738 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
739 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
740 case ISD::FNEG: return visitFNEG(N);
741 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000742 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000743 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000744 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000745 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000746 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
747 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000748 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000749 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000750 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000751 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
752 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
753 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
754 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
755 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
756 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
757 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
758 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000759 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000760 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000761}
762
Chris Lattner6270f682006-10-08 22:57:01 +0000763/// getInputChainForNode - Given a node, return its input chain if it has one,
764/// otherwise return a null sd operand.
765static SDOperand getInputChainForNode(SDNode *N) {
766 if (unsigned NumOps = N->getNumOperands()) {
767 if (N->getOperand(0).getValueType() == MVT::Other)
768 return N->getOperand(0);
769 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
770 return N->getOperand(NumOps-1);
771 for (unsigned i = 1; i < NumOps-1; ++i)
772 if (N->getOperand(i).getValueType() == MVT::Other)
773 return N->getOperand(i);
774 }
775 return SDOperand(0, 0);
776}
777
Nate Begeman83e75ec2005-09-06 04:43:02 +0000778SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000779 // If N has two operands, where one has an input chain equal to the other,
780 // the 'other' chain is redundant.
781 if (N->getNumOperands() == 2) {
782 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
783 return N->getOperand(0);
784 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
785 return N->getOperand(1);
786 }
787
788
Jim Laskey6ff23e52006-10-04 16:53:27 +0000789 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Jim Laskey279f0532006-09-25 16:29:54 +0000790 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000791 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000792
793 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000794 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000795
Jim Laskey71382342006-10-07 23:37:56 +0000796 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000797 // encountered.
798 for (unsigned i = 0; i < TFs.size(); ++i) {
799 SDNode *TF = TFs[i];
800
Jim Laskey6ff23e52006-10-04 16:53:27 +0000801 // Check each of the operands.
802 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
803 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000804
Jim Laskey6ff23e52006-10-04 16:53:27 +0000805 switch (Op.getOpcode()) {
806 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000807 // Entry tokens don't need to be added to the list. They are
808 // rededundant.
809 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000810 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000811
Jim Laskey6ff23e52006-10-04 16:53:27 +0000812 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000813 if ((CombinerAA || Op.hasOneUse()) &&
814 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000815 // Queue up for processing.
816 TFs.push_back(Op.Val);
817 // Clean up in case the token factor is removed.
818 AddToWorkList(Op.Val);
819 Changed = true;
820 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000821 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000822 // Fall thru
823
824 default:
Jim Laskeybc588b82006-10-05 15:07:25 +0000825 // Only add if not there prior.
826 if (std::find(Ops.begin(), Ops.end(), Op) == Ops.end())
827 Ops.push_back(Op);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000828 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000829 }
830 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000831 }
832
833 SDOperand Result;
834
835 // If we've change things around then replace token factor.
836 if (Changed) {
837 if (Ops.size() == 0) {
838 // The entry token is the only possible outcome.
839 Result = DAG.getEntryNode();
840 } else {
841 // New and improved token factor.
842 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000843 }
Jim Laskey274062c2006-10-13 23:32:28 +0000844
845 // Don't add users to work list.
846 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000847 }
Jim Laskey279f0532006-09-25 16:29:54 +0000848
Jim Laskey6ff23e52006-10-04 16:53:27 +0000849 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000850}
851
Nate Begeman83e75ec2005-09-06 04:43:02 +0000852SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000853 SDOperand N0 = N->getOperand(0);
854 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000855 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
856 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000857 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000858
859 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000860 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000861 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000862 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000863 if (N0C && !N1C)
864 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000865 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000866 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000867 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000868 // fold ((c1-A)+c2) -> (c1+c2)-A
869 if (N1C && N0.getOpcode() == ISD::SUB)
870 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
871 return DAG.getNode(ISD::SUB, VT,
872 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
873 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000874 // reassociate add
875 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
876 if (RADD.Val != 0)
877 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000878 // fold ((0-A) + B) -> B-A
879 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
880 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000881 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000882 // fold (A + (0-B)) -> A-B
883 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
884 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000885 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000886 // fold (A+(B-A)) -> B
887 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000888 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000889
Evan Cheng860771d2006-03-01 01:09:54 +0000890 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000891 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000892
893 // fold (a+b) -> (a|b) iff a and b share no bits.
894 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
895 uint64_t LHSZero, LHSOne;
896 uint64_t RHSZero, RHSOne;
897 uint64_t Mask = MVT::getIntVTBitMask(VT);
898 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
899 if (LHSZero) {
900 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
901
902 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
903 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
904 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
905 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
906 return DAG.getNode(ISD::OR, VT, N0, N1);
907 }
908 }
Evan Cheng3ef554d2006-11-06 08:14:30 +0000909
Nate Begeman83e75ec2005-09-06 04:43:02 +0000910 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000911}
912
Nate Begeman83e75ec2005-09-06 04:43:02 +0000913SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000914 SDOperand N0 = N->getOperand(0);
915 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000916 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
917 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000918 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000919
Chris Lattner854077d2005-10-17 01:07:11 +0000920 // fold (sub x, x) -> 0
921 if (N0 == N1)
922 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000923 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000924 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000925 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000926 // fold (sub x, c) -> (add x, -c)
927 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000928 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000929 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000930 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000931 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000932 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000933 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000934 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000935 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000936}
937
Nate Begeman83e75ec2005-09-06 04:43:02 +0000938SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000939 SDOperand N0 = N->getOperand(0);
940 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000941 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
942 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000943 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000944
945 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000946 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000947 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000948 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000949 if (N0C && !N1C)
950 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000951 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000952 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000953 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000954 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000955 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000956 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000957 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000958 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000959 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000960 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000961 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000962 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
963 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
964 // FIXME: If the input is something that is easily negated (e.g. a
965 // single-use add), we should put the negate there.
966 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
967 DAG.getNode(ISD::SHL, VT, N0,
968 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
969 TLI.getShiftAmountTy())));
970 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +0000971
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000972 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
973 if (N1C && N0.getOpcode() == ISD::SHL &&
974 isa<ConstantSDNode>(N0.getOperand(1))) {
975 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000976 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000977 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
978 }
979
980 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
981 // use.
982 {
983 SDOperand Sh(0,0), Y(0,0);
984 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
985 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
986 N0.Val->hasOneUse()) {
987 Sh = N0; Y = N1;
988 } else if (N1.getOpcode() == ISD::SHL &&
989 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
990 Sh = N1; Y = N0;
991 }
992 if (Sh.Val) {
993 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
994 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
995 }
996 }
Chris Lattnera1deca32006-03-04 23:33:26 +0000997 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
998 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
999 isa<ConstantSDNode>(N0.getOperand(1))) {
1000 return DAG.getNode(ISD::ADD, VT,
1001 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1002 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1003 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001004
Nate Begemancd4d58c2006-02-03 06:46:56 +00001005 // reassociate mul
1006 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1007 if (RMUL.Val != 0)
1008 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +00001009 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001010}
1011
Nate Begeman83e75ec2005-09-06 04:43:02 +00001012SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001013 SDOperand N0 = N->getOperand(0);
1014 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001015 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1016 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001017 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001018
1019 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001020 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001021 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001022 // fold (sdiv X, 1) -> X
1023 if (N1C && N1C->getSignExtended() == 1LL)
1024 return N0;
1025 // fold (sdiv X, -1) -> 0-X
1026 if (N1C && N1C->isAllOnesValue())
1027 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001028 // If we know the sign bits of both operands are zero, strength reduce to a
1029 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
1030 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001031 if (TLI.MaskedValueIsZero(N1, SignBit) &&
1032 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +00001033 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001034 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +00001035 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001036 (isPowerOf2_64(N1C->getSignExtended()) ||
1037 isPowerOf2_64(-N1C->getSignExtended()))) {
1038 // If dividing by powers of two is cheap, then don't perform the following
1039 // fold.
1040 if (TLI.isPow2DivCheap())
1041 return SDOperand();
1042 int64_t pow2 = N1C->getSignExtended();
1043 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001044 unsigned lg2 = Log2_64(abs2);
1045 // Splat the sign bit into the register
1046 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001047 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1048 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001049 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001050 // Add (N0 < 0) ? abs2 - 1 : 0;
1051 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1052 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001053 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001054 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001055 AddToWorkList(SRL.Val);
1056 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001057 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1058 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001059 // If we're dividing by a positive value, we're done. Otherwise, we must
1060 // negate the result.
1061 if (pow2 > 0)
1062 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001063 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001064 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1065 }
Nate Begeman69575232005-10-20 02:15:44 +00001066 // if integer divide is expensive and we satisfy the requirements, emit an
1067 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001068 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001069 !TLI.isIntDivCheap()) {
1070 SDOperand Op = BuildSDIV(N);
1071 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001072 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001073 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001074}
1075
Nate Begeman83e75ec2005-09-06 04:43:02 +00001076SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001077 SDOperand N0 = N->getOperand(0);
1078 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001079 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1080 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001081 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001082
1083 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001084 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001085 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001086 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +00001087 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001088 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001089 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001090 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001091 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1092 if (N1.getOpcode() == ISD::SHL) {
1093 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1094 if (isPowerOf2_64(SHC->getValue())) {
1095 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001096 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
1097 DAG.getConstant(Log2_64(SHC->getValue()),
1098 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001099 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001100 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001101 }
1102 }
1103 }
Nate Begeman69575232005-10-20 02:15:44 +00001104 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +00001105 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
1106 SDOperand Op = BuildUDIV(N);
1107 if (Op.Val) return Op;
1108 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001109 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001110}
1111
Nate Begeman83e75ec2005-09-06 04:43:02 +00001112SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001113 SDOperand N0 = N->getOperand(0);
1114 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001115 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1116 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001117 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001118
1119 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001120 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001121 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001122 // If we know the sign bits of both operands are zero, strength reduce to a
1123 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
1124 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001125 if (TLI.MaskedValueIsZero(N1, SignBit) &&
1126 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +00001127 return DAG.getNode(ISD::UREM, VT, N0, N1);
Chris Lattner26d29902006-10-12 20:58:32 +00001128
1129 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1130 // the remainder operation.
1131 if (N1C && !N1C->isNullValue()) {
1132 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
1133 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1134 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1135 AddToWorkList(Div.Val);
1136 AddToWorkList(Mul.Val);
1137 return Sub;
1138 }
1139
Nate Begeman83e75ec2005-09-06 04:43:02 +00001140 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001141}
1142
Nate Begeman83e75ec2005-09-06 04:43:02 +00001143SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001144 SDOperand N0 = N->getOperand(0);
1145 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001146 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1147 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001148 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001149
1150 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001151 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001152 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001153 // fold (urem x, pow2) -> (and x, pow2-1)
1154 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +00001155 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001156 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1157 if (N1.getOpcode() == ISD::SHL) {
1158 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1159 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +00001160 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001161 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001162 return DAG.getNode(ISD::AND, VT, N0, Add);
1163 }
1164 }
1165 }
Chris Lattner26d29902006-10-12 20:58:32 +00001166
1167 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1168 // the remainder operation.
1169 if (N1C && !N1C->isNullValue()) {
1170 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
1171 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1172 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1173 AddToWorkList(Div.Val);
1174 AddToWorkList(Mul.Val);
1175 return Sub;
1176 }
1177
Nate Begeman83e75ec2005-09-06 04:43:02 +00001178 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001179}
1180
Nate Begeman83e75ec2005-09-06 04:43:02 +00001181SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001182 SDOperand N0 = N->getOperand(0);
1183 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001184 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001185
1186 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001187 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001188 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001189 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001190 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001191 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1192 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001193 TLI.getShiftAmountTy()));
1194 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001195}
1196
Nate Begeman83e75ec2005-09-06 04:43:02 +00001197SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001198 SDOperand N0 = N->getOperand(0);
1199 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001200 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001201
1202 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001203 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001204 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001205 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001206 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001207 return DAG.getConstant(0, N0.getValueType());
1208 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001209}
1210
Chris Lattner35e5c142006-05-05 05:51:50 +00001211/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1212/// two operands of the same opcode, try to simplify it.
1213SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1214 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1215 MVT::ValueType VT = N0.getValueType();
1216 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1217
Chris Lattner540121f2006-05-05 06:31:05 +00001218 // For each of OP in AND/OR/XOR:
1219 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1220 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1221 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001222 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001223 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001224 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001225 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1226 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1227 N0.getOperand(0).getValueType(),
1228 N0.getOperand(0), N1.getOperand(0));
1229 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001230 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001231 }
1232
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001233 // For each of OP in SHL/SRL/SRA/AND...
1234 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1235 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1236 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001237 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001238 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001239 N0.getOperand(1) == N1.getOperand(1)) {
1240 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1241 N0.getOperand(0).getValueType(),
1242 N0.getOperand(0), N1.getOperand(0));
1243 AddToWorkList(ORNode.Val);
1244 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1245 }
1246
1247 return SDOperand();
1248}
1249
Nate Begeman83e75ec2005-09-06 04:43:02 +00001250SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001251 SDOperand N0 = N->getOperand(0);
1252 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001253 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001254 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1255 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001256 MVT::ValueType VT = N1.getValueType();
1257
1258 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001259 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001260 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001261 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001262 if (N0C && !N1C)
1263 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001264 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001265 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001266 return N0;
1267 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +00001268 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001269 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001270 // reassociate and
1271 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1272 if (RAND.Val != 0)
1273 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001274 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001275 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001276 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001277 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001278 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001279 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1280 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001281 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +00001282 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001283 ~N1C->getValue() & InMask)) {
1284 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1285 N0.getOperand(0));
1286
1287 // Replace uses of the AND with uses of the Zero extend node.
1288 CombineTo(N, Zext);
1289
Chris Lattner3603cd62006-02-02 07:17:31 +00001290 // We actually want to replace all uses of the any_extend with the
1291 // zero_extend, to avoid duplicating things. This will later cause this
1292 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001293 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001294 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001295 }
1296 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001297 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1298 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1299 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1300 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1301
1302 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1303 MVT::isInteger(LL.getValueType())) {
1304 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1305 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1306 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001307 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001308 return DAG.getSetCC(VT, ORNode, LR, Op1);
1309 }
1310 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1311 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1312 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001313 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001314 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1315 }
1316 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1317 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1318 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001319 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001320 return DAG.getSetCC(VT, ORNode, LR, Op1);
1321 }
1322 }
1323 // canonicalize equivalent to ll == rl
1324 if (LL == RR && LR == RL) {
1325 Op1 = ISD::getSetCCSwappedOperands(Op1);
1326 std::swap(RL, RR);
1327 }
1328 if (LL == RL && LR == RR) {
1329 bool isInteger = MVT::isInteger(LL.getValueType());
1330 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1331 if (Result != ISD::SETCC_INVALID)
1332 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1333 }
1334 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001335
1336 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1337 if (N0.getOpcode() == N1.getOpcode()) {
1338 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1339 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001340 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001341
Nate Begemande996292006-02-03 22:24:05 +00001342 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1343 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001344 if (!MVT::isVector(VT) &&
1345 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001346 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001347 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Chengc5484282006-10-04 00:56:09 +00001348 if (ISD::isEXTLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001349 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001350 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001351 // If we zero all the possible extended bits, then we can turn this into
1352 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001353 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001354 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001355 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1356 LN0->getBasePtr(), LN0->getSrcValue(),
1357 LN0->getSrcValueOffset(), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001358 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001359 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001360 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001361 }
1362 }
1363 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Chengc5484282006-10-04 00:56:09 +00001364 if (ISD::isSEXTLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001365 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001366 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001367 // If we zero all the possible extended bits, then we can turn this into
1368 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001369 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001370 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001371 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1372 LN0->getBasePtr(), LN0->getSrcValue(),
1373 LN0->getSrcValueOffset(), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001374 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001375 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001376 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001377 }
1378 }
Chris Lattner15045b62006-02-28 06:35:35 +00001379
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001380 // fold (and (load x), 255) -> (zextload x, i8)
1381 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001382 if (N1C && N0.getOpcode() == ISD::LOAD) {
1383 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1384 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
1385 N0.hasOneUse()) {
1386 MVT::ValueType EVT, LoadedVT;
1387 if (N1C->getValue() == 255)
1388 EVT = MVT::i8;
1389 else if (N1C->getValue() == 65535)
1390 EVT = MVT::i16;
1391 else if (N1C->getValue() == ~0U)
1392 EVT = MVT::i32;
1393 else
1394 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001395
Evan Cheng2e49f092006-10-11 07:10:22 +00001396 LoadedVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001397 if (EVT != MVT::Other && LoadedVT > EVT &&
1398 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1399 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1400 // For big endian targets, we need to add an offset to the pointer to
1401 // load the correct bytes. For little endian systems, we merely need to
1402 // read fewer bytes from the same pointer.
1403 unsigned PtrOff =
1404 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1405 SDOperand NewPtr = LN0->getBasePtr();
1406 if (!TLI.isLittleEndian())
1407 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1408 DAG.getConstant(PtrOff, PtrType));
1409 AddToWorkList(NewPtr.Val);
1410 SDOperand Load =
1411 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
1412 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT);
1413 AddToWorkList(N);
1414 CombineTo(N0.Val, Load, Load.getValue(1));
1415 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1416 }
Chris Lattner15045b62006-02-28 06:35:35 +00001417 }
1418 }
1419
Nate Begeman83e75ec2005-09-06 04:43:02 +00001420 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001421}
1422
Nate Begeman83e75ec2005-09-06 04:43:02 +00001423SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001424 SDOperand N0 = N->getOperand(0);
1425 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001426 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001427 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1428 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001429 MVT::ValueType VT = N1.getValueType();
1430 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001431
1432 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001433 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001434 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001435 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001436 if (N0C && !N1C)
1437 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001438 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001439 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001440 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001441 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001442 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001443 return N1;
1444 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001445 if (N1C &&
1446 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001447 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001448 // reassociate or
1449 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1450 if (ROR.Val != 0)
1451 return ROR;
1452 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1453 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001454 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001455 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1456 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1457 N1),
1458 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001459 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001460 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1461 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1462 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1463 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1464
1465 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1466 MVT::isInteger(LL.getValueType())) {
1467 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1468 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1469 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1470 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1471 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001472 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001473 return DAG.getSetCC(VT, ORNode, LR, Op1);
1474 }
1475 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1476 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1477 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1478 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1479 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001480 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001481 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1482 }
1483 }
1484 // canonicalize equivalent to ll == rl
1485 if (LL == RR && LR == RL) {
1486 Op1 = ISD::getSetCCSwappedOperands(Op1);
1487 std::swap(RL, RR);
1488 }
1489 if (LL == RL && LR == RR) {
1490 bool isInteger = MVT::isInteger(LL.getValueType());
1491 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1492 if (Result != ISD::SETCC_INVALID)
1493 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1494 }
1495 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001496
1497 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1498 if (N0.getOpcode() == N1.getOpcode()) {
1499 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1500 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001501 }
Chris Lattner516b9622006-09-14 20:50:57 +00001502
Chris Lattner1ec72732006-09-14 21:11:37 +00001503 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1504 if (N0.getOpcode() == ISD::AND &&
1505 N1.getOpcode() == ISD::AND &&
1506 N0.getOperand(1).getOpcode() == ISD::Constant &&
1507 N1.getOperand(1).getOpcode() == ISD::Constant &&
1508 // Don't increase # computations.
1509 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1510 // We can only do this xform if we know that bits from X that are set in C2
1511 // but not in C1 are already zero. Likewise for Y.
1512 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1513 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1514
1515 if (TLI.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1516 TLI.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
1517 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1518 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1519 }
1520 }
1521
1522
Chris Lattner516b9622006-09-14 20:50:57 +00001523 // See if this is some rotate idiom.
1524 if (SDNode *Rot = MatchRotate(N0, N1))
1525 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001526
Nate Begeman83e75ec2005-09-06 04:43:02 +00001527 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001528}
1529
Chris Lattner516b9622006-09-14 20:50:57 +00001530
1531/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1532static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1533 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001534 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001535 Mask = Op.getOperand(1);
1536 Op = Op.getOperand(0);
1537 } else {
1538 return false;
1539 }
1540 }
1541
1542 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1543 Shift = Op;
1544 return true;
1545 }
1546 return false;
1547}
1548
1549
1550// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1551// idioms for rotate, and if the target supports rotation instructions, generate
1552// a rot[lr].
1553SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1554 // Must be a legal type. Expanded an promoted things won't work with rotates.
1555 MVT::ValueType VT = LHS.getValueType();
1556 if (!TLI.isTypeLegal(VT)) return 0;
1557
1558 // The target must have at least one rotate flavor.
1559 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1560 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1561 if (!HasROTL && !HasROTR) return 0;
1562
1563 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1564 SDOperand LHSShift; // The shift.
1565 SDOperand LHSMask; // AND value if any.
1566 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1567 return 0; // Not part of a rotate.
1568
1569 SDOperand RHSShift; // The shift.
1570 SDOperand RHSMask; // AND value if any.
1571 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1572 return 0; // Not part of a rotate.
1573
1574 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1575 return 0; // Not shifting the same value.
1576
1577 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1578 return 0; // Shifts must disagree.
1579
1580 // Canonicalize shl to left side in a shl/srl pair.
1581 if (RHSShift.getOpcode() == ISD::SHL) {
1582 std::swap(LHS, RHS);
1583 std::swap(LHSShift, RHSShift);
1584 std::swap(LHSMask , RHSMask );
1585 }
1586
1587 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1588
1589 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1590 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
1591 if (LHSShift.getOperand(1).getOpcode() == ISD::Constant &&
1592 RHSShift.getOperand(1).getOpcode() == ISD::Constant) {
1593 uint64_t LShVal = cast<ConstantSDNode>(LHSShift.getOperand(1))->getValue();
1594 uint64_t RShVal = cast<ConstantSDNode>(RHSShift.getOperand(1))->getValue();
1595 if ((LShVal + RShVal) != OpSizeInBits)
1596 return 0;
1597
1598 SDOperand Rot;
1599 if (HasROTL)
1600 Rot = DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1601 LHSShift.getOperand(1));
1602 else
1603 Rot = DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1604 RHSShift.getOperand(1));
1605
1606 // If there is an AND of either shifted operand, apply it to the result.
1607 if (LHSMask.Val || RHSMask.Val) {
1608 uint64_t Mask = MVT::getIntVTBitMask(VT);
1609
1610 if (LHSMask.Val) {
1611 uint64_t RHSBits = (1ULL << LShVal)-1;
1612 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1613 }
1614 if (RHSMask.Val) {
1615 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1616 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1617 }
1618
1619 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1620 }
1621
1622 return Rot.Val;
1623 }
1624
1625 // If there is a mask here, and we have a variable shift, we can't be sure
1626 // that we're masking out the right stuff.
1627 if (LHSMask.Val || RHSMask.Val)
1628 return 0;
1629
1630 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1631 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
1632 if (RHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1633 LHSShift.getOperand(1) == RHSShift.getOperand(1).getOperand(1)) {
1634 if (ConstantSDNode *SUBC =
1635 dyn_cast<ConstantSDNode>(RHSShift.getOperand(1).getOperand(0))) {
1636 if (SUBC->getValue() == OpSizeInBits)
1637 if (HasROTL)
1638 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1639 LHSShift.getOperand(1)).Val;
1640 else
1641 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1642 LHSShift.getOperand(1)).Val;
1643 }
1644 }
1645
1646 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1647 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
1648 if (LHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1649 RHSShift.getOperand(1) == LHSShift.getOperand(1).getOperand(1)) {
1650 if (ConstantSDNode *SUBC =
1651 dyn_cast<ConstantSDNode>(LHSShift.getOperand(1).getOperand(0))) {
1652 if (SUBC->getValue() == OpSizeInBits)
1653 if (HasROTL)
1654 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1655 LHSShift.getOperand(1)).Val;
1656 else
1657 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1658 RHSShift.getOperand(1)).Val;
1659 }
1660 }
1661
1662 return 0;
1663}
1664
1665
Nate Begeman83e75ec2005-09-06 04:43:02 +00001666SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001667 SDOperand N0 = N->getOperand(0);
1668 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001669 SDOperand LHS, RHS, CC;
1670 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1671 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001672 MVT::ValueType VT = N0.getValueType();
1673
1674 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001675 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001676 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001677 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001678 if (N0C && !N1C)
1679 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001680 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001681 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001682 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001683 // reassociate xor
1684 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1685 if (RXOR.Val != 0)
1686 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001687 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001688 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1689 bool isInt = MVT::isInteger(LHS.getValueType());
1690 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1691 isInt);
1692 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001693 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001694 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001695 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001696 assert(0 && "Unhandled SetCC Equivalent!");
1697 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001698 }
Nate Begeman99801192005-09-07 23:25:52 +00001699 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1700 if (N1C && N1C->getValue() == 1 &&
1701 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001702 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001703 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1704 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001705 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1706 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001707 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001708 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001709 }
1710 }
Nate Begeman99801192005-09-07 23:25:52 +00001711 // fold !(x or y) -> (!x and !y) iff x or y are constants
1712 if (N1C && N1C->isAllOnesValue() &&
1713 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001714 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001715 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1716 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001717 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1718 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001719 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001720 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001721 }
1722 }
Nate Begeman223df222005-09-08 20:18:10 +00001723 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1724 if (N1C && N0.getOpcode() == ISD::XOR) {
1725 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1726 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1727 if (N00C)
1728 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1729 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1730 if (N01C)
1731 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1732 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1733 }
1734 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001735 if (N0 == N1) {
1736 if (!MVT::isVector(VT)) {
1737 return DAG.getConstant(0, VT);
1738 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1739 // Produce a vector of zeros.
1740 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1741 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001742 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001743 }
1744 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001745
1746 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1747 if (N0.getOpcode() == N1.getOpcode()) {
1748 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1749 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001750 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001751
Chris Lattner3e104b12006-04-08 04:15:24 +00001752 // Simplify the expression using non-local knowledge.
1753 if (!MVT::isVector(VT) &&
1754 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001755 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001756
Nate Begeman83e75ec2005-09-06 04:43:02 +00001757 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001758}
1759
Nate Begeman83e75ec2005-09-06 04:43:02 +00001760SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001761 SDOperand N0 = N->getOperand(0);
1762 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001763 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1764 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001765 MVT::ValueType VT = N0.getValueType();
1766 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1767
1768 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001769 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001770 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001771 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001772 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001773 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001774 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001775 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001776 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001777 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001778 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001779 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001780 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001781 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001782 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001783 if (SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001784 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001785 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001786 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001787 N0.getOperand(1).getOpcode() == ISD::Constant) {
1788 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001789 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001790 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001791 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001792 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001793 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001794 }
1795 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1796 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001797 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001798 N0.getOperand(1).getOpcode() == ISD::Constant) {
1799 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001800 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001801 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1802 DAG.getConstant(~0ULL << c1, VT));
1803 if (c2 > c1)
1804 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001805 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001806 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001807 return DAG.getNode(ISD::SRL, VT, Mask,
1808 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001809 }
1810 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001811 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001812 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001813 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnercac70592006-03-05 19:53:55 +00001814 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1815 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1816 isa<ConstantSDNode>(N0.getOperand(1))) {
1817 return DAG.getNode(ISD::ADD, VT,
1818 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1819 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1820 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001821 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001822}
1823
Nate Begeman83e75ec2005-09-06 04:43:02 +00001824SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001825 SDOperand N0 = N->getOperand(0);
1826 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001827 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1828 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001829 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001830
1831 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001832 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001833 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001834 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001835 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001836 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001837 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001838 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001839 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001840 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001841 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001842 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001843 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001844 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001845 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001846 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1847 // sext_inreg.
1848 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1849 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1850 MVT::ValueType EVT;
1851 switch (LowBits) {
1852 default: EVT = MVT::Other; break;
1853 case 1: EVT = MVT::i1; break;
1854 case 8: EVT = MVT::i8; break;
1855 case 16: EVT = MVT::i16; break;
1856 case 32: EVT = MVT::i32; break;
1857 }
1858 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1859 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1860 DAG.getValueType(EVT));
1861 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001862
1863 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1864 if (N1C && N0.getOpcode() == ISD::SRA) {
1865 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1866 unsigned Sum = N1C->getValue() + C1->getValue();
1867 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1868 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1869 DAG.getConstant(Sum, N1C->getValueType(0)));
1870 }
1871 }
1872
Chris Lattnera8504462006-05-08 20:51:54 +00001873 // Simplify, based on bits shifted out of the LHS.
1874 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
1875 return SDOperand(N, 0);
1876
1877
Nate Begeman1d4d4142005-09-01 00:19:25 +00001878 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001879 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001880 return DAG.getNode(ISD::SRL, VT, N0, N1);
1881 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001882}
1883
Nate Begeman83e75ec2005-09-06 04:43:02 +00001884SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001885 SDOperand N0 = N->getOperand(0);
1886 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001887 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1888 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001889 MVT::ValueType VT = N0.getValueType();
1890 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1891
1892 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001893 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001894 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001895 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001896 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001897 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001898 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001899 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001900 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001901 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001902 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001903 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001904 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001905 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001906 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001907 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001908 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001909 N0.getOperand(1).getOpcode() == ISD::Constant) {
1910 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001911 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001912 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001913 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001914 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001915 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001916 }
Chris Lattner350bec02006-04-02 06:11:11 +00001917
Chris Lattner06afe072006-05-05 22:53:17 +00001918 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
1919 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1920 // Shifting in all undef bits?
1921 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
1922 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
1923 return DAG.getNode(ISD::UNDEF, VT);
1924
1925 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
1926 AddToWorkList(SmallShift.Val);
1927 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
1928 }
1929
Chris Lattner3657ffe2006-10-12 20:23:19 +00001930 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
1931 // bit, which is unmodified by sra.
1932 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
1933 if (N0.getOpcode() == ISD::SRA)
1934 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
1935 }
1936
Chris Lattner350bec02006-04-02 06:11:11 +00001937 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1938 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1939 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1940 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1941 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
1942
1943 // If any of the input bits are KnownOne, then the input couldn't be all
1944 // zeros, thus the result of the srl will always be zero.
1945 if (KnownOne) return DAG.getConstant(0, VT);
1946
1947 // If all of the bits input the to ctlz node are known to be zero, then
1948 // the result of the ctlz is "32" and the result of the shift is one.
1949 uint64_t UnknownBits = ~KnownZero & Mask;
1950 if (UnknownBits == 0) return DAG.getConstant(1, VT);
1951
1952 // Otherwise, check to see if there is exactly one bit input to the ctlz.
1953 if ((UnknownBits & (UnknownBits-1)) == 0) {
1954 // Okay, we know that only that the single bit specified by UnknownBits
1955 // could be set on input to the CTLZ node. If this bit is set, the SRL
1956 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
1957 // to an SRL,XOR pair, which is likely to simplify more.
1958 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
1959 SDOperand Op = N0.getOperand(0);
1960 if (ShAmt) {
1961 Op = DAG.getNode(ISD::SRL, VT, Op,
1962 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
1963 AddToWorkList(Op.Val);
1964 }
1965 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
1966 }
1967 }
1968
Nate Begeman83e75ec2005-09-06 04:43:02 +00001969 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001970}
1971
Nate Begeman83e75ec2005-09-06 04:43:02 +00001972SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001973 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001974 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001975
1976 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001977 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001978 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001979 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001980}
1981
Nate Begeman83e75ec2005-09-06 04:43:02 +00001982SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001983 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001984 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001985
1986 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001987 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001988 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001989 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001990}
1991
Nate Begeman83e75ec2005-09-06 04:43:02 +00001992SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001993 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001994 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001995
1996 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001997 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001998 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001999 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002000}
2001
Nate Begeman452d7be2005-09-16 00:54:12 +00002002SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2003 SDOperand N0 = N->getOperand(0);
2004 SDOperand N1 = N->getOperand(1);
2005 SDOperand N2 = N->getOperand(2);
2006 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2007 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2008 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2009 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00002010
Nate Begeman452d7be2005-09-16 00:54:12 +00002011 // fold select C, X, X -> X
2012 if (N1 == N2)
2013 return N1;
2014 // fold select true, X, Y -> X
2015 if (N0C && !N0C->isNullValue())
2016 return N1;
2017 // fold select false, X, Y -> Y
2018 if (N0C && N0C->isNullValue())
2019 return N2;
2020 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002021 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002022 return DAG.getNode(ISD::OR, VT, N0, N2);
2023 // fold select C, 0, X -> ~C & X
2024 // FIXME: this should check for C type == X type, not i1?
2025 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
2026 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002027 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002028 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2029 }
2030 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002031 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002032 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002033 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002034 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2035 }
2036 // fold select C, X, 0 -> C & X
2037 // FIXME: this should check for C type == X type, not i1?
2038 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2039 return DAG.getNode(ISD::AND, VT, N0, N1);
2040 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2041 if (MVT::i1 == VT && N0 == N1)
2042 return DAG.getNode(ISD::OR, VT, N0, N2);
2043 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2044 if (MVT::i1 == VT && N0 == N2)
2045 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002046
Chris Lattner40c62d52005-10-18 06:04:22 +00002047 // If we can fold this based on the true/false value, do so.
2048 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002049 return SDOperand(N, 0); // Don't revisit N.
2050
Nate Begeman44728a72005-09-19 22:34:01 +00002051 // fold selects based on a setcc into other things, such as min/max/abs
2052 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00002053 // FIXME:
2054 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2055 // having to say they don't support SELECT_CC on every type the DAG knows
2056 // about, since there is no way to mark an opcode illegal at all value types
2057 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2058 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2059 N1, N2, N0.getOperand(2));
2060 else
2061 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002062 return SDOperand();
2063}
2064
2065SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002066 SDOperand N0 = N->getOperand(0);
2067 SDOperand N1 = N->getOperand(1);
2068 SDOperand N2 = N->getOperand(2);
2069 SDOperand N3 = N->getOperand(3);
2070 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002071 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2072
Nate Begeman44728a72005-09-19 22:34:01 +00002073 // fold select_cc lhs, rhs, x, x, cc -> x
2074 if (N2 == N3)
2075 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002076
Chris Lattner5f42a242006-09-20 06:19:26 +00002077 // Determine if the condition we're dealing with is constant
2078 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002079 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002080
2081 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
2082 if (SCCC->getValue())
2083 return N2; // cond always true -> true val
2084 else
2085 return N3; // cond always false -> false val
2086 }
2087
2088 // Fold to a simpler select_cc
2089 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2090 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2091 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2092 SCC.getOperand(2));
2093
Chris Lattner40c62d52005-10-18 06:04:22 +00002094 // If we can fold this based on the true/false value, do so.
2095 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002096 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002097
Nate Begeman44728a72005-09-19 22:34:01 +00002098 // fold select_cc into other things, such as min/max/abs
2099 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002100}
2101
2102SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2103 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2104 cast<CondCodeSDNode>(N->getOperand(2))->get());
2105}
2106
Nate Begeman83e75ec2005-09-06 04:43:02 +00002107SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002108 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002109 MVT::ValueType VT = N->getValueType(0);
2110
Nate Begeman1d4d4142005-09-01 00:19:25 +00002111 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002112 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002113 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002114
Nate Begeman1d4d4142005-09-01 00:19:25 +00002115 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002116 // fold (sext (aext x)) -> (sext x)
2117 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002118 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002119
Chris Lattner6007b842006-09-21 06:00:20 +00002120 // fold (sext (truncate x)) -> (sextinreg x).
2121 if (N0.getOpcode() == ISD::TRUNCATE &&
Chris Lattnerbf370872006-09-21 06:17:39 +00002122 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2123 N0.getValueType()))) {
Chris Lattner6007b842006-09-21 06:00:20 +00002124 SDOperand Op = N0.getOperand(0);
2125 if (Op.getValueType() < VT) {
2126 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2127 } else if (Op.getValueType() > VT) {
2128 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2129 }
2130 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00002131 DAG.getValueType(N0.getValueType()));
Chris Lattner6007b842006-09-21 06:00:20 +00002132 }
Chris Lattner310b5782006-05-06 23:06:26 +00002133
Evan Cheng110dec22005-12-14 02:19:23 +00002134 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002135 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002136 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng466685d2006-10-09 20:57:25 +00002137 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2138 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2139 LN0->getBasePtr(), LN0->getSrcValue(),
2140 LN0->getSrcValueOffset(),
Nate Begeman3df4d522005-10-12 20:40:40 +00002141 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00002142 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00002143 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2144 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002145 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002146 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002147
2148 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2149 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Chengc5484282006-10-04 00:56:09 +00002150 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002151 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002152 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002153 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2154 LN0->getBasePtr(), LN0->getSrcValue(),
2155 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002156 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002157 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2158 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002159 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002160 }
2161
Nate Begeman83e75ec2005-09-06 04:43:02 +00002162 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002163}
2164
Nate Begeman83e75ec2005-09-06 04:43:02 +00002165SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002166 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002167 MVT::ValueType VT = N->getValueType(0);
2168
Nate Begeman1d4d4142005-09-01 00:19:25 +00002169 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002170 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002171 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002172 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002173 // fold (zext (aext x)) -> (zext x)
2174 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002175 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002176
2177 // fold (zext (truncate x)) -> (and x, mask)
2178 if (N0.getOpcode() == ISD::TRUNCATE &&
2179 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2180 SDOperand Op = N0.getOperand(0);
2181 if (Op.getValueType() < VT) {
2182 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2183 } else if (Op.getValueType() > VT) {
2184 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2185 }
2186 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2187 }
2188
Chris Lattner111c2282006-09-21 06:14:31 +00002189 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2190 if (N0.getOpcode() == ISD::AND &&
2191 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2192 N0.getOperand(1).getOpcode() == ISD::Constant) {
2193 SDOperand X = N0.getOperand(0).getOperand(0);
2194 if (X.getValueType() < VT) {
2195 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2196 } else if (X.getValueType() > VT) {
2197 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2198 }
2199 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2200 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2201 }
2202
Evan Cheng110dec22005-12-14 02:19:23 +00002203 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002204 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002205 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002206 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2207 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2208 LN0->getBasePtr(), LN0->getSrcValue(),
2209 LN0->getSrcValueOffset(),
Evan Cheng110dec22005-12-14 02:19:23 +00002210 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00002211 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00002212 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2213 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002214 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00002215 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002216
2217 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2218 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Chengc5484282006-10-04 00:56:09 +00002219 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002220 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002221 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002222 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2223 LN0->getBasePtr(), LN0->getSrcValue(),
2224 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002225 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002226 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2227 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002228 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002229 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002230 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002231}
2232
Chris Lattner5ffc0662006-05-05 05:58:59 +00002233SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2234 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002235 MVT::ValueType VT = N->getValueType(0);
2236
2237 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002238 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002239 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2240 // fold (aext (aext x)) -> (aext x)
2241 // fold (aext (zext x)) -> (zext x)
2242 // fold (aext (sext x)) -> (sext x)
2243 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2244 N0.getOpcode() == ISD::ZERO_EXTEND ||
2245 N0.getOpcode() == ISD::SIGN_EXTEND)
2246 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2247
Chris Lattner84750582006-09-20 06:29:17 +00002248 // fold (aext (truncate x))
2249 if (N0.getOpcode() == ISD::TRUNCATE) {
2250 SDOperand TruncOp = N0.getOperand(0);
2251 if (TruncOp.getValueType() == VT)
2252 return TruncOp; // x iff x size == zext size.
2253 if (TruncOp.getValueType() > VT)
2254 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2255 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2256 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002257
2258 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2259 if (N0.getOpcode() == ISD::AND &&
2260 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2261 N0.getOperand(1).getOpcode() == ISD::Constant) {
2262 SDOperand X = N0.getOperand(0).getOperand(0);
2263 if (X.getValueType() < VT) {
2264 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2265 } else if (X.getValueType() > VT) {
2266 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2267 }
2268 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2269 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2270 }
2271
Chris Lattner5ffc0662006-05-05 05:58:59 +00002272 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002273 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002274 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002275 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2276 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2277 LN0->getBasePtr(), LN0->getSrcValue(),
2278 LN0->getSrcValueOffset(),
Chris Lattner5ffc0662006-05-05 05:58:59 +00002279 N0.getValueType());
2280 CombineTo(N, ExtLoad);
2281 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2282 ExtLoad.getValue(1));
2283 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2284 }
2285
2286 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2287 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2288 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002289 if (N0.getOpcode() == ISD::LOAD && !ISD::isNON_EXTLoad(N0.Val) &&
2290 N0.hasOneUse()) {
2291 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002292 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002293 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
2294 LN0->getChain(), LN0->getBasePtr(),
2295 LN0->getSrcValue(),
2296 LN0->getSrcValueOffset(), EVT);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002297 CombineTo(N, ExtLoad);
2298 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2299 ExtLoad.getValue(1));
2300 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2301 }
2302 return SDOperand();
2303}
2304
2305
Nate Begeman83e75ec2005-09-06 04:43:02 +00002306SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002307 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002308 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002309 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002310 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00002311 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002312
Nate Begeman1d4d4142005-09-01 00:19:25 +00002313 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00002314 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00002315 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00002316
Chris Lattner541a24f2006-05-06 22:43:44 +00002317 // If the input is already sign extended, just drop the extension.
Chris Lattneree4ea922006-05-06 09:30:03 +00002318 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
2319 return N0;
2320
Nate Begeman646d7e22005-09-02 21:18:40 +00002321 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
2322 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2323 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00002324 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002325 }
Chris Lattner4b37e872006-05-08 21:18:59 +00002326
Nate Begeman07ed4172005-10-10 21:26:48 +00002327 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00002328 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00002329 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00002330
2331 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
2332 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
2333 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
2334 if (N0.getOpcode() == ISD::SRL) {
2335 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2336 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
2337 // We can turn this into an SRA iff the input to the SRL is already sign
2338 // extended enough.
2339 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
2340 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
2341 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
2342 }
2343 }
2344
Nate Begemanded49632005-10-13 03:11:28 +00002345 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00002346 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002347 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002348 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002349 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2350 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2351 LN0->getBasePtr(), LN0->getSrcValue(),
2352 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002353 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002354 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002355 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002356 }
2357 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Chengc5484282006-10-04 00:56:09 +00002358 if (ISD::isZEXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002359 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002360 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002361 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2362 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2363 LN0->getBasePtr(), LN0->getSrcValue(),
2364 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002365 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002366 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002367 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002368 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002369 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002370}
2371
Nate Begeman83e75ec2005-09-06 04:43:02 +00002372SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002373 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002374 MVT::ValueType VT = N->getValueType(0);
2375
2376 // noop truncate
2377 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002378 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002379 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002380 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002381 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002382 // fold (truncate (truncate x)) -> (truncate x)
2383 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002384 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002385 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00002386 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2387 N0.getOpcode() == ISD::ANY_EXTEND) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002388 if (N0.getValueType() < VT)
2389 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00002390 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002391 else if (N0.getValueType() > VT)
2392 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002393 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002394 else
2395 // if the source and dest are the same type, we can drop both the extend
2396 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002397 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002398 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002399 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng466685d2006-10-09 20:57:25 +00002400 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00002401 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
2402 "Cannot truncate to larger type!");
Evan Cheng466685d2006-10-09 20:57:25 +00002403 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Nate Begeman3df4d522005-10-12 20:40:40 +00002404 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00002405 // For big endian targets, we need to add an offset to the pointer to load
2406 // the correct bytes. For little endian systems, we merely need to read
2407 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00002408 uint64_t PtrOff =
2409 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Evan Cheng466685d2006-10-09 20:57:25 +00002410 SDOperand NewPtr = TLI.isLittleEndian() ? LN0->getBasePtr() :
2411 DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Nate Begeman765784a2005-10-12 23:18:53 +00002412 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00002413 AddToWorkList(NewPtr.Val);
Evan Cheng466685d2006-10-09 20:57:25 +00002414 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), NewPtr,
2415 LN0->getSrcValue(), LN0->getSrcValueOffset());
Chris Lattner5750df92006-03-01 04:03:14 +00002416 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00002417 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002418 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002419 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002420 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002421}
2422
Chris Lattner94683772005-12-23 05:30:37 +00002423SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2424 SDOperand N0 = N->getOperand(0);
2425 MVT::ValueType VT = N->getValueType(0);
2426
2427 // If the input is a constant, let getNode() fold it.
2428 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2429 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2430 if (Res.Val != N) return Res;
2431 }
2432
Chris Lattnerc8547d82005-12-23 05:37:50 +00002433 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2434 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00002435
Chris Lattner57104102005-12-23 05:44:41 +00002436 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002437 // FIXME: These xforms need to know that the resultant load doesn't need a
2438 // higher alignment than the original!
Evan Cheng466685d2006-10-09 20:57:25 +00002439 if (0 && ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse()) {
2440 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2441 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
2442 LN0->getSrcValue(), LN0->getSrcValueOffset());
Chris Lattner5750df92006-03-01 04:03:14 +00002443 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00002444 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2445 Load.getValue(1));
2446 return Load;
2447 }
2448
Chris Lattner94683772005-12-23 05:30:37 +00002449 return SDOperand();
2450}
2451
Chris Lattner6258fb22006-04-02 02:53:43 +00002452SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2453 SDOperand N0 = N->getOperand(0);
2454 MVT::ValueType VT = N->getValueType(0);
2455
2456 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2457 // First check to see if this is all constant.
2458 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2459 VT == MVT::Vector) {
2460 bool isSimple = true;
2461 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2462 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2463 N0.getOperand(i).getOpcode() != ISD::Constant &&
2464 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2465 isSimple = false;
2466 break;
2467 }
2468
Chris Lattner97c20732006-04-03 17:29:28 +00002469 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2470 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002471 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2472 }
2473 }
2474
2475 return SDOperand();
2476}
2477
2478/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2479/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2480/// destination element value type.
2481SDOperand DAGCombiner::
2482ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2483 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2484
2485 // If this is already the right type, we're done.
2486 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2487
2488 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2489 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2490
2491 // If this is a conversion of N elements of one type to N elements of another
2492 // type, convert each element. This handles FP<->INT cases.
2493 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002494 SmallVector<SDOperand, 8> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00002495 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002496 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002497 AddToWorkList(Ops.back().Val);
2498 }
Chris Lattner6258fb22006-04-02 02:53:43 +00002499 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2500 Ops.push_back(DAG.getValueType(DstEltVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002501 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002502 }
2503
2504 // Otherwise, we're growing or shrinking the elements. To avoid having to
2505 // handle annoying details of growing/shrinking FP values, we convert them to
2506 // int first.
2507 if (MVT::isFloatingPoint(SrcEltVT)) {
2508 // Convert the input float vector to a int vector where the elements are the
2509 // same sizes.
2510 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2511 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2512 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2513 SrcEltVT = IntVT;
2514 }
2515
2516 // Now we know the input is an integer vector. If the output is a FP type,
2517 // convert to integer first, then to FP of the right size.
2518 if (MVT::isFloatingPoint(DstEltVT)) {
2519 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2520 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2521 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2522
2523 // Next, convert to FP elements of the same size.
2524 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2525 }
2526
2527 // Okay, we know the src/dst types are both integers of differing types.
2528 // Handling growing first.
2529 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2530 if (SrcBitSize < DstBitSize) {
2531 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2532
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002533 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002534 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2535 i += NumInputsPerOutput) {
2536 bool isLE = TLI.isLittleEndian();
2537 uint64_t NewBits = 0;
2538 bool EltIsUndef = true;
2539 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2540 // Shift the previously computed bits over.
2541 NewBits <<= SrcBitSize;
2542 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2543 if (Op.getOpcode() == ISD::UNDEF) continue;
2544 EltIsUndef = false;
2545
2546 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2547 }
2548
2549 if (EltIsUndef)
2550 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2551 else
2552 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2553 }
2554
2555 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2556 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002557 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002558 }
2559
2560 // Finally, this must be the case where we are shrinking elements: each input
2561 // turns into multiple outputs.
2562 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002563 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002564 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2565 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2566 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2567 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2568 continue;
2569 }
2570 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2571
2572 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2573 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2574 OpVal >>= DstBitSize;
2575 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2576 }
2577
2578 // For big endian targets, swap the order of the pieces of each element.
2579 if (!TLI.isLittleEndian())
2580 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2581 }
2582 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2583 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002584 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002585}
2586
2587
2588
Chris Lattner01b3d732005-09-28 22:28:18 +00002589SDOperand DAGCombiner::visitFADD(SDNode *N) {
2590 SDOperand N0 = N->getOperand(0);
2591 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002592 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2593 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002594 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002595
2596 // fold (fadd c1, c2) -> c1+c2
2597 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002598 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002599 // canonicalize constant to RHS
2600 if (N0CFP && !N1CFP)
2601 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002602 // fold (A + (-B)) -> A-B
2603 if (N1.getOpcode() == ISD::FNEG)
2604 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002605 // fold ((-A) + B) -> B-A
2606 if (N0.getOpcode() == ISD::FNEG)
2607 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002608 return SDOperand();
2609}
2610
2611SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2612 SDOperand N0 = N->getOperand(0);
2613 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002614 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2615 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002616 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002617
2618 // fold (fsub c1, c2) -> c1-c2
2619 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002620 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002621 // fold (A-(-B)) -> A+B
2622 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00002623 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002624 return SDOperand();
2625}
2626
2627SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2628 SDOperand N0 = N->getOperand(0);
2629 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002630 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2631 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002632 MVT::ValueType VT = N->getValueType(0);
2633
Nate Begeman11af4ea2005-10-17 20:40:11 +00002634 // fold (fmul c1, c2) -> c1*c2
2635 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002636 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002637 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002638 if (N0CFP && !N1CFP)
2639 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002640 // fold (fmul X, 2.0) -> (fadd X, X)
2641 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2642 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002643 return SDOperand();
2644}
2645
2646SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2647 SDOperand N0 = N->getOperand(0);
2648 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002649 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2650 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002651 MVT::ValueType VT = N->getValueType(0);
2652
Nate Begemana148d982006-01-18 22:35:16 +00002653 // fold (fdiv c1, c2) -> c1/c2
2654 if (N0CFP && N1CFP)
2655 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002656 return SDOperand();
2657}
2658
2659SDOperand DAGCombiner::visitFREM(SDNode *N) {
2660 SDOperand N0 = N->getOperand(0);
2661 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002662 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2663 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002664 MVT::ValueType VT = N->getValueType(0);
2665
Nate Begemana148d982006-01-18 22:35:16 +00002666 // fold (frem c1, c2) -> fmod(c1,c2)
2667 if (N0CFP && N1CFP)
2668 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002669 return SDOperand();
2670}
2671
Chris Lattner12d83032006-03-05 05:30:57 +00002672SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2673 SDOperand N0 = N->getOperand(0);
2674 SDOperand N1 = N->getOperand(1);
2675 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2676 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2677 MVT::ValueType VT = N->getValueType(0);
2678
2679 if (N0CFP && N1CFP) // Constant fold
2680 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2681
2682 if (N1CFP) {
2683 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2684 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2685 union {
2686 double d;
2687 int64_t i;
2688 } u;
2689 u.d = N1CFP->getValue();
2690 if (u.i >= 0)
2691 return DAG.getNode(ISD::FABS, VT, N0);
2692 else
2693 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2694 }
2695
2696 // copysign(fabs(x), y) -> copysign(x, y)
2697 // copysign(fneg(x), y) -> copysign(x, y)
2698 // copysign(copysign(x,z), y) -> copysign(x, y)
2699 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2700 N0.getOpcode() == ISD::FCOPYSIGN)
2701 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2702
2703 // copysign(x, abs(y)) -> abs(x)
2704 if (N1.getOpcode() == ISD::FABS)
2705 return DAG.getNode(ISD::FABS, VT, N0);
2706
2707 // copysign(x, copysign(y,z)) -> copysign(x, z)
2708 if (N1.getOpcode() == ISD::FCOPYSIGN)
2709 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2710
2711 // copysign(x, fp_extend(y)) -> copysign(x, y)
2712 // copysign(x, fp_round(y)) -> copysign(x, y)
2713 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2714 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2715
2716 return SDOperand();
2717}
2718
2719
Chris Lattner01b3d732005-09-28 22:28:18 +00002720
Nate Begeman83e75ec2005-09-06 04:43:02 +00002721SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002722 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002723 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002724 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002725
2726 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002727 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002728 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002729 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002730}
2731
Nate Begeman83e75ec2005-09-06 04:43:02 +00002732SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002733 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002734 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002735 MVT::ValueType VT = N->getValueType(0);
2736
Nate Begeman1d4d4142005-09-01 00:19:25 +00002737 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002738 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002739 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002740 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002741}
2742
Nate Begeman83e75ec2005-09-06 04:43:02 +00002743SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002744 SDOperand N0 = N->getOperand(0);
2745 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2746 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002747
2748 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002749 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002750 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002751 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002752}
2753
Nate Begeman83e75ec2005-09-06 04:43:02 +00002754SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002755 SDOperand N0 = N->getOperand(0);
2756 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2757 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002758
2759 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002760 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002761 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002762 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002763}
2764
Nate Begeman83e75ec2005-09-06 04:43:02 +00002765SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002766 SDOperand N0 = N->getOperand(0);
2767 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2768 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002769
2770 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002771 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002772 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002773
2774 // fold (fp_round (fp_extend x)) -> x
2775 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2776 return N0.getOperand(0);
2777
2778 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2779 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2780 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2781 AddToWorkList(Tmp.Val);
2782 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2783 }
2784
Nate Begeman83e75ec2005-09-06 04:43:02 +00002785 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002786}
2787
Nate Begeman83e75ec2005-09-06 04:43:02 +00002788SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002789 SDOperand N0 = N->getOperand(0);
2790 MVT::ValueType VT = N->getValueType(0);
2791 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002792 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002793
Nate Begeman1d4d4142005-09-01 00:19:25 +00002794 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002795 if (N0CFP) {
2796 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002797 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002798 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002799 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002800}
2801
Nate Begeman83e75ec2005-09-06 04:43:02 +00002802SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002803 SDOperand N0 = N->getOperand(0);
2804 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2805 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002806
2807 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002808 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002809 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00002810
2811 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002812 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002813 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002814 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2815 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2816 LN0->getBasePtr(), LN0->getSrcValue(),
2817 LN0->getSrcValueOffset(),
Chris Lattnere564dbb2006-05-05 21:34:35 +00002818 N0.getValueType());
2819 CombineTo(N, ExtLoad);
2820 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
2821 ExtLoad.getValue(1));
2822 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2823 }
2824
2825
Nate Begeman83e75ec2005-09-06 04:43:02 +00002826 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002827}
2828
Nate Begeman83e75ec2005-09-06 04:43:02 +00002829SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002830 SDOperand N0 = N->getOperand(0);
2831 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2832 MVT::ValueType VT = N->getValueType(0);
2833
2834 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002835 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002836 return DAG.getNode(ISD::FNEG, VT, N0);
2837 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002838 if (N0.getOpcode() == ISD::SUB)
2839 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002840 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002841 if (N0.getOpcode() == ISD::FNEG)
2842 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002843 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002844}
2845
Nate Begeman83e75ec2005-09-06 04:43:02 +00002846SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002847 SDOperand N0 = N->getOperand(0);
2848 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2849 MVT::ValueType VT = N->getValueType(0);
2850
Nate Begeman1d4d4142005-09-01 00:19:25 +00002851 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002852 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002853 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002854 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002855 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002856 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002857 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002858 // fold (fabs (fcopysign x, y)) -> (fabs x)
2859 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2860 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2861
Nate Begeman83e75ec2005-09-06 04:43:02 +00002862 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002863}
2864
Nate Begeman44728a72005-09-19 22:34:01 +00002865SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2866 SDOperand Chain = N->getOperand(0);
2867 SDOperand N1 = N->getOperand(1);
2868 SDOperand N2 = N->getOperand(2);
2869 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2870
2871 // never taken branch, fold to chain
2872 if (N1C && N1C->isNullValue())
2873 return Chain;
2874 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002875 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002876 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002877 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2878 // on the target.
2879 if (N1.getOpcode() == ISD::SETCC &&
2880 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2881 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2882 N1.getOperand(0), N1.getOperand(1), N2);
2883 }
Nate Begeman44728a72005-09-19 22:34:01 +00002884 return SDOperand();
2885}
2886
Chris Lattner3ea0b472005-10-05 06:47:48 +00002887// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2888//
Nate Begeman44728a72005-09-19 22:34:01 +00002889SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002890 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2891 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2892
2893 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002894 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002895 if (Simp.Val) AddToWorkList(Simp.Val);
2896
Nate Begemane17daeb2005-10-05 21:43:42 +00002897 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2898
2899 // fold br_cc true, dest -> br dest (unconditional branch)
2900 if (SCCC && SCCC->getValue())
2901 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2902 N->getOperand(4));
2903 // fold br_cc false, dest -> unconditional fall through
2904 if (SCCC && SCCC->isNullValue())
2905 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00002906
Nate Begemane17daeb2005-10-05 21:43:42 +00002907 // fold to a simpler setcc
2908 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2909 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2910 Simp.getOperand(2), Simp.getOperand(0),
2911 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002912 return SDOperand();
2913}
2914
Chris Lattner01a22022005-10-10 22:04:48 +00002915SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00002916 LoadSDNode *LD = cast<LoadSDNode>(N);
2917 SDOperand Chain = LD->getChain();
2918 SDOperand Ptr = LD->getBasePtr();
Jim Laskey6ff23e52006-10-04 16:53:27 +00002919
Chris Lattnere4b95392006-03-31 18:06:18 +00002920 // If there are no uses of the loaded value, change uses of the chain value
2921 // into uses of the chain input (i.e. delete the dead load).
2922 if (N->hasNUsesOfValue(0, 0))
2923 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
Chris Lattner01a22022005-10-10 22:04:48 +00002924
2925 // If this load is directly stored, replace the load value with the stored
2926 // value.
2927 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002928 // TODO: Handle TRUNCSTORE/LOADEXT
2929 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002930 if (ISD::isNON_TRUNCStore(Chain.Val)) {
2931 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
2932 if (PrevST->getBasePtr() == Ptr &&
2933 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002934 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002935 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002936 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00002937
Jim Laskey7ca56af2006-10-11 13:47:09 +00002938 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00002939 // Walk up chain skipping non-aliasing memory nodes.
2940 SDOperand BetterChain = FindBetterChain(N, Chain);
2941
Jim Laskey6ff23e52006-10-04 16:53:27 +00002942 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00002943 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002944 SDOperand ReplLoad;
2945
Jim Laskey279f0532006-09-25 16:29:54 +00002946 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002947 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
2948 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
2949 LD->getSrcValue(), LD->getSrcValueOffset());
2950 } else {
2951 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
2952 LD->getValueType(0),
2953 BetterChain, Ptr, LD->getSrcValue(),
2954 LD->getSrcValueOffset(),
2955 LD->getLoadedVT());
2956 }
Jim Laskey279f0532006-09-25 16:29:54 +00002957
Jim Laskey6ff23e52006-10-04 16:53:27 +00002958 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00002959 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
2960 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00002961
Jim Laskey274062c2006-10-13 23:32:28 +00002962 // Replace uses with load result and token factor. Don't add users
2963 // to work list.
2964 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00002965 }
2966 }
2967
Evan Cheng7fc033a2006-11-03 03:06:21 +00002968 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00002969 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00002970 return SDOperand(N, 0);
2971
Chris Lattner01a22022005-10-10 22:04:48 +00002972 return SDOperand();
2973}
2974
Chris Lattner87514ca2005-10-10 22:31:19 +00002975SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002976 StoreSDNode *ST = cast<StoreSDNode>(N);
2977 SDOperand Chain = ST->getChain();
2978 SDOperand Value = ST->getValue();
2979 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00002980
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002981 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002982 // FIXME: This needs to know that the resultant store does not need a
2983 // higher alignment than the original.
Jim Laskey14fbcbf2006-09-25 21:11:32 +00002984 if (0 && Value.getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002985 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
2986 ST->getSrcValueOffset());
Jim Laskey279f0532006-09-25 16:29:54 +00002987 }
2988
2989 if (CombinerAA) {
2990 // Walk up chain skipping non-aliasing memory nodes.
2991 SDOperand BetterChain = FindBetterChain(N, Chain);
2992
Jim Laskey6ff23e52006-10-04 16:53:27 +00002993 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00002994 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00002995 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00002996 SDOperand ReplStore;
2997 if (ST->isTruncatingStore()) {
2998 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
2999 ST->getSrcValue(),ST->getSrcValueOffset(), ST->getStoredVT());
3000 } else {
3001 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
3002 ST->getSrcValue(), ST->getSrcValueOffset());
3003 }
3004
Jim Laskey279f0532006-09-25 16:29:54 +00003005 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00003006 SDOperand Token =
3007 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
3008
3009 // Don't add users to work list.
3010 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003011 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00003012 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00003013
Evan Cheng33dbedc2006-11-05 09:31:14 +00003014 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003015 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00003016 return SDOperand(N, 0);
3017
Chris Lattner87514ca2005-10-10 22:31:19 +00003018 return SDOperand();
3019}
3020
Chris Lattnerca242442006-03-19 01:27:56 +00003021SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
3022 SDOperand InVec = N->getOperand(0);
3023 SDOperand InVal = N->getOperand(1);
3024 SDOperand EltNo = N->getOperand(2);
3025
3026 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
3027 // vector with the inserted element.
3028 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
3029 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003030 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00003031 if (Elt < Ops.size())
3032 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003033 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
3034 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00003035 }
3036
3037 return SDOperand();
3038}
3039
3040SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
3041 SDOperand InVec = N->getOperand(0);
3042 SDOperand InVal = N->getOperand(1);
3043 SDOperand EltNo = N->getOperand(2);
3044 SDOperand NumElts = N->getOperand(3);
3045 SDOperand EltType = N->getOperand(4);
3046
3047 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
3048 // vector with the inserted element.
3049 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
3050 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003051 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00003052 if (Elt < Ops.size()-2)
3053 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003054 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
3055 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00003056 }
3057
3058 return SDOperand();
3059}
3060
Chris Lattnerd7648c82006-03-28 20:28:38 +00003061SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
3062 unsigned NumInScalars = N->getNumOperands()-2;
3063 SDOperand NumElts = N->getOperand(NumInScalars);
3064 SDOperand EltType = N->getOperand(NumInScalars+1);
3065
3066 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
3067 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
3068 // two distinct vectors, turn this into a shuffle node.
3069 SDOperand VecIn1, VecIn2;
3070 for (unsigned i = 0; i != NumInScalars; ++i) {
3071 // Ignore undef inputs.
3072 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3073
3074 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
3075 // constant index, bail out.
3076 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
3077 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
3078 VecIn1 = VecIn2 = SDOperand(0, 0);
3079 break;
3080 }
3081
3082 // If the input vector type disagrees with the result of the vbuild_vector,
3083 // we can't make a shuffle.
3084 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
3085 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
3086 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
3087 VecIn1 = VecIn2 = SDOperand(0, 0);
3088 break;
3089 }
3090
3091 // Otherwise, remember this. We allow up to two distinct input vectors.
3092 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
3093 continue;
3094
3095 if (VecIn1.Val == 0) {
3096 VecIn1 = ExtractedFromVec;
3097 } else if (VecIn2.Val == 0) {
3098 VecIn2 = ExtractedFromVec;
3099 } else {
3100 // Too many inputs.
3101 VecIn1 = VecIn2 = SDOperand(0, 0);
3102 break;
3103 }
3104 }
3105
3106 // If everything is good, we can make a shuffle operation.
3107 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003108 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00003109 for (unsigned i = 0; i != NumInScalars; ++i) {
3110 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
3111 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3112 continue;
3113 }
3114
3115 SDOperand Extract = N->getOperand(i);
3116
3117 // If extracting from the first vector, just use the index directly.
3118 if (Extract.getOperand(0) == VecIn1) {
3119 BuildVecIndices.push_back(Extract.getOperand(1));
3120 continue;
3121 }
3122
3123 // Otherwise, use InIdx + VecSize
3124 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
3125 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
3126 }
3127
3128 // Add count and size info.
3129 BuildVecIndices.push_back(NumElts);
3130 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
3131
3132 // Return the new VVECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003133 SDOperand Ops[5];
3134 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00003135 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003136 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00003137 } else {
3138 // Use an undef vbuild_vector as input for the second operand.
3139 std::vector<SDOperand> UnOps(NumInScalars,
3140 DAG.getNode(ISD::UNDEF,
3141 cast<VTSDNode>(EltType)->getVT()));
3142 UnOps.push_back(NumElts);
3143 UnOps.push_back(EltType);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003144 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3145 &UnOps[0], UnOps.size());
3146 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00003147 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003148 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3149 &BuildVecIndices[0], BuildVecIndices.size());
3150 Ops[3] = NumElts;
3151 Ops[4] = EltType;
3152 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
Chris Lattnerd7648c82006-03-28 20:28:38 +00003153 }
3154
3155 return SDOperand();
3156}
3157
Chris Lattner66445d32006-03-28 22:11:53 +00003158SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003159 SDOperand ShufMask = N->getOperand(2);
3160 unsigned NumElts = ShufMask.getNumOperands();
3161
3162 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3163 bool isIdentity = true;
3164 for (unsigned i = 0; i != NumElts; ++i) {
3165 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3166 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3167 isIdentity = false;
3168 break;
3169 }
3170 }
3171 if (isIdentity) return N->getOperand(0);
3172
3173 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3174 isIdentity = true;
3175 for (unsigned i = 0; i != NumElts; ++i) {
3176 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3177 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3178 isIdentity = false;
3179 break;
3180 }
3181 }
3182 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00003183
3184 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3185 // needed at all.
3186 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003187 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003188 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003189 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003190 for (unsigned i = 0; i != NumElts; ++i)
3191 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3192 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3193 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003194 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003195 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003196 BaseIdx = Idx;
3197 } else {
3198 if (BaseIdx != Idx)
3199 isSplat = false;
3200 if (VecNum != V) {
3201 isUnary = false;
3202 break;
3203 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003204 }
3205 }
3206
3207 SDOperand N0 = N->getOperand(0);
3208 SDOperand N1 = N->getOperand(1);
3209 // Normalize unary shuffle so the RHS is undef.
3210 if (isUnary && VecNum == 1)
3211 std::swap(N0, N1);
3212
Evan Cheng917ec982006-07-21 08:25:53 +00003213 // If it is a splat, check if the argument vector is a build_vector with
3214 // all scalar elements the same.
3215 if (isSplat) {
3216 SDNode *V = N0.Val;
3217 if (V->getOpcode() == ISD::BIT_CONVERT)
3218 V = V->getOperand(0).Val;
3219 if (V->getOpcode() == ISD::BUILD_VECTOR) {
3220 unsigned NumElems = V->getNumOperands()-2;
3221 if (NumElems > BaseIdx) {
3222 SDOperand Base;
3223 bool AllSame = true;
3224 for (unsigned i = 0; i != NumElems; ++i) {
3225 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3226 Base = V->getOperand(i);
3227 break;
3228 }
3229 }
3230 // Splat of <u, u, u, u>, return <u, u, u, u>
3231 if (!Base.Val)
3232 return N0;
3233 for (unsigned i = 0; i != NumElems; ++i) {
3234 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3235 V->getOperand(i) != Base) {
3236 AllSame = false;
3237 break;
3238 }
3239 }
3240 // Splat of <x, x, x, x>, return <x, x, x, x>
3241 if (AllSame)
3242 return N0;
3243 }
3244 }
3245 }
3246
Evan Chenge7bec0d2006-07-20 22:44:41 +00003247 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3248 // into an undef.
3249 if (isUnary || N0 == N1) {
3250 if (N0.getOpcode() == ISD::UNDEF)
Evan Chengc04766a2006-04-06 23:20:43 +00003251 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00003252 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3253 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003254 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00003255 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00003256 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3257 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3258 MappedOps.push_back(ShufMask.getOperand(i));
3259 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00003260 unsigned NewIdx =
3261 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3262 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00003263 }
3264 }
3265 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003266 &MappedOps[0], MappedOps.size());
Chris Lattner3e104b12006-04-08 04:15:24 +00003267 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00003268 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
Evan Chenge7bec0d2006-07-20 22:44:41 +00003269 N0,
Chris Lattner66445d32006-03-28 22:11:53 +00003270 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
3271 ShufMask);
3272 }
3273
3274 return SDOperand();
3275}
3276
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003277SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
3278 SDOperand ShufMask = N->getOperand(2);
3279 unsigned NumElts = ShufMask.getNumOperands()-2;
3280
3281 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3282 bool isIdentity = true;
3283 for (unsigned i = 0; i != NumElts; ++i) {
3284 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3285 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3286 isIdentity = false;
3287 break;
3288 }
3289 }
3290 if (isIdentity) return N->getOperand(0);
3291
3292 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3293 isIdentity = true;
3294 for (unsigned i = 0; i != NumElts; ++i) {
3295 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3296 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3297 isIdentity = false;
3298 break;
3299 }
3300 }
3301 if (isIdentity) return N->getOperand(1);
3302
Evan Chenge7bec0d2006-07-20 22:44:41 +00003303 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3304 // needed at all.
3305 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003306 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003307 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003308 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003309 for (unsigned i = 0; i != NumElts; ++i)
3310 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3311 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3312 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003313 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003314 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003315 BaseIdx = Idx;
3316 } else {
3317 if (BaseIdx != Idx)
3318 isSplat = false;
3319 if (VecNum != V) {
3320 isUnary = false;
3321 break;
3322 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003323 }
3324 }
3325
3326 SDOperand N0 = N->getOperand(0);
3327 SDOperand N1 = N->getOperand(1);
3328 // Normalize unary shuffle so the RHS is undef.
3329 if (isUnary && VecNum == 1)
3330 std::swap(N0, N1);
3331
Evan Cheng917ec982006-07-21 08:25:53 +00003332 // If it is a splat, check if the argument vector is a build_vector with
3333 // all scalar elements the same.
3334 if (isSplat) {
3335 SDNode *V = N0.Val;
Evan Cheng59569222006-10-16 22:49:37 +00003336
3337 // If this is a vbit convert that changes the element type of the vector but
3338 // not the number of vector elements, look through it. Be careful not to
3339 // look though conversions that change things like v4f32 to v2f64.
3340 if (V->getOpcode() == ISD::VBIT_CONVERT) {
3341 SDOperand ConvInput = V->getOperand(0);
Evan Cheng5d04a1a2006-10-17 17:06:35 +00003342 if (ConvInput.getValueType() == MVT::Vector &&
3343 NumElts ==
Evan Cheng59569222006-10-16 22:49:37 +00003344 ConvInput.getConstantOperandVal(ConvInput.getNumOperands()-2))
3345 V = ConvInput.Val;
3346 }
3347
Evan Cheng917ec982006-07-21 08:25:53 +00003348 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
3349 unsigned NumElems = V->getNumOperands()-2;
3350 if (NumElems > BaseIdx) {
3351 SDOperand Base;
3352 bool AllSame = true;
3353 for (unsigned i = 0; i != NumElems; ++i) {
3354 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3355 Base = V->getOperand(i);
3356 break;
3357 }
3358 }
3359 // Splat of <u, u, u, u>, return <u, u, u, u>
3360 if (!Base.Val)
3361 return N0;
3362 for (unsigned i = 0; i != NumElems; ++i) {
3363 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3364 V->getOperand(i) != Base) {
3365 AllSame = false;
3366 break;
3367 }
3368 }
3369 // Splat of <x, x, x, x>, return <x, x, x, x>
3370 if (AllSame)
3371 return N0;
3372 }
3373 }
3374 }
3375
Evan Chenge7bec0d2006-07-20 22:44:41 +00003376 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3377 // into an undef.
3378 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00003379 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3380 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003381 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00003382 for (unsigned i = 0; i != NumElts; ++i) {
3383 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3384 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3385 MappedOps.push_back(ShufMask.getOperand(i));
3386 } else {
3387 unsigned NewIdx =
3388 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3389 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
3390 }
3391 }
3392 // Add the type/#elts values.
3393 MappedOps.push_back(ShufMask.getOperand(NumElts));
3394 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
3395
3396 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003397 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003398 AddToWorkList(ShufMask.Val);
3399
3400 // Build the undef vector.
3401 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
3402 for (unsigned i = 0; i != NumElts; ++i)
3403 MappedOps[i] = UDVal;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003404 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
3405 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003406 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3407 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003408
3409 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
Evan Chenge7bec0d2006-07-20 22:44:41 +00003410 N0, UDVal, ShufMask,
Chris Lattner17614ea2006-04-08 05:34:25 +00003411 MappedOps[NumElts], MappedOps[NumElts+1]);
3412 }
3413
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003414 return SDOperand();
3415}
3416
Evan Cheng44f1f092006-04-20 08:56:16 +00003417/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
3418/// a VAND to a vector_shuffle with the destination vector and a zero vector.
3419/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
3420/// vector_shuffle V, Zero, <0, 4, 2, 4>
3421SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
3422 SDOperand LHS = N->getOperand(0);
3423 SDOperand RHS = N->getOperand(1);
3424 if (N->getOpcode() == ISD::VAND) {
3425 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
3426 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
3427 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
3428 RHS = RHS.getOperand(0);
3429 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
3430 std::vector<SDOperand> IdxOps;
3431 unsigned NumOps = RHS.getNumOperands();
3432 unsigned NumElts = NumOps-2;
3433 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
3434 for (unsigned i = 0; i != NumElts; ++i) {
3435 SDOperand Elt = RHS.getOperand(i);
3436 if (!isa<ConstantSDNode>(Elt))
3437 return SDOperand();
3438 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
3439 IdxOps.push_back(DAG.getConstant(i, EVT));
3440 else if (cast<ConstantSDNode>(Elt)->isNullValue())
3441 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
3442 else
3443 return SDOperand();
3444 }
3445
3446 // Let's see if the target supports this vector_shuffle.
3447 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
3448 return SDOperand();
3449
3450 // Return the new VVECTOR_SHUFFLE node.
3451 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
3452 SDOperand EVTNode = DAG.getValueType(EVT);
3453 std::vector<SDOperand> Ops;
Chris Lattner516b9622006-09-14 20:50:57 +00003454 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode,
3455 EVTNode);
Evan Cheng44f1f092006-04-20 08:56:16 +00003456 Ops.push_back(LHS);
3457 AddToWorkList(LHS.Val);
3458 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
3459 ZeroOps.push_back(NumEltsNode);
3460 ZeroOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003461 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3462 &ZeroOps[0], ZeroOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003463 IdxOps.push_back(NumEltsNode);
3464 IdxOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003465 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3466 &IdxOps[0], IdxOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003467 Ops.push_back(NumEltsNode);
3468 Ops.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003469 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
3470 &Ops[0], Ops.size());
Evan Cheng44f1f092006-04-20 08:56:16 +00003471 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
3472 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
3473 DstVecSize, DstVecEVT);
3474 }
3475 return Result;
3476 }
3477 }
3478 return SDOperand();
3479}
3480
Chris Lattneredab1b92006-04-02 03:25:57 +00003481/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
3482/// the scalar operation of the vop if it is operating on an integer vector
3483/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
3484SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
3485 ISD::NodeType FPOp) {
3486 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
3487 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
3488 SDOperand LHS = N->getOperand(0);
3489 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00003490 SDOperand Shuffle = XformToShuffleWithZero(N);
3491 if (Shuffle.Val) return Shuffle;
3492
Chris Lattneredab1b92006-04-02 03:25:57 +00003493 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
3494 // this operation.
3495 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
3496 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003497 SmallVector<SDOperand, 8> Ops;
Chris Lattneredab1b92006-04-02 03:25:57 +00003498 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
3499 SDOperand LHSOp = LHS.getOperand(i);
3500 SDOperand RHSOp = RHS.getOperand(i);
3501 // If these two elements can't be folded, bail out.
3502 if ((LHSOp.getOpcode() != ISD::UNDEF &&
3503 LHSOp.getOpcode() != ISD::Constant &&
3504 LHSOp.getOpcode() != ISD::ConstantFP) ||
3505 (RHSOp.getOpcode() != ISD::UNDEF &&
3506 RHSOp.getOpcode() != ISD::Constant &&
3507 RHSOp.getOpcode() != ISD::ConstantFP))
3508 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00003509 // Can't fold divide by zero.
3510 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
3511 if ((RHSOp.getOpcode() == ISD::Constant &&
3512 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
3513 (RHSOp.getOpcode() == ISD::ConstantFP &&
3514 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
3515 break;
3516 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003517 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00003518 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00003519 assert((Ops.back().getOpcode() == ISD::UNDEF ||
3520 Ops.back().getOpcode() == ISD::Constant ||
3521 Ops.back().getOpcode() == ISD::ConstantFP) &&
3522 "Scalar binop didn't fold!");
3523 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003524
3525 if (Ops.size() == LHS.getNumOperands()-2) {
3526 Ops.push_back(*(LHS.Val->op_end()-2));
3527 Ops.push_back(*(LHS.Val->op_end()-1));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003528 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003529 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003530 }
3531
3532 return SDOperand();
3533}
3534
Nate Begeman44728a72005-09-19 22:34:01 +00003535SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00003536 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
3537
3538 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
3539 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3540 // If we got a simplified select_cc node back from SimplifySelectCC, then
3541 // break it down into a new SETCC node, and a new SELECT node, and then return
3542 // the SELECT node, since we were called with a SELECT node.
3543 if (SCC.Val) {
3544 // Check to see if we got a select_cc back (to turn into setcc/select).
3545 // Otherwise, just return whatever node we got back, like fabs.
3546 if (SCC.getOpcode() == ISD::SELECT_CC) {
3547 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
3548 SCC.getOperand(0), SCC.getOperand(1),
3549 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00003550 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003551 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
3552 SCC.getOperand(3), SETCC);
3553 }
3554 return SCC;
3555 }
Nate Begeman44728a72005-09-19 22:34:01 +00003556 return SDOperand();
3557}
3558
Chris Lattner40c62d52005-10-18 06:04:22 +00003559/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
3560/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00003561/// select. Callers of this should assume that TheSelect is deleted if this
3562/// returns true. As such, they should return the appropriate thing (e.g. the
3563/// node) back to the top-level of the DAG combiner loop to avoid it being
3564/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00003565///
3566bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
3567 SDOperand RHS) {
3568
3569 // If this is a select from two identical things, try to pull the operation
3570 // through the select.
3571 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00003572 // If this is a load and the token chain is identical, replace the select
3573 // of two loads with a load through a select of the address to load from.
3574 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
3575 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00003576 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00003577 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00003578 LHS.getOperand(0) == RHS.getOperand(0)) {
3579 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
3580 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
3581
3582 // If this is an EXTLOAD, the VT's must match.
Evan Cheng2e49f092006-10-11 07:10:22 +00003583 if (LLD->getLoadedVT() == RLD->getLoadedVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003584 // FIXME: this conflates two src values, discarding one. This is not
3585 // the right thing to do, but nothing uses srcvalues now. When they do,
3586 // turn SrcValue into a list of locations.
3587 SDOperand Addr;
3588 if (TheSelect->getOpcode() == ISD::SELECT)
3589 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
3590 TheSelect->getOperand(0), LLD->getBasePtr(),
3591 RLD->getBasePtr());
3592 else
3593 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
3594 TheSelect->getOperand(0),
3595 TheSelect->getOperand(1),
3596 LLD->getBasePtr(), RLD->getBasePtr(),
3597 TheSelect->getOperand(4));
Chris Lattner40c62d52005-10-18 06:04:22 +00003598
Evan Cheng466685d2006-10-09 20:57:25 +00003599 SDOperand Load;
3600 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
3601 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
3602 Addr,LLD->getSrcValue(), LLD->getSrcValueOffset());
3603 else {
3604 Load = DAG.getExtLoad(LLD->getExtensionType(),
3605 TheSelect->getValueType(0),
3606 LLD->getChain(), Addr, LLD->getSrcValue(),
3607 LLD->getSrcValueOffset(),
Evan Cheng2e49f092006-10-11 07:10:22 +00003608 LLD->getLoadedVT());
Evan Cheng466685d2006-10-09 20:57:25 +00003609 }
3610 // Users of the select now use the result of the load.
3611 CombineTo(TheSelect, Load);
3612
3613 // Users of the old loads now use the new load's chain. We know the
3614 // old-load value is dead now.
3615 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
3616 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
3617 return true;
Evan Chengc5484282006-10-04 00:56:09 +00003618 }
Chris Lattner40c62d52005-10-18 06:04:22 +00003619 }
3620 }
3621
3622 return false;
3623}
3624
Nate Begeman44728a72005-09-19 22:34:01 +00003625SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
3626 SDOperand N2, SDOperand N3,
3627 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00003628
3629 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00003630 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
3631 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
3632 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
3633
3634 // Determine if the condition we're dealing with is constant
3635 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003636 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003637 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
3638
3639 // fold select_cc true, x, y -> x
3640 if (SCCC && SCCC->getValue())
3641 return N2;
3642 // fold select_cc false, x, y -> y
3643 if (SCCC && SCCC->getValue() == 0)
3644 return N3;
3645
3646 // Check to see if we can simplify the select into an fabs node
3647 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
3648 // Allow either -0.0 or 0.0
3649 if (CFP->getValue() == 0.0) {
3650 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
3651 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
3652 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
3653 N2 == N3.getOperand(0))
3654 return DAG.getNode(ISD::FABS, VT, N0);
3655
3656 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
3657 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3658 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3659 N2.getOperand(0) == N3)
3660 return DAG.getNode(ISD::FABS, VT, N3);
3661 }
3662 }
3663
3664 // Check to see if we can perform the "gzip trick", transforming
3665 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00003666 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00003667 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00003668 MVT::isInteger(N2.getValueType()) &&
3669 (N1C->isNullValue() || // (a < 0) ? b : 0
3670 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00003671 MVT::ValueType XType = N0.getValueType();
3672 MVT::ValueType AType = N2.getValueType();
3673 if (XType >= AType) {
3674 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00003675 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00003676 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3677 unsigned ShCtV = Log2_64(N2C->getValue());
3678 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3679 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3680 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00003681 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003682 if (XType > AType) {
3683 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003684 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003685 }
3686 return DAG.getNode(ISD::AND, AType, Shift, N2);
3687 }
3688 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3689 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3690 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003691 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003692 if (XType > AType) {
3693 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003694 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003695 }
3696 return DAG.getNode(ISD::AND, AType, Shift, N2);
3697 }
3698 }
Nate Begeman07ed4172005-10-10 21:26:48 +00003699
3700 // fold select C, 16, 0 -> shl C, 4
3701 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3702 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3703 // Get a SetCC of the condition
3704 // FIXME: Should probably make sure that setcc is legal if we ever have a
3705 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00003706 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00003707 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00003708 if (AfterLegalize) {
3709 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003710 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00003711 } else {
3712 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003713 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00003714 }
Chris Lattner5750df92006-03-01 04:03:14 +00003715 AddToWorkList(SCC.Val);
3716 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00003717 // shl setcc result by log2 n2c
3718 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3719 DAG.getConstant(Log2_64(N2C->getValue()),
3720 TLI.getShiftAmountTy()));
3721 }
3722
Nate Begemanf845b452005-10-08 00:29:44 +00003723 // Check to see if this is the equivalent of setcc
3724 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3725 // otherwise, go ahead with the folds.
3726 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3727 MVT::ValueType XType = N0.getValueType();
3728 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3729 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3730 if (Res.getValueType() != VT)
3731 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3732 return Res;
3733 }
3734
3735 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3736 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3737 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3738 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3739 return DAG.getNode(ISD::SRL, XType, Ctlz,
3740 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3741 TLI.getShiftAmountTy()));
3742 }
3743 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3744 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3745 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3746 N0);
3747 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3748 DAG.getConstant(~0ULL, XType));
3749 return DAG.getNode(ISD::SRL, XType,
3750 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3751 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3752 TLI.getShiftAmountTy()));
3753 }
3754 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3755 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3756 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3757 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3758 TLI.getShiftAmountTy()));
3759 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3760 }
3761 }
3762
3763 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3764 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3765 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3766 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3767 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3768 MVT::ValueType XType = N0.getValueType();
3769 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3770 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3771 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3772 TLI.getShiftAmountTy()));
3773 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003774 AddToWorkList(Shift.Val);
3775 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003776 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3777 }
3778 }
3779 }
3780
Nate Begeman44728a72005-09-19 22:34:01 +00003781 return SDOperand();
3782}
3783
Nate Begeman452d7be2005-09-16 00:54:12 +00003784SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00003785 SDOperand N1, ISD::CondCode Cond,
3786 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003787 // These setcc operations always fold.
3788 switch (Cond) {
3789 default: break;
3790 case ISD::SETFALSE:
3791 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3792 case ISD::SETTRUE:
3793 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3794 }
3795
3796 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3797 uint64_t C1 = N1C->getValue();
Reid Spencer3ed469c2006-11-02 20:25:50 +00003798 if (isa<ConstantSDNode>(N0.Val)) {
Chris Lattner51dabfb2006-10-14 00:41:01 +00003799 return DAG.FoldSetCC(VT, N0, N1, Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00003800 } else {
Chris Lattner5f42a242006-09-20 06:19:26 +00003801 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
3802 // equality comparison, then we're just comparing whether X itself is
3803 // zero.
3804 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
3805 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
3806 N0.getOperand(1).getOpcode() == ISD::Constant) {
3807 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
3808 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3809 ShAmt == Log2_32(MVT::getSizeInBits(N0.getValueType()))) {
3810 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
3811 // (srl (ctlz x), 5) == 0 -> X != 0
3812 // (srl (ctlz x), 5) != 1 -> X != 0
3813 Cond = ISD::SETNE;
3814 } else {
3815 // (srl (ctlz x), 5) != 0 -> X == 0
3816 // (srl (ctlz x), 5) == 1 -> X == 0
3817 Cond = ISD::SETEQ;
3818 }
3819 SDOperand Zero = DAG.getConstant(0, N0.getValueType());
3820 return DAG.getSetCC(VT, N0.getOperand(0).getOperand(0),
3821 Zero, Cond);
3822 }
3823 }
3824
Nate Begeman452d7be2005-09-16 00:54:12 +00003825 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3826 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3827 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3828
3829 // If the comparison constant has bits in the upper part, the
3830 // zero-extended value could never match.
3831 if (C1 & (~0ULL << InSize)) {
3832 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3833 switch (Cond) {
3834 case ISD::SETUGT:
3835 case ISD::SETUGE:
3836 case ISD::SETEQ: return DAG.getConstant(0, VT);
3837 case ISD::SETULT:
3838 case ISD::SETULE:
3839 case ISD::SETNE: return DAG.getConstant(1, VT);
3840 case ISD::SETGT:
3841 case ISD::SETGE:
3842 // True if the sign bit of C1 is set.
3843 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3844 case ISD::SETLT:
3845 case ISD::SETLE:
3846 // True if the sign bit of C1 isn't set.
3847 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3848 default:
3849 break;
3850 }
3851 }
3852
3853 // Otherwise, we can perform the comparison with the low bits.
3854 switch (Cond) {
3855 case ISD::SETEQ:
3856 case ISD::SETNE:
3857 case ISD::SETUGT:
3858 case ISD::SETUGE:
3859 case ISD::SETULT:
3860 case ISD::SETULE:
3861 return DAG.getSetCC(VT, N0.getOperand(0),
3862 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3863 Cond);
3864 default:
3865 break; // todo, be more careful with signed comparisons
3866 }
3867 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3868 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3869 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3870 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3871 MVT::ValueType ExtDstTy = N0.getValueType();
3872 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3873
3874 // If the extended part has any inconsistent bits, it cannot ever
3875 // compare equal. In other words, they have to be all ones or all
3876 // zeros.
3877 uint64_t ExtBits =
3878 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3879 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3880 return DAG.getConstant(Cond == ISD::SETNE, VT);
3881
3882 SDOperand ZextOp;
3883 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3884 if (Op0Ty == ExtSrcTy) {
3885 ZextOp = N0.getOperand(0);
3886 } else {
3887 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3888 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3889 DAG.getConstant(Imm, Op0Ty));
3890 }
Chris Lattner5750df92006-03-01 04:03:14 +00003891 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003892 // Otherwise, make this a use of a zext.
3893 return DAG.getSetCC(VT, ZextOp,
3894 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3895 ExtDstTy),
3896 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00003897 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
Chris Lattner8ac9d0e2006-10-14 01:02:29 +00003898 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3899
3900 // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC
3901 if (N0.getOpcode() == ISD::SETCC) {
3902 bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getValue() != 1);
3903 if (TrueWhenTrue)
3904 return N0;
3905
3906 // Invert the condition.
3907 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
3908 CC = ISD::getSetCCInverse(CC,
3909 MVT::isInteger(N0.getOperand(0).getValueType()));
3910 return DAG.getSetCC(VT, N0.getOperand(0), N0.getOperand(1), CC);
3911 }
3912
3913 if ((N0.getOpcode() == ISD::XOR ||
3914 (N0.getOpcode() == ISD::AND &&
3915 N0.getOperand(0).getOpcode() == ISD::XOR &&
3916 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3917 isa<ConstantSDNode>(N0.getOperand(1)) &&
3918 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
3919 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We
3920 // can only do this if the top bits are known zero.
Chris Lattner50662be2006-10-17 21:24:15 +00003921 if (TLI.MaskedValueIsZero(N0,
Chris Lattner8ac9d0e2006-10-14 01:02:29 +00003922 MVT::getIntVTBitMask(N0.getValueType())-1)){
3923 // Okay, get the un-inverted input value.
3924 SDOperand Val;
3925 if (N0.getOpcode() == ISD::XOR)
3926 Val = N0.getOperand(0);
3927 else {
3928 assert(N0.getOpcode() == ISD::AND &&
3929 N0.getOperand(0).getOpcode() == ISD::XOR);
3930 // ((X^1)&1)^1 -> X & 1
3931 Val = DAG.getNode(ISD::AND, N0.getValueType(),
3932 N0.getOperand(0).getOperand(0),
3933 N0.getOperand(1));
3934 }
3935 return DAG.getSetCC(VT, Val, N1,
3936 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
Chris Lattner3391bcd2006-02-08 02:13:15 +00003937 }
Chris Lattner3391bcd2006-02-08 02:13:15 +00003938 }
Nate Begeman452d7be2005-09-16 00:54:12 +00003939 }
Chris Lattner5c46f742005-10-05 06:11:08 +00003940
Nate Begeman452d7be2005-09-16 00:54:12 +00003941 uint64_t MinVal, MaxVal;
3942 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
3943 if (ISD::isSignedIntSetCC(Cond)) {
3944 MinVal = 1ULL << (OperandBitSize-1);
3945 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
3946 MaxVal = ~0ULL >> (65-OperandBitSize);
3947 else
3948 MaxVal = 0;
3949 } else {
3950 MinVal = 0;
3951 MaxVal = ~0ULL >> (64-OperandBitSize);
3952 }
3953
3954 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
3955 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
3956 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
3957 --C1; // X >= C0 --> X > (C0-1)
3958 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3959 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
3960 }
3961
3962 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
3963 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
3964 ++C1; // X <= C0 --> X < (C0+1)
3965 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3966 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
3967 }
3968
3969 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
3970 return DAG.getConstant(0, VT); // X < MIN --> false
3971
3972 // Canonicalize setgt X, Min --> setne X, Min
3973 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
3974 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00003975 // Canonicalize setlt X, Max --> setne X, Max
3976 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
3977 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00003978
3979 // If we have setult X, 1, turn it into seteq X, 0
3980 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
3981 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
3982 ISD::SETEQ);
3983 // If we have setugt X, Max-1, turn it into seteq X, Max
3984 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
3985 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
3986 ISD::SETEQ);
3987
3988 // If we have "setcc X, C0", check to see if we can shrink the immediate
3989 // by changing cc.
3990
3991 // SETUGT X, SINTMAX -> SETLT X, 0
3992 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
3993 C1 == (~0ULL >> (65-OperandBitSize)))
3994 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
3995 ISD::SETLT);
3996
3997 // FIXME: Implement the rest of these.
3998
3999 // Fold bit comparisons when we can.
4000 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
4001 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
4002 if (ConstantSDNode *AndRHS =
4003 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
4004 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
4005 // Perform the xform if the AND RHS is a single bit.
Chris Lattner51dabfb2006-10-14 00:41:01 +00004006 if (isPowerOf2_64(AndRHS->getValue())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004007 return DAG.getNode(ISD::SRL, VT, N0,
4008 DAG.getConstant(Log2_64(AndRHS->getValue()),
4009 TLI.getShiftAmountTy()));
4010 }
4011 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
4012 // (X & 8) == 8 --> (X & 8) >> 3
4013 // Perform the xform if C1 is a single bit.
Chris Lattner51dabfb2006-10-14 00:41:01 +00004014 if (isPowerOf2_64(C1)) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004015 return DAG.getNode(ISD::SRL, VT, N0,
Chris Lattner729c6d12006-05-27 00:43:02 +00004016 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
Nate Begeman452d7be2005-09-16 00:54:12 +00004017 }
4018 }
4019 }
4020 }
4021 } else if (isa<ConstantSDNode>(N0.Val)) {
4022 // Ensure that the constant occurs on the RHS.
4023 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
4024 }
4025
Reid Spencer3ed469c2006-11-02 20:25:50 +00004026 if (isa<ConstantFPSDNode>(N0.Val)) {
Chris Lattner51dabfb2006-10-14 00:41:01 +00004027 // Constant fold or commute setcc.
4028 SDOperand O = DAG.FoldSetCC(VT, N0, N1, Cond);
4029 if (O.Val) return O;
4030 }
Nate Begeman452d7be2005-09-16 00:54:12 +00004031
4032 if (N0 == N1) {
Chris Lattner8ac9d0e2006-10-14 01:02:29 +00004033 // We can always fold X == X for integer setcc's.
Nate Begeman452d7be2005-09-16 00:54:12 +00004034 if (MVT::isInteger(N0.getValueType()))
4035 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
4036 unsigned UOF = ISD::getUnorderedFlavor(Cond);
4037 if (UOF == 2) // FP operators that are undefined on NaNs.
4038 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
4039 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
4040 return DAG.getConstant(UOF, VT);
4041 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
4042 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00004043 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00004044 if (NewCond != Cond)
4045 return DAG.getSetCC(VT, N0, N1, NewCond);
4046 }
4047
4048 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
4049 MVT::isInteger(N0.getValueType())) {
4050 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
4051 N0.getOpcode() == ISD::XOR) {
4052 // Simplify (X+Y) == (X+Z) --> Y == Z
4053 if (N0.getOpcode() == N1.getOpcode()) {
4054 if (N0.getOperand(0) == N1.getOperand(0))
4055 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
4056 if (N0.getOperand(1) == N1.getOperand(1))
4057 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
Evan Cheng1efba0e2006-08-29 06:42:35 +00004058 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004059 // If X op Y == Y op X, try other combinations.
4060 if (N0.getOperand(0) == N1.getOperand(1))
4061 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
4062 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00004063 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00004064 }
4065 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00004066
4067 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
4068 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
4069 // Turn (X+C1) == C2 --> X == C2-C1
4070 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
4071 return DAG.getSetCC(VT, N0.getOperand(0),
4072 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
4073 N0.getValueType()), Cond);
4074 }
4075
4076 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
4077 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00004078 // If we know that all of the inverted bits are zero, don't bother
4079 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00004080 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00004081 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00004082 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00004083 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00004084 }
4085
4086 // Turn (C1-X) == C2 --> X == C1-C2
4087 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
4088 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
4089 return DAG.getSetCC(VT, N0.getOperand(1),
4090 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
4091 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00004092 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00004093 }
4094 }
4095
Nate Begeman452d7be2005-09-16 00:54:12 +00004096 // Simplify (X+Z) == X --> Z == 0
4097 if (N0.getOperand(0) == N1)
4098 return DAG.getSetCC(VT, N0.getOperand(1),
4099 DAG.getConstant(0, N0.getValueType()), Cond);
4100 if (N0.getOperand(1) == N1) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00004101 if (DAG.isCommutativeBinOp(N0.getOpcode()))
Nate Begeman452d7be2005-09-16 00:54:12 +00004102 return DAG.getSetCC(VT, N0.getOperand(0),
4103 DAG.getConstant(0, N0.getValueType()), Cond);
4104 else {
4105 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
4106 // (Z-X) == X --> Z == X<<1
4107 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
4108 N1,
4109 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00004110 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004111 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
4112 }
4113 }
4114 }
4115
4116 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
4117 N1.getOpcode() == ISD::XOR) {
4118 // Simplify X == (X+Z) --> Z == 0
4119 if (N1.getOperand(0) == N0) {
4120 return DAG.getSetCC(VT, N1.getOperand(1),
4121 DAG.getConstant(0, N1.getValueType()), Cond);
4122 } else if (N1.getOperand(1) == N0) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00004123 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004124 return DAG.getSetCC(VT, N1.getOperand(0),
4125 DAG.getConstant(0, N1.getValueType()), Cond);
4126 } else {
4127 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
4128 // X == (Z-X) --> X<<1 == Z
4129 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
4130 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00004131 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004132 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
4133 }
4134 }
4135 }
4136 }
4137
4138 // Fold away ALL boolean setcc's.
4139 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00004140 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004141 switch (Cond) {
4142 default: assert(0 && "Unknown integer setcc!");
4143 case ISD::SETEQ: // X == Y -> (X^Y)^1
4144 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
4145 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00004146 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004147 break;
4148 case ISD::SETNE: // X != Y --> (X^Y)
4149 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
4150 break;
4151 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
4152 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
4153 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
4154 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00004155 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004156 break;
4157 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
4158 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
4159 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
4160 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00004161 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004162 break;
4163 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
4164 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
4165 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
4166 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00004167 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004168 break;
4169 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
4170 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
4171 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
4172 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
4173 break;
4174 }
4175 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00004176 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004177 // FIXME: If running after legalize, we probably can't do this.
4178 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
4179 }
4180 return N0;
4181 }
4182
4183 // Could not fold it.
4184 return SDOperand();
4185}
4186
Nate Begeman69575232005-10-20 02:15:44 +00004187/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
4188/// return a DAG expression to select that will generate the same value by
4189/// multiplying by a magic number. See:
4190/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4191SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004192 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004193 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
4194
Andrew Lenharth232c9102006-06-12 16:07:18 +00004195 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004196 ii != ee; ++ii)
4197 AddToWorkList(*ii);
4198 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004199}
4200
4201/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
4202/// return a DAG expression to select that will generate the same value by
4203/// multiplying by a magic number. See:
4204/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4205SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004206 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004207 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00004208
Andrew Lenharth232c9102006-06-12 16:07:18 +00004209 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004210 ii != ee; ++ii)
4211 AddToWorkList(*ii);
4212 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004213}
4214
Jim Laskey71382342006-10-07 23:37:56 +00004215/// FindBaseOffset - Return true if base is known not to alias with anything
4216/// but itself. Provides base object and offset as results.
4217static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
4218 // Assume it is a primitive operation.
4219 Base = Ptr; Offset = 0;
4220
4221 // If it's an adding a simple constant then integrate the offset.
4222 if (Base.getOpcode() == ISD::ADD) {
4223 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
4224 Base = Base.getOperand(0);
4225 Offset += C->getValue();
4226 }
4227 }
4228
4229 // If it's any of the following then it can't alias with anything but itself.
4230 return isa<FrameIndexSDNode>(Base) ||
4231 isa<ConstantPoolSDNode>(Base) ||
4232 isa<GlobalAddressSDNode>(Base);
4233}
4234
4235/// isAlias - Return true if there is any possibility that the two addresses
4236/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00004237bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
4238 const Value *SrcValue1, int SrcValueOffset1,
4239 SDOperand Ptr2, int64_t Size2,
4240 const Value *SrcValue2, int SrcValueOffset2)
4241{
Jim Laskey71382342006-10-07 23:37:56 +00004242 // If they are the same then they must be aliases.
4243 if (Ptr1 == Ptr2) return true;
4244
4245 // Gather base node and offset information.
4246 SDOperand Base1, Base2;
4247 int64_t Offset1, Offset2;
4248 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
4249 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
4250
4251 // If they have a same base address then...
4252 if (Base1 == Base2) {
4253 // Check to see if the addresses overlap.
4254 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
4255 }
4256
Jim Laskey096c22e2006-10-18 12:29:57 +00004257 // If we know both bases then they can't alias.
4258 if (KnownBase1 && KnownBase2) return false;
4259
Jim Laskey07a27092006-10-18 19:08:31 +00004260 if (CombinerGlobalAA) {
4261 // Use alias analysis information.
4262 int Overlap1 = Size1 + SrcValueOffset1 + Offset1;
4263 int Overlap2 = Size2 + SrcValueOffset2 + Offset2;
4264 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00004265 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00004266 if (AAResult == AliasAnalysis::NoAlias)
4267 return false;
4268 }
Jim Laskey096c22e2006-10-18 12:29:57 +00004269
4270 // Otherwise we have to assume they alias.
4271 return true;
Jim Laskey71382342006-10-07 23:37:56 +00004272}
4273
4274/// FindAliasInfo - Extracts the relevant alias information from the memory
4275/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00004276bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00004277 SDOperand &Ptr, int64_t &Size,
4278 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004279 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4280 Ptr = LD->getBasePtr();
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004281 Size = MVT::getSizeInBits(LD->getLoadedVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004282 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004283 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00004284 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004285 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004286 Ptr = ST->getBasePtr();
Evan Cheng8b2794a2006-10-13 21:14:26 +00004287 Size = MVT::getSizeInBits(ST->getStoredVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004288 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004289 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00004290 } else {
Jim Laskey71382342006-10-07 23:37:56 +00004291 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00004292 }
4293
4294 return false;
4295}
4296
Jim Laskey6ff23e52006-10-04 16:53:27 +00004297/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
4298/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00004299void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00004300 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004301 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00004302 std::set<SDNode *> Visited; // Visited node set.
4303
Jim Laskey279f0532006-09-25 16:29:54 +00004304 // Get alias information for node.
4305 SDOperand Ptr;
4306 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004307 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004308 int SrcValueOffset;
4309 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00004310
Jim Laskey6ff23e52006-10-04 16:53:27 +00004311 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00004312 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00004313
Jim Laskeybc588b82006-10-05 15:07:25 +00004314 // Look at each chain and determine if it is an alias. If so, add it to the
4315 // aliases list. If not, then continue up the chain looking for the next
4316 // candidate.
4317 while (!Chains.empty()) {
4318 SDOperand Chain = Chains.back();
4319 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00004320
Jim Laskeybc588b82006-10-05 15:07:25 +00004321 // Don't bother if we've been before.
4322 if (Visited.find(Chain.Val) != Visited.end()) continue;
4323 Visited.insert(Chain.Val);
4324
4325 switch (Chain.getOpcode()) {
4326 case ISD::EntryToken:
4327 // Entry token is ideal chain operand, but handled in FindBetterChain.
4328 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00004329
Jim Laskeybc588b82006-10-05 15:07:25 +00004330 case ISD::LOAD:
4331 case ISD::STORE: {
4332 // Get alias information for Chain.
4333 SDOperand OpPtr;
4334 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004335 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004336 int OpSrcValueOffset;
4337 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
4338 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00004339
4340 // If chain is alias then stop here.
4341 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00004342 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
4343 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004344 Aliases.push_back(Chain);
4345 } else {
4346 // Look further up the chain.
4347 Chains.push_back(Chain.getOperand(0));
4348 // Clean up old chain.
4349 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00004350 }
Jim Laskeybc588b82006-10-05 15:07:25 +00004351 break;
4352 }
4353
4354 case ISD::TokenFactor:
4355 // We have to check each of the operands of the token factor, so we queue
4356 // then up. Adding the operands to the queue (stack) in reverse order
4357 // maintains the original order and increases the likelihood that getNode
4358 // will find a matching token factor (CSE.)
4359 for (unsigned n = Chain.getNumOperands(); n;)
4360 Chains.push_back(Chain.getOperand(--n));
4361 // Eliminate the token factor if we can.
4362 AddToWorkList(Chain.Val);
4363 break;
4364
4365 default:
4366 // For all other instructions we will just have to take what we can get.
4367 Aliases.push_back(Chain);
4368 break;
Jim Laskey279f0532006-09-25 16:29:54 +00004369 }
4370 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004371}
4372
4373/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
4374/// for a better chain (aliasing node.)
4375SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
4376 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00004377
Jim Laskey6ff23e52006-10-04 16:53:27 +00004378 // Accumulate all the aliases to this node.
4379 GatherAllAliases(N, OldChain, Aliases);
4380
4381 if (Aliases.size() == 0) {
4382 // If no operands then chain to entry token.
4383 return DAG.getEntryNode();
4384 } else if (Aliases.size() == 1) {
4385 // If a single operand then chain to it. We don't need to revisit it.
4386 return Aliases[0];
4387 }
4388
4389 // Construct a custom tailored token factor.
4390 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4391 &Aliases[0], Aliases.size());
4392
4393 // Make sure the old chain gets cleaned up.
4394 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
4395
4396 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00004397}
4398
Nate Begeman1d4d4142005-09-01 00:19:25 +00004399// SelectionDAG::Combine - This is the entry point for the file.
4400//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004401void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00004402 /// run - This is the main entry point to this class.
4403 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004404 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004405}