blob: d8e2027d11acc8553b4b57aa517aa36031387ce5 [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>
Nate Begeman1d4d4142005-09-01 00:19:25 +000041#include <cmath>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000042#include <iostream>
Jim Laskey279f0532006-09-25 16:29:54 +000043#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000044using namespace llvm;
45
46namespace {
Andrew Lenharthae6153f2006-07-20 17:43:27 +000047 static Statistic<> NodesCombined ("dagcombiner",
48 "Number of dag nodes combined");
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000049
Evan Chengbbd6f6e2006-11-07 09:03:05 +000050 static Statistic<> PreIndexedNodes ("pre_indexed_ops",
51 "Number of pre-indexed nodes created");
52 static Statistic<> PostIndexedNodes ("post_indexed_ops",
53 "Number of post-indexed nodes created");
54
Jim Laskey71382342006-10-07 23:37:56 +000055 static cl::opt<bool>
56 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000057 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000058
Jim Laskey07a27092006-10-18 19:08:31 +000059 static cl::opt<bool>
60 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
61 cl::desc("Include global information in alias analysis"));
62
Jim Laskeybc588b82006-10-05 15:07:25 +000063//------------------------------ DAGCombiner ---------------------------------//
64
Jim Laskey71382342006-10-07 23:37:56 +000065 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000066 SelectionDAG &DAG;
67 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000068 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000069
70 // Worklist of all of the nodes that need to be simplified.
71 std::vector<SDNode*> WorkList;
72
Jim Laskeyc7c3f112006-10-16 20:52:31 +000073 // AA - Used for DAG load/store alias analysis.
74 AliasAnalysis &AA;
75
Nate Begeman1d4d4142005-09-01 00:19:25 +000076 /// AddUsersToWorkList - When an instruction is simplified, add all users of
77 /// the instruction to the work lists because they might get more simplified
78 /// now.
79 ///
80 void AddUsersToWorkList(SDNode *N) {
81 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000082 UI != UE; ++UI)
Jim Laskey6ff23e52006-10-04 16:53:27 +000083 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000084 }
85
86 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000087 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000088 void removeFromWorkList(SDNode *N) {
89 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
90 WorkList.end());
91 }
92
Chris Lattner24664722006-03-01 04:53:38 +000093 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000094 /// AddToWorkList - Add to the work list making sure it's instance is at the
95 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000096 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000097 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000098 WorkList.push_back(N);
99 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000100
Jim Laskey274062c2006-10-13 23:32:28 +0000101 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
102 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000103 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
Chris Lattner87514ca2005-10-10 22:31:19 +0000104 ++NodesCombined;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000105 DEBUG(std::cerr << "\nReplacing.1 "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000106 std::cerr << "\nWith: "; To[0].Val->dump(&DAG);
Chris Lattner3577e382006-08-11 17:56:38 +0000107 std::cerr << " and " << NumTo-1 << " other values\n");
Chris Lattner01a22022005-10-10 22:04:48 +0000108 std::vector<SDNode*> NowDead;
Chris Lattner3577e382006-08-11 17:56:38 +0000109 DAG.ReplaceAllUsesWith(N, To, &NowDead);
Chris Lattner01a22022005-10-10 22:04:48 +0000110
Jim Laskey274062c2006-10-13 23:32:28 +0000111 if (AddTo) {
112 // Push the new nodes and any users onto the worklist
113 for (unsigned i = 0, e = NumTo; i != e; ++i) {
114 AddToWorkList(To[i].Val);
115 AddUsersToWorkList(To[i].Val);
116 }
Chris Lattner01a22022005-10-10 22:04:48 +0000117 }
118
Jim Laskey6ff23e52006-10-04 16:53:27 +0000119 // Nodes can be reintroduced into the worklist. Make sure we do not
120 // process a node that has been replaced.
Chris Lattner01a22022005-10-10 22:04:48 +0000121 removeFromWorkList(N);
122 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
123 removeFromWorkList(NowDead[i]);
124
125 // Finally, since the node is now dead, remove it from the graph.
126 DAG.DeleteNode(N);
127 return SDOperand(N, 0);
128 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000129
Jim Laskey274062c2006-10-13 23:32:28 +0000130 SDOperand CombineTo(SDNode *N, SDOperand Res, bool AddTo = true) {
131 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000132 }
133
Jim Laskey274062c2006-10-13 23:32:28 +0000134 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1,
135 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000136 SDOperand To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000137 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000138 }
139 private:
140
Chris Lattner012f2412006-02-17 21:58:01 +0000141 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000142 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000143 /// propagation. If so, return true.
144 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000145 TargetLowering::TargetLoweringOpt TLO(DAG);
146 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000147 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
148 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
149 return false;
150
151 // Revisit the node.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000152 AddToWorkList(Op.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000153
154 // Replace the old value with the new one.
155 ++NodesCombined;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000156 DEBUG(std::cerr << "\nReplacing.2 "; TLO.Old.Val->dump();
Jim Laskey279f0532006-09-25 16:29:54 +0000157 std::cerr << "\nWith: "; TLO.New.Val->dump(&DAG);
158 std::cerr << '\n');
Chris Lattner012f2412006-02-17 21:58:01 +0000159
160 std::vector<SDNode*> NowDead;
161 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
162
Chris Lattner7d20d392006-02-20 06:51:04 +0000163 // Push the new node and any (possibly new) users onto the worklist.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000164 AddToWorkList(TLO.New.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000165 AddUsersToWorkList(TLO.New.Val);
166
167 // Nodes can end up on the worklist more than once. Make sure we do
168 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000169 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
170 removeFromWorkList(NowDead[i]);
171
Chris Lattner7d20d392006-02-20 06:51:04 +0000172 // Finally, if the node is now dead, remove it from the graph. The node
173 // may not be dead if the replacement process recursively simplified to
174 // something else needing this node.
175 if (TLO.Old.Val->use_empty()) {
176 removeFromWorkList(TLO.Old.Val);
177 DAG.DeleteNode(TLO.Old.Val);
178 }
Chris Lattner012f2412006-02-17 21:58:01 +0000179 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000180 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000181
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000182 /// CombineToPreIndexedLoadStore - Try turning a load / store and a
183 /// pre-indexed load / store when the base pointer is a add or subtract
184 /// and it has other uses besides the load / store. After the
185 /// transformation, the new indexed load / store has effectively folded
186 /// the add / subtract in and all of its other uses are redirected to the
187 /// new load / store.
Evan Cheng3ef554d2006-11-06 08:14:30 +0000188 bool CombineToPreIndexedLoadStore(SDNode *N) {
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000189 if (!AfterLegalize)
190 return false;
191
Evan Cheng33dbedc2006-11-05 09:31:14 +0000192 bool isLoad = true;
Evan Cheng7fc033a2006-11-03 03:06:21 +0000193 SDOperand Ptr;
Evan Cheng7fc033a2006-11-03 03:06:21 +0000194 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
195 Ptr = LD->getBasePtr();
Evan Cheng33dbedc2006-11-05 09:31:14 +0000196 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
197 Ptr = ST->getBasePtr();
198 isLoad = false;
Evan Cheng7fc033a2006-11-03 03:06:21 +0000199 } else
200 return false;
201
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000202 if ((Ptr.getOpcode() == ISD::ADD || Ptr.getOpcode() == ISD::SUB) &&
Evan Cheng7fc033a2006-11-03 03:06:21 +0000203 Ptr.Val->use_size() > 1) {
204 SDOperand BasePtr;
205 SDOperand Offset;
206 ISD::MemOpAddrMode AM = ISD::UNINDEXED;
Evan Cheng1a854be2006-11-03 07:21:16 +0000207 if (TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG)) {
Evan Cheng7fc033a2006-11-03 03:06:21 +0000208 // Try turning it into a pre-indexed load / store except when
209 // 1) Another use of base ptr is a predecessor of N. If ptr is folded
210 // that would create a cycle.
211 // 2) All uses are load / store ops that use it as base ptr and offset
212 // is just an addressing mode immediate.
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000213 // 3) If the would-be new base may not to be dead at N.
Evan Cheng7fc033a2006-11-03 03:06:21 +0000214
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000215 bool OffIsAMImm = Offset.getOpcode() == ISD::Constant && TLI.
216 isLegalAddressImmediate(cast<ConstantSDNode>(Offset)->getValue());
Evan Cheng7fc033a2006-11-03 03:06:21 +0000217
218 // Check for #3.
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000219 for (SDNode::use_iterator I = BasePtr.Val->use_begin(),
220 E = BasePtr.Val->use_end(); I != E; ++I) {
221 SDNode *Use = *I;
222 if (Use == Ptr.Val)
223 continue;
224 if (Use->getOpcode() == ISD::CopyToReg)
225 return false;
226 if (OffIsAMImm && (Use->getOpcode() == ISD::ADD ||
227 Use->getOpcode() == ISD::SUB)) {
228 for (SDNode::use_iterator II = Use->use_begin(),
229 EE = Use->use_end(); II != EE; ++II) {
230 SDNode *UseUse = *II;
231 if (UseUse->getOpcode() == ISD::LOAD &&
232 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use)
233 return false;
234 else if (UseUse->getOpcode() == ISD::STORE &&
235 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use)
236 return false;
Evan Cheng7fc033a2006-11-03 03:06:21 +0000237 }
238 }
239 }
240
241 // Now check for #1 and #2.
242 unsigned NumRealUses = 0;
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000243 if (OffIsAMImm) {
244 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
245 E = Ptr.Val->use_end(); I != E; ++I) {
246 SDNode *Use = *I;
247 if (Use == N)
248 continue;
249 if (Use->isPredecessor(N))
250 return false;
Evan Cheng7fc033a2006-11-03 03:06:21 +0000251
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000252 if (!OffIsAMImm) {
Evan Cheng7fc033a2006-11-03 03:06:21 +0000253 NumRealUses++;
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000254 } else if (Use->getOpcode() == ISD::LOAD) {
255 if (cast<LoadSDNode>(Use)->getBasePtr().Val != Ptr.Val)
256 NumRealUses++;
257 } else if (Use->getOpcode() == ISD::STORE) {
258 if (cast<StoreSDNode>(Use)->getBasePtr().Val != Ptr.Val)
259 NumRealUses++;
260 } else
Evan Cheng7fc033a2006-11-03 03:06:21 +0000261 NumRealUses++;
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000262 }
Evan Cheng7fc033a2006-11-03 03:06:21 +0000263 }
264 if (NumRealUses == 0)
265 return false;
266
Evan Cheng33dbedc2006-11-05 09:31:14 +0000267 SDOperand Result = isLoad
268 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
269 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000270 ++PreIndexedNodes;
Evan Cheng7fc033a2006-11-03 03:06:21 +0000271 ++NodesCombined;
272 DEBUG(std::cerr << "\nReplacing.4 "; N->dump();
273 std::cerr << "\nWith: "; Result.Val->dump(&DAG);
274 std::cerr << '\n');
275 std::vector<SDNode*> NowDead;
Evan Cheng33dbedc2006-11-05 09:31:14 +0000276 if (isLoad) {
277 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
278 NowDead);
279 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
280 NowDead);
281 } else {
282 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
283 NowDead);
284 }
Evan Cheng7fc033a2006-11-03 03:06:21 +0000285
286 // Nodes can end up on the worklist more than once. Make sure we do
287 // not process a node that has been replaced.
288 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
289 removeFromWorkList(NowDead[i]);
290 // Finally, since the node is now dead, remove it from the graph.
291 DAG.DeleteNode(N);
292
293 // Replace the uses of Ptr with uses of the updated base value.
Evan Cheng33dbedc2006-11-05 09:31:14 +0000294 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
295 NowDead);
Evan Cheng7fc033a2006-11-03 03:06:21 +0000296 removeFromWorkList(Ptr.Val);
297 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
298 removeFromWorkList(NowDead[i]);
299 DAG.DeleteNode(Ptr.Val);
300
301 return true;
302 }
303 }
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000304 return false;
305 }
Evan Cheng7fc033a2006-11-03 03:06:21 +0000306
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000307 /// CombineToPostIndexedLoadStore - Try combine a load / store with a
308 /// add / sub of the base pointer node into a post-indexed load / store.
309 /// The transformation folded the add / subtract into the new indexed
310 /// load / store effectively and all of its uses are redirected to the
311 /// new load / store.
312 bool CombineToPostIndexedLoadStore(SDNode *N) {
313 if (!AfterLegalize)
314 return false;
315
316 bool isLoad = true;
317 SDOperand Ptr;
318 MVT::ValueType VT;
319 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
320 Ptr = LD->getBasePtr();
321 VT = LD->getLoadedVT();
322 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
323 Ptr = ST->getBasePtr();
324 VT = ST->getStoredVT();
325 isLoad = false;
326 } else
327 return false;
328
329 if (Ptr.Val->use_size() > 1) {
330 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
331 E = Ptr.Val->use_end(); I != E; ++I) {
332 SDNode *Op = *I;
333 if (Op == N ||
334 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
335 continue;
336
337 SDOperand BasePtr;
338 SDOperand Offset;
339 ISD::MemOpAddrMode AM = ISD::UNINDEXED;
340 if (TLI.getPostIndexedAddressParts(Op, VT, BasePtr, Offset, AM,DAG) &&
Evan Cheng6c1491d2006-11-08 02:38:55 +0000341 BasePtr == Ptr) {
Evan Chengbbd6f6e2006-11-07 09:03:05 +0000342 // Try turning it into a post-indexed load / store except when
343 // 1) Op must be independent of N, i.e. Op is neither a predecessor
344 // nor a successor of N. Otherwise, if Op is folded that would
345 // create a cycle.
346 // 2) All uses are load / store ops that use it as base ptr and offset
347 // is just an addressing mode immediate.
348 // 3) If the would-be new base may not to be dead at N.
349
350 bool OffIsAMImm = Offset.getOpcode() == ISD::Constant && TLI.
351 isLegalAddressImmediate(cast<ConstantSDNode>(Offset)->getValue());
352
353 // Check for #3.
354 bool TryNext = false;
355 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
356 EE = BasePtr.Val->use_end(); II != EE; ++II) {
357 SDNode *Use = *II;
358 if (Use == Ptr.Val)
359 continue;
360 if (Use->getOpcode() == ISD::CopyToReg) {
361 TryNext = true;
362 break;
363 }
364
365 if (OffIsAMImm && (Use->getOpcode() == ISD::ADD ||
366 Use->getOpcode() == ISD::SUB)) {
367 for (SDNode::use_iterator III = Use->use_begin(),
368 EEE = Use->use_end(); III != EEE; ++III) {
369 SDNode *UseUse = *III;
370 if (UseUse->getOpcode() == ISD::LOAD &&
371 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) {
372 TryNext = true;
373 break;
374 } else if (UseUse->getOpcode() == ISD::STORE &&
375 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use) {
376
377 TryNext = true;
378 break;
379 }
380 }
381 }
382 }
383 if (TryNext)
384 continue;
385
386 if (OffIsAMImm) {
387 // Check for #2.
388 unsigned NumRealUses = 0;
389 for (SDNode::use_iterator II = Op->use_begin(), EE = Op->use_end();
390 II != EE; ++II) {
391 SDNode *Use = *II;
392 if (Use->getOpcode() == ISD::LOAD) {
393 if (cast<LoadSDNode>(Use)->getBasePtr().Val != Op)
394 NumRealUses++;
395 } else if (Use->getOpcode() == ISD::STORE) {
396 if (cast<StoreSDNode>(Use)->getBasePtr().Val != Op)
397 NumRealUses++;
398 } else
399 NumRealUses++;
400 }
401 if (NumRealUses == 0)
402 continue;
403 }
404
405 // Check for #1
406 if (!Op->isPredecessor(N) && !N->isPredecessor(Op)) {
407 SDOperand Result = isLoad
408 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
409 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
410 ++PostIndexedNodes;
411 ++NodesCombined;
412 DEBUG(std::cerr << "\nReplacing.5 "; N->dump();
413 std::cerr << "\nWith: "; Result.Val->dump(&DAG);
414 std::cerr << '\n');
415 std::vector<SDNode*> NowDead;
416 if (isLoad) {
417 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
418 NowDead);
419 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
420 NowDead);
421 } else {
422 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
423 NowDead);
424 }
425
426 // Nodes can end up on the worklist more than once. Make sure we do
427 // not process a node that has been replaced.
428 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
429 removeFromWorkList(NowDead[i]);
430 // Finally, since the node is now dead, remove it from the graph.
431 DAG.DeleteNode(N);
432
433 // Replace the uses of Use with uses of the updated base value.
434 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
435 Result.getValue(isLoad ? 1 : 0),
436 NowDead);
437 removeFromWorkList(Op);
438 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
439 removeFromWorkList(NowDead[i]);
440 DAG.DeleteNode(Op);
441
442 return true;
443 }
444 }
445 }
446 }
Evan Cheng7fc033a2006-11-03 03:06:21 +0000447 return false;
448 }
449
Nate Begeman1d4d4142005-09-01 00:19:25 +0000450 /// visit - call the node-specific routine that knows how to fold each
451 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000452 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000453
454 // Visitation implementation - Implement dag node combining for different
455 // node types. The semantics are as follows:
456 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000457 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000458 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000459 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000460 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000461 SDOperand visitTokenFactor(SDNode *N);
462 SDOperand visitADD(SDNode *N);
463 SDOperand visitSUB(SDNode *N);
464 SDOperand visitMUL(SDNode *N);
465 SDOperand visitSDIV(SDNode *N);
466 SDOperand visitUDIV(SDNode *N);
467 SDOperand visitSREM(SDNode *N);
468 SDOperand visitUREM(SDNode *N);
469 SDOperand visitMULHU(SDNode *N);
470 SDOperand visitMULHS(SDNode *N);
471 SDOperand visitAND(SDNode *N);
472 SDOperand visitOR(SDNode *N);
473 SDOperand visitXOR(SDNode *N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000474 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000475 SDOperand visitSHL(SDNode *N);
476 SDOperand visitSRA(SDNode *N);
477 SDOperand visitSRL(SDNode *N);
478 SDOperand visitCTLZ(SDNode *N);
479 SDOperand visitCTTZ(SDNode *N);
480 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000481 SDOperand visitSELECT(SDNode *N);
482 SDOperand visitSELECT_CC(SDNode *N);
483 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000484 SDOperand visitSIGN_EXTEND(SDNode *N);
485 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000486 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000487 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
488 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000489 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000490 SDOperand visitVBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000491 SDOperand visitFADD(SDNode *N);
492 SDOperand visitFSUB(SDNode *N);
493 SDOperand visitFMUL(SDNode *N);
494 SDOperand visitFDIV(SDNode *N);
495 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000496 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000497 SDOperand visitSINT_TO_FP(SDNode *N);
498 SDOperand visitUINT_TO_FP(SDNode *N);
499 SDOperand visitFP_TO_SINT(SDNode *N);
500 SDOperand visitFP_TO_UINT(SDNode *N);
501 SDOperand visitFP_ROUND(SDNode *N);
502 SDOperand visitFP_ROUND_INREG(SDNode *N);
503 SDOperand visitFP_EXTEND(SDNode *N);
504 SDOperand visitFNEG(SDNode *N);
505 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000506 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000507 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000508 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000509 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000510 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
511 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000512 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000513 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000514 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000515
Evan Cheng44f1f092006-04-20 08:56:16 +0000516 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000517 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
518
Chris Lattner40c62d52005-10-18 06:04:22 +0000519 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000520 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000521 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
522 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
523 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000524 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000525 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000526 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000527 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000528 SDOperand BuildUDIV(SDNode *N);
529 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Jim Laskey279f0532006-09-25 16:29:54 +0000530
Jim Laskey6ff23e52006-10-04 16:53:27 +0000531 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
532 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000533 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000534 SmallVector<SDOperand, 8> &Aliases);
535
Jim Laskey096c22e2006-10-18 12:29:57 +0000536 /// isAlias - Return true if there is any possibility that the two addresses
537 /// overlap.
538 bool isAlias(SDOperand Ptr1, int64_t Size1,
539 const Value *SrcValue1, int SrcValueOffset1,
540 SDOperand Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000541 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000542
Jim Laskey7ca56af2006-10-11 13:47:09 +0000543 /// FindAliasInfo - Extracts the relevant alias information from the memory
544 /// node. Returns true if the operand was a load.
545 bool FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +0000546 SDOperand &Ptr, int64_t &Size,
547 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000548
Jim Laskey279f0532006-09-25 16:29:54 +0000549 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000550 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000551 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
552
Nate Begeman1d4d4142005-09-01 00:19:25 +0000553public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000554 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
555 : DAG(D),
556 TLI(D.getTargetLoweringInfo()),
557 AfterLegalize(false),
558 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000559
560 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000561 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000562 };
563}
564
Chris Lattner24664722006-03-01 04:53:38 +0000565//===----------------------------------------------------------------------===//
566// TargetLowering::DAGCombinerInfo implementation
567//===----------------------------------------------------------------------===//
568
569void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
570 ((DAGCombiner*)DC)->AddToWorkList(N);
571}
572
573SDOperand TargetLowering::DAGCombinerInfo::
574CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000575 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000576}
577
578SDOperand TargetLowering::DAGCombinerInfo::
579CombineTo(SDNode *N, SDOperand Res) {
580 return ((DAGCombiner*)DC)->CombineTo(N, Res);
581}
582
583
584SDOperand TargetLowering::DAGCombinerInfo::
585CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
586 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
587}
588
589
590
591
592//===----------------------------------------------------------------------===//
593
594
Nate Begeman4ebd8052005-09-01 23:24:04 +0000595// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
596// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000597// Also, set the incoming LHS, RHS, and CC references to the appropriate
598// nodes based on the type of node we are checking. This simplifies life a
599// bit for the callers.
600static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
601 SDOperand &CC) {
602 if (N.getOpcode() == ISD::SETCC) {
603 LHS = N.getOperand(0);
604 RHS = N.getOperand(1);
605 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000606 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000607 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000608 if (N.getOpcode() == ISD::SELECT_CC &&
609 N.getOperand(2).getOpcode() == ISD::Constant &&
610 N.getOperand(3).getOpcode() == ISD::Constant &&
611 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000612 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
613 LHS = N.getOperand(0);
614 RHS = N.getOperand(1);
615 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000616 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000617 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000618 return false;
619}
620
Nate Begeman99801192005-09-07 23:25:52 +0000621// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
622// one use. If this is true, it allows the users to invert the operation for
623// free when it is profitable to do so.
624static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000625 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000626 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000627 return true;
628 return false;
629}
630
Nate Begemancd4d58c2006-02-03 06:46:56 +0000631SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
632 MVT::ValueType VT = N0.getValueType();
633 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
634 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
635 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
636 if (isa<ConstantSDNode>(N1)) {
637 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000638 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000639 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
640 } else if (N0.hasOneUse()) {
641 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000642 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000643 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
644 }
645 }
646 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
647 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
648 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
649 if (isa<ConstantSDNode>(N0)) {
650 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000651 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000652 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
653 } else if (N1.hasOneUse()) {
654 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000655 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000656 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
657 }
658 }
659 return SDOperand();
660}
661
Nate Begeman4ebd8052005-09-01 23:24:04 +0000662void DAGCombiner::Run(bool RunningAfterLegalize) {
663 // set the instance variable, so that the various visit routines may use it.
664 AfterLegalize = RunningAfterLegalize;
665
Nate Begeman646d7e22005-09-02 21:18:40 +0000666 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000667 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
668 E = DAG.allnodes_end(); I != E; ++I)
669 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000670
Chris Lattner95038592005-10-05 06:35:28 +0000671 // Create a dummy node (which is not added to allnodes), that adds a reference
672 // to the root node, preventing it from being deleted, and tracking any
673 // changes of the root.
674 HandleSDNode Dummy(DAG.getRoot());
675
Jim Laskey26f7fa72006-10-17 19:33:52 +0000676 // The root of the dag may dangle to deleted nodes until the dag combiner is
677 // done. Set it to null to avoid confusion.
678 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000679
680 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
681 TargetLowering::DAGCombinerInfo
682 DagCombineInfo(DAG, !RunningAfterLegalize, this);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000683
Nate Begeman1d4d4142005-09-01 00:19:25 +0000684 // while the worklist isn't empty, inspect the node on the end of it and
685 // try and combine it.
686 while (!WorkList.empty()) {
687 SDNode *N = WorkList.back();
688 WorkList.pop_back();
689
690 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000691 // N is deleted from the DAG, since they too may now be dead or may have a
692 // reduced number of uses, allowing other xforms.
693 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000694 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000695 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000696
Chris Lattner95038592005-10-05 06:35:28 +0000697 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000698 continue;
699 }
700
Nate Begeman83e75ec2005-09-06 04:43:02 +0000701 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000702
703 // If nothing happened, try a target-specific DAG combine.
704 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000705 assert(N->getOpcode() != ISD::DELETED_NODE &&
706 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000707 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
708 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
709 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
710 }
711
Nate Begeman83e75ec2005-09-06 04:43:02 +0000712 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000713 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000714 // If we get back the same node we passed in, rather than a new node or
715 // zero, we know that the node must have defined multiple values and
716 // CombineTo was used. Since CombineTo takes care of the worklist
717 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000718 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000719 assert(N->getOpcode() != ISD::DELETED_NODE &&
720 RV.Val->getOpcode() != ISD::DELETED_NODE &&
721 "Node was deleted but visit returned new node!");
722
Jim Laskey6ff23e52006-10-04 16:53:27 +0000723 DEBUG(std::cerr << "\nReplacing.3 "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000724 std::cerr << "\nWith: "; RV.Val->dump(&DAG);
Nate Begeman2300f552005-09-07 00:15:36 +0000725 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000726 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000727 if (N->getNumValues() == RV.Val->getNumValues())
728 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
729 else {
730 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
731 SDOperand OpV = RV;
732 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
733 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000734
735 // Push the new node and any users onto the worklist
Jim Laskey6ff23e52006-10-04 16:53:27 +0000736 AddToWorkList(RV.Val);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000737 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000738
Jim Laskey6ff23e52006-10-04 16:53:27 +0000739 // Nodes can be reintroduced into the worklist. Make sure we do not
740 // process a node that has been replaced.
Nate Begeman646d7e22005-09-02 21:18:40 +0000741 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000742 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
743 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000744
745 // Finally, since the node is now dead, remove it from the graph.
746 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000747 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000748 }
749 }
Chris Lattner95038592005-10-05 06:35:28 +0000750
751 // If the root changed (e.g. it was a dead load, update the root).
752 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000753}
754
Nate Begeman83e75ec2005-09-06 04:43:02 +0000755SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000756 switch(N->getOpcode()) {
757 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000758 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000759 case ISD::ADD: return visitADD(N);
760 case ISD::SUB: return visitSUB(N);
761 case ISD::MUL: return visitMUL(N);
762 case ISD::SDIV: return visitSDIV(N);
763 case ISD::UDIV: return visitUDIV(N);
764 case ISD::SREM: return visitSREM(N);
765 case ISD::UREM: return visitUREM(N);
766 case ISD::MULHU: return visitMULHU(N);
767 case ISD::MULHS: return visitMULHS(N);
768 case ISD::AND: return visitAND(N);
769 case ISD::OR: return visitOR(N);
770 case ISD::XOR: return visitXOR(N);
771 case ISD::SHL: return visitSHL(N);
772 case ISD::SRA: return visitSRA(N);
773 case ISD::SRL: return visitSRL(N);
774 case ISD::CTLZ: return visitCTLZ(N);
775 case ISD::CTTZ: return visitCTTZ(N);
776 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000777 case ISD::SELECT: return visitSELECT(N);
778 case ISD::SELECT_CC: return visitSELECT_CC(N);
779 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000780 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
781 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000782 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000783 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
784 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000785 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000786 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000787 case ISD::FADD: return visitFADD(N);
788 case ISD::FSUB: return visitFSUB(N);
789 case ISD::FMUL: return visitFMUL(N);
790 case ISD::FDIV: return visitFDIV(N);
791 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000792 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000793 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
794 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
795 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
796 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
797 case ISD::FP_ROUND: return visitFP_ROUND(N);
798 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
799 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
800 case ISD::FNEG: return visitFNEG(N);
801 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000802 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000803 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000804 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000805 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000806 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
807 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000808 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000809 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000810 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000811 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
812 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
813 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
814 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
815 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
816 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
817 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
818 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000819 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000820 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000821}
822
Chris Lattner6270f682006-10-08 22:57:01 +0000823/// getInputChainForNode - Given a node, return its input chain if it has one,
824/// otherwise return a null sd operand.
825static SDOperand getInputChainForNode(SDNode *N) {
826 if (unsigned NumOps = N->getNumOperands()) {
827 if (N->getOperand(0).getValueType() == MVT::Other)
828 return N->getOperand(0);
829 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
830 return N->getOperand(NumOps-1);
831 for (unsigned i = 1; i < NumOps-1; ++i)
832 if (N->getOperand(i).getValueType() == MVT::Other)
833 return N->getOperand(i);
834 }
835 return SDOperand(0, 0);
836}
837
Nate Begeman83e75ec2005-09-06 04:43:02 +0000838SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000839 // If N has two operands, where one has an input chain equal to the other,
840 // the 'other' chain is redundant.
841 if (N->getNumOperands() == 2) {
842 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
843 return N->getOperand(0);
844 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
845 return N->getOperand(1);
846 }
847
848
Jim Laskey6ff23e52006-10-04 16:53:27 +0000849 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Jim Laskey279f0532006-09-25 16:29:54 +0000850 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000851 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000852
853 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000854 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000855
Jim Laskey71382342006-10-07 23:37:56 +0000856 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000857 // encountered.
858 for (unsigned i = 0; i < TFs.size(); ++i) {
859 SDNode *TF = TFs[i];
860
Jim Laskey6ff23e52006-10-04 16:53:27 +0000861 // Check each of the operands.
862 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
863 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000864
Jim Laskey6ff23e52006-10-04 16:53:27 +0000865 switch (Op.getOpcode()) {
866 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000867 // Entry tokens don't need to be added to the list. They are
868 // rededundant.
869 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000870 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000871
Jim Laskey6ff23e52006-10-04 16:53:27 +0000872 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000873 if ((CombinerAA || Op.hasOneUse()) &&
874 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000875 // Queue up for processing.
876 TFs.push_back(Op.Val);
877 // Clean up in case the token factor is removed.
878 AddToWorkList(Op.Val);
879 Changed = true;
880 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000881 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000882 // Fall thru
883
884 default:
Jim Laskeybc588b82006-10-05 15:07:25 +0000885 // Only add if not there prior.
886 if (std::find(Ops.begin(), Ops.end(), Op) == Ops.end())
887 Ops.push_back(Op);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000888 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000889 }
890 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000891 }
892
893 SDOperand Result;
894
895 // If we've change things around then replace token factor.
896 if (Changed) {
897 if (Ops.size() == 0) {
898 // The entry token is the only possible outcome.
899 Result = DAG.getEntryNode();
900 } else {
901 // New and improved token factor.
902 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000903 }
Jim Laskey274062c2006-10-13 23:32:28 +0000904
905 // Don't add users to work list.
906 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000907 }
Jim Laskey279f0532006-09-25 16:29:54 +0000908
Jim Laskey6ff23e52006-10-04 16:53:27 +0000909 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000910}
911
Nate Begeman83e75ec2005-09-06 04:43:02 +0000912SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000913 SDOperand N0 = N->getOperand(0);
914 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000915 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
916 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000917 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000918
919 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000920 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000921 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000922 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000923 if (N0C && !N1C)
924 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000925 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000926 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000927 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000928 // fold ((c1-A)+c2) -> (c1+c2)-A
929 if (N1C && N0.getOpcode() == ISD::SUB)
930 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
931 return DAG.getNode(ISD::SUB, VT,
932 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
933 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000934 // reassociate add
935 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
936 if (RADD.Val != 0)
937 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000938 // fold ((0-A) + B) -> B-A
939 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
940 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000941 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000942 // fold (A + (0-B)) -> A-B
943 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
944 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000945 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000946 // fold (A+(B-A)) -> B
947 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000948 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000949
Evan Cheng860771d2006-03-01 01:09:54 +0000950 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000951 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000952
953 // fold (a+b) -> (a|b) iff a and b share no bits.
954 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
955 uint64_t LHSZero, LHSOne;
956 uint64_t RHSZero, RHSOne;
957 uint64_t Mask = MVT::getIntVTBitMask(VT);
958 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
959 if (LHSZero) {
960 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
961
962 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
963 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
964 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
965 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
966 return DAG.getNode(ISD::OR, VT, N0, N1);
967 }
968 }
Evan Cheng3ef554d2006-11-06 08:14:30 +0000969
Nate Begeman83e75ec2005-09-06 04:43:02 +0000970 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000971}
972
Nate Begeman83e75ec2005-09-06 04:43:02 +0000973SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000974 SDOperand N0 = N->getOperand(0);
975 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000976 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
977 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000978 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000979
Chris Lattner854077d2005-10-17 01:07:11 +0000980 // fold (sub x, x) -> 0
981 if (N0 == N1)
982 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000983 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000984 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000985 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000986 // fold (sub x, c) -> (add x, -c)
987 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000988 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000989 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000990 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000991 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000992 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000993 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000994 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000995 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000996}
997
Nate Begeman83e75ec2005-09-06 04:43:02 +0000998SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000999 SDOperand N0 = N->getOperand(0);
1000 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001001 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1002 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +00001003 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001004
1005 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001006 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001007 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001008 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001009 if (N0C && !N1C)
1010 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001011 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001012 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001013 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001014 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001015 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001016 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001017 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +00001018 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +00001019 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001020 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001021 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001022 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1023 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1024 // FIXME: If the input is something that is easily negated (e.g. a
1025 // single-use add), we should put the negate there.
1026 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1027 DAG.getNode(ISD::SHL, VT, N0,
1028 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1029 TLI.getShiftAmountTy())));
1030 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001031
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001032 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1033 if (N1C && N0.getOpcode() == ISD::SHL &&
1034 isa<ConstantSDNode>(N0.getOperand(1))) {
1035 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001036 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001037 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1038 }
1039
1040 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1041 // use.
1042 {
1043 SDOperand Sh(0,0), Y(0,0);
1044 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1045 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1046 N0.Val->hasOneUse()) {
1047 Sh = N0; Y = N1;
1048 } else if (N1.getOpcode() == ISD::SHL &&
1049 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1050 Sh = N1; Y = N0;
1051 }
1052 if (Sh.Val) {
1053 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1054 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1055 }
1056 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001057 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1058 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1059 isa<ConstantSDNode>(N0.getOperand(1))) {
1060 return DAG.getNode(ISD::ADD, VT,
1061 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1062 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1063 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001064
Nate Begemancd4d58c2006-02-03 06:46:56 +00001065 // reassociate mul
1066 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1067 if (RMUL.Val != 0)
1068 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +00001069 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001070}
1071
Nate Begeman83e75ec2005-09-06 04:43:02 +00001072SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001073 SDOperand N0 = N->getOperand(0);
1074 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001075 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1076 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001077 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001078
1079 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001080 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001081 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001082 // fold (sdiv X, 1) -> X
1083 if (N1C && N1C->getSignExtended() == 1LL)
1084 return N0;
1085 // fold (sdiv X, -1) -> 0-X
1086 if (N1C && N1C->isAllOnesValue())
1087 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001088 // If we know the sign bits of both operands are zero, strength reduce to a
1089 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
1090 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001091 if (TLI.MaskedValueIsZero(N1, SignBit) &&
1092 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +00001093 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001094 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +00001095 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001096 (isPowerOf2_64(N1C->getSignExtended()) ||
1097 isPowerOf2_64(-N1C->getSignExtended()))) {
1098 // If dividing by powers of two is cheap, then don't perform the following
1099 // fold.
1100 if (TLI.isPow2DivCheap())
1101 return SDOperand();
1102 int64_t pow2 = N1C->getSignExtended();
1103 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001104 unsigned lg2 = Log2_64(abs2);
1105 // Splat the sign bit into the register
1106 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001107 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1108 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001109 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001110 // Add (N0 < 0) ? abs2 - 1 : 0;
1111 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1112 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001113 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001114 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001115 AddToWorkList(SRL.Val);
1116 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001117 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1118 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001119 // If we're dividing by a positive value, we're done. Otherwise, we must
1120 // negate the result.
1121 if (pow2 > 0)
1122 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001123 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001124 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1125 }
Nate Begeman69575232005-10-20 02:15:44 +00001126 // if integer divide is expensive and we satisfy the requirements, emit an
1127 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001128 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001129 !TLI.isIntDivCheap()) {
1130 SDOperand Op = BuildSDIV(N);
1131 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001132 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001133 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001134}
1135
Nate Begeman83e75ec2005-09-06 04:43:02 +00001136SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001137 SDOperand N0 = N->getOperand(0);
1138 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001139 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1140 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001141 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001142
1143 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001144 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001145 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001146 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +00001147 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001148 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001149 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001150 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001151 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1152 if (N1.getOpcode() == ISD::SHL) {
1153 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1154 if (isPowerOf2_64(SHC->getValue())) {
1155 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001156 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
1157 DAG.getConstant(Log2_64(SHC->getValue()),
1158 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001159 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001160 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001161 }
1162 }
1163 }
Nate Begeman69575232005-10-20 02:15:44 +00001164 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +00001165 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
1166 SDOperand Op = BuildUDIV(N);
1167 if (Op.Val) return Op;
1168 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001169 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001170}
1171
Nate Begeman83e75ec2005-09-06 04:43:02 +00001172SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001173 SDOperand N0 = N->getOperand(0);
1174 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001175 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1176 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001177 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001178
1179 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001180 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001181 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001182 // If we know the sign bits of both operands are zero, strength reduce to a
1183 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
1184 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001185 if (TLI.MaskedValueIsZero(N1, SignBit) &&
1186 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +00001187 return DAG.getNode(ISD::UREM, VT, N0, N1);
Chris Lattner26d29902006-10-12 20:58:32 +00001188
1189 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1190 // the remainder operation.
1191 if (N1C && !N1C->isNullValue()) {
1192 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
1193 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1194 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1195 AddToWorkList(Div.Val);
1196 AddToWorkList(Mul.Val);
1197 return Sub;
1198 }
1199
Nate Begeman83e75ec2005-09-06 04:43:02 +00001200 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001201}
1202
Nate Begeman83e75ec2005-09-06 04:43:02 +00001203SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001204 SDOperand N0 = N->getOperand(0);
1205 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001206 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1207 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001208 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001209
1210 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001211 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001212 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001213 // fold (urem x, pow2) -> (and x, pow2-1)
1214 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +00001215 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001216 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1217 if (N1.getOpcode() == ISD::SHL) {
1218 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1219 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +00001220 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001221 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001222 return DAG.getNode(ISD::AND, VT, N0, Add);
1223 }
1224 }
1225 }
Chris Lattner26d29902006-10-12 20:58:32 +00001226
1227 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1228 // the remainder operation.
1229 if (N1C && !N1C->isNullValue()) {
1230 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
1231 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1232 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1233 AddToWorkList(Div.Val);
1234 AddToWorkList(Mul.Val);
1235 return Sub;
1236 }
1237
Nate Begeman83e75ec2005-09-06 04:43:02 +00001238 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001239}
1240
Nate Begeman83e75ec2005-09-06 04:43:02 +00001241SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001242 SDOperand N0 = N->getOperand(0);
1243 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001244 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001245
1246 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001247 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001248 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001249 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001250 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001251 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1252 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001253 TLI.getShiftAmountTy()));
1254 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001255}
1256
Nate Begeman83e75ec2005-09-06 04:43:02 +00001257SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001258 SDOperand N0 = N->getOperand(0);
1259 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001260 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001261
1262 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001263 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001264 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001265 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001266 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001267 return DAG.getConstant(0, N0.getValueType());
1268 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001269}
1270
Chris Lattner35e5c142006-05-05 05:51:50 +00001271/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1272/// two operands of the same opcode, try to simplify it.
1273SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1274 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1275 MVT::ValueType VT = N0.getValueType();
1276 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1277
Chris Lattner540121f2006-05-05 06:31:05 +00001278 // For each of OP in AND/OR/XOR:
1279 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1280 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1281 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001282 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001283 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001284 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001285 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1286 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1287 N0.getOperand(0).getValueType(),
1288 N0.getOperand(0), N1.getOperand(0));
1289 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001290 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001291 }
1292
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001293 // For each of OP in SHL/SRL/SRA/AND...
1294 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1295 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1296 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001297 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001298 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001299 N0.getOperand(1) == N1.getOperand(1)) {
1300 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1301 N0.getOperand(0).getValueType(),
1302 N0.getOperand(0), N1.getOperand(0));
1303 AddToWorkList(ORNode.Val);
1304 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1305 }
1306
1307 return SDOperand();
1308}
1309
Nate Begeman83e75ec2005-09-06 04:43:02 +00001310SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001311 SDOperand N0 = N->getOperand(0);
1312 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001313 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001314 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1315 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001316 MVT::ValueType VT = N1.getValueType();
1317
1318 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001319 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001320 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001321 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001322 if (N0C && !N1C)
1323 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001324 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001325 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001326 return N0;
1327 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +00001328 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001329 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001330 // reassociate and
1331 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1332 if (RAND.Val != 0)
1333 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001334 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001335 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001336 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001337 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001338 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001339 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1340 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001341 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +00001342 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001343 ~N1C->getValue() & InMask)) {
1344 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1345 N0.getOperand(0));
1346
1347 // Replace uses of the AND with uses of the Zero extend node.
1348 CombineTo(N, Zext);
1349
Chris Lattner3603cd62006-02-02 07:17:31 +00001350 // We actually want to replace all uses of the any_extend with the
1351 // zero_extend, to avoid duplicating things. This will later cause this
1352 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001353 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001354 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001355 }
1356 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001357 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1358 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1359 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1360 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1361
1362 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1363 MVT::isInteger(LL.getValueType())) {
1364 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1365 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1366 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001367 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001368 return DAG.getSetCC(VT, ORNode, LR, Op1);
1369 }
1370 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1371 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1372 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001373 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001374 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1375 }
1376 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1377 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1378 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001379 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001380 return DAG.getSetCC(VT, ORNode, LR, Op1);
1381 }
1382 }
1383 // canonicalize equivalent to ll == rl
1384 if (LL == RR && LR == RL) {
1385 Op1 = ISD::getSetCCSwappedOperands(Op1);
1386 std::swap(RL, RR);
1387 }
1388 if (LL == RL && LR == RR) {
1389 bool isInteger = MVT::isInteger(LL.getValueType());
1390 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1391 if (Result != ISD::SETCC_INVALID)
1392 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1393 }
1394 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001395
1396 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1397 if (N0.getOpcode() == N1.getOpcode()) {
1398 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1399 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001400 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001401
Nate Begemande996292006-02-03 22:24:05 +00001402 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1403 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001404 if (!MVT::isVector(VT) &&
1405 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001406 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001407 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Chengc5484282006-10-04 00:56:09 +00001408 if (ISD::isEXTLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001409 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001410 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001411 // If we zero all the possible extended bits, then we can turn this into
1412 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001413 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001414 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001415 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1416 LN0->getBasePtr(), LN0->getSrcValue(),
1417 LN0->getSrcValueOffset(), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001418 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001419 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001420 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001421 }
1422 }
1423 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Chengc5484282006-10-04 00:56:09 +00001424 if (ISD::isSEXTLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001425 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001426 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001427 // If we zero all the possible extended bits, then we can turn this into
1428 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001429 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001430 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001431 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1432 LN0->getBasePtr(), LN0->getSrcValue(),
1433 LN0->getSrcValueOffset(), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001434 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001435 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001436 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001437 }
1438 }
Chris Lattner15045b62006-02-28 06:35:35 +00001439
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001440 // fold (and (load x), 255) -> (zextload x, i8)
1441 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001442 if (N1C && N0.getOpcode() == ISD::LOAD) {
1443 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1444 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
1445 N0.hasOneUse()) {
1446 MVT::ValueType EVT, LoadedVT;
1447 if (N1C->getValue() == 255)
1448 EVT = MVT::i8;
1449 else if (N1C->getValue() == 65535)
1450 EVT = MVT::i16;
1451 else if (N1C->getValue() == ~0U)
1452 EVT = MVT::i32;
1453 else
1454 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001455
Evan Cheng2e49f092006-10-11 07:10:22 +00001456 LoadedVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001457 if (EVT != MVT::Other && LoadedVT > EVT &&
1458 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1459 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1460 // For big endian targets, we need to add an offset to the pointer to
1461 // load the correct bytes. For little endian systems, we merely need to
1462 // read fewer bytes from the same pointer.
1463 unsigned PtrOff =
1464 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1465 SDOperand NewPtr = LN0->getBasePtr();
1466 if (!TLI.isLittleEndian())
1467 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1468 DAG.getConstant(PtrOff, PtrType));
1469 AddToWorkList(NewPtr.Val);
1470 SDOperand Load =
1471 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
1472 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT);
1473 AddToWorkList(N);
1474 CombineTo(N0.Val, Load, Load.getValue(1));
1475 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1476 }
Chris Lattner15045b62006-02-28 06:35:35 +00001477 }
1478 }
1479
Nate Begeman83e75ec2005-09-06 04:43:02 +00001480 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001481}
1482
Nate Begeman83e75ec2005-09-06 04:43:02 +00001483SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001484 SDOperand N0 = N->getOperand(0);
1485 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001486 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001487 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1488 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001489 MVT::ValueType VT = N1.getValueType();
1490 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001491
1492 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001493 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001494 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001495 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001496 if (N0C && !N1C)
1497 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001498 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001499 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001500 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001501 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001502 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001503 return N1;
1504 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001505 if (N1C &&
1506 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001507 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001508 // reassociate or
1509 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1510 if (ROR.Val != 0)
1511 return ROR;
1512 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1513 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001514 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001515 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1516 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1517 N1),
1518 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001519 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001520 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1521 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1522 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1523 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1524
1525 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1526 MVT::isInteger(LL.getValueType())) {
1527 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1528 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1529 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1530 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1531 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001532 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001533 return DAG.getSetCC(VT, ORNode, LR, Op1);
1534 }
1535 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1536 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1537 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1538 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1539 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001540 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001541 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1542 }
1543 }
1544 // canonicalize equivalent to ll == rl
1545 if (LL == RR && LR == RL) {
1546 Op1 = ISD::getSetCCSwappedOperands(Op1);
1547 std::swap(RL, RR);
1548 }
1549 if (LL == RL && LR == RR) {
1550 bool isInteger = MVT::isInteger(LL.getValueType());
1551 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1552 if (Result != ISD::SETCC_INVALID)
1553 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1554 }
1555 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001556
1557 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1558 if (N0.getOpcode() == N1.getOpcode()) {
1559 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1560 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001561 }
Chris Lattner516b9622006-09-14 20:50:57 +00001562
Chris Lattner1ec72732006-09-14 21:11:37 +00001563 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1564 if (N0.getOpcode() == ISD::AND &&
1565 N1.getOpcode() == ISD::AND &&
1566 N0.getOperand(1).getOpcode() == ISD::Constant &&
1567 N1.getOperand(1).getOpcode() == ISD::Constant &&
1568 // Don't increase # computations.
1569 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1570 // We can only do this xform if we know that bits from X that are set in C2
1571 // but not in C1 are already zero. Likewise for Y.
1572 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1573 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1574
1575 if (TLI.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1576 TLI.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
1577 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1578 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1579 }
1580 }
1581
1582
Chris Lattner516b9622006-09-14 20:50:57 +00001583 // See if this is some rotate idiom.
1584 if (SDNode *Rot = MatchRotate(N0, N1))
1585 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001586
Nate Begeman83e75ec2005-09-06 04:43:02 +00001587 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001588}
1589
Chris Lattner516b9622006-09-14 20:50:57 +00001590
1591/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1592static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1593 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001594 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001595 Mask = Op.getOperand(1);
1596 Op = Op.getOperand(0);
1597 } else {
1598 return false;
1599 }
1600 }
1601
1602 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1603 Shift = Op;
1604 return true;
1605 }
1606 return false;
1607}
1608
1609
1610// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1611// idioms for rotate, and if the target supports rotation instructions, generate
1612// a rot[lr].
1613SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1614 // Must be a legal type. Expanded an promoted things won't work with rotates.
1615 MVT::ValueType VT = LHS.getValueType();
1616 if (!TLI.isTypeLegal(VT)) return 0;
1617
1618 // The target must have at least one rotate flavor.
1619 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1620 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1621 if (!HasROTL && !HasROTR) return 0;
1622
1623 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1624 SDOperand LHSShift; // The shift.
1625 SDOperand LHSMask; // AND value if any.
1626 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1627 return 0; // Not part of a rotate.
1628
1629 SDOperand RHSShift; // The shift.
1630 SDOperand RHSMask; // AND value if any.
1631 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1632 return 0; // Not part of a rotate.
1633
1634 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1635 return 0; // Not shifting the same value.
1636
1637 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1638 return 0; // Shifts must disagree.
1639
1640 // Canonicalize shl to left side in a shl/srl pair.
1641 if (RHSShift.getOpcode() == ISD::SHL) {
1642 std::swap(LHS, RHS);
1643 std::swap(LHSShift, RHSShift);
1644 std::swap(LHSMask , RHSMask );
1645 }
1646
1647 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1648
1649 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1650 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
1651 if (LHSShift.getOperand(1).getOpcode() == ISD::Constant &&
1652 RHSShift.getOperand(1).getOpcode() == ISD::Constant) {
1653 uint64_t LShVal = cast<ConstantSDNode>(LHSShift.getOperand(1))->getValue();
1654 uint64_t RShVal = cast<ConstantSDNode>(RHSShift.getOperand(1))->getValue();
1655 if ((LShVal + RShVal) != OpSizeInBits)
1656 return 0;
1657
1658 SDOperand Rot;
1659 if (HasROTL)
1660 Rot = DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1661 LHSShift.getOperand(1));
1662 else
1663 Rot = DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1664 RHSShift.getOperand(1));
1665
1666 // If there is an AND of either shifted operand, apply it to the result.
1667 if (LHSMask.Val || RHSMask.Val) {
1668 uint64_t Mask = MVT::getIntVTBitMask(VT);
1669
1670 if (LHSMask.Val) {
1671 uint64_t RHSBits = (1ULL << LShVal)-1;
1672 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1673 }
1674 if (RHSMask.Val) {
1675 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1676 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1677 }
1678
1679 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1680 }
1681
1682 return Rot.Val;
1683 }
1684
1685 // If there is a mask here, and we have a variable shift, we can't be sure
1686 // that we're masking out the right stuff.
1687 if (LHSMask.Val || RHSMask.Val)
1688 return 0;
1689
1690 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1691 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
1692 if (RHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1693 LHSShift.getOperand(1) == RHSShift.getOperand(1).getOperand(1)) {
1694 if (ConstantSDNode *SUBC =
1695 dyn_cast<ConstantSDNode>(RHSShift.getOperand(1).getOperand(0))) {
1696 if (SUBC->getValue() == OpSizeInBits)
1697 if (HasROTL)
1698 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1699 LHSShift.getOperand(1)).Val;
1700 else
1701 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1702 LHSShift.getOperand(1)).Val;
1703 }
1704 }
1705
1706 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1707 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
1708 if (LHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1709 RHSShift.getOperand(1) == LHSShift.getOperand(1).getOperand(1)) {
1710 if (ConstantSDNode *SUBC =
1711 dyn_cast<ConstantSDNode>(LHSShift.getOperand(1).getOperand(0))) {
1712 if (SUBC->getValue() == OpSizeInBits)
1713 if (HasROTL)
1714 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1715 LHSShift.getOperand(1)).Val;
1716 else
1717 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1718 RHSShift.getOperand(1)).Val;
1719 }
1720 }
1721
1722 return 0;
1723}
1724
1725
Nate Begeman83e75ec2005-09-06 04:43:02 +00001726SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001727 SDOperand N0 = N->getOperand(0);
1728 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001729 SDOperand LHS, RHS, CC;
1730 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1731 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001732 MVT::ValueType VT = N0.getValueType();
1733
1734 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001735 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001736 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001737 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001738 if (N0C && !N1C)
1739 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001740 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001741 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001742 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001743 // reassociate xor
1744 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1745 if (RXOR.Val != 0)
1746 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001747 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001748 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1749 bool isInt = MVT::isInteger(LHS.getValueType());
1750 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1751 isInt);
1752 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001753 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001754 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001755 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001756 assert(0 && "Unhandled SetCC Equivalent!");
1757 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001758 }
Nate Begeman99801192005-09-07 23:25:52 +00001759 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1760 if (N1C && N1C->getValue() == 1 &&
1761 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001762 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001763 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1764 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001765 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1766 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001767 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001768 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001769 }
1770 }
Nate Begeman99801192005-09-07 23:25:52 +00001771 // fold !(x or y) -> (!x and !y) iff x or y are constants
1772 if (N1C && N1C->isAllOnesValue() &&
1773 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001774 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001775 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1776 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001777 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1778 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001779 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001780 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001781 }
1782 }
Nate Begeman223df222005-09-08 20:18:10 +00001783 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1784 if (N1C && N0.getOpcode() == ISD::XOR) {
1785 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1786 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1787 if (N00C)
1788 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1789 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1790 if (N01C)
1791 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1792 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1793 }
1794 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001795 if (N0 == N1) {
1796 if (!MVT::isVector(VT)) {
1797 return DAG.getConstant(0, VT);
1798 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1799 // Produce a vector of zeros.
1800 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1801 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001802 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001803 }
1804 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001805
1806 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1807 if (N0.getOpcode() == N1.getOpcode()) {
1808 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1809 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001810 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001811
Chris Lattner3e104b12006-04-08 04:15:24 +00001812 // Simplify the expression using non-local knowledge.
1813 if (!MVT::isVector(VT) &&
1814 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001815 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001816
Nate Begeman83e75ec2005-09-06 04:43:02 +00001817 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001818}
1819
Nate Begeman83e75ec2005-09-06 04:43:02 +00001820SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001821 SDOperand N0 = N->getOperand(0);
1822 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001823 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1824 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001825 MVT::ValueType VT = N0.getValueType();
1826 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1827
1828 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001829 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001830 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001831 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001832 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001833 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001834 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001835 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001836 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001837 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001838 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001839 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001840 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001841 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001842 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001843 if (SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001844 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001845 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001846 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001847 N0.getOperand(1).getOpcode() == ISD::Constant) {
1848 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001849 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001850 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001851 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001852 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001853 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001854 }
1855 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1856 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001857 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001858 N0.getOperand(1).getOpcode() == ISD::Constant) {
1859 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001860 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001861 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1862 DAG.getConstant(~0ULL << c1, VT));
1863 if (c2 > c1)
1864 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001865 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001866 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001867 return DAG.getNode(ISD::SRL, VT, Mask,
1868 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001869 }
1870 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001871 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001872 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001873 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnercac70592006-03-05 19:53:55 +00001874 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1875 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1876 isa<ConstantSDNode>(N0.getOperand(1))) {
1877 return DAG.getNode(ISD::ADD, VT,
1878 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1879 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1880 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001881 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001882}
1883
Nate Begeman83e75ec2005-09-06 04:43:02 +00001884SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001885 SDOperand N0 = N->getOperand(0);
1886 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001887 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1888 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001889 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001890
1891 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001892 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001893 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001894 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001895 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001896 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001897 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001898 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001899 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001900 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001901 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001902 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001903 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001904 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001905 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001906 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1907 // sext_inreg.
1908 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1909 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1910 MVT::ValueType EVT;
1911 switch (LowBits) {
1912 default: EVT = MVT::Other; break;
1913 case 1: EVT = MVT::i1; break;
1914 case 8: EVT = MVT::i8; break;
1915 case 16: EVT = MVT::i16; break;
1916 case 32: EVT = MVT::i32; break;
1917 }
1918 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1919 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1920 DAG.getValueType(EVT));
1921 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001922
1923 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1924 if (N1C && N0.getOpcode() == ISD::SRA) {
1925 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1926 unsigned Sum = N1C->getValue() + C1->getValue();
1927 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1928 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1929 DAG.getConstant(Sum, N1C->getValueType(0)));
1930 }
1931 }
1932
Chris Lattnera8504462006-05-08 20:51:54 +00001933 // Simplify, based on bits shifted out of the LHS.
1934 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
1935 return SDOperand(N, 0);
1936
1937
Nate Begeman1d4d4142005-09-01 00:19:25 +00001938 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001939 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001940 return DAG.getNode(ISD::SRL, VT, N0, N1);
1941 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001942}
1943
Nate Begeman83e75ec2005-09-06 04:43:02 +00001944SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001945 SDOperand N0 = N->getOperand(0);
1946 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001947 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1948 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001949 MVT::ValueType VT = N0.getValueType();
1950 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1951
1952 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001953 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001954 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001955 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001956 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001957 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001958 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001959 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001960 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001961 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001962 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001963 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001964 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001965 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001966 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001967 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001968 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001969 N0.getOperand(1).getOpcode() == ISD::Constant) {
1970 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001971 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001972 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001973 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001974 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001975 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001976 }
Chris Lattner350bec02006-04-02 06:11:11 +00001977
Chris Lattner06afe072006-05-05 22:53:17 +00001978 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
1979 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1980 // Shifting in all undef bits?
1981 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
1982 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
1983 return DAG.getNode(ISD::UNDEF, VT);
1984
1985 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
1986 AddToWorkList(SmallShift.Val);
1987 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
1988 }
1989
Chris Lattner3657ffe2006-10-12 20:23:19 +00001990 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
1991 // bit, which is unmodified by sra.
1992 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
1993 if (N0.getOpcode() == ISD::SRA)
1994 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
1995 }
1996
Chris Lattner350bec02006-04-02 06:11:11 +00001997 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1998 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1999 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
2000 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
2001 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
2002
2003 // If any of the input bits are KnownOne, then the input couldn't be all
2004 // zeros, thus the result of the srl will always be zero.
2005 if (KnownOne) return DAG.getConstant(0, VT);
2006
2007 // If all of the bits input the to ctlz node are known to be zero, then
2008 // the result of the ctlz is "32" and the result of the shift is one.
2009 uint64_t UnknownBits = ~KnownZero & Mask;
2010 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2011
2012 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2013 if ((UnknownBits & (UnknownBits-1)) == 0) {
2014 // Okay, we know that only that the single bit specified by UnknownBits
2015 // could be set on input to the CTLZ node. If this bit is set, the SRL
2016 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2017 // to an SRL,XOR pair, which is likely to simplify more.
2018 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
2019 SDOperand Op = N0.getOperand(0);
2020 if (ShAmt) {
2021 Op = DAG.getNode(ISD::SRL, VT, Op,
2022 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2023 AddToWorkList(Op.Val);
2024 }
2025 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2026 }
2027 }
2028
Nate Begeman83e75ec2005-09-06 04:43:02 +00002029 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002030}
2031
Nate Begeman83e75ec2005-09-06 04:43:02 +00002032SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002033 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002034 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002035
2036 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002037 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002038 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002039 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002040}
2041
Nate Begeman83e75ec2005-09-06 04:43:02 +00002042SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002043 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002044 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002045
2046 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002047 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002048 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002049 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002050}
2051
Nate Begeman83e75ec2005-09-06 04:43:02 +00002052SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002053 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002054 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002055
2056 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002057 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002058 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002059 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002060}
2061
Nate Begeman452d7be2005-09-16 00:54:12 +00002062SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2063 SDOperand N0 = N->getOperand(0);
2064 SDOperand N1 = N->getOperand(1);
2065 SDOperand N2 = N->getOperand(2);
2066 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2067 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2068 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2069 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00002070
Nate Begeman452d7be2005-09-16 00:54:12 +00002071 // fold select C, X, X -> X
2072 if (N1 == N2)
2073 return N1;
2074 // fold select true, X, Y -> X
2075 if (N0C && !N0C->isNullValue())
2076 return N1;
2077 // fold select false, X, Y -> Y
2078 if (N0C && N0C->isNullValue())
2079 return N2;
2080 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002081 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002082 return DAG.getNode(ISD::OR, VT, N0, N2);
2083 // fold select C, 0, X -> ~C & X
2084 // FIXME: this should check for C type == X type, not i1?
2085 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
2086 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002087 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002088 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2089 }
2090 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002091 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002092 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002093 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002094 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2095 }
2096 // fold select C, X, 0 -> C & X
2097 // FIXME: this should check for C type == X type, not i1?
2098 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2099 return DAG.getNode(ISD::AND, VT, N0, N1);
2100 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2101 if (MVT::i1 == VT && N0 == N1)
2102 return DAG.getNode(ISD::OR, VT, N0, N2);
2103 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2104 if (MVT::i1 == VT && N0 == N2)
2105 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002106
Chris Lattner40c62d52005-10-18 06:04:22 +00002107 // If we can fold this based on the true/false value, do so.
2108 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002109 return SDOperand(N, 0); // Don't revisit N.
2110
Nate Begeman44728a72005-09-19 22:34:01 +00002111 // fold selects based on a setcc into other things, such as min/max/abs
2112 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00002113 // FIXME:
2114 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2115 // having to say they don't support SELECT_CC on every type the DAG knows
2116 // about, since there is no way to mark an opcode illegal at all value types
2117 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2118 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2119 N1, N2, N0.getOperand(2));
2120 else
2121 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002122 return SDOperand();
2123}
2124
2125SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002126 SDOperand N0 = N->getOperand(0);
2127 SDOperand N1 = N->getOperand(1);
2128 SDOperand N2 = N->getOperand(2);
2129 SDOperand N3 = N->getOperand(3);
2130 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002131 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2132
Nate Begeman44728a72005-09-19 22:34:01 +00002133 // fold select_cc lhs, rhs, x, x, cc -> x
2134 if (N2 == N3)
2135 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002136
Chris Lattner5f42a242006-09-20 06:19:26 +00002137 // Determine if the condition we're dealing with is constant
2138 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002139 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002140
2141 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
2142 if (SCCC->getValue())
2143 return N2; // cond always true -> true val
2144 else
2145 return N3; // cond always false -> false val
2146 }
2147
2148 // Fold to a simpler select_cc
2149 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2150 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2151 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2152 SCC.getOperand(2));
2153
Chris Lattner40c62d52005-10-18 06:04:22 +00002154 // If we can fold this based on the true/false value, do so.
2155 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002156 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002157
Nate Begeman44728a72005-09-19 22:34:01 +00002158 // fold select_cc into other things, such as min/max/abs
2159 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002160}
2161
2162SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2163 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2164 cast<CondCodeSDNode>(N->getOperand(2))->get());
2165}
2166
Nate Begeman83e75ec2005-09-06 04:43:02 +00002167SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002168 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002169 MVT::ValueType VT = N->getValueType(0);
2170
Nate Begeman1d4d4142005-09-01 00:19:25 +00002171 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002172 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002173 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002174
Nate Begeman1d4d4142005-09-01 00:19:25 +00002175 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002176 // fold (sext (aext x)) -> (sext x)
2177 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002178 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002179
Chris Lattner6007b842006-09-21 06:00:20 +00002180 // fold (sext (truncate x)) -> (sextinreg x).
2181 if (N0.getOpcode() == ISD::TRUNCATE &&
Chris Lattnerbf370872006-09-21 06:17:39 +00002182 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2183 N0.getValueType()))) {
Chris Lattner6007b842006-09-21 06:00:20 +00002184 SDOperand Op = N0.getOperand(0);
2185 if (Op.getValueType() < VT) {
2186 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2187 } else if (Op.getValueType() > VT) {
2188 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2189 }
2190 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00002191 DAG.getValueType(N0.getValueType()));
Chris Lattner6007b842006-09-21 06:00:20 +00002192 }
Chris Lattner310b5782006-05-06 23:06:26 +00002193
Evan Cheng110dec22005-12-14 02:19:23 +00002194 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002195 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002196 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng466685d2006-10-09 20:57:25 +00002197 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2198 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2199 LN0->getBasePtr(), LN0->getSrcValue(),
2200 LN0->getSrcValueOffset(),
Nate Begeman3df4d522005-10-12 20:40:40 +00002201 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00002202 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00002203 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2204 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002205 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002206 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002207
2208 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2209 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Chengc5484282006-10-04 00:56:09 +00002210 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002211 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002212 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002213 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2214 LN0->getBasePtr(), LN0->getSrcValue(),
2215 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002216 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002217 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2218 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002219 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002220 }
2221
Nate Begeman83e75ec2005-09-06 04:43:02 +00002222 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002223}
2224
Nate Begeman83e75ec2005-09-06 04:43:02 +00002225SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002226 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002227 MVT::ValueType VT = N->getValueType(0);
2228
Nate Begeman1d4d4142005-09-01 00:19:25 +00002229 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002230 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002231 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002232 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002233 // fold (zext (aext x)) -> (zext x)
2234 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002235 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002236
2237 // fold (zext (truncate x)) -> (and x, mask)
2238 if (N0.getOpcode() == ISD::TRUNCATE &&
2239 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2240 SDOperand Op = N0.getOperand(0);
2241 if (Op.getValueType() < VT) {
2242 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2243 } else if (Op.getValueType() > VT) {
2244 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2245 }
2246 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2247 }
2248
Chris Lattner111c2282006-09-21 06:14:31 +00002249 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2250 if (N0.getOpcode() == ISD::AND &&
2251 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2252 N0.getOperand(1).getOpcode() == ISD::Constant) {
2253 SDOperand X = N0.getOperand(0).getOperand(0);
2254 if (X.getValueType() < VT) {
2255 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2256 } else if (X.getValueType() > VT) {
2257 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2258 }
2259 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2260 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2261 }
2262
Evan Cheng110dec22005-12-14 02:19:23 +00002263 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002264 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002265 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002266 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2267 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2268 LN0->getBasePtr(), LN0->getSrcValue(),
2269 LN0->getSrcValueOffset(),
Evan Cheng110dec22005-12-14 02:19:23 +00002270 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00002271 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00002272 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2273 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002274 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00002275 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002276
2277 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2278 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Chengc5484282006-10-04 00:56:09 +00002279 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002280 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002281 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002282 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2283 LN0->getBasePtr(), LN0->getSrcValue(),
2284 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002285 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002286 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2287 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002288 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002289 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002290 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002291}
2292
Chris Lattner5ffc0662006-05-05 05:58:59 +00002293SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2294 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002295 MVT::ValueType VT = N->getValueType(0);
2296
2297 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002298 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002299 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2300 // fold (aext (aext x)) -> (aext x)
2301 // fold (aext (zext x)) -> (zext x)
2302 // fold (aext (sext x)) -> (sext x)
2303 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2304 N0.getOpcode() == ISD::ZERO_EXTEND ||
2305 N0.getOpcode() == ISD::SIGN_EXTEND)
2306 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2307
Chris Lattner84750582006-09-20 06:29:17 +00002308 // fold (aext (truncate x))
2309 if (N0.getOpcode() == ISD::TRUNCATE) {
2310 SDOperand TruncOp = N0.getOperand(0);
2311 if (TruncOp.getValueType() == VT)
2312 return TruncOp; // x iff x size == zext size.
2313 if (TruncOp.getValueType() > VT)
2314 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2315 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2316 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002317
2318 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2319 if (N0.getOpcode() == ISD::AND &&
2320 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2321 N0.getOperand(1).getOpcode() == ISD::Constant) {
2322 SDOperand X = N0.getOperand(0).getOperand(0);
2323 if (X.getValueType() < VT) {
2324 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2325 } else if (X.getValueType() > VT) {
2326 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2327 }
2328 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2329 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2330 }
2331
Chris Lattner5ffc0662006-05-05 05:58:59 +00002332 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002333 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002334 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002335 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2336 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2337 LN0->getBasePtr(), LN0->getSrcValue(),
2338 LN0->getSrcValueOffset(),
Chris Lattner5ffc0662006-05-05 05:58:59 +00002339 N0.getValueType());
2340 CombineTo(N, ExtLoad);
2341 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2342 ExtLoad.getValue(1));
2343 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2344 }
2345
2346 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2347 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2348 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002349 if (N0.getOpcode() == ISD::LOAD && !ISD::isNON_EXTLoad(N0.Val) &&
2350 N0.hasOneUse()) {
2351 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002352 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002353 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
2354 LN0->getChain(), LN0->getBasePtr(),
2355 LN0->getSrcValue(),
2356 LN0->getSrcValueOffset(), EVT);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002357 CombineTo(N, ExtLoad);
2358 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2359 ExtLoad.getValue(1));
2360 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2361 }
2362 return SDOperand();
2363}
2364
2365
Nate Begeman83e75ec2005-09-06 04:43:02 +00002366SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002367 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002368 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002369 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002370 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00002371 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002372
Nate Begeman1d4d4142005-09-01 00:19:25 +00002373 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00002374 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00002375 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00002376
Chris Lattner541a24f2006-05-06 22:43:44 +00002377 // If the input is already sign extended, just drop the extension.
Chris Lattneree4ea922006-05-06 09:30:03 +00002378 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
2379 return N0;
2380
Nate Begeman646d7e22005-09-02 21:18:40 +00002381 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
2382 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2383 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00002384 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002385 }
Chris Lattner4b37e872006-05-08 21:18:59 +00002386
Nate Begeman07ed4172005-10-10 21:26:48 +00002387 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00002388 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00002389 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00002390
2391 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
2392 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
2393 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
2394 if (N0.getOpcode() == ISD::SRL) {
2395 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2396 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
2397 // We can turn this into an SRA iff the input to the SRL is already sign
2398 // extended enough.
2399 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
2400 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
2401 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
2402 }
2403 }
2404
Nate Begemanded49632005-10-13 03:11:28 +00002405 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00002406 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002407 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002408 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002409 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2410 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2411 LN0->getBasePtr(), LN0->getSrcValue(),
2412 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002413 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002414 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002415 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002416 }
2417 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Chengc5484282006-10-04 00:56:09 +00002418 if (ISD::isZEXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002419 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002420 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002421 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2422 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2423 LN0->getBasePtr(), LN0->getSrcValue(),
2424 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002425 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002426 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002427 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002428 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002429 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002430}
2431
Nate Begeman83e75ec2005-09-06 04:43:02 +00002432SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002433 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002434 MVT::ValueType VT = N->getValueType(0);
2435
2436 // noop truncate
2437 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002438 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002439 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002440 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002441 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002442 // fold (truncate (truncate x)) -> (truncate x)
2443 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002444 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002445 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00002446 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2447 N0.getOpcode() == ISD::ANY_EXTEND) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002448 if (N0.getValueType() < VT)
2449 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00002450 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002451 else if (N0.getValueType() > VT)
2452 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002453 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002454 else
2455 // if the source and dest are the same type, we can drop both the extend
2456 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002457 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002458 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002459 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng466685d2006-10-09 20:57:25 +00002460 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00002461 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
2462 "Cannot truncate to larger type!");
Evan Cheng466685d2006-10-09 20:57:25 +00002463 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Nate Begeman3df4d522005-10-12 20:40:40 +00002464 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00002465 // For big endian targets, we need to add an offset to the pointer to load
2466 // the correct bytes. For little endian systems, we merely need to read
2467 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00002468 uint64_t PtrOff =
2469 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Evan Cheng466685d2006-10-09 20:57:25 +00002470 SDOperand NewPtr = TLI.isLittleEndian() ? LN0->getBasePtr() :
2471 DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Nate Begeman765784a2005-10-12 23:18:53 +00002472 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00002473 AddToWorkList(NewPtr.Val);
Evan Cheng466685d2006-10-09 20:57:25 +00002474 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), NewPtr,
2475 LN0->getSrcValue(), LN0->getSrcValueOffset());
Chris Lattner5750df92006-03-01 04:03:14 +00002476 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00002477 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002478 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002479 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002480 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002481}
2482
Chris Lattner94683772005-12-23 05:30:37 +00002483SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2484 SDOperand N0 = N->getOperand(0);
2485 MVT::ValueType VT = N->getValueType(0);
2486
2487 // If the input is a constant, let getNode() fold it.
2488 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2489 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2490 if (Res.Val != N) return Res;
2491 }
2492
Chris Lattnerc8547d82005-12-23 05:37:50 +00002493 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2494 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00002495
Chris Lattner57104102005-12-23 05:44:41 +00002496 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002497 // FIXME: These xforms need to know that the resultant load doesn't need a
2498 // higher alignment than the original!
Evan Cheng466685d2006-10-09 20:57:25 +00002499 if (0 && ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse()) {
2500 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2501 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
2502 LN0->getSrcValue(), LN0->getSrcValueOffset());
Chris Lattner5750df92006-03-01 04:03:14 +00002503 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00002504 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2505 Load.getValue(1));
2506 return Load;
2507 }
2508
Chris Lattner94683772005-12-23 05:30:37 +00002509 return SDOperand();
2510}
2511
Chris Lattner6258fb22006-04-02 02:53:43 +00002512SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2513 SDOperand N0 = N->getOperand(0);
2514 MVT::ValueType VT = N->getValueType(0);
2515
2516 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2517 // First check to see if this is all constant.
2518 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2519 VT == MVT::Vector) {
2520 bool isSimple = true;
2521 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2522 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2523 N0.getOperand(i).getOpcode() != ISD::Constant &&
2524 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2525 isSimple = false;
2526 break;
2527 }
2528
Chris Lattner97c20732006-04-03 17:29:28 +00002529 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2530 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002531 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2532 }
2533 }
2534
2535 return SDOperand();
2536}
2537
2538/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2539/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2540/// destination element value type.
2541SDOperand DAGCombiner::
2542ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2543 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2544
2545 // If this is already the right type, we're done.
2546 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2547
2548 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2549 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2550
2551 // If this is a conversion of N elements of one type to N elements of another
2552 // type, convert each element. This handles FP<->INT cases.
2553 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002554 SmallVector<SDOperand, 8> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00002555 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002556 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002557 AddToWorkList(Ops.back().Val);
2558 }
Chris Lattner6258fb22006-04-02 02:53:43 +00002559 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2560 Ops.push_back(DAG.getValueType(DstEltVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002561 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002562 }
2563
2564 // Otherwise, we're growing or shrinking the elements. To avoid having to
2565 // handle annoying details of growing/shrinking FP values, we convert them to
2566 // int first.
2567 if (MVT::isFloatingPoint(SrcEltVT)) {
2568 // Convert the input float vector to a int vector where the elements are the
2569 // same sizes.
2570 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2571 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2572 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2573 SrcEltVT = IntVT;
2574 }
2575
2576 // Now we know the input is an integer vector. If the output is a FP type,
2577 // convert to integer first, then to FP of the right size.
2578 if (MVT::isFloatingPoint(DstEltVT)) {
2579 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2580 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2581 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2582
2583 // Next, convert to FP elements of the same size.
2584 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2585 }
2586
2587 // Okay, we know the src/dst types are both integers of differing types.
2588 // Handling growing first.
2589 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2590 if (SrcBitSize < DstBitSize) {
2591 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2592
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002593 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002594 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2595 i += NumInputsPerOutput) {
2596 bool isLE = TLI.isLittleEndian();
2597 uint64_t NewBits = 0;
2598 bool EltIsUndef = true;
2599 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2600 // Shift the previously computed bits over.
2601 NewBits <<= SrcBitSize;
2602 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2603 if (Op.getOpcode() == ISD::UNDEF) continue;
2604 EltIsUndef = false;
2605
2606 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2607 }
2608
2609 if (EltIsUndef)
2610 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2611 else
2612 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2613 }
2614
2615 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2616 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002617 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002618 }
2619
2620 // Finally, this must be the case where we are shrinking elements: each input
2621 // turns into multiple outputs.
2622 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002623 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002624 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2625 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2626 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2627 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2628 continue;
2629 }
2630 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2631
2632 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2633 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2634 OpVal >>= DstBitSize;
2635 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2636 }
2637
2638 // For big endian targets, swap the order of the pieces of each element.
2639 if (!TLI.isLittleEndian())
2640 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2641 }
2642 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2643 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002644 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002645}
2646
2647
2648
Chris Lattner01b3d732005-09-28 22:28:18 +00002649SDOperand DAGCombiner::visitFADD(SDNode *N) {
2650 SDOperand N0 = N->getOperand(0);
2651 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002652 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2653 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002654 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002655
2656 // fold (fadd c1, c2) -> c1+c2
2657 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002658 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002659 // canonicalize constant to RHS
2660 if (N0CFP && !N1CFP)
2661 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002662 // fold (A + (-B)) -> A-B
2663 if (N1.getOpcode() == ISD::FNEG)
2664 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002665 // fold ((-A) + B) -> B-A
2666 if (N0.getOpcode() == ISD::FNEG)
2667 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002668 return SDOperand();
2669}
2670
2671SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2672 SDOperand N0 = N->getOperand(0);
2673 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002674 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2675 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002676 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002677
2678 // fold (fsub c1, c2) -> c1-c2
2679 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002680 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002681 // fold (A-(-B)) -> A+B
2682 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00002683 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002684 return SDOperand();
2685}
2686
2687SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2688 SDOperand N0 = N->getOperand(0);
2689 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002690 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2691 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002692 MVT::ValueType VT = N->getValueType(0);
2693
Nate Begeman11af4ea2005-10-17 20:40:11 +00002694 // fold (fmul c1, c2) -> c1*c2
2695 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002696 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002697 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002698 if (N0CFP && !N1CFP)
2699 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002700 // fold (fmul X, 2.0) -> (fadd X, X)
2701 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2702 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002703 return SDOperand();
2704}
2705
2706SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2707 SDOperand N0 = N->getOperand(0);
2708 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002709 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2710 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002711 MVT::ValueType VT = N->getValueType(0);
2712
Nate Begemana148d982006-01-18 22:35:16 +00002713 // fold (fdiv c1, c2) -> c1/c2
2714 if (N0CFP && N1CFP)
2715 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002716 return SDOperand();
2717}
2718
2719SDOperand DAGCombiner::visitFREM(SDNode *N) {
2720 SDOperand N0 = N->getOperand(0);
2721 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002722 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2723 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002724 MVT::ValueType VT = N->getValueType(0);
2725
Nate Begemana148d982006-01-18 22:35:16 +00002726 // fold (frem c1, c2) -> fmod(c1,c2)
2727 if (N0CFP && N1CFP)
2728 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002729 return SDOperand();
2730}
2731
Chris Lattner12d83032006-03-05 05:30:57 +00002732SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2733 SDOperand N0 = N->getOperand(0);
2734 SDOperand N1 = N->getOperand(1);
2735 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2736 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2737 MVT::ValueType VT = N->getValueType(0);
2738
2739 if (N0CFP && N1CFP) // Constant fold
2740 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2741
2742 if (N1CFP) {
2743 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2744 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2745 union {
2746 double d;
2747 int64_t i;
2748 } u;
2749 u.d = N1CFP->getValue();
2750 if (u.i >= 0)
2751 return DAG.getNode(ISD::FABS, VT, N0);
2752 else
2753 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2754 }
2755
2756 // copysign(fabs(x), y) -> copysign(x, y)
2757 // copysign(fneg(x), y) -> copysign(x, y)
2758 // copysign(copysign(x,z), y) -> copysign(x, y)
2759 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2760 N0.getOpcode() == ISD::FCOPYSIGN)
2761 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2762
2763 // copysign(x, abs(y)) -> abs(x)
2764 if (N1.getOpcode() == ISD::FABS)
2765 return DAG.getNode(ISD::FABS, VT, N0);
2766
2767 // copysign(x, copysign(y,z)) -> copysign(x, z)
2768 if (N1.getOpcode() == ISD::FCOPYSIGN)
2769 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2770
2771 // copysign(x, fp_extend(y)) -> copysign(x, y)
2772 // copysign(x, fp_round(y)) -> copysign(x, y)
2773 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2774 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2775
2776 return SDOperand();
2777}
2778
2779
Chris Lattner01b3d732005-09-28 22:28:18 +00002780
Nate Begeman83e75ec2005-09-06 04:43:02 +00002781SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002782 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002783 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002784 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002785
2786 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002787 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002788 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002789 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002790}
2791
Nate Begeman83e75ec2005-09-06 04:43:02 +00002792SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002793 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002794 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002795 MVT::ValueType VT = N->getValueType(0);
2796
Nate Begeman1d4d4142005-09-01 00:19:25 +00002797 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002798 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002799 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002800 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002801}
2802
Nate Begeman83e75ec2005-09-06 04:43:02 +00002803SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002804 SDOperand N0 = N->getOperand(0);
2805 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2806 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002807
2808 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002809 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002810 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002811 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002812}
2813
Nate Begeman83e75ec2005-09-06 04:43:02 +00002814SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002815 SDOperand N0 = N->getOperand(0);
2816 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2817 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002818
2819 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002820 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002821 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002822 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002823}
2824
Nate Begeman83e75ec2005-09-06 04:43:02 +00002825SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002826 SDOperand N0 = N->getOperand(0);
2827 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2828 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002829
2830 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002831 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002832 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002833
2834 // fold (fp_round (fp_extend x)) -> x
2835 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2836 return N0.getOperand(0);
2837
2838 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2839 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2840 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2841 AddToWorkList(Tmp.Val);
2842 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2843 }
2844
Nate Begeman83e75ec2005-09-06 04:43:02 +00002845 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002846}
2847
Nate Begeman83e75ec2005-09-06 04:43:02 +00002848SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002849 SDOperand N0 = N->getOperand(0);
2850 MVT::ValueType VT = N->getValueType(0);
2851 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002852 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002853
Nate Begeman1d4d4142005-09-01 00:19:25 +00002854 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002855 if (N0CFP) {
2856 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002857 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002858 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002859 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002860}
2861
Nate Begeman83e75ec2005-09-06 04:43:02 +00002862SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002863 SDOperand N0 = N->getOperand(0);
2864 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2865 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002866
2867 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002868 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002869 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00002870
2871 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002872 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002873 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002874 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2875 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2876 LN0->getBasePtr(), LN0->getSrcValue(),
2877 LN0->getSrcValueOffset(),
Chris Lattnere564dbb2006-05-05 21:34:35 +00002878 N0.getValueType());
2879 CombineTo(N, ExtLoad);
2880 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
2881 ExtLoad.getValue(1));
2882 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2883 }
2884
2885
Nate Begeman83e75ec2005-09-06 04:43:02 +00002886 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002887}
2888
Nate Begeman83e75ec2005-09-06 04:43:02 +00002889SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002890 SDOperand N0 = N->getOperand(0);
2891 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2892 MVT::ValueType VT = N->getValueType(0);
2893
2894 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002895 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002896 return DAG.getNode(ISD::FNEG, VT, N0);
2897 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002898 if (N0.getOpcode() == ISD::SUB)
2899 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002900 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002901 if (N0.getOpcode() == ISD::FNEG)
2902 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002903 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002904}
2905
Nate Begeman83e75ec2005-09-06 04:43:02 +00002906SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002907 SDOperand N0 = N->getOperand(0);
2908 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2909 MVT::ValueType VT = N->getValueType(0);
2910
Nate Begeman1d4d4142005-09-01 00:19:25 +00002911 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002912 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002913 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002914 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002915 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002916 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002917 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002918 // fold (fabs (fcopysign x, y)) -> (fabs x)
2919 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2920 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2921
Nate Begeman83e75ec2005-09-06 04:43:02 +00002922 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002923}
2924
Nate Begeman44728a72005-09-19 22:34:01 +00002925SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2926 SDOperand Chain = N->getOperand(0);
2927 SDOperand N1 = N->getOperand(1);
2928 SDOperand N2 = N->getOperand(2);
2929 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2930
2931 // never taken branch, fold to chain
2932 if (N1C && N1C->isNullValue())
2933 return Chain;
2934 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002935 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002936 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002937 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2938 // on the target.
2939 if (N1.getOpcode() == ISD::SETCC &&
2940 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2941 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2942 N1.getOperand(0), N1.getOperand(1), N2);
2943 }
Nate Begeman44728a72005-09-19 22:34:01 +00002944 return SDOperand();
2945}
2946
Chris Lattner3ea0b472005-10-05 06:47:48 +00002947// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2948//
Nate Begeman44728a72005-09-19 22:34:01 +00002949SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002950 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2951 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2952
2953 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002954 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002955 if (Simp.Val) AddToWorkList(Simp.Val);
2956
Nate Begemane17daeb2005-10-05 21:43:42 +00002957 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2958
2959 // fold br_cc true, dest -> br dest (unconditional branch)
2960 if (SCCC && SCCC->getValue())
2961 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2962 N->getOperand(4));
2963 // fold br_cc false, dest -> unconditional fall through
2964 if (SCCC && SCCC->isNullValue())
2965 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00002966
Nate Begemane17daeb2005-10-05 21:43:42 +00002967 // fold to a simpler setcc
2968 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2969 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2970 Simp.getOperand(2), Simp.getOperand(0),
2971 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002972 return SDOperand();
2973}
2974
Chris Lattner01a22022005-10-10 22:04:48 +00002975SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00002976 LoadSDNode *LD = cast<LoadSDNode>(N);
2977 SDOperand Chain = LD->getChain();
2978 SDOperand Ptr = LD->getBasePtr();
Jim Laskey6ff23e52006-10-04 16:53:27 +00002979
Chris Lattnere4b95392006-03-31 18:06:18 +00002980 // If there are no uses of the loaded value, change uses of the chain value
2981 // into uses of the chain input (i.e. delete the dead load).
2982 if (N->hasNUsesOfValue(0, 0))
2983 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
Chris Lattner01a22022005-10-10 22:04:48 +00002984
2985 // If this load is directly stored, replace the load value with the stored
2986 // value.
2987 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002988 // TODO: Handle TRUNCSTORE/LOADEXT
2989 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002990 if (ISD::isNON_TRUNCStore(Chain.Val)) {
2991 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
2992 if (PrevST->getBasePtr() == Ptr &&
2993 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002994 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002995 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002996 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00002997
Jim Laskey7ca56af2006-10-11 13:47:09 +00002998 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00002999 // Walk up chain skipping non-aliasing memory nodes.
3000 SDOperand BetterChain = FindBetterChain(N, Chain);
3001
Jim Laskey6ff23e52006-10-04 16:53:27 +00003002 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00003003 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003004 SDOperand ReplLoad;
3005
Jim Laskey279f0532006-09-25 16:29:54 +00003006 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003007 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
3008 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
3009 LD->getSrcValue(), LD->getSrcValueOffset());
3010 } else {
3011 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
3012 LD->getValueType(0),
3013 BetterChain, Ptr, LD->getSrcValue(),
3014 LD->getSrcValueOffset(),
3015 LD->getLoadedVT());
3016 }
Jim Laskey279f0532006-09-25 16:29:54 +00003017
Jim Laskey6ff23e52006-10-04 16:53:27 +00003018 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00003019 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
3020 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00003021
Jim Laskey274062c2006-10-13 23:32:28 +00003022 // Replace uses with load result and token factor. Don't add users
3023 // to work list.
3024 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003025 }
3026 }
3027
Evan Cheng7fc033a2006-11-03 03:06:21 +00003028 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003029 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00003030 return SDOperand(N, 0);
3031
Chris Lattner01a22022005-10-10 22:04:48 +00003032 return SDOperand();
3033}
3034
Chris Lattner87514ca2005-10-10 22:31:19 +00003035SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003036 StoreSDNode *ST = cast<StoreSDNode>(N);
3037 SDOperand Chain = ST->getChain();
3038 SDOperand Value = ST->getValue();
3039 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00003040
Chris Lattnerc33baaa2005-12-23 05:48:07 +00003041 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00003042 // FIXME: This needs to know that the resultant store does not need a
3043 // higher alignment than the original.
Jim Laskey14fbcbf2006-09-25 21:11:32 +00003044 if (0 && Value.getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003045 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
3046 ST->getSrcValueOffset());
Jim Laskey279f0532006-09-25 16:29:54 +00003047 }
3048
3049 if (CombinerAA) {
3050 // Walk up chain skipping non-aliasing memory nodes.
3051 SDOperand BetterChain = FindBetterChain(N, Chain);
3052
Jim Laskey6ff23e52006-10-04 16:53:27 +00003053 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00003054 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00003055 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00003056 SDOperand ReplStore;
3057 if (ST->isTruncatingStore()) {
3058 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
3059 ST->getSrcValue(),ST->getSrcValueOffset(), ST->getStoredVT());
3060 } else {
3061 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
3062 ST->getSrcValue(), ST->getSrcValueOffset());
3063 }
3064
Jim Laskey279f0532006-09-25 16:29:54 +00003065 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00003066 SDOperand Token =
3067 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
3068
3069 // Don't add users to work list.
3070 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003071 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00003072 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00003073
Evan Cheng33dbedc2006-11-05 09:31:14 +00003074 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003075 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00003076 return SDOperand(N, 0);
3077
Chris Lattner87514ca2005-10-10 22:31:19 +00003078 return SDOperand();
3079}
3080
Chris Lattnerca242442006-03-19 01:27:56 +00003081SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
3082 SDOperand InVec = N->getOperand(0);
3083 SDOperand InVal = N->getOperand(1);
3084 SDOperand EltNo = N->getOperand(2);
3085
3086 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
3087 // vector with the inserted element.
3088 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
3089 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003090 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00003091 if (Elt < Ops.size())
3092 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003093 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
3094 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00003095 }
3096
3097 return SDOperand();
3098}
3099
3100SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
3101 SDOperand InVec = N->getOperand(0);
3102 SDOperand InVal = N->getOperand(1);
3103 SDOperand EltNo = N->getOperand(2);
3104 SDOperand NumElts = N->getOperand(3);
3105 SDOperand EltType = N->getOperand(4);
3106
3107 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
3108 // vector with the inserted element.
3109 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
3110 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003111 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00003112 if (Elt < Ops.size()-2)
3113 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003114 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
3115 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00003116 }
3117
3118 return SDOperand();
3119}
3120
Chris Lattnerd7648c82006-03-28 20:28:38 +00003121SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
3122 unsigned NumInScalars = N->getNumOperands()-2;
3123 SDOperand NumElts = N->getOperand(NumInScalars);
3124 SDOperand EltType = N->getOperand(NumInScalars+1);
3125
3126 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
3127 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
3128 // two distinct vectors, turn this into a shuffle node.
3129 SDOperand VecIn1, VecIn2;
3130 for (unsigned i = 0; i != NumInScalars; ++i) {
3131 // Ignore undef inputs.
3132 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3133
3134 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
3135 // constant index, bail out.
3136 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
3137 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
3138 VecIn1 = VecIn2 = SDOperand(0, 0);
3139 break;
3140 }
3141
3142 // If the input vector type disagrees with the result of the vbuild_vector,
3143 // we can't make a shuffle.
3144 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
3145 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
3146 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
3147 VecIn1 = VecIn2 = SDOperand(0, 0);
3148 break;
3149 }
3150
3151 // Otherwise, remember this. We allow up to two distinct input vectors.
3152 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
3153 continue;
3154
3155 if (VecIn1.Val == 0) {
3156 VecIn1 = ExtractedFromVec;
3157 } else if (VecIn2.Val == 0) {
3158 VecIn2 = ExtractedFromVec;
3159 } else {
3160 // Too many inputs.
3161 VecIn1 = VecIn2 = SDOperand(0, 0);
3162 break;
3163 }
3164 }
3165
3166 // If everything is good, we can make a shuffle operation.
3167 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003168 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00003169 for (unsigned i = 0; i != NumInScalars; ++i) {
3170 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
3171 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3172 continue;
3173 }
3174
3175 SDOperand Extract = N->getOperand(i);
3176
3177 // If extracting from the first vector, just use the index directly.
3178 if (Extract.getOperand(0) == VecIn1) {
3179 BuildVecIndices.push_back(Extract.getOperand(1));
3180 continue;
3181 }
3182
3183 // Otherwise, use InIdx + VecSize
3184 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
3185 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
3186 }
3187
3188 // Add count and size info.
3189 BuildVecIndices.push_back(NumElts);
3190 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
3191
3192 // Return the new VVECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003193 SDOperand Ops[5];
3194 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00003195 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003196 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00003197 } else {
3198 // Use an undef vbuild_vector as input for the second operand.
3199 std::vector<SDOperand> UnOps(NumInScalars,
3200 DAG.getNode(ISD::UNDEF,
3201 cast<VTSDNode>(EltType)->getVT()));
3202 UnOps.push_back(NumElts);
3203 UnOps.push_back(EltType);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003204 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3205 &UnOps[0], UnOps.size());
3206 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00003207 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003208 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3209 &BuildVecIndices[0], BuildVecIndices.size());
3210 Ops[3] = NumElts;
3211 Ops[4] = EltType;
3212 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
Chris Lattnerd7648c82006-03-28 20:28:38 +00003213 }
3214
3215 return SDOperand();
3216}
3217
Chris Lattner66445d32006-03-28 22:11:53 +00003218SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003219 SDOperand ShufMask = N->getOperand(2);
3220 unsigned NumElts = ShufMask.getNumOperands();
3221
3222 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3223 bool isIdentity = true;
3224 for (unsigned i = 0; i != NumElts; ++i) {
3225 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3226 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3227 isIdentity = false;
3228 break;
3229 }
3230 }
3231 if (isIdentity) return N->getOperand(0);
3232
3233 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3234 isIdentity = true;
3235 for (unsigned i = 0; i != NumElts; ++i) {
3236 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3237 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3238 isIdentity = false;
3239 break;
3240 }
3241 }
3242 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00003243
3244 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3245 // needed at all.
3246 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003247 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003248 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003249 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003250 for (unsigned i = 0; i != NumElts; ++i)
3251 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3252 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3253 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003254 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003255 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003256 BaseIdx = Idx;
3257 } else {
3258 if (BaseIdx != Idx)
3259 isSplat = false;
3260 if (VecNum != V) {
3261 isUnary = false;
3262 break;
3263 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003264 }
3265 }
3266
3267 SDOperand N0 = N->getOperand(0);
3268 SDOperand N1 = N->getOperand(1);
3269 // Normalize unary shuffle so the RHS is undef.
3270 if (isUnary && VecNum == 1)
3271 std::swap(N0, N1);
3272
Evan Cheng917ec982006-07-21 08:25:53 +00003273 // If it is a splat, check if the argument vector is a build_vector with
3274 // all scalar elements the same.
3275 if (isSplat) {
3276 SDNode *V = N0.Val;
3277 if (V->getOpcode() == ISD::BIT_CONVERT)
3278 V = V->getOperand(0).Val;
3279 if (V->getOpcode() == ISD::BUILD_VECTOR) {
3280 unsigned NumElems = V->getNumOperands()-2;
3281 if (NumElems > BaseIdx) {
3282 SDOperand Base;
3283 bool AllSame = true;
3284 for (unsigned i = 0; i != NumElems; ++i) {
3285 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3286 Base = V->getOperand(i);
3287 break;
3288 }
3289 }
3290 // Splat of <u, u, u, u>, return <u, u, u, u>
3291 if (!Base.Val)
3292 return N0;
3293 for (unsigned i = 0; i != NumElems; ++i) {
3294 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3295 V->getOperand(i) != Base) {
3296 AllSame = false;
3297 break;
3298 }
3299 }
3300 // Splat of <x, x, x, x>, return <x, x, x, x>
3301 if (AllSame)
3302 return N0;
3303 }
3304 }
3305 }
3306
Evan Chenge7bec0d2006-07-20 22:44:41 +00003307 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3308 // into an undef.
3309 if (isUnary || N0 == N1) {
3310 if (N0.getOpcode() == ISD::UNDEF)
Evan Chengc04766a2006-04-06 23:20:43 +00003311 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00003312 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3313 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003314 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00003315 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00003316 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3317 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3318 MappedOps.push_back(ShufMask.getOperand(i));
3319 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00003320 unsigned NewIdx =
3321 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3322 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00003323 }
3324 }
3325 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003326 &MappedOps[0], MappedOps.size());
Chris Lattner3e104b12006-04-08 04:15:24 +00003327 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00003328 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
Evan Chenge7bec0d2006-07-20 22:44:41 +00003329 N0,
Chris Lattner66445d32006-03-28 22:11:53 +00003330 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
3331 ShufMask);
3332 }
3333
3334 return SDOperand();
3335}
3336
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003337SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
3338 SDOperand ShufMask = N->getOperand(2);
3339 unsigned NumElts = ShufMask.getNumOperands()-2;
3340
3341 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3342 bool isIdentity = true;
3343 for (unsigned i = 0; i != NumElts; ++i) {
3344 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3345 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3346 isIdentity = false;
3347 break;
3348 }
3349 }
3350 if (isIdentity) return N->getOperand(0);
3351
3352 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3353 isIdentity = true;
3354 for (unsigned i = 0; i != NumElts; ++i) {
3355 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3356 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3357 isIdentity = false;
3358 break;
3359 }
3360 }
3361 if (isIdentity) return N->getOperand(1);
3362
Evan Chenge7bec0d2006-07-20 22:44:41 +00003363 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3364 // needed at all.
3365 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003366 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003367 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003368 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003369 for (unsigned i = 0; i != NumElts; ++i)
3370 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3371 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3372 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003373 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003374 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003375 BaseIdx = Idx;
3376 } else {
3377 if (BaseIdx != Idx)
3378 isSplat = false;
3379 if (VecNum != V) {
3380 isUnary = false;
3381 break;
3382 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003383 }
3384 }
3385
3386 SDOperand N0 = N->getOperand(0);
3387 SDOperand N1 = N->getOperand(1);
3388 // Normalize unary shuffle so the RHS is undef.
3389 if (isUnary && VecNum == 1)
3390 std::swap(N0, N1);
3391
Evan Cheng917ec982006-07-21 08:25:53 +00003392 // If it is a splat, check if the argument vector is a build_vector with
3393 // all scalar elements the same.
3394 if (isSplat) {
3395 SDNode *V = N0.Val;
Evan Cheng59569222006-10-16 22:49:37 +00003396
3397 // If this is a vbit convert that changes the element type of the vector but
3398 // not the number of vector elements, look through it. Be careful not to
3399 // look though conversions that change things like v4f32 to v2f64.
3400 if (V->getOpcode() == ISD::VBIT_CONVERT) {
3401 SDOperand ConvInput = V->getOperand(0);
Evan Cheng5d04a1a2006-10-17 17:06:35 +00003402 if (ConvInput.getValueType() == MVT::Vector &&
3403 NumElts ==
Evan Cheng59569222006-10-16 22:49:37 +00003404 ConvInput.getConstantOperandVal(ConvInput.getNumOperands()-2))
3405 V = ConvInput.Val;
3406 }
3407
Evan Cheng917ec982006-07-21 08:25:53 +00003408 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
3409 unsigned NumElems = V->getNumOperands()-2;
3410 if (NumElems > BaseIdx) {
3411 SDOperand Base;
3412 bool AllSame = true;
3413 for (unsigned i = 0; i != NumElems; ++i) {
3414 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3415 Base = V->getOperand(i);
3416 break;
3417 }
3418 }
3419 // Splat of <u, u, u, u>, return <u, u, u, u>
3420 if (!Base.Val)
3421 return N0;
3422 for (unsigned i = 0; i != NumElems; ++i) {
3423 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3424 V->getOperand(i) != Base) {
3425 AllSame = false;
3426 break;
3427 }
3428 }
3429 // Splat of <x, x, x, x>, return <x, x, x, x>
3430 if (AllSame)
3431 return N0;
3432 }
3433 }
3434 }
3435
Evan Chenge7bec0d2006-07-20 22:44:41 +00003436 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3437 // into an undef.
3438 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00003439 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3440 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003441 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00003442 for (unsigned i = 0; i != NumElts; ++i) {
3443 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3444 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3445 MappedOps.push_back(ShufMask.getOperand(i));
3446 } else {
3447 unsigned NewIdx =
3448 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3449 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
3450 }
3451 }
3452 // Add the type/#elts values.
3453 MappedOps.push_back(ShufMask.getOperand(NumElts));
3454 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
3455
3456 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003457 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003458 AddToWorkList(ShufMask.Val);
3459
3460 // Build the undef vector.
3461 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
3462 for (unsigned i = 0; i != NumElts; ++i)
3463 MappedOps[i] = UDVal;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003464 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
3465 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003466 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3467 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003468
3469 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
Evan Chenge7bec0d2006-07-20 22:44:41 +00003470 N0, UDVal, ShufMask,
Chris Lattner17614ea2006-04-08 05:34:25 +00003471 MappedOps[NumElts], MappedOps[NumElts+1]);
3472 }
3473
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003474 return SDOperand();
3475}
3476
Evan Cheng44f1f092006-04-20 08:56:16 +00003477/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
3478/// a VAND to a vector_shuffle with the destination vector and a zero vector.
3479/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
3480/// vector_shuffle V, Zero, <0, 4, 2, 4>
3481SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
3482 SDOperand LHS = N->getOperand(0);
3483 SDOperand RHS = N->getOperand(1);
3484 if (N->getOpcode() == ISD::VAND) {
3485 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
3486 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
3487 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
3488 RHS = RHS.getOperand(0);
3489 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
3490 std::vector<SDOperand> IdxOps;
3491 unsigned NumOps = RHS.getNumOperands();
3492 unsigned NumElts = NumOps-2;
3493 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
3494 for (unsigned i = 0; i != NumElts; ++i) {
3495 SDOperand Elt = RHS.getOperand(i);
3496 if (!isa<ConstantSDNode>(Elt))
3497 return SDOperand();
3498 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
3499 IdxOps.push_back(DAG.getConstant(i, EVT));
3500 else if (cast<ConstantSDNode>(Elt)->isNullValue())
3501 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
3502 else
3503 return SDOperand();
3504 }
3505
3506 // Let's see if the target supports this vector_shuffle.
3507 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
3508 return SDOperand();
3509
3510 // Return the new VVECTOR_SHUFFLE node.
3511 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
3512 SDOperand EVTNode = DAG.getValueType(EVT);
3513 std::vector<SDOperand> Ops;
Chris Lattner516b9622006-09-14 20:50:57 +00003514 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode,
3515 EVTNode);
Evan Cheng44f1f092006-04-20 08:56:16 +00003516 Ops.push_back(LHS);
3517 AddToWorkList(LHS.Val);
3518 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
3519 ZeroOps.push_back(NumEltsNode);
3520 ZeroOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003521 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3522 &ZeroOps[0], ZeroOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003523 IdxOps.push_back(NumEltsNode);
3524 IdxOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003525 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3526 &IdxOps[0], IdxOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003527 Ops.push_back(NumEltsNode);
3528 Ops.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003529 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
3530 &Ops[0], Ops.size());
Evan Cheng44f1f092006-04-20 08:56:16 +00003531 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
3532 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
3533 DstVecSize, DstVecEVT);
3534 }
3535 return Result;
3536 }
3537 }
3538 return SDOperand();
3539}
3540
Chris Lattneredab1b92006-04-02 03:25:57 +00003541/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
3542/// the scalar operation of the vop if it is operating on an integer vector
3543/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
3544SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
3545 ISD::NodeType FPOp) {
3546 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
3547 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
3548 SDOperand LHS = N->getOperand(0);
3549 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00003550 SDOperand Shuffle = XformToShuffleWithZero(N);
3551 if (Shuffle.Val) return Shuffle;
3552
Chris Lattneredab1b92006-04-02 03:25:57 +00003553 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
3554 // this operation.
3555 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
3556 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003557 SmallVector<SDOperand, 8> Ops;
Chris Lattneredab1b92006-04-02 03:25:57 +00003558 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
3559 SDOperand LHSOp = LHS.getOperand(i);
3560 SDOperand RHSOp = RHS.getOperand(i);
3561 // If these two elements can't be folded, bail out.
3562 if ((LHSOp.getOpcode() != ISD::UNDEF &&
3563 LHSOp.getOpcode() != ISD::Constant &&
3564 LHSOp.getOpcode() != ISD::ConstantFP) ||
3565 (RHSOp.getOpcode() != ISD::UNDEF &&
3566 RHSOp.getOpcode() != ISD::Constant &&
3567 RHSOp.getOpcode() != ISD::ConstantFP))
3568 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00003569 // Can't fold divide by zero.
3570 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
3571 if ((RHSOp.getOpcode() == ISD::Constant &&
3572 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
3573 (RHSOp.getOpcode() == ISD::ConstantFP &&
3574 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
3575 break;
3576 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003577 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00003578 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00003579 assert((Ops.back().getOpcode() == ISD::UNDEF ||
3580 Ops.back().getOpcode() == ISD::Constant ||
3581 Ops.back().getOpcode() == ISD::ConstantFP) &&
3582 "Scalar binop didn't fold!");
3583 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003584
3585 if (Ops.size() == LHS.getNumOperands()-2) {
3586 Ops.push_back(*(LHS.Val->op_end()-2));
3587 Ops.push_back(*(LHS.Val->op_end()-1));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003588 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003589 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003590 }
3591
3592 return SDOperand();
3593}
3594
Nate Begeman44728a72005-09-19 22:34:01 +00003595SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00003596 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
3597
3598 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
3599 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3600 // If we got a simplified select_cc node back from SimplifySelectCC, then
3601 // break it down into a new SETCC node, and a new SELECT node, and then return
3602 // the SELECT node, since we were called with a SELECT node.
3603 if (SCC.Val) {
3604 // Check to see if we got a select_cc back (to turn into setcc/select).
3605 // Otherwise, just return whatever node we got back, like fabs.
3606 if (SCC.getOpcode() == ISD::SELECT_CC) {
3607 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
3608 SCC.getOperand(0), SCC.getOperand(1),
3609 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00003610 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003611 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
3612 SCC.getOperand(3), SETCC);
3613 }
3614 return SCC;
3615 }
Nate Begeman44728a72005-09-19 22:34:01 +00003616 return SDOperand();
3617}
3618
Chris Lattner40c62d52005-10-18 06:04:22 +00003619/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
3620/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00003621/// select. Callers of this should assume that TheSelect is deleted if this
3622/// returns true. As such, they should return the appropriate thing (e.g. the
3623/// node) back to the top-level of the DAG combiner loop to avoid it being
3624/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00003625///
3626bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
3627 SDOperand RHS) {
3628
3629 // If this is a select from two identical things, try to pull the operation
3630 // through the select.
3631 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00003632 // If this is a load and the token chain is identical, replace the select
3633 // of two loads with a load through a select of the address to load from.
3634 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
3635 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00003636 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00003637 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00003638 LHS.getOperand(0) == RHS.getOperand(0)) {
3639 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
3640 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
3641
3642 // If this is an EXTLOAD, the VT's must match.
Evan Cheng2e49f092006-10-11 07:10:22 +00003643 if (LLD->getLoadedVT() == RLD->getLoadedVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003644 // FIXME: this conflates two src values, discarding one. This is not
3645 // the right thing to do, but nothing uses srcvalues now. When they do,
3646 // turn SrcValue into a list of locations.
3647 SDOperand Addr;
3648 if (TheSelect->getOpcode() == ISD::SELECT)
3649 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
3650 TheSelect->getOperand(0), LLD->getBasePtr(),
3651 RLD->getBasePtr());
3652 else
3653 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
3654 TheSelect->getOperand(0),
3655 TheSelect->getOperand(1),
3656 LLD->getBasePtr(), RLD->getBasePtr(),
3657 TheSelect->getOperand(4));
Chris Lattner40c62d52005-10-18 06:04:22 +00003658
Evan Cheng466685d2006-10-09 20:57:25 +00003659 SDOperand Load;
3660 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
3661 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
3662 Addr,LLD->getSrcValue(), LLD->getSrcValueOffset());
3663 else {
3664 Load = DAG.getExtLoad(LLD->getExtensionType(),
3665 TheSelect->getValueType(0),
3666 LLD->getChain(), Addr, LLD->getSrcValue(),
3667 LLD->getSrcValueOffset(),
Evan Cheng2e49f092006-10-11 07:10:22 +00003668 LLD->getLoadedVT());
Evan Cheng466685d2006-10-09 20:57:25 +00003669 }
3670 // Users of the select now use the result of the load.
3671 CombineTo(TheSelect, Load);
3672
3673 // Users of the old loads now use the new load's chain. We know the
3674 // old-load value is dead now.
3675 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
3676 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
3677 return true;
Evan Chengc5484282006-10-04 00:56:09 +00003678 }
Chris Lattner40c62d52005-10-18 06:04:22 +00003679 }
3680 }
3681
3682 return false;
3683}
3684
Nate Begeman44728a72005-09-19 22:34:01 +00003685SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
3686 SDOperand N2, SDOperand N3,
3687 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00003688
3689 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00003690 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
3691 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
3692 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
3693
3694 // Determine if the condition we're dealing with is constant
3695 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003696 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003697 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
3698
3699 // fold select_cc true, x, y -> x
3700 if (SCCC && SCCC->getValue())
3701 return N2;
3702 // fold select_cc false, x, y -> y
3703 if (SCCC && SCCC->getValue() == 0)
3704 return N3;
3705
3706 // Check to see if we can simplify the select into an fabs node
3707 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
3708 // Allow either -0.0 or 0.0
3709 if (CFP->getValue() == 0.0) {
3710 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
3711 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
3712 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
3713 N2 == N3.getOperand(0))
3714 return DAG.getNode(ISD::FABS, VT, N0);
3715
3716 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
3717 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3718 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3719 N2.getOperand(0) == N3)
3720 return DAG.getNode(ISD::FABS, VT, N3);
3721 }
3722 }
3723
3724 // Check to see if we can perform the "gzip trick", transforming
3725 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00003726 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00003727 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00003728 MVT::isInteger(N2.getValueType()) &&
3729 (N1C->isNullValue() || // (a < 0) ? b : 0
3730 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00003731 MVT::ValueType XType = N0.getValueType();
3732 MVT::ValueType AType = N2.getValueType();
3733 if (XType >= AType) {
3734 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00003735 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00003736 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3737 unsigned ShCtV = Log2_64(N2C->getValue());
3738 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3739 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3740 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00003741 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003742 if (XType > AType) {
3743 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003744 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003745 }
3746 return DAG.getNode(ISD::AND, AType, Shift, N2);
3747 }
3748 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3749 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3750 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003751 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003752 if (XType > AType) {
3753 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003754 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003755 }
3756 return DAG.getNode(ISD::AND, AType, Shift, N2);
3757 }
3758 }
Nate Begeman07ed4172005-10-10 21:26:48 +00003759
3760 // fold select C, 16, 0 -> shl C, 4
3761 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3762 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3763 // Get a SetCC of the condition
3764 // FIXME: Should probably make sure that setcc is legal if we ever have a
3765 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00003766 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00003767 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00003768 if (AfterLegalize) {
3769 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003770 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00003771 } else {
3772 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003773 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00003774 }
Chris Lattner5750df92006-03-01 04:03:14 +00003775 AddToWorkList(SCC.Val);
3776 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00003777 // shl setcc result by log2 n2c
3778 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3779 DAG.getConstant(Log2_64(N2C->getValue()),
3780 TLI.getShiftAmountTy()));
3781 }
3782
Nate Begemanf845b452005-10-08 00:29:44 +00003783 // Check to see if this is the equivalent of setcc
3784 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3785 // otherwise, go ahead with the folds.
3786 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3787 MVT::ValueType XType = N0.getValueType();
3788 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3789 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3790 if (Res.getValueType() != VT)
3791 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3792 return Res;
3793 }
3794
3795 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3796 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3797 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3798 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3799 return DAG.getNode(ISD::SRL, XType, Ctlz,
3800 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3801 TLI.getShiftAmountTy()));
3802 }
3803 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3804 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3805 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3806 N0);
3807 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3808 DAG.getConstant(~0ULL, XType));
3809 return DAG.getNode(ISD::SRL, XType,
3810 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3811 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3812 TLI.getShiftAmountTy()));
3813 }
3814 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3815 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3816 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3817 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3818 TLI.getShiftAmountTy()));
3819 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3820 }
3821 }
3822
3823 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3824 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3825 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3826 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3827 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3828 MVT::ValueType XType = N0.getValueType();
3829 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3830 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3831 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3832 TLI.getShiftAmountTy()));
3833 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003834 AddToWorkList(Shift.Val);
3835 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003836 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3837 }
3838 }
3839 }
3840
Nate Begeman44728a72005-09-19 22:34:01 +00003841 return SDOperand();
3842}
3843
Nate Begeman452d7be2005-09-16 00:54:12 +00003844SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00003845 SDOperand N1, ISD::CondCode Cond,
3846 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003847 // These setcc operations always fold.
3848 switch (Cond) {
3849 default: break;
3850 case ISD::SETFALSE:
3851 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3852 case ISD::SETTRUE:
3853 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3854 }
3855
3856 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3857 uint64_t C1 = N1C->getValue();
Reid Spencer3ed469c2006-11-02 20:25:50 +00003858 if (isa<ConstantSDNode>(N0.Val)) {
Chris Lattner51dabfb2006-10-14 00:41:01 +00003859 return DAG.FoldSetCC(VT, N0, N1, Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00003860 } else {
Chris Lattner5f42a242006-09-20 06:19:26 +00003861 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
3862 // equality comparison, then we're just comparing whether X itself is
3863 // zero.
3864 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
3865 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
3866 N0.getOperand(1).getOpcode() == ISD::Constant) {
3867 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
3868 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3869 ShAmt == Log2_32(MVT::getSizeInBits(N0.getValueType()))) {
3870 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
3871 // (srl (ctlz x), 5) == 0 -> X != 0
3872 // (srl (ctlz x), 5) != 1 -> X != 0
3873 Cond = ISD::SETNE;
3874 } else {
3875 // (srl (ctlz x), 5) != 0 -> X == 0
3876 // (srl (ctlz x), 5) == 1 -> X == 0
3877 Cond = ISD::SETEQ;
3878 }
3879 SDOperand Zero = DAG.getConstant(0, N0.getValueType());
3880 return DAG.getSetCC(VT, N0.getOperand(0).getOperand(0),
3881 Zero, Cond);
3882 }
3883 }
3884
Nate Begeman452d7be2005-09-16 00:54:12 +00003885 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3886 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3887 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3888
3889 // If the comparison constant has bits in the upper part, the
3890 // zero-extended value could never match.
3891 if (C1 & (~0ULL << InSize)) {
3892 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3893 switch (Cond) {
3894 case ISD::SETUGT:
3895 case ISD::SETUGE:
3896 case ISD::SETEQ: return DAG.getConstant(0, VT);
3897 case ISD::SETULT:
3898 case ISD::SETULE:
3899 case ISD::SETNE: return DAG.getConstant(1, VT);
3900 case ISD::SETGT:
3901 case ISD::SETGE:
3902 // True if the sign bit of C1 is set.
3903 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3904 case ISD::SETLT:
3905 case ISD::SETLE:
3906 // True if the sign bit of C1 isn't set.
3907 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3908 default:
3909 break;
3910 }
3911 }
3912
3913 // Otherwise, we can perform the comparison with the low bits.
3914 switch (Cond) {
3915 case ISD::SETEQ:
3916 case ISD::SETNE:
3917 case ISD::SETUGT:
3918 case ISD::SETUGE:
3919 case ISD::SETULT:
3920 case ISD::SETULE:
3921 return DAG.getSetCC(VT, N0.getOperand(0),
3922 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3923 Cond);
3924 default:
3925 break; // todo, be more careful with signed comparisons
3926 }
3927 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3928 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3929 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3930 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3931 MVT::ValueType ExtDstTy = N0.getValueType();
3932 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3933
3934 // If the extended part has any inconsistent bits, it cannot ever
3935 // compare equal. In other words, they have to be all ones or all
3936 // zeros.
3937 uint64_t ExtBits =
3938 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3939 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3940 return DAG.getConstant(Cond == ISD::SETNE, VT);
3941
3942 SDOperand ZextOp;
3943 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3944 if (Op0Ty == ExtSrcTy) {
3945 ZextOp = N0.getOperand(0);
3946 } else {
3947 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3948 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3949 DAG.getConstant(Imm, Op0Ty));
3950 }
Chris Lattner5750df92006-03-01 04:03:14 +00003951 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003952 // Otherwise, make this a use of a zext.
3953 return DAG.getSetCC(VT, ZextOp,
3954 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3955 ExtDstTy),
3956 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00003957 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
Chris Lattner8ac9d0e2006-10-14 01:02:29 +00003958 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3959
3960 // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC
3961 if (N0.getOpcode() == ISD::SETCC) {
3962 bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getValue() != 1);
3963 if (TrueWhenTrue)
3964 return N0;
3965
3966 // Invert the condition.
3967 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
3968 CC = ISD::getSetCCInverse(CC,
3969 MVT::isInteger(N0.getOperand(0).getValueType()));
3970 return DAG.getSetCC(VT, N0.getOperand(0), N0.getOperand(1), CC);
3971 }
3972
3973 if ((N0.getOpcode() == ISD::XOR ||
3974 (N0.getOpcode() == ISD::AND &&
3975 N0.getOperand(0).getOpcode() == ISD::XOR &&
3976 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3977 isa<ConstantSDNode>(N0.getOperand(1)) &&
3978 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
3979 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We
3980 // can only do this if the top bits are known zero.
Chris Lattner50662be2006-10-17 21:24:15 +00003981 if (TLI.MaskedValueIsZero(N0,
Chris Lattner8ac9d0e2006-10-14 01:02:29 +00003982 MVT::getIntVTBitMask(N0.getValueType())-1)){
3983 // Okay, get the un-inverted input value.
3984 SDOperand Val;
3985 if (N0.getOpcode() == ISD::XOR)
3986 Val = N0.getOperand(0);
3987 else {
3988 assert(N0.getOpcode() == ISD::AND &&
3989 N0.getOperand(0).getOpcode() == ISD::XOR);
3990 // ((X^1)&1)^1 -> X & 1
3991 Val = DAG.getNode(ISD::AND, N0.getValueType(),
3992 N0.getOperand(0).getOperand(0),
3993 N0.getOperand(1));
3994 }
3995 return DAG.getSetCC(VT, Val, N1,
3996 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
Chris Lattner3391bcd2006-02-08 02:13:15 +00003997 }
Chris Lattner3391bcd2006-02-08 02:13:15 +00003998 }
Nate Begeman452d7be2005-09-16 00:54:12 +00003999 }
Chris Lattner5c46f742005-10-05 06:11:08 +00004000
Nate Begeman452d7be2005-09-16 00:54:12 +00004001 uint64_t MinVal, MaxVal;
4002 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
4003 if (ISD::isSignedIntSetCC(Cond)) {
4004 MinVal = 1ULL << (OperandBitSize-1);
4005 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
4006 MaxVal = ~0ULL >> (65-OperandBitSize);
4007 else
4008 MaxVal = 0;
4009 } else {
4010 MinVal = 0;
4011 MaxVal = ~0ULL >> (64-OperandBitSize);
4012 }
4013
4014 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
4015 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
4016 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
4017 --C1; // X >= C0 --> X > (C0-1)
4018 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
4019 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
4020 }
4021
4022 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
4023 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
4024 ++C1; // X <= C0 --> X < (C0+1)
4025 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
4026 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
4027 }
4028
4029 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
4030 return DAG.getConstant(0, VT); // X < MIN --> false
4031
4032 // Canonicalize setgt X, Min --> setne X, Min
4033 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
4034 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00004035 // Canonicalize setlt X, Max --> setne X, Max
4036 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
4037 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00004038
4039 // If we have setult X, 1, turn it into seteq X, 0
4040 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
4041 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
4042 ISD::SETEQ);
4043 // If we have setugt X, Max-1, turn it into seteq X, Max
4044 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
4045 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
4046 ISD::SETEQ);
4047
4048 // If we have "setcc X, C0", check to see if we can shrink the immediate
4049 // by changing cc.
4050
4051 // SETUGT X, SINTMAX -> SETLT X, 0
4052 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
4053 C1 == (~0ULL >> (65-OperandBitSize)))
4054 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
4055 ISD::SETLT);
4056
4057 // FIXME: Implement the rest of these.
4058
4059 // Fold bit comparisons when we can.
4060 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
4061 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
4062 if (ConstantSDNode *AndRHS =
4063 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
4064 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
4065 // Perform the xform if the AND RHS is a single bit.
Chris Lattner51dabfb2006-10-14 00:41:01 +00004066 if (isPowerOf2_64(AndRHS->getValue())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004067 return DAG.getNode(ISD::SRL, VT, N0,
4068 DAG.getConstant(Log2_64(AndRHS->getValue()),
4069 TLI.getShiftAmountTy()));
4070 }
4071 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
4072 // (X & 8) == 8 --> (X & 8) >> 3
4073 // Perform the xform if C1 is a single bit.
Chris Lattner51dabfb2006-10-14 00:41:01 +00004074 if (isPowerOf2_64(C1)) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004075 return DAG.getNode(ISD::SRL, VT, N0,
Chris Lattner729c6d12006-05-27 00:43:02 +00004076 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
Nate Begeman452d7be2005-09-16 00:54:12 +00004077 }
4078 }
4079 }
4080 }
4081 } else if (isa<ConstantSDNode>(N0.Val)) {
4082 // Ensure that the constant occurs on the RHS.
4083 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
4084 }
4085
Reid Spencer3ed469c2006-11-02 20:25:50 +00004086 if (isa<ConstantFPSDNode>(N0.Val)) {
Chris Lattner51dabfb2006-10-14 00:41:01 +00004087 // Constant fold or commute setcc.
4088 SDOperand O = DAG.FoldSetCC(VT, N0, N1, Cond);
4089 if (O.Val) return O;
4090 }
Nate Begeman452d7be2005-09-16 00:54:12 +00004091
4092 if (N0 == N1) {
Chris Lattner8ac9d0e2006-10-14 01:02:29 +00004093 // We can always fold X == X for integer setcc's.
Nate Begeman452d7be2005-09-16 00:54:12 +00004094 if (MVT::isInteger(N0.getValueType()))
4095 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
4096 unsigned UOF = ISD::getUnorderedFlavor(Cond);
4097 if (UOF == 2) // FP operators that are undefined on NaNs.
4098 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
4099 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
4100 return DAG.getConstant(UOF, VT);
4101 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
4102 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00004103 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00004104 if (NewCond != Cond)
4105 return DAG.getSetCC(VT, N0, N1, NewCond);
4106 }
4107
4108 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
4109 MVT::isInteger(N0.getValueType())) {
4110 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
4111 N0.getOpcode() == ISD::XOR) {
4112 // Simplify (X+Y) == (X+Z) --> Y == Z
4113 if (N0.getOpcode() == N1.getOpcode()) {
4114 if (N0.getOperand(0) == N1.getOperand(0))
4115 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
4116 if (N0.getOperand(1) == N1.getOperand(1))
4117 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
Evan Cheng1efba0e2006-08-29 06:42:35 +00004118 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004119 // If X op Y == Y op X, try other combinations.
4120 if (N0.getOperand(0) == N1.getOperand(1))
4121 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
4122 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00004123 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00004124 }
4125 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00004126
4127 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
4128 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
4129 // Turn (X+C1) == C2 --> X == C2-C1
4130 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
4131 return DAG.getSetCC(VT, N0.getOperand(0),
4132 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
4133 N0.getValueType()), Cond);
4134 }
4135
4136 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
4137 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00004138 // If we know that all of the inverted bits are zero, don't bother
4139 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00004140 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00004141 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00004142 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00004143 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00004144 }
4145
4146 // Turn (C1-X) == C2 --> X == C1-C2
4147 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
4148 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
4149 return DAG.getSetCC(VT, N0.getOperand(1),
4150 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
4151 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00004152 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00004153 }
4154 }
4155
Nate Begeman452d7be2005-09-16 00:54:12 +00004156 // Simplify (X+Z) == X --> Z == 0
4157 if (N0.getOperand(0) == N1)
4158 return DAG.getSetCC(VT, N0.getOperand(1),
4159 DAG.getConstant(0, N0.getValueType()), Cond);
4160 if (N0.getOperand(1) == N1) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00004161 if (DAG.isCommutativeBinOp(N0.getOpcode()))
Nate Begeman452d7be2005-09-16 00:54:12 +00004162 return DAG.getSetCC(VT, N0.getOperand(0),
4163 DAG.getConstant(0, N0.getValueType()), Cond);
4164 else {
4165 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
4166 // (Z-X) == X --> Z == X<<1
4167 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
4168 N1,
4169 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00004170 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004171 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
4172 }
4173 }
4174 }
4175
4176 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
4177 N1.getOpcode() == ISD::XOR) {
4178 // Simplify X == (X+Z) --> Z == 0
4179 if (N1.getOperand(0) == N0) {
4180 return DAG.getSetCC(VT, N1.getOperand(1),
4181 DAG.getConstant(0, N1.getValueType()), Cond);
4182 } else if (N1.getOperand(1) == N0) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00004183 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004184 return DAG.getSetCC(VT, N1.getOperand(0),
4185 DAG.getConstant(0, N1.getValueType()), Cond);
4186 } else {
4187 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
4188 // X == (Z-X) --> X<<1 == Z
4189 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
4190 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00004191 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004192 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
4193 }
4194 }
4195 }
4196 }
4197
4198 // Fold away ALL boolean setcc's.
4199 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00004200 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004201 switch (Cond) {
4202 default: assert(0 && "Unknown integer setcc!");
4203 case ISD::SETEQ: // X == Y -> (X^Y)^1
4204 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
4205 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00004206 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004207 break;
4208 case ISD::SETNE: // X != Y --> (X^Y)
4209 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
4210 break;
4211 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
4212 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
4213 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
4214 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00004215 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004216 break;
4217 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
4218 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
4219 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
4220 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00004221 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004222 break;
4223 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
4224 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
4225 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
4226 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00004227 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004228 break;
4229 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
4230 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
4231 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
4232 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
4233 break;
4234 }
4235 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00004236 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004237 // FIXME: If running after legalize, we probably can't do this.
4238 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
4239 }
4240 return N0;
4241 }
4242
4243 // Could not fold it.
4244 return SDOperand();
4245}
4246
Nate Begeman69575232005-10-20 02:15:44 +00004247/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
4248/// return a DAG expression to select that will generate the same value by
4249/// multiplying by a magic number. See:
4250/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4251SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004252 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004253 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
4254
Andrew Lenharth232c9102006-06-12 16:07:18 +00004255 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004256 ii != ee; ++ii)
4257 AddToWorkList(*ii);
4258 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004259}
4260
4261/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
4262/// return a DAG expression to select that will generate the same value by
4263/// multiplying by a magic number. See:
4264/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4265SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004266 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004267 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00004268
Andrew Lenharth232c9102006-06-12 16:07:18 +00004269 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004270 ii != ee; ++ii)
4271 AddToWorkList(*ii);
4272 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004273}
4274
Jim Laskey71382342006-10-07 23:37:56 +00004275/// FindBaseOffset - Return true if base is known not to alias with anything
4276/// but itself. Provides base object and offset as results.
4277static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
4278 // Assume it is a primitive operation.
4279 Base = Ptr; Offset = 0;
4280
4281 // If it's an adding a simple constant then integrate the offset.
4282 if (Base.getOpcode() == ISD::ADD) {
4283 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
4284 Base = Base.getOperand(0);
4285 Offset += C->getValue();
4286 }
4287 }
4288
4289 // If it's any of the following then it can't alias with anything but itself.
4290 return isa<FrameIndexSDNode>(Base) ||
4291 isa<ConstantPoolSDNode>(Base) ||
4292 isa<GlobalAddressSDNode>(Base);
4293}
4294
4295/// isAlias - Return true if there is any possibility that the two addresses
4296/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00004297bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
4298 const Value *SrcValue1, int SrcValueOffset1,
4299 SDOperand Ptr2, int64_t Size2,
4300 const Value *SrcValue2, int SrcValueOffset2)
4301{
Jim Laskey71382342006-10-07 23:37:56 +00004302 // If they are the same then they must be aliases.
4303 if (Ptr1 == Ptr2) return true;
4304
4305 // Gather base node and offset information.
4306 SDOperand Base1, Base2;
4307 int64_t Offset1, Offset2;
4308 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
4309 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
4310
4311 // If they have a same base address then...
4312 if (Base1 == Base2) {
4313 // Check to see if the addresses overlap.
4314 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
4315 }
4316
Jim Laskey096c22e2006-10-18 12:29:57 +00004317 // If we know both bases then they can't alias.
4318 if (KnownBase1 && KnownBase2) return false;
4319
Jim Laskey07a27092006-10-18 19:08:31 +00004320 if (CombinerGlobalAA) {
4321 // Use alias analysis information.
4322 int Overlap1 = Size1 + SrcValueOffset1 + Offset1;
4323 int Overlap2 = Size2 + SrcValueOffset2 + Offset2;
4324 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00004325 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00004326 if (AAResult == AliasAnalysis::NoAlias)
4327 return false;
4328 }
Jim Laskey096c22e2006-10-18 12:29:57 +00004329
4330 // Otherwise we have to assume they alias.
4331 return true;
Jim Laskey71382342006-10-07 23:37:56 +00004332}
4333
4334/// FindAliasInfo - Extracts the relevant alias information from the memory
4335/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00004336bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00004337 SDOperand &Ptr, int64_t &Size,
4338 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004339 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4340 Ptr = LD->getBasePtr();
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004341 Size = MVT::getSizeInBits(LD->getLoadedVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004342 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004343 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00004344 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004345 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004346 Ptr = ST->getBasePtr();
Evan Cheng8b2794a2006-10-13 21:14:26 +00004347 Size = MVT::getSizeInBits(ST->getStoredVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004348 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004349 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00004350 } else {
Jim Laskey71382342006-10-07 23:37:56 +00004351 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00004352 }
4353
4354 return false;
4355}
4356
Jim Laskey6ff23e52006-10-04 16:53:27 +00004357/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
4358/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00004359void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00004360 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004361 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00004362 std::set<SDNode *> Visited; // Visited node set.
4363
Jim Laskey279f0532006-09-25 16:29:54 +00004364 // Get alias information for node.
4365 SDOperand Ptr;
4366 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004367 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004368 int SrcValueOffset;
4369 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00004370
Jim Laskey6ff23e52006-10-04 16:53:27 +00004371 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00004372 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00004373
Jim Laskeybc588b82006-10-05 15:07:25 +00004374 // Look at each chain and determine if it is an alias. If so, add it to the
4375 // aliases list. If not, then continue up the chain looking for the next
4376 // candidate.
4377 while (!Chains.empty()) {
4378 SDOperand Chain = Chains.back();
4379 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00004380
Jim Laskeybc588b82006-10-05 15:07:25 +00004381 // Don't bother if we've been before.
4382 if (Visited.find(Chain.Val) != Visited.end()) continue;
4383 Visited.insert(Chain.Val);
4384
4385 switch (Chain.getOpcode()) {
4386 case ISD::EntryToken:
4387 // Entry token is ideal chain operand, but handled in FindBetterChain.
4388 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00004389
Jim Laskeybc588b82006-10-05 15:07:25 +00004390 case ISD::LOAD:
4391 case ISD::STORE: {
4392 // Get alias information for Chain.
4393 SDOperand OpPtr;
4394 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004395 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004396 int OpSrcValueOffset;
4397 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
4398 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00004399
4400 // If chain is alias then stop here.
4401 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00004402 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
4403 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004404 Aliases.push_back(Chain);
4405 } else {
4406 // Look further up the chain.
4407 Chains.push_back(Chain.getOperand(0));
4408 // Clean up old chain.
4409 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00004410 }
Jim Laskeybc588b82006-10-05 15:07:25 +00004411 break;
4412 }
4413
4414 case ISD::TokenFactor:
4415 // We have to check each of the operands of the token factor, so we queue
4416 // then up. Adding the operands to the queue (stack) in reverse order
4417 // maintains the original order and increases the likelihood that getNode
4418 // will find a matching token factor (CSE.)
4419 for (unsigned n = Chain.getNumOperands(); n;)
4420 Chains.push_back(Chain.getOperand(--n));
4421 // Eliminate the token factor if we can.
4422 AddToWorkList(Chain.Val);
4423 break;
4424
4425 default:
4426 // For all other instructions we will just have to take what we can get.
4427 Aliases.push_back(Chain);
4428 break;
Jim Laskey279f0532006-09-25 16:29:54 +00004429 }
4430 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004431}
4432
4433/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
4434/// for a better chain (aliasing node.)
4435SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
4436 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00004437
Jim Laskey6ff23e52006-10-04 16:53:27 +00004438 // Accumulate all the aliases to this node.
4439 GatherAllAliases(N, OldChain, Aliases);
4440
4441 if (Aliases.size() == 0) {
4442 // If no operands then chain to entry token.
4443 return DAG.getEntryNode();
4444 } else if (Aliases.size() == 1) {
4445 // If a single operand then chain to it. We don't need to revisit it.
4446 return Aliases[0];
4447 }
4448
4449 // Construct a custom tailored token factor.
4450 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4451 &Aliases[0], Aliases.size());
4452
4453 // Make sure the old chain gets cleaned up.
4454 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
4455
4456 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00004457}
4458
Nate Begeman1d4d4142005-09-01 00:19:25 +00004459// SelectionDAG::Combine - This is the entry point for the file.
4460//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004461void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00004462 /// run - This is the main entry point to this class.
4463 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004464 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004465}