blob: d4b633259d25f10044769f79ee8af320a2c4578d [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"
Nate Begeman1d4d4142005-09-01 00:19:25 +000032#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000033#include "llvm/Analysis/AliasAnalysis.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000034#include "llvm/Target/TargetData.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000035#include "llvm/Target/TargetLowering.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000036#include "llvm/Target/TargetMachine.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000037#include "llvm/Target/TargetOptions.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000038#include "llvm/ADT/SmallPtrSet.h"
39#include "llvm/ADT/Statistic.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000040#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000041#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000042#include "llvm/Support/Debug.h"
43#include "llvm/Support/MathExtras.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000044#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000045using namespace llvm;
46
Chris Lattnercd3245a2006-12-19 22:41:21 +000047STATISTIC(NodesCombined , "Number of dag nodes combined");
48STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
49STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
50
Nate Begeman1d4d4142005-09-01 00:19:25 +000051namespace {
Chris Lattner938ab022007-01-16 04:55:25 +000052#ifndef NDEBUG
53 static cl::opt<bool>
54 ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden,
55 cl::desc("Pop up a window to show dags before the first "
56 "dag combine pass"));
57 static cl::opt<bool>
58 ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden,
59 cl::desc("Pop up a window to show dags before the second "
60 "dag combine pass"));
61#else
62 static const bool ViewDAGCombine1 = false;
63 static const bool ViewDAGCombine2 = false;
64#endif
65
Jim Laskey71382342006-10-07 23:37:56 +000066 static cl::opt<bool>
67 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000068 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000069
Jim Laskey07a27092006-10-18 19:08:31 +000070 static cl::opt<bool>
71 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
72 cl::desc("Include global information in alias analysis"));
73
Jim Laskeybc588b82006-10-05 15:07:25 +000074//------------------------------ DAGCombiner ---------------------------------//
75
Jim Laskey71382342006-10-07 23:37:56 +000076 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000077 SelectionDAG &DAG;
78 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000079 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000080
81 // Worklist of all of the nodes that need to be simplified.
82 std::vector<SDNode*> WorkList;
83
Jim Laskeyc7c3f112006-10-16 20:52:31 +000084 // AA - Used for DAG load/store alias analysis.
85 AliasAnalysis &AA;
86
Nate Begeman1d4d4142005-09-01 00:19:25 +000087 /// AddUsersToWorkList - When an instruction is simplified, add all users of
88 /// the instruction to the work lists because they might get more simplified
89 /// now.
90 ///
91 void AddUsersToWorkList(SDNode *N) {
92 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000093 UI != UE; ++UI)
Jim Laskey6ff23e52006-10-04 16:53:27 +000094 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000095 }
96
97 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000098 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000099 void removeFromWorkList(SDNode *N) {
100 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
101 WorkList.end());
102 }
103
Chris Lattner24664722006-03-01 04:53:38 +0000104 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +0000105 /// AddToWorkList - Add to the work list making sure it's instance is at the
106 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +0000107 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000108 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +0000109 WorkList.push_back(N);
110 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000111
Jim Laskey274062c2006-10-13 23:32:28 +0000112 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
113 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000114 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
Chris Lattner87514ca2005-10-10 22:31:19 +0000115 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000116 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +0000117 DOUT << "\nWith: "; DEBUG(To[0].Val->dump(&DAG));
118 DOUT << " and " << NumTo-1 << " other values\n";
Chris Lattner01a22022005-10-10 22:04:48 +0000119 std::vector<SDNode*> NowDead;
Chris Lattner3577e382006-08-11 17:56:38 +0000120 DAG.ReplaceAllUsesWith(N, To, &NowDead);
Chris Lattner01a22022005-10-10 22:04:48 +0000121
Jim Laskey274062c2006-10-13 23:32:28 +0000122 if (AddTo) {
123 // Push the new nodes and any users onto the worklist
124 for (unsigned i = 0, e = NumTo; i != e; ++i) {
125 AddToWorkList(To[i].Val);
126 AddUsersToWorkList(To[i].Val);
127 }
Chris Lattner01a22022005-10-10 22:04:48 +0000128 }
129
Jim Laskey6ff23e52006-10-04 16:53:27 +0000130 // Nodes can be reintroduced into the worklist. Make sure we do not
131 // process a node that has been replaced.
Chris Lattner01a22022005-10-10 22:04:48 +0000132 removeFromWorkList(N);
133 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
134 removeFromWorkList(NowDead[i]);
135
136 // Finally, since the node is now dead, remove it from the graph.
137 DAG.DeleteNode(N);
138 return SDOperand(N, 0);
139 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000140
Jim Laskey274062c2006-10-13 23:32:28 +0000141 SDOperand CombineTo(SDNode *N, SDOperand Res, bool AddTo = true) {
142 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000143 }
144
Jim Laskey274062c2006-10-13 23:32:28 +0000145 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1,
146 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000147 SDOperand To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000148 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000149 }
150 private:
151
Chris Lattner012f2412006-02-17 21:58:01 +0000152 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000153 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000154 /// propagation. If so, return true.
155 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000156 TargetLowering::TargetLoweringOpt TLO(DAG);
157 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000158 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
159 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
160 return false;
161
162 // Revisit the node.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000163 AddToWorkList(Op.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000164
165 // Replace the old value with the new one.
166 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000167 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.Val->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +0000168 DOUT << "\nWith: "; DEBUG(TLO.New.Val->dump(&DAG));
169 DOUT << '\n';
Chris Lattner012f2412006-02-17 21:58:01 +0000170
171 std::vector<SDNode*> NowDead;
172 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
173
Chris Lattner7d20d392006-02-20 06:51:04 +0000174 // Push the new node and any (possibly new) users onto the worklist.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000175 AddToWorkList(TLO.New.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000176 AddUsersToWorkList(TLO.New.Val);
177
178 // Nodes can end up on the worklist more than once. Make sure we do
179 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000180 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
181 removeFromWorkList(NowDead[i]);
182
Chris Lattner7d20d392006-02-20 06:51:04 +0000183 // Finally, if the node is now dead, remove it from the graph. The node
184 // may not be dead if the replacement process recursively simplified to
185 // something else needing this node.
186 if (TLO.Old.Val->use_empty()) {
187 removeFromWorkList(TLO.Old.Val);
Chris Lattnerec06e9a2007-04-18 03:05:22 +0000188
189 // If the operands of this node are only used by the node, they will now
190 // be dead. Make sure to visit them first to delete dead nodes early.
191 for (unsigned i = 0, e = TLO.Old.Val->getNumOperands(); i != e; ++i)
192 if (TLO.Old.Val->getOperand(i).Val->hasOneUse())
193 AddToWorkList(TLO.Old.Val->getOperand(i).Val);
194
Chris Lattner7d20d392006-02-20 06:51:04 +0000195 DAG.DeleteNode(TLO.Old.Val);
196 }
Chris Lattner012f2412006-02-17 21:58:01 +0000197 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000198 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000199
Chris Lattner448f2192006-11-11 00:39:41 +0000200 bool CombineToPreIndexedLoadStore(SDNode *N);
201 bool CombineToPostIndexedLoadStore(SDNode *N);
202
203
Nate Begeman1d4d4142005-09-01 00:19:25 +0000204 /// visit - call the node-specific routine that knows how to fold each
205 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000206 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000207
208 // Visitation implementation - Implement dag node combining for different
209 // node types. The semantics are as follows:
210 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000211 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000212 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000213 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000214 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000215 SDOperand visitTokenFactor(SDNode *N);
216 SDOperand visitADD(SDNode *N);
217 SDOperand visitSUB(SDNode *N);
Chris Lattner91153682007-03-04 20:03:15 +0000218 SDOperand visitADDC(SDNode *N);
219 SDOperand visitADDE(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000220 SDOperand visitMUL(SDNode *N);
221 SDOperand visitSDIV(SDNode *N);
222 SDOperand visitUDIV(SDNode *N);
223 SDOperand visitSREM(SDNode *N);
224 SDOperand visitUREM(SDNode *N);
225 SDOperand visitMULHU(SDNode *N);
226 SDOperand visitMULHS(SDNode *N);
227 SDOperand visitAND(SDNode *N);
228 SDOperand visitOR(SDNode *N);
229 SDOperand visitXOR(SDNode *N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000230 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000231 SDOperand visitSHL(SDNode *N);
232 SDOperand visitSRA(SDNode *N);
233 SDOperand visitSRL(SDNode *N);
234 SDOperand visitCTLZ(SDNode *N);
235 SDOperand visitCTTZ(SDNode *N);
236 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000237 SDOperand visitSELECT(SDNode *N);
238 SDOperand visitSELECT_CC(SDNode *N);
239 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000240 SDOperand visitSIGN_EXTEND(SDNode *N);
241 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000242 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000243 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
244 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000245 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000246 SDOperand visitVBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000247 SDOperand visitFADD(SDNode *N);
248 SDOperand visitFSUB(SDNode *N);
249 SDOperand visitFMUL(SDNode *N);
250 SDOperand visitFDIV(SDNode *N);
251 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000252 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000253 SDOperand visitSINT_TO_FP(SDNode *N);
254 SDOperand visitUINT_TO_FP(SDNode *N);
255 SDOperand visitFP_TO_SINT(SDNode *N);
256 SDOperand visitFP_TO_UINT(SDNode *N);
257 SDOperand visitFP_ROUND(SDNode *N);
258 SDOperand visitFP_ROUND_INREG(SDNode *N);
259 SDOperand visitFP_EXTEND(SDNode *N);
260 SDOperand visitFNEG(SDNode *N);
261 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000262 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000263 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000264 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000265 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000266 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
267 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000268 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000269 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000270 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000271
Evan Cheng44f1f092006-04-20 08:56:16 +0000272 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000273 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
274
Chris Lattner40c62d52005-10-18 06:04:22 +0000275 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000276 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000277 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
278 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000279 SDOperand N3, ISD::CondCode CC,
280 bool NotExtCompare = false);
Nate Begeman452d7be2005-09-16 00:54:12 +0000281 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000282 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000283 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000284 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000285 SDOperand BuildUDIV(SDNode *N);
286 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Evan Chengc88138f2007-03-22 01:54:19 +0000287 SDOperand ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000288
Jim Laskey6ff23e52006-10-04 16:53:27 +0000289 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
290 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000291 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000292 SmallVector<SDOperand, 8> &Aliases);
293
Jim Laskey096c22e2006-10-18 12:29:57 +0000294 /// isAlias - Return true if there is any possibility that the two addresses
295 /// overlap.
296 bool isAlias(SDOperand Ptr1, int64_t Size1,
297 const Value *SrcValue1, int SrcValueOffset1,
298 SDOperand Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000299 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000300
Jim Laskey7ca56af2006-10-11 13:47:09 +0000301 /// FindAliasInfo - Extracts the relevant alias information from the memory
302 /// node. Returns true if the operand was a load.
303 bool FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +0000304 SDOperand &Ptr, int64_t &Size,
305 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000306
Jim Laskey279f0532006-09-25 16:29:54 +0000307 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000308 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000309 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
310
Nate Begeman1d4d4142005-09-01 00:19:25 +0000311public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000312 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
313 : DAG(D),
314 TLI(D.getTargetLoweringInfo()),
315 AfterLegalize(false),
316 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000317
318 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000319 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000320 };
321}
322
Chris Lattner24664722006-03-01 04:53:38 +0000323//===----------------------------------------------------------------------===//
324// TargetLowering::DAGCombinerInfo implementation
325//===----------------------------------------------------------------------===//
326
327void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
328 ((DAGCombiner*)DC)->AddToWorkList(N);
329}
330
331SDOperand TargetLowering::DAGCombinerInfo::
332CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000333 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000334}
335
336SDOperand TargetLowering::DAGCombinerInfo::
337CombineTo(SDNode *N, SDOperand Res) {
338 return ((DAGCombiner*)DC)->CombineTo(N, Res);
339}
340
341
342SDOperand TargetLowering::DAGCombinerInfo::
343CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
344 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
345}
346
347
Chris Lattner24664722006-03-01 04:53:38 +0000348//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000349// Helper Functions
350//===----------------------------------------------------------------------===//
351
352/// isNegatibleForFree - Return 1 if we can compute the negated form of the
353/// specified expression for the same cost as the expression itself, or 2 if we
354/// can compute the negated form more cheaply than the expression itself.
Chris Lattner501fee72007-05-23 07:35:22 +0000355static char isNegatibleForFree(SDOperand Op, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000356 // fneg is removable even if it has multiple uses.
357 if (Op.getOpcode() == ISD::FNEG) return 2;
358
359 // Don't allow anything with multiple uses.
360 if (!Op.hasOneUse()) return 0;
361
Chris Lattner3adf9512007-05-25 02:19:06 +0000362 // Don't recurse exponentially.
363 if (Depth > 6) return 0;
364
Chris Lattner29446522007-05-14 22:04:50 +0000365 switch (Op.getOpcode()) {
366 default: return false;
367 case ISD::ConstantFP:
368 return 1;
369 case ISD::FADD:
370 // FIXME: determine better conditions for this xform.
371 if (!UnsafeFPMath) return 0;
372
373 // -(A+B) -> -A - B
Chris Lattner501fee72007-05-23 07:35:22 +0000374 if (char V = isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000375 return V;
376 // -(A+B) -> -B - A
Chris Lattner501fee72007-05-23 07:35:22 +0000377 return isNegatibleForFree(Op.getOperand(1), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000378 case ISD::FSUB:
379 // We can't turn -(A-B) into B-A when we honor signed zeros.
380 if (!UnsafeFPMath) return 0;
381
382 // -(A-B) -> B-A
383 return 1;
384
385 case ISD::FMUL:
386 case ISD::FDIV:
387 if (HonorSignDependentRoundingFPMath()) return 0;
388
389 // -(X*Y) -> (-X * Y) or (X*-Y)
Chris Lattner501fee72007-05-23 07:35:22 +0000390 if (char V = isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000391 return V;
392
Chris Lattner501fee72007-05-23 07:35:22 +0000393 return isNegatibleForFree(Op.getOperand(1), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000394
395 case ISD::FP_EXTEND:
396 case ISD::FP_ROUND:
397 case ISD::FSIN:
Chris Lattner501fee72007-05-23 07:35:22 +0000398 return isNegatibleForFree(Op.getOperand(0), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000399 }
400}
401
402/// GetNegatedExpression - If isNegatibleForFree returns true, this function
403/// returns the newly negated expression.
Chris Lattner3adf9512007-05-25 02:19:06 +0000404static SDOperand GetNegatedExpression(SDOperand Op, SelectionDAG &DAG,
405 unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000406 // fneg is removable even if it has multiple uses.
407 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
408
409 // Don't allow anything with multiple uses.
410 assert(Op.hasOneUse() && "Unknown reuse!");
411
Chris Lattner3adf9512007-05-25 02:19:06 +0000412 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000413 switch (Op.getOpcode()) {
414 default: assert(0 && "Unknown code");
415 case ISD::ConstantFP:
416 return DAG.getConstantFP(-cast<ConstantFPSDNode>(Op)->getValue(),
417 Op.getValueType());
418 case ISD::FADD:
419 // FIXME: determine better conditions for this xform.
420 assert(UnsafeFPMath);
421
422 // -(A+B) -> -A - B
Chris Lattner3adf9512007-05-25 02:19:06 +0000423 if (isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000424 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000425 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000426 Op.getOperand(1));
427 // -(A+B) -> -B - A
428 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000429 GetNegatedExpression(Op.getOperand(1), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000430 Op.getOperand(0));
431 case ISD::FSUB:
432 // We can't turn -(A-B) into B-A when we honor signed zeros.
433 assert(UnsafeFPMath);
434
435 // -(A-B) -> B-A
436 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
437 Op.getOperand(0));
438
439 case ISD::FMUL:
440 case ISD::FDIV:
441 assert(!HonorSignDependentRoundingFPMath());
442
443 // -(X*Y) -> -X * Y
Chris Lattner3adf9512007-05-25 02:19:06 +0000444 if (isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000445 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000446 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000447 Op.getOperand(1));
448
449 // -(X*Y) -> X * -Y
450 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
451 Op.getOperand(0),
Chris Lattner3adf9512007-05-25 02:19:06 +0000452 GetNegatedExpression(Op.getOperand(1), DAG, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000453
454 case ISD::FP_EXTEND:
455 case ISD::FP_ROUND:
456 case ISD::FSIN:
457 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000458 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000459 }
460}
Chris Lattner24664722006-03-01 04:53:38 +0000461
462
Nate Begeman4ebd8052005-09-01 23:24:04 +0000463// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
464// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000465// Also, set the incoming LHS, RHS, and CC references to the appropriate
466// nodes based on the type of node we are checking. This simplifies life a
467// bit for the callers.
468static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
469 SDOperand &CC) {
470 if (N.getOpcode() == ISD::SETCC) {
471 LHS = N.getOperand(0);
472 RHS = N.getOperand(1);
473 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000474 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000475 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000476 if (N.getOpcode() == ISD::SELECT_CC &&
477 N.getOperand(2).getOpcode() == ISD::Constant &&
478 N.getOperand(3).getOpcode() == ISD::Constant &&
479 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000480 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
481 LHS = N.getOperand(0);
482 RHS = N.getOperand(1);
483 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000484 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000485 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000486 return false;
487}
488
Nate Begeman99801192005-09-07 23:25:52 +0000489// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
490// one use. If this is true, it allows the users to invert the operation for
491// free when it is profitable to do so.
492static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000493 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000494 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000495 return true;
496 return false;
497}
498
Nate Begemancd4d58c2006-02-03 06:46:56 +0000499SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
500 MVT::ValueType VT = N0.getValueType();
501 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
502 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
503 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
504 if (isa<ConstantSDNode>(N1)) {
505 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000506 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000507 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
508 } else if (N0.hasOneUse()) {
509 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000510 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000511 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
512 }
513 }
514 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
515 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
516 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
517 if (isa<ConstantSDNode>(N0)) {
518 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000519 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000520 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
521 } else if (N1.hasOneUse()) {
522 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000523 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000524 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
525 }
526 }
527 return SDOperand();
528}
529
Chris Lattner29446522007-05-14 22:04:50 +0000530//===----------------------------------------------------------------------===//
531// Main DAG Combiner implementation
532//===----------------------------------------------------------------------===//
533
Nate Begeman4ebd8052005-09-01 23:24:04 +0000534void DAGCombiner::Run(bool RunningAfterLegalize) {
535 // set the instance variable, so that the various visit routines may use it.
536 AfterLegalize = RunningAfterLegalize;
537
Nate Begeman646d7e22005-09-02 21:18:40 +0000538 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000539 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
540 E = DAG.allnodes_end(); I != E; ++I)
541 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000542
Chris Lattner95038592005-10-05 06:35:28 +0000543 // Create a dummy node (which is not added to allnodes), that adds a reference
544 // to the root node, preventing it from being deleted, and tracking any
545 // changes of the root.
546 HandleSDNode Dummy(DAG.getRoot());
547
Jim Laskey26f7fa72006-10-17 19:33:52 +0000548 // The root of the dag may dangle to deleted nodes until the dag combiner is
549 // done. Set it to null to avoid confusion.
550 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000551
552 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
553 TargetLowering::DAGCombinerInfo
Evan Chengfa1eb272007-02-08 22:13:59 +0000554 DagCombineInfo(DAG, !RunningAfterLegalize, false, this);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000555
Nate Begeman1d4d4142005-09-01 00:19:25 +0000556 // while the worklist isn't empty, inspect the node on the end of it and
557 // try and combine it.
558 while (!WorkList.empty()) {
559 SDNode *N = WorkList.back();
560 WorkList.pop_back();
561
562 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000563 // N is deleted from the DAG, since they too may now be dead or may have a
564 // reduced number of uses, allowing other xforms.
565 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000566 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000567 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000568
Chris Lattner95038592005-10-05 06:35:28 +0000569 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000570 continue;
571 }
572
Nate Begeman83e75ec2005-09-06 04:43:02 +0000573 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000574
575 // If nothing happened, try a target-specific DAG combine.
576 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000577 assert(N->getOpcode() != ISD::DELETED_NODE &&
578 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000579 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
580 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
581 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
582 }
583
Nate Begeman83e75ec2005-09-06 04:43:02 +0000584 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000585 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000586 // If we get back the same node we passed in, rather than a new node or
587 // zero, we know that the node must have defined multiple values and
588 // CombineTo was used. Since CombineTo takes care of the worklist
589 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000590 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000591 assert(N->getOpcode() != ISD::DELETED_NODE &&
592 RV.Val->getOpcode() != ISD::DELETED_NODE &&
593 "Node was deleted but visit returned new node!");
594
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000595 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +0000596 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
597 DOUT << '\n';
Chris Lattner01a22022005-10-10 22:04:48 +0000598 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000599 if (N->getNumValues() == RV.Val->getNumValues())
600 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
601 else {
602 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
603 SDOperand OpV = RV;
604 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
605 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000606
607 // Push the new node and any users onto the worklist
Jim Laskey6ff23e52006-10-04 16:53:27 +0000608 AddToWorkList(RV.Val);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000609 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000610
Jim Laskey6ff23e52006-10-04 16:53:27 +0000611 // Nodes can be reintroduced into the worklist. Make sure we do not
612 // process a node that has been replaced.
Nate Begeman646d7e22005-09-02 21:18:40 +0000613 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000614 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
615 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000616
617 // Finally, since the node is now dead, remove it from the graph.
618 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000619 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000620 }
621 }
Chris Lattner95038592005-10-05 06:35:28 +0000622
623 // If the root changed (e.g. it was a dead load, update the root).
624 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000625}
626
Nate Begeman83e75ec2005-09-06 04:43:02 +0000627SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000628 switch(N->getOpcode()) {
629 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000630 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000631 case ISD::ADD: return visitADD(N);
632 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000633 case ISD::ADDC: return visitADDC(N);
634 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000635 case ISD::MUL: return visitMUL(N);
636 case ISD::SDIV: return visitSDIV(N);
637 case ISD::UDIV: return visitUDIV(N);
638 case ISD::SREM: return visitSREM(N);
639 case ISD::UREM: return visitUREM(N);
640 case ISD::MULHU: return visitMULHU(N);
641 case ISD::MULHS: return visitMULHS(N);
642 case ISD::AND: return visitAND(N);
643 case ISD::OR: return visitOR(N);
644 case ISD::XOR: return visitXOR(N);
645 case ISD::SHL: return visitSHL(N);
646 case ISD::SRA: return visitSRA(N);
647 case ISD::SRL: return visitSRL(N);
648 case ISD::CTLZ: return visitCTLZ(N);
649 case ISD::CTTZ: return visitCTTZ(N);
650 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000651 case ISD::SELECT: return visitSELECT(N);
652 case ISD::SELECT_CC: return visitSELECT_CC(N);
653 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000654 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
655 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000656 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000657 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
658 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000659 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000660 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000661 case ISD::FADD: return visitFADD(N);
662 case ISD::FSUB: return visitFSUB(N);
663 case ISD::FMUL: return visitFMUL(N);
664 case ISD::FDIV: return visitFDIV(N);
665 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000666 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000667 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
668 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
669 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
670 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
671 case ISD::FP_ROUND: return visitFP_ROUND(N);
672 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
673 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
674 case ISD::FNEG: return visitFNEG(N);
675 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000676 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000677 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000678 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000679 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000680 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
681 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000682 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000683 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000684 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000685 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
686 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
687 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
688 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
689 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
690 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
691 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
692 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000693 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000694 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000695}
696
Chris Lattner6270f682006-10-08 22:57:01 +0000697/// getInputChainForNode - Given a node, return its input chain if it has one,
698/// otherwise return a null sd operand.
699static SDOperand getInputChainForNode(SDNode *N) {
700 if (unsigned NumOps = N->getNumOperands()) {
701 if (N->getOperand(0).getValueType() == MVT::Other)
702 return N->getOperand(0);
703 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
704 return N->getOperand(NumOps-1);
705 for (unsigned i = 1; i < NumOps-1; ++i)
706 if (N->getOperand(i).getValueType() == MVT::Other)
707 return N->getOperand(i);
708 }
709 return SDOperand(0, 0);
710}
711
Nate Begeman83e75ec2005-09-06 04:43:02 +0000712SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000713 // If N has two operands, where one has an input chain equal to the other,
714 // the 'other' chain is redundant.
715 if (N->getNumOperands() == 2) {
716 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
717 return N->getOperand(0);
718 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
719 return N->getOperand(1);
720 }
721
Chris Lattnerc76d4412007-05-16 06:37:59 +0000722 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
723 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
724 SmallPtrSet<SDNode*, 16> SeenOps;
725 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000726
727 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000728 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000729
Jim Laskey71382342006-10-07 23:37:56 +0000730 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000731 // encountered.
732 for (unsigned i = 0; i < TFs.size(); ++i) {
733 SDNode *TF = TFs[i];
734
Jim Laskey6ff23e52006-10-04 16:53:27 +0000735 // Check each of the operands.
736 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
737 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000738
Jim Laskey6ff23e52006-10-04 16:53:27 +0000739 switch (Op.getOpcode()) {
740 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000741 // Entry tokens don't need to be added to the list. They are
742 // rededundant.
743 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000744 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000745
Jim Laskey6ff23e52006-10-04 16:53:27 +0000746 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000747 if ((CombinerAA || Op.hasOneUse()) &&
748 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000749 // Queue up for processing.
750 TFs.push_back(Op.Val);
751 // Clean up in case the token factor is removed.
752 AddToWorkList(Op.Val);
753 Changed = true;
754 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000755 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000756 // Fall thru
757
758 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000759 // Only add if it isn't already in the list.
760 if (SeenOps.insert(Op.Val))
Jim Laskeybc588b82006-10-05 15:07:25 +0000761 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000762 else
763 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000764 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000765 }
766 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000767 }
768
769 SDOperand Result;
770
771 // If we've change things around then replace token factor.
772 if (Changed) {
773 if (Ops.size() == 0) {
774 // The entry token is the only possible outcome.
775 Result = DAG.getEntryNode();
776 } else {
777 // New and improved token factor.
778 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000779 }
Jim Laskey274062c2006-10-13 23:32:28 +0000780
781 // Don't add users to work list.
782 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000783 }
Jim Laskey279f0532006-09-25 16:29:54 +0000784
Jim Laskey6ff23e52006-10-04 16:53:27 +0000785 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000786}
787
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000788static
789SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
790 MVT::ValueType VT = N0.getValueType();
791 SDOperand N00 = N0.getOperand(0);
792 SDOperand N01 = N0.getOperand(1);
793 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
794 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
795 isa<ConstantSDNode>(N00.getOperand(1))) {
796 N0 = DAG.getNode(ISD::ADD, VT,
797 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
798 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
799 return DAG.getNode(ISD::ADD, VT, N0, N1);
800 }
801 return SDOperand();
802}
803
Nate Begeman83e75ec2005-09-06 04:43:02 +0000804SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000805 SDOperand N0 = N->getOperand(0);
806 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000807 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
808 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000809 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000810
811 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000812 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000813 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000814 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000815 if (N0C && !N1C)
816 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000817 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000818 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000819 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000820 // fold ((c1-A)+c2) -> (c1+c2)-A
821 if (N1C && N0.getOpcode() == ISD::SUB)
822 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
823 return DAG.getNode(ISD::SUB, VT,
824 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
825 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000826 // reassociate add
827 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
828 if (RADD.Val != 0)
829 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000830 // fold ((0-A) + B) -> B-A
831 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
832 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000833 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000834 // fold (A + (0-B)) -> A-B
835 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
836 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000837 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000838 // fold (A+(B-A)) -> B
839 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000840 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000841
Evan Cheng860771d2006-03-01 01:09:54 +0000842 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000843 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000844
845 // fold (a+b) -> (a|b) iff a and b share no bits.
846 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
847 uint64_t LHSZero, LHSOne;
848 uint64_t RHSZero, RHSOne;
849 uint64_t Mask = MVT::getIntVTBitMask(VT);
850 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
851 if (LHSZero) {
852 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
853
854 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
855 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
856 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
857 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
858 return DAG.getNode(ISD::OR, VT, N0, N1);
859 }
860 }
Evan Cheng3ef554d2006-11-06 08:14:30 +0000861
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000862 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
863 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
864 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
865 if (Result.Val) return Result;
866 }
867 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
868 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
869 if (Result.Val) return Result;
870 }
871
Nate Begeman83e75ec2005-09-06 04:43:02 +0000872 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000873}
874
Chris Lattner91153682007-03-04 20:03:15 +0000875SDOperand DAGCombiner::visitADDC(SDNode *N) {
876 SDOperand N0 = N->getOperand(0);
877 SDOperand N1 = N->getOperand(1);
878 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
879 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
880 MVT::ValueType VT = N0.getValueType();
881
882 // If the flag result is dead, turn this into an ADD.
883 if (N->hasNUsesOfValue(0, 1))
884 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +0000885 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +0000886
887 // canonicalize constant to RHS.
Chris Lattnerbcf24842007-03-04 20:08:45 +0000888 if (N0C && !N1C) {
889 SDOperand Ops[] = { N1, N0 };
890 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
891 }
Chris Lattner91153682007-03-04 20:03:15 +0000892
Chris Lattnerb6541762007-03-04 20:40:38 +0000893 // fold (addc x, 0) -> x + no carry out
894 if (N1C && N1C->isNullValue())
895 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
896
897 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
898 uint64_t LHSZero, LHSOne;
899 uint64_t RHSZero, RHSOne;
900 uint64_t Mask = MVT::getIntVTBitMask(VT);
901 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
902 if (LHSZero) {
903 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
904
905 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
906 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
907 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
908 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
909 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
910 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
911 }
Chris Lattner91153682007-03-04 20:03:15 +0000912
913 return SDOperand();
914}
915
916SDOperand DAGCombiner::visitADDE(SDNode *N) {
917 SDOperand N0 = N->getOperand(0);
918 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +0000919 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +0000920 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
921 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Chris Lattnerbcf24842007-03-04 20:08:45 +0000922 //MVT::ValueType VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +0000923
924 // canonicalize constant to RHS
Chris Lattnerbcf24842007-03-04 20:08:45 +0000925 if (N0C && !N1C) {
Chris Lattnerb6541762007-03-04 20:40:38 +0000926 SDOperand Ops[] = { N1, N0, CarryIn };
Chris Lattnerbcf24842007-03-04 20:08:45 +0000927 return DAG.getNode(ISD::ADDE, N->getVTList(), Ops, 3);
928 }
Chris Lattner91153682007-03-04 20:03:15 +0000929
Chris Lattnerb6541762007-03-04 20:40:38 +0000930 // fold (adde x, y, false) -> (addc x, y)
931 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) {
932 SDOperand Ops[] = { N1, N0 };
933 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
934 }
Chris Lattner91153682007-03-04 20:03:15 +0000935
936 return SDOperand();
937}
938
939
940
Nate Begeman83e75ec2005-09-06 04:43:02 +0000941SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000942 SDOperand N0 = N->getOperand(0);
943 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000944 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
945 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000946 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000947
Chris Lattner854077d2005-10-17 01:07:11 +0000948 // fold (sub x, x) -> 0
949 if (N0 == N1)
950 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000951 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000952 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000953 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000954 // fold (sub x, c) -> (add x, -c)
955 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000956 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000957 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000958 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000959 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000960 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000961 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000962 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000963 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000964}
965
Nate Begeman83e75ec2005-09-06 04:43:02 +0000966SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000967 SDOperand N0 = N->getOperand(0);
968 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000969 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
970 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000971 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000972
973 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000974 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000975 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000976 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000977 if (N0C && !N1C)
978 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000979 // fold (mul 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 (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000983 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000984 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000985 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000986 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000987 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000988 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000989 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000990 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
991 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
992 // FIXME: If the input is something that is easily negated (e.g. a
993 // single-use add), we should put the negate there.
994 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
995 DAG.getNode(ISD::SHL, VT, N0,
996 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
997 TLI.getShiftAmountTy())));
998 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +0000999
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001000 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1001 if (N1C && N0.getOpcode() == ISD::SHL &&
1002 isa<ConstantSDNode>(N0.getOperand(1))) {
1003 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001004 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001005 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1006 }
1007
1008 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1009 // use.
1010 {
1011 SDOperand Sh(0,0), Y(0,0);
1012 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1013 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1014 N0.Val->hasOneUse()) {
1015 Sh = N0; Y = N1;
1016 } else if (N1.getOpcode() == ISD::SHL &&
1017 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1018 Sh = N1; Y = N0;
1019 }
1020 if (Sh.Val) {
1021 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1022 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1023 }
1024 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001025 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1026 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1027 isa<ConstantSDNode>(N0.getOperand(1))) {
1028 return DAG.getNode(ISD::ADD, VT,
1029 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1030 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1031 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001032
Nate Begemancd4d58c2006-02-03 06:46:56 +00001033 // reassociate mul
1034 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1035 if (RMUL.Val != 0)
1036 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +00001037 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001038}
1039
Nate Begeman83e75ec2005-09-06 04:43:02 +00001040SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001041 SDOperand N0 = N->getOperand(0);
1042 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001043 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1044 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001045 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001046
1047 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001048 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001049 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001050 // fold (sdiv X, 1) -> X
1051 if (N1C && N1C->getSignExtended() == 1LL)
1052 return N0;
1053 // fold (sdiv X, -1) -> 0-X
1054 if (N1C && N1C->isAllOnesValue())
1055 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001056 // If we know the sign bits of both operands are zero, strength reduce to a
1057 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
1058 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001059 if (TLI.MaskedValueIsZero(N1, SignBit) &&
1060 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +00001061 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001062 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +00001063 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001064 (isPowerOf2_64(N1C->getSignExtended()) ||
1065 isPowerOf2_64(-N1C->getSignExtended()))) {
1066 // If dividing by powers of two is cheap, then don't perform the following
1067 // fold.
1068 if (TLI.isPow2DivCheap())
1069 return SDOperand();
1070 int64_t pow2 = N1C->getSignExtended();
1071 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001072 unsigned lg2 = Log2_64(abs2);
1073 // Splat the sign bit into the register
1074 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001075 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1076 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001077 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001078 // Add (N0 < 0) ? abs2 - 1 : 0;
1079 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1080 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001081 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001082 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001083 AddToWorkList(SRL.Val);
1084 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001085 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1086 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001087 // If we're dividing by a positive value, we're done. Otherwise, we must
1088 // negate the result.
1089 if (pow2 > 0)
1090 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001091 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001092 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1093 }
Nate Begeman69575232005-10-20 02:15:44 +00001094 // if integer divide is expensive and we satisfy the requirements, emit an
1095 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001096 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001097 !TLI.isIntDivCheap()) {
1098 SDOperand Op = BuildSDIV(N);
1099 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001100 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001101 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001102}
1103
Nate Begeman83e75ec2005-09-06 04:43:02 +00001104SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001105 SDOperand N0 = N->getOperand(0);
1106 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001107 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1108 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001109 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001110
1111 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001112 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001113 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001114 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +00001115 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001116 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001117 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001118 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001119 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1120 if (N1.getOpcode() == ISD::SHL) {
1121 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1122 if (isPowerOf2_64(SHC->getValue())) {
1123 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001124 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
1125 DAG.getConstant(Log2_64(SHC->getValue()),
1126 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001127 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001128 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001129 }
1130 }
1131 }
Nate Begeman69575232005-10-20 02:15:44 +00001132 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +00001133 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
1134 SDOperand Op = BuildUDIV(N);
1135 if (Op.Val) return Op;
1136 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001137 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001138}
1139
Nate Begeman83e75ec2005-09-06 04:43:02 +00001140SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001141 SDOperand N0 = N->getOperand(0);
1142 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001143 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1144 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001145 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001146
1147 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001148 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001149 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001150 // If we know the sign bits of both operands are zero, strength reduce to a
1151 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
1152 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001153 if (TLI.MaskedValueIsZero(N1, SignBit) &&
1154 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +00001155 return DAG.getNode(ISD::UREM, VT, N0, N1);
Chris Lattner26d29902006-10-12 20:58:32 +00001156
1157 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1158 // the remainder operation.
1159 if (N1C && !N1C->isNullValue()) {
1160 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
1161 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1162 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1163 AddToWorkList(Div.Val);
1164 AddToWorkList(Mul.Val);
1165 return Sub;
1166 }
1167
Nate Begeman83e75ec2005-09-06 04:43:02 +00001168 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001169}
1170
Nate Begeman83e75ec2005-09-06 04:43:02 +00001171SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001172 SDOperand N0 = N->getOperand(0);
1173 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001174 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1175 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001176 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001177
1178 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001179 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001180 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001181 // fold (urem x, pow2) -> (and x, pow2-1)
1182 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +00001183 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001184 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1185 if (N1.getOpcode() == ISD::SHL) {
1186 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1187 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +00001188 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001189 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001190 return DAG.getNode(ISD::AND, VT, N0, Add);
1191 }
1192 }
1193 }
Chris Lattner26d29902006-10-12 20:58:32 +00001194
1195 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1196 // the remainder operation.
1197 if (N1C && !N1C->isNullValue()) {
1198 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
1199 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1200 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1201 AddToWorkList(Div.Val);
1202 AddToWorkList(Mul.Val);
1203 return Sub;
1204 }
1205
Nate Begeman83e75ec2005-09-06 04:43:02 +00001206 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001207}
1208
Nate Begeman83e75ec2005-09-06 04:43:02 +00001209SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001210 SDOperand N0 = N->getOperand(0);
1211 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001212 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001213
1214 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001215 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001216 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001217 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001218 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001219 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1220 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001221 TLI.getShiftAmountTy()));
1222 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001223}
1224
Nate Begeman83e75ec2005-09-06 04:43:02 +00001225SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001226 SDOperand N0 = N->getOperand(0);
1227 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001228 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001229
1230 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001231 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001232 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001233 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001234 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001235 return DAG.getConstant(0, N0.getValueType());
1236 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001237}
1238
Chris Lattner35e5c142006-05-05 05:51:50 +00001239/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1240/// two operands of the same opcode, try to simplify it.
1241SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1242 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1243 MVT::ValueType VT = N0.getValueType();
1244 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1245
Chris Lattner540121f2006-05-05 06:31:05 +00001246 // For each of OP in AND/OR/XOR:
1247 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1248 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1249 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001250 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001251 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001252 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001253 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1254 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1255 N0.getOperand(0).getValueType(),
1256 N0.getOperand(0), N1.getOperand(0));
1257 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001258 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001259 }
1260
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001261 // For each of OP in SHL/SRL/SRA/AND...
1262 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1263 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1264 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001265 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001266 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001267 N0.getOperand(1) == N1.getOperand(1)) {
1268 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1269 N0.getOperand(0).getValueType(),
1270 N0.getOperand(0), N1.getOperand(0));
1271 AddToWorkList(ORNode.Val);
1272 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1273 }
1274
1275 return SDOperand();
1276}
1277
Nate Begeman83e75ec2005-09-06 04:43:02 +00001278SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001279 SDOperand N0 = N->getOperand(0);
1280 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001281 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001282 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1283 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001284 MVT::ValueType VT = N1.getValueType();
1285
1286 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001287 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001288 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001289 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001290 if (N0C && !N1C)
1291 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001292 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001293 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001294 return N0;
1295 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +00001296 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001297 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001298 // reassociate and
1299 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1300 if (RAND.Val != 0)
1301 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001302 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001303 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001304 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001305 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001306 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001307 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1308 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001309 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +00001310 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001311 ~N1C->getValue() & InMask)) {
1312 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1313 N0.getOperand(0));
1314
1315 // Replace uses of the AND with uses of the Zero extend node.
1316 CombineTo(N, Zext);
1317
Chris Lattner3603cd62006-02-02 07:17:31 +00001318 // We actually want to replace all uses of the any_extend with the
1319 // zero_extend, to avoid duplicating things. This will later cause this
1320 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001321 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001322 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001323 }
1324 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001325 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1326 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1327 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1328 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1329
1330 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1331 MVT::isInteger(LL.getValueType())) {
1332 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1333 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1334 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001335 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001336 return DAG.getSetCC(VT, ORNode, LR, Op1);
1337 }
1338 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1339 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1340 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001341 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001342 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1343 }
1344 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1345 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1346 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001347 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001348 return DAG.getSetCC(VT, ORNode, LR, Op1);
1349 }
1350 }
1351 // canonicalize equivalent to ll == rl
1352 if (LL == RR && LR == RL) {
1353 Op1 = ISD::getSetCCSwappedOperands(Op1);
1354 std::swap(RL, RR);
1355 }
1356 if (LL == RL && LR == RR) {
1357 bool isInteger = MVT::isInteger(LL.getValueType());
1358 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1359 if (Result != ISD::SETCC_INVALID)
1360 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1361 }
1362 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001363
1364 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1365 if (N0.getOpcode() == N1.getOpcode()) {
1366 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1367 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001368 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001369
Nate Begemande996292006-02-03 22:24:05 +00001370 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1371 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001372 if (!MVT::isVector(VT) &&
1373 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001374 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001375 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001376 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001377 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001378 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001379 // If we zero all the possible extended bits, then we can turn this into
1380 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001381 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001382 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001383 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1384 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001385 LN0->getSrcValueOffset(), EVT,
1386 LN0->isVolatile(),
1387 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001388 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001389 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001390 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001391 }
1392 }
1393 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001394 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1395 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001396 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001397 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001398 // If we zero all the possible extended bits, then we can turn this into
1399 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001400 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001401 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001402 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1403 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001404 LN0->getSrcValueOffset(), EVT,
1405 LN0->isVolatile(),
1406 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001407 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001408 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001409 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001410 }
1411 }
Chris Lattner15045b62006-02-28 06:35:35 +00001412
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001413 // fold (and (load x), 255) -> (zextload x, i8)
1414 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001415 if (N1C && N0.getOpcode() == ISD::LOAD) {
1416 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1417 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Evan Cheng83060c52007-03-07 08:07:03 +00001418 LN0->getAddressingMode() == ISD::UNINDEXED &&
Evan Cheng466685d2006-10-09 20:57:25 +00001419 N0.hasOneUse()) {
1420 MVT::ValueType EVT, LoadedVT;
1421 if (N1C->getValue() == 255)
1422 EVT = MVT::i8;
1423 else if (N1C->getValue() == 65535)
1424 EVT = MVT::i16;
1425 else if (N1C->getValue() == ~0U)
1426 EVT = MVT::i32;
1427 else
1428 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001429
Evan Cheng2e49f092006-10-11 07:10:22 +00001430 LoadedVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001431 if (EVT != MVT::Other && LoadedVT > EVT &&
1432 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1433 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1434 // For big endian targets, we need to add an offset to the pointer to
1435 // load the correct bytes. For little endian systems, we merely need to
1436 // read fewer bytes from the same pointer.
1437 unsigned PtrOff =
1438 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1439 SDOperand NewPtr = LN0->getBasePtr();
1440 if (!TLI.isLittleEndian())
1441 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1442 DAG.getConstant(PtrOff, PtrType));
1443 AddToWorkList(NewPtr.Val);
1444 SDOperand Load =
1445 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001446 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
1447 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001448 AddToWorkList(N);
1449 CombineTo(N0.Val, Load, Load.getValue(1));
1450 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1451 }
Chris Lattner15045b62006-02-28 06:35:35 +00001452 }
1453 }
1454
Nate Begeman83e75ec2005-09-06 04:43:02 +00001455 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001456}
1457
Nate Begeman83e75ec2005-09-06 04:43:02 +00001458SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001459 SDOperand N0 = N->getOperand(0);
1460 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001461 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001462 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1463 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001464 MVT::ValueType VT = N1.getValueType();
1465 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001466
1467 // fold (or 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::OR, 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::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001473 // fold (or 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 Begeman1d4d4142005-09-01 00:19:25 +00001476 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001477 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001478 return N1;
1479 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001480 if (N1C &&
1481 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001482 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001483 // reassociate or
1484 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1485 if (ROR.Val != 0)
1486 return ROR;
1487 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1488 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001489 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001490 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1491 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1492 N1),
1493 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001494 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001495 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1496 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1497 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1498 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1499
1500 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1501 MVT::isInteger(LL.getValueType())) {
1502 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1503 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1504 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1505 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1506 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001507 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001508 return DAG.getSetCC(VT, ORNode, LR, Op1);
1509 }
1510 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1511 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1512 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1513 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1514 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001515 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001516 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1517 }
1518 }
1519 // canonicalize equivalent to ll == rl
1520 if (LL == RR && LR == RL) {
1521 Op1 = ISD::getSetCCSwappedOperands(Op1);
1522 std::swap(RL, RR);
1523 }
1524 if (LL == RL && LR == RR) {
1525 bool isInteger = MVT::isInteger(LL.getValueType());
1526 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1527 if (Result != ISD::SETCC_INVALID)
1528 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1529 }
1530 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001531
1532 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1533 if (N0.getOpcode() == N1.getOpcode()) {
1534 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1535 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001536 }
Chris Lattner516b9622006-09-14 20:50:57 +00001537
Chris Lattner1ec72732006-09-14 21:11:37 +00001538 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1539 if (N0.getOpcode() == ISD::AND &&
1540 N1.getOpcode() == ISD::AND &&
1541 N0.getOperand(1).getOpcode() == ISD::Constant &&
1542 N1.getOperand(1).getOpcode() == ISD::Constant &&
1543 // Don't increase # computations.
1544 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1545 // We can only do this xform if we know that bits from X that are set in C2
1546 // but not in C1 are already zero. Likewise for Y.
1547 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1548 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1549
1550 if (TLI.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1551 TLI.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
1552 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1553 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1554 }
1555 }
1556
1557
Chris Lattner516b9622006-09-14 20:50:57 +00001558 // See if this is some rotate idiom.
1559 if (SDNode *Rot = MatchRotate(N0, N1))
1560 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001561
Nate Begeman83e75ec2005-09-06 04:43:02 +00001562 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001563}
1564
Chris Lattner516b9622006-09-14 20:50:57 +00001565
1566/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1567static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1568 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001569 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001570 Mask = Op.getOperand(1);
1571 Op = Op.getOperand(0);
1572 } else {
1573 return false;
1574 }
1575 }
1576
1577 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1578 Shift = Op;
1579 return true;
1580 }
1581 return false;
1582}
1583
1584
1585// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1586// idioms for rotate, and if the target supports rotation instructions, generate
1587// a rot[lr].
1588SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1589 // Must be a legal type. Expanded an promoted things won't work with rotates.
1590 MVT::ValueType VT = LHS.getValueType();
1591 if (!TLI.isTypeLegal(VT)) return 0;
1592
1593 // The target must have at least one rotate flavor.
1594 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1595 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1596 if (!HasROTL && !HasROTR) return 0;
1597
1598 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1599 SDOperand LHSShift; // The shift.
1600 SDOperand LHSMask; // AND value if any.
1601 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1602 return 0; // Not part of a rotate.
1603
1604 SDOperand RHSShift; // The shift.
1605 SDOperand RHSMask; // AND value if any.
1606 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1607 return 0; // Not part of a rotate.
1608
1609 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1610 return 0; // Not shifting the same value.
1611
1612 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1613 return 0; // Shifts must disagree.
1614
1615 // Canonicalize shl to left side in a shl/srl pair.
1616 if (RHSShift.getOpcode() == ISD::SHL) {
1617 std::swap(LHS, RHS);
1618 std::swap(LHSShift, RHSShift);
1619 std::swap(LHSMask , RHSMask );
1620 }
1621
1622 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Scott Michelc9dc1142007-04-02 21:36:32 +00001623 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1624 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1625 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001626
1627 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1628 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001629 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1630 RHSShiftAmt.getOpcode() == ISD::Constant) {
1631 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
1632 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00001633 if ((LShVal + RShVal) != OpSizeInBits)
1634 return 0;
1635
1636 SDOperand Rot;
1637 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001638 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001639 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001640 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001641
1642 // If there is an AND of either shifted operand, apply it to the result.
1643 if (LHSMask.Val || RHSMask.Val) {
1644 uint64_t Mask = MVT::getIntVTBitMask(VT);
1645
1646 if (LHSMask.Val) {
1647 uint64_t RHSBits = (1ULL << LShVal)-1;
1648 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1649 }
1650 if (RHSMask.Val) {
1651 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1652 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1653 }
1654
1655 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1656 }
1657
1658 return Rot.Val;
1659 }
1660
1661 // If there is a mask here, and we have a variable shift, we can't be sure
1662 // that we're masking out the right stuff.
1663 if (LHSMask.Val || RHSMask.Val)
1664 return 0;
1665
1666 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1667 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001668 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
1669 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001670 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001671 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001672 if (SUBC->getValue() == OpSizeInBits)
1673 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001674 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001675 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001676 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001677 }
1678 }
1679
1680 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1681 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001682 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
1683 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001684 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001685 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001686 if (SUBC->getValue() == OpSizeInBits)
1687 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001688 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001689 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001690 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
1691 }
1692 }
1693
1694 // Look for sign/zext/any-extended cases:
1695 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1696 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1697 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
1698 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1699 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1700 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
1701 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
1702 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
1703 if (RExtOp0.getOpcode() == ISD::SUB &&
1704 RExtOp0.getOperand(1) == LExtOp0) {
1705 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
1706 // (rotr x, y)
1707 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
1708 // (rotl x, (sub 32, y))
1709 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
1710 if (SUBC->getValue() == OpSizeInBits) {
1711 if (HasROTL)
1712 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
1713 else
1714 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
1715 }
1716 }
1717 } else if (LExtOp0.getOpcode() == ISD::SUB &&
1718 RExtOp0 == LExtOp0.getOperand(1)) {
1719 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
1720 // (rotl x, y)
1721 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
1722 // (rotr x, (sub 32, y))
1723 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
1724 if (SUBC->getValue() == OpSizeInBits) {
1725 if (HasROTL)
1726 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
1727 else
1728 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
1729 }
1730 }
Chris Lattner516b9622006-09-14 20:50:57 +00001731 }
1732 }
1733
1734 return 0;
1735}
1736
1737
Nate Begeman83e75ec2005-09-06 04:43:02 +00001738SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001739 SDOperand N0 = N->getOperand(0);
1740 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001741 SDOperand LHS, RHS, CC;
1742 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1743 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001744 MVT::ValueType VT = N0.getValueType();
1745
1746 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001747 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001748 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001749 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001750 if (N0C && !N1C)
1751 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001752 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001753 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001754 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001755 // reassociate xor
1756 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1757 if (RXOR.Val != 0)
1758 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001759 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001760 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1761 bool isInt = MVT::isInteger(LHS.getValueType());
1762 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1763 isInt);
1764 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001765 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001766 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001767 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001768 assert(0 && "Unhandled SetCC Equivalent!");
1769 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001770 }
Nate Begeman99801192005-09-07 23:25:52 +00001771 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Chris Lattner734c91d2006-11-10 21:37:15 +00001772 if (N1C && N1C->getValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00001773 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001774 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001775 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1776 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001777 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1778 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001779 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001780 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001781 }
1782 }
Nate Begeman99801192005-09-07 23:25:52 +00001783 // fold !(x or y) -> (!x and !y) iff x or y are constants
1784 if (N1C && N1C->isAllOnesValue() &&
1785 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001786 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001787 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1788 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001789 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1790 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001791 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001792 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001793 }
1794 }
Nate Begeman223df222005-09-08 20:18:10 +00001795 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1796 if (N1C && N0.getOpcode() == ISD::XOR) {
1797 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1798 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1799 if (N00C)
1800 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1801 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1802 if (N01C)
1803 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1804 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1805 }
1806 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001807 if (N0 == N1) {
1808 if (!MVT::isVector(VT)) {
1809 return DAG.getConstant(0, VT);
1810 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1811 // Produce a vector of zeros.
Dan Gohman51eaa862007-06-14 22:58:02 +00001812 SDOperand El = DAG.getConstant(0, MVT::getVectorElementType(VT));
Chris Lattner4fbdd592006-03-28 19:11:05 +00001813 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001814 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001815 }
1816 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001817
1818 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1819 if (N0.getOpcode() == N1.getOpcode()) {
1820 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1821 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001822 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001823
Chris Lattner3e104b12006-04-08 04:15:24 +00001824 // Simplify the expression using non-local knowledge.
1825 if (!MVT::isVector(VT) &&
1826 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001827 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001828
Nate Begeman83e75ec2005-09-06 04:43:02 +00001829 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001830}
1831
Nate Begeman83e75ec2005-09-06 04:43:02 +00001832SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001833 SDOperand N0 = N->getOperand(0);
1834 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001835 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1836 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001837 MVT::ValueType VT = N0.getValueType();
1838 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1839
1840 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001841 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001842 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001843 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001844 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001845 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001846 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001847 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001848 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001849 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001850 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001851 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001852 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001853 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001854 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00001855 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001856 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001857 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001858 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001859 N0.getOperand(1).getOpcode() == ISD::Constant) {
1860 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001861 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001862 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001863 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001864 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001865 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001866 }
1867 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1868 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001869 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001870 N0.getOperand(1).getOpcode() == ISD::Constant) {
1871 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001872 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001873 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1874 DAG.getConstant(~0ULL << c1, VT));
1875 if (c2 > c1)
1876 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001877 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001878 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001879 return DAG.getNode(ISD::SRL, VT, Mask,
1880 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001881 }
1882 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001883 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001884 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001885 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1886 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001887}
1888
Nate Begeman83e75ec2005-09-06 04:43:02 +00001889SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001890 SDOperand N0 = N->getOperand(0);
1891 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001892 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1893 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001894 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001895
1896 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001897 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001898 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001899 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001900 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001901 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001902 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001903 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001904 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001905 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001906 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001907 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001908 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001909 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001910 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001911 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1912 // sext_inreg.
1913 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1914 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1915 MVT::ValueType EVT;
1916 switch (LowBits) {
1917 default: EVT = MVT::Other; break;
1918 case 1: EVT = MVT::i1; break;
1919 case 8: EVT = MVT::i8; break;
1920 case 16: EVT = MVT::i16; break;
1921 case 32: EVT = MVT::i32; break;
1922 }
1923 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1924 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1925 DAG.getValueType(EVT));
1926 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001927
1928 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1929 if (N1C && N0.getOpcode() == ISD::SRA) {
1930 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1931 unsigned Sum = N1C->getValue() + C1->getValue();
1932 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1933 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1934 DAG.getConstant(Sum, N1C->getValueType(0)));
1935 }
1936 }
1937
Chris Lattnera8504462006-05-08 20:51:54 +00001938 // Simplify, based on bits shifted out of the LHS.
1939 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
1940 return SDOperand(N, 0);
1941
1942
Nate Begeman1d4d4142005-09-01 00:19:25 +00001943 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001944 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001945 return DAG.getNode(ISD::SRL, VT, N0, N1);
1946 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001947}
1948
Nate Begeman83e75ec2005-09-06 04:43:02 +00001949SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001950 SDOperand N0 = N->getOperand(0);
1951 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001952 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1953 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001954 MVT::ValueType VT = N0.getValueType();
1955 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1956
1957 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001958 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001959 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001960 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001961 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001962 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001963 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001964 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001965 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001966 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001967 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001968 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001969 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001970 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001971 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00001972
Nate Begeman1d4d4142005-09-01 00:19:25 +00001973 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001974 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001975 N0.getOperand(1).getOpcode() == ISD::Constant) {
1976 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001977 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001978 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001979 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001980 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001981 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001982 }
Chris Lattner350bec02006-04-02 06:11:11 +00001983
Chris Lattner06afe072006-05-05 22:53:17 +00001984 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
1985 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1986 // Shifting in all undef bits?
1987 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
1988 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
1989 return DAG.getNode(ISD::UNDEF, VT);
1990
1991 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
1992 AddToWorkList(SmallShift.Val);
1993 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
1994 }
1995
Chris Lattner3657ffe2006-10-12 20:23:19 +00001996 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
1997 // bit, which is unmodified by sra.
1998 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
1999 if (N0.getOpcode() == ISD::SRA)
2000 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2001 }
2002
Chris Lattner350bec02006-04-02 06:11:11 +00002003 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2004 if (N1C && N0.getOpcode() == ISD::CTLZ &&
2005 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
2006 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
2007 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
2008
2009 // If any of the input bits are KnownOne, then the input couldn't be all
2010 // zeros, thus the result of the srl will always be zero.
2011 if (KnownOne) return DAG.getConstant(0, VT);
2012
2013 // If all of the bits input the to ctlz node are known to be zero, then
2014 // the result of the ctlz is "32" and the result of the shift is one.
2015 uint64_t UnknownBits = ~KnownZero & Mask;
2016 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2017
2018 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2019 if ((UnknownBits & (UnknownBits-1)) == 0) {
2020 // Okay, we know that only that the single bit specified by UnknownBits
2021 // could be set on input to the CTLZ node. If this bit is set, the SRL
2022 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2023 // to an SRL,XOR pair, which is likely to simplify more.
2024 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
2025 SDOperand Op = N0.getOperand(0);
2026 if (ShAmt) {
2027 Op = DAG.getNode(ISD::SRL, VT, Op,
2028 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2029 AddToWorkList(Op.Val);
2030 }
2031 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2032 }
2033 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002034
2035 // fold operands of srl based on knowledge that the low bits are not
2036 // demanded.
2037 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2038 return SDOperand(N, 0);
2039
Nate Begeman83e75ec2005-09-06 04:43:02 +00002040 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002041}
2042
Nate Begeman83e75ec2005-09-06 04:43:02 +00002043SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002044 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002045 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002046
2047 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002048 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002049 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002050 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002051}
2052
Nate Begeman83e75ec2005-09-06 04:43:02 +00002053SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002054 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002055 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002056
2057 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002058 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002059 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002060 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002061}
2062
Nate Begeman83e75ec2005-09-06 04:43:02 +00002063SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002064 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002065 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002066
2067 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002068 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002069 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002070 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002071}
2072
Nate Begeman452d7be2005-09-16 00:54:12 +00002073SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2074 SDOperand N0 = N->getOperand(0);
2075 SDOperand N1 = N->getOperand(1);
2076 SDOperand N2 = N->getOperand(2);
2077 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2078 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2079 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2080 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00002081
Nate Begeman452d7be2005-09-16 00:54:12 +00002082 // fold select C, X, X -> X
2083 if (N1 == N2)
2084 return N1;
2085 // fold select true, X, Y -> X
2086 if (N0C && !N0C->isNullValue())
2087 return N1;
2088 // fold select false, X, Y -> Y
2089 if (N0C && N0C->isNullValue())
2090 return N2;
2091 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002092 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002093 return DAG.getNode(ISD::OR, VT, N0, N2);
2094 // fold select C, 0, X -> ~C & X
2095 // FIXME: this should check for C type == X type, not i1?
2096 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
2097 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002098 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002099 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2100 }
2101 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002102 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002103 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002104 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002105 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2106 }
2107 // fold select C, X, 0 -> C & X
2108 // FIXME: this should check for C type == X type, not i1?
2109 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2110 return DAG.getNode(ISD::AND, VT, N0, N1);
2111 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2112 if (MVT::i1 == VT && N0 == N1)
2113 return DAG.getNode(ISD::OR, VT, N0, N2);
2114 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2115 if (MVT::i1 == VT && N0 == N2)
2116 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002117
Chris Lattner40c62d52005-10-18 06:04:22 +00002118 // If we can fold this based on the true/false value, do so.
2119 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002120 return SDOperand(N, 0); // Don't revisit N.
2121
Nate Begeman44728a72005-09-19 22:34:01 +00002122 // fold selects based on a setcc into other things, such as min/max/abs
2123 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00002124 // FIXME:
2125 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2126 // having to say they don't support SELECT_CC on every type the DAG knows
2127 // about, since there is no way to mark an opcode illegal at all value types
2128 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2129 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2130 N1, N2, N0.getOperand(2));
2131 else
2132 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002133 return SDOperand();
2134}
2135
2136SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002137 SDOperand N0 = N->getOperand(0);
2138 SDOperand N1 = N->getOperand(1);
2139 SDOperand N2 = N->getOperand(2);
2140 SDOperand N3 = N->getOperand(3);
2141 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002142 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2143
Nate Begeman44728a72005-09-19 22:34:01 +00002144 // fold select_cc lhs, rhs, x, x, cc -> x
2145 if (N2 == N3)
2146 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002147
Chris Lattner5f42a242006-09-20 06:19:26 +00002148 // Determine if the condition we're dealing with is constant
2149 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002150 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002151
2152 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
2153 if (SCCC->getValue())
2154 return N2; // cond always true -> true val
2155 else
2156 return N3; // cond always false -> false val
2157 }
2158
2159 // Fold to a simpler select_cc
2160 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2161 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2162 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2163 SCC.getOperand(2));
2164
Chris Lattner40c62d52005-10-18 06:04:22 +00002165 // If we can fold this based on the true/false value, do so.
2166 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002167 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002168
Nate Begeman44728a72005-09-19 22:34:01 +00002169 // fold select_cc into other things, such as min/max/abs
2170 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002171}
2172
2173SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2174 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2175 cast<CondCodeSDNode>(N->getOperand(2))->get());
2176}
2177
Nate Begeman83e75ec2005-09-06 04:43:02 +00002178SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002179 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002180 MVT::ValueType VT = N->getValueType(0);
2181
Nate Begeman1d4d4142005-09-01 00:19:25 +00002182 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002183 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002184 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002185
Nate Begeman1d4d4142005-09-01 00:19:25 +00002186 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002187 // fold (sext (aext x)) -> (sext x)
2188 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002189 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002190
Evan Chengc88138f2007-03-22 01:54:19 +00002191 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2192 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Chris Lattner22558872007-02-26 03:13:59 +00002193 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002194 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002195 if (NarrowLoad.Val) {
2196 if (NarrowLoad.Val != N0.Val)
2197 CombineTo(N0.Val, NarrowLoad);
2198 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2199 }
Evan Chengc88138f2007-03-22 01:54:19 +00002200 }
2201
2202 // See if the value being truncated is already sign extended. If so, just
2203 // eliminate the trunc/sext pair.
2204 if (N0.getOpcode() == ISD::TRUNCATE) {
Chris Lattner6007b842006-09-21 06:00:20 +00002205 SDOperand Op = N0.getOperand(0);
Chris Lattner22558872007-02-26 03:13:59 +00002206 unsigned OpBits = MVT::getSizeInBits(Op.getValueType());
2207 unsigned MidBits = MVT::getSizeInBits(N0.getValueType());
2208 unsigned DestBits = MVT::getSizeInBits(VT);
2209 unsigned NumSignBits = TLI.ComputeNumSignBits(Op);
2210
2211 if (OpBits == DestBits) {
2212 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2213 // bits, it is already ready.
2214 if (NumSignBits > DestBits-MidBits)
2215 return Op;
2216 } else if (OpBits < DestBits) {
2217 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2218 // bits, just sext from i32.
2219 if (NumSignBits > OpBits-MidBits)
2220 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2221 } else {
2222 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2223 // bits, just truncate to i32.
2224 if (NumSignBits > OpBits-MidBits)
2225 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002226 }
Chris Lattner22558872007-02-26 03:13:59 +00002227
2228 // fold (sext (truncate x)) -> (sextinreg x).
2229 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2230 N0.getValueType())) {
2231 if (Op.getValueType() < VT)
2232 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2233 else if (Op.getValueType() > VT)
2234 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2235 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2236 DAG.getValueType(N0.getValueType()));
2237 }
Chris Lattner6007b842006-09-21 06:00:20 +00002238 }
Chris Lattner310b5782006-05-06 23:06:26 +00002239
Evan Cheng110dec22005-12-14 02:19:23 +00002240 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002241 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002242 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng466685d2006-10-09 20:57:25 +00002243 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2244 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2245 LN0->getBasePtr(), LN0->getSrcValue(),
2246 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002247 N0.getValueType(),
2248 LN0->isVolatile());
Chris Lattnerd4771842005-12-14 19:25:30 +00002249 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00002250 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2251 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002252 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002253 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002254
2255 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2256 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002257 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2258 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002259 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002260 MVT::ValueType EVT = LN0->getLoadedVT();
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002261 if (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
2262 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2263 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002264 LN0->getSrcValueOffset(), EVT,
2265 LN0->isVolatile(),
2266 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002267 CombineTo(N, ExtLoad);
2268 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2269 ExtLoad.getValue(1));
2270 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2271 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002272 }
2273
Chris Lattner20a35c32007-04-11 05:32:27 +00002274 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2275 if (N0.getOpcode() == ISD::SETCC) {
2276 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002277 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2278 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2279 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2280 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002281 }
2282
Nate Begeman83e75ec2005-09-06 04:43:02 +00002283 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002284}
2285
Nate Begeman83e75ec2005-09-06 04:43:02 +00002286SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002287 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002288 MVT::ValueType VT = N->getValueType(0);
2289
Nate Begeman1d4d4142005-09-01 00:19:25 +00002290 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002291 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002292 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002293 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002294 // fold (zext (aext x)) -> (zext x)
2295 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002296 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002297
Evan Chengc88138f2007-03-22 01:54:19 +00002298 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2299 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002300 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002301 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002302 if (NarrowLoad.Val) {
2303 if (NarrowLoad.Val != N0.Val)
2304 CombineTo(N0.Val, NarrowLoad);
2305 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2306 }
Evan Chengc88138f2007-03-22 01:54:19 +00002307 }
2308
Chris Lattner6007b842006-09-21 06:00:20 +00002309 // fold (zext (truncate x)) -> (and x, mask)
2310 if (N0.getOpcode() == ISD::TRUNCATE &&
2311 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2312 SDOperand Op = N0.getOperand(0);
2313 if (Op.getValueType() < VT) {
2314 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2315 } else if (Op.getValueType() > VT) {
2316 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2317 }
2318 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2319 }
2320
Chris Lattner111c2282006-09-21 06:14:31 +00002321 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2322 if (N0.getOpcode() == ISD::AND &&
2323 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2324 N0.getOperand(1).getOpcode() == ISD::Constant) {
2325 SDOperand X = N0.getOperand(0).getOperand(0);
2326 if (X.getValueType() < VT) {
2327 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2328 } else if (X.getValueType() > VT) {
2329 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2330 }
2331 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2332 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2333 }
2334
Evan Cheng110dec22005-12-14 02:19:23 +00002335 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002336 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002337 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002338 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2339 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2340 LN0->getBasePtr(), LN0->getSrcValue(),
2341 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002342 N0.getValueType(),
2343 LN0->isVolatile(),
2344 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002345 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00002346 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2347 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002348 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00002349 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002350
2351 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2352 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002353 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2354 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002355 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002356 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002357 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2358 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002359 LN0->getSrcValueOffset(), EVT,
2360 LN0->isVolatile(),
2361 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002362 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002363 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2364 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002365 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002366 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002367
2368 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2369 if (N0.getOpcode() == ISD::SETCC) {
2370 SDOperand SCC =
2371 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2372 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00002373 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2374 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002375 }
2376
Nate Begeman83e75ec2005-09-06 04:43:02 +00002377 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002378}
2379
Chris Lattner5ffc0662006-05-05 05:58:59 +00002380SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2381 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002382 MVT::ValueType VT = N->getValueType(0);
2383
2384 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002385 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002386 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2387 // fold (aext (aext x)) -> (aext x)
2388 // fold (aext (zext x)) -> (zext x)
2389 // fold (aext (sext x)) -> (sext x)
2390 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2391 N0.getOpcode() == ISD::ZERO_EXTEND ||
2392 N0.getOpcode() == ISD::SIGN_EXTEND)
2393 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2394
Evan Chengc88138f2007-03-22 01:54:19 +00002395 // fold (aext (truncate (load x))) -> (aext (smaller load x))
2396 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
2397 if (N0.getOpcode() == ISD::TRUNCATE) {
2398 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002399 if (NarrowLoad.Val) {
2400 if (NarrowLoad.Val != N0.Val)
2401 CombineTo(N0.Val, NarrowLoad);
2402 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
2403 }
Evan Chengc88138f2007-03-22 01:54:19 +00002404 }
2405
Chris Lattner84750582006-09-20 06:29:17 +00002406 // fold (aext (truncate x))
2407 if (N0.getOpcode() == ISD::TRUNCATE) {
2408 SDOperand TruncOp = N0.getOperand(0);
2409 if (TruncOp.getValueType() == VT)
2410 return TruncOp; // x iff x size == zext size.
2411 if (TruncOp.getValueType() > VT)
2412 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2413 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2414 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002415
2416 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2417 if (N0.getOpcode() == ISD::AND &&
2418 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2419 N0.getOperand(1).getOpcode() == ISD::Constant) {
2420 SDOperand X = N0.getOperand(0).getOperand(0);
2421 if (X.getValueType() < VT) {
2422 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2423 } else if (X.getValueType() > VT) {
2424 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2425 }
2426 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2427 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2428 }
2429
Chris Lattner5ffc0662006-05-05 05:58:59 +00002430 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002431 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002432 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002433 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2434 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2435 LN0->getBasePtr(), LN0->getSrcValue(),
2436 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002437 N0.getValueType(),
2438 LN0->isVolatile(),
2439 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002440 CombineTo(N, ExtLoad);
2441 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2442 ExtLoad.getValue(1));
2443 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2444 }
2445
2446 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2447 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2448 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002449 if (N0.getOpcode() == ISD::LOAD &&
2450 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00002451 N0.hasOneUse()) {
2452 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002453 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002454 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
2455 LN0->getChain(), LN0->getBasePtr(),
2456 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002457 LN0->getSrcValueOffset(), EVT,
2458 LN0->isVolatile(),
2459 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002460 CombineTo(N, ExtLoad);
2461 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2462 ExtLoad.getValue(1));
2463 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2464 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002465
2466 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2467 if (N0.getOpcode() == ISD::SETCC) {
2468 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002469 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2470 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00002471 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00002472 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00002473 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002474 }
2475
Chris Lattner5ffc0662006-05-05 05:58:59 +00002476 return SDOperand();
2477}
2478
Evan Chengc88138f2007-03-22 01:54:19 +00002479/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
2480/// bits and then truncated to a narrower type and where N is a multiple
2481/// of number of bits of the narrower type, transform it to a narrower load
2482/// from address + N / num of bits of new type. If the result is to be
2483/// extended, also fold the extension to form a extending load.
2484SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
2485 unsigned Opc = N->getOpcode();
2486 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
2487 SDOperand N0 = N->getOperand(0);
2488 MVT::ValueType VT = N->getValueType(0);
2489 MVT::ValueType EVT = N->getValueType(0);
2490
Evan Chenge177e302007-03-23 22:13:36 +00002491 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
2492 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00002493 if (Opc == ISD::SIGN_EXTEND_INREG) {
2494 ExtType = ISD::SEXTLOAD;
2495 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00002496 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
2497 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00002498 }
2499
2500 unsigned EVTBits = MVT::getSizeInBits(EVT);
2501 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00002502 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00002503 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
2504 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2505 ShAmt = N01->getValue();
2506 // Is the shift amount a multiple of size of VT?
2507 if ((ShAmt & (EVTBits-1)) == 0) {
2508 N0 = N0.getOperand(0);
2509 if (MVT::getSizeInBits(N0.getValueType()) <= EVTBits)
2510 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00002511 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00002512 }
2513 }
2514 }
2515
2516 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
2517 // Do not allow folding to i1 here. i1 is implicitly stored in memory in
2518 // zero extended form: by shrinking the load, we lose track of the fact
2519 // that it is already zero extended.
2520 // FIXME: This should be reevaluated.
2521 VT != MVT::i1) {
2522 assert(MVT::getSizeInBits(N0.getValueType()) > EVTBits &&
2523 "Cannot truncate to larger type!");
2524 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2525 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00002526 // For big endian targets, we need to adjust the offset to the pointer to
2527 // load the correct bytes.
2528 if (!TLI.isLittleEndian())
2529 ShAmt = MVT::getSizeInBits(N0.getValueType()) - ShAmt - EVTBits;
2530 uint64_t PtrOff = ShAmt / 8;
Evan Chengc88138f2007-03-22 01:54:19 +00002531 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
2532 DAG.getConstant(PtrOff, PtrType));
2533 AddToWorkList(NewPtr.Val);
2534 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
2535 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002536 LN0->getSrcValue(), LN0->getSrcValueOffset(),
2537 LN0->isVolatile(), LN0->getAlignment())
Evan Chengc88138f2007-03-22 01:54:19 +00002538 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002539 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
2540 LN0->isVolatile(), LN0->getAlignment());
Evan Chengc88138f2007-03-22 01:54:19 +00002541 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00002542 if (CombineSRL) {
2543 std::vector<SDNode*> NowDead;
2544 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1), NowDead);
2545 CombineTo(N->getOperand(0).Val, Load);
2546 } else
2547 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00002548 if (ShAmt) {
2549 if (Opc == ISD::SIGN_EXTEND_INREG)
2550 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
2551 else
2552 return DAG.getNode(Opc, VT, Load);
2553 }
Evan Chengc88138f2007-03-22 01:54:19 +00002554 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2555 }
2556
2557 return SDOperand();
2558}
2559
Chris Lattner5ffc0662006-05-05 05:58:59 +00002560
Nate Begeman83e75ec2005-09-06 04:43:02 +00002561SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002562 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002563 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002564 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002565 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00002566 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002567
Nate Begeman1d4d4142005-09-01 00:19:25 +00002568 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00002569 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00002570 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00002571
Chris Lattner541a24f2006-05-06 22:43:44 +00002572 // If the input is already sign extended, just drop the extension.
Chris Lattneree4ea922006-05-06 09:30:03 +00002573 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
2574 return N0;
2575
Nate Begeman646d7e22005-09-02 21:18:40 +00002576 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
2577 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2578 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00002579 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002580 }
Chris Lattner4b37e872006-05-08 21:18:59 +00002581
Chris Lattner95a5e052007-04-17 19:03:21 +00002582 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00002583 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00002584 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00002585
Chris Lattner95a5e052007-04-17 19:03:21 +00002586 // fold operands of sext_in_reg based on knowledge that the top bits are not
2587 // demanded.
2588 if (SimplifyDemandedBits(SDOperand(N, 0)))
2589 return SDOperand(N, 0);
2590
Evan Chengc88138f2007-03-22 01:54:19 +00002591 // fold (sext_in_reg (load x)) -> (smaller sextload x)
2592 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
2593 SDOperand NarrowLoad = ReduceLoadWidth(N);
2594 if (NarrowLoad.Val)
2595 return NarrowLoad;
2596
Chris Lattner4b37e872006-05-08 21:18:59 +00002597 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
2598 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
2599 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
2600 if (N0.getOpcode() == ISD::SRL) {
2601 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2602 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
2603 // We can turn this into an SRA iff the input to the SRL is already sign
2604 // extended enough.
2605 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
2606 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
2607 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
2608 }
2609 }
Evan Chengc88138f2007-03-22 01:54:19 +00002610
Nate Begemanded49632005-10-13 03:11:28 +00002611 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00002612 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002613 ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002614 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002615 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002616 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2617 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2618 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002619 LN0->getSrcValueOffset(), EVT,
2620 LN0->isVolatile(),
2621 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002622 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002623 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002624 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002625 }
2626 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00002627 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
2628 N0.hasOneUse() &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002629 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002630 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002631 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2632 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2633 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002634 LN0->getSrcValueOffset(), EVT,
2635 LN0->isVolatile(),
2636 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002637 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002638 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002639 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002640 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002641 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002642}
2643
Nate Begeman83e75ec2005-09-06 04:43:02 +00002644SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002645 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002646 MVT::ValueType VT = N->getValueType(0);
2647
2648 // noop truncate
2649 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002650 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002651 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002652 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002653 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002654 // fold (truncate (truncate x)) -> (truncate x)
2655 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002656 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002657 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00002658 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2659 N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner32ba1aa2006-11-20 18:05:46 +00002660 if (N0.getOperand(0).getValueType() < VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002661 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00002662 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Chris Lattner32ba1aa2006-11-20 18:05:46 +00002663 else if (N0.getOperand(0).getValueType() > VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002664 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002665 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002666 else
2667 // if the source and dest are the same type, we can drop both the extend
2668 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002669 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002670 }
Evan Cheng007b69e2007-03-21 20:14:05 +00002671
Nate Begeman3df4d522005-10-12 20:40:40 +00002672 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00002673 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00002674 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002675}
2676
Chris Lattner94683772005-12-23 05:30:37 +00002677SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2678 SDOperand N0 = N->getOperand(0);
2679 MVT::ValueType VT = N->getValueType(0);
2680
2681 // If the input is a constant, let getNode() fold it.
2682 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2683 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2684 if (Res.Val != N) return Res;
2685 }
2686
Chris Lattnerc8547d82005-12-23 05:37:50 +00002687 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2688 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00002689
Chris Lattner57104102005-12-23 05:44:41 +00002690 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng59d5b682007-05-07 21:27:48 +00002691 // If the resultant load doesn't need a higher alignment than the original!
2692 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Dale Johannesen98a6c622007-05-16 22:45:30 +00002693 ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng59d5b682007-05-07 21:27:48 +00002694 TLI.isOperationLegal(ISD::LOAD, VT)) {
Evan Cheng466685d2006-10-09 20:57:25 +00002695 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00002696 unsigned Align = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00002697 getABITypeAlignment(MVT::getTypeForValueType(VT));
Evan Cheng59d5b682007-05-07 21:27:48 +00002698 unsigned OrigAlign = LN0->getAlignment();
2699 if (Align <= OrigAlign) {
2700 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
2701 LN0->getSrcValue(), LN0->getSrcValueOffset(),
2702 LN0->isVolatile(), LN0->getAlignment());
2703 AddToWorkList(N);
2704 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2705 Load.getValue(1));
2706 return Load;
2707 }
Chris Lattner57104102005-12-23 05:44:41 +00002708 }
2709
Chris Lattner94683772005-12-23 05:30:37 +00002710 return SDOperand();
2711}
2712
Chris Lattner6258fb22006-04-02 02:53:43 +00002713SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2714 SDOperand N0 = N->getOperand(0);
2715 MVT::ValueType VT = N->getValueType(0);
2716
2717 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2718 // First check to see if this is all constant.
2719 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2720 VT == MVT::Vector) {
2721 bool isSimple = true;
2722 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2723 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2724 N0.getOperand(i).getOpcode() != ISD::Constant &&
2725 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2726 isSimple = false;
2727 break;
2728 }
2729
Chris Lattner97c20732006-04-03 17:29:28 +00002730 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2731 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002732 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2733 }
2734 }
2735
2736 return SDOperand();
2737}
2738
2739/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2740/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2741/// destination element value type.
2742SDOperand DAGCombiner::
2743ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2744 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2745
2746 // If this is already the right type, we're done.
2747 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2748
2749 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2750 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2751
2752 // If this is a conversion of N elements of one type to N elements of another
2753 // type, convert each element. This handles FP<->INT cases.
2754 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002755 SmallVector<SDOperand, 8> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00002756 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002757 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002758 AddToWorkList(Ops.back().Val);
2759 }
Chris Lattner6258fb22006-04-02 02:53:43 +00002760 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2761 Ops.push_back(DAG.getValueType(DstEltVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002762 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002763 }
2764
2765 // Otherwise, we're growing or shrinking the elements. To avoid having to
2766 // handle annoying details of growing/shrinking FP values, we convert them to
2767 // int first.
2768 if (MVT::isFloatingPoint(SrcEltVT)) {
2769 // Convert the input float vector to a int vector where the elements are the
2770 // same sizes.
2771 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2772 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2773 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2774 SrcEltVT = IntVT;
2775 }
2776
2777 // Now we know the input is an integer vector. If the output is a FP type,
2778 // convert to integer first, then to FP of the right size.
2779 if (MVT::isFloatingPoint(DstEltVT)) {
2780 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2781 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2782 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2783
2784 // Next, convert to FP elements of the same size.
2785 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2786 }
2787
2788 // Okay, we know the src/dst types are both integers of differing types.
2789 // Handling growing first.
2790 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2791 if (SrcBitSize < DstBitSize) {
2792 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2793
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002794 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002795 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2796 i += NumInputsPerOutput) {
2797 bool isLE = TLI.isLittleEndian();
2798 uint64_t NewBits = 0;
2799 bool EltIsUndef = true;
2800 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2801 // Shift the previously computed bits over.
2802 NewBits <<= SrcBitSize;
2803 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2804 if (Op.getOpcode() == ISD::UNDEF) continue;
2805 EltIsUndef = false;
2806
2807 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2808 }
2809
2810 if (EltIsUndef)
2811 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2812 else
2813 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2814 }
2815
2816 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2817 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002818 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002819 }
2820
2821 // Finally, this must be the case where we are shrinking elements: each input
2822 // turns into multiple outputs.
2823 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002824 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002825 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2826 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2827 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2828 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2829 continue;
2830 }
2831 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2832
2833 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2834 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2835 OpVal >>= DstBitSize;
2836 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2837 }
2838
2839 // For big endian targets, swap the order of the pieces of each element.
2840 if (!TLI.isLittleEndian())
2841 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2842 }
2843 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2844 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002845 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002846}
2847
2848
2849
Chris Lattner01b3d732005-09-28 22:28:18 +00002850SDOperand DAGCombiner::visitFADD(SDNode *N) {
2851 SDOperand N0 = N->getOperand(0);
2852 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002853 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2854 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002855 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002856
2857 // fold (fadd c1, c2) -> c1+c2
2858 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002859 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002860 // canonicalize constant to RHS
2861 if (N0CFP && !N1CFP)
2862 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002863 // fold (A + (-B)) -> A-B
Chris Lattner29446522007-05-14 22:04:50 +00002864 if (isNegatibleForFree(N1) == 2)
2865 return DAG.getNode(ISD::FSUB, VT, N0, GetNegatedExpression(N1, DAG));
Chris Lattner01b3d732005-09-28 22:28:18 +00002866 // fold ((-A) + B) -> B-A
Chris Lattner29446522007-05-14 22:04:50 +00002867 if (isNegatibleForFree(N0) == 2)
2868 return DAG.getNode(ISD::FSUB, VT, N1, GetNegatedExpression(N0, DAG));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00002869
2870 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
2871 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
2872 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
2873 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
2874 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
2875
Chris Lattner01b3d732005-09-28 22:28:18 +00002876 return SDOperand();
2877}
2878
2879SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2880 SDOperand N0 = N->getOperand(0);
2881 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002882 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2883 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002884 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002885
2886 // fold (fsub c1, c2) -> c1-c2
2887 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002888 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002889 // fold (A-(-B)) -> A+B
Chris Lattner29446522007-05-14 22:04:50 +00002890 if (isNegatibleForFree(N1))
2891 return DAG.getNode(ISD::FADD, VT, N0, GetNegatedExpression(N1, DAG));
2892
Chris Lattner01b3d732005-09-28 22:28:18 +00002893 return SDOperand();
2894}
2895
2896SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2897 SDOperand N0 = N->getOperand(0);
2898 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002899 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2900 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002901 MVT::ValueType VT = N->getValueType(0);
2902
Nate Begeman11af4ea2005-10-17 20:40:11 +00002903 // fold (fmul c1, c2) -> c1*c2
2904 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002905 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002906 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002907 if (N0CFP && !N1CFP)
2908 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002909 // fold (fmul X, 2.0) -> (fadd X, X)
2910 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2911 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00002912 // fold (fmul X, -1.0) -> (fneg X)
2913 if (N1CFP && N1CFP->isExactlyValue(-1.0))
2914 return DAG.getNode(ISD::FNEG, VT, N0);
2915
2916 // -X * -Y -> X*Y
2917 if (char LHSNeg = isNegatibleForFree(N0)) {
2918 if (char RHSNeg = isNegatibleForFree(N1)) {
2919 // Both can be negated for free, check to see if at least one is cheaper
2920 // negated.
2921 if (LHSNeg == 2 || RHSNeg == 2)
2922 return DAG.getNode(ISD::FMUL, VT, GetNegatedExpression(N0, DAG),
2923 GetNegatedExpression(N1, DAG));
2924 }
2925 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00002926
2927 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
2928 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
2929 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
2930 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
2931 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
2932
Chris Lattner01b3d732005-09-28 22:28:18 +00002933 return SDOperand();
2934}
2935
2936SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2937 SDOperand N0 = N->getOperand(0);
2938 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002939 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2940 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002941 MVT::ValueType VT = N->getValueType(0);
2942
Nate Begemana148d982006-01-18 22:35:16 +00002943 // fold (fdiv c1, c2) -> c1/c2
2944 if (N0CFP && N1CFP)
2945 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00002946
2947
2948 // -X / -Y -> X*Y
2949 if (char LHSNeg = isNegatibleForFree(N0)) {
2950 if (char RHSNeg = isNegatibleForFree(N1)) {
2951 // Both can be negated for free, check to see if at least one is cheaper
2952 // negated.
2953 if (LHSNeg == 2 || RHSNeg == 2)
2954 return DAG.getNode(ISD::FDIV, VT, GetNegatedExpression(N0, DAG),
2955 GetNegatedExpression(N1, DAG));
2956 }
2957 }
2958
Chris Lattner01b3d732005-09-28 22:28:18 +00002959 return SDOperand();
2960}
2961
2962SDOperand DAGCombiner::visitFREM(SDNode *N) {
2963 SDOperand N0 = N->getOperand(0);
2964 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002965 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2966 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002967 MVT::ValueType VT = N->getValueType(0);
2968
Nate Begemana148d982006-01-18 22:35:16 +00002969 // fold (frem c1, c2) -> fmod(c1,c2)
2970 if (N0CFP && N1CFP)
2971 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002972 return SDOperand();
2973}
2974
Chris Lattner12d83032006-03-05 05:30:57 +00002975SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2976 SDOperand N0 = N->getOperand(0);
2977 SDOperand N1 = N->getOperand(1);
2978 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2979 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2980 MVT::ValueType VT = N->getValueType(0);
2981
2982 if (N0CFP && N1CFP) // Constant fold
2983 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2984
2985 if (N1CFP) {
2986 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2987 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2988 union {
2989 double d;
2990 int64_t i;
2991 } u;
2992 u.d = N1CFP->getValue();
2993 if (u.i >= 0)
2994 return DAG.getNode(ISD::FABS, VT, N0);
2995 else
2996 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2997 }
2998
2999 // copysign(fabs(x), y) -> copysign(x, y)
3000 // copysign(fneg(x), y) -> copysign(x, y)
3001 // copysign(copysign(x,z), y) -> copysign(x, y)
3002 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3003 N0.getOpcode() == ISD::FCOPYSIGN)
3004 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3005
3006 // copysign(x, abs(y)) -> abs(x)
3007 if (N1.getOpcode() == ISD::FABS)
3008 return DAG.getNode(ISD::FABS, VT, N0);
3009
3010 // copysign(x, copysign(y,z)) -> copysign(x, z)
3011 if (N1.getOpcode() == ISD::FCOPYSIGN)
3012 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3013
3014 // copysign(x, fp_extend(y)) -> copysign(x, y)
3015 // copysign(x, fp_round(y)) -> copysign(x, y)
3016 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3017 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3018
3019 return SDOperand();
3020}
3021
3022
Chris Lattner01b3d732005-09-28 22:28:18 +00003023
Nate Begeman83e75ec2005-09-06 04:43:02 +00003024SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003025 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003026 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003027 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003028
3029 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003030 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00003031 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003032 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003033}
3034
Nate Begeman83e75ec2005-09-06 04:43:02 +00003035SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003036 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003037 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003038 MVT::ValueType VT = N->getValueType(0);
3039
Nate Begeman1d4d4142005-09-01 00:19:25 +00003040 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003041 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00003042 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003043 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003044}
3045
Nate Begeman83e75ec2005-09-06 04:43:02 +00003046SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003047 SDOperand N0 = N->getOperand(0);
3048 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3049 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003050
3051 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003052 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003053 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003054 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003055}
3056
Nate Begeman83e75ec2005-09-06 04:43:02 +00003057SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003058 SDOperand N0 = N->getOperand(0);
3059 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3060 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003061
3062 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003063 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003064 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003065 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003066}
3067
Nate Begeman83e75ec2005-09-06 04:43:02 +00003068SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003069 SDOperand N0 = N->getOperand(0);
3070 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3071 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003072
3073 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003074 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003075 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00003076
3077 // fold (fp_round (fp_extend x)) -> x
3078 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3079 return N0.getOperand(0);
3080
3081 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3082 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
3083 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
3084 AddToWorkList(Tmp.Val);
3085 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3086 }
3087
Nate Begeman83e75ec2005-09-06 04:43:02 +00003088 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003089}
3090
Nate Begeman83e75ec2005-09-06 04:43:02 +00003091SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003092 SDOperand N0 = N->getOperand(0);
3093 MVT::ValueType VT = N->getValueType(0);
3094 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003095 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003096
Nate Begeman1d4d4142005-09-01 00:19:25 +00003097 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003098 if (N0CFP) {
3099 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003100 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003101 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003102 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003103}
3104
Nate Begeman83e75ec2005-09-06 04:43:02 +00003105SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003106 SDOperand N0 = N->getOperand(0);
3107 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3108 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003109
3110 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003111 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003112 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00003113
3114 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00003115 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003116 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003117 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3118 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3119 LN0->getBasePtr(), LN0->getSrcValue(),
3120 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003121 N0.getValueType(),
3122 LN0->isVolatile(),
3123 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003124 CombineTo(N, ExtLoad);
3125 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
3126 ExtLoad.getValue(1));
3127 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3128 }
3129
3130
Nate Begeman83e75ec2005-09-06 04:43:02 +00003131 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003132}
3133
Nate Begeman83e75ec2005-09-06 04:43:02 +00003134SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003135 SDOperand N0 = N->getOperand(0);
3136 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3137 MVT::ValueType VT = N->getValueType(0);
3138
3139 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003140 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003141 return DAG.getNode(ISD::FNEG, VT, N0);
3142 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00003143 if (N0.getOpcode() == ISD::SUB)
3144 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00003145 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00003146 if (N0.getOpcode() == ISD::FNEG)
3147 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003148 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003149}
3150
Nate Begeman83e75ec2005-09-06 04:43:02 +00003151SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003152 SDOperand N0 = N->getOperand(0);
3153 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3154 MVT::ValueType VT = N->getValueType(0);
3155
Nate Begeman1d4d4142005-09-01 00:19:25 +00003156 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00003157 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003158 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003159 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003160 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003161 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003162 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003163 // fold (fabs (fcopysign x, y)) -> (fabs x)
3164 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
3165 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
3166
Nate Begeman83e75ec2005-09-06 04:43:02 +00003167 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003168}
3169
Nate Begeman44728a72005-09-19 22:34:01 +00003170SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
3171 SDOperand Chain = N->getOperand(0);
3172 SDOperand N1 = N->getOperand(1);
3173 SDOperand N2 = N->getOperand(2);
3174 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3175
3176 // never taken branch, fold to chain
3177 if (N1C && N1C->isNullValue())
3178 return Chain;
3179 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00003180 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00003181 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003182 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
3183 // on the target.
3184 if (N1.getOpcode() == ISD::SETCC &&
3185 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
3186 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
3187 N1.getOperand(0), N1.getOperand(1), N2);
3188 }
Nate Begeman44728a72005-09-19 22:34:01 +00003189 return SDOperand();
3190}
3191
Chris Lattner3ea0b472005-10-05 06:47:48 +00003192// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
3193//
Nate Begeman44728a72005-09-19 22:34:01 +00003194SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00003195 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
3196 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
3197
3198 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00003199 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003200 if (Simp.Val) AddToWorkList(Simp.Val);
3201
Nate Begemane17daeb2005-10-05 21:43:42 +00003202 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
3203
3204 // fold br_cc true, dest -> br dest (unconditional branch)
3205 if (SCCC && SCCC->getValue())
3206 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
3207 N->getOperand(4));
3208 // fold br_cc false, dest -> unconditional fall through
3209 if (SCCC && SCCC->isNullValue())
3210 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00003211
Nate Begemane17daeb2005-10-05 21:43:42 +00003212 // fold to a simpler setcc
3213 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
3214 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
3215 Simp.getOperand(2), Simp.getOperand(0),
3216 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00003217 return SDOperand();
3218}
3219
Chris Lattner448f2192006-11-11 00:39:41 +00003220
3221/// CombineToPreIndexedLoadStore - Try turning a load / store and a
3222/// pre-indexed load / store when the base pointer is a add or subtract
3223/// and it has other uses besides the load / store. After the
3224/// transformation, the new indexed load / store has effectively folded
3225/// the add / subtract in and all of its other uses are redirected to the
3226/// new load / store.
3227bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
3228 if (!AfterLegalize)
3229 return false;
3230
3231 bool isLoad = true;
3232 SDOperand Ptr;
3233 MVT::ValueType VT;
3234 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003235 if (LD->getAddressingMode() != ISD::UNINDEXED)
3236 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003237 VT = LD->getLoadedVT();
Evan Cheng83060c52007-03-07 08:07:03 +00003238 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00003239 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
3240 return false;
3241 Ptr = LD->getBasePtr();
3242 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003243 if (ST->getAddressingMode() != ISD::UNINDEXED)
3244 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003245 VT = ST->getStoredVT();
3246 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
3247 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
3248 return false;
3249 Ptr = ST->getBasePtr();
3250 isLoad = false;
3251 } else
3252 return false;
3253
Chris Lattner9f1794e2006-11-11 00:56:29 +00003254 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
3255 // out. There is no reason to make this a preinc/predec.
3256 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
3257 Ptr.Val->hasOneUse())
3258 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003259
Chris Lattner9f1794e2006-11-11 00:56:29 +00003260 // Ask the target to do addressing mode selection.
3261 SDOperand BasePtr;
3262 SDOperand Offset;
3263 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3264 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
3265 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00003266 // Don't create a indexed load / store with zero offset.
3267 if (isa<ConstantSDNode>(Offset) &&
3268 cast<ConstantSDNode>(Offset)->getValue() == 0)
3269 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00003270
Chris Lattner41e53fd2006-11-11 01:00:15 +00003271 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00003272 // 1) The new base ptr is a frame index.
3273 // 2) If N is a store and the new base ptr is either the same as or is a
Chris Lattner9f1794e2006-11-11 00:56:29 +00003274 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00003275 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00003276 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00003277 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00003278
Chris Lattner41e53fd2006-11-11 01:00:15 +00003279 // Check #1. Preinc'ing a frame index would require copying the stack pointer
3280 // (plus the implicit offset) to a register to preinc anyway.
3281 if (isa<FrameIndexSDNode>(BasePtr))
3282 return false;
3283
3284 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003285 if (!isLoad) {
3286 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Chengc843abe2007-05-24 02:35:39 +00003287 if (Val == BasePtr || BasePtr.Val->isPredecessor(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00003288 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003289 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003290
Evan Chengc843abe2007-05-24 02:35:39 +00003291 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003292 bool RealUse = false;
3293 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3294 E = Ptr.Val->use_end(); I != E; ++I) {
3295 SDNode *Use = *I;
3296 if (Use == N)
3297 continue;
3298 if (Use->isPredecessor(N))
3299 return false;
3300
3301 if (!((Use->getOpcode() == ISD::LOAD &&
3302 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
3303 (Use->getOpcode() == ISD::STORE) &&
3304 cast<StoreSDNode>(Use)->getBasePtr() == Ptr))
3305 RealUse = true;
3306 }
3307 if (!RealUse)
3308 return false;
3309
3310 SDOperand Result;
3311 if (isLoad)
3312 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
3313 else
3314 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
3315 ++PreIndexedNodes;
3316 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00003317 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00003318 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
3319 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00003320 std::vector<SDNode*> NowDead;
3321 if (isLoad) {
3322 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
3323 NowDead);
3324 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
3325 NowDead);
3326 } else {
3327 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
3328 NowDead);
3329 }
3330
3331 // Nodes can end up on the worklist more than once. Make sure we do
3332 // not process a node that has been replaced.
3333 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3334 removeFromWorkList(NowDead[i]);
3335 // Finally, since the node is now dead, remove it from the graph.
3336 DAG.DeleteNode(N);
3337
3338 // Replace the uses of Ptr with uses of the updated base value.
3339 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
3340 NowDead);
3341 removeFromWorkList(Ptr.Val);
3342 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3343 removeFromWorkList(NowDead[i]);
3344 DAG.DeleteNode(Ptr.Val);
3345
3346 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00003347}
3348
3349/// CombineToPostIndexedLoadStore - Try combine a load / store with a
3350/// add / sub of the base pointer node into a post-indexed load / store.
3351/// The transformation folded the add / subtract into the new indexed
3352/// load / store effectively and all of its uses are redirected to the
3353/// new load / store.
3354bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
3355 if (!AfterLegalize)
3356 return false;
3357
3358 bool isLoad = true;
3359 SDOperand Ptr;
3360 MVT::ValueType VT;
3361 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003362 if (LD->getAddressingMode() != ISD::UNINDEXED)
3363 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003364 VT = LD->getLoadedVT();
3365 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
3366 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
3367 return false;
3368 Ptr = LD->getBasePtr();
3369 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003370 if (ST->getAddressingMode() != ISD::UNINDEXED)
3371 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003372 VT = ST->getStoredVT();
3373 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
3374 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
3375 return false;
3376 Ptr = ST->getBasePtr();
3377 isLoad = false;
3378 } else
3379 return false;
3380
Evan Chengcc470212006-11-16 00:08:20 +00003381 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00003382 return false;
3383
3384 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3385 E = Ptr.Val->use_end(); I != E; ++I) {
3386 SDNode *Op = *I;
3387 if (Op == N ||
3388 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
3389 continue;
3390
3391 SDOperand BasePtr;
3392 SDOperand Offset;
3393 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3394 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
3395 if (Ptr == Offset)
3396 std::swap(BasePtr, Offset);
3397 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00003398 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00003399 // Don't create a indexed load / store with zero offset.
3400 if (isa<ConstantSDNode>(Offset) &&
3401 cast<ConstantSDNode>(Offset)->getValue() == 0)
3402 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00003403
Chris Lattner9f1794e2006-11-11 00:56:29 +00003404 // Try turning it into a post-indexed load / store except when
3405 // 1) All uses are load / store ops that use it as base ptr.
3406 // 2) Op must be independent of N, i.e. Op is neither a predecessor
3407 // nor a successor of N. Otherwise, if Op is folded that would
3408 // create a cycle.
3409
3410 // Check for #1.
3411 bool TryNext = false;
3412 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
3413 EE = BasePtr.Val->use_end(); II != EE; ++II) {
3414 SDNode *Use = *II;
3415 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00003416 continue;
3417
Chris Lattner9f1794e2006-11-11 00:56:29 +00003418 // If all the uses are load / store addresses, then don't do the
3419 // transformation.
3420 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
3421 bool RealUse = false;
3422 for (SDNode::use_iterator III = Use->use_begin(),
3423 EEE = Use->use_end(); III != EEE; ++III) {
3424 SDNode *UseUse = *III;
3425 if (!((UseUse->getOpcode() == ISD::LOAD &&
3426 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
3427 (UseUse->getOpcode() == ISD::STORE) &&
3428 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use))
3429 RealUse = true;
3430 }
Chris Lattner448f2192006-11-11 00:39:41 +00003431
Chris Lattner9f1794e2006-11-11 00:56:29 +00003432 if (!RealUse) {
3433 TryNext = true;
3434 break;
Chris Lattner448f2192006-11-11 00:39:41 +00003435 }
3436 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003437 }
3438 if (TryNext)
3439 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00003440
Chris Lattner9f1794e2006-11-11 00:56:29 +00003441 // Check for #2
3442 if (!Op->isPredecessor(N) && !N->isPredecessor(Op)) {
3443 SDOperand Result = isLoad
3444 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
3445 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
3446 ++PostIndexedNodes;
3447 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00003448 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00003449 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
3450 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00003451 std::vector<SDNode*> NowDead;
3452 if (isLoad) {
3453 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattner448f2192006-11-11 00:39:41 +00003454 NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003455 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
3456 NowDead);
3457 } else {
3458 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
3459 NowDead);
Chris Lattner448f2192006-11-11 00:39:41 +00003460 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003461
3462 // Nodes can end up on the worklist more than once. Make sure we do
3463 // not process a node that has been replaced.
3464 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3465 removeFromWorkList(NowDead[i]);
3466 // Finally, since the node is now dead, remove it from the graph.
3467 DAG.DeleteNode(N);
3468
3469 // Replace the uses of Use with uses of the updated base value.
3470 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
3471 Result.getValue(isLoad ? 1 : 0),
3472 NowDead);
3473 removeFromWorkList(Op);
3474 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3475 removeFromWorkList(NowDead[i]);
3476 DAG.DeleteNode(Op);
3477
3478 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00003479 }
3480 }
3481 }
3482 return false;
3483}
3484
3485
Chris Lattner01a22022005-10-10 22:04:48 +00003486SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00003487 LoadSDNode *LD = cast<LoadSDNode>(N);
3488 SDOperand Chain = LD->getChain();
3489 SDOperand Ptr = LD->getBasePtr();
Evan Cheng45a7ca92007-05-01 00:38:21 +00003490
3491 // If load is not volatile and there are no uses of the loaded value (and
3492 // the updated indexed value in case of indexed loads), change uses of the
3493 // chain value into uses of the chain input (i.e. delete the dead load).
3494 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00003495 if (N->getValueType(1) == MVT::Other) {
3496 // Unindexed loads.
3497 if (N->hasNUsesOfValue(0, 0))
3498 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
3499 } else {
3500 // Indexed loads.
3501 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
3502 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
3503 SDOperand Undef0 = DAG.getNode(ISD::UNDEF, N->getValueType(0));
3504 SDOperand Undef1 = DAG.getNode(ISD::UNDEF, N->getValueType(1));
3505 SDOperand To[] = { Undef0, Undef1, Chain };
3506 return CombineTo(N, To, 3);
Evan Cheng45a7ca92007-05-01 00:38:21 +00003507 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00003508 }
3509 }
Chris Lattner01a22022005-10-10 22:04:48 +00003510
3511 // If this load is directly stored, replace the load value with the stored
3512 // value.
3513 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003514 // TODO: Handle TRUNCSTORE/LOADEXT
3515 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003516 if (ISD::isNON_TRUNCStore(Chain.Val)) {
3517 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
3518 if (PrevST->getBasePtr() == Ptr &&
3519 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003520 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003521 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003522 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00003523
Jim Laskey7ca56af2006-10-11 13:47:09 +00003524 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00003525 // Walk up chain skipping non-aliasing memory nodes.
3526 SDOperand BetterChain = FindBetterChain(N, Chain);
3527
Jim Laskey6ff23e52006-10-04 16:53:27 +00003528 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00003529 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003530 SDOperand ReplLoad;
3531
Jim Laskey279f0532006-09-25 16:29:54 +00003532 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003533 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
3534 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003535 LD->getSrcValue(), LD->getSrcValueOffset(),
3536 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003537 } else {
3538 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
3539 LD->getValueType(0),
3540 BetterChain, Ptr, LD->getSrcValue(),
3541 LD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003542 LD->getLoadedVT(),
3543 LD->isVolatile(),
3544 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003545 }
Jim Laskey279f0532006-09-25 16:29:54 +00003546
Jim Laskey6ff23e52006-10-04 16:53:27 +00003547 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00003548 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
3549 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00003550
Jim Laskey274062c2006-10-13 23:32:28 +00003551 // Replace uses with load result and token factor. Don't add users
3552 // to work list.
3553 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003554 }
3555 }
3556
Evan Cheng7fc033a2006-11-03 03:06:21 +00003557 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003558 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00003559 return SDOperand(N, 0);
3560
Chris Lattner01a22022005-10-10 22:04:48 +00003561 return SDOperand();
3562}
3563
Chris Lattner87514ca2005-10-10 22:31:19 +00003564SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003565 StoreSDNode *ST = cast<StoreSDNode>(N);
3566 SDOperand Chain = ST->getChain();
3567 SDOperand Value = ST->getValue();
3568 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00003569
Evan Cheng59d5b682007-05-07 21:27:48 +00003570 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00003571 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00003572 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
3573 ST->getAddressingMode() == ISD::UNINDEXED) {
Evan Cheng59d5b682007-05-07 21:27:48 +00003574 unsigned Align = ST->getAlignment();
3575 MVT::ValueType SVT = Value.getOperand(0).getValueType();
3576 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00003577 getABITypeAlignment(MVT::getTypeForValueType(SVT));
Evan Chengc2cd2b22007-05-07 21:36:06 +00003578 if (Align <= OrigAlign && TLI.isOperationLegal(ISD::STORE, SVT))
Evan Cheng59d5b682007-05-07 21:27:48 +00003579 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
3580 ST->getSrcValueOffset());
Jim Laskey279f0532006-09-25 16:29:54 +00003581 }
3582
Nate Begeman2cbba892006-12-11 02:23:46 +00003583 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00003584 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Evan Cheng25ece662006-12-11 17:25:19 +00003585 if (Value.getOpcode() != ISD::TargetConstantFP) {
3586 SDOperand Tmp;
Chris Lattner62be1a72006-12-12 04:16:14 +00003587 switch (CFP->getValueType(0)) {
3588 default: assert(0 && "Unknown FP type");
3589 case MVT::f32:
3590 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
3591 Tmp = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
3592 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
3593 ST->getSrcValueOffset());
3594 }
3595 break;
3596 case MVT::f64:
3597 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
3598 Tmp = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
3599 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
3600 ST->getSrcValueOffset());
3601 } else if (TLI.isTypeLegal(MVT::i32)) {
3602 // Many FP stores are not make apparent until after legalize, e.g. for
3603 // argument passing. Since this is so common, custom legalize the
3604 // 64-bit integer store into two 32-bit stores.
3605 uint64_t Val = DoubleToBits(CFP->getValue());
3606 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
3607 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
3608 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
3609
3610 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
3611 ST->getSrcValueOffset());
3612 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3613 DAG.getConstant(4, Ptr.getValueType()));
3614 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
3615 ST->getSrcValueOffset()+4);
3616 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
3617 }
3618 break;
Evan Cheng25ece662006-12-11 17:25:19 +00003619 }
Nate Begeman2cbba892006-12-11 02:23:46 +00003620 }
Nate Begeman2cbba892006-12-11 02:23:46 +00003621 }
3622
Jim Laskey279f0532006-09-25 16:29:54 +00003623 if (CombinerAA) {
3624 // Walk up chain skipping non-aliasing memory nodes.
3625 SDOperand BetterChain = FindBetterChain(N, Chain);
3626
Jim Laskey6ff23e52006-10-04 16:53:27 +00003627 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00003628 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00003629 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00003630 SDOperand ReplStore;
3631 if (ST->isTruncatingStore()) {
3632 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
3633 ST->getSrcValue(),ST->getSrcValueOffset(), ST->getStoredVT());
3634 } else {
3635 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
3636 ST->getSrcValue(), ST->getSrcValueOffset());
3637 }
3638
Jim Laskey279f0532006-09-25 16:29:54 +00003639 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00003640 SDOperand Token =
3641 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
3642
3643 // Don't add users to work list.
3644 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003645 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00003646 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00003647
Evan Cheng33dbedc2006-11-05 09:31:14 +00003648 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003649 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00003650 return SDOperand(N, 0);
3651
Chris Lattner87514ca2005-10-10 22:31:19 +00003652 return SDOperand();
3653}
3654
Chris Lattnerca242442006-03-19 01:27:56 +00003655SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
3656 SDOperand InVec = N->getOperand(0);
3657 SDOperand InVal = N->getOperand(1);
3658 SDOperand EltNo = N->getOperand(2);
3659
3660 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
3661 // vector with the inserted element.
3662 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
3663 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003664 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00003665 if (Elt < Ops.size())
3666 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003667 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
3668 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00003669 }
3670
3671 return SDOperand();
3672}
3673
3674SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
3675 SDOperand InVec = N->getOperand(0);
3676 SDOperand InVal = N->getOperand(1);
3677 SDOperand EltNo = N->getOperand(2);
3678 SDOperand NumElts = N->getOperand(3);
3679 SDOperand EltType = N->getOperand(4);
3680
3681 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
3682 // vector with the inserted element.
3683 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
3684 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003685 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00003686 if (Elt < Ops.size()-2)
3687 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003688 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
3689 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00003690 }
3691
3692 return SDOperand();
3693}
3694
Chris Lattnerd7648c82006-03-28 20:28:38 +00003695SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
3696 unsigned NumInScalars = N->getNumOperands()-2;
3697 SDOperand NumElts = N->getOperand(NumInScalars);
3698 SDOperand EltType = N->getOperand(NumInScalars+1);
3699
3700 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
3701 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
3702 // two distinct vectors, turn this into a shuffle node.
3703 SDOperand VecIn1, VecIn2;
3704 for (unsigned i = 0; i != NumInScalars; ++i) {
3705 // Ignore undef inputs.
3706 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3707
3708 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
3709 // constant index, bail out.
3710 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
3711 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
3712 VecIn1 = VecIn2 = SDOperand(0, 0);
3713 break;
3714 }
3715
3716 // If the input vector type disagrees with the result of the vbuild_vector,
3717 // we can't make a shuffle.
3718 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
3719 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
3720 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
3721 VecIn1 = VecIn2 = SDOperand(0, 0);
3722 break;
3723 }
3724
3725 // Otherwise, remember this. We allow up to two distinct input vectors.
3726 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
3727 continue;
3728
3729 if (VecIn1.Val == 0) {
3730 VecIn1 = ExtractedFromVec;
3731 } else if (VecIn2.Val == 0) {
3732 VecIn2 = ExtractedFromVec;
3733 } else {
3734 // Too many inputs.
3735 VecIn1 = VecIn2 = SDOperand(0, 0);
3736 break;
3737 }
3738 }
3739
3740 // If everything is good, we can make a shuffle operation.
3741 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003742 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00003743 for (unsigned i = 0; i != NumInScalars; ++i) {
3744 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00003745 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00003746 continue;
3747 }
3748
3749 SDOperand Extract = N->getOperand(i);
3750
3751 // If extracting from the first vector, just use the index directly.
3752 if (Extract.getOperand(0) == VecIn1) {
3753 BuildVecIndices.push_back(Extract.getOperand(1));
3754 continue;
3755 }
3756
3757 // Otherwise, use InIdx + VecSize
3758 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Evan Cheng597a3bd2007-01-20 10:10:26 +00003759 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars,
3760 TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00003761 }
3762
3763 // Add count and size info.
3764 BuildVecIndices.push_back(NumElts);
Evan Cheng597a3bd2007-01-20 10:10:26 +00003765 BuildVecIndices.push_back(DAG.getValueType(TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00003766
3767 // Return the new VVECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003768 SDOperand Ops[5];
3769 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00003770 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003771 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00003772 } else {
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00003773 // Use an undef vbuild_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00003774 std::vector<SDOperand> UnOps(NumInScalars,
3775 DAG.getNode(ISD::UNDEF,
3776 cast<VTSDNode>(EltType)->getVT()));
3777 UnOps.push_back(NumElts);
3778 UnOps.push_back(EltType);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003779 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3780 &UnOps[0], UnOps.size());
3781 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00003782 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003783 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3784 &BuildVecIndices[0], BuildVecIndices.size());
3785 Ops[3] = NumElts;
3786 Ops[4] = EltType;
3787 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
Chris Lattnerd7648c82006-03-28 20:28:38 +00003788 }
3789
3790 return SDOperand();
3791}
3792
Chris Lattner66445d32006-03-28 22:11:53 +00003793SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003794 SDOperand ShufMask = N->getOperand(2);
3795 unsigned NumElts = ShufMask.getNumOperands();
3796
3797 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3798 bool isIdentity = true;
3799 for (unsigned i = 0; i != NumElts; ++i) {
3800 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3801 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3802 isIdentity = false;
3803 break;
3804 }
3805 }
3806 if (isIdentity) return N->getOperand(0);
3807
3808 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3809 isIdentity = true;
3810 for (unsigned i = 0; i != NumElts; ++i) {
3811 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3812 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3813 isIdentity = false;
3814 break;
3815 }
3816 }
3817 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00003818
3819 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3820 // needed at all.
3821 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003822 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003823 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003824 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003825 for (unsigned i = 0; i != NumElts; ++i)
3826 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3827 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3828 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003829 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003830 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003831 BaseIdx = Idx;
3832 } else {
3833 if (BaseIdx != Idx)
3834 isSplat = false;
3835 if (VecNum != V) {
3836 isUnary = false;
3837 break;
3838 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003839 }
3840 }
3841
3842 SDOperand N0 = N->getOperand(0);
3843 SDOperand N1 = N->getOperand(1);
3844 // Normalize unary shuffle so the RHS is undef.
3845 if (isUnary && VecNum == 1)
3846 std::swap(N0, N1);
3847
Evan Cheng917ec982006-07-21 08:25:53 +00003848 // If it is a splat, check if the argument vector is a build_vector with
3849 // all scalar elements the same.
3850 if (isSplat) {
3851 SDNode *V = N0.Val;
3852 if (V->getOpcode() == ISD::BIT_CONVERT)
3853 V = V->getOperand(0).Val;
3854 if (V->getOpcode() == ISD::BUILD_VECTOR) {
3855 unsigned NumElems = V->getNumOperands()-2;
3856 if (NumElems > BaseIdx) {
3857 SDOperand Base;
3858 bool AllSame = true;
3859 for (unsigned i = 0; i != NumElems; ++i) {
3860 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3861 Base = V->getOperand(i);
3862 break;
3863 }
3864 }
3865 // Splat of <u, u, u, u>, return <u, u, u, u>
3866 if (!Base.Val)
3867 return N0;
3868 for (unsigned i = 0; i != NumElems; ++i) {
3869 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3870 V->getOperand(i) != Base) {
3871 AllSame = false;
3872 break;
3873 }
3874 }
3875 // Splat of <x, x, x, x>, return <x, x, x, x>
3876 if (AllSame)
3877 return N0;
3878 }
3879 }
3880 }
3881
Evan Chenge7bec0d2006-07-20 22:44:41 +00003882 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3883 // into an undef.
3884 if (isUnary || N0 == N1) {
3885 if (N0.getOpcode() == ISD::UNDEF)
Evan Chengc04766a2006-04-06 23:20:43 +00003886 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00003887 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3888 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003889 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00003890 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00003891 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3892 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3893 MappedOps.push_back(ShufMask.getOperand(i));
3894 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00003895 unsigned NewIdx =
3896 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3897 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00003898 }
3899 }
3900 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003901 &MappedOps[0], MappedOps.size());
Chris Lattner3e104b12006-04-08 04:15:24 +00003902 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00003903 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
Evan Chenge7bec0d2006-07-20 22:44:41 +00003904 N0,
Chris Lattner66445d32006-03-28 22:11:53 +00003905 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
3906 ShufMask);
3907 }
3908
3909 return SDOperand();
3910}
3911
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003912SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
3913 SDOperand ShufMask = N->getOperand(2);
3914 unsigned NumElts = ShufMask.getNumOperands()-2;
3915
3916 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3917 bool isIdentity = true;
3918 for (unsigned i = 0; i != NumElts; ++i) {
3919 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3920 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3921 isIdentity = false;
3922 break;
3923 }
3924 }
3925 if (isIdentity) return N->getOperand(0);
3926
3927 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3928 isIdentity = true;
3929 for (unsigned i = 0; i != NumElts; ++i) {
3930 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3931 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3932 isIdentity = false;
3933 break;
3934 }
3935 }
3936 if (isIdentity) return N->getOperand(1);
3937
Evan Chenge7bec0d2006-07-20 22:44:41 +00003938 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3939 // needed at all.
3940 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003941 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003942 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003943 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003944 for (unsigned i = 0; i != NumElts; ++i)
3945 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3946 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3947 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003948 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003949 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003950 BaseIdx = Idx;
3951 } else {
3952 if (BaseIdx != Idx)
3953 isSplat = false;
3954 if (VecNum != V) {
3955 isUnary = false;
3956 break;
3957 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003958 }
3959 }
3960
3961 SDOperand N0 = N->getOperand(0);
3962 SDOperand N1 = N->getOperand(1);
3963 // Normalize unary shuffle so the RHS is undef.
3964 if (isUnary && VecNum == 1)
3965 std::swap(N0, N1);
3966
Evan Cheng917ec982006-07-21 08:25:53 +00003967 // If it is a splat, check if the argument vector is a build_vector with
3968 // all scalar elements the same.
3969 if (isSplat) {
3970 SDNode *V = N0.Val;
Evan Cheng59569222006-10-16 22:49:37 +00003971
3972 // If this is a vbit convert that changes the element type of the vector but
3973 // not the number of vector elements, look through it. Be careful not to
3974 // look though conversions that change things like v4f32 to v2f64.
3975 if (V->getOpcode() == ISD::VBIT_CONVERT) {
3976 SDOperand ConvInput = V->getOperand(0);
Evan Cheng5d04a1a2006-10-17 17:06:35 +00003977 if (ConvInput.getValueType() == MVT::Vector &&
3978 NumElts ==
Evan Cheng59569222006-10-16 22:49:37 +00003979 ConvInput.getConstantOperandVal(ConvInput.getNumOperands()-2))
3980 V = ConvInput.Val;
3981 }
3982
Evan Cheng917ec982006-07-21 08:25:53 +00003983 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
3984 unsigned NumElems = V->getNumOperands()-2;
3985 if (NumElems > BaseIdx) {
3986 SDOperand Base;
3987 bool AllSame = true;
3988 for (unsigned i = 0; i != NumElems; ++i) {
3989 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3990 Base = V->getOperand(i);
3991 break;
3992 }
3993 }
3994 // Splat of <u, u, u, u>, return <u, u, u, u>
3995 if (!Base.Val)
3996 return N0;
3997 for (unsigned i = 0; i != NumElems; ++i) {
3998 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3999 V->getOperand(i) != Base) {
4000 AllSame = false;
4001 break;
4002 }
4003 }
4004 // Splat of <x, x, x, x>, return <x, x, x, x>
4005 if (AllSame)
4006 return N0;
4007 }
4008 }
4009 }
4010
Evan Chenge7bec0d2006-07-20 22:44:41 +00004011 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4012 // into an undef.
4013 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004014 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4015 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004016 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004017 for (unsigned i = 0; i != NumElts; ++i) {
4018 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4019 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4020 MappedOps.push_back(ShufMask.getOperand(i));
4021 } else {
4022 unsigned NewIdx =
4023 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
4024 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
4025 }
4026 }
4027 // Add the type/#elts values.
4028 MappedOps.push_back(ShufMask.getOperand(NumElts));
4029 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
4030
4031 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004032 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004033 AddToWorkList(ShufMask.Val);
4034
4035 // Build the undef vector.
4036 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
4037 for (unsigned i = 0; i != NumElts; ++i)
4038 MappedOps[i] = UDVal;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004039 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
4040 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004041 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
4042 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004043
4044 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
Evan Chenge7bec0d2006-07-20 22:44:41 +00004045 N0, UDVal, ShufMask,
Chris Lattner17614ea2006-04-08 05:34:25 +00004046 MappedOps[NumElts], MappedOps[NumElts+1]);
4047 }
4048
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004049 return SDOperand();
4050}
4051
Evan Cheng44f1f092006-04-20 08:56:16 +00004052/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
4053/// a VAND to a vector_shuffle with the destination vector and a zero vector.
4054/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
4055/// vector_shuffle V, Zero, <0, 4, 2, 4>
4056SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
4057 SDOperand LHS = N->getOperand(0);
4058 SDOperand RHS = N->getOperand(1);
4059 if (N->getOpcode() == ISD::VAND) {
4060 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
4061 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
4062 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
4063 RHS = RHS.getOperand(0);
4064 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
4065 std::vector<SDOperand> IdxOps;
4066 unsigned NumOps = RHS.getNumOperands();
4067 unsigned NumElts = NumOps-2;
4068 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
4069 for (unsigned i = 0; i != NumElts; ++i) {
4070 SDOperand Elt = RHS.getOperand(i);
4071 if (!isa<ConstantSDNode>(Elt))
4072 return SDOperand();
4073 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
4074 IdxOps.push_back(DAG.getConstant(i, EVT));
4075 else if (cast<ConstantSDNode>(Elt)->isNullValue())
4076 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
4077 else
4078 return SDOperand();
4079 }
4080
4081 // Let's see if the target supports this vector_shuffle.
4082 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
4083 return SDOperand();
4084
4085 // Return the new VVECTOR_SHUFFLE node.
4086 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
4087 SDOperand EVTNode = DAG.getValueType(EVT);
4088 std::vector<SDOperand> Ops;
Chris Lattner516b9622006-09-14 20:50:57 +00004089 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode,
4090 EVTNode);
Evan Cheng44f1f092006-04-20 08:56:16 +00004091 Ops.push_back(LHS);
4092 AddToWorkList(LHS.Val);
4093 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
4094 ZeroOps.push_back(NumEltsNode);
4095 ZeroOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004096 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
4097 &ZeroOps[0], ZeroOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00004098 IdxOps.push_back(NumEltsNode);
4099 IdxOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004100 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
4101 &IdxOps[0], IdxOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00004102 Ops.push_back(NumEltsNode);
4103 Ops.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004104 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
4105 &Ops[0], Ops.size());
Evan Cheng44f1f092006-04-20 08:56:16 +00004106 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
4107 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
4108 DstVecSize, DstVecEVT);
4109 }
4110 return Result;
4111 }
4112 }
4113 return SDOperand();
4114}
4115
Chris Lattneredab1b92006-04-02 03:25:57 +00004116/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
4117/// the scalar operation of the vop if it is operating on an integer vector
4118/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
4119SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
4120 ISD::NodeType FPOp) {
4121 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
4122 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
4123 SDOperand LHS = N->getOperand(0);
4124 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00004125 SDOperand Shuffle = XformToShuffleWithZero(N);
4126 if (Shuffle.Val) return Shuffle;
4127
Chris Lattneredab1b92006-04-02 03:25:57 +00004128 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
4129 // this operation.
4130 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
4131 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004132 SmallVector<SDOperand, 8> Ops;
Chris Lattneredab1b92006-04-02 03:25:57 +00004133 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
4134 SDOperand LHSOp = LHS.getOperand(i);
4135 SDOperand RHSOp = RHS.getOperand(i);
4136 // If these two elements can't be folded, bail out.
4137 if ((LHSOp.getOpcode() != ISD::UNDEF &&
4138 LHSOp.getOpcode() != ISD::Constant &&
4139 LHSOp.getOpcode() != ISD::ConstantFP) ||
4140 (RHSOp.getOpcode() != ISD::UNDEF &&
4141 RHSOp.getOpcode() != ISD::Constant &&
4142 RHSOp.getOpcode() != ISD::ConstantFP))
4143 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00004144 // Can't fold divide by zero.
4145 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
4146 if ((RHSOp.getOpcode() == ISD::Constant &&
4147 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
4148 (RHSOp.getOpcode() == ISD::ConstantFP &&
4149 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
4150 break;
4151 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004152 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00004153 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00004154 assert((Ops.back().getOpcode() == ISD::UNDEF ||
4155 Ops.back().getOpcode() == ISD::Constant ||
4156 Ops.back().getOpcode() == ISD::ConstantFP) &&
4157 "Scalar binop didn't fold!");
4158 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004159
4160 if (Ops.size() == LHS.getNumOperands()-2) {
4161 Ops.push_back(*(LHS.Val->op_end()-2));
4162 Ops.push_back(*(LHS.Val->op_end()-1));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004163 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004164 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004165 }
4166
4167 return SDOperand();
4168}
4169
Nate Begeman44728a72005-09-19 22:34:01 +00004170SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00004171 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
4172
4173 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
4174 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4175 // If we got a simplified select_cc node back from SimplifySelectCC, then
4176 // break it down into a new SETCC node, and a new SELECT node, and then return
4177 // the SELECT node, since we were called with a SELECT node.
4178 if (SCC.Val) {
4179 // Check to see if we got a select_cc back (to turn into setcc/select).
4180 // Otherwise, just return whatever node we got back, like fabs.
4181 if (SCC.getOpcode() == ISD::SELECT_CC) {
4182 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
4183 SCC.getOperand(0), SCC.getOperand(1),
4184 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00004185 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004186 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
4187 SCC.getOperand(3), SETCC);
4188 }
4189 return SCC;
4190 }
Nate Begeman44728a72005-09-19 22:34:01 +00004191 return SDOperand();
4192}
4193
Chris Lattner40c62d52005-10-18 06:04:22 +00004194/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
4195/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00004196/// select. Callers of this should assume that TheSelect is deleted if this
4197/// returns true. As such, they should return the appropriate thing (e.g. the
4198/// node) back to the top-level of the DAG combiner loop to avoid it being
4199/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00004200///
4201bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
4202 SDOperand RHS) {
4203
4204 // If this is a select from two identical things, try to pull the operation
4205 // through the select.
4206 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00004207 // If this is a load and the token chain is identical, replace the select
4208 // of two loads with a load through a select of the address to load from.
4209 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
4210 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00004211 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00004212 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00004213 LHS.getOperand(0) == RHS.getOperand(0)) {
4214 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
4215 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
4216
4217 // If this is an EXTLOAD, the VT's must match.
Evan Cheng2e49f092006-10-11 07:10:22 +00004218 if (LLD->getLoadedVT() == RLD->getLoadedVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004219 // FIXME: this conflates two src values, discarding one. This is not
4220 // the right thing to do, but nothing uses srcvalues now. When they do,
4221 // turn SrcValue into a list of locations.
4222 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004223 if (TheSelect->getOpcode() == ISD::SELECT) {
4224 // Check that the condition doesn't reach either load. If so, folding
4225 // this will induce a cycle into the DAG.
4226 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4227 !RLD->isPredecessor(TheSelect->getOperand(0).Val)) {
4228 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
4229 TheSelect->getOperand(0), LLD->getBasePtr(),
4230 RLD->getBasePtr());
4231 }
4232 } else {
4233 // Check that the condition doesn't reach either load. If so, folding
4234 // this will induce a cycle into the DAG.
4235 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4236 !RLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4237 !LLD->isPredecessor(TheSelect->getOperand(1).Val) &&
4238 !RLD->isPredecessor(TheSelect->getOperand(1).Val)) {
4239 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00004240 TheSelect->getOperand(0),
4241 TheSelect->getOperand(1),
4242 LLD->getBasePtr(), RLD->getBasePtr(),
4243 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004244 }
Evan Cheng466685d2006-10-09 20:57:25 +00004245 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004246
4247 if (Addr.Val) {
4248 SDOperand Load;
4249 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
4250 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
4251 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004252 LLD->getSrcValueOffset(),
4253 LLD->isVolatile(),
4254 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004255 else {
4256 Load = DAG.getExtLoad(LLD->getExtensionType(),
4257 TheSelect->getValueType(0),
4258 LLD->getChain(), Addr, LLD->getSrcValue(),
4259 LLD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004260 LLD->getLoadedVT(),
4261 LLD->isVolatile(),
4262 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004263 }
4264 // Users of the select now use the result of the load.
4265 CombineTo(TheSelect, Load);
4266
4267 // Users of the old loads now use the new load's chain. We know the
4268 // old-load value is dead now.
4269 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
4270 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
4271 return true;
4272 }
Evan Chengc5484282006-10-04 00:56:09 +00004273 }
Chris Lattner40c62d52005-10-18 06:04:22 +00004274 }
4275 }
4276
4277 return false;
4278}
4279
Nate Begeman44728a72005-09-19 22:34:01 +00004280SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
4281 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00004282 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00004283
4284 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00004285 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
4286 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
4287 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
4288
4289 // Determine if the condition we're dealing with is constant
4290 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00004291 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004292 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
4293
4294 // fold select_cc true, x, y -> x
4295 if (SCCC && SCCC->getValue())
4296 return N2;
4297 // fold select_cc false, x, y -> y
4298 if (SCCC && SCCC->getValue() == 0)
4299 return N3;
4300
4301 // Check to see if we can simplify the select into an fabs node
4302 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
4303 // Allow either -0.0 or 0.0
4304 if (CFP->getValue() == 0.0) {
4305 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
4306 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
4307 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
4308 N2 == N3.getOperand(0))
4309 return DAG.getNode(ISD::FABS, VT, N0);
4310
4311 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
4312 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
4313 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
4314 N2.getOperand(0) == N3)
4315 return DAG.getNode(ISD::FABS, VT, N3);
4316 }
4317 }
4318
4319 // Check to see if we can perform the "gzip trick", transforming
4320 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00004321 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00004322 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00004323 MVT::isInteger(N2.getValueType()) &&
4324 (N1C->isNullValue() || // (a < 0) ? b : 0
4325 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00004326 MVT::ValueType XType = N0.getValueType();
4327 MVT::ValueType AType = N2.getValueType();
4328 if (XType >= AType) {
4329 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00004330 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00004331 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
4332 unsigned ShCtV = Log2_64(N2C->getValue());
4333 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
4334 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
4335 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00004336 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004337 if (XType > AType) {
4338 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004339 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004340 }
4341 return DAG.getNode(ISD::AND, AType, Shift, N2);
4342 }
4343 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4344 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4345 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00004346 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004347 if (XType > AType) {
4348 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004349 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004350 }
4351 return DAG.getNode(ISD::AND, AType, Shift, N2);
4352 }
4353 }
Nate Begeman07ed4172005-10-10 21:26:48 +00004354
4355 // fold select C, 16, 0 -> shl C, 4
4356 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
4357 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00004358
4359 // If the caller doesn't want us to simplify this into a zext of a compare,
4360 // don't do it.
4361 if (NotExtCompare && N2C->getValue() == 1)
4362 return SDOperand();
4363
Nate Begeman07ed4172005-10-10 21:26:48 +00004364 // Get a SetCC of the condition
4365 // FIXME: Should probably make sure that setcc is legal if we ever have a
4366 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00004367 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00004368 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00004369 if (AfterLegalize) {
4370 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Chris Lattner555d8d62006-12-07 22:36:47 +00004371 if (N2.getValueType() < SCC.getValueType())
4372 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
4373 else
4374 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004375 } else {
4376 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00004377 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004378 }
Chris Lattner5750df92006-03-01 04:03:14 +00004379 AddToWorkList(SCC.Val);
4380 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00004381
4382 if (N2C->getValue() == 1)
4383 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00004384 // shl setcc result by log2 n2c
4385 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
4386 DAG.getConstant(Log2_64(N2C->getValue()),
4387 TLI.getShiftAmountTy()));
4388 }
4389
Nate Begemanf845b452005-10-08 00:29:44 +00004390 // Check to see if this is the equivalent of setcc
4391 // FIXME: Turn all of these into setcc if setcc if setcc is legal
4392 // otherwise, go ahead with the folds.
4393 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
4394 MVT::ValueType XType = N0.getValueType();
4395 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
4396 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
4397 if (Res.getValueType() != VT)
4398 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
4399 return Res;
4400 }
4401
4402 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
4403 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
4404 TLI.isOperationLegal(ISD::CTLZ, XType)) {
4405 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
4406 return DAG.getNode(ISD::SRL, XType, Ctlz,
4407 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
4408 TLI.getShiftAmountTy()));
4409 }
4410 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
4411 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
4412 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
4413 N0);
4414 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
4415 DAG.getConstant(~0ULL, XType));
4416 return DAG.getNode(ISD::SRL, XType,
4417 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
4418 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4419 TLI.getShiftAmountTy()));
4420 }
4421 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
4422 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
4423 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
4424 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4425 TLI.getShiftAmountTy()));
4426 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
4427 }
4428 }
4429
4430 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
4431 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4432 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00004433 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
4434 N2.getOperand(0) == N1 && MVT::isInteger(N0.getValueType())) {
4435 MVT::ValueType XType = N0.getValueType();
4436 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4437 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4438 TLI.getShiftAmountTy()));
4439 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
4440 AddToWorkList(Shift.Val);
4441 AddToWorkList(Add.Val);
4442 return DAG.getNode(ISD::XOR, XType, Add, Shift);
4443 }
4444 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
4445 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4446 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
4447 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
4448 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Nate Begemanf845b452005-10-08 00:29:44 +00004449 MVT::ValueType XType = N0.getValueType();
4450 if (SubC->isNullValue() && MVT::isInteger(XType)) {
4451 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4452 DAG.getConstant(MVT::getSizeInBits(XType)-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00004453 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00004454 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004455 AddToWorkList(Shift.Val);
4456 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004457 return DAG.getNode(ISD::XOR, XType, Add, Shift);
4458 }
4459 }
4460 }
Chris Lattner1982ef22007-04-11 05:11:38 +00004461
Nate Begeman44728a72005-09-19 22:34:01 +00004462 return SDOperand();
4463}
4464
Evan Chengfa1eb272007-02-08 22:13:59 +00004465/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Nate Begeman452d7be2005-09-16 00:54:12 +00004466SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00004467 SDOperand N1, ISD::CondCode Cond,
4468 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00004469 TargetLowering::DAGCombinerInfo
4470 DagCombineInfo(DAG, !AfterLegalize, false, this);
4471 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00004472}
4473
Nate Begeman69575232005-10-20 02:15:44 +00004474/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
4475/// return a DAG expression to select that will generate the same value by
4476/// multiplying by a magic number. See:
4477/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4478SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004479 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004480 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
4481
Andrew Lenharth232c9102006-06-12 16:07:18 +00004482 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004483 ii != ee; ++ii)
4484 AddToWorkList(*ii);
4485 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004486}
4487
4488/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
4489/// return a DAG expression to select that will generate the same value by
4490/// multiplying by a magic number. See:
4491/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4492SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004493 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004494 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00004495
Andrew Lenharth232c9102006-06-12 16:07:18 +00004496 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004497 ii != ee; ++ii)
4498 AddToWorkList(*ii);
4499 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004500}
4501
Jim Laskey71382342006-10-07 23:37:56 +00004502/// FindBaseOffset - Return true if base is known not to alias with anything
4503/// but itself. Provides base object and offset as results.
4504static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
4505 // Assume it is a primitive operation.
4506 Base = Ptr; Offset = 0;
4507
4508 // If it's an adding a simple constant then integrate the offset.
4509 if (Base.getOpcode() == ISD::ADD) {
4510 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
4511 Base = Base.getOperand(0);
4512 Offset += C->getValue();
4513 }
4514 }
4515
4516 // If it's any of the following then it can't alias with anything but itself.
4517 return isa<FrameIndexSDNode>(Base) ||
4518 isa<ConstantPoolSDNode>(Base) ||
4519 isa<GlobalAddressSDNode>(Base);
4520}
4521
4522/// isAlias - Return true if there is any possibility that the two addresses
4523/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00004524bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
4525 const Value *SrcValue1, int SrcValueOffset1,
4526 SDOperand Ptr2, int64_t Size2,
4527 const Value *SrcValue2, int SrcValueOffset2)
4528{
Jim Laskey71382342006-10-07 23:37:56 +00004529 // If they are the same then they must be aliases.
4530 if (Ptr1 == Ptr2) return true;
4531
4532 // Gather base node and offset information.
4533 SDOperand Base1, Base2;
4534 int64_t Offset1, Offset2;
4535 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
4536 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
4537
4538 // If they have a same base address then...
4539 if (Base1 == Base2) {
4540 // Check to see if the addresses overlap.
4541 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
4542 }
4543
Jim Laskey096c22e2006-10-18 12:29:57 +00004544 // If we know both bases then they can't alias.
4545 if (KnownBase1 && KnownBase2) return false;
4546
Jim Laskey07a27092006-10-18 19:08:31 +00004547 if (CombinerGlobalAA) {
4548 // Use alias analysis information.
4549 int Overlap1 = Size1 + SrcValueOffset1 + Offset1;
4550 int Overlap2 = Size2 + SrcValueOffset2 + Offset2;
4551 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00004552 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00004553 if (AAResult == AliasAnalysis::NoAlias)
4554 return false;
4555 }
Jim Laskey096c22e2006-10-18 12:29:57 +00004556
4557 // Otherwise we have to assume they alias.
4558 return true;
Jim Laskey71382342006-10-07 23:37:56 +00004559}
4560
4561/// FindAliasInfo - Extracts the relevant alias information from the memory
4562/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00004563bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00004564 SDOperand &Ptr, int64_t &Size,
4565 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004566 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4567 Ptr = LD->getBasePtr();
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004568 Size = MVT::getSizeInBits(LD->getLoadedVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004569 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004570 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00004571 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004572 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004573 Ptr = ST->getBasePtr();
Evan Cheng8b2794a2006-10-13 21:14:26 +00004574 Size = MVT::getSizeInBits(ST->getStoredVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004575 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004576 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00004577 } else {
Jim Laskey71382342006-10-07 23:37:56 +00004578 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00004579 }
4580
4581 return false;
4582}
4583
Jim Laskey6ff23e52006-10-04 16:53:27 +00004584/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
4585/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00004586void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00004587 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004588 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00004589 std::set<SDNode *> Visited; // Visited node set.
4590
Jim Laskey279f0532006-09-25 16:29:54 +00004591 // Get alias information for node.
4592 SDOperand Ptr;
4593 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004594 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004595 int SrcValueOffset;
4596 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00004597
Jim Laskey6ff23e52006-10-04 16:53:27 +00004598 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00004599 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00004600
Jim Laskeybc588b82006-10-05 15:07:25 +00004601 // Look at each chain and determine if it is an alias. If so, add it to the
4602 // aliases list. If not, then continue up the chain looking for the next
4603 // candidate.
4604 while (!Chains.empty()) {
4605 SDOperand Chain = Chains.back();
4606 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00004607
Jim Laskeybc588b82006-10-05 15:07:25 +00004608 // Don't bother if we've been before.
4609 if (Visited.find(Chain.Val) != Visited.end()) continue;
4610 Visited.insert(Chain.Val);
4611
4612 switch (Chain.getOpcode()) {
4613 case ISD::EntryToken:
4614 // Entry token is ideal chain operand, but handled in FindBetterChain.
4615 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00004616
Jim Laskeybc588b82006-10-05 15:07:25 +00004617 case ISD::LOAD:
4618 case ISD::STORE: {
4619 // Get alias information for Chain.
4620 SDOperand OpPtr;
4621 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004622 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004623 int OpSrcValueOffset;
4624 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
4625 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00004626
4627 // If chain is alias then stop here.
4628 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00004629 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
4630 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004631 Aliases.push_back(Chain);
4632 } else {
4633 // Look further up the chain.
4634 Chains.push_back(Chain.getOperand(0));
4635 // Clean up old chain.
4636 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00004637 }
Jim Laskeybc588b82006-10-05 15:07:25 +00004638 break;
4639 }
4640
4641 case ISD::TokenFactor:
4642 // We have to check each of the operands of the token factor, so we queue
4643 // then up. Adding the operands to the queue (stack) in reverse order
4644 // maintains the original order and increases the likelihood that getNode
4645 // will find a matching token factor (CSE.)
4646 for (unsigned n = Chain.getNumOperands(); n;)
4647 Chains.push_back(Chain.getOperand(--n));
4648 // Eliminate the token factor if we can.
4649 AddToWorkList(Chain.Val);
4650 break;
4651
4652 default:
4653 // For all other instructions we will just have to take what we can get.
4654 Aliases.push_back(Chain);
4655 break;
Jim Laskey279f0532006-09-25 16:29:54 +00004656 }
4657 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004658}
4659
4660/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
4661/// for a better chain (aliasing node.)
4662SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
4663 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00004664
Jim Laskey6ff23e52006-10-04 16:53:27 +00004665 // Accumulate all the aliases to this node.
4666 GatherAllAliases(N, OldChain, Aliases);
4667
4668 if (Aliases.size() == 0) {
4669 // If no operands then chain to entry token.
4670 return DAG.getEntryNode();
4671 } else if (Aliases.size() == 1) {
4672 // If a single operand then chain to it. We don't need to revisit it.
4673 return Aliases[0];
4674 }
4675
4676 // Construct a custom tailored token factor.
4677 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4678 &Aliases[0], Aliases.size());
4679
4680 // Make sure the old chain gets cleaned up.
4681 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
4682
4683 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00004684}
4685
Nate Begeman1d4d4142005-09-01 00:19:25 +00004686// SelectionDAG::Combine - This is the entry point for the file.
4687//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004688void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00004689 if (!RunningAfterLegalize && ViewDAGCombine1)
4690 viewGraph();
4691 if (RunningAfterLegalize && ViewDAGCombine2)
4692 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004693 /// run - This is the main entry point to this class.
4694 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004695 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004696}