blob: b74de8e843b918d7f8845447d852163d0b188e63 [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 Cheng8dc5cad2006-11-09 19:10:46 +0000193 MVT::ValueType VT;
Evan Cheng7fc033a2006-11-03 03:06:21 +0000194 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Cheng8dc5cad2006-11-09 19:10:46 +0000195 VT = LD->getLoadedVT();
196 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
197 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
198 return false;
Evan Cheng7fc033a2006-11-03 03:06:21 +0000199 Ptr = LD->getBasePtr();
Evan Cheng33dbedc2006-11-05 09:31:14 +0000200 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Cheng8dc5cad2006-11-09 19:10:46 +0000201 VT = ST->getStoredVT();
202 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
203 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
204 return false;
Evan Cheng33dbedc2006-11-05 09:31:14 +0000205 Ptr = ST->getBasePtr();
206 isLoad = false;
Evan Cheng7fc033a2006-11-03 03:06:21 +0000207 } else
208 return false;
209
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000210 if ((Ptr.getOpcode() == ISD::ADD || Ptr.getOpcode() == ISD::SUB) &&
Evan Cheng7fc033a2006-11-03 03:06:21 +0000211 Ptr.Val->use_size() > 1) {
212 SDOperand BasePtr;
213 SDOperand Offset;
Evan Cheng144d8f02006-11-09 17:55:04 +0000214 ISD::MemIndexedMode AM = ISD::UNINDEXED;
Evan Cheng1a854be2006-11-03 07:21:16 +0000215 if (TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG)) {
Evan Cheng7fc033a2006-11-03 03:06:21 +0000216 // Try turning it into a pre-indexed load / store except when
217 // 1) Another use of base ptr is a predecessor of N. If ptr is folded
218 // that would create a cycle.
Evan Cheng03fa6ea2006-11-08 08:30:28 +0000219 // 2) All uses are load / store ops that use it as base ptr.
Evan Cheng7fc033a2006-11-03 03:06:21 +0000220
221 // Now check for #1 and #2.
Evan Cheng03fa6ea2006-11-08 08:30:28 +0000222 bool RealUse = false;
223 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
224 E = Ptr.Val->use_end(); I != E; ++I) {
225 SDNode *Use = *I;
226 if (Use == N)
227 continue;
228 if (Use->isPredecessor(N))
Evan Chenga4f53ef2006-11-08 06:56:05 +0000229 return false;
Evan Cheng03fa6ea2006-11-08 08:30:28 +0000230
231 if (!((Use->getOpcode() == ISD::LOAD &&
232 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
233 (Use->getOpcode() == ISD::STORE) &&
234 cast<StoreSDNode>(Use)->getBasePtr() == Ptr))
235 RealUse = true;
Evan Cheng7fc033a2006-11-03 03:06:21 +0000236 }
Evan Cheng03fa6ea2006-11-08 08:30:28 +0000237 if (!RealUse)
238 return false;
Evan Cheng7fc033a2006-11-03 03:06:21 +0000239
Evan Cheng33dbedc2006-11-05 09:31:14 +0000240 SDOperand Result = isLoad
241 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
242 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000243 ++PreIndexedNodes;
Evan Cheng7fc033a2006-11-03 03:06:21 +0000244 ++NodesCombined;
245 DEBUG(std::cerr << "\nReplacing.4 "; N->dump();
246 std::cerr << "\nWith: "; Result.Val->dump(&DAG);
247 std::cerr << '\n');
248 std::vector<SDNode*> NowDead;
Evan Cheng33dbedc2006-11-05 09:31:14 +0000249 if (isLoad) {
250 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
251 NowDead);
252 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
253 NowDead);
254 } else {
255 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
256 NowDead);
257 }
Evan Cheng7fc033a2006-11-03 03:06:21 +0000258
259 // Nodes can end up on the worklist more than once. Make sure we do
260 // not process a node that has been replaced.
261 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
262 removeFromWorkList(NowDead[i]);
263 // Finally, since the node is now dead, remove it from the graph.
264 DAG.DeleteNode(N);
265
266 // Replace the uses of Ptr with uses of the updated base value.
Evan Cheng33dbedc2006-11-05 09:31:14 +0000267 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
268 NowDead);
Evan Cheng7fc033a2006-11-03 03:06:21 +0000269 removeFromWorkList(Ptr.Val);
270 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
271 removeFromWorkList(NowDead[i]);
272 DAG.DeleteNode(Ptr.Val);
273
274 return true;
275 }
276 }
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000277 return false;
278 }
Evan Cheng7fc033a2006-11-03 03:06:21 +0000279
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000280 /// CombineToPostIndexedLoadStore - Try combine a load / store with a
281 /// add / sub of the base pointer node into a post-indexed load / store.
282 /// The transformation folded the add / subtract into the new indexed
283 /// load / store effectively and all of its uses are redirected to the
284 /// new load / store.
285 bool CombineToPostIndexedLoadStore(SDNode *N) {
286 if (!AfterLegalize)
287 return false;
288
289 bool isLoad = true;
290 SDOperand Ptr;
291 MVT::ValueType VT;
292 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Cheng8dc5cad2006-11-09 19:10:46 +0000293 VT = LD->getLoadedVT();
294 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
295 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
296 return false;
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000297 Ptr = LD->getBasePtr();
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000298 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Cheng8dc5cad2006-11-09 19:10:46 +0000299 VT = ST->getStoredVT();
300 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
301 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
302 return false;
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000303 Ptr = ST->getBasePtr();
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000304 isLoad = false;
305 } else
306 return false;
307
308 if (Ptr.Val->use_size() > 1) {
309 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
310 E = Ptr.Val->use_end(); I != E; ++I) {
311 SDNode *Op = *I;
312 if (Op == N ||
313 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
314 continue;
315
316 SDOperand BasePtr;
317 SDOperand Offset;
Evan Cheng144d8f02006-11-09 17:55:04 +0000318 ISD::MemIndexedMode AM = ISD::UNINDEXED;
Evan Cheng8dc5cad2006-11-09 19:10:46 +0000319 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Evan Chengb00dddd2006-11-08 20:27:27 +0000320 if (Ptr == Offset)
321 std::swap(BasePtr, Offset);
322 if (Ptr != BasePtr)
323 continue;
324
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000325 // Try turning it into a post-indexed load / store except when
326 // 1) Op must be independent of N, i.e. Op is neither a predecessor
327 // nor a successor of N. Otherwise, if Op is folded that would
328 // create a cycle.
Evan Cheng03fa6ea2006-11-08 08:30:28 +0000329 // 2) All uses are load / store ops that use it as base ptr.
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000330
331 // Check for #3.
332 bool TryNext = false;
333 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
334 EE = BasePtr.Val->use_end(); II != EE; ++II) {
335 SDNode *Use = *II;
336 if (Use == Ptr.Val)
337 continue;
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000338
Evan Cheng03fa6ea2006-11-08 08:30:28 +0000339 // If all the uses are load / store addresses, then don't do the
340 // transformation.
341 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
342 bool RealUse = false;
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000343 for (SDNode::use_iterator III = Use->use_begin(),
344 EEE = Use->use_end(); III != EEE; ++III) {
345 SDNode *UseUse = *III;
Evan Cheng03fa6ea2006-11-08 08:30:28 +0000346 if (!((UseUse->getOpcode() == ISD::LOAD &&
347 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
348 (UseUse->getOpcode() == ISD::STORE) &&
349 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use))
350 RealUse = true;
351 }
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000352
Evan Cheng03fa6ea2006-11-08 08:30:28 +0000353 if (!RealUse) {
354 TryNext = true;
355 break;
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000356 }
357 }
358 }
359 if (TryNext)
360 continue;
361
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000362 // Check for #1
363 if (!Op->isPredecessor(N) && !N->isPredecessor(Op)) {
364 SDOperand Result = isLoad
365 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
366 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
367 ++PostIndexedNodes;
368 ++NodesCombined;
369 DEBUG(std::cerr << "\nReplacing.5 "; N->dump();
370 std::cerr << "\nWith: "; Result.Val->dump(&DAG);
371 std::cerr << '\n');
372 std::vector<SDNode*> NowDead;
373 if (isLoad) {
374 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
375 NowDead);
376 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
377 NowDead);
378 } else {
379 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
380 NowDead);
381 }
382
383 // Nodes can end up on the worklist more than once. Make sure we do
384 // not process a node that has been replaced.
385 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
386 removeFromWorkList(NowDead[i]);
387 // Finally, since the node is now dead, remove it from the graph.
388 DAG.DeleteNode(N);
389
390 // Replace the uses of Use with uses of the updated base value.
391 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
392 Result.getValue(isLoad ? 1 : 0),
393 NowDead);
394 removeFromWorkList(Op);
395 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
396 removeFromWorkList(NowDead[i]);
397 DAG.DeleteNode(Op);
398
399 return true;
400 }
401 }
402 }
403 }
Evan Cheng7fc033a2006-11-03 03:06:21 +0000404 return false;
405 }
406
Nate Begeman1d4d4142005-09-01 00:19:25 +0000407 /// visit - call the node-specific routine that knows how to fold each
408 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000409 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000410
411 // Visitation implementation - Implement dag node combining for different
412 // node types. The semantics are as follows:
413 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000414 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000415 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000416 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000417 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000418 SDOperand visitTokenFactor(SDNode *N);
419 SDOperand visitADD(SDNode *N);
420 SDOperand visitSUB(SDNode *N);
421 SDOperand visitMUL(SDNode *N);
422 SDOperand visitSDIV(SDNode *N);
423 SDOperand visitUDIV(SDNode *N);
424 SDOperand visitSREM(SDNode *N);
425 SDOperand visitUREM(SDNode *N);
426 SDOperand visitMULHU(SDNode *N);
427 SDOperand visitMULHS(SDNode *N);
428 SDOperand visitAND(SDNode *N);
429 SDOperand visitOR(SDNode *N);
430 SDOperand visitXOR(SDNode *N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000431 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000432 SDOperand visitSHL(SDNode *N);
433 SDOperand visitSRA(SDNode *N);
434 SDOperand visitSRL(SDNode *N);
435 SDOperand visitCTLZ(SDNode *N);
436 SDOperand visitCTTZ(SDNode *N);
437 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000438 SDOperand visitSELECT(SDNode *N);
439 SDOperand visitSELECT_CC(SDNode *N);
440 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000441 SDOperand visitSIGN_EXTEND(SDNode *N);
442 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000443 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000444 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
445 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000446 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000447 SDOperand visitVBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000448 SDOperand visitFADD(SDNode *N);
449 SDOperand visitFSUB(SDNode *N);
450 SDOperand visitFMUL(SDNode *N);
451 SDOperand visitFDIV(SDNode *N);
452 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000453 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000454 SDOperand visitSINT_TO_FP(SDNode *N);
455 SDOperand visitUINT_TO_FP(SDNode *N);
456 SDOperand visitFP_TO_SINT(SDNode *N);
457 SDOperand visitFP_TO_UINT(SDNode *N);
458 SDOperand visitFP_ROUND(SDNode *N);
459 SDOperand visitFP_ROUND_INREG(SDNode *N);
460 SDOperand visitFP_EXTEND(SDNode *N);
461 SDOperand visitFNEG(SDNode *N);
462 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000463 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000464 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000465 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000466 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000467 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
468 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000469 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000470 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000471 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000472
Evan Cheng44f1f092006-04-20 08:56:16 +0000473 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000474 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
475
Chris Lattner40c62d52005-10-18 06:04:22 +0000476 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000477 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000478 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
479 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
480 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000481 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000482 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000483 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000484 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000485 SDOperand BuildUDIV(SDNode *N);
486 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Jim Laskey279f0532006-09-25 16:29:54 +0000487
Jim Laskey6ff23e52006-10-04 16:53:27 +0000488 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
489 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000490 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000491 SmallVector<SDOperand, 8> &Aliases);
492
Jim Laskey096c22e2006-10-18 12:29:57 +0000493 /// isAlias - Return true if there is any possibility that the two addresses
494 /// overlap.
495 bool isAlias(SDOperand Ptr1, int64_t Size1,
496 const Value *SrcValue1, int SrcValueOffset1,
497 SDOperand Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000498 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000499
Jim Laskey7ca56af2006-10-11 13:47:09 +0000500 /// FindAliasInfo - Extracts the relevant alias information from the memory
501 /// node. Returns true if the operand was a load.
502 bool FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +0000503 SDOperand &Ptr, int64_t &Size,
504 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000505
Jim Laskey279f0532006-09-25 16:29:54 +0000506 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000507 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000508 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
509
Nate Begeman1d4d4142005-09-01 00:19:25 +0000510public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000511 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
512 : DAG(D),
513 TLI(D.getTargetLoweringInfo()),
514 AfterLegalize(false),
515 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000516
517 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000518 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000519 };
520}
521
Chris Lattner24664722006-03-01 04:53:38 +0000522//===----------------------------------------------------------------------===//
523// TargetLowering::DAGCombinerInfo implementation
524//===----------------------------------------------------------------------===//
525
526void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
527 ((DAGCombiner*)DC)->AddToWorkList(N);
528}
529
530SDOperand TargetLowering::DAGCombinerInfo::
531CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000532 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000533}
534
535SDOperand TargetLowering::DAGCombinerInfo::
536CombineTo(SDNode *N, SDOperand Res) {
537 return ((DAGCombiner*)DC)->CombineTo(N, Res);
538}
539
540
541SDOperand TargetLowering::DAGCombinerInfo::
542CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
543 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
544}
545
546
547
548
549//===----------------------------------------------------------------------===//
550
551
Nate Begeman4ebd8052005-09-01 23:24:04 +0000552// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
553// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000554// Also, set the incoming LHS, RHS, and CC references to the appropriate
555// nodes based on the type of node we are checking. This simplifies life a
556// bit for the callers.
557static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
558 SDOperand &CC) {
559 if (N.getOpcode() == ISD::SETCC) {
560 LHS = N.getOperand(0);
561 RHS = N.getOperand(1);
562 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000563 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000564 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000565 if (N.getOpcode() == ISD::SELECT_CC &&
566 N.getOperand(2).getOpcode() == ISD::Constant &&
567 N.getOperand(3).getOpcode() == ISD::Constant &&
568 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000569 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
570 LHS = N.getOperand(0);
571 RHS = N.getOperand(1);
572 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000573 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000574 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000575 return false;
576}
577
Nate Begeman99801192005-09-07 23:25:52 +0000578// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
579// one use. If this is true, it allows the users to invert the operation for
580// free when it is profitable to do so.
581static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000582 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000583 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000584 return true;
585 return false;
586}
587
Nate Begemancd4d58c2006-02-03 06:46:56 +0000588SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
589 MVT::ValueType VT = N0.getValueType();
590 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
591 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
592 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
593 if (isa<ConstantSDNode>(N1)) {
594 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
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, N0.getOperand(0));
597 } else if (N0.hasOneUse()) {
598 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000599 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000600 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
601 }
602 }
603 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
604 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
605 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
606 if (isa<ConstantSDNode>(N0)) {
607 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000608 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000609 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
610 } else if (N1.hasOneUse()) {
611 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000612 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000613 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
614 }
615 }
616 return SDOperand();
617}
618
Nate Begeman4ebd8052005-09-01 23:24:04 +0000619void DAGCombiner::Run(bool RunningAfterLegalize) {
620 // set the instance variable, so that the various visit routines may use it.
621 AfterLegalize = RunningAfterLegalize;
622
Nate Begeman646d7e22005-09-02 21:18:40 +0000623 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000624 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
625 E = DAG.allnodes_end(); I != E; ++I)
626 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000627
Chris Lattner95038592005-10-05 06:35:28 +0000628 // Create a dummy node (which is not added to allnodes), that adds a reference
629 // to the root node, preventing it from being deleted, and tracking any
630 // changes of the root.
631 HandleSDNode Dummy(DAG.getRoot());
632
Jim Laskey26f7fa72006-10-17 19:33:52 +0000633 // The root of the dag may dangle to deleted nodes until the dag combiner is
634 // done. Set it to null to avoid confusion.
635 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000636
637 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
638 TargetLowering::DAGCombinerInfo
639 DagCombineInfo(DAG, !RunningAfterLegalize, this);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000640
Nate Begeman1d4d4142005-09-01 00:19:25 +0000641 // while the worklist isn't empty, inspect the node on the end of it and
642 // try and combine it.
643 while (!WorkList.empty()) {
644 SDNode *N = WorkList.back();
645 WorkList.pop_back();
646
647 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000648 // N is deleted from the DAG, since they too may now be dead or may have a
649 // reduced number of uses, allowing other xforms.
650 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000651 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000652 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000653
Chris Lattner95038592005-10-05 06:35:28 +0000654 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000655 continue;
656 }
657
Nate Begeman83e75ec2005-09-06 04:43:02 +0000658 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000659
660 // If nothing happened, try a target-specific DAG combine.
661 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000662 assert(N->getOpcode() != ISD::DELETED_NODE &&
663 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000664 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
665 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
666 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
667 }
668
Nate Begeman83e75ec2005-09-06 04:43:02 +0000669 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000670 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000671 // If we get back the same node we passed in, rather than a new node or
672 // zero, we know that the node must have defined multiple values and
673 // CombineTo was used. Since CombineTo takes care of the worklist
674 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000675 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000676 assert(N->getOpcode() != ISD::DELETED_NODE &&
677 RV.Val->getOpcode() != ISD::DELETED_NODE &&
678 "Node was deleted but visit returned new node!");
679
Jim Laskey6ff23e52006-10-04 16:53:27 +0000680 DEBUG(std::cerr << "\nReplacing.3 "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000681 std::cerr << "\nWith: "; RV.Val->dump(&DAG);
Nate Begeman2300f552005-09-07 00:15:36 +0000682 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000683 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000684 if (N->getNumValues() == RV.Val->getNumValues())
685 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
686 else {
687 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
688 SDOperand OpV = RV;
689 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
690 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000691
692 // Push the new node and any users onto the worklist
Jim Laskey6ff23e52006-10-04 16:53:27 +0000693 AddToWorkList(RV.Val);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000694 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000695
Jim Laskey6ff23e52006-10-04 16:53:27 +0000696 // Nodes can be reintroduced into the worklist. Make sure we do not
697 // process a node that has been replaced.
Nate Begeman646d7e22005-09-02 21:18:40 +0000698 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000699 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
700 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000701
702 // Finally, since the node is now dead, remove it from the graph.
703 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000704 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000705 }
706 }
Chris Lattner95038592005-10-05 06:35:28 +0000707
708 // If the root changed (e.g. it was a dead load, update the root).
709 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000710}
711
Nate Begeman83e75ec2005-09-06 04:43:02 +0000712SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000713 switch(N->getOpcode()) {
714 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000715 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000716 case ISD::ADD: return visitADD(N);
717 case ISD::SUB: return visitSUB(N);
718 case ISD::MUL: return visitMUL(N);
719 case ISD::SDIV: return visitSDIV(N);
720 case ISD::UDIV: return visitUDIV(N);
721 case ISD::SREM: return visitSREM(N);
722 case ISD::UREM: return visitUREM(N);
723 case ISD::MULHU: return visitMULHU(N);
724 case ISD::MULHS: return visitMULHS(N);
725 case ISD::AND: return visitAND(N);
726 case ISD::OR: return visitOR(N);
727 case ISD::XOR: return visitXOR(N);
728 case ISD::SHL: return visitSHL(N);
729 case ISD::SRA: return visitSRA(N);
730 case ISD::SRL: return visitSRL(N);
731 case ISD::CTLZ: return visitCTLZ(N);
732 case ISD::CTTZ: return visitCTTZ(N);
733 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000734 case ISD::SELECT: return visitSELECT(N);
735 case ISD::SELECT_CC: return visitSELECT_CC(N);
736 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000737 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
738 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000739 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000740 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
741 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000742 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000743 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000744 case ISD::FADD: return visitFADD(N);
745 case ISD::FSUB: return visitFSUB(N);
746 case ISD::FMUL: return visitFMUL(N);
747 case ISD::FDIV: return visitFDIV(N);
748 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000749 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000750 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
751 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
752 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
753 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
754 case ISD::FP_ROUND: return visitFP_ROUND(N);
755 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
756 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
757 case ISD::FNEG: return visitFNEG(N);
758 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000759 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000760 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000761 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000762 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000763 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
764 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000765 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000766 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000767 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000768 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
769 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
770 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
771 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
772 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
773 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
774 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
775 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000776 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000777 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000778}
779
Chris Lattner6270f682006-10-08 22:57:01 +0000780/// getInputChainForNode - Given a node, return its input chain if it has one,
781/// otherwise return a null sd operand.
782static SDOperand getInputChainForNode(SDNode *N) {
783 if (unsigned NumOps = N->getNumOperands()) {
784 if (N->getOperand(0).getValueType() == MVT::Other)
785 return N->getOperand(0);
786 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
787 return N->getOperand(NumOps-1);
788 for (unsigned i = 1; i < NumOps-1; ++i)
789 if (N->getOperand(i).getValueType() == MVT::Other)
790 return N->getOperand(i);
791 }
792 return SDOperand(0, 0);
793}
794
Nate Begeman83e75ec2005-09-06 04:43:02 +0000795SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000796 // If N has two operands, where one has an input chain equal to the other,
797 // the 'other' chain is redundant.
798 if (N->getNumOperands() == 2) {
799 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
800 return N->getOperand(0);
801 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
802 return N->getOperand(1);
803 }
804
805
Jim Laskey6ff23e52006-10-04 16:53:27 +0000806 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Jim Laskey279f0532006-09-25 16:29:54 +0000807 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000808 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000809
810 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000811 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000812
Jim Laskey71382342006-10-07 23:37:56 +0000813 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000814 // encountered.
815 for (unsigned i = 0; i < TFs.size(); ++i) {
816 SDNode *TF = TFs[i];
817
Jim Laskey6ff23e52006-10-04 16:53:27 +0000818 // Check each of the operands.
819 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
820 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000821
Jim Laskey6ff23e52006-10-04 16:53:27 +0000822 switch (Op.getOpcode()) {
823 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000824 // Entry tokens don't need to be added to the list. They are
825 // rededundant.
826 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000827 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000828
Jim Laskey6ff23e52006-10-04 16:53:27 +0000829 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000830 if ((CombinerAA || Op.hasOneUse()) &&
831 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000832 // Queue up for processing.
833 TFs.push_back(Op.Val);
834 // Clean up in case the token factor is removed.
835 AddToWorkList(Op.Val);
836 Changed = true;
837 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000838 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000839 // Fall thru
840
841 default:
Jim Laskeybc588b82006-10-05 15:07:25 +0000842 // Only add if not there prior.
843 if (std::find(Ops.begin(), Ops.end(), Op) == Ops.end())
844 Ops.push_back(Op);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000845 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000846 }
847 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000848 }
849
850 SDOperand Result;
851
852 // If we've change things around then replace token factor.
853 if (Changed) {
854 if (Ops.size() == 0) {
855 // The entry token is the only possible outcome.
856 Result = DAG.getEntryNode();
857 } else {
858 // New and improved token factor.
859 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000860 }
Jim Laskey274062c2006-10-13 23:32:28 +0000861
862 // Don't add users to work list.
863 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000864 }
Jim Laskey279f0532006-09-25 16:29:54 +0000865
Jim Laskey6ff23e52006-10-04 16:53:27 +0000866 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000867}
868
Nate Begeman83e75ec2005-09-06 04:43:02 +0000869SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000870 SDOperand N0 = N->getOperand(0);
871 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000872 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
873 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000874 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000875
876 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000877 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000878 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000879 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000880 if (N0C && !N1C)
881 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000882 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000883 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000884 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000885 // fold ((c1-A)+c2) -> (c1+c2)-A
886 if (N1C && N0.getOpcode() == ISD::SUB)
887 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
888 return DAG.getNode(ISD::SUB, VT,
889 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
890 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000891 // reassociate add
892 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
893 if (RADD.Val != 0)
894 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000895 // fold ((0-A) + B) -> B-A
896 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
897 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000898 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000899 // fold (A + (0-B)) -> A-B
900 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
901 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000902 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000903 // fold (A+(B-A)) -> B
904 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000905 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000906
Evan Cheng860771d2006-03-01 01:09:54 +0000907 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000908 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000909
910 // fold (a+b) -> (a|b) iff a and b share no bits.
911 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
912 uint64_t LHSZero, LHSOne;
913 uint64_t RHSZero, RHSOne;
914 uint64_t Mask = MVT::getIntVTBitMask(VT);
915 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
916 if (LHSZero) {
917 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
918
919 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
920 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
921 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
922 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
923 return DAG.getNode(ISD::OR, VT, N0, N1);
924 }
925 }
Evan Cheng3ef554d2006-11-06 08:14:30 +0000926
Nate Begeman83e75ec2005-09-06 04:43:02 +0000927 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000928}
929
Nate Begeman83e75ec2005-09-06 04:43:02 +0000930SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000931 SDOperand N0 = N->getOperand(0);
932 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000933 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
934 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000935 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000936
Chris Lattner854077d2005-10-17 01:07:11 +0000937 // fold (sub x, x) -> 0
938 if (N0 == N1)
939 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000940 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000941 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000942 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000943 // fold (sub x, c) -> (add x, -c)
944 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000945 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000946 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000947 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000948 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000949 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000950 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000951 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000952 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000953}
954
Nate Begeman83e75ec2005-09-06 04:43:02 +0000955SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000956 SDOperand N0 = N->getOperand(0);
957 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000958 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
959 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000960 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000961
962 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000963 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000964 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000965 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000966 if (N0C && !N1C)
967 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000968 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000969 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000970 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000971 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000972 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000973 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000974 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000975 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000976 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000977 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000978 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000979 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
980 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
981 // FIXME: If the input is something that is easily negated (e.g. a
982 // single-use add), we should put the negate there.
983 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
984 DAG.getNode(ISD::SHL, VT, N0,
985 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
986 TLI.getShiftAmountTy())));
987 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +0000988
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000989 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
990 if (N1C && N0.getOpcode() == ISD::SHL &&
991 isa<ConstantSDNode>(N0.getOperand(1))) {
992 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000993 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000994 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
995 }
996
997 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
998 // use.
999 {
1000 SDOperand Sh(0,0), Y(0,0);
1001 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1002 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1003 N0.Val->hasOneUse()) {
1004 Sh = N0; Y = N1;
1005 } else if (N1.getOpcode() == ISD::SHL &&
1006 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1007 Sh = N1; Y = N0;
1008 }
1009 if (Sh.Val) {
1010 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1011 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1012 }
1013 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001014 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1015 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1016 isa<ConstantSDNode>(N0.getOperand(1))) {
1017 return DAG.getNode(ISD::ADD, VT,
1018 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1019 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1020 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001021
Nate Begemancd4d58c2006-02-03 06:46:56 +00001022 // reassociate mul
1023 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1024 if (RMUL.Val != 0)
1025 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +00001026 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001027}
1028
Nate Begeman83e75ec2005-09-06 04:43:02 +00001029SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001030 SDOperand N0 = N->getOperand(0);
1031 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001032 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1033 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001034 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001035
1036 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001037 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001038 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001039 // fold (sdiv X, 1) -> X
1040 if (N1C && N1C->getSignExtended() == 1LL)
1041 return N0;
1042 // fold (sdiv X, -1) -> 0-X
1043 if (N1C && N1C->isAllOnesValue())
1044 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001045 // If we know the sign bits of both operands are zero, strength reduce to a
1046 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
1047 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001048 if (TLI.MaskedValueIsZero(N1, SignBit) &&
1049 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +00001050 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001051 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +00001052 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001053 (isPowerOf2_64(N1C->getSignExtended()) ||
1054 isPowerOf2_64(-N1C->getSignExtended()))) {
1055 // If dividing by powers of two is cheap, then don't perform the following
1056 // fold.
1057 if (TLI.isPow2DivCheap())
1058 return SDOperand();
1059 int64_t pow2 = N1C->getSignExtended();
1060 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001061 unsigned lg2 = Log2_64(abs2);
1062 // Splat the sign bit into the register
1063 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001064 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1065 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001066 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001067 // Add (N0 < 0) ? abs2 - 1 : 0;
1068 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1069 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001070 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001071 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001072 AddToWorkList(SRL.Val);
1073 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001074 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1075 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001076 // If we're dividing by a positive value, we're done. Otherwise, we must
1077 // negate the result.
1078 if (pow2 > 0)
1079 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001080 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001081 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1082 }
Nate Begeman69575232005-10-20 02:15:44 +00001083 // if integer divide is expensive and we satisfy the requirements, emit an
1084 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001085 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001086 !TLI.isIntDivCheap()) {
1087 SDOperand Op = BuildSDIV(N);
1088 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001089 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001090 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001091}
1092
Nate Begeman83e75ec2005-09-06 04:43:02 +00001093SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001094 SDOperand N0 = N->getOperand(0);
1095 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001096 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1097 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001098 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001099
1100 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001101 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001102 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001103 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +00001104 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001105 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001106 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001107 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001108 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1109 if (N1.getOpcode() == ISD::SHL) {
1110 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1111 if (isPowerOf2_64(SHC->getValue())) {
1112 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001113 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
1114 DAG.getConstant(Log2_64(SHC->getValue()),
1115 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001116 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001117 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001118 }
1119 }
1120 }
Nate Begeman69575232005-10-20 02:15:44 +00001121 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +00001122 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
1123 SDOperand Op = BuildUDIV(N);
1124 if (Op.Val) return Op;
1125 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001126 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001127}
1128
Nate Begeman83e75ec2005-09-06 04:43:02 +00001129SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001130 SDOperand N0 = N->getOperand(0);
1131 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001132 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1133 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001134 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001135
1136 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001137 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001138 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001139 // If we know the sign bits of both operands are zero, strength reduce to a
1140 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
1141 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001142 if (TLI.MaskedValueIsZero(N1, SignBit) &&
1143 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +00001144 return DAG.getNode(ISD::UREM, VT, N0, N1);
Chris Lattner26d29902006-10-12 20:58:32 +00001145
1146 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1147 // the remainder operation.
1148 if (N1C && !N1C->isNullValue()) {
1149 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
1150 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1151 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1152 AddToWorkList(Div.Val);
1153 AddToWorkList(Mul.Val);
1154 return Sub;
1155 }
1156
Nate Begeman83e75ec2005-09-06 04:43:02 +00001157 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001158}
1159
Nate Begeman83e75ec2005-09-06 04:43:02 +00001160SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001161 SDOperand N0 = N->getOperand(0);
1162 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001163 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1164 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001165 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001166
1167 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001168 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001169 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001170 // fold (urem x, pow2) -> (and x, pow2-1)
1171 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +00001172 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001173 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1174 if (N1.getOpcode() == ISD::SHL) {
1175 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1176 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +00001177 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001178 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001179 return DAG.getNode(ISD::AND, VT, N0, Add);
1180 }
1181 }
1182 }
Chris Lattner26d29902006-10-12 20:58:32 +00001183
1184 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1185 // the remainder operation.
1186 if (N1C && !N1C->isNullValue()) {
1187 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
1188 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1189 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1190 AddToWorkList(Div.Val);
1191 AddToWorkList(Mul.Val);
1192 return Sub;
1193 }
1194
Nate Begeman83e75ec2005-09-06 04:43:02 +00001195 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001196}
1197
Nate Begeman83e75ec2005-09-06 04:43:02 +00001198SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001199 SDOperand N0 = N->getOperand(0);
1200 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001201 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001202
1203 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001204 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001205 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001206 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001207 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001208 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1209 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001210 TLI.getShiftAmountTy()));
1211 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001212}
1213
Nate Begeman83e75ec2005-09-06 04:43:02 +00001214SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001215 SDOperand N0 = N->getOperand(0);
1216 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001217 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001218
1219 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001220 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001221 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001222 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001223 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001224 return DAG.getConstant(0, N0.getValueType());
1225 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001226}
1227
Chris Lattner35e5c142006-05-05 05:51:50 +00001228/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1229/// two operands of the same opcode, try to simplify it.
1230SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1231 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1232 MVT::ValueType VT = N0.getValueType();
1233 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1234
Chris Lattner540121f2006-05-05 06:31:05 +00001235 // For each of OP in AND/OR/XOR:
1236 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1237 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1238 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001239 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001240 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001241 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001242 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1243 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1244 N0.getOperand(0).getValueType(),
1245 N0.getOperand(0), N1.getOperand(0));
1246 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001247 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001248 }
1249
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001250 // For each of OP in SHL/SRL/SRA/AND...
1251 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1252 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1253 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001254 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001255 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001256 N0.getOperand(1) == N1.getOperand(1)) {
1257 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1258 N0.getOperand(0).getValueType(),
1259 N0.getOperand(0), N1.getOperand(0));
1260 AddToWorkList(ORNode.Val);
1261 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1262 }
1263
1264 return SDOperand();
1265}
1266
Nate Begeman83e75ec2005-09-06 04:43:02 +00001267SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001268 SDOperand N0 = N->getOperand(0);
1269 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001270 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001271 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1272 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001273 MVT::ValueType VT = N1.getValueType();
1274
1275 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001276 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001277 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001278 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001279 if (N0C && !N1C)
1280 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001281 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001282 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001283 return N0;
1284 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +00001285 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001286 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001287 // reassociate and
1288 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1289 if (RAND.Val != 0)
1290 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001291 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001292 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001293 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001294 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001295 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001296 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1297 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001298 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +00001299 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001300 ~N1C->getValue() & InMask)) {
1301 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1302 N0.getOperand(0));
1303
1304 // Replace uses of the AND with uses of the Zero extend node.
1305 CombineTo(N, Zext);
1306
Chris Lattner3603cd62006-02-02 07:17:31 +00001307 // We actually want to replace all uses of the any_extend with the
1308 // zero_extend, to avoid duplicating things. This will later cause this
1309 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001310 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001311 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001312 }
1313 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001314 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1315 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1316 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1317 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1318
1319 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1320 MVT::isInteger(LL.getValueType())) {
1321 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1322 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1323 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001324 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001325 return DAG.getSetCC(VT, ORNode, LR, Op1);
1326 }
1327 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1328 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1329 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001330 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001331 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1332 }
1333 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1334 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1335 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001336 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001337 return DAG.getSetCC(VT, ORNode, LR, Op1);
1338 }
1339 }
1340 // canonicalize equivalent to ll == rl
1341 if (LL == RR && LR == RL) {
1342 Op1 = ISD::getSetCCSwappedOperands(Op1);
1343 std::swap(RL, RR);
1344 }
1345 if (LL == RL && LR == RR) {
1346 bool isInteger = MVT::isInteger(LL.getValueType());
1347 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1348 if (Result != ISD::SETCC_INVALID)
1349 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1350 }
1351 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001352
1353 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1354 if (N0.getOpcode() == N1.getOpcode()) {
1355 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1356 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001357 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001358
Nate Begemande996292006-02-03 22:24:05 +00001359 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1360 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001361 if (!MVT::isVector(VT) &&
1362 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001363 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001364 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Chengc5484282006-10-04 00:56:09 +00001365 if (ISD::isEXTLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001366 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001367 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001368 // If we zero all the possible extended bits, then we can turn this into
1369 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001370 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001371 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001372 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1373 LN0->getBasePtr(), LN0->getSrcValue(),
1374 LN0->getSrcValueOffset(), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001375 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001376 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001377 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001378 }
1379 }
1380 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Chengc5484282006-10-04 00:56:09 +00001381 if (ISD::isSEXTLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001382 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001383 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001384 // If we zero all the possible extended bits, then we can turn this into
1385 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001386 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001387 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001388 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1389 LN0->getBasePtr(), LN0->getSrcValue(),
1390 LN0->getSrcValueOffset(), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001391 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001392 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001393 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001394 }
1395 }
Chris Lattner15045b62006-02-28 06:35:35 +00001396
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001397 // fold (and (load x), 255) -> (zextload x, i8)
1398 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001399 if (N1C && N0.getOpcode() == ISD::LOAD) {
1400 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1401 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
1402 N0.hasOneUse()) {
1403 MVT::ValueType EVT, LoadedVT;
1404 if (N1C->getValue() == 255)
1405 EVT = MVT::i8;
1406 else if (N1C->getValue() == 65535)
1407 EVT = MVT::i16;
1408 else if (N1C->getValue() == ~0U)
1409 EVT = MVT::i32;
1410 else
1411 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001412
Evan Cheng2e49f092006-10-11 07:10:22 +00001413 LoadedVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001414 if (EVT != MVT::Other && LoadedVT > EVT &&
1415 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1416 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1417 // For big endian targets, we need to add an offset to the pointer to
1418 // load the correct bytes. For little endian systems, we merely need to
1419 // read fewer bytes from the same pointer.
1420 unsigned PtrOff =
1421 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1422 SDOperand NewPtr = LN0->getBasePtr();
1423 if (!TLI.isLittleEndian())
1424 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1425 DAG.getConstant(PtrOff, PtrType));
1426 AddToWorkList(NewPtr.Val);
1427 SDOperand Load =
1428 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
1429 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT);
1430 AddToWorkList(N);
1431 CombineTo(N0.Val, Load, Load.getValue(1));
1432 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1433 }
Chris Lattner15045b62006-02-28 06:35:35 +00001434 }
1435 }
1436
Nate Begeman83e75ec2005-09-06 04:43:02 +00001437 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001438}
1439
Nate Begeman83e75ec2005-09-06 04:43:02 +00001440SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001441 SDOperand N0 = N->getOperand(0);
1442 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001443 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001444 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1445 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001446 MVT::ValueType VT = N1.getValueType();
1447 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001448
1449 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001450 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001451 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001452 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001453 if (N0C && !N1C)
1454 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001455 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001456 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001457 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001458 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001459 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001460 return N1;
1461 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001462 if (N1C &&
1463 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001464 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001465 // reassociate or
1466 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1467 if (ROR.Val != 0)
1468 return ROR;
1469 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1470 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001471 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001472 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1473 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1474 N1),
1475 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001476 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001477 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1478 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1479 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1480 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1481
1482 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1483 MVT::isInteger(LL.getValueType())) {
1484 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1485 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1486 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1487 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1488 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001489 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001490 return DAG.getSetCC(VT, ORNode, LR, Op1);
1491 }
1492 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1493 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1494 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1495 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1496 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001497 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001498 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1499 }
1500 }
1501 // canonicalize equivalent to ll == rl
1502 if (LL == RR && LR == RL) {
1503 Op1 = ISD::getSetCCSwappedOperands(Op1);
1504 std::swap(RL, RR);
1505 }
1506 if (LL == RL && LR == RR) {
1507 bool isInteger = MVT::isInteger(LL.getValueType());
1508 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1509 if (Result != ISD::SETCC_INVALID)
1510 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1511 }
1512 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001513
1514 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1515 if (N0.getOpcode() == N1.getOpcode()) {
1516 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1517 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001518 }
Chris Lattner516b9622006-09-14 20:50:57 +00001519
Chris Lattner1ec72732006-09-14 21:11:37 +00001520 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1521 if (N0.getOpcode() == ISD::AND &&
1522 N1.getOpcode() == ISD::AND &&
1523 N0.getOperand(1).getOpcode() == ISD::Constant &&
1524 N1.getOperand(1).getOpcode() == ISD::Constant &&
1525 // Don't increase # computations.
1526 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1527 // We can only do this xform if we know that bits from X that are set in C2
1528 // but not in C1 are already zero. Likewise for Y.
1529 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1530 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1531
1532 if (TLI.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1533 TLI.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
1534 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1535 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1536 }
1537 }
1538
1539
Chris Lattner516b9622006-09-14 20:50:57 +00001540 // See if this is some rotate idiom.
1541 if (SDNode *Rot = MatchRotate(N0, N1))
1542 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001543
Nate Begeman83e75ec2005-09-06 04:43:02 +00001544 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001545}
1546
Chris Lattner516b9622006-09-14 20:50:57 +00001547
1548/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1549static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1550 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001551 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001552 Mask = Op.getOperand(1);
1553 Op = Op.getOperand(0);
1554 } else {
1555 return false;
1556 }
1557 }
1558
1559 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1560 Shift = Op;
1561 return true;
1562 }
1563 return false;
1564}
1565
1566
1567// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1568// idioms for rotate, and if the target supports rotation instructions, generate
1569// a rot[lr].
1570SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1571 // Must be a legal type. Expanded an promoted things won't work with rotates.
1572 MVT::ValueType VT = LHS.getValueType();
1573 if (!TLI.isTypeLegal(VT)) return 0;
1574
1575 // The target must have at least one rotate flavor.
1576 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1577 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1578 if (!HasROTL && !HasROTR) return 0;
1579
1580 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1581 SDOperand LHSShift; // The shift.
1582 SDOperand LHSMask; // AND value if any.
1583 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1584 return 0; // Not part of a rotate.
1585
1586 SDOperand RHSShift; // The shift.
1587 SDOperand RHSMask; // AND value if any.
1588 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1589 return 0; // Not part of a rotate.
1590
1591 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1592 return 0; // Not shifting the same value.
1593
1594 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1595 return 0; // Shifts must disagree.
1596
1597 // Canonicalize shl to left side in a shl/srl pair.
1598 if (RHSShift.getOpcode() == ISD::SHL) {
1599 std::swap(LHS, RHS);
1600 std::swap(LHSShift, RHSShift);
1601 std::swap(LHSMask , RHSMask );
1602 }
1603
1604 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1605
1606 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1607 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
1608 if (LHSShift.getOperand(1).getOpcode() == ISD::Constant &&
1609 RHSShift.getOperand(1).getOpcode() == ISD::Constant) {
1610 uint64_t LShVal = cast<ConstantSDNode>(LHSShift.getOperand(1))->getValue();
1611 uint64_t RShVal = cast<ConstantSDNode>(RHSShift.getOperand(1))->getValue();
1612 if ((LShVal + RShVal) != OpSizeInBits)
1613 return 0;
1614
1615 SDOperand Rot;
1616 if (HasROTL)
1617 Rot = DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1618 LHSShift.getOperand(1));
1619 else
1620 Rot = DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1621 RHSShift.getOperand(1));
1622
1623 // If there is an AND of either shifted operand, apply it to the result.
1624 if (LHSMask.Val || RHSMask.Val) {
1625 uint64_t Mask = MVT::getIntVTBitMask(VT);
1626
1627 if (LHSMask.Val) {
1628 uint64_t RHSBits = (1ULL << LShVal)-1;
1629 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1630 }
1631 if (RHSMask.Val) {
1632 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1633 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1634 }
1635
1636 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1637 }
1638
1639 return Rot.Val;
1640 }
1641
1642 // If there is a mask here, and we have a variable shift, we can't be sure
1643 // that we're masking out the right stuff.
1644 if (LHSMask.Val || RHSMask.Val)
1645 return 0;
1646
1647 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1648 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
1649 if (RHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1650 LHSShift.getOperand(1) == RHSShift.getOperand(1).getOperand(1)) {
1651 if (ConstantSDNode *SUBC =
1652 dyn_cast<ConstantSDNode>(RHSShift.getOperand(1).getOperand(0))) {
1653 if (SUBC->getValue() == OpSizeInBits)
1654 if (HasROTL)
1655 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1656 LHSShift.getOperand(1)).Val;
1657 else
1658 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1659 LHSShift.getOperand(1)).Val;
1660 }
1661 }
1662
1663 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1664 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
1665 if (LHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1666 RHSShift.getOperand(1) == LHSShift.getOperand(1).getOperand(1)) {
1667 if (ConstantSDNode *SUBC =
1668 dyn_cast<ConstantSDNode>(LHSShift.getOperand(1).getOperand(0))) {
1669 if (SUBC->getValue() == OpSizeInBits)
1670 if (HasROTL)
1671 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1672 LHSShift.getOperand(1)).Val;
1673 else
1674 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1675 RHSShift.getOperand(1)).Val;
1676 }
1677 }
1678
1679 return 0;
1680}
1681
1682
Nate Begeman83e75ec2005-09-06 04:43:02 +00001683SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001684 SDOperand N0 = N->getOperand(0);
1685 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001686 SDOperand LHS, RHS, CC;
1687 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1688 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001689 MVT::ValueType VT = N0.getValueType();
1690
1691 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001692 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001693 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001694 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001695 if (N0C && !N1C)
1696 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001697 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001698 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001699 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001700 // reassociate xor
1701 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1702 if (RXOR.Val != 0)
1703 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001704 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001705 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1706 bool isInt = MVT::isInteger(LHS.getValueType());
1707 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1708 isInt);
1709 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001710 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001711 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001712 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001713 assert(0 && "Unhandled SetCC Equivalent!");
1714 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001715 }
Nate Begeman99801192005-09-07 23:25:52 +00001716 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1717 if (N1C && N1C->getValue() == 1 &&
1718 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001719 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001720 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1721 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001722 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1723 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001724 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001725 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001726 }
1727 }
Nate Begeman99801192005-09-07 23:25:52 +00001728 // fold !(x or y) -> (!x and !y) iff x or y are constants
1729 if (N1C && N1C->isAllOnesValue() &&
1730 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001731 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001732 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1733 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001734 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1735 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001736 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001737 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001738 }
1739 }
Nate Begeman223df222005-09-08 20:18:10 +00001740 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1741 if (N1C && N0.getOpcode() == ISD::XOR) {
1742 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1743 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1744 if (N00C)
1745 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1746 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1747 if (N01C)
1748 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1749 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1750 }
1751 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001752 if (N0 == N1) {
1753 if (!MVT::isVector(VT)) {
1754 return DAG.getConstant(0, VT);
1755 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1756 // Produce a vector of zeros.
1757 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1758 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001759 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001760 }
1761 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001762
1763 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1764 if (N0.getOpcode() == N1.getOpcode()) {
1765 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1766 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001767 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001768
Chris Lattner3e104b12006-04-08 04:15:24 +00001769 // Simplify the expression using non-local knowledge.
1770 if (!MVT::isVector(VT) &&
1771 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001772 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001773
Nate Begeman83e75ec2005-09-06 04:43:02 +00001774 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001775}
1776
Nate Begeman83e75ec2005-09-06 04:43:02 +00001777SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001778 SDOperand N0 = N->getOperand(0);
1779 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001780 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1781 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001782 MVT::ValueType VT = N0.getValueType();
1783 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1784
1785 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001786 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001787 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001788 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001789 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001790 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001791 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001792 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001793 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001794 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001795 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001796 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001797 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001798 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001799 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001800 if (SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001801 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001802 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001803 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001804 N0.getOperand(1).getOpcode() == ISD::Constant) {
1805 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001806 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001807 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001808 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001809 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001810 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001811 }
1812 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1813 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001814 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001815 N0.getOperand(1).getOpcode() == ISD::Constant) {
1816 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001817 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001818 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1819 DAG.getConstant(~0ULL << c1, VT));
1820 if (c2 > c1)
1821 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001822 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001823 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001824 return DAG.getNode(ISD::SRL, VT, Mask,
1825 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001826 }
1827 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001828 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001829 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001830 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnercac70592006-03-05 19:53:55 +00001831 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1832 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1833 isa<ConstantSDNode>(N0.getOperand(1))) {
1834 return DAG.getNode(ISD::ADD, VT,
1835 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1836 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1837 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001838 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001839}
1840
Nate Begeman83e75ec2005-09-06 04:43:02 +00001841SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001842 SDOperand N0 = N->getOperand(0);
1843 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001844 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1845 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001846 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001847
1848 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001849 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001850 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001851 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001852 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001853 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001854 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001855 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001856 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001857 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001858 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001859 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001860 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001861 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001862 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001863 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1864 // sext_inreg.
1865 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1866 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1867 MVT::ValueType EVT;
1868 switch (LowBits) {
1869 default: EVT = MVT::Other; break;
1870 case 1: EVT = MVT::i1; break;
1871 case 8: EVT = MVT::i8; break;
1872 case 16: EVT = MVT::i16; break;
1873 case 32: EVT = MVT::i32; break;
1874 }
1875 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1876 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1877 DAG.getValueType(EVT));
1878 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001879
1880 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1881 if (N1C && N0.getOpcode() == ISD::SRA) {
1882 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1883 unsigned Sum = N1C->getValue() + C1->getValue();
1884 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1885 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1886 DAG.getConstant(Sum, N1C->getValueType(0)));
1887 }
1888 }
1889
Chris Lattnera8504462006-05-08 20:51:54 +00001890 // Simplify, based on bits shifted out of the LHS.
1891 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
1892 return SDOperand(N, 0);
1893
1894
Nate Begeman1d4d4142005-09-01 00:19:25 +00001895 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001896 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001897 return DAG.getNode(ISD::SRL, VT, N0, N1);
1898 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001899}
1900
Nate Begeman83e75ec2005-09-06 04:43:02 +00001901SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001902 SDOperand N0 = N->getOperand(0);
1903 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001904 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1905 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001906 MVT::ValueType VT = N0.getValueType();
1907 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1908
1909 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001910 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001911 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001912 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001913 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001914 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001915 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001916 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001917 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001918 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001919 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001920 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001921 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001922 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001923 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001924 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001925 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001926 N0.getOperand(1).getOpcode() == ISD::Constant) {
1927 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001928 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001929 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001930 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001931 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001932 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001933 }
Chris Lattner350bec02006-04-02 06:11:11 +00001934
Chris Lattner06afe072006-05-05 22:53:17 +00001935 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
1936 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1937 // Shifting in all undef bits?
1938 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
1939 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
1940 return DAG.getNode(ISD::UNDEF, VT);
1941
1942 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
1943 AddToWorkList(SmallShift.Val);
1944 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
1945 }
1946
Chris Lattner3657ffe2006-10-12 20:23:19 +00001947 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
1948 // bit, which is unmodified by sra.
1949 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
1950 if (N0.getOpcode() == ISD::SRA)
1951 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
1952 }
1953
Chris Lattner350bec02006-04-02 06:11:11 +00001954 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1955 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1956 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1957 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1958 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
1959
1960 // If any of the input bits are KnownOne, then the input couldn't be all
1961 // zeros, thus the result of the srl will always be zero.
1962 if (KnownOne) return DAG.getConstant(0, VT);
1963
1964 // If all of the bits input the to ctlz node are known to be zero, then
1965 // the result of the ctlz is "32" and the result of the shift is one.
1966 uint64_t UnknownBits = ~KnownZero & Mask;
1967 if (UnknownBits == 0) return DAG.getConstant(1, VT);
1968
1969 // Otherwise, check to see if there is exactly one bit input to the ctlz.
1970 if ((UnknownBits & (UnknownBits-1)) == 0) {
1971 // Okay, we know that only that the single bit specified by UnknownBits
1972 // could be set on input to the CTLZ node. If this bit is set, the SRL
1973 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
1974 // to an SRL,XOR pair, which is likely to simplify more.
1975 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
1976 SDOperand Op = N0.getOperand(0);
1977 if (ShAmt) {
1978 Op = DAG.getNode(ISD::SRL, VT, Op,
1979 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
1980 AddToWorkList(Op.Val);
1981 }
1982 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
1983 }
1984 }
1985
Nate Begeman83e75ec2005-09-06 04:43:02 +00001986 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001987}
1988
Nate Begeman83e75ec2005-09-06 04:43:02 +00001989SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001990 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001991 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001992
1993 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001994 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001995 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001996 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001997}
1998
Nate Begeman83e75ec2005-09-06 04:43:02 +00001999SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002000 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002001 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002002
2003 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002004 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002005 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002006 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002007}
2008
Nate Begeman83e75ec2005-09-06 04:43:02 +00002009SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002010 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002011 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002012
2013 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002014 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002015 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002016 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002017}
2018
Nate Begeman452d7be2005-09-16 00:54:12 +00002019SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2020 SDOperand N0 = N->getOperand(0);
2021 SDOperand N1 = N->getOperand(1);
2022 SDOperand N2 = N->getOperand(2);
2023 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2024 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2025 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2026 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00002027
Nate Begeman452d7be2005-09-16 00:54:12 +00002028 // fold select C, X, X -> X
2029 if (N1 == N2)
2030 return N1;
2031 // fold select true, X, Y -> X
2032 if (N0C && !N0C->isNullValue())
2033 return N1;
2034 // fold select false, X, Y -> Y
2035 if (N0C && N0C->isNullValue())
2036 return N2;
2037 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002038 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002039 return DAG.getNode(ISD::OR, VT, N0, N2);
2040 // fold select C, 0, X -> ~C & X
2041 // FIXME: this should check for C type == X type, not i1?
2042 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
2043 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002044 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002045 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2046 }
2047 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002048 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002049 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002050 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002051 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2052 }
2053 // fold select C, X, 0 -> C & X
2054 // FIXME: this should check for C type == X type, not i1?
2055 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2056 return DAG.getNode(ISD::AND, VT, N0, N1);
2057 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2058 if (MVT::i1 == VT && N0 == N1)
2059 return DAG.getNode(ISD::OR, VT, N0, N2);
2060 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2061 if (MVT::i1 == VT && N0 == N2)
2062 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002063
Chris Lattner40c62d52005-10-18 06:04:22 +00002064 // If we can fold this based on the true/false value, do so.
2065 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002066 return SDOperand(N, 0); // Don't revisit N.
2067
Nate Begeman44728a72005-09-19 22:34:01 +00002068 // fold selects based on a setcc into other things, such as min/max/abs
2069 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00002070 // FIXME:
2071 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2072 // having to say they don't support SELECT_CC on every type the DAG knows
2073 // about, since there is no way to mark an opcode illegal at all value types
2074 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2075 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2076 N1, N2, N0.getOperand(2));
2077 else
2078 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002079 return SDOperand();
2080}
2081
2082SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002083 SDOperand N0 = N->getOperand(0);
2084 SDOperand N1 = N->getOperand(1);
2085 SDOperand N2 = N->getOperand(2);
2086 SDOperand N3 = N->getOperand(3);
2087 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002088 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2089
Nate Begeman44728a72005-09-19 22:34:01 +00002090 // fold select_cc lhs, rhs, x, x, cc -> x
2091 if (N2 == N3)
2092 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002093
Chris Lattner5f42a242006-09-20 06:19:26 +00002094 // Determine if the condition we're dealing with is constant
2095 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002096 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002097
2098 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
2099 if (SCCC->getValue())
2100 return N2; // cond always true -> true val
2101 else
2102 return N3; // cond always false -> false val
2103 }
2104
2105 // Fold to a simpler select_cc
2106 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2107 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2108 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2109 SCC.getOperand(2));
2110
Chris Lattner40c62d52005-10-18 06:04:22 +00002111 // If we can fold this based on the true/false value, do so.
2112 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002113 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002114
Nate Begeman44728a72005-09-19 22:34:01 +00002115 // fold select_cc into other things, such as min/max/abs
2116 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002117}
2118
2119SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2120 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2121 cast<CondCodeSDNode>(N->getOperand(2))->get());
2122}
2123
Nate Begeman83e75ec2005-09-06 04:43:02 +00002124SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002125 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002126 MVT::ValueType VT = N->getValueType(0);
2127
Nate Begeman1d4d4142005-09-01 00:19:25 +00002128 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002129 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002130 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002131
Nate Begeman1d4d4142005-09-01 00:19:25 +00002132 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002133 // fold (sext (aext x)) -> (sext x)
2134 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002135 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002136
Chris Lattner6007b842006-09-21 06:00:20 +00002137 // fold (sext (truncate x)) -> (sextinreg x).
2138 if (N0.getOpcode() == ISD::TRUNCATE &&
Chris Lattnerbf370872006-09-21 06:17:39 +00002139 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2140 N0.getValueType()))) {
Chris Lattner6007b842006-09-21 06:00:20 +00002141 SDOperand Op = N0.getOperand(0);
2142 if (Op.getValueType() < VT) {
2143 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2144 } else if (Op.getValueType() > VT) {
2145 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2146 }
2147 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00002148 DAG.getValueType(N0.getValueType()));
Chris Lattner6007b842006-09-21 06:00:20 +00002149 }
Chris Lattner310b5782006-05-06 23:06:26 +00002150
Evan Cheng110dec22005-12-14 02:19:23 +00002151 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002152 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002153 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng466685d2006-10-09 20:57:25 +00002154 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2155 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2156 LN0->getBasePtr(), LN0->getSrcValue(),
2157 LN0->getSrcValueOffset(),
Nate Begeman3df4d522005-10-12 20:40:40 +00002158 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00002159 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00002160 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2161 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002162 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002163 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002164
2165 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2166 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Chengc5484282006-10-04 00:56:09 +00002167 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002168 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002169 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002170 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2171 LN0->getBasePtr(), LN0->getSrcValue(),
2172 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002173 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002174 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2175 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002176 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002177 }
2178
Nate Begeman83e75ec2005-09-06 04:43:02 +00002179 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002180}
2181
Nate Begeman83e75ec2005-09-06 04:43:02 +00002182SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002183 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002184 MVT::ValueType VT = N->getValueType(0);
2185
Nate Begeman1d4d4142005-09-01 00:19:25 +00002186 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002187 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002188 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002189 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002190 // fold (zext (aext x)) -> (zext x)
2191 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002192 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002193
2194 // fold (zext (truncate x)) -> (and x, mask)
2195 if (N0.getOpcode() == ISD::TRUNCATE &&
2196 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2197 SDOperand Op = N0.getOperand(0);
2198 if (Op.getValueType() < VT) {
2199 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2200 } else if (Op.getValueType() > VT) {
2201 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2202 }
2203 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2204 }
2205
Chris Lattner111c2282006-09-21 06:14:31 +00002206 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2207 if (N0.getOpcode() == ISD::AND &&
2208 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2209 N0.getOperand(1).getOpcode() == ISD::Constant) {
2210 SDOperand X = N0.getOperand(0).getOperand(0);
2211 if (X.getValueType() < VT) {
2212 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2213 } else if (X.getValueType() > VT) {
2214 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2215 }
2216 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2217 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2218 }
2219
Evan Cheng110dec22005-12-14 02:19:23 +00002220 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002221 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002222 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002223 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2224 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2225 LN0->getBasePtr(), LN0->getSrcValue(),
2226 LN0->getSrcValueOffset(),
Evan Cheng110dec22005-12-14 02:19:23 +00002227 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00002228 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00002229 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2230 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002231 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00002232 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002233
2234 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2235 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Chengc5484282006-10-04 00:56:09 +00002236 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002237 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002238 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002239 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2240 LN0->getBasePtr(), LN0->getSrcValue(),
2241 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002242 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002243 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2244 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002245 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002246 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002247 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002248}
2249
Chris Lattner5ffc0662006-05-05 05:58:59 +00002250SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2251 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002252 MVT::ValueType VT = N->getValueType(0);
2253
2254 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002255 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002256 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2257 // fold (aext (aext x)) -> (aext x)
2258 // fold (aext (zext x)) -> (zext x)
2259 // fold (aext (sext x)) -> (sext x)
2260 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2261 N0.getOpcode() == ISD::ZERO_EXTEND ||
2262 N0.getOpcode() == ISD::SIGN_EXTEND)
2263 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2264
Chris Lattner84750582006-09-20 06:29:17 +00002265 // fold (aext (truncate x))
2266 if (N0.getOpcode() == ISD::TRUNCATE) {
2267 SDOperand TruncOp = N0.getOperand(0);
2268 if (TruncOp.getValueType() == VT)
2269 return TruncOp; // x iff x size == zext size.
2270 if (TruncOp.getValueType() > VT)
2271 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2272 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2273 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002274
2275 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2276 if (N0.getOpcode() == ISD::AND &&
2277 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2278 N0.getOperand(1).getOpcode() == ISD::Constant) {
2279 SDOperand X = N0.getOperand(0).getOperand(0);
2280 if (X.getValueType() < VT) {
2281 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2282 } else if (X.getValueType() > VT) {
2283 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2284 }
2285 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2286 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2287 }
2288
Chris Lattner5ffc0662006-05-05 05:58:59 +00002289 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002290 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002291 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002292 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2293 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2294 LN0->getBasePtr(), LN0->getSrcValue(),
2295 LN0->getSrcValueOffset(),
Chris Lattner5ffc0662006-05-05 05:58:59 +00002296 N0.getValueType());
2297 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
2303 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2304 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2305 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002306 if (N0.getOpcode() == ISD::LOAD && !ISD::isNON_EXTLoad(N0.Val) &&
2307 N0.hasOneUse()) {
2308 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002309 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002310 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
2311 LN0->getChain(), LN0->getBasePtr(),
2312 LN0->getSrcValue(),
2313 LN0->getSrcValueOffset(), EVT);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002314 CombineTo(N, ExtLoad);
2315 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2316 ExtLoad.getValue(1));
2317 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2318 }
2319 return SDOperand();
2320}
2321
2322
Nate Begeman83e75ec2005-09-06 04:43:02 +00002323SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002324 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002325 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002326 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002327 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00002328 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002329
Nate Begeman1d4d4142005-09-01 00:19:25 +00002330 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00002331 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00002332 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00002333
Chris Lattner541a24f2006-05-06 22:43:44 +00002334 // If the input is already sign extended, just drop the extension.
Chris Lattneree4ea922006-05-06 09:30:03 +00002335 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
2336 return N0;
2337
Nate Begeman646d7e22005-09-02 21:18:40 +00002338 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
2339 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2340 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00002341 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002342 }
Chris Lattner4b37e872006-05-08 21:18:59 +00002343
Nate Begeman07ed4172005-10-10 21:26:48 +00002344 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00002345 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00002346 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00002347
2348 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
2349 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
2350 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
2351 if (N0.getOpcode() == ISD::SRL) {
2352 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2353 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
2354 // We can turn this into an SRA iff the input to the SRL is already sign
2355 // extended enough.
2356 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
2357 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
2358 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
2359 }
2360 }
2361
Nate Begemanded49632005-10-13 03:11:28 +00002362 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00002363 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002364 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002365 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002366 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2367 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2368 LN0->getBasePtr(), LN0->getSrcValue(),
2369 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002370 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002371 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002372 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002373 }
2374 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Chengc5484282006-10-04 00:56:09 +00002375 if (ISD::isZEXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002376 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002377 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002378 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2379 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2380 LN0->getBasePtr(), LN0->getSrcValue(),
2381 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002382 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002383 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002384 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002385 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002386 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002387}
2388
Nate Begeman83e75ec2005-09-06 04:43:02 +00002389SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002390 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002391 MVT::ValueType VT = N->getValueType(0);
2392
2393 // noop truncate
2394 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002395 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002396 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002397 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002398 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002399 // fold (truncate (truncate x)) -> (truncate x)
2400 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002401 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002402 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00002403 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2404 N0.getOpcode() == ISD::ANY_EXTEND) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002405 if (N0.getValueType() < VT)
2406 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00002407 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002408 else if (N0.getValueType() > VT)
2409 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002410 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002411 else
2412 // if the source and dest are the same type, we can drop both the extend
2413 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002414 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002415 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002416 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng466685d2006-10-09 20:57:25 +00002417 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00002418 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
2419 "Cannot truncate to larger type!");
Evan Cheng466685d2006-10-09 20:57:25 +00002420 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Nate Begeman3df4d522005-10-12 20:40:40 +00002421 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00002422 // For big endian targets, we need to add an offset to the pointer to load
2423 // the correct bytes. For little endian systems, we merely need to read
2424 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00002425 uint64_t PtrOff =
2426 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Evan Cheng466685d2006-10-09 20:57:25 +00002427 SDOperand NewPtr = TLI.isLittleEndian() ? LN0->getBasePtr() :
2428 DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Nate Begeman765784a2005-10-12 23:18:53 +00002429 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00002430 AddToWorkList(NewPtr.Val);
Evan Cheng466685d2006-10-09 20:57:25 +00002431 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), NewPtr,
2432 LN0->getSrcValue(), LN0->getSrcValueOffset());
Chris Lattner5750df92006-03-01 04:03:14 +00002433 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00002434 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002435 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002436 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002437 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002438}
2439
Chris Lattner94683772005-12-23 05:30:37 +00002440SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2441 SDOperand N0 = N->getOperand(0);
2442 MVT::ValueType VT = N->getValueType(0);
2443
2444 // If the input is a constant, let getNode() fold it.
2445 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2446 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2447 if (Res.Val != N) return Res;
2448 }
2449
Chris Lattnerc8547d82005-12-23 05:37:50 +00002450 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2451 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00002452
Chris Lattner57104102005-12-23 05:44:41 +00002453 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002454 // FIXME: These xforms need to know that the resultant load doesn't need a
2455 // higher alignment than the original!
Evan Cheng466685d2006-10-09 20:57:25 +00002456 if (0 && ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse()) {
2457 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2458 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
2459 LN0->getSrcValue(), LN0->getSrcValueOffset());
Chris Lattner5750df92006-03-01 04:03:14 +00002460 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00002461 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2462 Load.getValue(1));
2463 return Load;
2464 }
2465
Chris Lattner94683772005-12-23 05:30:37 +00002466 return SDOperand();
2467}
2468
Chris Lattner6258fb22006-04-02 02:53:43 +00002469SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2470 SDOperand N0 = N->getOperand(0);
2471 MVT::ValueType VT = N->getValueType(0);
2472
2473 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2474 // First check to see if this is all constant.
2475 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2476 VT == MVT::Vector) {
2477 bool isSimple = true;
2478 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2479 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2480 N0.getOperand(i).getOpcode() != ISD::Constant &&
2481 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2482 isSimple = false;
2483 break;
2484 }
2485
Chris Lattner97c20732006-04-03 17:29:28 +00002486 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2487 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002488 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2489 }
2490 }
2491
2492 return SDOperand();
2493}
2494
2495/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2496/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2497/// destination element value type.
2498SDOperand DAGCombiner::
2499ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2500 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2501
2502 // If this is already the right type, we're done.
2503 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2504
2505 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2506 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2507
2508 // If this is a conversion of N elements of one type to N elements of another
2509 // type, convert each element. This handles FP<->INT cases.
2510 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002511 SmallVector<SDOperand, 8> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00002512 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002513 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002514 AddToWorkList(Ops.back().Val);
2515 }
Chris Lattner6258fb22006-04-02 02:53:43 +00002516 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2517 Ops.push_back(DAG.getValueType(DstEltVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002518 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002519 }
2520
2521 // Otherwise, we're growing or shrinking the elements. To avoid having to
2522 // handle annoying details of growing/shrinking FP values, we convert them to
2523 // int first.
2524 if (MVT::isFloatingPoint(SrcEltVT)) {
2525 // Convert the input float vector to a int vector where the elements are the
2526 // same sizes.
2527 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2528 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2529 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2530 SrcEltVT = IntVT;
2531 }
2532
2533 // Now we know the input is an integer vector. If the output is a FP type,
2534 // convert to integer first, then to FP of the right size.
2535 if (MVT::isFloatingPoint(DstEltVT)) {
2536 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2537 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2538 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2539
2540 // Next, convert to FP elements of the same size.
2541 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2542 }
2543
2544 // Okay, we know the src/dst types are both integers of differing types.
2545 // Handling growing first.
2546 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2547 if (SrcBitSize < DstBitSize) {
2548 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2549
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002550 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002551 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2552 i += NumInputsPerOutput) {
2553 bool isLE = TLI.isLittleEndian();
2554 uint64_t NewBits = 0;
2555 bool EltIsUndef = true;
2556 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2557 // Shift the previously computed bits over.
2558 NewBits <<= SrcBitSize;
2559 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2560 if (Op.getOpcode() == ISD::UNDEF) continue;
2561 EltIsUndef = false;
2562
2563 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2564 }
2565
2566 if (EltIsUndef)
2567 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2568 else
2569 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2570 }
2571
2572 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2573 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002574 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002575 }
2576
2577 // Finally, this must be the case where we are shrinking elements: each input
2578 // turns into multiple outputs.
2579 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002580 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002581 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2582 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2583 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2584 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2585 continue;
2586 }
2587 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2588
2589 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2590 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2591 OpVal >>= DstBitSize;
2592 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2593 }
2594
2595 // For big endian targets, swap the order of the pieces of each element.
2596 if (!TLI.isLittleEndian())
2597 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2598 }
2599 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2600 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002601 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002602}
2603
2604
2605
Chris Lattner01b3d732005-09-28 22:28:18 +00002606SDOperand DAGCombiner::visitFADD(SDNode *N) {
2607 SDOperand N0 = N->getOperand(0);
2608 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002609 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2610 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002611 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002612
2613 // fold (fadd c1, c2) -> c1+c2
2614 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002615 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002616 // canonicalize constant to RHS
2617 if (N0CFP && !N1CFP)
2618 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002619 // fold (A + (-B)) -> A-B
2620 if (N1.getOpcode() == ISD::FNEG)
2621 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002622 // fold ((-A) + B) -> B-A
2623 if (N0.getOpcode() == ISD::FNEG)
2624 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002625 return SDOperand();
2626}
2627
2628SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2629 SDOperand N0 = N->getOperand(0);
2630 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002631 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2632 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002633 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002634
2635 // fold (fsub c1, c2) -> c1-c2
2636 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002637 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002638 // fold (A-(-B)) -> A+B
2639 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00002640 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002641 return SDOperand();
2642}
2643
2644SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2645 SDOperand N0 = N->getOperand(0);
2646 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002647 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2648 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002649 MVT::ValueType VT = N->getValueType(0);
2650
Nate Begeman11af4ea2005-10-17 20:40:11 +00002651 // fold (fmul c1, c2) -> c1*c2
2652 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002653 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002654 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002655 if (N0CFP && !N1CFP)
2656 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002657 // fold (fmul X, 2.0) -> (fadd X, X)
2658 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2659 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002660 return SDOperand();
2661}
2662
2663SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2664 SDOperand N0 = N->getOperand(0);
2665 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002666 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2667 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002668 MVT::ValueType VT = N->getValueType(0);
2669
Nate Begemana148d982006-01-18 22:35:16 +00002670 // fold (fdiv c1, c2) -> c1/c2
2671 if (N0CFP && N1CFP)
2672 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002673 return SDOperand();
2674}
2675
2676SDOperand DAGCombiner::visitFREM(SDNode *N) {
2677 SDOperand N0 = N->getOperand(0);
2678 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002679 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2680 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002681 MVT::ValueType VT = N->getValueType(0);
2682
Nate Begemana148d982006-01-18 22:35:16 +00002683 // fold (frem c1, c2) -> fmod(c1,c2)
2684 if (N0CFP && N1CFP)
2685 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002686 return SDOperand();
2687}
2688
Chris Lattner12d83032006-03-05 05:30:57 +00002689SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2690 SDOperand N0 = N->getOperand(0);
2691 SDOperand N1 = N->getOperand(1);
2692 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2693 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2694 MVT::ValueType VT = N->getValueType(0);
2695
2696 if (N0CFP && N1CFP) // Constant fold
2697 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2698
2699 if (N1CFP) {
2700 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2701 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2702 union {
2703 double d;
2704 int64_t i;
2705 } u;
2706 u.d = N1CFP->getValue();
2707 if (u.i >= 0)
2708 return DAG.getNode(ISD::FABS, VT, N0);
2709 else
2710 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2711 }
2712
2713 // copysign(fabs(x), y) -> copysign(x, y)
2714 // copysign(fneg(x), y) -> copysign(x, y)
2715 // copysign(copysign(x,z), y) -> copysign(x, y)
2716 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2717 N0.getOpcode() == ISD::FCOPYSIGN)
2718 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2719
2720 // copysign(x, abs(y)) -> abs(x)
2721 if (N1.getOpcode() == ISD::FABS)
2722 return DAG.getNode(ISD::FABS, VT, N0);
2723
2724 // copysign(x, copysign(y,z)) -> copysign(x, z)
2725 if (N1.getOpcode() == ISD::FCOPYSIGN)
2726 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2727
2728 // copysign(x, fp_extend(y)) -> copysign(x, y)
2729 // copysign(x, fp_round(y)) -> copysign(x, y)
2730 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2731 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2732
2733 return SDOperand();
2734}
2735
2736
Chris Lattner01b3d732005-09-28 22:28:18 +00002737
Nate Begeman83e75ec2005-09-06 04:43:02 +00002738SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002739 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002740 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002741 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002742
2743 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002744 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002745 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002746 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002747}
2748
Nate Begeman83e75ec2005-09-06 04:43:02 +00002749SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002750 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002751 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002752 MVT::ValueType VT = N->getValueType(0);
2753
Nate Begeman1d4d4142005-09-01 00:19:25 +00002754 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002755 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002756 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002757 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002758}
2759
Nate Begeman83e75ec2005-09-06 04:43:02 +00002760SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002761 SDOperand N0 = N->getOperand(0);
2762 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2763 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002764
2765 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002766 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002767 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002768 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002769}
2770
Nate Begeman83e75ec2005-09-06 04:43:02 +00002771SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002772 SDOperand N0 = N->getOperand(0);
2773 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2774 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002775
2776 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002777 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002778 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002779 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002780}
2781
Nate Begeman83e75ec2005-09-06 04:43:02 +00002782SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002783 SDOperand N0 = N->getOperand(0);
2784 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2785 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002786
2787 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002788 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002789 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002790
2791 // fold (fp_round (fp_extend x)) -> x
2792 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2793 return N0.getOperand(0);
2794
2795 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2796 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2797 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2798 AddToWorkList(Tmp.Val);
2799 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2800 }
2801
Nate Begeman83e75ec2005-09-06 04:43:02 +00002802 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002803}
2804
Nate Begeman83e75ec2005-09-06 04:43:02 +00002805SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002806 SDOperand N0 = N->getOperand(0);
2807 MVT::ValueType VT = N->getValueType(0);
2808 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002809 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002810
Nate Begeman1d4d4142005-09-01 00:19:25 +00002811 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002812 if (N0CFP) {
2813 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002814 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002815 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002816 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002817}
2818
Nate Begeman83e75ec2005-09-06 04:43:02 +00002819SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002820 SDOperand N0 = N->getOperand(0);
2821 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2822 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002823
2824 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002825 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002826 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00002827
2828 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002829 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002830 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002831 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2832 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2833 LN0->getBasePtr(), LN0->getSrcValue(),
2834 LN0->getSrcValueOffset(),
Chris Lattnere564dbb2006-05-05 21:34:35 +00002835 N0.getValueType());
2836 CombineTo(N, ExtLoad);
2837 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
2838 ExtLoad.getValue(1));
2839 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2840 }
2841
2842
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::visitFNEG(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
2851 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002852 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002853 return DAG.getNode(ISD::FNEG, VT, N0);
2854 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002855 if (N0.getOpcode() == ISD::SUB)
2856 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002857 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002858 if (N0.getOpcode() == ISD::FNEG)
2859 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002860 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002861}
2862
Nate Begeman83e75ec2005-09-06 04:43:02 +00002863SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002864 SDOperand N0 = N->getOperand(0);
2865 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2866 MVT::ValueType VT = N->getValueType(0);
2867
Nate Begeman1d4d4142005-09-01 00:19:25 +00002868 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002869 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002870 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002871 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002872 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002873 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002874 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002875 // fold (fabs (fcopysign x, y)) -> (fabs x)
2876 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2877 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2878
Nate Begeman83e75ec2005-09-06 04:43:02 +00002879 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002880}
2881
Nate Begeman44728a72005-09-19 22:34:01 +00002882SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2883 SDOperand Chain = N->getOperand(0);
2884 SDOperand N1 = N->getOperand(1);
2885 SDOperand N2 = N->getOperand(2);
2886 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2887
2888 // never taken branch, fold to chain
2889 if (N1C && N1C->isNullValue())
2890 return Chain;
2891 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002892 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002893 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002894 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2895 // on the target.
2896 if (N1.getOpcode() == ISD::SETCC &&
2897 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2898 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2899 N1.getOperand(0), N1.getOperand(1), N2);
2900 }
Nate Begeman44728a72005-09-19 22:34:01 +00002901 return SDOperand();
2902}
2903
Chris Lattner3ea0b472005-10-05 06:47:48 +00002904// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2905//
Nate Begeman44728a72005-09-19 22:34:01 +00002906SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002907 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2908 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2909
2910 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002911 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002912 if (Simp.Val) AddToWorkList(Simp.Val);
2913
Nate Begemane17daeb2005-10-05 21:43:42 +00002914 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2915
2916 // fold br_cc true, dest -> br dest (unconditional branch)
2917 if (SCCC && SCCC->getValue())
2918 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2919 N->getOperand(4));
2920 // fold br_cc false, dest -> unconditional fall through
2921 if (SCCC && SCCC->isNullValue())
2922 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00002923
Nate Begemane17daeb2005-10-05 21:43:42 +00002924 // fold to a simpler setcc
2925 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2926 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2927 Simp.getOperand(2), Simp.getOperand(0),
2928 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002929 return SDOperand();
2930}
2931
Chris Lattner01a22022005-10-10 22:04:48 +00002932SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00002933 LoadSDNode *LD = cast<LoadSDNode>(N);
2934 SDOperand Chain = LD->getChain();
2935 SDOperand Ptr = LD->getBasePtr();
Jim Laskey6ff23e52006-10-04 16:53:27 +00002936
Chris Lattnere4b95392006-03-31 18:06:18 +00002937 // If there are no uses of the loaded value, change uses of the chain value
2938 // into uses of the chain input (i.e. delete the dead load).
2939 if (N->hasNUsesOfValue(0, 0))
2940 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
Chris Lattner01a22022005-10-10 22:04:48 +00002941
2942 // If this load is directly stored, replace the load value with the stored
2943 // value.
2944 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002945 // TODO: Handle TRUNCSTORE/LOADEXT
2946 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002947 if (ISD::isNON_TRUNCStore(Chain.Val)) {
2948 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
2949 if (PrevST->getBasePtr() == Ptr &&
2950 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002951 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002952 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002953 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00002954
Jim Laskey7ca56af2006-10-11 13:47:09 +00002955 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00002956 // Walk up chain skipping non-aliasing memory nodes.
2957 SDOperand BetterChain = FindBetterChain(N, Chain);
2958
Jim Laskey6ff23e52006-10-04 16:53:27 +00002959 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00002960 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002961 SDOperand ReplLoad;
2962
Jim Laskey279f0532006-09-25 16:29:54 +00002963 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002964 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
2965 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
2966 LD->getSrcValue(), LD->getSrcValueOffset());
2967 } else {
2968 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
2969 LD->getValueType(0),
2970 BetterChain, Ptr, LD->getSrcValue(),
2971 LD->getSrcValueOffset(),
2972 LD->getLoadedVT());
2973 }
Jim Laskey279f0532006-09-25 16:29:54 +00002974
Jim Laskey6ff23e52006-10-04 16:53:27 +00002975 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00002976 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
2977 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00002978
Jim Laskey274062c2006-10-13 23:32:28 +00002979 // Replace uses with load result and token factor. Don't add users
2980 // to work list.
2981 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00002982 }
2983 }
2984
Evan Cheng7fc033a2006-11-03 03:06:21 +00002985 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00002986 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00002987 return SDOperand(N, 0);
2988
Chris Lattner01a22022005-10-10 22:04:48 +00002989 return SDOperand();
2990}
2991
Chris Lattner87514ca2005-10-10 22:31:19 +00002992SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002993 StoreSDNode *ST = cast<StoreSDNode>(N);
2994 SDOperand Chain = ST->getChain();
2995 SDOperand Value = ST->getValue();
2996 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00002997
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002998 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002999 // FIXME: This needs to know that the resultant store does not need a
3000 // higher alignment than the original.
Jim Laskey14fbcbf2006-09-25 21:11:32 +00003001 if (0 && Value.getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003002 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
3003 ST->getSrcValueOffset());
Jim Laskey279f0532006-09-25 16:29:54 +00003004 }
3005
3006 if (CombinerAA) {
3007 // Walk up chain skipping non-aliasing memory nodes.
3008 SDOperand BetterChain = FindBetterChain(N, Chain);
3009
Jim Laskey6ff23e52006-10-04 16:53:27 +00003010 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00003011 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00003012 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00003013 SDOperand ReplStore;
3014 if (ST->isTruncatingStore()) {
3015 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
3016 ST->getSrcValue(),ST->getSrcValueOffset(), ST->getStoredVT());
3017 } else {
3018 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
3019 ST->getSrcValue(), ST->getSrcValueOffset());
3020 }
3021
Jim Laskey279f0532006-09-25 16:29:54 +00003022 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00003023 SDOperand Token =
3024 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
3025
3026 // Don't add users to work list.
3027 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003028 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00003029 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00003030
Evan Cheng33dbedc2006-11-05 09:31:14 +00003031 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003032 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00003033 return SDOperand(N, 0);
3034
Chris Lattner87514ca2005-10-10 22:31:19 +00003035 return SDOperand();
3036}
3037
Chris Lattnerca242442006-03-19 01:27:56 +00003038SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
3039 SDOperand InVec = N->getOperand(0);
3040 SDOperand InVal = N->getOperand(1);
3041 SDOperand EltNo = N->getOperand(2);
3042
3043 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
3044 // vector with the inserted element.
3045 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
3046 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003047 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00003048 if (Elt < Ops.size())
3049 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003050 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
3051 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00003052 }
3053
3054 return SDOperand();
3055}
3056
3057SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
3058 SDOperand InVec = N->getOperand(0);
3059 SDOperand InVal = N->getOperand(1);
3060 SDOperand EltNo = N->getOperand(2);
3061 SDOperand NumElts = N->getOperand(3);
3062 SDOperand EltType = N->getOperand(4);
3063
3064 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
3065 // vector with the inserted element.
3066 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
3067 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003068 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00003069 if (Elt < Ops.size()-2)
3070 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003071 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
3072 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00003073 }
3074
3075 return SDOperand();
3076}
3077
Chris Lattnerd7648c82006-03-28 20:28:38 +00003078SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
3079 unsigned NumInScalars = N->getNumOperands()-2;
3080 SDOperand NumElts = N->getOperand(NumInScalars);
3081 SDOperand EltType = N->getOperand(NumInScalars+1);
3082
3083 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
3084 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
3085 // two distinct vectors, turn this into a shuffle node.
3086 SDOperand VecIn1, VecIn2;
3087 for (unsigned i = 0; i != NumInScalars; ++i) {
3088 // Ignore undef inputs.
3089 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3090
3091 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
3092 // constant index, bail out.
3093 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
3094 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
3095 VecIn1 = VecIn2 = SDOperand(0, 0);
3096 break;
3097 }
3098
3099 // If the input vector type disagrees with the result of the vbuild_vector,
3100 // we can't make a shuffle.
3101 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
3102 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
3103 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
3104 VecIn1 = VecIn2 = SDOperand(0, 0);
3105 break;
3106 }
3107
3108 // Otherwise, remember this. We allow up to two distinct input vectors.
3109 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
3110 continue;
3111
3112 if (VecIn1.Val == 0) {
3113 VecIn1 = ExtractedFromVec;
3114 } else if (VecIn2.Val == 0) {
3115 VecIn2 = ExtractedFromVec;
3116 } else {
3117 // Too many inputs.
3118 VecIn1 = VecIn2 = SDOperand(0, 0);
3119 break;
3120 }
3121 }
3122
3123 // If everything is good, we can make a shuffle operation.
3124 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003125 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00003126 for (unsigned i = 0; i != NumInScalars; ++i) {
3127 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
3128 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3129 continue;
3130 }
3131
3132 SDOperand Extract = N->getOperand(i);
3133
3134 // If extracting from the first vector, just use the index directly.
3135 if (Extract.getOperand(0) == VecIn1) {
3136 BuildVecIndices.push_back(Extract.getOperand(1));
3137 continue;
3138 }
3139
3140 // Otherwise, use InIdx + VecSize
3141 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
3142 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
3143 }
3144
3145 // Add count and size info.
3146 BuildVecIndices.push_back(NumElts);
3147 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
3148
3149 // Return the new VVECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003150 SDOperand Ops[5];
3151 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00003152 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003153 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00003154 } else {
3155 // Use an undef vbuild_vector as input for the second operand.
3156 std::vector<SDOperand> UnOps(NumInScalars,
3157 DAG.getNode(ISD::UNDEF,
3158 cast<VTSDNode>(EltType)->getVT()));
3159 UnOps.push_back(NumElts);
3160 UnOps.push_back(EltType);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003161 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3162 &UnOps[0], UnOps.size());
3163 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00003164 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003165 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3166 &BuildVecIndices[0], BuildVecIndices.size());
3167 Ops[3] = NumElts;
3168 Ops[4] = EltType;
3169 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
Chris Lattnerd7648c82006-03-28 20:28:38 +00003170 }
3171
3172 return SDOperand();
3173}
3174
Chris Lattner66445d32006-03-28 22:11:53 +00003175SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003176 SDOperand ShufMask = N->getOperand(2);
3177 unsigned NumElts = ShufMask.getNumOperands();
3178
3179 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3180 bool isIdentity = true;
3181 for (unsigned i = 0; i != NumElts; ++i) {
3182 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3183 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3184 isIdentity = false;
3185 break;
3186 }
3187 }
3188 if (isIdentity) return N->getOperand(0);
3189
3190 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3191 isIdentity = true;
3192 for (unsigned i = 0; i != NumElts; ++i) {
3193 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3194 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3195 isIdentity = false;
3196 break;
3197 }
3198 }
3199 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00003200
3201 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3202 // needed at all.
3203 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003204 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003205 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003206 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003207 for (unsigned i = 0; i != NumElts; ++i)
3208 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3209 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3210 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003211 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003212 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003213 BaseIdx = Idx;
3214 } else {
3215 if (BaseIdx != Idx)
3216 isSplat = false;
3217 if (VecNum != V) {
3218 isUnary = false;
3219 break;
3220 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003221 }
3222 }
3223
3224 SDOperand N0 = N->getOperand(0);
3225 SDOperand N1 = N->getOperand(1);
3226 // Normalize unary shuffle so the RHS is undef.
3227 if (isUnary && VecNum == 1)
3228 std::swap(N0, N1);
3229
Evan Cheng917ec982006-07-21 08:25:53 +00003230 // If it is a splat, check if the argument vector is a build_vector with
3231 // all scalar elements the same.
3232 if (isSplat) {
3233 SDNode *V = N0.Val;
3234 if (V->getOpcode() == ISD::BIT_CONVERT)
3235 V = V->getOperand(0).Val;
3236 if (V->getOpcode() == ISD::BUILD_VECTOR) {
3237 unsigned NumElems = V->getNumOperands()-2;
3238 if (NumElems > BaseIdx) {
3239 SDOperand Base;
3240 bool AllSame = true;
3241 for (unsigned i = 0; i != NumElems; ++i) {
3242 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3243 Base = V->getOperand(i);
3244 break;
3245 }
3246 }
3247 // Splat of <u, u, u, u>, return <u, u, u, u>
3248 if (!Base.Val)
3249 return N0;
3250 for (unsigned i = 0; i != NumElems; ++i) {
3251 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3252 V->getOperand(i) != Base) {
3253 AllSame = false;
3254 break;
3255 }
3256 }
3257 // Splat of <x, x, x, x>, return <x, x, x, x>
3258 if (AllSame)
3259 return N0;
3260 }
3261 }
3262 }
3263
Evan Chenge7bec0d2006-07-20 22:44:41 +00003264 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3265 // into an undef.
3266 if (isUnary || N0 == N1) {
3267 if (N0.getOpcode() == ISD::UNDEF)
Evan Chengc04766a2006-04-06 23:20:43 +00003268 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00003269 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3270 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003271 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00003272 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00003273 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3274 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3275 MappedOps.push_back(ShufMask.getOperand(i));
3276 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00003277 unsigned NewIdx =
3278 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3279 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00003280 }
3281 }
3282 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003283 &MappedOps[0], MappedOps.size());
Chris Lattner3e104b12006-04-08 04:15:24 +00003284 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00003285 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
Evan Chenge7bec0d2006-07-20 22:44:41 +00003286 N0,
Chris Lattner66445d32006-03-28 22:11:53 +00003287 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
3288 ShufMask);
3289 }
3290
3291 return SDOperand();
3292}
3293
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003294SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
3295 SDOperand ShufMask = N->getOperand(2);
3296 unsigned NumElts = ShufMask.getNumOperands()-2;
3297
3298 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3299 bool isIdentity = true;
3300 for (unsigned i = 0; i != NumElts; ++i) {
3301 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3302 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3303 isIdentity = false;
3304 break;
3305 }
3306 }
3307 if (isIdentity) return N->getOperand(0);
3308
3309 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3310 isIdentity = true;
3311 for (unsigned i = 0; i != NumElts; ++i) {
3312 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3313 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3314 isIdentity = false;
3315 break;
3316 }
3317 }
3318 if (isIdentity) return N->getOperand(1);
3319
Evan Chenge7bec0d2006-07-20 22:44:41 +00003320 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3321 // needed at all.
3322 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003323 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003324 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003325 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003326 for (unsigned i = 0; i != NumElts; ++i)
3327 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3328 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3329 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003330 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003331 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003332 BaseIdx = Idx;
3333 } else {
3334 if (BaseIdx != Idx)
3335 isSplat = false;
3336 if (VecNum != V) {
3337 isUnary = false;
3338 break;
3339 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003340 }
3341 }
3342
3343 SDOperand N0 = N->getOperand(0);
3344 SDOperand N1 = N->getOperand(1);
3345 // Normalize unary shuffle so the RHS is undef.
3346 if (isUnary && VecNum == 1)
3347 std::swap(N0, N1);
3348
Evan Cheng917ec982006-07-21 08:25:53 +00003349 // If it is a splat, check if the argument vector is a build_vector with
3350 // all scalar elements the same.
3351 if (isSplat) {
3352 SDNode *V = N0.Val;
Evan Cheng59569222006-10-16 22:49:37 +00003353
3354 // If this is a vbit convert that changes the element type of the vector but
3355 // not the number of vector elements, look through it. Be careful not to
3356 // look though conversions that change things like v4f32 to v2f64.
3357 if (V->getOpcode() == ISD::VBIT_CONVERT) {
3358 SDOperand ConvInput = V->getOperand(0);
Evan Cheng5d04a1a2006-10-17 17:06:35 +00003359 if (ConvInput.getValueType() == MVT::Vector &&
3360 NumElts ==
Evan Cheng59569222006-10-16 22:49:37 +00003361 ConvInput.getConstantOperandVal(ConvInput.getNumOperands()-2))
3362 V = ConvInput.Val;
3363 }
3364
Evan Cheng917ec982006-07-21 08:25:53 +00003365 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
3366 unsigned NumElems = V->getNumOperands()-2;
3367 if (NumElems > BaseIdx) {
3368 SDOperand Base;
3369 bool AllSame = true;
3370 for (unsigned i = 0; i != NumElems; ++i) {
3371 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3372 Base = V->getOperand(i);
3373 break;
3374 }
3375 }
3376 // Splat of <u, u, u, u>, return <u, u, u, u>
3377 if (!Base.Val)
3378 return N0;
3379 for (unsigned i = 0; i != NumElems; ++i) {
3380 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3381 V->getOperand(i) != Base) {
3382 AllSame = false;
3383 break;
3384 }
3385 }
3386 // Splat of <x, x, x, x>, return <x, x, x, x>
3387 if (AllSame)
3388 return N0;
3389 }
3390 }
3391 }
3392
Evan Chenge7bec0d2006-07-20 22:44:41 +00003393 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3394 // into an undef.
3395 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00003396 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3397 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003398 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00003399 for (unsigned i = 0; i != NumElts; ++i) {
3400 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3401 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3402 MappedOps.push_back(ShufMask.getOperand(i));
3403 } else {
3404 unsigned NewIdx =
3405 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3406 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
3407 }
3408 }
3409 // Add the type/#elts values.
3410 MappedOps.push_back(ShufMask.getOperand(NumElts));
3411 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
3412
3413 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003414 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003415 AddToWorkList(ShufMask.Val);
3416
3417 // Build the undef vector.
3418 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
3419 for (unsigned i = 0; i != NumElts; ++i)
3420 MappedOps[i] = UDVal;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003421 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
3422 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003423 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3424 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003425
3426 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
Evan Chenge7bec0d2006-07-20 22:44:41 +00003427 N0, UDVal, ShufMask,
Chris Lattner17614ea2006-04-08 05:34:25 +00003428 MappedOps[NumElts], MappedOps[NumElts+1]);
3429 }
3430
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003431 return SDOperand();
3432}
3433
Evan Cheng44f1f092006-04-20 08:56:16 +00003434/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
3435/// a VAND to a vector_shuffle with the destination vector and a zero vector.
3436/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
3437/// vector_shuffle V, Zero, <0, 4, 2, 4>
3438SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
3439 SDOperand LHS = N->getOperand(0);
3440 SDOperand RHS = N->getOperand(1);
3441 if (N->getOpcode() == ISD::VAND) {
3442 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
3443 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
3444 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
3445 RHS = RHS.getOperand(0);
3446 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
3447 std::vector<SDOperand> IdxOps;
3448 unsigned NumOps = RHS.getNumOperands();
3449 unsigned NumElts = NumOps-2;
3450 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
3451 for (unsigned i = 0; i != NumElts; ++i) {
3452 SDOperand Elt = RHS.getOperand(i);
3453 if (!isa<ConstantSDNode>(Elt))
3454 return SDOperand();
3455 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
3456 IdxOps.push_back(DAG.getConstant(i, EVT));
3457 else if (cast<ConstantSDNode>(Elt)->isNullValue())
3458 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
3459 else
3460 return SDOperand();
3461 }
3462
3463 // Let's see if the target supports this vector_shuffle.
3464 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
3465 return SDOperand();
3466
3467 // Return the new VVECTOR_SHUFFLE node.
3468 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
3469 SDOperand EVTNode = DAG.getValueType(EVT);
3470 std::vector<SDOperand> Ops;
Chris Lattner516b9622006-09-14 20:50:57 +00003471 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode,
3472 EVTNode);
Evan Cheng44f1f092006-04-20 08:56:16 +00003473 Ops.push_back(LHS);
3474 AddToWorkList(LHS.Val);
3475 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
3476 ZeroOps.push_back(NumEltsNode);
3477 ZeroOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003478 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3479 &ZeroOps[0], ZeroOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003480 IdxOps.push_back(NumEltsNode);
3481 IdxOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003482 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3483 &IdxOps[0], IdxOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003484 Ops.push_back(NumEltsNode);
3485 Ops.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003486 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
3487 &Ops[0], Ops.size());
Evan Cheng44f1f092006-04-20 08:56:16 +00003488 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
3489 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
3490 DstVecSize, DstVecEVT);
3491 }
3492 return Result;
3493 }
3494 }
3495 return SDOperand();
3496}
3497
Chris Lattneredab1b92006-04-02 03:25:57 +00003498/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
3499/// the scalar operation of the vop if it is operating on an integer vector
3500/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
3501SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
3502 ISD::NodeType FPOp) {
3503 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
3504 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
3505 SDOperand LHS = N->getOperand(0);
3506 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00003507 SDOperand Shuffle = XformToShuffleWithZero(N);
3508 if (Shuffle.Val) return Shuffle;
3509
Chris Lattneredab1b92006-04-02 03:25:57 +00003510 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
3511 // this operation.
3512 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
3513 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003514 SmallVector<SDOperand, 8> Ops;
Chris Lattneredab1b92006-04-02 03:25:57 +00003515 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
3516 SDOperand LHSOp = LHS.getOperand(i);
3517 SDOperand RHSOp = RHS.getOperand(i);
3518 // If these two elements can't be folded, bail out.
3519 if ((LHSOp.getOpcode() != ISD::UNDEF &&
3520 LHSOp.getOpcode() != ISD::Constant &&
3521 LHSOp.getOpcode() != ISD::ConstantFP) ||
3522 (RHSOp.getOpcode() != ISD::UNDEF &&
3523 RHSOp.getOpcode() != ISD::Constant &&
3524 RHSOp.getOpcode() != ISD::ConstantFP))
3525 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00003526 // Can't fold divide by zero.
3527 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
3528 if ((RHSOp.getOpcode() == ISD::Constant &&
3529 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
3530 (RHSOp.getOpcode() == ISD::ConstantFP &&
3531 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
3532 break;
3533 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003534 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00003535 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00003536 assert((Ops.back().getOpcode() == ISD::UNDEF ||
3537 Ops.back().getOpcode() == ISD::Constant ||
3538 Ops.back().getOpcode() == ISD::ConstantFP) &&
3539 "Scalar binop didn't fold!");
3540 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003541
3542 if (Ops.size() == LHS.getNumOperands()-2) {
3543 Ops.push_back(*(LHS.Val->op_end()-2));
3544 Ops.push_back(*(LHS.Val->op_end()-1));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003545 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003546 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003547 }
3548
3549 return SDOperand();
3550}
3551
Nate Begeman44728a72005-09-19 22:34:01 +00003552SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00003553 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
3554
3555 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
3556 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3557 // If we got a simplified select_cc node back from SimplifySelectCC, then
3558 // break it down into a new SETCC node, and a new SELECT node, and then return
3559 // the SELECT node, since we were called with a SELECT node.
3560 if (SCC.Val) {
3561 // Check to see if we got a select_cc back (to turn into setcc/select).
3562 // Otherwise, just return whatever node we got back, like fabs.
3563 if (SCC.getOpcode() == ISD::SELECT_CC) {
3564 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
3565 SCC.getOperand(0), SCC.getOperand(1),
3566 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00003567 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003568 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
3569 SCC.getOperand(3), SETCC);
3570 }
3571 return SCC;
3572 }
Nate Begeman44728a72005-09-19 22:34:01 +00003573 return SDOperand();
3574}
3575
Chris Lattner40c62d52005-10-18 06:04:22 +00003576/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
3577/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00003578/// select. Callers of this should assume that TheSelect is deleted if this
3579/// returns true. As such, they should return the appropriate thing (e.g. the
3580/// node) back to the top-level of the DAG combiner loop to avoid it being
3581/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00003582///
3583bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
3584 SDOperand RHS) {
3585
3586 // If this is a select from two identical things, try to pull the operation
3587 // through the select.
3588 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00003589 // If this is a load and the token chain is identical, replace the select
3590 // of two loads with a load through a select of the address to load from.
3591 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
3592 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00003593 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00003594 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00003595 LHS.getOperand(0) == RHS.getOperand(0)) {
3596 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
3597 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
3598
3599 // If this is an EXTLOAD, the VT's must match.
Evan Cheng2e49f092006-10-11 07:10:22 +00003600 if (LLD->getLoadedVT() == RLD->getLoadedVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003601 // FIXME: this conflates two src values, discarding one. This is not
3602 // the right thing to do, but nothing uses srcvalues now. When they do,
3603 // turn SrcValue into a list of locations.
3604 SDOperand Addr;
3605 if (TheSelect->getOpcode() == ISD::SELECT)
3606 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
3607 TheSelect->getOperand(0), LLD->getBasePtr(),
3608 RLD->getBasePtr());
3609 else
3610 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
3611 TheSelect->getOperand(0),
3612 TheSelect->getOperand(1),
3613 LLD->getBasePtr(), RLD->getBasePtr(),
3614 TheSelect->getOperand(4));
Chris Lattner40c62d52005-10-18 06:04:22 +00003615
Evan Cheng466685d2006-10-09 20:57:25 +00003616 SDOperand Load;
3617 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
3618 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
3619 Addr,LLD->getSrcValue(), LLD->getSrcValueOffset());
3620 else {
3621 Load = DAG.getExtLoad(LLD->getExtensionType(),
3622 TheSelect->getValueType(0),
3623 LLD->getChain(), Addr, LLD->getSrcValue(),
3624 LLD->getSrcValueOffset(),
Evan Cheng2e49f092006-10-11 07:10:22 +00003625 LLD->getLoadedVT());
Evan Cheng466685d2006-10-09 20:57:25 +00003626 }
3627 // Users of the select now use the result of the load.
3628 CombineTo(TheSelect, Load);
3629
3630 // Users of the old loads now use the new load's chain. We know the
3631 // old-load value is dead now.
3632 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
3633 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
3634 return true;
Evan Chengc5484282006-10-04 00:56:09 +00003635 }
Chris Lattner40c62d52005-10-18 06:04:22 +00003636 }
3637 }
3638
3639 return false;
3640}
3641
Nate Begeman44728a72005-09-19 22:34:01 +00003642SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
3643 SDOperand N2, SDOperand N3,
3644 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00003645
3646 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00003647 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
3648 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
3649 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
3650
3651 // Determine if the condition we're dealing with is constant
3652 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003653 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003654 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
3655
3656 // fold select_cc true, x, y -> x
3657 if (SCCC && SCCC->getValue())
3658 return N2;
3659 // fold select_cc false, x, y -> y
3660 if (SCCC && SCCC->getValue() == 0)
3661 return N3;
3662
3663 // Check to see if we can simplify the select into an fabs node
3664 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
3665 // Allow either -0.0 or 0.0
3666 if (CFP->getValue() == 0.0) {
3667 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
3668 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
3669 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
3670 N2 == N3.getOperand(0))
3671 return DAG.getNode(ISD::FABS, VT, N0);
3672
3673 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
3674 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3675 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3676 N2.getOperand(0) == N3)
3677 return DAG.getNode(ISD::FABS, VT, N3);
3678 }
3679 }
3680
3681 // Check to see if we can perform the "gzip trick", transforming
3682 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00003683 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00003684 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00003685 MVT::isInteger(N2.getValueType()) &&
3686 (N1C->isNullValue() || // (a < 0) ? b : 0
3687 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00003688 MVT::ValueType XType = N0.getValueType();
3689 MVT::ValueType AType = N2.getValueType();
3690 if (XType >= AType) {
3691 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00003692 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00003693 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3694 unsigned ShCtV = Log2_64(N2C->getValue());
3695 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3696 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3697 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00003698 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003699 if (XType > AType) {
3700 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003701 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003702 }
3703 return DAG.getNode(ISD::AND, AType, Shift, N2);
3704 }
3705 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3706 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3707 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003708 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003709 if (XType > AType) {
3710 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003711 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003712 }
3713 return DAG.getNode(ISD::AND, AType, Shift, N2);
3714 }
3715 }
Nate Begeman07ed4172005-10-10 21:26:48 +00003716
3717 // fold select C, 16, 0 -> shl C, 4
3718 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3719 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3720 // Get a SetCC of the condition
3721 // FIXME: Should probably make sure that setcc is legal if we ever have a
3722 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00003723 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00003724 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00003725 if (AfterLegalize) {
3726 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003727 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00003728 } else {
3729 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003730 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00003731 }
Chris Lattner5750df92006-03-01 04:03:14 +00003732 AddToWorkList(SCC.Val);
3733 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00003734 // shl setcc result by log2 n2c
3735 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3736 DAG.getConstant(Log2_64(N2C->getValue()),
3737 TLI.getShiftAmountTy()));
3738 }
3739
Nate Begemanf845b452005-10-08 00:29:44 +00003740 // Check to see if this is the equivalent of setcc
3741 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3742 // otherwise, go ahead with the folds.
3743 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3744 MVT::ValueType XType = N0.getValueType();
3745 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3746 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3747 if (Res.getValueType() != VT)
3748 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3749 return Res;
3750 }
3751
3752 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3753 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3754 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3755 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3756 return DAG.getNode(ISD::SRL, XType, Ctlz,
3757 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3758 TLI.getShiftAmountTy()));
3759 }
3760 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3761 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3762 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3763 N0);
3764 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3765 DAG.getConstant(~0ULL, XType));
3766 return DAG.getNode(ISD::SRL, XType,
3767 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3768 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3769 TLI.getShiftAmountTy()));
3770 }
3771 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3772 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3773 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3774 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3775 TLI.getShiftAmountTy()));
3776 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3777 }
3778 }
3779
3780 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3781 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3782 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3783 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3784 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3785 MVT::ValueType XType = N0.getValueType();
3786 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3787 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3788 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3789 TLI.getShiftAmountTy()));
3790 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003791 AddToWorkList(Shift.Val);
3792 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003793 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3794 }
3795 }
3796 }
3797
Nate Begeman44728a72005-09-19 22:34:01 +00003798 return SDOperand();
3799}
3800
Nate Begeman452d7be2005-09-16 00:54:12 +00003801SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00003802 SDOperand N1, ISD::CondCode Cond,
3803 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003804 // These setcc operations always fold.
3805 switch (Cond) {
3806 default: break;
3807 case ISD::SETFALSE:
3808 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3809 case ISD::SETTRUE:
3810 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3811 }
3812
3813 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3814 uint64_t C1 = N1C->getValue();
Reid Spencer3ed469c2006-11-02 20:25:50 +00003815 if (isa<ConstantSDNode>(N0.Val)) {
Chris Lattner51dabfb2006-10-14 00:41:01 +00003816 return DAG.FoldSetCC(VT, N0, N1, Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00003817 } else {
Chris Lattner5f42a242006-09-20 06:19:26 +00003818 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
3819 // equality comparison, then we're just comparing whether X itself is
3820 // zero.
3821 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
3822 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
3823 N0.getOperand(1).getOpcode() == ISD::Constant) {
3824 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
3825 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3826 ShAmt == Log2_32(MVT::getSizeInBits(N0.getValueType()))) {
3827 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
3828 // (srl (ctlz x), 5) == 0 -> X != 0
3829 // (srl (ctlz x), 5) != 1 -> X != 0
3830 Cond = ISD::SETNE;
3831 } else {
3832 // (srl (ctlz x), 5) != 0 -> X == 0
3833 // (srl (ctlz x), 5) == 1 -> X == 0
3834 Cond = ISD::SETEQ;
3835 }
3836 SDOperand Zero = DAG.getConstant(0, N0.getValueType());
3837 return DAG.getSetCC(VT, N0.getOperand(0).getOperand(0),
3838 Zero, Cond);
3839 }
3840 }
3841
Nate Begeman452d7be2005-09-16 00:54:12 +00003842 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3843 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3844 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3845
3846 // If the comparison constant has bits in the upper part, the
3847 // zero-extended value could never match.
3848 if (C1 & (~0ULL << InSize)) {
3849 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3850 switch (Cond) {
3851 case ISD::SETUGT:
3852 case ISD::SETUGE:
3853 case ISD::SETEQ: return DAG.getConstant(0, VT);
3854 case ISD::SETULT:
3855 case ISD::SETULE:
3856 case ISD::SETNE: return DAG.getConstant(1, VT);
3857 case ISD::SETGT:
3858 case ISD::SETGE:
3859 // True if the sign bit of C1 is set.
3860 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3861 case ISD::SETLT:
3862 case ISD::SETLE:
3863 // True if the sign bit of C1 isn't set.
3864 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3865 default:
3866 break;
3867 }
3868 }
3869
3870 // Otherwise, we can perform the comparison with the low bits.
3871 switch (Cond) {
3872 case ISD::SETEQ:
3873 case ISD::SETNE:
3874 case ISD::SETUGT:
3875 case ISD::SETUGE:
3876 case ISD::SETULT:
3877 case ISD::SETULE:
3878 return DAG.getSetCC(VT, N0.getOperand(0),
3879 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3880 Cond);
3881 default:
3882 break; // todo, be more careful with signed comparisons
3883 }
3884 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3885 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3886 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3887 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3888 MVT::ValueType ExtDstTy = N0.getValueType();
3889 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3890
3891 // If the extended part has any inconsistent bits, it cannot ever
3892 // compare equal. In other words, they have to be all ones or all
3893 // zeros.
3894 uint64_t ExtBits =
3895 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3896 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3897 return DAG.getConstant(Cond == ISD::SETNE, VT);
3898
3899 SDOperand ZextOp;
3900 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3901 if (Op0Ty == ExtSrcTy) {
3902 ZextOp = N0.getOperand(0);
3903 } else {
3904 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3905 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3906 DAG.getConstant(Imm, Op0Ty));
3907 }
Chris Lattner5750df92006-03-01 04:03:14 +00003908 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003909 // Otherwise, make this a use of a zext.
3910 return DAG.getSetCC(VT, ZextOp,
3911 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3912 ExtDstTy),
3913 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00003914 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
Chris Lattner8ac9d0e2006-10-14 01:02:29 +00003915 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3916
3917 // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC
3918 if (N0.getOpcode() == ISD::SETCC) {
3919 bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getValue() != 1);
3920 if (TrueWhenTrue)
3921 return N0;
3922
3923 // Invert the condition.
3924 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
3925 CC = ISD::getSetCCInverse(CC,
3926 MVT::isInteger(N0.getOperand(0).getValueType()));
3927 return DAG.getSetCC(VT, N0.getOperand(0), N0.getOperand(1), CC);
3928 }
3929
3930 if ((N0.getOpcode() == ISD::XOR ||
3931 (N0.getOpcode() == ISD::AND &&
3932 N0.getOperand(0).getOpcode() == ISD::XOR &&
3933 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3934 isa<ConstantSDNode>(N0.getOperand(1)) &&
3935 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
3936 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We
3937 // can only do this if the top bits are known zero.
Chris Lattner50662be2006-10-17 21:24:15 +00003938 if (TLI.MaskedValueIsZero(N0,
Chris Lattner8ac9d0e2006-10-14 01:02:29 +00003939 MVT::getIntVTBitMask(N0.getValueType())-1)){
3940 // Okay, get the un-inverted input value.
3941 SDOperand Val;
3942 if (N0.getOpcode() == ISD::XOR)
3943 Val = N0.getOperand(0);
3944 else {
3945 assert(N0.getOpcode() == ISD::AND &&
3946 N0.getOperand(0).getOpcode() == ISD::XOR);
3947 // ((X^1)&1)^1 -> X & 1
3948 Val = DAG.getNode(ISD::AND, N0.getValueType(),
3949 N0.getOperand(0).getOperand(0),
3950 N0.getOperand(1));
3951 }
3952 return DAG.getSetCC(VT, Val, N1,
3953 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
Chris Lattner3391bcd2006-02-08 02:13:15 +00003954 }
Chris Lattner3391bcd2006-02-08 02:13:15 +00003955 }
Nate Begeman452d7be2005-09-16 00:54:12 +00003956 }
Chris Lattner5c46f742005-10-05 06:11:08 +00003957
Nate Begeman452d7be2005-09-16 00:54:12 +00003958 uint64_t MinVal, MaxVal;
3959 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
3960 if (ISD::isSignedIntSetCC(Cond)) {
3961 MinVal = 1ULL << (OperandBitSize-1);
3962 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
3963 MaxVal = ~0ULL >> (65-OperandBitSize);
3964 else
3965 MaxVal = 0;
3966 } else {
3967 MinVal = 0;
3968 MaxVal = ~0ULL >> (64-OperandBitSize);
3969 }
3970
3971 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
3972 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
3973 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
3974 --C1; // X >= C0 --> X > (C0-1)
3975 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3976 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
3977 }
3978
3979 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
3980 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
3981 ++C1; // X <= C0 --> X < (C0+1)
3982 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3983 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
3984 }
3985
3986 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
3987 return DAG.getConstant(0, VT); // X < MIN --> false
3988
3989 // Canonicalize setgt X, Min --> setne X, Min
3990 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
3991 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00003992 // Canonicalize setlt X, Max --> setne X, Max
3993 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
3994 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00003995
3996 // If we have setult X, 1, turn it into seteq X, 0
3997 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
3998 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
3999 ISD::SETEQ);
4000 // If we have setugt X, Max-1, turn it into seteq X, Max
4001 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
4002 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
4003 ISD::SETEQ);
4004
4005 // If we have "setcc X, C0", check to see if we can shrink the immediate
4006 // by changing cc.
4007
4008 // SETUGT X, SINTMAX -> SETLT X, 0
4009 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
4010 C1 == (~0ULL >> (65-OperandBitSize)))
4011 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
4012 ISD::SETLT);
4013
4014 // FIXME: Implement the rest of these.
4015
4016 // Fold bit comparisons when we can.
4017 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
4018 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
4019 if (ConstantSDNode *AndRHS =
4020 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
4021 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
4022 // Perform the xform if the AND RHS is a single bit.
Chris Lattner51dabfb2006-10-14 00:41:01 +00004023 if (isPowerOf2_64(AndRHS->getValue())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004024 return DAG.getNode(ISD::SRL, VT, N0,
4025 DAG.getConstant(Log2_64(AndRHS->getValue()),
4026 TLI.getShiftAmountTy()));
4027 }
4028 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
4029 // (X & 8) == 8 --> (X & 8) >> 3
4030 // Perform the xform if C1 is a single bit.
Chris Lattner51dabfb2006-10-14 00:41:01 +00004031 if (isPowerOf2_64(C1)) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004032 return DAG.getNode(ISD::SRL, VT, N0,
Chris Lattner729c6d12006-05-27 00:43:02 +00004033 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
Nate Begeman452d7be2005-09-16 00:54:12 +00004034 }
4035 }
4036 }
4037 }
4038 } else if (isa<ConstantSDNode>(N0.Val)) {
4039 // Ensure that the constant occurs on the RHS.
4040 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
4041 }
4042
Reid Spencer3ed469c2006-11-02 20:25:50 +00004043 if (isa<ConstantFPSDNode>(N0.Val)) {
Chris Lattner51dabfb2006-10-14 00:41:01 +00004044 // Constant fold or commute setcc.
4045 SDOperand O = DAG.FoldSetCC(VT, N0, N1, Cond);
4046 if (O.Val) return O;
4047 }
Nate Begeman452d7be2005-09-16 00:54:12 +00004048
4049 if (N0 == N1) {
Chris Lattner8ac9d0e2006-10-14 01:02:29 +00004050 // We can always fold X == X for integer setcc's.
Nate Begeman452d7be2005-09-16 00:54:12 +00004051 if (MVT::isInteger(N0.getValueType()))
4052 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
4053 unsigned UOF = ISD::getUnorderedFlavor(Cond);
4054 if (UOF == 2) // FP operators that are undefined on NaNs.
4055 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
4056 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
4057 return DAG.getConstant(UOF, VT);
4058 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
4059 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00004060 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00004061 if (NewCond != Cond)
4062 return DAG.getSetCC(VT, N0, N1, NewCond);
4063 }
4064
4065 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
4066 MVT::isInteger(N0.getValueType())) {
4067 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
4068 N0.getOpcode() == ISD::XOR) {
4069 // Simplify (X+Y) == (X+Z) --> Y == Z
4070 if (N0.getOpcode() == N1.getOpcode()) {
4071 if (N0.getOperand(0) == N1.getOperand(0))
4072 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
4073 if (N0.getOperand(1) == N1.getOperand(1))
4074 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
Evan Cheng1efba0e2006-08-29 06:42:35 +00004075 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004076 // If X op Y == Y op X, try other combinations.
4077 if (N0.getOperand(0) == N1.getOperand(1))
4078 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
4079 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00004080 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00004081 }
4082 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00004083
4084 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
4085 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
4086 // Turn (X+C1) == C2 --> X == C2-C1
4087 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
4088 return DAG.getSetCC(VT, N0.getOperand(0),
4089 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
4090 N0.getValueType()), Cond);
4091 }
4092
4093 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
4094 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00004095 // If we know that all of the inverted bits are zero, don't bother
4096 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00004097 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00004098 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00004099 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00004100 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00004101 }
4102
4103 // Turn (C1-X) == C2 --> X == C1-C2
4104 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
4105 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
4106 return DAG.getSetCC(VT, N0.getOperand(1),
4107 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
4108 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00004109 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00004110 }
4111 }
4112
Nate Begeman452d7be2005-09-16 00:54:12 +00004113 // Simplify (X+Z) == X --> Z == 0
4114 if (N0.getOperand(0) == N1)
4115 return DAG.getSetCC(VT, N0.getOperand(1),
4116 DAG.getConstant(0, N0.getValueType()), Cond);
4117 if (N0.getOperand(1) == N1) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00004118 if (DAG.isCommutativeBinOp(N0.getOpcode()))
Nate Begeman452d7be2005-09-16 00:54:12 +00004119 return DAG.getSetCC(VT, N0.getOperand(0),
4120 DAG.getConstant(0, N0.getValueType()), Cond);
4121 else {
4122 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
4123 // (Z-X) == X --> Z == X<<1
4124 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
4125 N1,
4126 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00004127 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004128 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
4129 }
4130 }
4131 }
4132
4133 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
4134 N1.getOpcode() == ISD::XOR) {
4135 // Simplify X == (X+Z) --> Z == 0
4136 if (N1.getOperand(0) == N0) {
4137 return DAG.getSetCC(VT, N1.getOperand(1),
4138 DAG.getConstant(0, N1.getValueType()), Cond);
4139 } else if (N1.getOperand(1) == N0) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00004140 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004141 return DAG.getSetCC(VT, N1.getOperand(0),
4142 DAG.getConstant(0, N1.getValueType()), Cond);
4143 } else {
4144 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
4145 // X == (Z-X) --> X<<1 == Z
4146 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
4147 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00004148 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004149 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
4150 }
4151 }
4152 }
4153 }
4154
4155 // Fold away ALL boolean setcc's.
4156 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00004157 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004158 switch (Cond) {
4159 default: assert(0 && "Unknown integer setcc!");
4160 case ISD::SETEQ: // X == Y -> (X^Y)^1
4161 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
4162 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00004163 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004164 break;
4165 case ISD::SETNE: // X != Y --> (X^Y)
4166 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
4167 break;
4168 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
4169 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
4170 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
4171 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00004172 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004173 break;
4174 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
4175 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
4176 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
4177 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00004178 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004179 break;
4180 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
4181 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
4182 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
4183 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00004184 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004185 break;
4186 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
4187 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
4188 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
4189 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
4190 break;
4191 }
4192 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00004193 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004194 // FIXME: If running after legalize, we probably can't do this.
4195 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
4196 }
4197 return N0;
4198 }
4199
4200 // Could not fold it.
4201 return SDOperand();
4202}
4203
Nate Begeman69575232005-10-20 02:15:44 +00004204/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
4205/// return a DAG expression to select that will generate the same value by
4206/// multiplying by a magic number. See:
4207/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4208SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004209 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004210 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
4211
Andrew Lenharth232c9102006-06-12 16:07:18 +00004212 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004213 ii != ee; ++ii)
4214 AddToWorkList(*ii);
4215 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004216}
4217
4218/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
4219/// return a DAG expression to select that will generate the same value by
4220/// multiplying by a magic number. See:
4221/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4222SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004223 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004224 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00004225
Andrew Lenharth232c9102006-06-12 16:07:18 +00004226 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004227 ii != ee; ++ii)
4228 AddToWorkList(*ii);
4229 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004230}
4231
Jim Laskey71382342006-10-07 23:37:56 +00004232/// FindBaseOffset - Return true if base is known not to alias with anything
4233/// but itself. Provides base object and offset as results.
4234static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
4235 // Assume it is a primitive operation.
4236 Base = Ptr; Offset = 0;
4237
4238 // If it's an adding a simple constant then integrate the offset.
4239 if (Base.getOpcode() == ISD::ADD) {
4240 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
4241 Base = Base.getOperand(0);
4242 Offset += C->getValue();
4243 }
4244 }
4245
4246 // If it's any of the following then it can't alias with anything but itself.
4247 return isa<FrameIndexSDNode>(Base) ||
4248 isa<ConstantPoolSDNode>(Base) ||
4249 isa<GlobalAddressSDNode>(Base);
4250}
4251
4252/// isAlias - Return true if there is any possibility that the two addresses
4253/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00004254bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
4255 const Value *SrcValue1, int SrcValueOffset1,
4256 SDOperand Ptr2, int64_t Size2,
4257 const Value *SrcValue2, int SrcValueOffset2)
4258{
Jim Laskey71382342006-10-07 23:37:56 +00004259 // If they are the same then they must be aliases.
4260 if (Ptr1 == Ptr2) return true;
4261
4262 // Gather base node and offset information.
4263 SDOperand Base1, Base2;
4264 int64_t Offset1, Offset2;
4265 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
4266 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
4267
4268 // If they have a same base address then...
4269 if (Base1 == Base2) {
4270 // Check to see if the addresses overlap.
4271 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
4272 }
4273
Jim Laskey096c22e2006-10-18 12:29:57 +00004274 // If we know both bases then they can't alias.
4275 if (KnownBase1 && KnownBase2) return false;
4276
Jim Laskey07a27092006-10-18 19:08:31 +00004277 if (CombinerGlobalAA) {
4278 // Use alias analysis information.
4279 int Overlap1 = Size1 + SrcValueOffset1 + Offset1;
4280 int Overlap2 = Size2 + SrcValueOffset2 + Offset2;
4281 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00004282 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00004283 if (AAResult == AliasAnalysis::NoAlias)
4284 return false;
4285 }
Jim Laskey096c22e2006-10-18 12:29:57 +00004286
4287 // Otherwise we have to assume they alias.
4288 return true;
Jim Laskey71382342006-10-07 23:37:56 +00004289}
4290
4291/// FindAliasInfo - Extracts the relevant alias information from the memory
4292/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00004293bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00004294 SDOperand &Ptr, int64_t &Size,
4295 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004296 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4297 Ptr = LD->getBasePtr();
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004298 Size = MVT::getSizeInBits(LD->getLoadedVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004299 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004300 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00004301 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004302 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004303 Ptr = ST->getBasePtr();
Evan Cheng8b2794a2006-10-13 21:14:26 +00004304 Size = MVT::getSizeInBits(ST->getStoredVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004305 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004306 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00004307 } else {
Jim Laskey71382342006-10-07 23:37:56 +00004308 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00004309 }
4310
4311 return false;
4312}
4313
Jim Laskey6ff23e52006-10-04 16:53:27 +00004314/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
4315/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00004316void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00004317 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004318 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00004319 std::set<SDNode *> Visited; // Visited node set.
4320
Jim Laskey279f0532006-09-25 16:29:54 +00004321 // Get alias information for node.
4322 SDOperand Ptr;
4323 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004324 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004325 int SrcValueOffset;
4326 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00004327
Jim Laskey6ff23e52006-10-04 16:53:27 +00004328 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00004329 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00004330
Jim Laskeybc588b82006-10-05 15:07:25 +00004331 // Look at each chain and determine if it is an alias. If so, add it to the
4332 // aliases list. If not, then continue up the chain looking for the next
4333 // candidate.
4334 while (!Chains.empty()) {
4335 SDOperand Chain = Chains.back();
4336 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00004337
Jim Laskeybc588b82006-10-05 15:07:25 +00004338 // Don't bother if we've been before.
4339 if (Visited.find(Chain.Val) != Visited.end()) continue;
4340 Visited.insert(Chain.Val);
4341
4342 switch (Chain.getOpcode()) {
4343 case ISD::EntryToken:
4344 // Entry token is ideal chain operand, but handled in FindBetterChain.
4345 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00004346
Jim Laskeybc588b82006-10-05 15:07:25 +00004347 case ISD::LOAD:
4348 case ISD::STORE: {
4349 // Get alias information for Chain.
4350 SDOperand OpPtr;
4351 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004352 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004353 int OpSrcValueOffset;
4354 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
4355 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00004356
4357 // If chain is alias then stop here.
4358 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00004359 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
4360 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004361 Aliases.push_back(Chain);
4362 } else {
4363 // Look further up the chain.
4364 Chains.push_back(Chain.getOperand(0));
4365 // Clean up old chain.
4366 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00004367 }
Jim Laskeybc588b82006-10-05 15:07:25 +00004368 break;
4369 }
4370
4371 case ISD::TokenFactor:
4372 // We have to check each of the operands of the token factor, so we queue
4373 // then up. Adding the operands to the queue (stack) in reverse order
4374 // maintains the original order and increases the likelihood that getNode
4375 // will find a matching token factor (CSE.)
4376 for (unsigned n = Chain.getNumOperands(); n;)
4377 Chains.push_back(Chain.getOperand(--n));
4378 // Eliminate the token factor if we can.
4379 AddToWorkList(Chain.Val);
4380 break;
4381
4382 default:
4383 // For all other instructions we will just have to take what we can get.
4384 Aliases.push_back(Chain);
4385 break;
Jim Laskey279f0532006-09-25 16:29:54 +00004386 }
4387 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004388}
4389
4390/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
4391/// for a better chain (aliasing node.)
4392SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
4393 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00004394
Jim Laskey6ff23e52006-10-04 16:53:27 +00004395 // Accumulate all the aliases to this node.
4396 GatherAllAliases(N, OldChain, Aliases);
4397
4398 if (Aliases.size() == 0) {
4399 // If no operands then chain to entry token.
4400 return DAG.getEntryNode();
4401 } else if (Aliases.size() == 1) {
4402 // If a single operand then chain to it. We don't need to revisit it.
4403 return Aliases[0];
4404 }
4405
4406 // Construct a custom tailored token factor.
4407 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4408 &Aliases[0], Aliases.size());
4409
4410 // Make sure the old chain gets cleaned up.
4411 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
4412
4413 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00004414}
4415
Nate Begeman1d4d4142005-09-01 00:19:25 +00004416// SelectionDAG::Combine - This is the entry point for the file.
4417//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004418void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00004419 /// run - This is the main entry point to this class.
4420 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004421 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004422}