blob: 04f21b9557ce11cb6c39d41e609b77349f03c3e8 [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 +000041using namespace llvm;
42
43namespace {
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000044 static Statistic NodesCombined ("dagcombiner",
Andrew Lenharthae6153f2006-07-20 17:43:27 +000045 "Number of dag nodes combined");
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000046
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000047 static Statistic PreIndexedNodes ("pre_indexed_ops",
Evan Chengbbd6f6e2006-11-07 09:03:05 +000048 "Number of pre-indexed nodes created");
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000049 static Statistic PostIndexedNodes ("post_indexed_ops",
Evan Chengbbd6f6e2006-11-07 09:03:05 +000050 "Number of post-indexed nodes created");
51
Jim Laskey71382342006-10-07 23:37:56 +000052 static cl::opt<bool>
53 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000054 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000055
Jim Laskey07a27092006-10-18 19:08:31 +000056 static cl::opt<bool>
57 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
58 cl::desc("Include global information in alias analysis"));
59
Jim Laskeybc588b82006-10-05 15:07:25 +000060//------------------------------ DAGCombiner ---------------------------------//
61
Jim Laskey71382342006-10-07 23:37:56 +000062 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000063 SelectionDAG &DAG;
64 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000065 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000066
67 // Worklist of all of the nodes that need to be simplified.
68 std::vector<SDNode*> WorkList;
69
Jim Laskeyc7c3f112006-10-16 20:52:31 +000070 // AA - Used for DAG load/store alias analysis.
71 AliasAnalysis &AA;
72
Nate Begeman1d4d4142005-09-01 00:19:25 +000073 /// AddUsersToWorkList - When an instruction is simplified, add all users of
74 /// the instruction to the work lists because they might get more simplified
75 /// now.
76 ///
77 void AddUsersToWorkList(SDNode *N) {
78 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000079 UI != UE; ++UI)
Jim Laskey6ff23e52006-10-04 16:53:27 +000080 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000081 }
82
83 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000084 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000085 void removeFromWorkList(SDNode *N) {
86 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
87 WorkList.end());
88 }
89
Chris Lattner24664722006-03-01 04:53:38 +000090 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000091 /// AddToWorkList - Add to the work list making sure it's instance is at the
92 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000093 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000094 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000095 WorkList.push_back(N);
96 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000097
Jim Laskey274062c2006-10-13 23:32:28 +000098 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
99 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000100 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
Chris Lattner87514ca2005-10-10 22:31:19 +0000101 ++NodesCombined;
Bill Wendling832171c2006-12-07 20:04:42 +0000102 DOUT << "\nReplacing.1 "; DEBUG(N->dump());
103 DOUT << "\nWith: "; DEBUG(To[0].Val->dump(&DAG));
104 DOUT << " and " << NumTo-1 << " other values\n";
Chris Lattner01a22022005-10-10 22:04:48 +0000105 std::vector<SDNode*> NowDead;
Chris Lattner3577e382006-08-11 17:56:38 +0000106 DAG.ReplaceAllUsesWith(N, To, &NowDead);
Chris Lattner01a22022005-10-10 22:04:48 +0000107
Jim Laskey274062c2006-10-13 23:32:28 +0000108 if (AddTo) {
109 // Push the new nodes and any users onto the worklist
110 for (unsigned i = 0, e = NumTo; i != e; ++i) {
111 AddToWorkList(To[i].Val);
112 AddUsersToWorkList(To[i].Val);
113 }
Chris Lattner01a22022005-10-10 22:04:48 +0000114 }
115
Jim Laskey6ff23e52006-10-04 16:53:27 +0000116 // Nodes can be reintroduced into the worklist. Make sure we do not
117 // process a node that has been replaced.
Chris Lattner01a22022005-10-10 22:04:48 +0000118 removeFromWorkList(N);
119 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
120 removeFromWorkList(NowDead[i]);
121
122 // Finally, since the node is now dead, remove it from the graph.
123 DAG.DeleteNode(N);
124 return SDOperand(N, 0);
125 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000126
Jim Laskey274062c2006-10-13 23:32:28 +0000127 SDOperand CombineTo(SDNode *N, SDOperand Res, bool AddTo = true) {
128 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000129 }
130
Jim Laskey274062c2006-10-13 23:32:28 +0000131 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1,
132 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000133 SDOperand To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000134 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000135 }
136 private:
137
Chris Lattner012f2412006-02-17 21:58:01 +0000138 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000139 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000140 /// propagation. If so, return true.
141 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000142 TargetLowering::TargetLoweringOpt TLO(DAG);
143 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000144 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
145 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
146 return false;
147
148 // Revisit the node.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000149 AddToWorkList(Op.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000150
151 // Replace the old value with the new one.
152 ++NodesCombined;
Bill Wendling832171c2006-12-07 20:04:42 +0000153 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.Val->dump());
154 DOUT << "\nWith: "; DEBUG(TLO.New.Val->dump(&DAG));
155 DOUT << '\n';
Chris Lattner012f2412006-02-17 21:58:01 +0000156
157 std::vector<SDNode*> NowDead;
158 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
159
Chris Lattner7d20d392006-02-20 06:51:04 +0000160 // Push the new node and any (possibly new) users onto the worklist.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000161 AddToWorkList(TLO.New.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000162 AddUsersToWorkList(TLO.New.Val);
163
164 // Nodes can end up on the worklist more than once. Make sure we do
165 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000166 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
167 removeFromWorkList(NowDead[i]);
168
Chris Lattner7d20d392006-02-20 06:51:04 +0000169 // Finally, if the node is now dead, remove it from the graph. The node
170 // may not be dead if the replacement process recursively simplified to
171 // something else needing this node.
172 if (TLO.Old.Val->use_empty()) {
173 removeFromWorkList(TLO.Old.Val);
174 DAG.DeleteNode(TLO.Old.Val);
175 }
Chris Lattner012f2412006-02-17 21:58:01 +0000176 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000177 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000178
Chris Lattner448f2192006-11-11 00:39:41 +0000179 bool CombineToPreIndexedLoadStore(SDNode *N);
180 bool CombineToPostIndexedLoadStore(SDNode *N);
181
182
Nate Begeman1d4d4142005-09-01 00:19:25 +0000183 /// visit - call the node-specific routine that knows how to fold each
184 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000185 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000186
187 // Visitation implementation - Implement dag node combining for different
188 // node types. The semantics are as follows:
189 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000190 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000191 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000192 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000193 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000194 SDOperand visitTokenFactor(SDNode *N);
195 SDOperand visitADD(SDNode *N);
196 SDOperand visitSUB(SDNode *N);
197 SDOperand visitMUL(SDNode *N);
198 SDOperand visitSDIV(SDNode *N);
199 SDOperand visitUDIV(SDNode *N);
200 SDOperand visitSREM(SDNode *N);
201 SDOperand visitUREM(SDNode *N);
202 SDOperand visitMULHU(SDNode *N);
203 SDOperand visitMULHS(SDNode *N);
204 SDOperand visitAND(SDNode *N);
205 SDOperand visitOR(SDNode *N);
206 SDOperand visitXOR(SDNode *N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000207 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000208 SDOperand visitSHL(SDNode *N);
209 SDOperand visitSRA(SDNode *N);
210 SDOperand visitSRL(SDNode *N);
211 SDOperand visitCTLZ(SDNode *N);
212 SDOperand visitCTTZ(SDNode *N);
213 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000214 SDOperand visitSELECT(SDNode *N);
215 SDOperand visitSELECT_CC(SDNode *N);
216 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000217 SDOperand visitSIGN_EXTEND(SDNode *N);
218 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000219 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000220 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
221 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000222 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000223 SDOperand visitVBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000224 SDOperand visitFADD(SDNode *N);
225 SDOperand visitFSUB(SDNode *N);
226 SDOperand visitFMUL(SDNode *N);
227 SDOperand visitFDIV(SDNode *N);
228 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000229 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000230 SDOperand visitSINT_TO_FP(SDNode *N);
231 SDOperand visitUINT_TO_FP(SDNode *N);
232 SDOperand visitFP_TO_SINT(SDNode *N);
233 SDOperand visitFP_TO_UINT(SDNode *N);
234 SDOperand visitFP_ROUND(SDNode *N);
235 SDOperand visitFP_ROUND_INREG(SDNode *N);
236 SDOperand visitFP_EXTEND(SDNode *N);
237 SDOperand visitFNEG(SDNode *N);
238 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000239 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000240 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000241 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000242 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000243 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
244 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000245 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000246 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000247 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000248
Evan Cheng44f1f092006-04-20 08:56:16 +0000249 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000250 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
251
Chris Lattner40c62d52005-10-18 06:04:22 +0000252 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000253 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000254 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
255 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
256 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000257 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000258 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000259 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000260 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000261 SDOperand BuildUDIV(SDNode *N);
262 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Jim Laskey279f0532006-09-25 16:29:54 +0000263
Jim Laskey6ff23e52006-10-04 16:53:27 +0000264 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
265 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000266 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000267 SmallVector<SDOperand, 8> &Aliases);
268
Jim Laskey096c22e2006-10-18 12:29:57 +0000269 /// isAlias - Return true if there is any possibility that the two addresses
270 /// overlap.
271 bool isAlias(SDOperand Ptr1, int64_t Size1,
272 const Value *SrcValue1, int SrcValueOffset1,
273 SDOperand Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000274 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000275
Jim Laskey7ca56af2006-10-11 13:47:09 +0000276 /// FindAliasInfo - Extracts the relevant alias information from the memory
277 /// node. Returns true if the operand was a load.
278 bool FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +0000279 SDOperand &Ptr, int64_t &Size,
280 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000281
Jim Laskey279f0532006-09-25 16:29:54 +0000282 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000283 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000284 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
285
Nate Begeman1d4d4142005-09-01 00:19:25 +0000286public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000287 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
288 : DAG(D),
289 TLI(D.getTargetLoweringInfo()),
290 AfterLegalize(false),
291 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000292
293 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000294 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000295 };
296}
297
Chris Lattner24664722006-03-01 04:53:38 +0000298//===----------------------------------------------------------------------===//
299// TargetLowering::DAGCombinerInfo implementation
300//===----------------------------------------------------------------------===//
301
302void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
303 ((DAGCombiner*)DC)->AddToWorkList(N);
304}
305
306SDOperand TargetLowering::DAGCombinerInfo::
307CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000308 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000309}
310
311SDOperand TargetLowering::DAGCombinerInfo::
312CombineTo(SDNode *N, SDOperand Res) {
313 return ((DAGCombiner*)DC)->CombineTo(N, Res);
314}
315
316
317SDOperand TargetLowering::DAGCombinerInfo::
318CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
319 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
320}
321
322
323
324
325//===----------------------------------------------------------------------===//
326
327
Nate Begeman4ebd8052005-09-01 23:24:04 +0000328// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
329// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000330// Also, set the incoming LHS, RHS, and CC references to the appropriate
331// nodes based on the type of node we are checking. This simplifies life a
332// bit for the callers.
333static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
334 SDOperand &CC) {
335 if (N.getOpcode() == ISD::SETCC) {
336 LHS = N.getOperand(0);
337 RHS = N.getOperand(1);
338 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000339 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000340 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000341 if (N.getOpcode() == ISD::SELECT_CC &&
342 N.getOperand(2).getOpcode() == ISD::Constant &&
343 N.getOperand(3).getOpcode() == ISD::Constant &&
344 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000345 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
346 LHS = N.getOperand(0);
347 RHS = N.getOperand(1);
348 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000349 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000350 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000351 return false;
352}
353
Nate Begeman99801192005-09-07 23:25:52 +0000354// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
355// one use. If this is true, it allows the users to invert the operation for
356// free when it is profitable to do so.
357static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000358 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000359 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000360 return true;
361 return false;
362}
363
Nate Begemancd4d58c2006-02-03 06:46:56 +0000364SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
365 MVT::ValueType VT = N0.getValueType();
366 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
367 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
368 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
369 if (isa<ConstantSDNode>(N1)) {
370 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000371 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000372 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
373 } else if (N0.hasOneUse()) {
374 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000375 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000376 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
377 }
378 }
379 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
380 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
381 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
382 if (isa<ConstantSDNode>(N0)) {
383 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000384 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000385 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
386 } else if (N1.hasOneUse()) {
387 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000388 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000389 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
390 }
391 }
392 return SDOperand();
393}
394
Nate Begeman4ebd8052005-09-01 23:24:04 +0000395void DAGCombiner::Run(bool RunningAfterLegalize) {
396 // set the instance variable, so that the various visit routines may use it.
397 AfterLegalize = RunningAfterLegalize;
398
Nate Begeman646d7e22005-09-02 21:18:40 +0000399 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000400 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
401 E = DAG.allnodes_end(); I != E; ++I)
402 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000403
Chris Lattner95038592005-10-05 06:35:28 +0000404 // Create a dummy node (which is not added to allnodes), that adds a reference
405 // to the root node, preventing it from being deleted, and tracking any
406 // changes of the root.
407 HandleSDNode Dummy(DAG.getRoot());
408
Jim Laskey26f7fa72006-10-17 19:33:52 +0000409 // The root of the dag may dangle to deleted nodes until the dag combiner is
410 // done. Set it to null to avoid confusion.
411 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000412
413 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
414 TargetLowering::DAGCombinerInfo
415 DagCombineInfo(DAG, !RunningAfterLegalize, this);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000416
Nate Begeman1d4d4142005-09-01 00:19:25 +0000417 // while the worklist isn't empty, inspect the node on the end of it and
418 // try and combine it.
419 while (!WorkList.empty()) {
420 SDNode *N = WorkList.back();
421 WorkList.pop_back();
422
423 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000424 // N is deleted from the DAG, since they too may now be dead or may have a
425 // reduced number of uses, allowing other xforms.
426 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000427 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000428 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000429
Chris Lattner95038592005-10-05 06:35:28 +0000430 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000431 continue;
432 }
433
Nate Begeman83e75ec2005-09-06 04:43:02 +0000434 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000435
436 // If nothing happened, try a target-specific DAG combine.
437 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000438 assert(N->getOpcode() != ISD::DELETED_NODE &&
439 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000440 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
441 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
442 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
443 }
444
Nate Begeman83e75ec2005-09-06 04:43:02 +0000445 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000446 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000447 // If we get back the same node we passed in, rather than a new node or
448 // zero, we know that the node must have defined multiple values and
449 // CombineTo was used. Since CombineTo takes care of the worklist
450 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000451 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000452 assert(N->getOpcode() != ISD::DELETED_NODE &&
453 RV.Val->getOpcode() != ISD::DELETED_NODE &&
454 "Node was deleted but visit returned new node!");
455
Bill Wendling832171c2006-12-07 20:04:42 +0000456 DOUT << "\nReplacing.3 "; DEBUG(N->dump());
457 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
458 DOUT << '\n';
Chris Lattner01a22022005-10-10 22:04:48 +0000459 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000460 if (N->getNumValues() == RV.Val->getNumValues())
461 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
462 else {
463 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
464 SDOperand OpV = RV;
465 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
466 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000467
468 // Push the new node and any users onto the worklist
Jim Laskey6ff23e52006-10-04 16:53:27 +0000469 AddToWorkList(RV.Val);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000470 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000471
Jim Laskey6ff23e52006-10-04 16:53:27 +0000472 // Nodes can be reintroduced into the worklist. Make sure we do not
473 // process a node that has been replaced.
Nate Begeman646d7e22005-09-02 21:18:40 +0000474 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000475 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
476 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000477
478 // Finally, since the node is now dead, remove it from the graph.
479 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000480 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000481 }
482 }
Chris Lattner95038592005-10-05 06:35:28 +0000483
484 // If the root changed (e.g. it was a dead load, update the root).
485 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000486}
487
Nate Begeman83e75ec2005-09-06 04:43:02 +0000488SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000489 switch(N->getOpcode()) {
490 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000491 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000492 case ISD::ADD: return visitADD(N);
493 case ISD::SUB: return visitSUB(N);
494 case ISD::MUL: return visitMUL(N);
495 case ISD::SDIV: return visitSDIV(N);
496 case ISD::UDIV: return visitUDIV(N);
497 case ISD::SREM: return visitSREM(N);
498 case ISD::UREM: return visitUREM(N);
499 case ISD::MULHU: return visitMULHU(N);
500 case ISD::MULHS: return visitMULHS(N);
501 case ISD::AND: return visitAND(N);
502 case ISD::OR: return visitOR(N);
503 case ISD::XOR: return visitXOR(N);
504 case ISD::SHL: return visitSHL(N);
505 case ISD::SRA: return visitSRA(N);
506 case ISD::SRL: return visitSRL(N);
507 case ISD::CTLZ: return visitCTLZ(N);
508 case ISD::CTTZ: return visitCTTZ(N);
509 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000510 case ISD::SELECT: return visitSELECT(N);
511 case ISD::SELECT_CC: return visitSELECT_CC(N);
512 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000513 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
514 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000515 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000516 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
517 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000518 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000519 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000520 case ISD::FADD: return visitFADD(N);
521 case ISD::FSUB: return visitFSUB(N);
522 case ISD::FMUL: return visitFMUL(N);
523 case ISD::FDIV: return visitFDIV(N);
524 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000525 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000526 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
527 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
528 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
529 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
530 case ISD::FP_ROUND: return visitFP_ROUND(N);
531 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
532 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
533 case ISD::FNEG: return visitFNEG(N);
534 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000535 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000536 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000537 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000538 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000539 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
540 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000541 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000542 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000543 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000544 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
545 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
546 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
547 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
548 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
549 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
550 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
551 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000552 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000553 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000554}
555
Chris Lattner6270f682006-10-08 22:57:01 +0000556/// getInputChainForNode - Given a node, return its input chain if it has one,
557/// otherwise return a null sd operand.
558static SDOperand getInputChainForNode(SDNode *N) {
559 if (unsigned NumOps = N->getNumOperands()) {
560 if (N->getOperand(0).getValueType() == MVT::Other)
561 return N->getOperand(0);
562 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
563 return N->getOperand(NumOps-1);
564 for (unsigned i = 1; i < NumOps-1; ++i)
565 if (N->getOperand(i).getValueType() == MVT::Other)
566 return N->getOperand(i);
567 }
568 return SDOperand(0, 0);
569}
570
Nate Begeman83e75ec2005-09-06 04:43:02 +0000571SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000572 // If N has two operands, where one has an input chain equal to the other,
573 // the 'other' chain is redundant.
574 if (N->getNumOperands() == 2) {
575 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
576 return N->getOperand(0);
577 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
578 return N->getOperand(1);
579 }
580
581
Jim Laskey6ff23e52006-10-04 16:53:27 +0000582 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Jim Laskey279f0532006-09-25 16:29:54 +0000583 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000584 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000585
586 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000587 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000588
Jim Laskey71382342006-10-07 23:37:56 +0000589 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000590 // encountered.
591 for (unsigned i = 0; i < TFs.size(); ++i) {
592 SDNode *TF = TFs[i];
593
Jim Laskey6ff23e52006-10-04 16:53:27 +0000594 // Check each of the operands.
595 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
596 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000597
Jim Laskey6ff23e52006-10-04 16:53:27 +0000598 switch (Op.getOpcode()) {
599 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000600 // Entry tokens don't need to be added to the list. They are
601 // rededundant.
602 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000603 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000604
Jim Laskey6ff23e52006-10-04 16:53:27 +0000605 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000606 if ((CombinerAA || Op.hasOneUse()) &&
607 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000608 // Queue up for processing.
609 TFs.push_back(Op.Val);
610 // Clean up in case the token factor is removed.
611 AddToWorkList(Op.Val);
612 Changed = true;
613 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000614 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000615 // Fall thru
616
617 default:
Jim Laskeybc588b82006-10-05 15:07:25 +0000618 // Only add if not there prior.
619 if (std::find(Ops.begin(), Ops.end(), Op) == Ops.end())
620 Ops.push_back(Op);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000621 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000622 }
623 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000624 }
625
626 SDOperand Result;
627
628 // If we've change things around then replace token factor.
629 if (Changed) {
630 if (Ops.size() == 0) {
631 // The entry token is the only possible outcome.
632 Result = DAG.getEntryNode();
633 } else {
634 // New and improved token factor.
635 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000636 }
Jim Laskey274062c2006-10-13 23:32:28 +0000637
638 // Don't add users to work list.
639 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000640 }
Jim Laskey279f0532006-09-25 16:29:54 +0000641
Jim Laskey6ff23e52006-10-04 16:53:27 +0000642 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000643}
644
Nate Begeman83e75ec2005-09-06 04:43:02 +0000645SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000646 SDOperand N0 = N->getOperand(0);
647 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000648 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
649 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000650 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000651
652 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000653 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000654 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000655 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000656 if (N0C && !N1C)
657 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000658 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000659 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000660 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000661 // fold ((c1-A)+c2) -> (c1+c2)-A
662 if (N1C && N0.getOpcode() == ISD::SUB)
663 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
664 return DAG.getNode(ISD::SUB, VT,
665 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
666 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000667 // reassociate add
668 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
669 if (RADD.Val != 0)
670 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000671 // fold ((0-A) + B) -> B-A
672 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
673 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000674 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000675 // fold (A + (0-B)) -> A-B
676 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
677 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000678 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000679 // fold (A+(B-A)) -> B
680 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000681 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000682
Evan Cheng860771d2006-03-01 01:09:54 +0000683 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000684 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000685
686 // fold (a+b) -> (a|b) iff a and b share no bits.
687 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
688 uint64_t LHSZero, LHSOne;
689 uint64_t RHSZero, RHSOne;
690 uint64_t Mask = MVT::getIntVTBitMask(VT);
691 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
692 if (LHSZero) {
693 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
694
695 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
696 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
697 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
698 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
699 return DAG.getNode(ISD::OR, VT, N0, N1);
700 }
701 }
Evan Cheng3ef554d2006-11-06 08:14:30 +0000702
Nate Begeman83e75ec2005-09-06 04:43:02 +0000703 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000704}
705
Nate Begeman83e75ec2005-09-06 04:43:02 +0000706SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000707 SDOperand N0 = N->getOperand(0);
708 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000709 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
710 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000711 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000712
Chris Lattner854077d2005-10-17 01:07:11 +0000713 // fold (sub x, x) -> 0
714 if (N0 == N1)
715 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000716 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000717 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000718 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000719 // fold (sub x, c) -> (add x, -c)
720 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000721 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000722 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000723 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000724 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000725 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000726 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000727 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000728 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000729}
730
Nate Begeman83e75ec2005-09-06 04:43:02 +0000731SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000732 SDOperand N0 = N->getOperand(0);
733 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000734 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
735 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000736 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000737
738 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000739 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000740 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000741 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000742 if (N0C && !N1C)
743 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000744 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000745 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000746 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000747 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000748 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000749 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000750 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000751 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000752 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000753 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000754 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000755 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
756 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
757 // FIXME: If the input is something that is easily negated (e.g. a
758 // single-use add), we should put the negate there.
759 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
760 DAG.getNode(ISD::SHL, VT, N0,
761 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
762 TLI.getShiftAmountTy())));
763 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +0000764
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000765 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
766 if (N1C && N0.getOpcode() == ISD::SHL &&
767 isa<ConstantSDNode>(N0.getOperand(1))) {
768 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000769 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000770 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
771 }
772
773 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
774 // use.
775 {
776 SDOperand Sh(0,0), Y(0,0);
777 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
778 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
779 N0.Val->hasOneUse()) {
780 Sh = N0; Y = N1;
781 } else if (N1.getOpcode() == ISD::SHL &&
782 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
783 Sh = N1; Y = N0;
784 }
785 if (Sh.Val) {
786 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
787 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
788 }
789 }
Chris Lattnera1deca32006-03-04 23:33:26 +0000790 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
791 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
792 isa<ConstantSDNode>(N0.getOperand(1))) {
793 return DAG.getNode(ISD::ADD, VT,
794 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
795 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
796 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000797
Nate Begemancd4d58c2006-02-03 06:46:56 +0000798 // reassociate mul
799 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
800 if (RMUL.Val != 0)
801 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000802 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000803}
804
Nate Begeman83e75ec2005-09-06 04:43:02 +0000805SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000806 SDOperand N0 = N->getOperand(0);
807 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000808 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
809 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000810 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000811
812 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000813 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000814 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000815 // fold (sdiv X, 1) -> X
816 if (N1C && N1C->getSignExtended() == 1LL)
817 return N0;
818 // fold (sdiv X, -1) -> 0-X
819 if (N1C && N1C->isAllOnesValue())
820 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000821 // If we know the sign bits of both operands are zero, strength reduce to a
822 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
823 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000824 if (TLI.MaskedValueIsZero(N1, SignBit) &&
825 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000826 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000827 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000828 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000829 (isPowerOf2_64(N1C->getSignExtended()) ||
830 isPowerOf2_64(-N1C->getSignExtended()))) {
831 // If dividing by powers of two is cheap, then don't perform the following
832 // fold.
833 if (TLI.isPow2DivCheap())
834 return SDOperand();
835 int64_t pow2 = N1C->getSignExtended();
836 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000837 unsigned lg2 = Log2_64(abs2);
838 // Splat the sign bit into the register
839 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000840 DAG.getConstant(MVT::getSizeInBits(VT)-1,
841 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +0000842 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000843 // Add (N0 < 0) ? abs2 - 1 : 0;
844 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
845 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000846 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000847 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +0000848 AddToWorkList(SRL.Val);
849 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +0000850 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
851 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000852 // If we're dividing by a positive value, we're done. Otherwise, we must
853 // negate the result.
854 if (pow2 > 0)
855 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +0000856 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000857 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
858 }
Nate Begeman69575232005-10-20 02:15:44 +0000859 // if integer divide is expensive and we satisfy the requirements, emit an
860 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000861 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000862 !TLI.isIntDivCheap()) {
863 SDOperand Op = BuildSDIV(N);
864 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000865 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000866 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000867}
868
Nate Begeman83e75ec2005-09-06 04:43:02 +0000869SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000870 SDOperand N0 = N->getOperand(0);
871 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000872 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
873 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000874 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000875
876 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000877 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000878 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000879 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000880 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000881 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000882 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000883 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000884 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
885 if (N1.getOpcode() == ISD::SHL) {
886 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
887 if (isPowerOf2_64(SHC->getValue())) {
888 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000889 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
890 DAG.getConstant(Log2_64(SHC->getValue()),
891 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +0000892 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000893 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000894 }
895 }
896 }
Nate Begeman69575232005-10-20 02:15:44 +0000897 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000898 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
899 SDOperand Op = BuildUDIV(N);
900 if (Op.Val) return Op;
901 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000902 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000903}
904
Nate Begeman83e75ec2005-09-06 04:43:02 +0000905SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000906 SDOperand N0 = N->getOperand(0);
907 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000908 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
909 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000910 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000911
912 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000913 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000914 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000915 // If we know the sign bits of both operands are zero, strength reduce to a
916 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
917 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000918 if (TLI.MaskedValueIsZero(N1, SignBit) &&
919 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000920 return DAG.getNode(ISD::UREM, VT, N0, N1);
Chris Lattner26d29902006-10-12 20:58:32 +0000921
922 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
923 // the remainder operation.
924 if (N1C && !N1C->isNullValue()) {
925 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
926 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
927 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
928 AddToWorkList(Div.Val);
929 AddToWorkList(Mul.Val);
930 return Sub;
931 }
932
Nate Begeman83e75ec2005-09-06 04:43:02 +0000933 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000934}
935
Nate Begeman83e75ec2005-09-06 04:43:02 +0000936SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000937 SDOperand N0 = N->getOperand(0);
938 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000939 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
940 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000941 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000942
943 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000944 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000945 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000946 // fold (urem x, pow2) -> (and x, pow2-1)
947 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000948 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000949 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
950 if (N1.getOpcode() == ISD::SHL) {
951 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
952 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000953 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +0000954 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000955 return DAG.getNode(ISD::AND, VT, N0, Add);
956 }
957 }
958 }
Chris Lattner26d29902006-10-12 20:58:32 +0000959
960 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
961 // the remainder operation.
962 if (N1C && !N1C->isNullValue()) {
963 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
964 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
965 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
966 AddToWorkList(Div.Val);
967 AddToWorkList(Mul.Val);
968 return Sub;
969 }
970
Nate Begeman83e75ec2005-09-06 04:43:02 +0000971 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000972}
973
Nate Begeman83e75ec2005-09-06 04:43:02 +0000974SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000975 SDOperand N0 = N->getOperand(0);
976 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000977 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000978
979 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000980 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000981 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000982 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000983 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000984 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
985 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000986 TLI.getShiftAmountTy()));
987 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000988}
989
Nate Begeman83e75ec2005-09-06 04:43:02 +0000990SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000991 SDOperand N0 = N->getOperand(0);
992 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000993 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000994
995 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000996 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000997 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000998 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000999 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001000 return DAG.getConstant(0, N0.getValueType());
1001 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001002}
1003
Chris Lattner35e5c142006-05-05 05:51:50 +00001004/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1005/// two operands of the same opcode, try to simplify it.
1006SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1007 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1008 MVT::ValueType VT = N0.getValueType();
1009 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1010
Chris Lattner540121f2006-05-05 06:31:05 +00001011 // For each of OP in AND/OR/XOR:
1012 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1013 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1014 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001015 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001016 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001017 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001018 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1019 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1020 N0.getOperand(0).getValueType(),
1021 N0.getOperand(0), N1.getOperand(0));
1022 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001023 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001024 }
1025
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001026 // For each of OP in SHL/SRL/SRA/AND...
1027 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1028 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1029 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001030 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001031 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001032 N0.getOperand(1) == N1.getOperand(1)) {
1033 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1034 N0.getOperand(0).getValueType(),
1035 N0.getOperand(0), N1.getOperand(0));
1036 AddToWorkList(ORNode.Val);
1037 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1038 }
1039
1040 return SDOperand();
1041}
1042
Nate Begeman83e75ec2005-09-06 04:43:02 +00001043SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001044 SDOperand N0 = N->getOperand(0);
1045 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001046 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001047 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1048 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001049 MVT::ValueType VT = N1.getValueType();
1050
1051 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001052 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001053 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001054 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001055 if (N0C && !N1C)
1056 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001057 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001058 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001059 return N0;
1060 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +00001061 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001062 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001063 // reassociate and
1064 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1065 if (RAND.Val != 0)
1066 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001067 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001068 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001069 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001070 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001071 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001072 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1073 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001074 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +00001075 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001076 ~N1C->getValue() & InMask)) {
1077 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1078 N0.getOperand(0));
1079
1080 // Replace uses of the AND with uses of the Zero extend node.
1081 CombineTo(N, Zext);
1082
Chris Lattner3603cd62006-02-02 07:17:31 +00001083 // We actually want to replace all uses of the any_extend with the
1084 // zero_extend, to avoid duplicating things. This will later cause this
1085 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001086 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001087 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001088 }
1089 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001090 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1091 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1092 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1093 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1094
1095 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1096 MVT::isInteger(LL.getValueType())) {
1097 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1098 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1099 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001100 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001101 return DAG.getSetCC(VT, ORNode, LR, Op1);
1102 }
1103 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1104 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1105 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001106 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001107 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1108 }
1109 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1110 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1111 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001112 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001113 return DAG.getSetCC(VT, ORNode, LR, Op1);
1114 }
1115 }
1116 // canonicalize equivalent to ll == rl
1117 if (LL == RR && LR == RL) {
1118 Op1 = ISD::getSetCCSwappedOperands(Op1);
1119 std::swap(RL, RR);
1120 }
1121 if (LL == RL && LR == RR) {
1122 bool isInteger = MVT::isInteger(LL.getValueType());
1123 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1124 if (Result != ISD::SETCC_INVALID)
1125 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1126 }
1127 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001128
1129 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1130 if (N0.getOpcode() == N1.getOpcode()) {
1131 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1132 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001133 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001134
Nate Begemande996292006-02-03 22:24:05 +00001135 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1136 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001137 if (!MVT::isVector(VT) &&
1138 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001139 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001140 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Chengc5484282006-10-04 00:56:09 +00001141 if (ISD::isEXTLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001142 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001143 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001144 // If we zero all the possible extended bits, then we can turn this into
1145 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001146 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001147 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001148 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1149 LN0->getBasePtr(), LN0->getSrcValue(),
1150 LN0->getSrcValueOffset(), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001151 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001152 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001153 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001154 }
1155 }
1156 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Chengc5484282006-10-04 00:56:09 +00001157 if (ISD::isSEXTLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001158 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001159 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001160 // If we zero all the possible extended bits, then we can turn this into
1161 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001162 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001163 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001164 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1165 LN0->getBasePtr(), LN0->getSrcValue(),
1166 LN0->getSrcValueOffset(), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001167 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001168 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001169 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001170 }
1171 }
Chris Lattner15045b62006-02-28 06:35:35 +00001172
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001173 // fold (and (load x), 255) -> (zextload x, i8)
1174 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001175 if (N1C && N0.getOpcode() == ISD::LOAD) {
1176 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1177 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
1178 N0.hasOneUse()) {
1179 MVT::ValueType EVT, LoadedVT;
1180 if (N1C->getValue() == 255)
1181 EVT = MVT::i8;
1182 else if (N1C->getValue() == 65535)
1183 EVT = MVT::i16;
1184 else if (N1C->getValue() == ~0U)
1185 EVT = MVT::i32;
1186 else
1187 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001188
Evan Cheng2e49f092006-10-11 07:10:22 +00001189 LoadedVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001190 if (EVT != MVT::Other && LoadedVT > EVT &&
1191 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1192 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1193 // For big endian targets, we need to add an offset to the pointer to
1194 // load the correct bytes. For little endian systems, we merely need to
1195 // read fewer bytes from the same pointer.
1196 unsigned PtrOff =
1197 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1198 SDOperand NewPtr = LN0->getBasePtr();
1199 if (!TLI.isLittleEndian())
1200 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1201 DAG.getConstant(PtrOff, PtrType));
1202 AddToWorkList(NewPtr.Val);
1203 SDOperand Load =
1204 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
1205 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT);
1206 AddToWorkList(N);
1207 CombineTo(N0.Val, Load, Load.getValue(1));
1208 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1209 }
Chris Lattner15045b62006-02-28 06:35:35 +00001210 }
1211 }
1212
Nate Begeman83e75ec2005-09-06 04:43:02 +00001213 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001214}
1215
Nate Begeman83e75ec2005-09-06 04:43:02 +00001216SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001217 SDOperand N0 = N->getOperand(0);
1218 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001219 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001220 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1221 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001222 MVT::ValueType VT = N1.getValueType();
1223 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001224
1225 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001226 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001227 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001228 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001229 if (N0C && !N1C)
1230 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001231 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001232 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001233 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001234 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001235 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001236 return N1;
1237 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001238 if (N1C &&
1239 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001240 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001241 // reassociate or
1242 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1243 if (ROR.Val != 0)
1244 return ROR;
1245 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1246 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001247 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001248 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1249 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1250 N1),
1251 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001252 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001253 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1254 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1255 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1256 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1257
1258 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1259 MVT::isInteger(LL.getValueType())) {
1260 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1261 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1262 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1263 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1264 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001265 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001266 return DAG.getSetCC(VT, ORNode, LR, Op1);
1267 }
1268 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1269 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1270 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1271 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1272 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001273 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001274 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1275 }
1276 }
1277 // canonicalize equivalent to ll == rl
1278 if (LL == RR && LR == RL) {
1279 Op1 = ISD::getSetCCSwappedOperands(Op1);
1280 std::swap(RL, RR);
1281 }
1282 if (LL == RL && LR == RR) {
1283 bool isInteger = MVT::isInteger(LL.getValueType());
1284 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1285 if (Result != ISD::SETCC_INVALID)
1286 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1287 }
1288 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001289
1290 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1291 if (N0.getOpcode() == N1.getOpcode()) {
1292 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1293 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001294 }
Chris Lattner516b9622006-09-14 20:50:57 +00001295
Chris Lattner1ec72732006-09-14 21:11:37 +00001296 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1297 if (N0.getOpcode() == ISD::AND &&
1298 N1.getOpcode() == ISD::AND &&
1299 N0.getOperand(1).getOpcode() == ISD::Constant &&
1300 N1.getOperand(1).getOpcode() == ISD::Constant &&
1301 // Don't increase # computations.
1302 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1303 // We can only do this xform if we know that bits from X that are set in C2
1304 // but not in C1 are already zero. Likewise for Y.
1305 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1306 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1307
1308 if (TLI.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1309 TLI.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
1310 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1311 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1312 }
1313 }
1314
1315
Chris Lattner516b9622006-09-14 20:50:57 +00001316 // See if this is some rotate idiom.
1317 if (SDNode *Rot = MatchRotate(N0, N1))
1318 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001319
Nate Begeman83e75ec2005-09-06 04:43:02 +00001320 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001321}
1322
Chris Lattner516b9622006-09-14 20:50:57 +00001323
1324/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1325static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1326 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001327 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001328 Mask = Op.getOperand(1);
1329 Op = Op.getOperand(0);
1330 } else {
1331 return false;
1332 }
1333 }
1334
1335 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1336 Shift = Op;
1337 return true;
1338 }
1339 return false;
1340}
1341
1342
1343// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1344// idioms for rotate, and if the target supports rotation instructions, generate
1345// a rot[lr].
1346SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1347 // Must be a legal type. Expanded an promoted things won't work with rotates.
1348 MVT::ValueType VT = LHS.getValueType();
1349 if (!TLI.isTypeLegal(VT)) return 0;
1350
1351 // The target must have at least one rotate flavor.
1352 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1353 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1354 if (!HasROTL && !HasROTR) return 0;
1355
1356 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1357 SDOperand LHSShift; // The shift.
1358 SDOperand LHSMask; // AND value if any.
1359 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1360 return 0; // Not part of a rotate.
1361
1362 SDOperand RHSShift; // The shift.
1363 SDOperand RHSMask; // AND value if any.
1364 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1365 return 0; // Not part of a rotate.
1366
1367 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1368 return 0; // Not shifting the same value.
1369
1370 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1371 return 0; // Shifts must disagree.
1372
1373 // Canonicalize shl to left side in a shl/srl pair.
1374 if (RHSShift.getOpcode() == ISD::SHL) {
1375 std::swap(LHS, RHS);
1376 std::swap(LHSShift, RHSShift);
1377 std::swap(LHSMask , RHSMask );
1378 }
1379
1380 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1381
1382 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1383 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
1384 if (LHSShift.getOperand(1).getOpcode() == ISD::Constant &&
1385 RHSShift.getOperand(1).getOpcode() == ISD::Constant) {
1386 uint64_t LShVal = cast<ConstantSDNode>(LHSShift.getOperand(1))->getValue();
1387 uint64_t RShVal = cast<ConstantSDNode>(RHSShift.getOperand(1))->getValue();
1388 if ((LShVal + RShVal) != OpSizeInBits)
1389 return 0;
1390
1391 SDOperand Rot;
1392 if (HasROTL)
1393 Rot = DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1394 LHSShift.getOperand(1));
1395 else
1396 Rot = DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1397 RHSShift.getOperand(1));
1398
1399 // If there is an AND of either shifted operand, apply it to the result.
1400 if (LHSMask.Val || RHSMask.Val) {
1401 uint64_t Mask = MVT::getIntVTBitMask(VT);
1402
1403 if (LHSMask.Val) {
1404 uint64_t RHSBits = (1ULL << LShVal)-1;
1405 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1406 }
1407 if (RHSMask.Val) {
1408 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1409 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1410 }
1411
1412 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1413 }
1414
1415 return Rot.Val;
1416 }
1417
1418 // If there is a mask here, and we have a variable shift, we can't be sure
1419 // that we're masking out the right stuff.
1420 if (LHSMask.Val || RHSMask.Val)
1421 return 0;
1422
1423 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1424 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
1425 if (RHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1426 LHSShift.getOperand(1) == RHSShift.getOperand(1).getOperand(1)) {
1427 if (ConstantSDNode *SUBC =
1428 dyn_cast<ConstantSDNode>(RHSShift.getOperand(1).getOperand(0))) {
1429 if (SUBC->getValue() == OpSizeInBits)
1430 if (HasROTL)
1431 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1432 LHSShift.getOperand(1)).Val;
1433 else
1434 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1435 LHSShift.getOperand(1)).Val;
1436 }
1437 }
1438
1439 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1440 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
1441 if (LHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1442 RHSShift.getOperand(1) == LHSShift.getOperand(1).getOperand(1)) {
1443 if (ConstantSDNode *SUBC =
1444 dyn_cast<ConstantSDNode>(LHSShift.getOperand(1).getOperand(0))) {
1445 if (SUBC->getValue() == OpSizeInBits)
1446 if (HasROTL)
1447 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1448 LHSShift.getOperand(1)).Val;
1449 else
1450 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1451 RHSShift.getOperand(1)).Val;
1452 }
1453 }
1454
1455 return 0;
1456}
1457
1458
Nate Begeman83e75ec2005-09-06 04:43:02 +00001459SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001460 SDOperand N0 = N->getOperand(0);
1461 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001462 SDOperand LHS, RHS, CC;
1463 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1464 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001465 MVT::ValueType VT = N0.getValueType();
1466
1467 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001468 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001469 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001470 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001471 if (N0C && !N1C)
1472 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001473 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001474 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001475 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001476 // reassociate xor
1477 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1478 if (RXOR.Val != 0)
1479 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001480 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001481 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1482 bool isInt = MVT::isInteger(LHS.getValueType());
1483 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1484 isInt);
1485 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001486 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001487 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001488 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001489 assert(0 && "Unhandled SetCC Equivalent!");
1490 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001491 }
Nate Begeman99801192005-09-07 23:25:52 +00001492 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Chris Lattner734c91d2006-11-10 21:37:15 +00001493 if (N1C && N1C->getValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00001494 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001495 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001496 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1497 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001498 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1499 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001500 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001501 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001502 }
1503 }
Nate Begeman99801192005-09-07 23:25:52 +00001504 // fold !(x or y) -> (!x and !y) iff x or y are constants
1505 if (N1C && N1C->isAllOnesValue() &&
1506 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001507 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001508 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1509 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001510 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1511 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001512 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001513 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001514 }
1515 }
Nate Begeman223df222005-09-08 20:18:10 +00001516 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1517 if (N1C && N0.getOpcode() == ISD::XOR) {
1518 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1519 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1520 if (N00C)
1521 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1522 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1523 if (N01C)
1524 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1525 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1526 }
1527 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001528 if (N0 == N1) {
1529 if (!MVT::isVector(VT)) {
1530 return DAG.getConstant(0, VT);
1531 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1532 // Produce a vector of zeros.
1533 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1534 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001535 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001536 }
1537 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001538
1539 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1540 if (N0.getOpcode() == N1.getOpcode()) {
1541 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1542 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001543 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001544
Chris Lattner3e104b12006-04-08 04:15:24 +00001545 // Simplify the expression using non-local knowledge.
1546 if (!MVT::isVector(VT) &&
1547 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001548 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001549
Nate Begeman83e75ec2005-09-06 04:43:02 +00001550 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001551}
1552
Nate Begeman83e75ec2005-09-06 04:43:02 +00001553SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001554 SDOperand N0 = N->getOperand(0);
1555 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001556 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1557 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001558 MVT::ValueType VT = N0.getValueType();
1559 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1560
1561 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001562 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001563 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001564 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001565 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001566 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001567 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001568 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001569 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001570 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001571 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001572 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001573 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001574 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001575 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001576 if (SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001577 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001578 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001579 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001580 N0.getOperand(1).getOpcode() == ISD::Constant) {
1581 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001582 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001583 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001584 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001585 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001586 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001587 }
1588 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1589 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001590 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001591 N0.getOperand(1).getOpcode() == ISD::Constant) {
1592 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001593 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001594 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1595 DAG.getConstant(~0ULL << c1, VT));
1596 if (c2 > c1)
1597 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001598 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001599 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001600 return DAG.getNode(ISD::SRL, VT, Mask,
1601 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001602 }
1603 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001604 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001605 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001606 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnercac70592006-03-05 19:53:55 +00001607 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1608 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1609 isa<ConstantSDNode>(N0.getOperand(1))) {
1610 return DAG.getNode(ISD::ADD, VT,
1611 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1612 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1613 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001614 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001615}
1616
Nate Begeman83e75ec2005-09-06 04:43:02 +00001617SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001618 SDOperand N0 = N->getOperand(0);
1619 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001620 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1621 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001622 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001623
1624 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001625 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001626 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001627 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001628 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001629 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001630 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001631 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001632 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001633 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001634 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001635 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001636 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001637 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001638 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001639 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1640 // sext_inreg.
1641 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1642 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1643 MVT::ValueType EVT;
1644 switch (LowBits) {
1645 default: EVT = MVT::Other; break;
1646 case 1: EVT = MVT::i1; break;
1647 case 8: EVT = MVT::i8; break;
1648 case 16: EVT = MVT::i16; break;
1649 case 32: EVT = MVT::i32; break;
1650 }
1651 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1652 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1653 DAG.getValueType(EVT));
1654 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001655
1656 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1657 if (N1C && N0.getOpcode() == ISD::SRA) {
1658 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1659 unsigned Sum = N1C->getValue() + C1->getValue();
1660 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1661 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1662 DAG.getConstant(Sum, N1C->getValueType(0)));
1663 }
1664 }
1665
Chris Lattnera8504462006-05-08 20:51:54 +00001666 // Simplify, based on bits shifted out of the LHS.
1667 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
1668 return SDOperand(N, 0);
1669
1670
Nate Begeman1d4d4142005-09-01 00:19:25 +00001671 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001672 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001673 return DAG.getNode(ISD::SRL, VT, N0, N1);
1674 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001675}
1676
Nate Begeman83e75ec2005-09-06 04:43:02 +00001677SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001678 SDOperand N0 = N->getOperand(0);
1679 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001680 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1681 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001682 MVT::ValueType VT = N0.getValueType();
1683 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1684
1685 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001686 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001687 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001688 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001689 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001690 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001691 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001692 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001693 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001694 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001695 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001696 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001697 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001698 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001699 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001700 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001701 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001702 N0.getOperand(1).getOpcode() == ISD::Constant) {
1703 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001704 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001705 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001706 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001707 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001708 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001709 }
Chris Lattner350bec02006-04-02 06:11:11 +00001710
Chris Lattner06afe072006-05-05 22:53:17 +00001711 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
1712 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1713 // Shifting in all undef bits?
1714 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
1715 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
1716 return DAG.getNode(ISD::UNDEF, VT);
1717
1718 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
1719 AddToWorkList(SmallShift.Val);
1720 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
1721 }
1722
Chris Lattner3657ffe2006-10-12 20:23:19 +00001723 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
1724 // bit, which is unmodified by sra.
1725 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
1726 if (N0.getOpcode() == ISD::SRA)
1727 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
1728 }
1729
Chris Lattner350bec02006-04-02 06:11:11 +00001730 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1731 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1732 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1733 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1734 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
1735
1736 // If any of the input bits are KnownOne, then the input couldn't be all
1737 // zeros, thus the result of the srl will always be zero.
1738 if (KnownOne) return DAG.getConstant(0, VT);
1739
1740 // If all of the bits input the to ctlz node are known to be zero, then
1741 // the result of the ctlz is "32" and the result of the shift is one.
1742 uint64_t UnknownBits = ~KnownZero & Mask;
1743 if (UnknownBits == 0) return DAG.getConstant(1, VT);
1744
1745 // Otherwise, check to see if there is exactly one bit input to the ctlz.
1746 if ((UnknownBits & (UnknownBits-1)) == 0) {
1747 // Okay, we know that only that the single bit specified by UnknownBits
1748 // could be set on input to the CTLZ node. If this bit is set, the SRL
1749 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
1750 // to an SRL,XOR pair, which is likely to simplify more.
1751 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
1752 SDOperand Op = N0.getOperand(0);
1753 if (ShAmt) {
1754 Op = DAG.getNode(ISD::SRL, VT, Op,
1755 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
1756 AddToWorkList(Op.Val);
1757 }
1758 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
1759 }
1760 }
1761
Nate Begeman83e75ec2005-09-06 04:43:02 +00001762 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001763}
1764
Nate Begeman83e75ec2005-09-06 04:43:02 +00001765SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001766 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001767 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001768
1769 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001770 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001771 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001772 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001773}
1774
Nate Begeman83e75ec2005-09-06 04:43:02 +00001775SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001776 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001777 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001778
1779 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001780 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001781 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001782 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001783}
1784
Nate Begeman83e75ec2005-09-06 04:43:02 +00001785SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001786 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001787 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001788
1789 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001790 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001791 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001792 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001793}
1794
Nate Begeman452d7be2005-09-16 00:54:12 +00001795SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1796 SDOperand N0 = N->getOperand(0);
1797 SDOperand N1 = N->getOperand(1);
1798 SDOperand N2 = N->getOperand(2);
1799 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1800 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1801 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1802 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001803
Nate Begeman452d7be2005-09-16 00:54:12 +00001804 // fold select C, X, X -> X
1805 if (N1 == N2)
1806 return N1;
1807 // fold select true, X, Y -> X
1808 if (N0C && !N0C->isNullValue())
1809 return N1;
1810 // fold select false, X, Y -> Y
1811 if (N0C && N0C->isNullValue())
1812 return N2;
1813 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001814 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001815 return DAG.getNode(ISD::OR, VT, N0, N2);
1816 // fold select C, 0, X -> ~C & X
1817 // FIXME: this should check for C type == X type, not i1?
1818 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1819 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001820 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001821 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1822 }
1823 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001824 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001825 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001826 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001827 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1828 }
1829 // fold select C, X, 0 -> C & X
1830 // FIXME: this should check for C type == X type, not i1?
1831 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1832 return DAG.getNode(ISD::AND, VT, N0, N1);
1833 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1834 if (MVT::i1 == VT && N0 == N1)
1835 return DAG.getNode(ISD::OR, VT, N0, N2);
1836 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1837 if (MVT::i1 == VT && N0 == N2)
1838 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00001839
Chris Lattner40c62d52005-10-18 06:04:22 +00001840 // If we can fold this based on the true/false value, do so.
1841 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00001842 return SDOperand(N, 0); // Don't revisit N.
1843
Nate Begeman44728a72005-09-19 22:34:01 +00001844 // fold selects based on a setcc into other things, such as min/max/abs
1845 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001846 // FIXME:
1847 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1848 // having to say they don't support SELECT_CC on every type the DAG knows
1849 // about, since there is no way to mark an opcode illegal at all value types
1850 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1851 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1852 N1, N2, N0.getOperand(2));
1853 else
1854 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001855 return SDOperand();
1856}
1857
1858SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001859 SDOperand N0 = N->getOperand(0);
1860 SDOperand N1 = N->getOperand(1);
1861 SDOperand N2 = N->getOperand(2);
1862 SDOperand N3 = N->getOperand(3);
1863 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00001864 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1865
Nate Begeman44728a72005-09-19 22:34:01 +00001866 // fold select_cc lhs, rhs, x, x, cc -> x
1867 if (N2 == N3)
1868 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001869
Chris Lattner5f42a242006-09-20 06:19:26 +00001870 // Determine if the condition we're dealing with is constant
1871 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00001872 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00001873
1874 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
1875 if (SCCC->getValue())
1876 return N2; // cond always true -> true val
1877 else
1878 return N3; // cond always false -> false val
1879 }
1880
1881 // Fold to a simpler select_cc
1882 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
1883 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
1884 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
1885 SCC.getOperand(2));
1886
Chris Lattner40c62d52005-10-18 06:04:22 +00001887 // If we can fold this based on the true/false value, do so.
1888 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00001889 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00001890
Nate Begeman44728a72005-09-19 22:34:01 +00001891 // fold select_cc into other things, such as min/max/abs
1892 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001893}
1894
1895SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1896 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1897 cast<CondCodeSDNode>(N->getOperand(2))->get());
1898}
1899
Nate Begeman83e75ec2005-09-06 04:43:02 +00001900SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001901 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001902 MVT::ValueType VT = N->getValueType(0);
1903
Nate Begeman1d4d4142005-09-01 00:19:25 +00001904 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00001905 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001906 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00001907
Nate Begeman1d4d4142005-09-01 00:19:25 +00001908 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001909 // fold (sext (aext x)) -> (sext x)
1910 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001911 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00001912
Chris Lattner6007b842006-09-21 06:00:20 +00001913 // fold (sext (truncate x)) -> (sextinreg x).
1914 if (N0.getOpcode() == ISD::TRUNCATE &&
Chris Lattnerbf370872006-09-21 06:17:39 +00001915 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
1916 N0.getValueType()))) {
Chris Lattner6007b842006-09-21 06:00:20 +00001917 SDOperand Op = N0.getOperand(0);
1918 if (Op.getValueType() < VT) {
1919 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
1920 } else if (Op.getValueType() > VT) {
1921 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
1922 }
1923 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001924 DAG.getValueType(N0.getValueType()));
Chris Lattner6007b842006-09-21 06:00:20 +00001925 }
Chris Lattner310b5782006-05-06 23:06:26 +00001926
Evan Cheng110dec22005-12-14 02:19:23 +00001927 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00001928 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00001929 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng466685d2006-10-09 20:57:25 +00001930 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1931 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
1932 LN0->getBasePtr(), LN0->getSrcValue(),
1933 LN0->getSrcValueOffset(),
Nate Begeman3df4d522005-10-12 20:40:40 +00001934 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001935 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001936 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1937 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001938 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00001939 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001940
1941 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1942 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Chengc5484282006-10-04 00:56:09 +00001943 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001944 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001945 MVT::ValueType EVT = LN0->getLoadedVT();
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00001946 if (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
1947 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
1948 LN0->getBasePtr(), LN0->getSrcValue(),
1949 LN0->getSrcValueOffset(), EVT);
1950 CombineTo(N, ExtLoad);
1951 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1952 ExtLoad.getValue(1));
1953 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1954 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001955 }
1956
Nate Begeman83e75ec2005-09-06 04:43:02 +00001957 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001958}
1959
Nate Begeman83e75ec2005-09-06 04:43:02 +00001960SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001961 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001962 MVT::ValueType VT = N->getValueType(0);
1963
Nate Begeman1d4d4142005-09-01 00:19:25 +00001964 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00001965 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001966 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001967 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001968 // fold (zext (aext x)) -> (zext x)
1969 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001970 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00001971
1972 // fold (zext (truncate x)) -> (and x, mask)
1973 if (N0.getOpcode() == ISD::TRUNCATE &&
1974 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
1975 SDOperand Op = N0.getOperand(0);
1976 if (Op.getValueType() < VT) {
1977 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
1978 } else if (Op.getValueType() > VT) {
1979 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
1980 }
1981 return DAG.getZeroExtendInReg(Op, N0.getValueType());
1982 }
1983
Chris Lattner111c2282006-09-21 06:14:31 +00001984 // fold (zext (and (trunc x), cst)) -> (and x, cst).
1985 if (N0.getOpcode() == ISD::AND &&
1986 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1987 N0.getOperand(1).getOpcode() == ISD::Constant) {
1988 SDOperand X = N0.getOperand(0).getOperand(0);
1989 if (X.getValueType() < VT) {
1990 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
1991 } else if (X.getValueType() > VT) {
1992 X = DAG.getNode(ISD::TRUNCATE, VT, X);
1993 }
1994 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1995 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
1996 }
1997
Evan Cheng110dec22005-12-14 02:19:23 +00001998 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00001999 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002000 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002001 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2002 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2003 LN0->getBasePtr(), LN0->getSrcValue(),
2004 LN0->getSrcValueOffset(),
Evan Cheng110dec22005-12-14 02:19:23 +00002005 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00002006 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00002007 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2008 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002009 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00002010 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002011
2012 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2013 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Chengc5484282006-10-04 00:56:09 +00002014 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002015 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002016 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002017 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2018 LN0->getBasePtr(), LN0->getSrcValue(),
2019 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002020 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002021 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2022 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002023 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002024 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002025 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002026}
2027
Chris Lattner5ffc0662006-05-05 05:58:59 +00002028SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2029 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002030 MVT::ValueType VT = N->getValueType(0);
2031
2032 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002033 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002034 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2035 // fold (aext (aext x)) -> (aext x)
2036 // fold (aext (zext x)) -> (zext x)
2037 // fold (aext (sext x)) -> (sext x)
2038 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2039 N0.getOpcode() == ISD::ZERO_EXTEND ||
2040 N0.getOpcode() == ISD::SIGN_EXTEND)
2041 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2042
Chris Lattner84750582006-09-20 06:29:17 +00002043 // fold (aext (truncate x))
2044 if (N0.getOpcode() == ISD::TRUNCATE) {
2045 SDOperand TruncOp = N0.getOperand(0);
2046 if (TruncOp.getValueType() == VT)
2047 return TruncOp; // x iff x size == zext size.
2048 if (TruncOp.getValueType() > VT)
2049 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2050 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2051 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002052
2053 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2054 if (N0.getOpcode() == ISD::AND &&
2055 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2056 N0.getOperand(1).getOpcode() == ISD::Constant) {
2057 SDOperand X = N0.getOperand(0).getOperand(0);
2058 if (X.getValueType() < VT) {
2059 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2060 } else if (X.getValueType() > VT) {
2061 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2062 }
2063 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2064 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2065 }
2066
Chris Lattner5ffc0662006-05-05 05:58:59 +00002067 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002068 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002069 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002070 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2071 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2072 LN0->getBasePtr(), LN0->getSrcValue(),
2073 LN0->getSrcValueOffset(),
Chris Lattner5ffc0662006-05-05 05:58:59 +00002074 N0.getValueType());
2075 CombineTo(N, ExtLoad);
2076 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2077 ExtLoad.getValue(1));
2078 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2079 }
2080
2081 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2082 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2083 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002084 if (N0.getOpcode() == ISD::LOAD && !ISD::isNON_EXTLoad(N0.Val) &&
2085 N0.hasOneUse()) {
2086 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002087 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002088 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
2089 LN0->getChain(), LN0->getBasePtr(),
2090 LN0->getSrcValue(),
2091 LN0->getSrcValueOffset(), EVT);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002092 CombineTo(N, ExtLoad);
2093 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2094 ExtLoad.getValue(1));
2095 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2096 }
2097 return SDOperand();
2098}
2099
2100
Nate Begeman83e75ec2005-09-06 04:43:02 +00002101SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002102 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002103 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002104 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002105 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00002106 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002107
Nate Begeman1d4d4142005-09-01 00:19:25 +00002108 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00002109 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00002110 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00002111
Chris Lattner541a24f2006-05-06 22:43:44 +00002112 // If the input is already sign extended, just drop the extension.
Chris Lattneree4ea922006-05-06 09:30:03 +00002113 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
2114 return N0;
2115
Nate Begeman646d7e22005-09-02 21:18:40 +00002116 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
2117 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2118 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00002119 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002120 }
Chris Lattner4b37e872006-05-08 21:18:59 +00002121
Nate Begeman07ed4172005-10-10 21:26:48 +00002122 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00002123 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00002124 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00002125
2126 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
2127 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
2128 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
2129 if (N0.getOpcode() == ISD::SRL) {
2130 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2131 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
2132 // We can turn this into an SRA iff the input to the SRL is already sign
2133 // extended enough.
2134 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
2135 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
2136 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
2137 }
2138 }
2139
Nate Begemanded49632005-10-13 03:11:28 +00002140 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00002141 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002142 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002143 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002144 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2145 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2146 LN0->getBasePtr(), LN0->getSrcValue(),
2147 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002148 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002149 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002150 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002151 }
2152 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Chengc5484282006-10-04 00:56:09 +00002153 if (ISD::isZEXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002154 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002155 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002156 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2157 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2158 LN0->getBasePtr(), LN0->getSrcValue(),
2159 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002160 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002161 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002162 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002163 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002164 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002165}
2166
Nate Begeman83e75ec2005-09-06 04:43:02 +00002167SDOperand DAGCombiner::visitTRUNCATE(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
2171 // noop truncate
2172 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002173 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002174 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002175 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002176 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002177 // fold (truncate (truncate x)) -> (truncate x)
2178 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002179 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002180 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00002181 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2182 N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner32ba1aa2006-11-20 18:05:46 +00002183 if (N0.getOperand(0).getValueType() < VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002184 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00002185 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Chris Lattner32ba1aa2006-11-20 18:05:46 +00002186 else if (N0.getOperand(0).getValueType() > VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002187 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002188 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002189 else
2190 // if the source and dest are the same type, we can drop both the extend
2191 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002192 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002193 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002194 // fold (truncate (load x)) -> (smaller load x)
Chris Lattnerbc4cf8d2006-11-27 04:40:53 +00002195 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
2196 // Do not allow folding to i1 here. i1 is implicitly stored in memory in
2197 // zero extended form: by shrinking the load, we lose track of the fact
2198 // that it is already zero extended.
2199 // FIXME: This should be reevaluated.
2200 VT != MVT::i1) {
Nate Begeman3df4d522005-10-12 20:40:40 +00002201 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
2202 "Cannot truncate to larger type!");
Evan Cheng466685d2006-10-09 20:57:25 +00002203 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Nate Begeman3df4d522005-10-12 20:40:40 +00002204 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00002205 // For big endian targets, we need to add an offset to the pointer to load
2206 // the correct bytes. For little endian systems, we merely need to read
2207 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00002208 uint64_t PtrOff =
2209 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Evan Cheng466685d2006-10-09 20:57:25 +00002210 SDOperand NewPtr = TLI.isLittleEndian() ? LN0->getBasePtr() :
2211 DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Nate Begeman765784a2005-10-12 23:18:53 +00002212 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00002213 AddToWorkList(NewPtr.Val);
Evan Cheng466685d2006-10-09 20:57:25 +00002214 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), NewPtr,
2215 LN0->getSrcValue(), LN0->getSrcValueOffset());
Chris Lattner5750df92006-03-01 04:03:14 +00002216 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00002217 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002218 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002219 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002220 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002221}
2222
Chris Lattner94683772005-12-23 05:30:37 +00002223SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2224 SDOperand N0 = N->getOperand(0);
2225 MVT::ValueType VT = N->getValueType(0);
2226
2227 // If the input is a constant, let getNode() fold it.
2228 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2229 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2230 if (Res.Val != N) return Res;
2231 }
2232
Chris Lattnerc8547d82005-12-23 05:37:50 +00002233 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2234 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00002235
Chris Lattner57104102005-12-23 05:44:41 +00002236 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002237 // FIXME: These xforms need to know that the resultant load doesn't need a
2238 // higher alignment than the original!
Evan Cheng466685d2006-10-09 20:57:25 +00002239 if (0 && ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse()) {
2240 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2241 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
2242 LN0->getSrcValue(), LN0->getSrcValueOffset());
Chris Lattner5750df92006-03-01 04:03:14 +00002243 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00002244 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2245 Load.getValue(1));
2246 return Load;
2247 }
2248
Chris Lattner94683772005-12-23 05:30:37 +00002249 return SDOperand();
2250}
2251
Chris Lattner6258fb22006-04-02 02:53:43 +00002252SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2253 SDOperand N0 = N->getOperand(0);
2254 MVT::ValueType VT = N->getValueType(0);
2255
2256 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2257 // First check to see if this is all constant.
2258 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2259 VT == MVT::Vector) {
2260 bool isSimple = true;
2261 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2262 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2263 N0.getOperand(i).getOpcode() != ISD::Constant &&
2264 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2265 isSimple = false;
2266 break;
2267 }
2268
Chris Lattner97c20732006-04-03 17:29:28 +00002269 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2270 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002271 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2272 }
2273 }
2274
2275 return SDOperand();
2276}
2277
2278/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2279/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2280/// destination element value type.
2281SDOperand DAGCombiner::
2282ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2283 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2284
2285 // If this is already the right type, we're done.
2286 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2287
2288 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2289 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2290
2291 // If this is a conversion of N elements of one type to N elements of another
2292 // type, convert each element. This handles FP<->INT cases.
2293 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002294 SmallVector<SDOperand, 8> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00002295 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002296 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002297 AddToWorkList(Ops.back().Val);
2298 }
Chris Lattner6258fb22006-04-02 02:53:43 +00002299 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2300 Ops.push_back(DAG.getValueType(DstEltVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002301 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002302 }
2303
2304 // Otherwise, we're growing or shrinking the elements. To avoid having to
2305 // handle annoying details of growing/shrinking FP values, we convert them to
2306 // int first.
2307 if (MVT::isFloatingPoint(SrcEltVT)) {
2308 // Convert the input float vector to a int vector where the elements are the
2309 // same sizes.
2310 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2311 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2312 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2313 SrcEltVT = IntVT;
2314 }
2315
2316 // Now we know the input is an integer vector. If the output is a FP type,
2317 // convert to integer first, then to FP of the right size.
2318 if (MVT::isFloatingPoint(DstEltVT)) {
2319 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2320 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2321 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2322
2323 // Next, convert to FP elements of the same size.
2324 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2325 }
2326
2327 // Okay, we know the src/dst types are both integers of differing types.
2328 // Handling growing first.
2329 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2330 if (SrcBitSize < DstBitSize) {
2331 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2332
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002333 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002334 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2335 i += NumInputsPerOutput) {
2336 bool isLE = TLI.isLittleEndian();
2337 uint64_t NewBits = 0;
2338 bool EltIsUndef = true;
2339 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2340 // Shift the previously computed bits over.
2341 NewBits <<= SrcBitSize;
2342 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2343 if (Op.getOpcode() == ISD::UNDEF) continue;
2344 EltIsUndef = false;
2345
2346 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2347 }
2348
2349 if (EltIsUndef)
2350 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2351 else
2352 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2353 }
2354
2355 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2356 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002357 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002358 }
2359
2360 // Finally, this must be the case where we are shrinking elements: each input
2361 // turns into multiple outputs.
2362 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002363 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002364 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2365 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2366 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2367 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2368 continue;
2369 }
2370 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2371
2372 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2373 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2374 OpVal >>= DstBitSize;
2375 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2376 }
2377
2378 // For big endian targets, swap the order of the pieces of each element.
2379 if (!TLI.isLittleEndian())
2380 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2381 }
2382 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2383 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002384 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002385}
2386
2387
2388
Chris Lattner01b3d732005-09-28 22:28:18 +00002389SDOperand DAGCombiner::visitFADD(SDNode *N) {
2390 SDOperand N0 = N->getOperand(0);
2391 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002392 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2393 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002394 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002395
2396 // fold (fadd c1, c2) -> c1+c2
2397 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002398 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002399 // canonicalize constant to RHS
2400 if (N0CFP && !N1CFP)
2401 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002402 // fold (A + (-B)) -> A-B
2403 if (N1.getOpcode() == ISD::FNEG)
2404 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002405 // fold ((-A) + B) -> B-A
2406 if (N0.getOpcode() == ISD::FNEG)
2407 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002408 return SDOperand();
2409}
2410
2411SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2412 SDOperand N0 = N->getOperand(0);
2413 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002414 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2415 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002416 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002417
2418 // fold (fsub c1, c2) -> c1-c2
2419 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002420 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002421 // fold (A-(-B)) -> A+B
2422 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00002423 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002424 return SDOperand();
2425}
2426
2427SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2428 SDOperand N0 = N->getOperand(0);
2429 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002430 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2431 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002432 MVT::ValueType VT = N->getValueType(0);
2433
Nate Begeman11af4ea2005-10-17 20:40:11 +00002434 // fold (fmul c1, c2) -> c1*c2
2435 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002436 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002437 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002438 if (N0CFP && !N1CFP)
2439 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002440 // fold (fmul X, 2.0) -> (fadd X, X)
2441 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2442 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002443 return SDOperand();
2444}
2445
2446SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2447 SDOperand N0 = N->getOperand(0);
2448 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002449 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2450 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002451 MVT::ValueType VT = N->getValueType(0);
2452
Nate Begemana148d982006-01-18 22:35:16 +00002453 // fold (fdiv c1, c2) -> c1/c2
2454 if (N0CFP && N1CFP)
2455 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002456 return SDOperand();
2457}
2458
2459SDOperand DAGCombiner::visitFREM(SDNode *N) {
2460 SDOperand N0 = N->getOperand(0);
2461 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002462 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2463 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002464 MVT::ValueType VT = N->getValueType(0);
2465
Nate Begemana148d982006-01-18 22:35:16 +00002466 // fold (frem c1, c2) -> fmod(c1,c2)
2467 if (N0CFP && N1CFP)
2468 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002469 return SDOperand();
2470}
2471
Chris Lattner12d83032006-03-05 05:30:57 +00002472SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2473 SDOperand N0 = N->getOperand(0);
2474 SDOperand N1 = N->getOperand(1);
2475 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2476 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2477 MVT::ValueType VT = N->getValueType(0);
2478
2479 if (N0CFP && N1CFP) // Constant fold
2480 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2481
2482 if (N1CFP) {
2483 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2484 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2485 union {
2486 double d;
2487 int64_t i;
2488 } u;
2489 u.d = N1CFP->getValue();
2490 if (u.i >= 0)
2491 return DAG.getNode(ISD::FABS, VT, N0);
2492 else
2493 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2494 }
2495
2496 // copysign(fabs(x), y) -> copysign(x, y)
2497 // copysign(fneg(x), y) -> copysign(x, y)
2498 // copysign(copysign(x,z), y) -> copysign(x, y)
2499 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2500 N0.getOpcode() == ISD::FCOPYSIGN)
2501 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2502
2503 // copysign(x, abs(y)) -> abs(x)
2504 if (N1.getOpcode() == ISD::FABS)
2505 return DAG.getNode(ISD::FABS, VT, N0);
2506
2507 // copysign(x, copysign(y,z)) -> copysign(x, z)
2508 if (N1.getOpcode() == ISD::FCOPYSIGN)
2509 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2510
2511 // copysign(x, fp_extend(y)) -> copysign(x, y)
2512 // copysign(x, fp_round(y)) -> copysign(x, y)
2513 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2514 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2515
2516 return SDOperand();
2517}
2518
2519
Chris Lattner01b3d732005-09-28 22:28:18 +00002520
Nate Begeman83e75ec2005-09-06 04:43:02 +00002521SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002522 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002523 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002524 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002525
2526 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002527 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002528 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002529 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002530}
2531
Nate Begeman83e75ec2005-09-06 04:43:02 +00002532SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002533 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002534 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002535 MVT::ValueType VT = N->getValueType(0);
2536
Nate Begeman1d4d4142005-09-01 00:19:25 +00002537 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002538 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002539 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002540 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002541}
2542
Nate Begeman83e75ec2005-09-06 04:43:02 +00002543SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002544 SDOperand N0 = N->getOperand(0);
2545 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2546 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002547
2548 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002549 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002550 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002551 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002552}
2553
Nate Begeman83e75ec2005-09-06 04:43:02 +00002554SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002555 SDOperand N0 = N->getOperand(0);
2556 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2557 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002558
2559 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002560 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002561 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002562 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002563}
2564
Nate Begeman83e75ec2005-09-06 04:43:02 +00002565SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002566 SDOperand N0 = N->getOperand(0);
2567 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2568 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002569
2570 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002571 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002572 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002573
2574 // fold (fp_round (fp_extend x)) -> x
2575 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2576 return N0.getOperand(0);
2577
2578 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2579 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2580 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2581 AddToWorkList(Tmp.Val);
2582 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2583 }
2584
Nate Begeman83e75ec2005-09-06 04:43:02 +00002585 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002586}
2587
Nate Begeman83e75ec2005-09-06 04:43:02 +00002588SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002589 SDOperand N0 = N->getOperand(0);
2590 MVT::ValueType VT = N->getValueType(0);
2591 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002592 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002593
Nate Begeman1d4d4142005-09-01 00:19:25 +00002594 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002595 if (N0CFP) {
2596 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002597 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002598 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002599 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002600}
2601
Nate Begeman83e75ec2005-09-06 04:43:02 +00002602SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002603 SDOperand N0 = N->getOperand(0);
2604 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2605 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002606
2607 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002608 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002609 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00002610
2611 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002612 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002613 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002614 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2615 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2616 LN0->getBasePtr(), LN0->getSrcValue(),
2617 LN0->getSrcValueOffset(),
Chris Lattnere564dbb2006-05-05 21:34:35 +00002618 N0.getValueType());
2619 CombineTo(N, ExtLoad);
2620 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
2621 ExtLoad.getValue(1));
2622 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2623 }
2624
2625
Nate Begeman83e75ec2005-09-06 04:43:02 +00002626 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002627}
2628
Nate Begeman83e75ec2005-09-06 04:43:02 +00002629SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002630 SDOperand N0 = N->getOperand(0);
2631 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2632 MVT::ValueType VT = N->getValueType(0);
2633
2634 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002635 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002636 return DAG.getNode(ISD::FNEG, VT, N0);
2637 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002638 if (N0.getOpcode() == ISD::SUB)
2639 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002640 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002641 if (N0.getOpcode() == ISD::FNEG)
2642 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002643 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002644}
2645
Nate Begeman83e75ec2005-09-06 04:43:02 +00002646SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002647 SDOperand N0 = N->getOperand(0);
2648 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2649 MVT::ValueType VT = N->getValueType(0);
2650
Nate Begeman1d4d4142005-09-01 00:19:25 +00002651 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002652 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002653 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002654 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002655 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002656 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002657 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002658 // fold (fabs (fcopysign x, y)) -> (fabs x)
2659 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2660 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2661
Nate Begeman83e75ec2005-09-06 04:43:02 +00002662 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002663}
2664
Nate Begeman44728a72005-09-19 22:34:01 +00002665SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2666 SDOperand Chain = N->getOperand(0);
2667 SDOperand N1 = N->getOperand(1);
2668 SDOperand N2 = N->getOperand(2);
2669 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2670
2671 // never taken branch, fold to chain
2672 if (N1C && N1C->isNullValue())
2673 return Chain;
2674 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002675 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002676 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002677 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2678 // on the target.
2679 if (N1.getOpcode() == ISD::SETCC &&
2680 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2681 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2682 N1.getOperand(0), N1.getOperand(1), N2);
2683 }
Nate Begeman44728a72005-09-19 22:34:01 +00002684 return SDOperand();
2685}
2686
Chris Lattner3ea0b472005-10-05 06:47:48 +00002687// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2688//
Nate Begeman44728a72005-09-19 22:34:01 +00002689SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002690 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2691 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2692
2693 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002694 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002695 if (Simp.Val) AddToWorkList(Simp.Val);
2696
Nate Begemane17daeb2005-10-05 21:43:42 +00002697 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2698
2699 // fold br_cc true, dest -> br dest (unconditional branch)
2700 if (SCCC && SCCC->getValue())
2701 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2702 N->getOperand(4));
2703 // fold br_cc false, dest -> unconditional fall through
2704 if (SCCC && SCCC->isNullValue())
2705 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00002706
Nate Begemane17daeb2005-10-05 21:43:42 +00002707 // fold to a simpler setcc
2708 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2709 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2710 Simp.getOperand(2), Simp.getOperand(0),
2711 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002712 return SDOperand();
2713}
2714
Chris Lattner448f2192006-11-11 00:39:41 +00002715
2716/// CombineToPreIndexedLoadStore - Try turning a load / store and a
2717/// pre-indexed load / store when the base pointer is a add or subtract
2718/// and it has other uses besides the load / store. After the
2719/// transformation, the new indexed load / store has effectively folded
2720/// the add / subtract in and all of its other uses are redirected to the
2721/// new load / store.
2722bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
2723 if (!AfterLegalize)
2724 return false;
2725
2726 bool isLoad = true;
2727 SDOperand Ptr;
2728 MVT::ValueType VT;
2729 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
2730 VT = LD->getLoadedVT();
2731 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
2732 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
2733 return false;
2734 Ptr = LD->getBasePtr();
2735 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
2736 VT = ST->getStoredVT();
2737 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
2738 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
2739 return false;
2740 Ptr = ST->getBasePtr();
2741 isLoad = false;
2742 } else
2743 return false;
2744
Chris Lattner9f1794e2006-11-11 00:56:29 +00002745 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
2746 // out. There is no reason to make this a preinc/predec.
2747 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
2748 Ptr.Val->hasOneUse())
2749 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00002750
Chris Lattner9f1794e2006-11-11 00:56:29 +00002751 // Ask the target to do addressing mode selection.
2752 SDOperand BasePtr;
2753 SDOperand Offset;
2754 ISD::MemIndexedMode AM = ISD::UNINDEXED;
2755 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
2756 return false;
2757
Chris Lattner41e53fd2006-11-11 01:00:15 +00002758 // Try turning it into a pre-indexed load / store except when:
2759 // 1) The base is a frame index.
2760 // 2) If N is a store and the ptr is either the same as or is a
Chris Lattner9f1794e2006-11-11 00:56:29 +00002761 // predecessor of the value being stored.
Chris Lattner41e53fd2006-11-11 01:00:15 +00002762 // 3) Another use of base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00002763 // that would create a cycle.
Chris Lattner41e53fd2006-11-11 01:00:15 +00002764 // 4) All uses are load / store ops that use it as base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00002765
Chris Lattner41e53fd2006-11-11 01:00:15 +00002766 // Check #1. Preinc'ing a frame index would require copying the stack pointer
2767 // (plus the implicit offset) to a register to preinc anyway.
2768 if (isa<FrameIndexSDNode>(BasePtr))
2769 return false;
2770
2771 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00002772 if (!isLoad) {
2773 SDOperand Val = cast<StoreSDNode>(N)->getValue();
2774 if (Val == Ptr || Ptr.Val->isPredecessor(Val.Val))
2775 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00002776 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00002777
2778 // Now check for #2 and #3.
2779 bool RealUse = false;
2780 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
2781 E = Ptr.Val->use_end(); I != E; ++I) {
2782 SDNode *Use = *I;
2783 if (Use == N)
2784 continue;
2785 if (Use->isPredecessor(N))
2786 return false;
2787
2788 if (!((Use->getOpcode() == ISD::LOAD &&
2789 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
2790 (Use->getOpcode() == ISD::STORE) &&
2791 cast<StoreSDNode>(Use)->getBasePtr() == Ptr))
2792 RealUse = true;
2793 }
2794 if (!RealUse)
2795 return false;
2796
2797 SDOperand Result;
2798 if (isLoad)
2799 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
2800 else
2801 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
2802 ++PreIndexedNodes;
2803 ++NodesCombined;
Bill Wendling832171c2006-12-07 20:04:42 +00002804 DOUT << "\nReplacing.4 "; DEBUG(N->dump());
2805 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
2806 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00002807 std::vector<SDNode*> NowDead;
2808 if (isLoad) {
2809 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
2810 NowDead);
2811 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
2812 NowDead);
2813 } else {
2814 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
2815 NowDead);
2816 }
2817
2818 // Nodes can end up on the worklist more than once. Make sure we do
2819 // not process a node that has been replaced.
2820 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
2821 removeFromWorkList(NowDead[i]);
2822 // Finally, since the node is now dead, remove it from the graph.
2823 DAG.DeleteNode(N);
2824
2825 // Replace the uses of Ptr with uses of the updated base value.
2826 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
2827 NowDead);
2828 removeFromWorkList(Ptr.Val);
2829 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
2830 removeFromWorkList(NowDead[i]);
2831 DAG.DeleteNode(Ptr.Val);
2832
2833 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00002834}
2835
2836/// CombineToPostIndexedLoadStore - Try combine a load / store with a
2837/// add / sub of the base pointer node into a post-indexed load / store.
2838/// The transformation folded the add / subtract into the new indexed
2839/// load / store effectively and all of its uses are redirected to the
2840/// new load / store.
2841bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
2842 if (!AfterLegalize)
2843 return false;
2844
2845 bool isLoad = true;
2846 SDOperand Ptr;
2847 MVT::ValueType VT;
2848 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
2849 VT = LD->getLoadedVT();
2850 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
2851 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
2852 return false;
2853 Ptr = LD->getBasePtr();
2854 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
2855 VT = ST->getStoredVT();
2856 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
2857 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
2858 return false;
2859 Ptr = ST->getBasePtr();
2860 isLoad = false;
2861 } else
2862 return false;
2863
Evan Chengcc470212006-11-16 00:08:20 +00002864 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00002865 return false;
2866
2867 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
2868 E = Ptr.Val->use_end(); I != E; ++I) {
2869 SDNode *Op = *I;
2870 if (Op == N ||
2871 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
2872 continue;
2873
2874 SDOperand BasePtr;
2875 SDOperand Offset;
2876 ISD::MemIndexedMode AM = ISD::UNINDEXED;
2877 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
2878 if (Ptr == Offset)
2879 std::swap(BasePtr, Offset);
2880 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00002881 continue;
2882
Chris Lattner9f1794e2006-11-11 00:56:29 +00002883 // Try turning it into a post-indexed load / store except when
2884 // 1) All uses are load / store ops that use it as base ptr.
2885 // 2) Op must be independent of N, i.e. Op is neither a predecessor
2886 // nor a successor of N. Otherwise, if Op is folded that would
2887 // create a cycle.
2888
2889 // Check for #1.
2890 bool TryNext = false;
2891 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
2892 EE = BasePtr.Val->use_end(); II != EE; ++II) {
2893 SDNode *Use = *II;
2894 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00002895 continue;
2896
Chris Lattner9f1794e2006-11-11 00:56:29 +00002897 // If all the uses are load / store addresses, then don't do the
2898 // transformation.
2899 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
2900 bool RealUse = false;
2901 for (SDNode::use_iterator III = Use->use_begin(),
2902 EEE = Use->use_end(); III != EEE; ++III) {
2903 SDNode *UseUse = *III;
2904 if (!((UseUse->getOpcode() == ISD::LOAD &&
2905 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
2906 (UseUse->getOpcode() == ISD::STORE) &&
2907 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use))
2908 RealUse = true;
2909 }
Chris Lattner448f2192006-11-11 00:39:41 +00002910
Chris Lattner9f1794e2006-11-11 00:56:29 +00002911 if (!RealUse) {
2912 TryNext = true;
2913 break;
Chris Lattner448f2192006-11-11 00:39:41 +00002914 }
2915 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00002916 }
2917 if (TryNext)
2918 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00002919
Chris Lattner9f1794e2006-11-11 00:56:29 +00002920 // Check for #2
2921 if (!Op->isPredecessor(N) && !N->isPredecessor(Op)) {
2922 SDOperand Result = isLoad
2923 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
2924 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
2925 ++PostIndexedNodes;
2926 ++NodesCombined;
Bill Wendling832171c2006-12-07 20:04:42 +00002927 DOUT << "\nReplacing.5 "; DEBUG(N->dump());
2928 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
2929 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00002930 std::vector<SDNode*> NowDead;
2931 if (isLoad) {
2932 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattner448f2192006-11-11 00:39:41 +00002933 NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00002934 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
2935 NowDead);
2936 } else {
2937 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
2938 NowDead);
Chris Lattner448f2192006-11-11 00:39:41 +00002939 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00002940
2941 // Nodes can end up on the worklist more than once. Make sure we do
2942 // not process a node that has been replaced.
2943 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
2944 removeFromWorkList(NowDead[i]);
2945 // Finally, since the node is now dead, remove it from the graph.
2946 DAG.DeleteNode(N);
2947
2948 // Replace the uses of Use with uses of the updated base value.
2949 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
2950 Result.getValue(isLoad ? 1 : 0),
2951 NowDead);
2952 removeFromWorkList(Op);
2953 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
2954 removeFromWorkList(NowDead[i]);
2955 DAG.DeleteNode(Op);
2956
2957 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00002958 }
2959 }
2960 }
2961 return false;
2962}
2963
2964
Chris Lattner01a22022005-10-10 22:04:48 +00002965SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00002966 LoadSDNode *LD = cast<LoadSDNode>(N);
2967 SDOperand Chain = LD->getChain();
2968 SDOperand Ptr = LD->getBasePtr();
Jim Laskey6ff23e52006-10-04 16:53:27 +00002969
Chris Lattnere4b95392006-03-31 18:06:18 +00002970 // If there are no uses of the loaded value, change uses of the chain value
2971 // into uses of the chain input (i.e. delete the dead load).
2972 if (N->hasNUsesOfValue(0, 0))
2973 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
Chris Lattner01a22022005-10-10 22:04:48 +00002974
2975 // If this load is directly stored, replace the load value with the stored
2976 // value.
2977 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002978 // TODO: Handle TRUNCSTORE/LOADEXT
2979 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002980 if (ISD::isNON_TRUNCStore(Chain.Val)) {
2981 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
2982 if (PrevST->getBasePtr() == Ptr &&
2983 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002984 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002985 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002986 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00002987
Jim Laskey7ca56af2006-10-11 13:47:09 +00002988 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00002989 // Walk up chain skipping non-aliasing memory nodes.
2990 SDOperand BetterChain = FindBetterChain(N, Chain);
2991
Jim Laskey6ff23e52006-10-04 16:53:27 +00002992 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00002993 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002994 SDOperand ReplLoad;
2995
Jim Laskey279f0532006-09-25 16:29:54 +00002996 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002997 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
2998 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
2999 LD->getSrcValue(), LD->getSrcValueOffset());
3000 } else {
3001 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
3002 LD->getValueType(0),
3003 BetterChain, Ptr, LD->getSrcValue(),
3004 LD->getSrcValueOffset(),
3005 LD->getLoadedVT());
3006 }
Jim Laskey279f0532006-09-25 16:29:54 +00003007
Jim Laskey6ff23e52006-10-04 16:53:27 +00003008 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00003009 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
3010 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00003011
Jim Laskey274062c2006-10-13 23:32:28 +00003012 // Replace uses with load result and token factor. Don't add users
3013 // to work list.
3014 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003015 }
3016 }
3017
Evan Cheng7fc033a2006-11-03 03:06:21 +00003018 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003019 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00003020 return SDOperand(N, 0);
3021
Chris Lattner01a22022005-10-10 22:04:48 +00003022 return SDOperand();
3023}
3024
Chris Lattner87514ca2005-10-10 22:31:19 +00003025SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003026 StoreSDNode *ST = cast<StoreSDNode>(N);
3027 SDOperand Chain = ST->getChain();
3028 SDOperand Value = ST->getValue();
3029 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00003030
Chris Lattnerc33baaa2005-12-23 05:48:07 +00003031 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00003032 // FIXME: This needs to know that the resultant store does not need a
3033 // higher alignment than the original.
Jim Laskey14fbcbf2006-09-25 21:11:32 +00003034 if (0 && Value.getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003035 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
3036 ST->getSrcValueOffset());
Jim Laskey279f0532006-09-25 16:29:54 +00003037 }
3038
Nate Begeman2cbba892006-12-11 02:23:46 +00003039 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00003040 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Evan Cheng25ece662006-12-11 17:25:19 +00003041 if (Value.getOpcode() != ISD::TargetConstantFP) {
3042 SDOperand Tmp;
Chris Lattner62be1a72006-12-12 04:16:14 +00003043 switch (CFP->getValueType(0)) {
3044 default: assert(0 && "Unknown FP type");
3045 case MVT::f32:
3046 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
3047 Tmp = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
3048 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
3049 ST->getSrcValueOffset());
3050 }
3051 break;
3052 case MVT::f64:
3053 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
3054 Tmp = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
3055 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
3056 ST->getSrcValueOffset());
3057 } else if (TLI.isTypeLegal(MVT::i32)) {
3058 // Many FP stores are not make apparent until after legalize, e.g. for
3059 // argument passing. Since this is so common, custom legalize the
3060 // 64-bit integer store into two 32-bit stores.
3061 uint64_t Val = DoubleToBits(CFP->getValue());
3062 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
3063 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
3064 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
3065
3066 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
3067 ST->getSrcValueOffset());
3068 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3069 DAG.getConstant(4, Ptr.getValueType()));
3070 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
3071 ST->getSrcValueOffset()+4);
3072 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
3073 }
3074 break;
Evan Cheng25ece662006-12-11 17:25:19 +00003075 }
Nate Begeman2cbba892006-12-11 02:23:46 +00003076 }
Nate Begeman2cbba892006-12-11 02:23:46 +00003077 }
3078
Jim Laskey279f0532006-09-25 16:29:54 +00003079 if (CombinerAA) {
3080 // Walk up chain skipping non-aliasing memory nodes.
3081 SDOperand BetterChain = FindBetterChain(N, Chain);
3082
Jim Laskey6ff23e52006-10-04 16:53:27 +00003083 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00003084 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00003085 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00003086 SDOperand ReplStore;
3087 if (ST->isTruncatingStore()) {
3088 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
3089 ST->getSrcValue(),ST->getSrcValueOffset(), ST->getStoredVT());
3090 } else {
3091 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
3092 ST->getSrcValue(), ST->getSrcValueOffset());
3093 }
3094
Jim Laskey279f0532006-09-25 16:29:54 +00003095 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00003096 SDOperand Token =
3097 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
3098
3099 // Don't add users to work list.
3100 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003101 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00003102 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00003103
Evan Cheng33dbedc2006-11-05 09:31:14 +00003104 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003105 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00003106 return SDOperand(N, 0);
3107
Chris Lattner87514ca2005-10-10 22:31:19 +00003108 return SDOperand();
3109}
3110
Chris Lattnerca242442006-03-19 01:27:56 +00003111SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
3112 SDOperand InVec = N->getOperand(0);
3113 SDOperand InVal = N->getOperand(1);
3114 SDOperand EltNo = N->getOperand(2);
3115
3116 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
3117 // vector with the inserted element.
3118 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
3119 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003120 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00003121 if (Elt < Ops.size())
3122 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003123 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
3124 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00003125 }
3126
3127 return SDOperand();
3128}
3129
3130SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
3131 SDOperand InVec = N->getOperand(0);
3132 SDOperand InVal = N->getOperand(1);
3133 SDOperand EltNo = N->getOperand(2);
3134 SDOperand NumElts = N->getOperand(3);
3135 SDOperand EltType = N->getOperand(4);
3136
3137 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
3138 // vector with the inserted element.
3139 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
3140 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003141 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00003142 if (Elt < Ops.size()-2)
3143 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003144 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
3145 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00003146 }
3147
3148 return SDOperand();
3149}
3150
Chris Lattnerd7648c82006-03-28 20:28:38 +00003151SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
3152 unsigned NumInScalars = N->getNumOperands()-2;
3153 SDOperand NumElts = N->getOperand(NumInScalars);
3154 SDOperand EltType = N->getOperand(NumInScalars+1);
3155
3156 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
3157 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
3158 // two distinct vectors, turn this into a shuffle node.
3159 SDOperand VecIn1, VecIn2;
3160 for (unsigned i = 0; i != NumInScalars; ++i) {
3161 // Ignore undef inputs.
3162 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3163
3164 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
3165 // constant index, bail out.
3166 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
3167 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
3168 VecIn1 = VecIn2 = SDOperand(0, 0);
3169 break;
3170 }
3171
3172 // If the input vector type disagrees with the result of the vbuild_vector,
3173 // we can't make a shuffle.
3174 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
3175 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
3176 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
3177 VecIn1 = VecIn2 = SDOperand(0, 0);
3178 break;
3179 }
3180
3181 // Otherwise, remember this. We allow up to two distinct input vectors.
3182 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
3183 continue;
3184
3185 if (VecIn1.Val == 0) {
3186 VecIn1 = ExtractedFromVec;
3187 } else if (VecIn2.Val == 0) {
3188 VecIn2 = ExtractedFromVec;
3189 } else {
3190 // Too many inputs.
3191 VecIn1 = VecIn2 = SDOperand(0, 0);
3192 break;
3193 }
3194 }
3195
3196 // If everything is good, we can make a shuffle operation.
3197 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003198 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00003199 for (unsigned i = 0; i != NumInScalars; ++i) {
3200 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
3201 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3202 continue;
3203 }
3204
3205 SDOperand Extract = N->getOperand(i);
3206
3207 // If extracting from the first vector, just use the index directly.
3208 if (Extract.getOperand(0) == VecIn1) {
3209 BuildVecIndices.push_back(Extract.getOperand(1));
3210 continue;
3211 }
3212
3213 // Otherwise, use InIdx + VecSize
3214 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
3215 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
3216 }
3217
3218 // Add count and size info.
3219 BuildVecIndices.push_back(NumElts);
3220 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
3221
3222 // Return the new VVECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003223 SDOperand Ops[5];
3224 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00003225 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003226 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00003227 } else {
3228 // Use an undef vbuild_vector as input for the second operand.
3229 std::vector<SDOperand> UnOps(NumInScalars,
3230 DAG.getNode(ISD::UNDEF,
3231 cast<VTSDNode>(EltType)->getVT()));
3232 UnOps.push_back(NumElts);
3233 UnOps.push_back(EltType);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003234 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3235 &UnOps[0], UnOps.size());
3236 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00003237 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003238 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3239 &BuildVecIndices[0], BuildVecIndices.size());
3240 Ops[3] = NumElts;
3241 Ops[4] = EltType;
3242 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
Chris Lattnerd7648c82006-03-28 20:28:38 +00003243 }
3244
3245 return SDOperand();
3246}
3247
Chris Lattner66445d32006-03-28 22:11:53 +00003248SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003249 SDOperand ShufMask = N->getOperand(2);
3250 unsigned NumElts = ShufMask.getNumOperands();
3251
3252 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3253 bool isIdentity = true;
3254 for (unsigned i = 0; i != NumElts; ++i) {
3255 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3256 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3257 isIdentity = false;
3258 break;
3259 }
3260 }
3261 if (isIdentity) return N->getOperand(0);
3262
3263 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3264 isIdentity = true;
3265 for (unsigned i = 0; i != NumElts; ++i) {
3266 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3267 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3268 isIdentity = false;
3269 break;
3270 }
3271 }
3272 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00003273
3274 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3275 // needed at all.
3276 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003277 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003278 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003279 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003280 for (unsigned i = 0; i != NumElts; ++i)
3281 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3282 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3283 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003284 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003285 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003286 BaseIdx = Idx;
3287 } else {
3288 if (BaseIdx != Idx)
3289 isSplat = false;
3290 if (VecNum != V) {
3291 isUnary = false;
3292 break;
3293 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003294 }
3295 }
3296
3297 SDOperand N0 = N->getOperand(0);
3298 SDOperand N1 = N->getOperand(1);
3299 // Normalize unary shuffle so the RHS is undef.
3300 if (isUnary && VecNum == 1)
3301 std::swap(N0, N1);
3302
Evan Cheng917ec982006-07-21 08:25:53 +00003303 // If it is a splat, check if the argument vector is a build_vector with
3304 // all scalar elements the same.
3305 if (isSplat) {
3306 SDNode *V = N0.Val;
3307 if (V->getOpcode() == ISD::BIT_CONVERT)
3308 V = V->getOperand(0).Val;
3309 if (V->getOpcode() == ISD::BUILD_VECTOR) {
3310 unsigned NumElems = V->getNumOperands()-2;
3311 if (NumElems > BaseIdx) {
3312 SDOperand Base;
3313 bool AllSame = true;
3314 for (unsigned i = 0; i != NumElems; ++i) {
3315 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3316 Base = V->getOperand(i);
3317 break;
3318 }
3319 }
3320 // Splat of <u, u, u, u>, return <u, u, u, u>
3321 if (!Base.Val)
3322 return N0;
3323 for (unsigned i = 0; i != NumElems; ++i) {
3324 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3325 V->getOperand(i) != Base) {
3326 AllSame = false;
3327 break;
3328 }
3329 }
3330 // Splat of <x, x, x, x>, return <x, x, x, x>
3331 if (AllSame)
3332 return N0;
3333 }
3334 }
3335 }
3336
Evan Chenge7bec0d2006-07-20 22:44:41 +00003337 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3338 // into an undef.
3339 if (isUnary || N0 == N1) {
3340 if (N0.getOpcode() == ISD::UNDEF)
Evan Chengc04766a2006-04-06 23:20:43 +00003341 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00003342 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3343 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003344 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00003345 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00003346 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3347 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3348 MappedOps.push_back(ShufMask.getOperand(i));
3349 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00003350 unsigned NewIdx =
3351 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3352 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00003353 }
3354 }
3355 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003356 &MappedOps[0], MappedOps.size());
Chris Lattner3e104b12006-04-08 04:15:24 +00003357 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00003358 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
Evan Chenge7bec0d2006-07-20 22:44:41 +00003359 N0,
Chris Lattner66445d32006-03-28 22:11:53 +00003360 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
3361 ShufMask);
3362 }
3363
3364 return SDOperand();
3365}
3366
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003367SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
3368 SDOperand ShufMask = N->getOperand(2);
3369 unsigned NumElts = ShufMask.getNumOperands()-2;
3370
3371 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3372 bool isIdentity = true;
3373 for (unsigned i = 0; i != NumElts; ++i) {
3374 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3375 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3376 isIdentity = false;
3377 break;
3378 }
3379 }
3380 if (isIdentity) return N->getOperand(0);
3381
3382 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3383 isIdentity = true;
3384 for (unsigned i = 0; i != NumElts; ++i) {
3385 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3386 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3387 isIdentity = false;
3388 break;
3389 }
3390 }
3391 if (isIdentity) return N->getOperand(1);
3392
Evan Chenge7bec0d2006-07-20 22:44:41 +00003393 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3394 // needed at all.
3395 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003396 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003397 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003398 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003399 for (unsigned i = 0; i != NumElts; ++i)
3400 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3401 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3402 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003403 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003404 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003405 BaseIdx = Idx;
3406 } else {
3407 if (BaseIdx != Idx)
3408 isSplat = false;
3409 if (VecNum != V) {
3410 isUnary = false;
3411 break;
3412 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003413 }
3414 }
3415
3416 SDOperand N0 = N->getOperand(0);
3417 SDOperand N1 = N->getOperand(1);
3418 // Normalize unary shuffle so the RHS is undef.
3419 if (isUnary && VecNum == 1)
3420 std::swap(N0, N1);
3421
Evan Cheng917ec982006-07-21 08:25:53 +00003422 // If it is a splat, check if the argument vector is a build_vector with
3423 // all scalar elements the same.
3424 if (isSplat) {
3425 SDNode *V = N0.Val;
Evan Cheng59569222006-10-16 22:49:37 +00003426
3427 // If this is a vbit convert that changes the element type of the vector but
3428 // not the number of vector elements, look through it. Be careful not to
3429 // look though conversions that change things like v4f32 to v2f64.
3430 if (V->getOpcode() == ISD::VBIT_CONVERT) {
3431 SDOperand ConvInput = V->getOperand(0);
Evan Cheng5d04a1a2006-10-17 17:06:35 +00003432 if (ConvInput.getValueType() == MVT::Vector &&
3433 NumElts ==
Evan Cheng59569222006-10-16 22:49:37 +00003434 ConvInput.getConstantOperandVal(ConvInput.getNumOperands()-2))
3435 V = ConvInput.Val;
3436 }
3437
Evan Cheng917ec982006-07-21 08:25:53 +00003438 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
3439 unsigned NumElems = V->getNumOperands()-2;
3440 if (NumElems > BaseIdx) {
3441 SDOperand Base;
3442 bool AllSame = true;
3443 for (unsigned i = 0; i != NumElems; ++i) {
3444 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3445 Base = V->getOperand(i);
3446 break;
3447 }
3448 }
3449 // Splat of <u, u, u, u>, return <u, u, u, u>
3450 if (!Base.Val)
3451 return N0;
3452 for (unsigned i = 0; i != NumElems; ++i) {
3453 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3454 V->getOperand(i) != Base) {
3455 AllSame = false;
3456 break;
3457 }
3458 }
3459 // Splat of <x, x, x, x>, return <x, x, x, x>
3460 if (AllSame)
3461 return N0;
3462 }
3463 }
3464 }
3465
Evan Chenge7bec0d2006-07-20 22:44:41 +00003466 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3467 // into an undef.
3468 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00003469 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3470 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003471 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00003472 for (unsigned i = 0; i != NumElts; ++i) {
3473 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3474 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3475 MappedOps.push_back(ShufMask.getOperand(i));
3476 } else {
3477 unsigned NewIdx =
3478 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3479 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
3480 }
3481 }
3482 // Add the type/#elts values.
3483 MappedOps.push_back(ShufMask.getOperand(NumElts));
3484 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
3485
3486 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003487 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003488 AddToWorkList(ShufMask.Val);
3489
3490 // Build the undef vector.
3491 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
3492 for (unsigned i = 0; i != NumElts; ++i)
3493 MappedOps[i] = UDVal;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003494 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
3495 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003496 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3497 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003498
3499 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
Evan Chenge7bec0d2006-07-20 22:44:41 +00003500 N0, UDVal, ShufMask,
Chris Lattner17614ea2006-04-08 05:34:25 +00003501 MappedOps[NumElts], MappedOps[NumElts+1]);
3502 }
3503
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003504 return SDOperand();
3505}
3506
Evan Cheng44f1f092006-04-20 08:56:16 +00003507/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
3508/// a VAND to a vector_shuffle with the destination vector and a zero vector.
3509/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
3510/// vector_shuffle V, Zero, <0, 4, 2, 4>
3511SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
3512 SDOperand LHS = N->getOperand(0);
3513 SDOperand RHS = N->getOperand(1);
3514 if (N->getOpcode() == ISD::VAND) {
3515 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
3516 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
3517 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
3518 RHS = RHS.getOperand(0);
3519 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
3520 std::vector<SDOperand> IdxOps;
3521 unsigned NumOps = RHS.getNumOperands();
3522 unsigned NumElts = NumOps-2;
3523 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
3524 for (unsigned i = 0; i != NumElts; ++i) {
3525 SDOperand Elt = RHS.getOperand(i);
3526 if (!isa<ConstantSDNode>(Elt))
3527 return SDOperand();
3528 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
3529 IdxOps.push_back(DAG.getConstant(i, EVT));
3530 else if (cast<ConstantSDNode>(Elt)->isNullValue())
3531 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
3532 else
3533 return SDOperand();
3534 }
3535
3536 // Let's see if the target supports this vector_shuffle.
3537 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
3538 return SDOperand();
3539
3540 // Return the new VVECTOR_SHUFFLE node.
3541 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
3542 SDOperand EVTNode = DAG.getValueType(EVT);
3543 std::vector<SDOperand> Ops;
Chris Lattner516b9622006-09-14 20:50:57 +00003544 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode,
3545 EVTNode);
Evan Cheng44f1f092006-04-20 08:56:16 +00003546 Ops.push_back(LHS);
3547 AddToWorkList(LHS.Val);
3548 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
3549 ZeroOps.push_back(NumEltsNode);
3550 ZeroOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003551 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3552 &ZeroOps[0], ZeroOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003553 IdxOps.push_back(NumEltsNode);
3554 IdxOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003555 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3556 &IdxOps[0], IdxOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003557 Ops.push_back(NumEltsNode);
3558 Ops.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003559 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
3560 &Ops[0], Ops.size());
Evan Cheng44f1f092006-04-20 08:56:16 +00003561 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
3562 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
3563 DstVecSize, DstVecEVT);
3564 }
3565 return Result;
3566 }
3567 }
3568 return SDOperand();
3569}
3570
Chris Lattneredab1b92006-04-02 03:25:57 +00003571/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
3572/// the scalar operation of the vop if it is operating on an integer vector
3573/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
3574SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
3575 ISD::NodeType FPOp) {
3576 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
3577 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
3578 SDOperand LHS = N->getOperand(0);
3579 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00003580 SDOperand Shuffle = XformToShuffleWithZero(N);
3581 if (Shuffle.Val) return Shuffle;
3582
Chris Lattneredab1b92006-04-02 03:25:57 +00003583 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
3584 // this operation.
3585 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
3586 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003587 SmallVector<SDOperand, 8> Ops;
Chris Lattneredab1b92006-04-02 03:25:57 +00003588 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
3589 SDOperand LHSOp = LHS.getOperand(i);
3590 SDOperand RHSOp = RHS.getOperand(i);
3591 // If these two elements can't be folded, bail out.
3592 if ((LHSOp.getOpcode() != ISD::UNDEF &&
3593 LHSOp.getOpcode() != ISD::Constant &&
3594 LHSOp.getOpcode() != ISD::ConstantFP) ||
3595 (RHSOp.getOpcode() != ISD::UNDEF &&
3596 RHSOp.getOpcode() != ISD::Constant &&
3597 RHSOp.getOpcode() != ISD::ConstantFP))
3598 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00003599 // Can't fold divide by zero.
3600 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
3601 if ((RHSOp.getOpcode() == ISD::Constant &&
3602 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
3603 (RHSOp.getOpcode() == ISD::ConstantFP &&
3604 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
3605 break;
3606 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003607 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00003608 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00003609 assert((Ops.back().getOpcode() == ISD::UNDEF ||
3610 Ops.back().getOpcode() == ISD::Constant ||
3611 Ops.back().getOpcode() == ISD::ConstantFP) &&
3612 "Scalar binop didn't fold!");
3613 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003614
3615 if (Ops.size() == LHS.getNumOperands()-2) {
3616 Ops.push_back(*(LHS.Val->op_end()-2));
3617 Ops.push_back(*(LHS.Val->op_end()-1));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003618 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003619 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003620 }
3621
3622 return SDOperand();
3623}
3624
Nate Begeman44728a72005-09-19 22:34:01 +00003625SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00003626 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
3627
3628 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
3629 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3630 // If we got a simplified select_cc node back from SimplifySelectCC, then
3631 // break it down into a new SETCC node, and a new SELECT node, and then return
3632 // the SELECT node, since we were called with a SELECT node.
3633 if (SCC.Val) {
3634 // Check to see if we got a select_cc back (to turn into setcc/select).
3635 // Otherwise, just return whatever node we got back, like fabs.
3636 if (SCC.getOpcode() == ISD::SELECT_CC) {
3637 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
3638 SCC.getOperand(0), SCC.getOperand(1),
3639 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00003640 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003641 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
3642 SCC.getOperand(3), SETCC);
3643 }
3644 return SCC;
3645 }
Nate Begeman44728a72005-09-19 22:34:01 +00003646 return SDOperand();
3647}
3648
Chris Lattner40c62d52005-10-18 06:04:22 +00003649/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
3650/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00003651/// select. Callers of this should assume that TheSelect is deleted if this
3652/// returns true. As such, they should return the appropriate thing (e.g. the
3653/// node) back to the top-level of the DAG combiner loop to avoid it being
3654/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00003655///
3656bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
3657 SDOperand RHS) {
3658
3659 // If this is a select from two identical things, try to pull the operation
3660 // through the select.
3661 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00003662 // If this is a load and the token chain is identical, replace the select
3663 // of two loads with a load through a select of the address to load from.
3664 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
3665 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00003666 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00003667 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00003668 LHS.getOperand(0) == RHS.getOperand(0)) {
3669 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
3670 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
3671
3672 // If this is an EXTLOAD, the VT's must match.
Evan Cheng2e49f092006-10-11 07:10:22 +00003673 if (LLD->getLoadedVT() == RLD->getLoadedVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003674 // FIXME: this conflates two src values, discarding one. This is not
3675 // the right thing to do, but nothing uses srcvalues now. When they do,
3676 // turn SrcValue into a list of locations.
3677 SDOperand Addr;
3678 if (TheSelect->getOpcode() == ISD::SELECT)
3679 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
3680 TheSelect->getOperand(0), LLD->getBasePtr(),
3681 RLD->getBasePtr());
3682 else
3683 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
3684 TheSelect->getOperand(0),
3685 TheSelect->getOperand(1),
3686 LLD->getBasePtr(), RLD->getBasePtr(),
3687 TheSelect->getOperand(4));
Chris Lattner40c62d52005-10-18 06:04:22 +00003688
Evan Cheng466685d2006-10-09 20:57:25 +00003689 SDOperand Load;
3690 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
3691 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
3692 Addr,LLD->getSrcValue(), LLD->getSrcValueOffset());
3693 else {
3694 Load = DAG.getExtLoad(LLD->getExtensionType(),
3695 TheSelect->getValueType(0),
3696 LLD->getChain(), Addr, LLD->getSrcValue(),
3697 LLD->getSrcValueOffset(),
Evan Cheng2e49f092006-10-11 07:10:22 +00003698 LLD->getLoadedVT());
Evan Cheng466685d2006-10-09 20:57:25 +00003699 }
3700 // Users of the select now use the result of the load.
3701 CombineTo(TheSelect, Load);
3702
3703 // Users of the old loads now use the new load's chain. We know the
3704 // old-load value is dead now.
3705 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
3706 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
3707 return true;
Evan Chengc5484282006-10-04 00:56:09 +00003708 }
Chris Lattner40c62d52005-10-18 06:04:22 +00003709 }
3710 }
3711
3712 return false;
3713}
3714
Nate Begeman44728a72005-09-19 22:34:01 +00003715SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
3716 SDOperand N2, SDOperand N3,
3717 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00003718
3719 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00003720 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
3721 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
3722 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
3723
3724 // Determine if the condition we're dealing with is constant
3725 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003726 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003727 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
3728
3729 // fold select_cc true, x, y -> x
3730 if (SCCC && SCCC->getValue())
3731 return N2;
3732 // fold select_cc false, x, y -> y
3733 if (SCCC && SCCC->getValue() == 0)
3734 return N3;
3735
3736 // Check to see if we can simplify the select into an fabs node
3737 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
3738 // Allow either -0.0 or 0.0
3739 if (CFP->getValue() == 0.0) {
3740 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
3741 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
3742 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
3743 N2 == N3.getOperand(0))
3744 return DAG.getNode(ISD::FABS, VT, N0);
3745
3746 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
3747 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3748 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3749 N2.getOperand(0) == N3)
3750 return DAG.getNode(ISD::FABS, VT, N3);
3751 }
3752 }
3753
3754 // Check to see if we can perform the "gzip trick", transforming
3755 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00003756 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00003757 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00003758 MVT::isInteger(N2.getValueType()) &&
3759 (N1C->isNullValue() || // (a < 0) ? b : 0
3760 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00003761 MVT::ValueType XType = N0.getValueType();
3762 MVT::ValueType AType = N2.getValueType();
3763 if (XType >= AType) {
3764 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00003765 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00003766 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3767 unsigned ShCtV = Log2_64(N2C->getValue());
3768 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3769 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3770 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00003771 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003772 if (XType > AType) {
3773 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003774 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003775 }
3776 return DAG.getNode(ISD::AND, AType, Shift, N2);
3777 }
3778 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3779 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3780 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003781 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003782 if (XType > AType) {
3783 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003784 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003785 }
3786 return DAG.getNode(ISD::AND, AType, Shift, N2);
3787 }
3788 }
Nate Begeman07ed4172005-10-10 21:26:48 +00003789
3790 // fold select C, 16, 0 -> shl C, 4
3791 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3792 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3793 // Get a SetCC of the condition
3794 // FIXME: Should probably make sure that setcc is legal if we ever have a
3795 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00003796 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00003797 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00003798 if (AfterLegalize) {
3799 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Chris Lattner555d8d62006-12-07 22:36:47 +00003800 if (N2.getValueType() < SCC.getValueType())
3801 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
3802 else
3803 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00003804 } else {
3805 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003806 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00003807 }
Chris Lattner5750df92006-03-01 04:03:14 +00003808 AddToWorkList(SCC.Val);
3809 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00003810 // shl setcc result by log2 n2c
3811 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3812 DAG.getConstant(Log2_64(N2C->getValue()),
3813 TLI.getShiftAmountTy()));
3814 }
3815
Nate Begemanf845b452005-10-08 00:29:44 +00003816 // Check to see if this is the equivalent of setcc
3817 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3818 // otherwise, go ahead with the folds.
3819 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3820 MVT::ValueType XType = N0.getValueType();
3821 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3822 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3823 if (Res.getValueType() != VT)
3824 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3825 return Res;
3826 }
3827
3828 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3829 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3830 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3831 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3832 return DAG.getNode(ISD::SRL, XType, Ctlz,
3833 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3834 TLI.getShiftAmountTy()));
3835 }
3836 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3837 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3838 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3839 N0);
3840 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3841 DAG.getConstant(~0ULL, XType));
3842 return DAG.getNode(ISD::SRL, XType,
3843 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3844 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3845 TLI.getShiftAmountTy()));
3846 }
3847 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3848 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3849 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3850 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3851 TLI.getShiftAmountTy()));
3852 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3853 }
3854 }
3855
3856 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3857 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3858 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3859 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3860 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3861 MVT::ValueType XType = N0.getValueType();
3862 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3863 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3864 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3865 TLI.getShiftAmountTy()));
3866 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003867 AddToWorkList(Shift.Val);
3868 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003869 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3870 }
3871 }
3872 }
3873
Nate Begeman44728a72005-09-19 22:34:01 +00003874 return SDOperand();
3875}
3876
Nate Begeman452d7be2005-09-16 00:54:12 +00003877SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00003878 SDOperand N1, ISD::CondCode Cond,
3879 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003880 // These setcc operations always fold.
3881 switch (Cond) {
3882 default: break;
3883 case ISD::SETFALSE:
3884 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3885 case ISD::SETTRUE:
3886 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3887 }
3888
3889 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3890 uint64_t C1 = N1C->getValue();
Reid Spencer3ed469c2006-11-02 20:25:50 +00003891 if (isa<ConstantSDNode>(N0.Val)) {
Chris Lattner51dabfb2006-10-14 00:41:01 +00003892 return DAG.FoldSetCC(VT, N0, N1, Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00003893 } else {
Chris Lattner5f42a242006-09-20 06:19:26 +00003894 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
3895 // equality comparison, then we're just comparing whether X itself is
3896 // zero.
3897 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
3898 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
3899 N0.getOperand(1).getOpcode() == ISD::Constant) {
3900 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
3901 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3902 ShAmt == Log2_32(MVT::getSizeInBits(N0.getValueType()))) {
3903 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
3904 // (srl (ctlz x), 5) == 0 -> X != 0
3905 // (srl (ctlz x), 5) != 1 -> X != 0
3906 Cond = ISD::SETNE;
3907 } else {
3908 // (srl (ctlz x), 5) != 0 -> X == 0
3909 // (srl (ctlz x), 5) == 1 -> X == 0
3910 Cond = ISD::SETEQ;
3911 }
3912 SDOperand Zero = DAG.getConstant(0, N0.getValueType());
3913 return DAG.getSetCC(VT, N0.getOperand(0).getOperand(0),
3914 Zero, Cond);
3915 }
3916 }
3917
Nate Begeman452d7be2005-09-16 00:54:12 +00003918 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3919 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3920 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3921
3922 // If the comparison constant has bits in the upper part, the
3923 // zero-extended value could never match.
3924 if (C1 & (~0ULL << InSize)) {
3925 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3926 switch (Cond) {
3927 case ISD::SETUGT:
3928 case ISD::SETUGE:
3929 case ISD::SETEQ: return DAG.getConstant(0, VT);
3930 case ISD::SETULT:
3931 case ISD::SETULE:
3932 case ISD::SETNE: return DAG.getConstant(1, VT);
3933 case ISD::SETGT:
3934 case ISD::SETGE:
3935 // True if the sign bit of C1 is set.
3936 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3937 case ISD::SETLT:
3938 case ISD::SETLE:
3939 // True if the sign bit of C1 isn't set.
3940 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3941 default:
3942 break;
3943 }
3944 }
3945
3946 // Otherwise, we can perform the comparison with the low bits.
3947 switch (Cond) {
3948 case ISD::SETEQ:
3949 case ISD::SETNE:
3950 case ISD::SETUGT:
3951 case ISD::SETUGE:
3952 case ISD::SETULT:
3953 case ISD::SETULE:
3954 return DAG.getSetCC(VT, N0.getOperand(0),
3955 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3956 Cond);
3957 default:
3958 break; // todo, be more careful with signed comparisons
3959 }
3960 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3961 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3962 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3963 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3964 MVT::ValueType ExtDstTy = N0.getValueType();
3965 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3966
3967 // If the extended part has any inconsistent bits, it cannot ever
3968 // compare equal. In other words, they have to be all ones or all
3969 // zeros.
3970 uint64_t ExtBits =
3971 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3972 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3973 return DAG.getConstant(Cond == ISD::SETNE, VT);
3974
3975 SDOperand ZextOp;
3976 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3977 if (Op0Ty == ExtSrcTy) {
3978 ZextOp = N0.getOperand(0);
3979 } else {
3980 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3981 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3982 DAG.getConstant(Imm, Op0Ty));
3983 }
Chris Lattner5750df92006-03-01 04:03:14 +00003984 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003985 // Otherwise, make this a use of a zext.
3986 return DAG.getSetCC(VT, ZextOp,
3987 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3988 ExtDstTy),
3989 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00003990 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
Chris Lattner8ac9d0e2006-10-14 01:02:29 +00003991 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3992
3993 // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC
3994 if (N0.getOpcode() == ISD::SETCC) {
3995 bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getValue() != 1);
3996 if (TrueWhenTrue)
3997 return N0;
3998
3999 // Invert the condition.
4000 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
4001 CC = ISD::getSetCCInverse(CC,
4002 MVT::isInteger(N0.getOperand(0).getValueType()));
4003 return DAG.getSetCC(VT, N0.getOperand(0), N0.getOperand(1), CC);
4004 }
4005
4006 if ((N0.getOpcode() == ISD::XOR ||
4007 (N0.getOpcode() == ISD::AND &&
4008 N0.getOperand(0).getOpcode() == ISD::XOR &&
4009 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
4010 isa<ConstantSDNode>(N0.getOperand(1)) &&
4011 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
4012 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We
4013 // can only do this if the top bits are known zero.
Chris Lattner50662be2006-10-17 21:24:15 +00004014 if (TLI.MaskedValueIsZero(N0,
Chris Lattner8ac9d0e2006-10-14 01:02:29 +00004015 MVT::getIntVTBitMask(N0.getValueType())-1)){
4016 // Okay, get the un-inverted input value.
4017 SDOperand Val;
4018 if (N0.getOpcode() == ISD::XOR)
4019 Val = N0.getOperand(0);
4020 else {
4021 assert(N0.getOpcode() == ISD::AND &&
4022 N0.getOperand(0).getOpcode() == ISD::XOR);
4023 // ((X^1)&1)^1 -> X & 1
4024 Val = DAG.getNode(ISD::AND, N0.getValueType(),
4025 N0.getOperand(0).getOperand(0),
4026 N0.getOperand(1));
4027 }
4028 return DAG.getSetCC(VT, Val, N1,
4029 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
Chris Lattner3391bcd2006-02-08 02:13:15 +00004030 }
Chris Lattner3391bcd2006-02-08 02:13:15 +00004031 }
Nate Begeman452d7be2005-09-16 00:54:12 +00004032 }
Chris Lattner5c46f742005-10-05 06:11:08 +00004033
Nate Begeman452d7be2005-09-16 00:54:12 +00004034 uint64_t MinVal, MaxVal;
4035 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
4036 if (ISD::isSignedIntSetCC(Cond)) {
4037 MinVal = 1ULL << (OperandBitSize-1);
4038 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
4039 MaxVal = ~0ULL >> (65-OperandBitSize);
4040 else
4041 MaxVal = 0;
4042 } else {
4043 MinVal = 0;
4044 MaxVal = ~0ULL >> (64-OperandBitSize);
4045 }
4046
4047 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
4048 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
4049 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
4050 --C1; // X >= C0 --> X > (C0-1)
4051 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
4052 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
4053 }
4054
4055 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
4056 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
4057 ++C1; // X <= C0 --> X < (C0+1)
4058 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
4059 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
4060 }
4061
4062 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
4063 return DAG.getConstant(0, VT); // X < MIN --> false
4064
4065 // Canonicalize setgt X, Min --> setne X, Min
4066 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
4067 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00004068 // Canonicalize setlt X, Max --> setne X, Max
4069 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
4070 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00004071
4072 // If we have setult X, 1, turn it into seteq X, 0
4073 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
4074 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
4075 ISD::SETEQ);
4076 // If we have setugt X, Max-1, turn it into seteq X, Max
4077 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
4078 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
4079 ISD::SETEQ);
4080
4081 // If we have "setcc X, C0", check to see if we can shrink the immediate
4082 // by changing cc.
4083
4084 // SETUGT X, SINTMAX -> SETLT X, 0
4085 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
4086 C1 == (~0ULL >> (65-OperandBitSize)))
4087 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
4088 ISD::SETLT);
4089
4090 // FIXME: Implement the rest of these.
4091
4092 // Fold bit comparisons when we can.
4093 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
4094 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
4095 if (ConstantSDNode *AndRHS =
4096 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
4097 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
4098 // Perform the xform if the AND RHS is a single bit.
Chris Lattner51dabfb2006-10-14 00:41:01 +00004099 if (isPowerOf2_64(AndRHS->getValue())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004100 return DAG.getNode(ISD::SRL, VT, N0,
4101 DAG.getConstant(Log2_64(AndRHS->getValue()),
4102 TLI.getShiftAmountTy()));
4103 }
4104 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
4105 // (X & 8) == 8 --> (X & 8) >> 3
4106 // Perform the xform if C1 is a single bit.
Chris Lattner51dabfb2006-10-14 00:41:01 +00004107 if (isPowerOf2_64(C1)) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004108 return DAG.getNode(ISD::SRL, VT, N0,
Chris Lattner729c6d12006-05-27 00:43:02 +00004109 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
Nate Begeman452d7be2005-09-16 00:54:12 +00004110 }
4111 }
4112 }
4113 }
4114 } else if (isa<ConstantSDNode>(N0.Val)) {
4115 // Ensure that the constant occurs on the RHS.
4116 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
4117 }
4118
Reid Spencer3ed469c2006-11-02 20:25:50 +00004119 if (isa<ConstantFPSDNode>(N0.Val)) {
Chris Lattner51dabfb2006-10-14 00:41:01 +00004120 // Constant fold or commute setcc.
4121 SDOperand O = DAG.FoldSetCC(VT, N0, N1, Cond);
4122 if (O.Val) return O;
4123 }
Nate Begeman452d7be2005-09-16 00:54:12 +00004124
4125 if (N0 == N1) {
Chris Lattner8ac9d0e2006-10-14 01:02:29 +00004126 // We can always fold X == X for integer setcc's.
Nate Begeman452d7be2005-09-16 00:54:12 +00004127 if (MVT::isInteger(N0.getValueType()))
4128 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
4129 unsigned UOF = ISD::getUnorderedFlavor(Cond);
4130 if (UOF == 2) // FP operators that are undefined on NaNs.
4131 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
4132 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
4133 return DAG.getConstant(UOF, VT);
4134 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
4135 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00004136 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00004137 if (NewCond != Cond)
4138 return DAG.getSetCC(VT, N0, N1, NewCond);
4139 }
4140
4141 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
4142 MVT::isInteger(N0.getValueType())) {
4143 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
4144 N0.getOpcode() == ISD::XOR) {
4145 // Simplify (X+Y) == (X+Z) --> Y == Z
4146 if (N0.getOpcode() == N1.getOpcode()) {
4147 if (N0.getOperand(0) == N1.getOperand(0))
4148 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
4149 if (N0.getOperand(1) == N1.getOperand(1))
4150 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
Evan Cheng1efba0e2006-08-29 06:42:35 +00004151 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004152 // If X op Y == Y op X, try other combinations.
4153 if (N0.getOperand(0) == N1.getOperand(1))
4154 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
4155 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00004156 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00004157 }
4158 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00004159
4160 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
4161 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
4162 // Turn (X+C1) == C2 --> X == C2-C1
4163 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
4164 return DAG.getSetCC(VT, N0.getOperand(0),
4165 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
4166 N0.getValueType()), Cond);
4167 }
4168
4169 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
4170 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00004171 // If we know that all of the inverted bits are zero, don't bother
4172 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00004173 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00004174 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00004175 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00004176 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00004177 }
4178
4179 // Turn (C1-X) == C2 --> X == C1-C2
4180 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
4181 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
4182 return DAG.getSetCC(VT, N0.getOperand(1),
4183 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
4184 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00004185 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00004186 }
4187 }
4188
Nate Begeman452d7be2005-09-16 00:54:12 +00004189 // Simplify (X+Z) == X --> Z == 0
4190 if (N0.getOperand(0) == N1)
4191 return DAG.getSetCC(VT, N0.getOperand(1),
4192 DAG.getConstant(0, N0.getValueType()), Cond);
4193 if (N0.getOperand(1) == N1) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00004194 if (DAG.isCommutativeBinOp(N0.getOpcode()))
Nate Begeman452d7be2005-09-16 00:54:12 +00004195 return DAG.getSetCC(VT, N0.getOperand(0),
4196 DAG.getConstant(0, N0.getValueType()), Cond);
4197 else {
4198 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
4199 // (Z-X) == X --> Z == X<<1
4200 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
4201 N1,
4202 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00004203 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004204 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
4205 }
4206 }
4207 }
4208
4209 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
4210 N1.getOpcode() == ISD::XOR) {
4211 // Simplify X == (X+Z) --> Z == 0
4212 if (N1.getOperand(0) == N0) {
4213 return DAG.getSetCC(VT, N1.getOperand(1),
4214 DAG.getConstant(0, N1.getValueType()), Cond);
4215 } else if (N1.getOperand(1) == N0) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00004216 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004217 return DAG.getSetCC(VT, N1.getOperand(0),
4218 DAG.getConstant(0, N1.getValueType()), Cond);
4219 } else {
4220 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
4221 // X == (Z-X) --> X<<1 == Z
4222 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
4223 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00004224 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004225 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
4226 }
4227 }
4228 }
4229 }
4230
4231 // Fold away ALL boolean setcc's.
4232 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00004233 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004234 switch (Cond) {
4235 default: assert(0 && "Unknown integer setcc!");
4236 case ISD::SETEQ: // X == Y -> (X^Y)^1
4237 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
4238 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00004239 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004240 break;
4241 case ISD::SETNE: // X != Y --> (X^Y)
4242 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
4243 break;
4244 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
4245 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
4246 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
4247 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00004248 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004249 break;
4250 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
4251 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
4252 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
4253 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00004254 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004255 break;
4256 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
4257 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
4258 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
4259 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00004260 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004261 break;
4262 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
4263 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
4264 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
4265 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
4266 break;
4267 }
4268 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00004269 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00004270 // FIXME: If running after legalize, we probably can't do this.
4271 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
4272 }
4273 return N0;
4274 }
4275
4276 // Could not fold it.
4277 return SDOperand();
4278}
4279
Nate Begeman69575232005-10-20 02:15:44 +00004280/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
4281/// return a DAG expression to select that will generate the same value by
4282/// multiplying by a magic number. See:
4283/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4284SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004285 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004286 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
4287
Andrew Lenharth232c9102006-06-12 16:07:18 +00004288 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004289 ii != ee; ++ii)
4290 AddToWorkList(*ii);
4291 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004292}
4293
4294/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
4295/// return a DAG expression to select that will generate the same value by
4296/// multiplying by a magic number. See:
4297/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4298SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004299 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004300 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00004301
Andrew Lenharth232c9102006-06-12 16:07:18 +00004302 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004303 ii != ee; ++ii)
4304 AddToWorkList(*ii);
4305 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004306}
4307
Jim Laskey71382342006-10-07 23:37:56 +00004308/// FindBaseOffset - Return true if base is known not to alias with anything
4309/// but itself. Provides base object and offset as results.
4310static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
4311 // Assume it is a primitive operation.
4312 Base = Ptr; Offset = 0;
4313
4314 // If it's an adding a simple constant then integrate the offset.
4315 if (Base.getOpcode() == ISD::ADD) {
4316 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
4317 Base = Base.getOperand(0);
4318 Offset += C->getValue();
4319 }
4320 }
4321
4322 // If it's any of the following then it can't alias with anything but itself.
4323 return isa<FrameIndexSDNode>(Base) ||
4324 isa<ConstantPoolSDNode>(Base) ||
4325 isa<GlobalAddressSDNode>(Base);
4326}
4327
4328/// isAlias - Return true if there is any possibility that the two addresses
4329/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00004330bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
4331 const Value *SrcValue1, int SrcValueOffset1,
4332 SDOperand Ptr2, int64_t Size2,
4333 const Value *SrcValue2, int SrcValueOffset2)
4334{
Jim Laskey71382342006-10-07 23:37:56 +00004335 // If they are the same then they must be aliases.
4336 if (Ptr1 == Ptr2) return true;
4337
4338 // Gather base node and offset information.
4339 SDOperand Base1, Base2;
4340 int64_t Offset1, Offset2;
4341 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
4342 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
4343
4344 // If they have a same base address then...
4345 if (Base1 == Base2) {
4346 // Check to see if the addresses overlap.
4347 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
4348 }
4349
Jim Laskey096c22e2006-10-18 12:29:57 +00004350 // If we know both bases then they can't alias.
4351 if (KnownBase1 && KnownBase2) return false;
4352
Jim Laskey07a27092006-10-18 19:08:31 +00004353 if (CombinerGlobalAA) {
4354 // Use alias analysis information.
4355 int Overlap1 = Size1 + SrcValueOffset1 + Offset1;
4356 int Overlap2 = Size2 + SrcValueOffset2 + Offset2;
4357 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00004358 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00004359 if (AAResult == AliasAnalysis::NoAlias)
4360 return false;
4361 }
Jim Laskey096c22e2006-10-18 12:29:57 +00004362
4363 // Otherwise we have to assume they alias.
4364 return true;
Jim Laskey71382342006-10-07 23:37:56 +00004365}
4366
4367/// FindAliasInfo - Extracts the relevant alias information from the memory
4368/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00004369bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00004370 SDOperand &Ptr, int64_t &Size,
4371 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004372 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4373 Ptr = LD->getBasePtr();
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004374 Size = MVT::getSizeInBits(LD->getLoadedVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004375 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004376 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00004377 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004378 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004379 Ptr = ST->getBasePtr();
Evan Cheng8b2794a2006-10-13 21:14:26 +00004380 Size = MVT::getSizeInBits(ST->getStoredVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004381 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004382 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00004383 } else {
Jim Laskey71382342006-10-07 23:37:56 +00004384 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00004385 }
4386
4387 return false;
4388}
4389
Jim Laskey6ff23e52006-10-04 16:53:27 +00004390/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
4391/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00004392void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00004393 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004394 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00004395 std::set<SDNode *> Visited; // Visited node set.
4396
Jim Laskey279f0532006-09-25 16:29:54 +00004397 // Get alias information for node.
4398 SDOperand Ptr;
4399 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004400 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004401 int SrcValueOffset;
4402 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00004403
Jim Laskey6ff23e52006-10-04 16:53:27 +00004404 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00004405 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00004406
Jim Laskeybc588b82006-10-05 15:07:25 +00004407 // Look at each chain and determine if it is an alias. If so, add it to the
4408 // aliases list. If not, then continue up the chain looking for the next
4409 // candidate.
4410 while (!Chains.empty()) {
4411 SDOperand Chain = Chains.back();
4412 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00004413
Jim Laskeybc588b82006-10-05 15:07:25 +00004414 // Don't bother if we've been before.
4415 if (Visited.find(Chain.Val) != Visited.end()) continue;
4416 Visited.insert(Chain.Val);
4417
4418 switch (Chain.getOpcode()) {
4419 case ISD::EntryToken:
4420 // Entry token is ideal chain operand, but handled in FindBetterChain.
4421 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00004422
Jim Laskeybc588b82006-10-05 15:07:25 +00004423 case ISD::LOAD:
4424 case ISD::STORE: {
4425 // Get alias information for Chain.
4426 SDOperand OpPtr;
4427 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004428 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004429 int OpSrcValueOffset;
4430 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
4431 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00004432
4433 // If chain is alias then stop here.
4434 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00004435 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
4436 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004437 Aliases.push_back(Chain);
4438 } else {
4439 // Look further up the chain.
4440 Chains.push_back(Chain.getOperand(0));
4441 // Clean up old chain.
4442 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00004443 }
Jim Laskeybc588b82006-10-05 15:07:25 +00004444 break;
4445 }
4446
4447 case ISD::TokenFactor:
4448 // We have to check each of the operands of the token factor, so we queue
4449 // then up. Adding the operands to the queue (stack) in reverse order
4450 // maintains the original order and increases the likelihood that getNode
4451 // will find a matching token factor (CSE.)
4452 for (unsigned n = Chain.getNumOperands(); n;)
4453 Chains.push_back(Chain.getOperand(--n));
4454 // Eliminate the token factor if we can.
4455 AddToWorkList(Chain.Val);
4456 break;
4457
4458 default:
4459 // For all other instructions we will just have to take what we can get.
4460 Aliases.push_back(Chain);
4461 break;
Jim Laskey279f0532006-09-25 16:29:54 +00004462 }
4463 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004464}
4465
4466/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
4467/// for a better chain (aliasing node.)
4468SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
4469 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00004470
Jim Laskey6ff23e52006-10-04 16:53:27 +00004471 // Accumulate all the aliases to this node.
4472 GatherAllAliases(N, OldChain, Aliases);
4473
4474 if (Aliases.size() == 0) {
4475 // If no operands then chain to entry token.
4476 return DAG.getEntryNode();
4477 } else if (Aliases.size() == 1) {
4478 // If a single operand then chain to it. We don't need to revisit it.
4479 return Aliases[0];
4480 }
4481
4482 // Construct a custom tailored token factor.
4483 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4484 &Aliases[0], Aliases.size());
4485
4486 // Make sure the old chain gets cleaned up.
4487 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
4488
4489 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00004490}
4491
Nate Begeman1d4d4142005-09-01 00:19:25 +00004492// SelectionDAG::Combine - This is the entry point for the file.
4493//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004494void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00004495 /// run - This is the main entry point to this class.
4496 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004497 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004498}