blob: af4ee8398118543db4c5de63171b78b401fdd867 [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);
Dan Gohman7f321562007-06-25 16:23:39 +0000230 SDOperand SimplifyVBinOp(SDNode *N);
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 Lattner01b3d732005-09-28 22:28:18 +0000246 SDOperand visitFADD(SDNode *N);
247 SDOperand visitFSUB(SDNode *N);
248 SDOperand visitFMUL(SDNode *N);
249 SDOperand visitFDIV(SDNode *N);
250 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000251 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000252 SDOperand visitSINT_TO_FP(SDNode *N);
253 SDOperand visitUINT_TO_FP(SDNode *N);
254 SDOperand visitFP_TO_SINT(SDNode *N);
255 SDOperand visitFP_TO_UINT(SDNode *N);
256 SDOperand visitFP_ROUND(SDNode *N);
257 SDOperand visitFP_ROUND_INREG(SDNode *N);
258 SDOperand visitFP_EXTEND(SDNode *N);
259 SDOperand visitFNEG(SDNode *N);
260 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000261 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000262 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000263 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000264 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000265 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
Evan Cheng513da432007-10-06 08:19:55 +0000266 SDOperand visitEXTRACT_VECTOR_ELT(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000267 SDOperand visitBUILD_VECTOR(SDNode *N);
268 SDOperand visitCONCAT_VECTORS(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000269 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000270
Evan Cheng44f1f092006-04-20 08:56:16 +0000271 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000272 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
273
Chris Lattner40c62d52005-10-18 06:04:22 +0000274 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000275 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000276 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
277 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000278 SDOperand N3, ISD::CondCode CC,
279 bool NotExtCompare = false);
Nate Begeman452d7be2005-09-16 00:54:12 +0000280 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000281 ISD::CondCode Cond, bool foldBooleans = true);
Dan Gohman7f321562007-06-25 16:23:39 +0000282 SDOperand ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000283 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000284 SDOperand BuildUDIV(SDNode *N);
285 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Evan Chengc88138f2007-03-22 01:54:19 +0000286 SDOperand ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000287
Jim Laskey6ff23e52006-10-04 16:53:27 +0000288 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
289 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000290 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000291 SmallVector<SDOperand, 8> &Aliases);
292
Jim Laskey096c22e2006-10-18 12:29:57 +0000293 /// isAlias - Return true if there is any possibility that the two addresses
294 /// overlap.
295 bool isAlias(SDOperand Ptr1, int64_t Size1,
296 const Value *SrcValue1, int SrcValueOffset1,
297 SDOperand Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000298 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000299
Jim Laskey7ca56af2006-10-11 13:47:09 +0000300 /// FindAliasInfo - Extracts the relevant alias information from the memory
301 /// node. Returns true if the operand was a load.
302 bool FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +0000303 SDOperand &Ptr, int64_t &Size,
304 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000305
Jim Laskey279f0532006-09-25 16:29:54 +0000306 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000307 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000308 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
309
Nate Begeman1d4d4142005-09-01 00:19:25 +0000310public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000311 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
312 : DAG(D),
313 TLI(D.getTargetLoweringInfo()),
314 AfterLegalize(false),
315 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000316
317 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000318 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000319 };
320}
321
Chris Lattner24664722006-03-01 04:53:38 +0000322//===----------------------------------------------------------------------===//
323// TargetLowering::DAGCombinerInfo implementation
324//===----------------------------------------------------------------------===//
325
326void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
327 ((DAGCombiner*)DC)->AddToWorkList(N);
328}
329
330SDOperand TargetLowering::DAGCombinerInfo::
331CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000332 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000333}
334
335SDOperand TargetLowering::DAGCombinerInfo::
336CombineTo(SDNode *N, SDOperand Res) {
337 return ((DAGCombiner*)DC)->CombineTo(N, Res);
338}
339
340
341SDOperand TargetLowering::DAGCombinerInfo::
342CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
343 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
344}
345
346
Chris Lattner24664722006-03-01 04:53:38 +0000347//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000348// Helper Functions
349//===----------------------------------------------------------------------===//
350
351/// isNegatibleForFree - Return 1 if we can compute the negated form of the
352/// specified expression for the same cost as the expression itself, or 2 if we
353/// can compute the negated form more cheaply than the expression itself.
Chris Lattner501fee72007-05-23 07:35:22 +0000354static char isNegatibleForFree(SDOperand Op, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000355 // fneg is removable even if it has multiple uses.
356 if (Op.getOpcode() == ISD::FNEG) return 2;
357
358 // Don't allow anything with multiple uses.
359 if (!Op.hasOneUse()) return 0;
360
Chris Lattner3adf9512007-05-25 02:19:06 +0000361 // Don't recurse exponentially.
362 if (Depth > 6) return 0;
363
Chris Lattner29446522007-05-14 22:04:50 +0000364 switch (Op.getOpcode()) {
365 default: return false;
366 case ISD::ConstantFP:
367 return 1;
368 case ISD::FADD:
369 // FIXME: determine better conditions for this xform.
370 if (!UnsafeFPMath) return 0;
371
372 // -(A+B) -> -A - B
Chris Lattner501fee72007-05-23 07:35:22 +0000373 if (char V = isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000374 return V;
375 // -(A+B) -> -B - A
Chris Lattner501fee72007-05-23 07:35:22 +0000376 return isNegatibleForFree(Op.getOperand(1), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000377 case ISD::FSUB:
378 // We can't turn -(A-B) into B-A when we honor signed zeros.
379 if (!UnsafeFPMath) return 0;
380
381 // -(A-B) -> B-A
382 return 1;
383
384 case ISD::FMUL:
385 case ISD::FDIV:
386 if (HonorSignDependentRoundingFPMath()) return 0;
387
388 // -(X*Y) -> (-X * Y) or (X*-Y)
Chris Lattner501fee72007-05-23 07:35:22 +0000389 if (char V = isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000390 return V;
391
Chris Lattner501fee72007-05-23 07:35:22 +0000392 return isNegatibleForFree(Op.getOperand(1), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000393
394 case ISD::FP_EXTEND:
395 case ISD::FP_ROUND:
396 case ISD::FSIN:
Chris Lattner501fee72007-05-23 07:35:22 +0000397 return isNegatibleForFree(Op.getOperand(0), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000398 }
399}
400
401/// GetNegatedExpression - If isNegatibleForFree returns true, this function
402/// returns the newly negated expression.
Chris Lattner3adf9512007-05-25 02:19:06 +0000403static SDOperand GetNegatedExpression(SDOperand Op, SelectionDAG &DAG,
404 unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000405 // fneg is removable even if it has multiple uses.
406 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
407
408 // Don't allow anything with multiple uses.
409 assert(Op.hasOneUse() && "Unknown reuse!");
410
Chris Lattner3adf9512007-05-25 02:19:06 +0000411 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000412 switch (Op.getOpcode()) {
413 default: assert(0 && "Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000414 case ISD::ConstantFP: {
415 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
416 V.changeSign();
417 return DAG.getConstantFP(V, Op.getValueType());
418 }
Chris Lattner29446522007-05-14 22:04:50 +0000419 case ISD::FADD:
420 // FIXME: determine better conditions for this xform.
421 assert(UnsafeFPMath);
422
423 // -(A+B) -> -A - B
Chris Lattner3adf9512007-05-25 02:19:06 +0000424 if (isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000425 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000426 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000427 Op.getOperand(1));
428 // -(A+B) -> -B - A
429 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000430 GetNegatedExpression(Op.getOperand(1), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000431 Op.getOperand(0));
432 case ISD::FSUB:
433 // We can't turn -(A-B) into B-A when we honor signed zeros.
434 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000435
436 // -(0-B) -> B
437 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000438 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000439 return Op.getOperand(1);
Chris Lattner29446522007-05-14 22:04:50 +0000440
441 // -(A-B) -> B-A
442 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
443 Op.getOperand(0));
444
445 case ISD::FMUL:
446 case ISD::FDIV:
447 assert(!HonorSignDependentRoundingFPMath());
448
449 // -(X*Y) -> -X * Y
Chris Lattner3adf9512007-05-25 02:19:06 +0000450 if (isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000451 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000452 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000453 Op.getOperand(1));
454
455 // -(X*Y) -> X * -Y
456 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
457 Op.getOperand(0),
Chris Lattner3adf9512007-05-25 02:19:06 +0000458 GetNegatedExpression(Op.getOperand(1), DAG, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000459
460 case ISD::FP_EXTEND:
461 case ISD::FP_ROUND:
462 case ISD::FSIN:
463 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000464 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000465 }
466}
Chris Lattner24664722006-03-01 04:53:38 +0000467
468
Nate Begeman4ebd8052005-09-01 23:24:04 +0000469// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
470// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000471// Also, set the incoming LHS, RHS, and CC references to the appropriate
472// nodes based on the type of node we are checking. This simplifies life a
473// bit for the callers.
474static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
475 SDOperand &CC) {
476 if (N.getOpcode() == ISD::SETCC) {
477 LHS = N.getOperand(0);
478 RHS = N.getOperand(1);
479 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000480 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000481 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000482 if (N.getOpcode() == ISD::SELECT_CC &&
483 N.getOperand(2).getOpcode() == ISD::Constant &&
484 N.getOperand(3).getOpcode() == ISD::Constant &&
485 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000486 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
487 LHS = N.getOperand(0);
488 RHS = N.getOperand(1);
489 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000490 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000491 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000492 return false;
493}
494
Nate Begeman99801192005-09-07 23:25:52 +0000495// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
496// one use. If this is true, it allows the users to invert the operation for
497// free when it is profitable to do so.
498static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000499 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000500 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000501 return true;
502 return false;
503}
504
Nate Begemancd4d58c2006-02-03 06:46:56 +0000505SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
506 MVT::ValueType VT = N0.getValueType();
507 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
508 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
509 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
510 if (isa<ConstantSDNode>(N1)) {
511 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000512 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000513 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
514 } else if (N0.hasOneUse()) {
515 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000516 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000517 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
518 }
519 }
520 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
521 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
522 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
523 if (isa<ConstantSDNode>(N0)) {
524 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000525 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000526 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
527 } else if (N1.hasOneUse()) {
528 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000529 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000530 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
531 }
532 }
533 return SDOperand();
534}
535
Chris Lattner29446522007-05-14 22:04:50 +0000536//===----------------------------------------------------------------------===//
537// Main DAG Combiner implementation
538//===----------------------------------------------------------------------===//
539
Nate Begeman4ebd8052005-09-01 23:24:04 +0000540void DAGCombiner::Run(bool RunningAfterLegalize) {
541 // set the instance variable, so that the various visit routines may use it.
542 AfterLegalize = RunningAfterLegalize;
543
Nate Begeman646d7e22005-09-02 21:18:40 +0000544 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000545 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
546 E = DAG.allnodes_end(); I != E; ++I)
547 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000548
Chris Lattner95038592005-10-05 06:35:28 +0000549 // Create a dummy node (which is not added to allnodes), that adds a reference
550 // to the root node, preventing it from being deleted, and tracking any
551 // changes of the root.
552 HandleSDNode Dummy(DAG.getRoot());
553
Jim Laskey26f7fa72006-10-17 19:33:52 +0000554 // The root of the dag may dangle to deleted nodes until the dag combiner is
555 // done. Set it to null to avoid confusion.
556 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000557
558 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
559 TargetLowering::DAGCombinerInfo
Evan Chengfa1eb272007-02-08 22:13:59 +0000560 DagCombineInfo(DAG, !RunningAfterLegalize, false, this);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000561
Nate Begeman1d4d4142005-09-01 00:19:25 +0000562 // while the worklist isn't empty, inspect the node on the end of it and
563 // try and combine it.
564 while (!WorkList.empty()) {
565 SDNode *N = WorkList.back();
566 WorkList.pop_back();
567
568 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000569 // N is deleted from the DAG, since they too may now be dead or may have a
570 // reduced number of uses, allowing other xforms.
571 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000572 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000573 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000574
Chris Lattner95038592005-10-05 06:35:28 +0000575 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000576 continue;
577 }
578
Nate Begeman83e75ec2005-09-06 04:43:02 +0000579 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000580
581 // If nothing happened, try a target-specific DAG combine.
582 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000583 assert(N->getOpcode() != ISD::DELETED_NODE &&
584 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000585 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
586 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
587 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
588 }
589
Nate Begeman83e75ec2005-09-06 04:43:02 +0000590 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000591 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000592 // If we get back the same node we passed in, rather than a new node or
593 // zero, we know that the node must have defined multiple values and
594 // CombineTo was used. Since CombineTo takes care of the worklist
595 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000596 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000597 assert(N->getOpcode() != ISD::DELETED_NODE &&
598 RV.Val->getOpcode() != ISD::DELETED_NODE &&
599 "Node was deleted but visit returned new node!");
600
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000601 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +0000602 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
603 DOUT << '\n';
Chris Lattner01a22022005-10-10 22:04:48 +0000604 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000605 if (N->getNumValues() == RV.Val->getNumValues())
606 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
607 else {
608 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
609 SDOperand OpV = RV;
610 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
611 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000612
613 // Push the new node and any users onto the worklist
Jim Laskey6ff23e52006-10-04 16:53:27 +0000614 AddToWorkList(RV.Val);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000615 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000616
Jim Laskey6ff23e52006-10-04 16:53:27 +0000617 // Nodes can be reintroduced into the worklist. Make sure we do not
618 // process a node that has been replaced.
Nate Begeman646d7e22005-09-02 21:18:40 +0000619 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000620 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
621 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000622
623 // Finally, since the node is now dead, remove it from the graph.
624 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000625 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000626 }
627 }
Chris Lattner95038592005-10-05 06:35:28 +0000628
629 // If the root changed (e.g. it was a dead load, update the root).
630 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000631}
632
Nate Begeman83e75ec2005-09-06 04:43:02 +0000633SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000634 switch(N->getOpcode()) {
635 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000636 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000637 case ISD::ADD: return visitADD(N);
638 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000639 case ISD::ADDC: return visitADDC(N);
640 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000641 case ISD::MUL: return visitMUL(N);
642 case ISD::SDIV: return visitSDIV(N);
643 case ISD::UDIV: return visitUDIV(N);
644 case ISD::SREM: return visitSREM(N);
645 case ISD::UREM: return visitUREM(N);
646 case ISD::MULHU: return visitMULHU(N);
647 case ISD::MULHS: return visitMULHS(N);
648 case ISD::AND: return visitAND(N);
649 case ISD::OR: return visitOR(N);
650 case ISD::XOR: return visitXOR(N);
651 case ISD::SHL: return visitSHL(N);
652 case ISD::SRA: return visitSRA(N);
653 case ISD::SRL: return visitSRL(N);
654 case ISD::CTLZ: return visitCTLZ(N);
655 case ISD::CTTZ: return visitCTTZ(N);
656 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000657 case ISD::SELECT: return visitSELECT(N);
658 case ISD::SELECT_CC: return visitSELECT_CC(N);
659 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000660 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
661 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000662 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000663 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
664 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000665 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000666 case ISD::FADD: return visitFADD(N);
667 case ISD::FSUB: return visitFSUB(N);
668 case ISD::FMUL: return visitFMUL(N);
669 case ISD::FDIV: return visitFDIV(N);
670 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000671 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000672 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
673 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
674 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
675 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
676 case ISD::FP_ROUND: return visitFP_ROUND(N);
677 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
678 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
679 case ISD::FNEG: return visitFNEG(N);
680 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000681 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000682 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000683 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000684 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000685 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000686 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000687 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
688 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000689 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000690 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000691 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000692}
693
Chris Lattner6270f682006-10-08 22:57:01 +0000694/// getInputChainForNode - Given a node, return its input chain if it has one,
695/// otherwise return a null sd operand.
696static SDOperand getInputChainForNode(SDNode *N) {
697 if (unsigned NumOps = N->getNumOperands()) {
698 if (N->getOperand(0).getValueType() == MVT::Other)
699 return N->getOperand(0);
700 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
701 return N->getOperand(NumOps-1);
702 for (unsigned i = 1; i < NumOps-1; ++i)
703 if (N->getOperand(i).getValueType() == MVT::Other)
704 return N->getOperand(i);
705 }
706 return SDOperand(0, 0);
707}
708
Nate Begeman83e75ec2005-09-06 04:43:02 +0000709SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000710 // If N has two operands, where one has an input chain equal to the other,
711 // the 'other' chain is redundant.
712 if (N->getNumOperands() == 2) {
713 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
714 return N->getOperand(0);
715 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
716 return N->getOperand(1);
717 }
718
Chris Lattnerc76d4412007-05-16 06:37:59 +0000719 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
720 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
721 SmallPtrSet<SDNode*, 16> SeenOps;
722 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000723
724 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000725 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000726
Jim Laskey71382342006-10-07 23:37:56 +0000727 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000728 // encountered.
729 for (unsigned i = 0; i < TFs.size(); ++i) {
730 SDNode *TF = TFs[i];
731
Jim Laskey6ff23e52006-10-04 16:53:27 +0000732 // Check each of the operands.
733 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
734 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000735
Jim Laskey6ff23e52006-10-04 16:53:27 +0000736 switch (Op.getOpcode()) {
737 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000738 // Entry tokens don't need to be added to the list. They are
739 // rededundant.
740 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000741 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000742
Jim Laskey6ff23e52006-10-04 16:53:27 +0000743 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000744 if ((CombinerAA || Op.hasOneUse()) &&
745 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000746 // Queue up for processing.
747 TFs.push_back(Op.Val);
748 // Clean up in case the token factor is removed.
749 AddToWorkList(Op.Val);
750 Changed = true;
751 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000752 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000753 // Fall thru
754
755 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000756 // Only add if it isn't already in the list.
757 if (SeenOps.insert(Op.Val))
Jim Laskeybc588b82006-10-05 15:07:25 +0000758 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000759 else
760 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000761 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000762 }
763 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000764 }
765
766 SDOperand Result;
767
768 // If we've change things around then replace token factor.
769 if (Changed) {
770 if (Ops.size() == 0) {
771 // The entry token is the only possible outcome.
772 Result = DAG.getEntryNode();
773 } else {
774 // New and improved token factor.
775 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000776 }
Jim Laskey274062c2006-10-13 23:32:28 +0000777
778 // Don't add users to work list.
779 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000780 }
Jim Laskey279f0532006-09-25 16:29:54 +0000781
Jim Laskey6ff23e52006-10-04 16:53:27 +0000782 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000783}
784
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000785static
786SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
787 MVT::ValueType VT = N0.getValueType();
788 SDOperand N00 = N0.getOperand(0);
789 SDOperand N01 = N0.getOperand(1);
790 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
791 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
792 isa<ConstantSDNode>(N00.getOperand(1))) {
793 N0 = DAG.getNode(ISD::ADD, VT,
794 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
795 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
796 return DAG.getNode(ISD::ADD, VT, N0, N1);
797 }
798 return SDOperand();
799}
800
Evan Chengb13cdbd2007-06-21 07:39:16 +0000801static
802SDOperand combineSelectAndUse(SDNode *N, SDOperand Slct, SDOperand OtherOp,
803 SelectionDAG &DAG) {
804 MVT::ValueType VT = N->getValueType(0);
805 unsigned Opc = N->getOpcode();
806 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
807 SDOperand LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
808 SDOperand RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
809 ISD::CondCode CC = ISD::SETCC_INVALID;
810 if (isSlctCC)
811 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
812 else {
813 SDOperand CCOp = Slct.getOperand(0);
814 if (CCOp.getOpcode() == ISD::SETCC)
815 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
816 }
817
818 bool DoXform = false;
819 bool InvCC = false;
820 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
821 "Bad input!");
822 if (LHS.getOpcode() == ISD::Constant &&
823 cast<ConstantSDNode>(LHS)->isNullValue())
824 DoXform = true;
825 else if (CC != ISD::SETCC_INVALID &&
826 RHS.getOpcode() == ISD::Constant &&
827 cast<ConstantSDNode>(RHS)->isNullValue()) {
828 std::swap(LHS, RHS);
829 bool isInt = MVT::isInteger(isSlctCC ? Slct.getOperand(0).getValueType()
830 : Slct.getOperand(0).getOperand(0).getValueType());
831 CC = ISD::getSetCCInverse(CC, isInt);
832 DoXform = true;
833 InvCC = true;
834 }
835
836 if (DoXform) {
837 SDOperand Result = DAG.getNode(Opc, VT, OtherOp, RHS);
838 if (isSlctCC)
839 return DAG.getSelectCC(OtherOp, Result,
840 Slct.getOperand(0), Slct.getOperand(1), CC);
841 SDOperand CCOp = Slct.getOperand(0);
842 if (InvCC)
843 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
844 CCOp.getOperand(1), CC);
845 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
846 }
847 return SDOperand();
848}
849
Nate Begeman83e75ec2005-09-06 04:43:02 +0000850SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000851 SDOperand N0 = N->getOperand(0);
852 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000853 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
854 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000855 MVT::ValueType VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000856
857 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +0000858 if (MVT::isVector(VT)) {
859 SDOperand FoldedVOp = SimplifyVBinOp(N);
860 if (FoldedVOp.Val) return FoldedVOp;
861 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000862
Dan Gohman613e0d82007-07-03 14:03:57 +0000863 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000864 if (N0.getOpcode() == ISD::UNDEF)
865 return N0;
866 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000867 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000868 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000869 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000870 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000871 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000872 if (N0C && !N1C)
873 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000874 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000875 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000876 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000877 // fold ((c1-A)+c2) -> (c1+c2)-A
878 if (N1C && N0.getOpcode() == ISD::SUB)
879 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
880 return DAG.getNode(ISD::SUB, VT,
881 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
882 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000883 // reassociate add
884 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
885 if (RADD.Val != 0)
886 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000887 // fold ((0-A) + B) -> B-A
888 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
889 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000890 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000891 // fold (A + (0-B)) -> A-B
892 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
893 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000894 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000895 // fold (A+(B-A)) -> B
896 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000897 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000898
Evan Cheng860771d2006-03-01 01:09:54 +0000899 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000900 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000901
902 // fold (a+b) -> (a|b) iff a and b share no bits.
903 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
904 uint64_t LHSZero, LHSOne;
905 uint64_t RHSZero, RHSOne;
906 uint64_t Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +0000907 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000908 if (LHSZero) {
Dan Gohmanea859be2007-06-22 14:59:07 +0000909 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000910
911 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
912 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
913 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
914 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
915 return DAG.getNode(ISD::OR, VT, N0, N1);
916 }
917 }
Evan Cheng3ef554d2006-11-06 08:14:30 +0000918
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000919 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
920 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
921 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
922 if (Result.Val) return Result;
923 }
924 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
925 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
926 if (Result.Val) return Result;
927 }
928
Evan Chengb13cdbd2007-06-21 07:39:16 +0000929 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
930 if (N0.getOpcode() == ISD::SELECT && N0.Val->hasOneUse()) {
931 SDOperand Result = combineSelectAndUse(N, N0, N1, DAG);
932 if (Result.Val) return Result;
933 }
934 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
935 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
936 if (Result.Val) return Result;
937 }
938
Nate Begeman83e75ec2005-09-06 04:43:02 +0000939 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000940}
941
Chris Lattner91153682007-03-04 20:03:15 +0000942SDOperand DAGCombiner::visitADDC(SDNode *N) {
943 SDOperand N0 = N->getOperand(0);
944 SDOperand N1 = N->getOperand(1);
945 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
946 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
947 MVT::ValueType VT = N0.getValueType();
948
949 // If the flag result is dead, turn this into an ADD.
950 if (N->hasNUsesOfValue(0, 1))
951 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +0000952 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +0000953
954 // canonicalize constant to RHS.
Chris Lattnerbcf24842007-03-04 20:08:45 +0000955 if (N0C && !N1C) {
956 SDOperand Ops[] = { N1, N0 };
957 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
958 }
Chris Lattner91153682007-03-04 20:03:15 +0000959
Chris Lattnerb6541762007-03-04 20:40:38 +0000960 // fold (addc x, 0) -> x + no carry out
961 if (N1C && N1C->isNullValue())
962 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
963
964 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
965 uint64_t LHSZero, LHSOne;
966 uint64_t RHSZero, RHSOne;
967 uint64_t Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +0000968 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +0000969 if (LHSZero) {
Dan Gohmanea859be2007-06-22 14:59:07 +0000970 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +0000971
972 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
973 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
974 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
975 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
976 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
977 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
978 }
Chris Lattner91153682007-03-04 20:03:15 +0000979
980 return SDOperand();
981}
982
983SDOperand DAGCombiner::visitADDE(SDNode *N) {
984 SDOperand N0 = N->getOperand(0);
985 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +0000986 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +0000987 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
988 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Chris Lattnerbcf24842007-03-04 20:08:45 +0000989 //MVT::ValueType VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +0000990
991 // canonicalize constant to RHS
Chris Lattnerbcf24842007-03-04 20:08:45 +0000992 if (N0C && !N1C) {
Chris Lattnerb6541762007-03-04 20:40:38 +0000993 SDOperand Ops[] = { N1, N0, CarryIn };
Chris Lattnerbcf24842007-03-04 20:08:45 +0000994 return DAG.getNode(ISD::ADDE, N->getVTList(), Ops, 3);
995 }
Chris Lattner91153682007-03-04 20:03:15 +0000996
Chris Lattnerb6541762007-03-04 20:40:38 +0000997 // fold (adde x, y, false) -> (addc x, y)
998 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) {
999 SDOperand Ops[] = { N1, N0 };
1000 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
1001 }
Chris Lattner91153682007-03-04 20:03:15 +00001002
1003 return SDOperand();
1004}
1005
1006
1007
Nate Begeman83e75ec2005-09-06 04:43:02 +00001008SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001009 SDOperand N0 = N->getOperand(0);
1010 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001011 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1012 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001013 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001014
Dan Gohman7f321562007-06-25 16:23:39 +00001015 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001016 if (MVT::isVector(VT)) {
1017 SDOperand FoldedVOp = SimplifyVBinOp(N);
1018 if (FoldedVOp.Val) return FoldedVOp;
1019 }
Dan Gohman7f321562007-06-25 16:23:39 +00001020
Chris Lattner854077d2005-10-17 01:07:11 +00001021 // fold (sub x, x) -> 0
1022 if (N0 == N1)
1023 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001024 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001025 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001026 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001027 // fold (sub x, c) -> (add x, -c)
1028 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001029 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001030 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001031 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001032 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001033 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001034 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001035 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001036 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1037 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1038 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1039 if (Result.Val) return Result;
1040 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001041 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001042 if (N0.getOpcode() == ISD::UNDEF)
1043 return N0;
1044 if (N1.getOpcode() == ISD::UNDEF)
1045 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001046
Nate Begeman83e75ec2005-09-06 04:43:02 +00001047 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001048}
1049
Nate Begeman83e75ec2005-09-06 04:43:02 +00001050SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001051 SDOperand N0 = N->getOperand(0);
1052 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001053 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1054 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +00001055 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001056
Dan Gohman7f321562007-06-25 16:23:39 +00001057 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001058 if (MVT::isVector(VT)) {
1059 SDOperand FoldedVOp = SimplifyVBinOp(N);
1060 if (FoldedVOp.Val) return FoldedVOp;
1061 }
Dan Gohman7f321562007-06-25 16:23:39 +00001062
Dan Gohman613e0d82007-07-03 14:03:57 +00001063 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001064 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001065 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001066 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001067 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001068 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001069 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001070 if (N0C && !N1C)
1071 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001072 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001073 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001074 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001075 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001076 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001077 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001078 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +00001079 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +00001080 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001081 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001082 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001083 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1084 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1085 // FIXME: If the input is something that is easily negated (e.g. a
1086 // single-use add), we should put the negate there.
1087 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1088 DAG.getNode(ISD::SHL, VT, N0,
1089 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1090 TLI.getShiftAmountTy())));
1091 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001092
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001093 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1094 if (N1C && N0.getOpcode() == ISD::SHL &&
1095 isa<ConstantSDNode>(N0.getOperand(1))) {
1096 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001097 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001098 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1099 }
1100
1101 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1102 // use.
1103 {
1104 SDOperand Sh(0,0), Y(0,0);
1105 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1106 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1107 N0.Val->hasOneUse()) {
1108 Sh = N0; Y = N1;
1109 } else if (N1.getOpcode() == ISD::SHL &&
1110 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1111 Sh = N1; Y = N0;
1112 }
1113 if (Sh.Val) {
1114 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1115 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1116 }
1117 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001118 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1119 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1120 isa<ConstantSDNode>(N0.getOperand(1))) {
1121 return DAG.getNode(ISD::ADD, VT,
1122 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1123 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1124 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001125
Nate Begemancd4d58c2006-02-03 06:46:56 +00001126 // reassociate mul
1127 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1128 if (RMUL.Val != 0)
1129 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001130
Nate Begeman83e75ec2005-09-06 04:43:02 +00001131 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001132}
1133
Nate Begeman83e75ec2005-09-06 04:43:02 +00001134SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001135 SDOperand N0 = N->getOperand(0);
1136 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001137 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1138 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001139 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001140
Dan Gohman7f321562007-06-25 16:23:39 +00001141 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001142 if (MVT::isVector(VT)) {
1143 SDOperand FoldedVOp = SimplifyVBinOp(N);
1144 if (FoldedVOp.Val) return FoldedVOp;
1145 }
Dan Gohman7f321562007-06-25 16:23:39 +00001146
Nate Begeman1d4d4142005-09-01 00:19:25 +00001147 // fold (sdiv 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::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001150 // fold (sdiv X, 1) -> X
1151 if (N1C && N1C->getSignExtended() == 1LL)
1152 return N0;
1153 // fold (sdiv X, -1) -> 0-X
1154 if (N1C && N1C->isAllOnesValue())
1155 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001156 // If we know the sign bits of both operands are zero, strength reduce to a
1157 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
1158 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001159 if (DAG.MaskedValueIsZero(N1, SignBit) &&
1160 DAG.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +00001161 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001162 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +00001163 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001164 (isPowerOf2_64(N1C->getSignExtended()) ||
1165 isPowerOf2_64(-N1C->getSignExtended()))) {
1166 // If dividing by powers of two is cheap, then don't perform the following
1167 // fold.
1168 if (TLI.isPow2DivCheap())
1169 return SDOperand();
1170 int64_t pow2 = N1C->getSignExtended();
1171 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001172 unsigned lg2 = Log2_64(abs2);
1173 // Splat the sign bit into the register
1174 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001175 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1176 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001177 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001178 // Add (N0 < 0) ? abs2 - 1 : 0;
1179 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1180 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001181 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001182 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001183 AddToWorkList(SRL.Val);
1184 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001185 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1186 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001187 // If we're dividing by a positive value, we're done. Otherwise, we must
1188 // negate the result.
1189 if (pow2 > 0)
1190 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001191 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001192 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1193 }
Nate Begeman69575232005-10-20 02:15:44 +00001194 // if integer divide is expensive and we satisfy the requirements, emit an
1195 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001196 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001197 !TLI.isIntDivCheap()) {
1198 SDOperand Op = BuildSDIV(N);
1199 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001200 }
Dan Gohman7f321562007-06-25 16:23:39 +00001201
Dan Gohman613e0d82007-07-03 14:03:57 +00001202 // undef / X -> 0
1203 if (N0.getOpcode() == ISD::UNDEF)
1204 return DAG.getConstant(0, VT);
1205 // X / undef -> undef
1206 if (N1.getOpcode() == ISD::UNDEF)
1207 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001208
Nate Begeman83e75ec2005-09-06 04:43:02 +00001209 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001210}
1211
Nate Begeman83e75ec2005-09-06 04:43:02 +00001212SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001213 SDOperand N0 = N->getOperand(0);
1214 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001215 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1216 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001217 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001218
Dan Gohman7f321562007-06-25 16:23:39 +00001219 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001220 if (MVT::isVector(VT)) {
1221 SDOperand FoldedVOp = SimplifyVBinOp(N);
1222 if (FoldedVOp.Val) return FoldedVOp;
1223 }
Dan Gohman7f321562007-06-25 16:23:39 +00001224
Nate Begeman1d4d4142005-09-01 00:19:25 +00001225 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001226 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001227 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001228 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +00001229 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001230 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001231 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001232 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001233 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1234 if (N1.getOpcode() == ISD::SHL) {
1235 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1236 if (isPowerOf2_64(SHC->getValue())) {
1237 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001238 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
1239 DAG.getConstant(Log2_64(SHC->getValue()),
1240 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001241 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001242 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001243 }
1244 }
1245 }
Nate Begeman69575232005-10-20 02:15:44 +00001246 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +00001247 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
1248 SDOperand Op = BuildUDIV(N);
1249 if (Op.Val) return Op;
1250 }
Dan Gohman7f321562007-06-25 16:23:39 +00001251
Dan Gohman613e0d82007-07-03 14:03:57 +00001252 // undef / X -> 0
1253 if (N0.getOpcode() == ISD::UNDEF)
1254 return DAG.getConstant(0, VT);
1255 // X / undef -> undef
1256 if (N1.getOpcode() == ISD::UNDEF)
1257 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001258
Nate Begeman83e75ec2005-09-06 04:43:02 +00001259 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001260}
1261
Nate Begeman83e75ec2005-09-06 04:43:02 +00001262SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001263 SDOperand N0 = N->getOperand(0);
1264 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001265 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1266 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001267 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001268
1269 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001270 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001271 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001272 // If we know the sign bits of both operands are zero, strength reduce to a
1273 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
1274 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001275 if (DAG.MaskedValueIsZero(N1, SignBit) &&
1276 DAG.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +00001277 return DAG.getNode(ISD::UREM, VT, N0, N1);
Chris Lattner26d29902006-10-12 20:58:32 +00001278
1279 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1280 // the remainder operation.
1281 if (N1C && !N1C->isNullValue()) {
1282 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
1283 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1284 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1285 AddToWorkList(Div.Val);
1286 AddToWorkList(Mul.Val);
1287 return Sub;
1288 }
1289
Dan Gohman613e0d82007-07-03 14:03:57 +00001290 // undef % X -> 0
1291 if (N0.getOpcode() == ISD::UNDEF)
1292 return DAG.getConstant(0, VT);
1293 // X % undef -> undef
1294 if (N1.getOpcode() == ISD::UNDEF)
1295 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001296
Nate Begeman83e75ec2005-09-06 04:43:02 +00001297 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001298}
1299
Nate Begeman83e75ec2005-09-06 04:43:02 +00001300SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001301 SDOperand N0 = N->getOperand(0);
1302 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001303 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1304 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001305 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001306
1307 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001308 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001309 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001310 // fold (urem x, pow2) -> (and x, pow2-1)
1311 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +00001312 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001313 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1314 if (N1.getOpcode() == ISD::SHL) {
1315 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1316 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +00001317 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001318 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001319 return DAG.getNode(ISD::AND, VT, N0, Add);
1320 }
1321 }
1322 }
Chris Lattner26d29902006-10-12 20:58:32 +00001323
1324 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1325 // the remainder operation.
1326 if (N1C && !N1C->isNullValue()) {
1327 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
1328 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1329 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1330 AddToWorkList(Div.Val);
1331 AddToWorkList(Mul.Val);
1332 return Sub;
1333 }
1334
Dan Gohman613e0d82007-07-03 14:03:57 +00001335 // undef % X -> 0
1336 if (N0.getOpcode() == ISD::UNDEF)
1337 return DAG.getConstant(0, VT);
1338 // X % undef -> undef
1339 if (N1.getOpcode() == ISD::UNDEF)
1340 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001341
Nate Begeman83e75ec2005-09-06 04:43:02 +00001342 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001343}
1344
Nate Begeman83e75ec2005-09-06 04:43:02 +00001345SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001346 SDOperand N0 = N->getOperand(0);
1347 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001348 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001349 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001350
1351 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001352 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001353 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001354 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001355 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001356 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1357 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001358 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001359 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001360 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001361 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001362
Nate Begeman83e75ec2005-09-06 04:43:02 +00001363 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001364}
1365
Nate Begeman83e75ec2005-09-06 04:43:02 +00001366SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001367 SDOperand N0 = N->getOperand(0);
1368 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001369 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001370 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001371
1372 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001373 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001374 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001375 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001376 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001377 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001378 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001379 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001380 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001381
Nate Begeman83e75ec2005-09-06 04:43:02 +00001382 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001383}
1384
Chris Lattner35e5c142006-05-05 05:51:50 +00001385/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1386/// two operands of the same opcode, try to simplify it.
1387SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1388 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1389 MVT::ValueType VT = N0.getValueType();
1390 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1391
Chris Lattner540121f2006-05-05 06:31:05 +00001392 // For each of OP in AND/OR/XOR:
1393 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1394 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1395 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001396 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001397 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001398 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001399 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1400 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1401 N0.getOperand(0).getValueType(),
1402 N0.getOperand(0), N1.getOperand(0));
1403 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001404 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001405 }
1406
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001407 // For each of OP in SHL/SRL/SRA/AND...
1408 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1409 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1410 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001411 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001412 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001413 N0.getOperand(1) == N1.getOperand(1)) {
1414 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1415 N0.getOperand(0).getValueType(),
1416 N0.getOperand(0), N1.getOperand(0));
1417 AddToWorkList(ORNode.Val);
1418 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1419 }
1420
1421 return SDOperand();
1422}
1423
Nate Begeman83e75ec2005-09-06 04:43:02 +00001424SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001425 SDOperand N0 = N->getOperand(0);
1426 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001427 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001428 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1429 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001430 MVT::ValueType VT = N1.getValueType();
1431
Dan Gohman7f321562007-06-25 16:23:39 +00001432 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001433 if (MVT::isVector(VT)) {
1434 SDOperand FoldedVOp = SimplifyVBinOp(N);
1435 if (FoldedVOp.Val) return FoldedVOp;
1436 }
Dan Gohman7f321562007-06-25 16:23:39 +00001437
Dan Gohman613e0d82007-07-03 14:03:57 +00001438 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001439 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001440 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001441 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001442 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001443 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001444 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001445 if (N0C && !N1C)
1446 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001447 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001448 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001449 return N0;
1450 // if (and x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001451 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001452 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001453 // reassociate and
1454 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1455 if (RAND.Val != 0)
1456 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001457 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001458 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001459 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001460 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001461 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001462 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1463 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001464 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Dan Gohmanea859be2007-06-22 14:59:07 +00001465 if (DAG.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001466 ~N1C->getValue() & InMask)) {
1467 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1468 N0.getOperand(0));
1469
1470 // Replace uses of the AND with uses of the Zero extend node.
1471 CombineTo(N, Zext);
1472
Chris Lattner3603cd62006-02-02 07:17:31 +00001473 // We actually want to replace all uses of the any_extend with the
1474 // zero_extend, to avoid duplicating things. This will later cause this
1475 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001476 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001477 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001478 }
1479 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001480 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1481 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1482 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1483 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1484
1485 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1486 MVT::isInteger(LL.getValueType())) {
1487 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1488 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1489 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001490 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001491 return DAG.getSetCC(VT, ORNode, LR, Op1);
1492 }
1493 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1494 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1495 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001496 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001497 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1498 }
1499 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1500 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1501 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001502 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001503 return DAG.getSetCC(VT, ORNode, LR, Op1);
1504 }
1505 }
1506 // canonicalize equivalent to ll == rl
1507 if (LL == RR && LR == RL) {
1508 Op1 = ISD::getSetCCSwappedOperands(Op1);
1509 std::swap(RL, RR);
1510 }
1511 if (LL == RL && LR == RR) {
1512 bool isInteger = MVT::isInteger(LL.getValueType());
1513 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1514 if (Result != ISD::SETCC_INVALID)
1515 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1516 }
1517 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001518
1519 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1520 if (N0.getOpcode() == N1.getOpcode()) {
1521 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1522 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001523 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001524
Nate Begemande996292006-02-03 22:24:05 +00001525 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1526 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001527 if (!MVT::isVector(VT) &&
1528 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001529 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001530 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001531 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001532 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001533 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001534 // If we zero all the possible extended bits, then we can turn this into
1535 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmanea859be2007-06-22 14:59:07 +00001536 if (DAG.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001537 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001538 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1539 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001540 LN0->getSrcValueOffset(), EVT,
1541 LN0->isVolatile(),
1542 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001543 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001544 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001545 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001546 }
1547 }
1548 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001549 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1550 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001551 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001552 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001553 // If we zero all the possible extended bits, then we can turn this into
1554 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmanea859be2007-06-22 14:59:07 +00001555 if (DAG.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001556 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001557 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1558 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001559 LN0->getSrcValueOffset(), EVT,
1560 LN0->isVolatile(),
1561 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001562 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001563 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001564 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001565 }
1566 }
Chris Lattner15045b62006-02-28 06:35:35 +00001567
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001568 // fold (and (load x), 255) -> (zextload x, i8)
1569 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001570 if (N1C && N0.getOpcode() == ISD::LOAD) {
1571 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1572 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Evan Cheng83060c52007-03-07 08:07:03 +00001573 LN0->getAddressingMode() == ISD::UNINDEXED &&
Evan Cheng466685d2006-10-09 20:57:25 +00001574 N0.hasOneUse()) {
1575 MVT::ValueType EVT, LoadedVT;
1576 if (N1C->getValue() == 255)
1577 EVT = MVT::i8;
1578 else if (N1C->getValue() == 65535)
1579 EVT = MVT::i16;
1580 else if (N1C->getValue() == ~0U)
1581 EVT = MVT::i32;
1582 else
1583 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001584
Evan Cheng2e49f092006-10-11 07:10:22 +00001585 LoadedVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001586 if (EVT != MVT::Other && LoadedVT > EVT &&
1587 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1588 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1589 // For big endian targets, we need to add an offset to the pointer to
1590 // load the correct bytes. For little endian systems, we merely need to
1591 // read fewer bytes from the same pointer.
1592 unsigned PtrOff =
1593 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1594 SDOperand NewPtr = LN0->getBasePtr();
1595 if (!TLI.isLittleEndian())
1596 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1597 DAG.getConstant(PtrOff, PtrType));
1598 AddToWorkList(NewPtr.Val);
1599 SDOperand Load =
1600 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001601 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
1602 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001603 AddToWorkList(N);
1604 CombineTo(N0.Val, Load, Load.getValue(1));
1605 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1606 }
Chris Lattner15045b62006-02-28 06:35:35 +00001607 }
1608 }
1609
Nate Begeman83e75ec2005-09-06 04:43:02 +00001610 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001611}
1612
Nate Begeman83e75ec2005-09-06 04:43:02 +00001613SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001614 SDOperand N0 = N->getOperand(0);
1615 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001616 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001617 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1618 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001619 MVT::ValueType VT = N1.getValueType();
1620 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001621
Dan Gohman7f321562007-06-25 16:23:39 +00001622 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001623 if (MVT::isVector(VT)) {
1624 SDOperand FoldedVOp = SimplifyVBinOp(N);
1625 if (FoldedVOp.Val) return FoldedVOp;
1626 }
Dan Gohman7f321562007-06-25 16:23:39 +00001627
Dan Gohman613e0d82007-07-03 14:03:57 +00001628 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001629 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001630 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001631 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001632 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001633 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001634 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001635 if (N0C && !N1C)
1636 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001637 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001638 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001639 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001640 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001641 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001642 return N1;
1643 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001644 if (N1C &&
Dan Gohmanea859be2007-06-22 14:59:07 +00001645 DAG.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001646 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001647 // reassociate or
1648 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1649 if (ROR.Val != 0)
1650 return ROR;
1651 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1652 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001653 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001654 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1655 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1656 N1),
1657 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001658 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001659 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1660 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1661 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1662 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1663
1664 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1665 MVT::isInteger(LL.getValueType())) {
1666 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1667 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1668 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1669 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1670 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001671 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001672 return DAG.getSetCC(VT, ORNode, LR, Op1);
1673 }
1674 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1675 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1676 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1677 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1678 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001679 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001680 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1681 }
1682 }
1683 // canonicalize equivalent to ll == rl
1684 if (LL == RR && LR == RL) {
1685 Op1 = ISD::getSetCCSwappedOperands(Op1);
1686 std::swap(RL, RR);
1687 }
1688 if (LL == RL && LR == RR) {
1689 bool isInteger = MVT::isInteger(LL.getValueType());
1690 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1691 if (Result != ISD::SETCC_INVALID)
1692 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1693 }
1694 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001695
1696 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1697 if (N0.getOpcode() == N1.getOpcode()) {
1698 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1699 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001700 }
Chris Lattner516b9622006-09-14 20:50:57 +00001701
Chris Lattner1ec72732006-09-14 21:11:37 +00001702 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1703 if (N0.getOpcode() == ISD::AND &&
1704 N1.getOpcode() == ISD::AND &&
1705 N0.getOperand(1).getOpcode() == ISD::Constant &&
1706 N1.getOperand(1).getOpcode() == ISD::Constant &&
1707 // Don't increase # computations.
1708 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1709 // We can only do this xform if we know that bits from X that are set in C2
1710 // but not in C1 are already zero. Likewise for Y.
1711 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1712 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1713
Dan Gohmanea859be2007-06-22 14:59:07 +00001714 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1715 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001716 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1717 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1718 }
1719 }
1720
1721
Chris Lattner516b9622006-09-14 20:50:57 +00001722 // See if this is some rotate idiom.
1723 if (SDNode *Rot = MatchRotate(N0, N1))
1724 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001725
Nate Begeman83e75ec2005-09-06 04:43:02 +00001726 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001727}
1728
Chris Lattner516b9622006-09-14 20:50:57 +00001729
1730/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1731static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1732 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001733 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001734 Mask = Op.getOperand(1);
1735 Op = Op.getOperand(0);
1736 } else {
1737 return false;
1738 }
1739 }
1740
1741 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1742 Shift = Op;
1743 return true;
1744 }
1745 return false;
1746}
1747
1748
1749// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1750// idioms for rotate, and if the target supports rotation instructions, generate
1751// a rot[lr].
1752SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1753 // Must be a legal type. Expanded an promoted things won't work with rotates.
1754 MVT::ValueType VT = LHS.getValueType();
1755 if (!TLI.isTypeLegal(VT)) return 0;
1756
1757 // The target must have at least one rotate flavor.
1758 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1759 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1760 if (!HasROTL && !HasROTR) return 0;
1761
1762 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1763 SDOperand LHSShift; // The shift.
1764 SDOperand LHSMask; // AND value if any.
1765 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1766 return 0; // Not part of a rotate.
1767
1768 SDOperand RHSShift; // The shift.
1769 SDOperand RHSMask; // AND value if any.
1770 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1771 return 0; // Not part of a rotate.
1772
1773 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1774 return 0; // Not shifting the same value.
1775
1776 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1777 return 0; // Shifts must disagree.
1778
1779 // Canonicalize shl to left side in a shl/srl pair.
1780 if (RHSShift.getOpcode() == ISD::SHL) {
1781 std::swap(LHS, RHS);
1782 std::swap(LHSShift, RHSShift);
1783 std::swap(LHSMask , RHSMask );
1784 }
1785
1786 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Scott Michelc9dc1142007-04-02 21:36:32 +00001787 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1788 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1789 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001790
1791 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1792 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001793 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1794 RHSShiftAmt.getOpcode() == ISD::Constant) {
1795 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
1796 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00001797 if ((LShVal + RShVal) != OpSizeInBits)
1798 return 0;
1799
1800 SDOperand Rot;
1801 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001802 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001803 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001804 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001805
1806 // If there is an AND of either shifted operand, apply it to the result.
1807 if (LHSMask.Val || RHSMask.Val) {
1808 uint64_t Mask = MVT::getIntVTBitMask(VT);
1809
1810 if (LHSMask.Val) {
1811 uint64_t RHSBits = (1ULL << LShVal)-1;
1812 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1813 }
1814 if (RHSMask.Val) {
1815 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1816 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1817 }
1818
1819 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1820 }
1821
1822 return Rot.Val;
1823 }
1824
1825 // If there is a mask here, and we have a variable shift, we can't be sure
1826 // that we're masking out the right stuff.
1827 if (LHSMask.Val || RHSMask.Val)
1828 return 0;
1829
1830 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1831 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001832 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
1833 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001834 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001835 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001836 if (SUBC->getValue() == OpSizeInBits)
1837 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001838 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001839 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001840 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001841 }
1842 }
1843
1844 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1845 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001846 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
1847 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001848 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001849 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001850 if (SUBC->getValue() == OpSizeInBits)
1851 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001852 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001853 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001854 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
1855 }
1856 }
1857
1858 // Look for sign/zext/any-extended cases:
1859 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1860 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1861 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
1862 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1863 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1864 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
1865 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
1866 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
1867 if (RExtOp0.getOpcode() == ISD::SUB &&
1868 RExtOp0.getOperand(1) == LExtOp0) {
1869 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
1870 // (rotr x, y)
1871 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
1872 // (rotl x, (sub 32, y))
1873 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
1874 if (SUBC->getValue() == OpSizeInBits) {
1875 if (HasROTL)
1876 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
1877 else
1878 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
1879 }
1880 }
1881 } else if (LExtOp0.getOpcode() == ISD::SUB &&
1882 RExtOp0 == LExtOp0.getOperand(1)) {
1883 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
1884 // (rotl x, y)
1885 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
1886 // (rotr x, (sub 32, y))
1887 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
1888 if (SUBC->getValue() == OpSizeInBits) {
1889 if (HasROTL)
1890 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
1891 else
1892 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
1893 }
1894 }
Chris Lattner516b9622006-09-14 20:50:57 +00001895 }
1896 }
1897
1898 return 0;
1899}
1900
1901
Nate Begeman83e75ec2005-09-06 04:43:02 +00001902SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001903 SDOperand N0 = N->getOperand(0);
1904 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001905 SDOperand LHS, RHS, CC;
1906 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1907 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001908 MVT::ValueType VT = N0.getValueType();
1909
Dan Gohman7f321562007-06-25 16:23:39 +00001910 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001911 if (MVT::isVector(VT)) {
1912 SDOperand FoldedVOp = SimplifyVBinOp(N);
1913 if (FoldedVOp.Val) return FoldedVOp;
1914 }
Dan Gohman7f321562007-06-25 16:23:39 +00001915
Dan Gohman613e0d82007-07-03 14:03:57 +00001916 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001917 if (N0.getOpcode() == ISD::UNDEF)
1918 return N0;
1919 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001920 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001921 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001922 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001923 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001924 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001925 if (N0C && !N1C)
1926 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001927 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001928 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001929 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001930 // reassociate xor
1931 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1932 if (RXOR.Val != 0)
1933 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001934 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001935 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1936 bool isInt = MVT::isInteger(LHS.getValueType());
1937 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1938 isInt);
1939 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001940 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001941 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001942 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001943 assert(0 && "Unhandled SetCC Equivalent!");
1944 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001945 }
Chris Lattner61c5ff42007-09-10 21:39:07 +00001946 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
1947 if (N1C && N1C->getValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
1948 N0.Val->hasOneUse() && isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
1949 SDOperand V = N0.getOperand(0);
1950 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
1951 DAG.getConstant(V.getValueType(), 1));
1952 AddToWorkList(V.Val);
1953 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
1954 }
1955
Nate Begeman99801192005-09-07 23:25:52 +00001956 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Chris Lattner734c91d2006-11-10 21:37:15 +00001957 if (N1C && N1C->getValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00001958 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001959 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001960 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1961 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001962 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1963 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001964 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001965 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001966 }
1967 }
Nate Begeman99801192005-09-07 23:25:52 +00001968 // fold !(x or y) -> (!x and !y) iff x or y are constants
1969 if (N1C && N1C->isAllOnesValue() &&
1970 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001971 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001972 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1973 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001974 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1975 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001976 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001977 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001978 }
1979 }
Nate Begeman223df222005-09-08 20:18:10 +00001980 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1981 if (N1C && N0.getOpcode() == ISD::XOR) {
1982 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1983 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1984 if (N00C)
1985 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1986 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1987 if (N01C)
1988 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1989 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1990 }
1991 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001992 if (N0 == N1) {
1993 if (!MVT::isVector(VT)) {
1994 return DAG.getConstant(0, VT);
1995 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1996 // Produce a vector of zeros.
Dan Gohman51eaa862007-06-14 22:58:02 +00001997 SDOperand El = DAG.getConstant(0, MVT::getVectorElementType(VT));
Chris Lattner4fbdd592006-03-28 19:11:05 +00001998 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001999 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002000 }
2001 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002002
2003 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2004 if (N0.getOpcode() == N1.getOpcode()) {
2005 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
2006 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002007 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002008
Chris Lattner3e104b12006-04-08 04:15:24 +00002009 // Simplify the expression using non-local knowledge.
2010 if (!MVT::isVector(VT) &&
2011 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002012 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002013
Nate Begeman83e75ec2005-09-06 04:43:02 +00002014 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002015}
2016
Nate Begeman83e75ec2005-09-06 04:43:02 +00002017SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002018 SDOperand N0 = N->getOperand(0);
2019 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002020 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2021 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002022 MVT::ValueType VT = N0.getValueType();
2023 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2024
2025 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002026 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002027 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002028 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002029 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002030 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002031 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002032 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002033 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002034 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002035 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002036 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002037 // if (shl x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002038 if (DAG.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002039 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00002040 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002041 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002042 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002043 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002044 N0.getOperand(1).getOpcode() == ISD::Constant) {
2045 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002046 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002047 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002048 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002049 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002050 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002051 }
2052 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2053 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002054 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002055 N0.getOperand(1).getOpcode() == ISD::Constant) {
2056 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002057 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002058 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
2059 DAG.getConstant(~0ULL << c1, VT));
2060 if (c2 > c1)
2061 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002062 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002063 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002064 return DAG.getNode(ISD::SRL, VT, Mask,
2065 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002066 }
2067 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002068 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002069 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002070 DAG.getConstant(~0ULL << N1C->getValue(), VT));
2071 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002072}
2073
Nate Begeman83e75ec2005-09-06 04:43:02 +00002074SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002075 SDOperand N0 = N->getOperand(0);
2076 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002077 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2078 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002079 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002080
2081 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002082 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002083 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002084 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002085 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002086 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002087 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002088 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002089 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002090 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00002091 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002092 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002093 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002094 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002095 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002096 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2097 // sext_inreg.
2098 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
2099 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
2100 MVT::ValueType EVT;
2101 switch (LowBits) {
2102 default: EVT = MVT::Other; break;
2103 case 1: EVT = MVT::i1; break;
2104 case 8: EVT = MVT::i8; break;
2105 case 16: EVT = MVT::i16; break;
2106 case 32: EVT = MVT::i32; break;
2107 }
2108 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
2109 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2110 DAG.getValueType(EVT));
2111 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002112
2113 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2114 if (N1C && N0.getOpcode() == ISD::SRA) {
2115 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2116 unsigned Sum = N1C->getValue() + C1->getValue();
2117 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
2118 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2119 DAG.getConstant(Sum, N1C->getValueType(0)));
2120 }
2121 }
2122
Chris Lattnera8504462006-05-08 20:51:54 +00002123 // Simplify, based on bits shifted out of the LHS.
2124 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2125 return SDOperand(N, 0);
2126
2127
Nate Begeman1d4d4142005-09-01 00:19:25 +00002128 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohmanea859be2007-06-22 14:59:07 +00002129 if (DAG.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002130 return DAG.getNode(ISD::SRL, VT, N0, N1);
2131 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002132}
2133
Nate Begeman83e75ec2005-09-06 04:43:02 +00002134SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002135 SDOperand N0 = N->getOperand(0);
2136 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002137 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2138 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002139 MVT::ValueType VT = N0.getValueType();
2140 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2141
2142 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002143 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002144 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002145 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002146 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002147 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002148 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002149 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002150 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002151 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002152 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002153 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002154 // if (srl x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002155 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002156 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002157
Nate Begeman1d4d4142005-09-01 00:19:25 +00002158 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002159 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002160 N0.getOperand(1).getOpcode() == ISD::Constant) {
2161 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002162 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002163 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002164 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002165 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002166 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002167 }
Chris Lattner350bec02006-04-02 06:11:11 +00002168
Chris Lattner06afe072006-05-05 22:53:17 +00002169 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2170 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2171 // Shifting in all undef bits?
2172 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
2173 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
2174 return DAG.getNode(ISD::UNDEF, VT);
2175
2176 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
2177 AddToWorkList(SmallShift.Val);
2178 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2179 }
2180
Chris Lattner3657ffe2006-10-12 20:23:19 +00002181 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2182 // bit, which is unmodified by sra.
2183 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
2184 if (N0.getOpcode() == ISD::SRA)
2185 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2186 }
2187
Chris Lattner350bec02006-04-02 06:11:11 +00002188 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2189 if (N1C && N0.getOpcode() == ISD::CTLZ &&
2190 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
2191 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002192 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002193
2194 // If any of the input bits are KnownOne, then the input couldn't be all
2195 // zeros, thus the result of the srl will always be zero.
2196 if (KnownOne) return DAG.getConstant(0, VT);
2197
2198 // If all of the bits input the to ctlz node are known to be zero, then
2199 // the result of the ctlz is "32" and the result of the shift is one.
2200 uint64_t UnknownBits = ~KnownZero & Mask;
2201 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2202
2203 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2204 if ((UnknownBits & (UnknownBits-1)) == 0) {
2205 // Okay, we know that only that the single bit specified by UnknownBits
2206 // could be set on input to the CTLZ node. If this bit is set, the SRL
2207 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2208 // to an SRL,XOR pair, which is likely to simplify more.
2209 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
2210 SDOperand Op = N0.getOperand(0);
2211 if (ShAmt) {
2212 Op = DAG.getNode(ISD::SRL, VT, Op,
2213 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2214 AddToWorkList(Op.Val);
2215 }
2216 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2217 }
2218 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002219
2220 // fold operands of srl based on knowledge that the low bits are not
2221 // demanded.
2222 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2223 return SDOperand(N, 0);
2224
Nate Begeman83e75ec2005-09-06 04:43:02 +00002225 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002226}
2227
Nate Begeman83e75ec2005-09-06 04:43:02 +00002228SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002229 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002230 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002231
2232 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002233 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002234 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002235 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002236}
2237
Nate Begeman83e75ec2005-09-06 04:43:02 +00002238SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002239 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002240 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002241
2242 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002243 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002244 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002245 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002246}
2247
Nate Begeman83e75ec2005-09-06 04:43:02 +00002248SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002249 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002250 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002251
2252 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002253 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002254 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002255 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002256}
2257
Nate Begeman452d7be2005-09-16 00:54:12 +00002258SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2259 SDOperand N0 = N->getOperand(0);
2260 SDOperand N1 = N->getOperand(1);
2261 SDOperand N2 = N->getOperand(2);
2262 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2263 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2264 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2265 MVT::ValueType VT = N->getValueType(0);
Evan Cheng571c4782007-08-18 05:57:05 +00002266 MVT::ValueType VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002267
Nate Begeman452d7be2005-09-16 00:54:12 +00002268 // fold select C, X, X -> X
2269 if (N1 == N2)
2270 return N1;
2271 // fold select true, X, Y -> X
2272 if (N0C && !N0C->isNullValue())
2273 return N1;
2274 // fold select false, X, Y -> Y
2275 if (N0C && N0C->isNullValue())
2276 return N2;
2277 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002278 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002279 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002280 // fold select C, 0, 1 -> ~C
2281 if (MVT::isInteger(VT) && MVT::isInteger(VT0) &&
2282 N1C && N2C && N1C->isNullValue() && N2C->getValue() == 1) {
2283 SDOperand XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
2284 if (VT == VT0)
2285 return XORNode;
2286 AddToWorkList(XORNode.Val);
2287 if (MVT::getSizeInBits(VT) > MVT::getSizeInBits(VT0))
2288 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2289 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2290 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002291 // fold select C, 0, X -> ~C & X
Evan Cheng571c4782007-08-18 05:57:05 +00002292 if (VT == VT0 && N1C && N1C->isNullValue()) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002293 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002294 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002295 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2296 }
2297 // fold select C, X, 1 -> ~C | X
Evan Cheng571c4782007-08-18 05:57:05 +00002298 if (VT == VT0 && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002299 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002300 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002301 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2302 }
2303 // fold select C, X, 0 -> C & X
2304 // FIXME: this should check for C type == X type, not i1?
2305 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2306 return DAG.getNode(ISD::AND, VT, N0, N1);
2307 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2308 if (MVT::i1 == VT && N0 == N1)
2309 return DAG.getNode(ISD::OR, VT, N0, N2);
2310 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2311 if (MVT::i1 == VT && N0 == N2)
2312 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002313
Chris Lattner40c62d52005-10-18 06:04:22 +00002314 // If we can fold this based on the true/false value, do so.
2315 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002316 return SDOperand(N, 0); // Don't revisit N.
2317
Nate Begeman44728a72005-09-19 22:34:01 +00002318 // fold selects based on a setcc into other things, such as min/max/abs
2319 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00002320 // FIXME:
2321 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2322 // having to say they don't support SELECT_CC on every type the DAG knows
2323 // about, since there is no way to mark an opcode illegal at all value types
2324 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2325 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2326 N1, N2, N0.getOperand(2));
2327 else
2328 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002329 return SDOperand();
2330}
2331
2332SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002333 SDOperand N0 = N->getOperand(0);
2334 SDOperand N1 = N->getOperand(1);
2335 SDOperand N2 = N->getOperand(2);
2336 SDOperand N3 = N->getOperand(3);
2337 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002338 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2339
Nate Begeman44728a72005-09-19 22:34:01 +00002340 // fold select_cc lhs, rhs, x, x, cc -> x
2341 if (N2 == N3)
2342 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002343
Chris Lattner5f42a242006-09-20 06:19:26 +00002344 // Determine if the condition we're dealing with is constant
2345 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002346 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002347
2348 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
2349 if (SCCC->getValue())
2350 return N2; // cond always true -> true val
2351 else
2352 return N3; // cond always false -> false val
2353 }
2354
2355 // Fold to a simpler select_cc
2356 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2357 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2358 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2359 SCC.getOperand(2));
2360
Chris Lattner40c62d52005-10-18 06:04:22 +00002361 // If we can fold this based on the true/false value, do so.
2362 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002363 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002364
Nate Begeman44728a72005-09-19 22:34:01 +00002365 // fold select_cc into other things, such as min/max/abs
2366 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002367}
2368
2369SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2370 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2371 cast<CondCodeSDNode>(N->getOperand(2))->get());
2372}
2373
Nate Begeman83e75ec2005-09-06 04:43:02 +00002374SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002375 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002376 MVT::ValueType VT = N->getValueType(0);
2377
Nate Begeman1d4d4142005-09-01 00:19:25 +00002378 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002379 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002380 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002381
Nate Begeman1d4d4142005-09-01 00:19:25 +00002382 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002383 // fold (sext (aext x)) -> (sext x)
2384 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002385 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002386
Evan Chengc88138f2007-03-22 01:54:19 +00002387 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2388 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Chris Lattner22558872007-02-26 03:13:59 +00002389 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002390 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002391 if (NarrowLoad.Val) {
2392 if (NarrowLoad.Val != N0.Val)
2393 CombineTo(N0.Val, NarrowLoad);
2394 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2395 }
Evan Chengc88138f2007-03-22 01:54:19 +00002396 }
2397
2398 // See if the value being truncated is already sign extended. If so, just
2399 // eliminate the trunc/sext pair.
2400 if (N0.getOpcode() == ISD::TRUNCATE) {
Chris Lattner6007b842006-09-21 06:00:20 +00002401 SDOperand Op = N0.getOperand(0);
Chris Lattner22558872007-02-26 03:13:59 +00002402 unsigned OpBits = MVT::getSizeInBits(Op.getValueType());
2403 unsigned MidBits = MVT::getSizeInBits(N0.getValueType());
2404 unsigned DestBits = MVT::getSizeInBits(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002405 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002406
2407 if (OpBits == DestBits) {
2408 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2409 // bits, it is already ready.
2410 if (NumSignBits > DestBits-MidBits)
2411 return Op;
2412 } else if (OpBits < DestBits) {
2413 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2414 // bits, just sext from i32.
2415 if (NumSignBits > OpBits-MidBits)
2416 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2417 } else {
2418 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2419 // bits, just truncate to i32.
2420 if (NumSignBits > OpBits-MidBits)
2421 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002422 }
Chris Lattner22558872007-02-26 03:13:59 +00002423
2424 // fold (sext (truncate x)) -> (sextinreg x).
2425 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2426 N0.getValueType())) {
2427 if (Op.getValueType() < VT)
2428 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2429 else if (Op.getValueType() > VT)
2430 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2431 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2432 DAG.getValueType(N0.getValueType()));
2433 }
Chris Lattner6007b842006-09-21 06:00:20 +00002434 }
Chris Lattner310b5782006-05-06 23:06:26 +00002435
Evan Cheng110dec22005-12-14 02:19:23 +00002436 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002437 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002438 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng466685d2006-10-09 20:57:25 +00002439 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2440 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2441 LN0->getBasePtr(), LN0->getSrcValue(),
2442 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002443 N0.getValueType(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002444 LN0->isVolatile(),
2445 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002446 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00002447 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2448 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002449 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002450 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002451
2452 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2453 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002454 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2455 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002456 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002457 MVT::ValueType EVT = LN0->getLoadedVT();
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002458 if (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
2459 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2460 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002461 LN0->getSrcValueOffset(), EVT,
2462 LN0->isVolatile(),
2463 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002464 CombineTo(N, ExtLoad);
2465 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2466 ExtLoad.getValue(1));
2467 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2468 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002469 }
2470
Chris Lattner20a35c32007-04-11 05:32:27 +00002471 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2472 if (N0.getOpcode() == ISD::SETCC) {
2473 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002474 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2475 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2476 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2477 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002478 }
2479
Nate Begeman83e75ec2005-09-06 04:43:02 +00002480 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002481}
2482
Nate Begeman83e75ec2005-09-06 04:43:02 +00002483SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002484 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002485 MVT::ValueType VT = N->getValueType(0);
2486
Nate Begeman1d4d4142005-09-01 00:19:25 +00002487 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002488 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002489 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002490 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002491 // fold (zext (aext x)) -> (zext x)
2492 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002493 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002494
Evan Chengc88138f2007-03-22 01:54:19 +00002495 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2496 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002497 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002498 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002499 if (NarrowLoad.Val) {
2500 if (NarrowLoad.Val != N0.Val)
2501 CombineTo(N0.Val, NarrowLoad);
2502 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2503 }
Evan Chengc88138f2007-03-22 01:54:19 +00002504 }
2505
Chris Lattner6007b842006-09-21 06:00:20 +00002506 // fold (zext (truncate x)) -> (and x, mask)
2507 if (N0.getOpcode() == ISD::TRUNCATE &&
2508 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2509 SDOperand Op = N0.getOperand(0);
2510 if (Op.getValueType() < VT) {
2511 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2512 } else if (Op.getValueType() > VT) {
2513 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2514 }
2515 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2516 }
2517
Chris Lattner111c2282006-09-21 06:14:31 +00002518 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2519 if (N0.getOpcode() == ISD::AND &&
2520 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2521 N0.getOperand(1).getOpcode() == ISD::Constant) {
2522 SDOperand X = N0.getOperand(0).getOperand(0);
2523 if (X.getValueType() < VT) {
2524 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2525 } else if (X.getValueType() > VT) {
2526 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2527 }
2528 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2529 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2530 }
2531
Evan Cheng110dec22005-12-14 02:19:23 +00002532 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002533 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002534 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002535 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2536 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2537 LN0->getBasePtr(), LN0->getSrcValue(),
2538 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002539 N0.getValueType(),
2540 LN0->isVolatile(),
2541 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002542 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00002543 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2544 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002545 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00002546 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002547
2548 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2549 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002550 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2551 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002552 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002553 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002554 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2555 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002556 LN0->getSrcValueOffset(), EVT,
2557 LN0->isVolatile(),
2558 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002559 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002560 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2561 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002562 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002563 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002564
2565 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2566 if (N0.getOpcode() == ISD::SETCC) {
2567 SDOperand SCC =
2568 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2569 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00002570 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2571 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002572 }
2573
Nate Begeman83e75ec2005-09-06 04:43:02 +00002574 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002575}
2576
Chris Lattner5ffc0662006-05-05 05:58:59 +00002577SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2578 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002579 MVT::ValueType VT = N->getValueType(0);
2580
2581 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002582 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002583 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2584 // fold (aext (aext x)) -> (aext x)
2585 // fold (aext (zext x)) -> (zext x)
2586 // fold (aext (sext x)) -> (sext x)
2587 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2588 N0.getOpcode() == ISD::ZERO_EXTEND ||
2589 N0.getOpcode() == ISD::SIGN_EXTEND)
2590 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2591
Evan Chengc88138f2007-03-22 01:54:19 +00002592 // fold (aext (truncate (load x))) -> (aext (smaller load x))
2593 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
2594 if (N0.getOpcode() == ISD::TRUNCATE) {
2595 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002596 if (NarrowLoad.Val) {
2597 if (NarrowLoad.Val != N0.Val)
2598 CombineTo(N0.Val, NarrowLoad);
2599 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
2600 }
Evan Chengc88138f2007-03-22 01:54:19 +00002601 }
2602
Chris Lattner84750582006-09-20 06:29:17 +00002603 // fold (aext (truncate x))
2604 if (N0.getOpcode() == ISD::TRUNCATE) {
2605 SDOperand TruncOp = N0.getOperand(0);
2606 if (TruncOp.getValueType() == VT)
2607 return TruncOp; // x iff x size == zext size.
2608 if (TruncOp.getValueType() > VT)
2609 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2610 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2611 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002612
2613 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2614 if (N0.getOpcode() == ISD::AND &&
2615 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2616 N0.getOperand(1).getOpcode() == ISD::Constant) {
2617 SDOperand X = N0.getOperand(0).getOperand(0);
2618 if (X.getValueType() < VT) {
2619 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2620 } else if (X.getValueType() > VT) {
2621 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2622 }
2623 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2624 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2625 }
2626
Chris Lattner5ffc0662006-05-05 05:58:59 +00002627 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002628 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002629 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002630 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2631 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2632 LN0->getBasePtr(), LN0->getSrcValue(),
2633 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002634 N0.getValueType(),
2635 LN0->isVolatile(),
2636 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002637 CombineTo(N, ExtLoad);
2638 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2639 ExtLoad.getValue(1));
2640 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2641 }
2642
2643 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2644 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2645 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002646 if (N0.getOpcode() == ISD::LOAD &&
2647 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00002648 N0.hasOneUse()) {
2649 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002650 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002651 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
2652 LN0->getChain(), LN0->getBasePtr(),
2653 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002654 LN0->getSrcValueOffset(), EVT,
2655 LN0->isVolatile(),
2656 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002657 CombineTo(N, ExtLoad);
2658 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2659 ExtLoad.getValue(1));
2660 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2661 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002662
2663 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2664 if (N0.getOpcode() == ISD::SETCC) {
2665 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002666 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2667 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00002668 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00002669 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00002670 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002671 }
2672
Chris Lattner5ffc0662006-05-05 05:58:59 +00002673 return SDOperand();
2674}
2675
Evan Chengc88138f2007-03-22 01:54:19 +00002676/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
2677/// bits and then truncated to a narrower type and where N is a multiple
2678/// of number of bits of the narrower type, transform it to a narrower load
2679/// from address + N / num of bits of new type. If the result is to be
2680/// extended, also fold the extension to form a extending load.
2681SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
2682 unsigned Opc = N->getOpcode();
2683 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
2684 SDOperand N0 = N->getOperand(0);
2685 MVT::ValueType VT = N->getValueType(0);
2686 MVT::ValueType EVT = N->getValueType(0);
2687
Evan Chenge177e302007-03-23 22:13:36 +00002688 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
2689 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00002690 if (Opc == ISD::SIGN_EXTEND_INREG) {
2691 ExtType = ISD::SEXTLOAD;
2692 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00002693 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
2694 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00002695 }
2696
2697 unsigned EVTBits = MVT::getSizeInBits(EVT);
2698 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00002699 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00002700 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
2701 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2702 ShAmt = N01->getValue();
2703 // Is the shift amount a multiple of size of VT?
2704 if ((ShAmt & (EVTBits-1)) == 0) {
2705 N0 = N0.getOperand(0);
2706 if (MVT::getSizeInBits(N0.getValueType()) <= EVTBits)
2707 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00002708 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00002709 }
2710 }
2711 }
2712
2713 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
2714 // Do not allow folding to i1 here. i1 is implicitly stored in memory in
2715 // zero extended form: by shrinking the load, we lose track of the fact
2716 // that it is already zero extended.
2717 // FIXME: This should be reevaluated.
2718 VT != MVT::i1) {
2719 assert(MVT::getSizeInBits(N0.getValueType()) > EVTBits &&
2720 "Cannot truncate to larger type!");
2721 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2722 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00002723 // For big endian targets, we need to adjust the offset to the pointer to
2724 // load the correct bytes.
2725 if (!TLI.isLittleEndian())
2726 ShAmt = MVT::getSizeInBits(N0.getValueType()) - ShAmt - EVTBits;
2727 uint64_t PtrOff = ShAmt / 8;
Evan Chengc88138f2007-03-22 01:54:19 +00002728 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
2729 DAG.getConstant(PtrOff, PtrType));
2730 AddToWorkList(NewPtr.Val);
2731 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
2732 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002733 LN0->getSrcValue(), LN0->getSrcValueOffset(),
2734 LN0->isVolatile(), LN0->getAlignment())
Evan Chengc88138f2007-03-22 01:54:19 +00002735 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002736 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
2737 LN0->isVolatile(), LN0->getAlignment());
Evan Chengc88138f2007-03-22 01:54:19 +00002738 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00002739 if (CombineSRL) {
2740 std::vector<SDNode*> NowDead;
2741 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1), NowDead);
2742 CombineTo(N->getOperand(0).Val, Load);
2743 } else
2744 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00002745 if (ShAmt) {
2746 if (Opc == ISD::SIGN_EXTEND_INREG)
2747 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
2748 else
2749 return DAG.getNode(Opc, VT, Load);
2750 }
Evan Chengc88138f2007-03-22 01:54:19 +00002751 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2752 }
2753
2754 return SDOperand();
2755}
2756
Chris Lattner5ffc0662006-05-05 05:58:59 +00002757
Nate Begeman83e75ec2005-09-06 04:43:02 +00002758SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002759 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002760 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002761 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002762 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00002763 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002764
Nate Begeman1d4d4142005-09-01 00:19:25 +00002765 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00002766 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00002767 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00002768
Chris Lattner541a24f2006-05-06 22:43:44 +00002769 // If the input is already sign extended, just drop the extension.
Dan Gohmanea859be2007-06-22 14:59:07 +00002770 if (DAG.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00002771 return N0;
2772
Nate Begeman646d7e22005-09-02 21:18:40 +00002773 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
2774 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2775 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00002776 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002777 }
Chris Lattner4b37e872006-05-08 21:18:59 +00002778
Chris Lattner95a5e052007-04-17 19:03:21 +00002779 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohmanea859be2007-06-22 14:59:07 +00002780 if (DAG.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00002781 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00002782
Chris Lattner95a5e052007-04-17 19:03:21 +00002783 // fold operands of sext_in_reg based on knowledge that the top bits are not
2784 // demanded.
2785 if (SimplifyDemandedBits(SDOperand(N, 0)))
2786 return SDOperand(N, 0);
2787
Evan Chengc88138f2007-03-22 01:54:19 +00002788 // fold (sext_in_reg (load x)) -> (smaller sextload x)
2789 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
2790 SDOperand NarrowLoad = ReduceLoadWidth(N);
2791 if (NarrowLoad.Val)
2792 return NarrowLoad;
2793
Chris Lattner4b37e872006-05-08 21:18:59 +00002794 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
2795 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
2796 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
2797 if (N0.getOpcode() == ISD::SRL) {
2798 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2799 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
2800 // We can turn this into an SRA iff the input to the SRL is already sign
2801 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00002802 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Chris Lattner4b37e872006-05-08 21:18:59 +00002803 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
2804 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
2805 }
2806 }
Evan Chengc88138f2007-03-22 01:54:19 +00002807
Nate Begemanded49632005-10-13 03:11:28 +00002808 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00002809 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002810 ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002811 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002812 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002813 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2814 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2815 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002816 LN0->getSrcValueOffset(), EVT,
2817 LN0->isVolatile(),
2818 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002819 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002820 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002821 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002822 }
2823 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00002824 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
2825 N0.hasOneUse() &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002826 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002827 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002828 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2829 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2830 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002831 LN0->getSrcValueOffset(), EVT,
2832 LN0->isVolatile(),
2833 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002834 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002835 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002836 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002837 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002838 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002839}
2840
Nate Begeman83e75ec2005-09-06 04:43:02 +00002841SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002842 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002843 MVT::ValueType VT = N->getValueType(0);
2844
2845 // noop truncate
2846 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002847 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002848 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002849 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002850 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002851 // fold (truncate (truncate x)) -> (truncate x)
2852 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002853 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002854 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00002855 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2856 N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner32ba1aa2006-11-20 18:05:46 +00002857 if (N0.getOperand(0).getValueType() < VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002858 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00002859 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Chris Lattner32ba1aa2006-11-20 18:05:46 +00002860 else if (N0.getOperand(0).getValueType() > VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002861 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002862 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002863 else
2864 // if the source and dest are the same type, we can drop both the extend
2865 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002866 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002867 }
Evan Cheng007b69e2007-03-21 20:14:05 +00002868
Nate Begeman3df4d522005-10-12 20:40:40 +00002869 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00002870 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00002871 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002872}
2873
Chris Lattner94683772005-12-23 05:30:37 +00002874SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2875 SDOperand N0 = N->getOperand(0);
2876 MVT::ValueType VT = N->getValueType(0);
2877
Dan Gohman7f321562007-06-25 16:23:39 +00002878 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
2879 // Only do this before legalize, since afterward the target may be depending
2880 // on the bitconvert.
2881 // First check to see if this is all constant.
2882 if (!AfterLegalize &&
2883 N0.getOpcode() == ISD::BUILD_VECTOR && N0.Val->hasOneUse() &&
2884 MVT::isVector(VT)) {
2885 bool isSimple = true;
2886 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
2887 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2888 N0.getOperand(i).getOpcode() != ISD::Constant &&
2889 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2890 isSimple = false;
2891 break;
2892 }
2893
2894 MVT::ValueType DestEltVT = MVT::getVectorElementType(N->getValueType(0));
2895 assert(!MVT::isVector(DestEltVT) &&
2896 "Element type of vector ValueType must not be vector!");
2897 if (isSimple) {
2898 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.Val, DestEltVT);
2899 }
2900 }
2901
Chris Lattner94683772005-12-23 05:30:37 +00002902 // If the input is a constant, let getNode() fold it.
2903 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2904 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2905 if (Res.Val != N) return Res;
2906 }
2907
Chris Lattnerc8547d82005-12-23 05:37:50 +00002908 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2909 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00002910
Chris Lattner57104102005-12-23 05:44:41 +00002911 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00002912 // If the resultant load doesn't need a higher alignment than the original!
2913 if (ISD::isNormalLoad(N0.Val) && N0.hasOneUse() &&
Evan Cheng59d5b682007-05-07 21:27:48 +00002914 TLI.isOperationLegal(ISD::LOAD, VT)) {
Evan Cheng466685d2006-10-09 20:57:25 +00002915 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00002916 unsigned Align = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00002917 getABITypeAlignment(MVT::getTypeForValueType(VT));
Evan Cheng59d5b682007-05-07 21:27:48 +00002918 unsigned OrigAlign = LN0->getAlignment();
2919 if (Align <= OrigAlign) {
2920 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
2921 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002922 LN0->isVolatile(), Align);
Evan Cheng59d5b682007-05-07 21:27:48 +00002923 AddToWorkList(N);
2924 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2925 Load.getValue(1));
2926 return Load;
2927 }
Chris Lattner57104102005-12-23 05:44:41 +00002928 }
2929
Chris Lattner94683772005-12-23 05:30:37 +00002930 return SDOperand();
2931}
2932
Dan Gohman7f321562007-06-25 16:23:39 +00002933/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00002934/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2935/// destination element value type.
2936SDOperand DAGCombiner::
Dan Gohman7f321562007-06-25 16:23:39 +00002937ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002938 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2939
2940 // If this is already the right type, we're done.
2941 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2942
2943 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2944 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2945
2946 // If this is a conversion of N elements of one type to N elements of another
2947 // type, convert each element. This handles FP<->INT cases.
2948 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002949 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00002950 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002951 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002952 AddToWorkList(Ops.back().Val);
2953 }
Dan Gohman7f321562007-06-25 16:23:39 +00002954 MVT::ValueType VT =
2955 MVT::getVectorType(DstEltVT,
2956 MVT::getVectorNumElements(BV->getValueType(0)));
2957 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002958 }
2959
2960 // Otherwise, we're growing or shrinking the elements. To avoid having to
2961 // handle annoying details of growing/shrinking FP values, we convert them to
2962 // int first.
2963 if (MVT::isFloatingPoint(SrcEltVT)) {
2964 // Convert the input float vector to a int vector where the elements are the
2965 // same sizes.
2966 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2967 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00002968 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00002969 SrcEltVT = IntVT;
2970 }
2971
2972 // Now we know the input is an integer vector. If the output is a FP type,
2973 // convert to integer first, then to FP of the right size.
2974 if (MVT::isFloatingPoint(DstEltVT)) {
2975 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2976 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00002977 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00002978
2979 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00002980 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00002981 }
2982
2983 // Okay, we know the src/dst types are both integers of differing types.
2984 // Handling growing first.
2985 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2986 if (SrcBitSize < DstBitSize) {
2987 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2988
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002989 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00002990 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00002991 i += NumInputsPerOutput) {
2992 bool isLE = TLI.isLittleEndian();
2993 uint64_t NewBits = 0;
2994 bool EltIsUndef = true;
2995 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2996 // Shift the previously computed bits over.
2997 NewBits <<= SrcBitSize;
2998 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2999 if (Op.getOpcode() == ISD::UNDEF) continue;
3000 EltIsUndef = false;
3001
3002 NewBits |= cast<ConstantSDNode>(Op)->getValue();
3003 }
3004
3005 if (EltIsUndef)
3006 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3007 else
3008 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3009 }
3010
Dan Gohman7f321562007-06-25 16:23:39 +00003011 MVT::ValueType VT = MVT::getVectorType(DstEltVT,
3012 Ops.size());
3013 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003014 }
3015
3016 // Finally, this must be the case where we are shrinking elements: each input
3017 // turns into multiple outputs.
3018 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003019 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003020 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003021 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3022 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3023 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3024 continue;
3025 }
3026 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
3027
3028 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
3029 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
3030 OpVal >>= DstBitSize;
3031 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
3032 }
3033
3034 // For big endian targets, swap the order of the pieces of each element.
3035 if (!TLI.isLittleEndian())
3036 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3037 }
Dan Gohman7f321562007-06-25 16:23:39 +00003038 MVT::ValueType VT = MVT::getVectorType(DstEltVT, Ops.size());
3039 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003040}
3041
3042
3043
Chris Lattner01b3d732005-09-28 22:28:18 +00003044SDOperand DAGCombiner::visitFADD(SDNode *N) {
3045 SDOperand N0 = N->getOperand(0);
3046 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003047 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3048 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003049 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003050
Dan Gohman7f321562007-06-25 16:23:39 +00003051 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003052 if (MVT::isVector(VT)) {
3053 SDOperand FoldedVOp = SimplifyVBinOp(N);
3054 if (FoldedVOp.Val) return FoldedVOp;
3055 }
Dan Gohman7f321562007-06-25 16:23:39 +00003056
Nate Begemana0e221d2005-10-18 00:28:13 +00003057 // fold (fadd c1, c2) -> c1+c2
3058 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003059 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003060 // canonicalize constant to RHS
3061 if (N0CFP && !N1CFP)
3062 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003063 // fold (A + (-B)) -> A-B
Chris Lattner29446522007-05-14 22:04:50 +00003064 if (isNegatibleForFree(N1) == 2)
3065 return DAG.getNode(ISD::FSUB, VT, N0, GetNegatedExpression(N1, DAG));
Chris Lattner01b3d732005-09-28 22:28:18 +00003066 // fold ((-A) + B) -> B-A
Chris Lattner29446522007-05-14 22:04:50 +00003067 if (isNegatibleForFree(N0) == 2)
3068 return DAG.getNode(ISD::FSUB, VT, N1, GetNegatedExpression(N0, DAG));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003069
3070 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3071 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
3072 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3073 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3074 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3075
Chris Lattner01b3d732005-09-28 22:28:18 +00003076 return SDOperand();
3077}
3078
3079SDOperand DAGCombiner::visitFSUB(SDNode *N) {
3080 SDOperand N0 = N->getOperand(0);
3081 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003082 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3083 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003084 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003085
Dan Gohman7f321562007-06-25 16:23:39 +00003086 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003087 if (MVT::isVector(VT)) {
3088 SDOperand FoldedVOp = SimplifyVBinOp(N);
3089 if (FoldedVOp.Val) return FoldedVOp;
3090 }
Dan Gohman7f321562007-06-25 16:23:39 +00003091
Nate Begemana0e221d2005-10-18 00:28:13 +00003092 // fold (fsub c1, c2) -> c1-c2
3093 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003094 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003095 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003096 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Dan Gohman23ff1822007-07-02 15:48:56 +00003097 if (isNegatibleForFree(N1))
3098 return GetNegatedExpression(N1, DAG);
3099 return DAG.getNode(ISD::FNEG, VT, N1);
3100 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003101 // fold (A-(-B)) -> A+B
Chris Lattner29446522007-05-14 22:04:50 +00003102 if (isNegatibleForFree(N1))
3103 return DAG.getNode(ISD::FADD, VT, N0, GetNegatedExpression(N1, DAG));
3104
Chris Lattner01b3d732005-09-28 22:28:18 +00003105 return SDOperand();
3106}
3107
3108SDOperand DAGCombiner::visitFMUL(SDNode *N) {
3109 SDOperand N0 = N->getOperand(0);
3110 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003111 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3112 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003113 MVT::ValueType VT = N->getValueType(0);
3114
Dan Gohman7f321562007-06-25 16:23:39 +00003115 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003116 if (MVT::isVector(VT)) {
3117 SDOperand FoldedVOp = SimplifyVBinOp(N);
3118 if (FoldedVOp.Val) return FoldedVOp;
3119 }
Dan Gohman7f321562007-06-25 16:23:39 +00003120
Nate Begeman11af4ea2005-10-17 20:40:11 +00003121 // fold (fmul c1, c2) -> c1*c2
3122 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003123 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003124 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003125 if (N0CFP && !N1CFP)
3126 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003127 // fold (fmul X, 2.0) -> (fadd X, X)
3128 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3129 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003130 // fold (fmul X, -1.0) -> (fneg X)
3131 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3132 return DAG.getNode(ISD::FNEG, VT, N0);
3133
3134 // -X * -Y -> X*Y
3135 if (char LHSNeg = isNegatibleForFree(N0)) {
3136 if (char RHSNeg = isNegatibleForFree(N1)) {
3137 // Both can be negated for free, check to see if at least one is cheaper
3138 // negated.
3139 if (LHSNeg == 2 || RHSNeg == 2)
3140 return DAG.getNode(ISD::FMUL, VT, GetNegatedExpression(N0, DAG),
3141 GetNegatedExpression(N1, DAG));
3142 }
3143 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003144
3145 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3146 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
3147 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3148 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3149 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3150
Chris Lattner01b3d732005-09-28 22:28:18 +00003151 return SDOperand();
3152}
3153
3154SDOperand DAGCombiner::visitFDIV(SDNode *N) {
3155 SDOperand N0 = N->getOperand(0);
3156 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003157 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3158 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003159 MVT::ValueType VT = N->getValueType(0);
3160
Dan Gohman7f321562007-06-25 16:23:39 +00003161 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003162 if (MVT::isVector(VT)) {
3163 SDOperand FoldedVOp = SimplifyVBinOp(N);
3164 if (FoldedVOp.Val) return FoldedVOp;
3165 }
Dan Gohman7f321562007-06-25 16:23:39 +00003166
Nate Begemana148d982006-01-18 22:35:16 +00003167 // fold (fdiv c1, c2) -> c1/c2
3168 if (N0CFP && N1CFP)
3169 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003170
3171
3172 // -X / -Y -> X*Y
3173 if (char LHSNeg = isNegatibleForFree(N0)) {
3174 if (char RHSNeg = isNegatibleForFree(N1)) {
3175 // Both can be negated for free, check to see if at least one is cheaper
3176 // negated.
3177 if (LHSNeg == 2 || RHSNeg == 2)
3178 return DAG.getNode(ISD::FDIV, VT, GetNegatedExpression(N0, DAG),
3179 GetNegatedExpression(N1, DAG));
3180 }
3181 }
3182
Chris Lattner01b3d732005-09-28 22:28:18 +00003183 return SDOperand();
3184}
3185
3186SDOperand DAGCombiner::visitFREM(SDNode *N) {
3187 SDOperand N0 = N->getOperand(0);
3188 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003189 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3190 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003191 MVT::ValueType VT = N->getValueType(0);
3192
Nate Begemana148d982006-01-18 22:35:16 +00003193 // fold (frem c1, c2) -> fmod(c1,c2)
3194 if (N0CFP && N1CFP)
3195 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003196
Chris Lattner01b3d732005-09-28 22:28:18 +00003197 return SDOperand();
3198}
3199
Chris Lattner12d83032006-03-05 05:30:57 +00003200SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3201 SDOperand N0 = N->getOperand(0);
3202 SDOperand N1 = N->getOperand(1);
3203 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3204 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3205 MVT::ValueType VT = N->getValueType(0);
3206
3207 if (N0CFP && N1CFP) // Constant fold
3208 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3209
3210 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003211 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003212 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3213 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003214 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003215 return DAG.getNode(ISD::FABS, VT, N0);
3216 else
3217 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3218 }
3219
3220 // copysign(fabs(x), y) -> copysign(x, y)
3221 // copysign(fneg(x), y) -> copysign(x, y)
3222 // copysign(copysign(x,z), y) -> copysign(x, y)
3223 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3224 N0.getOpcode() == ISD::FCOPYSIGN)
3225 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3226
3227 // copysign(x, abs(y)) -> abs(x)
3228 if (N1.getOpcode() == ISD::FABS)
3229 return DAG.getNode(ISD::FABS, VT, N0);
3230
3231 // copysign(x, copysign(y,z)) -> copysign(x, z)
3232 if (N1.getOpcode() == ISD::FCOPYSIGN)
3233 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3234
3235 // copysign(x, fp_extend(y)) -> copysign(x, y)
3236 // copysign(x, fp_round(y)) -> copysign(x, y)
3237 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3238 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3239
3240 return SDOperand();
3241}
3242
3243
Chris Lattner01b3d732005-09-28 22:28:18 +00003244
Nate Begeman83e75ec2005-09-06 04:43:02 +00003245SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003246 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003247 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003248 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003249
3250 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003251 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00003252 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003253 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003254}
3255
Nate Begeman83e75ec2005-09-06 04:43:02 +00003256SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003257 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003258 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003259 MVT::ValueType VT = N->getValueType(0);
3260
Nate Begeman1d4d4142005-09-01 00:19:25 +00003261 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003262 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00003263 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003264 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003265}
3266
Nate Begeman83e75ec2005-09-06 04:43:02 +00003267SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003268 SDOperand N0 = N->getOperand(0);
3269 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3270 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003271
3272 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003273 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003274 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003275 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003276}
3277
Nate Begeman83e75ec2005-09-06 04:43:02 +00003278SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003279 SDOperand N0 = N->getOperand(0);
3280 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3281 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003282
3283 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003284 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003285 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003286 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003287}
3288
Nate Begeman83e75ec2005-09-06 04:43:02 +00003289SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003290 SDOperand N0 = N->getOperand(0);
3291 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3292 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003293
3294 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003295 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003296 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00003297
3298 // fold (fp_round (fp_extend x)) -> x
3299 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3300 return N0.getOperand(0);
3301
3302 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3303 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
3304 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
3305 AddToWorkList(Tmp.Val);
3306 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3307 }
3308
Nate Begeman83e75ec2005-09-06 04:43:02 +00003309 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003310}
3311
Nate Begeman83e75ec2005-09-06 04:43:02 +00003312SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003313 SDOperand N0 = N->getOperand(0);
3314 MVT::ValueType VT = N->getValueType(0);
3315 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003316 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003317
Nate Begeman1d4d4142005-09-01 00:19:25 +00003318 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003319 if (N0CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003320 SDOperand Round = DAG.getConstantFP(N0CFP->getValueAPF(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003321 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003322 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003323 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003324}
3325
Nate Begeman83e75ec2005-09-06 04:43:02 +00003326SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003327 SDOperand N0 = N->getOperand(0);
3328 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3329 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003330
3331 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003332 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003333 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00003334
3335 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00003336 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003337 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003338 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3339 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3340 LN0->getBasePtr(), LN0->getSrcValue(),
3341 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003342 N0.getValueType(),
3343 LN0->isVolatile(),
3344 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003345 CombineTo(N, ExtLoad);
3346 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
3347 ExtLoad.getValue(1));
3348 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3349 }
3350
3351
Nate Begeman83e75ec2005-09-06 04:43:02 +00003352 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003353}
3354
Nate Begeman83e75ec2005-09-06 04:43:02 +00003355SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003356 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003357
Dan Gohman23ff1822007-07-02 15:48:56 +00003358 if (isNegatibleForFree(N0))
3359 return GetNegatedExpression(N0, DAG);
3360
Nate Begeman83e75ec2005-09-06 04:43:02 +00003361 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003362}
3363
Nate Begeman83e75ec2005-09-06 04:43:02 +00003364SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003365 SDOperand N0 = N->getOperand(0);
3366 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3367 MVT::ValueType VT = N->getValueType(0);
3368
Nate Begeman1d4d4142005-09-01 00:19:25 +00003369 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00003370 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003371 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003372 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003373 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003374 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003375 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003376 // fold (fabs (fcopysign x, y)) -> (fabs x)
3377 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
3378 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
3379
Nate Begeman83e75ec2005-09-06 04:43:02 +00003380 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003381}
3382
Nate Begeman44728a72005-09-19 22:34:01 +00003383SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
3384 SDOperand Chain = N->getOperand(0);
3385 SDOperand N1 = N->getOperand(1);
3386 SDOperand N2 = N->getOperand(2);
3387 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3388
3389 // never taken branch, fold to chain
3390 if (N1C && N1C->isNullValue())
3391 return Chain;
3392 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00003393 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00003394 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003395 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
3396 // on the target.
3397 if (N1.getOpcode() == ISD::SETCC &&
3398 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
3399 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
3400 N1.getOperand(0), N1.getOperand(1), N2);
3401 }
Nate Begeman44728a72005-09-19 22:34:01 +00003402 return SDOperand();
3403}
3404
Chris Lattner3ea0b472005-10-05 06:47:48 +00003405// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
3406//
Nate Begeman44728a72005-09-19 22:34:01 +00003407SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00003408 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
3409 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
3410
3411 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00003412 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003413 if (Simp.Val) AddToWorkList(Simp.Val);
3414
Nate Begemane17daeb2005-10-05 21:43:42 +00003415 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
3416
3417 // fold br_cc true, dest -> br dest (unconditional branch)
3418 if (SCCC && SCCC->getValue())
3419 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
3420 N->getOperand(4));
3421 // fold br_cc false, dest -> unconditional fall through
3422 if (SCCC && SCCC->isNullValue())
3423 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00003424
Nate Begemane17daeb2005-10-05 21:43:42 +00003425 // fold to a simpler setcc
3426 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
3427 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
3428 Simp.getOperand(2), Simp.getOperand(0),
3429 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00003430 return SDOperand();
3431}
3432
Chris Lattner448f2192006-11-11 00:39:41 +00003433
3434/// CombineToPreIndexedLoadStore - Try turning a load / store and a
3435/// pre-indexed load / store when the base pointer is a add or subtract
3436/// and it has other uses besides the load / store. After the
3437/// transformation, the new indexed load / store has effectively folded
3438/// the add / subtract in and all of its other uses are redirected to the
3439/// new load / store.
3440bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
3441 if (!AfterLegalize)
3442 return false;
3443
3444 bool isLoad = true;
3445 SDOperand Ptr;
3446 MVT::ValueType VT;
3447 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003448 if (LD->getAddressingMode() != ISD::UNINDEXED)
3449 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003450 VT = LD->getLoadedVT();
Evan Cheng83060c52007-03-07 08:07:03 +00003451 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00003452 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
3453 return false;
3454 Ptr = LD->getBasePtr();
3455 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003456 if (ST->getAddressingMode() != ISD::UNINDEXED)
3457 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003458 VT = ST->getStoredVT();
3459 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
3460 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
3461 return false;
3462 Ptr = ST->getBasePtr();
3463 isLoad = false;
3464 } else
3465 return false;
3466
Chris Lattner9f1794e2006-11-11 00:56:29 +00003467 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
3468 // out. There is no reason to make this a preinc/predec.
3469 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
3470 Ptr.Val->hasOneUse())
3471 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003472
Chris Lattner9f1794e2006-11-11 00:56:29 +00003473 // Ask the target to do addressing mode selection.
3474 SDOperand BasePtr;
3475 SDOperand Offset;
3476 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3477 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
3478 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00003479 // Don't create a indexed load / store with zero offset.
3480 if (isa<ConstantSDNode>(Offset) &&
3481 cast<ConstantSDNode>(Offset)->getValue() == 0)
3482 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00003483
Chris Lattner41e53fd2006-11-11 01:00:15 +00003484 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00003485 // 1) The new base ptr is a frame index.
3486 // 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 +00003487 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00003488 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00003489 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00003490 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00003491
Chris Lattner41e53fd2006-11-11 01:00:15 +00003492 // Check #1. Preinc'ing a frame index would require copying the stack pointer
3493 // (plus the implicit offset) to a register to preinc anyway.
3494 if (isa<FrameIndexSDNode>(BasePtr))
3495 return false;
3496
3497 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003498 if (!isLoad) {
3499 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Chengc843abe2007-05-24 02:35:39 +00003500 if (Val == BasePtr || BasePtr.Val->isPredecessor(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00003501 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003502 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003503
Evan Chengc843abe2007-05-24 02:35:39 +00003504 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003505 bool RealUse = false;
3506 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3507 E = Ptr.Val->use_end(); I != E; ++I) {
3508 SDNode *Use = *I;
3509 if (Use == N)
3510 continue;
3511 if (Use->isPredecessor(N))
3512 return false;
3513
3514 if (!((Use->getOpcode() == ISD::LOAD &&
3515 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
3516 (Use->getOpcode() == ISD::STORE) &&
3517 cast<StoreSDNode>(Use)->getBasePtr() == Ptr))
3518 RealUse = true;
3519 }
3520 if (!RealUse)
3521 return false;
3522
3523 SDOperand Result;
3524 if (isLoad)
3525 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
3526 else
3527 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
3528 ++PreIndexedNodes;
3529 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00003530 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00003531 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
3532 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00003533 std::vector<SDNode*> NowDead;
3534 if (isLoad) {
3535 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
3536 NowDead);
3537 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
3538 NowDead);
3539 } else {
3540 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
3541 NowDead);
3542 }
3543
3544 // Nodes can end up on the worklist more than once. Make sure we do
3545 // not process a node that has been replaced.
3546 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3547 removeFromWorkList(NowDead[i]);
3548 // Finally, since the node is now dead, remove it from the graph.
3549 DAG.DeleteNode(N);
3550
3551 // Replace the uses of Ptr with uses of the updated base value.
3552 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
3553 NowDead);
3554 removeFromWorkList(Ptr.Val);
3555 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3556 removeFromWorkList(NowDead[i]);
3557 DAG.DeleteNode(Ptr.Val);
3558
3559 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00003560}
3561
3562/// CombineToPostIndexedLoadStore - Try combine a load / store with a
3563/// add / sub of the base pointer node into a post-indexed load / store.
3564/// The transformation folded the add / subtract into the new indexed
3565/// load / store effectively and all of its uses are redirected to the
3566/// new load / store.
3567bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
3568 if (!AfterLegalize)
3569 return false;
3570
3571 bool isLoad = true;
3572 SDOperand Ptr;
3573 MVT::ValueType VT;
3574 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003575 if (LD->getAddressingMode() != ISD::UNINDEXED)
3576 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003577 VT = LD->getLoadedVT();
3578 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
3579 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
3580 return false;
3581 Ptr = LD->getBasePtr();
3582 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003583 if (ST->getAddressingMode() != ISD::UNINDEXED)
3584 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003585 VT = ST->getStoredVT();
3586 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
3587 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
3588 return false;
3589 Ptr = ST->getBasePtr();
3590 isLoad = false;
3591 } else
3592 return false;
3593
Evan Chengcc470212006-11-16 00:08:20 +00003594 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00003595 return false;
3596
3597 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3598 E = Ptr.Val->use_end(); I != E; ++I) {
3599 SDNode *Op = *I;
3600 if (Op == N ||
3601 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
3602 continue;
3603
3604 SDOperand BasePtr;
3605 SDOperand Offset;
3606 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3607 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
3608 if (Ptr == Offset)
3609 std::swap(BasePtr, Offset);
3610 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00003611 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00003612 // Don't create a indexed load / store with zero offset.
3613 if (isa<ConstantSDNode>(Offset) &&
3614 cast<ConstantSDNode>(Offset)->getValue() == 0)
3615 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00003616
Chris Lattner9f1794e2006-11-11 00:56:29 +00003617 // Try turning it into a post-indexed load / store except when
3618 // 1) All uses are load / store ops that use it as base ptr.
3619 // 2) Op must be independent of N, i.e. Op is neither a predecessor
3620 // nor a successor of N. Otherwise, if Op is folded that would
3621 // create a cycle.
3622
3623 // Check for #1.
3624 bool TryNext = false;
3625 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
3626 EE = BasePtr.Val->use_end(); II != EE; ++II) {
3627 SDNode *Use = *II;
3628 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00003629 continue;
3630
Chris Lattner9f1794e2006-11-11 00:56:29 +00003631 // If all the uses are load / store addresses, then don't do the
3632 // transformation.
3633 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
3634 bool RealUse = false;
3635 for (SDNode::use_iterator III = Use->use_begin(),
3636 EEE = Use->use_end(); III != EEE; ++III) {
3637 SDNode *UseUse = *III;
3638 if (!((UseUse->getOpcode() == ISD::LOAD &&
3639 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
3640 (UseUse->getOpcode() == ISD::STORE) &&
3641 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use))
3642 RealUse = true;
3643 }
Chris Lattner448f2192006-11-11 00:39:41 +00003644
Chris Lattner9f1794e2006-11-11 00:56:29 +00003645 if (!RealUse) {
3646 TryNext = true;
3647 break;
Chris Lattner448f2192006-11-11 00:39:41 +00003648 }
3649 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003650 }
3651 if (TryNext)
3652 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00003653
Chris Lattner9f1794e2006-11-11 00:56:29 +00003654 // Check for #2
3655 if (!Op->isPredecessor(N) && !N->isPredecessor(Op)) {
3656 SDOperand Result = isLoad
3657 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
3658 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
3659 ++PostIndexedNodes;
3660 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00003661 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00003662 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
3663 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00003664 std::vector<SDNode*> NowDead;
3665 if (isLoad) {
3666 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattner448f2192006-11-11 00:39:41 +00003667 NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003668 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
3669 NowDead);
3670 } else {
3671 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
3672 NowDead);
Chris Lattner448f2192006-11-11 00:39:41 +00003673 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003674
3675 // Nodes can end up on the worklist more than once. Make sure we do
3676 // not process a node that has been replaced.
3677 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3678 removeFromWorkList(NowDead[i]);
3679 // Finally, since the node is now dead, remove it from the graph.
3680 DAG.DeleteNode(N);
3681
3682 // Replace the uses of Use with uses of the updated base value.
3683 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
3684 Result.getValue(isLoad ? 1 : 0),
3685 NowDead);
3686 removeFromWorkList(Op);
3687 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3688 removeFromWorkList(NowDead[i]);
3689 DAG.DeleteNode(Op);
3690
3691 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00003692 }
3693 }
3694 }
3695 return false;
3696}
3697
3698
Chris Lattner01a22022005-10-10 22:04:48 +00003699SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00003700 LoadSDNode *LD = cast<LoadSDNode>(N);
3701 SDOperand Chain = LD->getChain();
3702 SDOperand Ptr = LD->getBasePtr();
Evan Cheng45a7ca92007-05-01 00:38:21 +00003703
3704 // If load is not volatile and there are no uses of the loaded value (and
3705 // the updated indexed value in case of indexed loads), change uses of the
3706 // chain value into uses of the chain input (i.e. delete the dead load).
3707 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00003708 if (N->getValueType(1) == MVT::Other) {
3709 // Unindexed loads.
3710 if (N->hasNUsesOfValue(0, 0))
3711 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
3712 } else {
3713 // Indexed loads.
3714 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
3715 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
3716 SDOperand Undef0 = DAG.getNode(ISD::UNDEF, N->getValueType(0));
3717 SDOperand Undef1 = DAG.getNode(ISD::UNDEF, N->getValueType(1));
3718 SDOperand To[] = { Undef0, Undef1, Chain };
3719 return CombineTo(N, To, 3);
Evan Cheng45a7ca92007-05-01 00:38:21 +00003720 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00003721 }
3722 }
Chris Lattner01a22022005-10-10 22:04:48 +00003723
3724 // If this load is directly stored, replace the load value with the stored
3725 // value.
3726 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003727 // TODO: Handle TRUNCSTORE/LOADEXT
3728 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003729 if (ISD::isNON_TRUNCStore(Chain.Val)) {
3730 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
3731 if (PrevST->getBasePtr() == Ptr &&
3732 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003733 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003734 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003735 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00003736
Jim Laskey7ca56af2006-10-11 13:47:09 +00003737 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00003738 // Walk up chain skipping non-aliasing memory nodes.
3739 SDOperand BetterChain = FindBetterChain(N, Chain);
3740
Jim Laskey6ff23e52006-10-04 16:53:27 +00003741 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00003742 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003743 SDOperand ReplLoad;
3744
Jim Laskey279f0532006-09-25 16:29:54 +00003745 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003746 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
3747 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003748 LD->getSrcValue(), LD->getSrcValueOffset(),
3749 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003750 } else {
3751 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
3752 LD->getValueType(0),
3753 BetterChain, Ptr, LD->getSrcValue(),
3754 LD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003755 LD->getLoadedVT(),
3756 LD->isVolatile(),
3757 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003758 }
Jim Laskey279f0532006-09-25 16:29:54 +00003759
Jim Laskey6ff23e52006-10-04 16:53:27 +00003760 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00003761 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
3762 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00003763
Jim Laskey274062c2006-10-13 23:32:28 +00003764 // Replace uses with load result and token factor. Don't add users
3765 // to work list.
3766 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003767 }
3768 }
3769
Evan Cheng7fc033a2006-11-03 03:06:21 +00003770 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003771 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00003772 return SDOperand(N, 0);
3773
Chris Lattner01a22022005-10-10 22:04:48 +00003774 return SDOperand();
3775}
3776
Chris Lattner87514ca2005-10-10 22:31:19 +00003777SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003778 StoreSDNode *ST = cast<StoreSDNode>(N);
3779 SDOperand Chain = ST->getChain();
3780 SDOperand Value = ST->getValue();
3781 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00003782
Evan Cheng59d5b682007-05-07 21:27:48 +00003783 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00003784 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00003785 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
3786 ST->getAddressingMode() == ISD::UNINDEXED) {
Evan Cheng59d5b682007-05-07 21:27:48 +00003787 unsigned Align = ST->getAlignment();
3788 MVT::ValueType SVT = Value.getOperand(0).getValueType();
3789 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00003790 getABITypeAlignment(MVT::getTypeForValueType(SVT));
Evan Chengc2cd2b22007-05-07 21:36:06 +00003791 if (Align <= OrigAlign && TLI.isOperationLegal(ISD::STORE, SVT))
Evan Cheng59d5b682007-05-07 21:27:48 +00003792 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003793 ST->getSrcValueOffset(), ST->isVolatile(), Align);
Jim Laskey279f0532006-09-25 16:29:54 +00003794 }
3795
Nate Begeman2cbba892006-12-11 02:23:46 +00003796 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00003797 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Evan Cheng25ece662006-12-11 17:25:19 +00003798 if (Value.getOpcode() != ISD::TargetConstantFP) {
3799 SDOperand Tmp;
Chris Lattner62be1a72006-12-12 04:16:14 +00003800 switch (CFP->getValueType(0)) {
3801 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00003802 case MVT::f80: // We don't do this for these yet.
3803 case MVT::f128:
3804 case MVT::ppcf128:
3805 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00003806 case MVT::f32:
3807 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00003808 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
3809 convertToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00003810 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003811 ST->getSrcValueOffset(), ST->isVolatile(),
3812 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00003813 }
3814 break;
3815 case MVT::f64:
3816 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00003817 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
3818 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00003819 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003820 ST->getSrcValueOffset(), ST->isVolatile(),
3821 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00003822 } else if (TLI.isTypeLegal(MVT::i32)) {
3823 // Many FP stores are not make apparent until after legalize, e.g. for
3824 // argument passing. Since this is so common, custom legalize the
3825 // 64-bit integer store into two 32-bit stores.
Dale Johannesen9d5f4562007-09-12 03:30:33 +00003826 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
Chris Lattner62be1a72006-12-12 04:16:14 +00003827 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
3828 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
3829 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
3830
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003831 int SVOffset = ST->getSrcValueOffset();
3832 unsigned Alignment = ST->getAlignment();
3833 bool isVolatile = ST->isVolatile();
3834
Chris Lattner62be1a72006-12-12 04:16:14 +00003835 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003836 ST->getSrcValueOffset(),
3837 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00003838 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3839 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003840 SVOffset += 4;
3841 if (Alignment > 4)
3842 Alignment = 4;
Chris Lattner62be1a72006-12-12 04:16:14 +00003843 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003844 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00003845 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
3846 }
3847 break;
Evan Cheng25ece662006-12-11 17:25:19 +00003848 }
Nate Begeman2cbba892006-12-11 02:23:46 +00003849 }
Nate Begeman2cbba892006-12-11 02:23:46 +00003850 }
3851
Jim Laskey279f0532006-09-25 16:29:54 +00003852 if (CombinerAA) {
3853 // Walk up chain skipping non-aliasing memory nodes.
3854 SDOperand BetterChain = FindBetterChain(N, Chain);
3855
Jim Laskey6ff23e52006-10-04 16:53:27 +00003856 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00003857 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00003858 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00003859 SDOperand ReplStore;
3860 if (ST->isTruncatingStore()) {
3861 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003862 ST->getSrcValue(), ST->getSrcValueOffset(), ST->getStoredVT(),
3863 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00003864 } else {
3865 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003866 ST->getSrcValue(), ST->getSrcValueOffset(),
3867 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00003868 }
3869
Jim Laskey279f0532006-09-25 16:29:54 +00003870 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00003871 SDOperand Token =
3872 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
3873
3874 // Don't add users to work list.
3875 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003876 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00003877 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00003878
Evan Cheng33dbedc2006-11-05 09:31:14 +00003879 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003880 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00003881 return SDOperand(N, 0);
3882
Chris Lattner87514ca2005-10-10 22:31:19 +00003883 return SDOperand();
3884}
3885
Chris Lattnerca242442006-03-19 01:27:56 +00003886SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
3887 SDOperand InVec = N->getOperand(0);
3888 SDOperand InVal = N->getOperand(1);
3889 SDOperand EltNo = N->getOperand(2);
3890
3891 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
3892 // vector with the inserted element.
3893 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
3894 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003895 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00003896 if (Elt < Ops.size())
3897 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003898 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
3899 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00003900 }
3901
3902 return SDOperand();
3903}
3904
Evan Cheng513da432007-10-06 08:19:55 +00003905SDOperand DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
3906 SDOperand InVec = N->getOperand(0);
3907 SDOperand EltNo = N->getOperand(1);
3908
3909 // (vextract (v4f32 s2v (f32 load $addr)), 0) -> (f32 load $addr)
3910 // (vextract (v4i32 bc (v4f32 s2v (f32 load $addr))), 0) -> (i32 load $addr)
3911 if (isa<ConstantSDNode>(EltNo)) {
3912 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
3913 bool NewLoad = false;
3914 if (Elt == 0) {
3915 MVT::ValueType VT = InVec.getValueType();
3916 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3917 MVT::ValueType LVT = EVT;
3918 unsigned NumElts = MVT::getVectorNumElements(VT);
3919 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
3920 MVT::ValueType BCVT = InVec.getOperand(0).getValueType();
3921 if (NumElts != MVT::getVectorNumElements(BCVT))
3922 return SDOperand();
3923 InVec = InVec.getOperand(0);
3924 EVT = MVT::getVectorElementType(BCVT);
3925 NewLoad = true;
3926 }
3927 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3928 InVec.getOperand(0).getValueType() == EVT &&
3929 ISD::isNormalLoad(InVec.getOperand(0).Val) &&
3930 InVec.getOperand(0).hasOneUse()) {
3931 LoadSDNode *LN0 = cast<LoadSDNode>(InVec.getOperand(0));
3932 unsigned Align = LN0->getAlignment();
3933 if (NewLoad) {
3934 // Check the resultant load doesn't need a higher alignment than the
3935 // original load.
3936 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
3937 getABITypeAlignment(MVT::getTypeForValueType(LVT));
3938 if (!TLI.isOperationLegal(ISD::LOAD, LVT) || NewAlign > Align)
3939 return SDOperand();
3940 Align = NewAlign;
3941 }
3942
3943 return DAG.getLoad(LVT, LN0->getChain(), LN0->getBasePtr(),
3944 LN0->getSrcValue(), LN0->getSrcValueOffset(),
3945 LN0->isVolatile(), Align);
3946 }
3947 }
3948 }
3949 return SDOperand();
3950}
3951
3952
Dan Gohman7f321562007-06-25 16:23:39 +00003953SDOperand DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
3954 unsigned NumInScalars = N->getNumOperands();
3955 MVT::ValueType VT = N->getValueType(0);
3956 unsigned NumElts = MVT::getVectorNumElements(VT);
3957 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattnerca242442006-03-19 01:27:56 +00003958
Dan Gohman7f321562007-06-25 16:23:39 +00003959 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
3960 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
3961 // at most two distinct vectors, turn this into a shuffle node.
Chris Lattnerd7648c82006-03-28 20:28:38 +00003962 SDOperand VecIn1, VecIn2;
3963 for (unsigned i = 0; i != NumInScalars; ++i) {
3964 // Ignore undef inputs.
3965 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3966
Dan Gohman7f321562007-06-25 16:23:39 +00003967 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00003968 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00003969 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00003970 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
3971 VecIn1 = VecIn2 = SDOperand(0, 0);
3972 break;
3973 }
3974
Dan Gohman7f321562007-06-25 16:23:39 +00003975 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00003976 // we can't make a shuffle.
3977 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00003978 if (ExtractedFromVec.getValueType() != VT) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00003979 VecIn1 = VecIn2 = SDOperand(0, 0);
3980 break;
3981 }
3982
3983 // Otherwise, remember this. We allow up to two distinct input vectors.
3984 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
3985 continue;
3986
3987 if (VecIn1.Val == 0) {
3988 VecIn1 = ExtractedFromVec;
3989 } else if (VecIn2.Val == 0) {
3990 VecIn2 = ExtractedFromVec;
3991 } else {
3992 // Too many inputs.
3993 VecIn1 = VecIn2 = SDOperand(0, 0);
3994 break;
3995 }
3996 }
3997
3998 // If everything is good, we can make a shuffle operation.
3999 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004000 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004001 for (unsigned i = 0; i != NumInScalars; ++i) {
4002 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004003 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004004 continue;
4005 }
4006
4007 SDOperand Extract = N->getOperand(i);
4008
4009 // If extracting from the first vector, just use the index directly.
4010 if (Extract.getOperand(0) == VecIn1) {
4011 BuildVecIndices.push_back(Extract.getOperand(1));
4012 continue;
4013 }
4014
4015 // Otherwise, use InIdx + VecSize
4016 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Evan Cheng597a3bd2007-01-20 10:10:26 +00004017 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars,
4018 TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004019 }
4020
4021 // Add count and size info.
Dan Gohman7f321562007-06-25 16:23:39 +00004022 MVT::ValueType BuildVecVT =
4023 MVT::getVectorType(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004024
Dan Gohman7f321562007-06-25 16:23:39 +00004025 // Return the new VECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004026 SDOperand Ops[5];
4027 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00004028 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004029 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004030 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004031 // Use an undef build_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00004032 std::vector<SDOperand> UnOps(NumInScalars,
4033 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004034 EltType));
4035 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004036 &UnOps[0], UnOps.size());
4037 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00004038 }
Dan Gohman7f321562007-06-25 16:23:39 +00004039 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004040 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004041 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004042 }
4043
4044 return SDOperand();
4045}
4046
Dan Gohman7f321562007-06-25 16:23:39 +00004047SDOperand DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
4048 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
4049 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
4050 // inputs come from at most two distinct vectors, turn this into a shuffle
4051 // node.
4052
4053 // If we only have one input vector, we don't need to do any concatenation.
4054 if (N->getNumOperands() == 1) {
4055 return N->getOperand(0);
4056 }
4057
4058 return SDOperand();
4059}
4060
Chris Lattner66445d32006-03-28 22:11:53 +00004061SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004062 SDOperand ShufMask = N->getOperand(2);
4063 unsigned NumElts = ShufMask.getNumOperands();
4064
4065 // If the shuffle mask is an identity operation on the LHS, return the LHS.
4066 bool isIdentity = true;
4067 for (unsigned i = 0; i != NumElts; ++i) {
4068 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4069 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
4070 isIdentity = false;
4071 break;
4072 }
4073 }
4074 if (isIdentity) return N->getOperand(0);
4075
4076 // If the shuffle mask is an identity operation on the RHS, return the RHS.
4077 isIdentity = true;
4078 for (unsigned i = 0; i != NumElts; ++i) {
4079 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4080 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
4081 isIdentity = false;
4082 break;
4083 }
4084 }
4085 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004086
4087 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
4088 // needed at all.
4089 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00004090 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004091 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00004092 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004093 for (unsigned i = 0; i != NumElts; ++i)
4094 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
4095 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
4096 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00004097 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004098 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004099 BaseIdx = Idx;
4100 } else {
4101 if (BaseIdx != Idx)
4102 isSplat = false;
4103 if (VecNum != V) {
4104 isUnary = false;
4105 break;
4106 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004107 }
4108 }
4109
4110 SDOperand N0 = N->getOperand(0);
4111 SDOperand N1 = N->getOperand(1);
4112 // Normalize unary shuffle so the RHS is undef.
4113 if (isUnary && VecNum == 1)
4114 std::swap(N0, N1);
4115
Evan Cheng917ec982006-07-21 08:25:53 +00004116 // If it is a splat, check if the argument vector is a build_vector with
4117 // all scalar elements the same.
4118 if (isSplat) {
4119 SDNode *V = N0.Val;
Evan Cheng917ec982006-07-21 08:25:53 +00004120
Dan Gohman7f321562007-06-25 16:23:39 +00004121 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004122 // not the number of vector elements, look through it. Be careful not to
4123 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004124 if (V->getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng59569222006-10-16 22:49:37 +00004125 SDOperand ConvInput = V->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004126 if (MVT::getVectorNumElements(ConvInput.getValueType()) == NumElts)
Evan Cheng59569222006-10-16 22:49:37 +00004127 V = ConvInput.Val;
4128 }
4129
Dan Gohman7f321562007-06-25 16:23:39 +00004130 if (V->getOpcode() == ISD::BUILD_VECTOR) {
4131 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00004132 if (NumElems > BaseIdx) {
4133 SDOperand Base;
4134 bool AllSame = true;
4135 for (unsigned i = 0; i != NumElems; ++i) {
4136 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4137 Base = V->getOperand(i);
4138 break;
4139 }
4140 }
4141 // Splat of <u, u, u, u>, return <u, u, u, u>
4142 if (!Base.Val)
4143 return N0;
4144 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00004145 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00004146 AllSame = false;
4147 break;
4148 }
4149 }
4150 // Splat of <x, x, x, x>, return <x, x, x, x>
4151 if (AllSame)
4152 return N0;
4153 }
4154 }
4155 }
4156
Evan Chenge7bec0d2006-07-20 22:44:41 +00004157 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4158 // into an undef.
4159 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004160 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4161 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004162 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004163 for (unsigned i = 0; i != NumElts; ++i) {
4164 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4165 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4166 MappedOps.push_back(ShufMask.getOperand(i));
4167 } else {
4168 unsigned NewIdx =
4169 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
4170 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
4171 }
4172 }
Dan Gohman7f321562007-06-25 16:23:39 +00004173 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004174 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004175 AddToWorkList(ShufMask.Val);
Dan Gohman7f321562007-06-25 16:23:39 +00004176 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
4177 N0,
4178 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
4179 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00004180 }
Dan Gohman7f321562007-06-25 16:23:39 +00004181
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004182 return SDOperand();
4183}
4184
Evan Cheng44f1f092006-04-20 08:56:16 +00004185/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00004186/// an AND to a vector_shuffle with the destination vector and a zero vector.
4187/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00004188/// vector_shuffle V, Zero, <0, 4, 2, 4>
4189SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
4190 SDOperand LHS = N->getOperand(0);
4191 SDOperand RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00004192 if (N->getOpcode() == ISD::AND) {
4193 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00004194 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004195 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Evan Cheng44f1f092006-04-20 08:56:16 +00004196 std::vector<SDOperand> IdxOps;
4197 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00004198 unsigned NumElts = NumOps;
4199 MVT::ValueType EVT = MVT::getVectorElementType(RHS.getValueType());
Evan Cheng44f1f092006-04-20 08:56:16 +00004200 for (unsigned i = 0; i != NumElts; ++i) {
4201 SDOperand Elt = RHS.getOperand(i);
4202 if (!isa<ConstantSDNode>(Elt))
4203 return SDOperand();
4204 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
4205 IdxOps.push_back(DAG.getConstant(i, EVT));
4206 else if (cast<ConstantSDNode>(Elt)->isNullValue())
4207 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
4208 else
4209 return SDOperand();
4210 }
4211
4212 // Let's see if the target supports this vector_shuffle.
4213 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
4214 return SDOperand();
4215
Dan Gohman7f321562007-06-25 16:23:39 +00004216 // Return the new VECTOR_SHUFFLE node.
4217 MVT::ValueType VT = MVT::getVectorType(EVT, NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00004218 std::vector<SDOperand> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004219 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00004220 Ops.push_back(LHS);
4221 AddToWorkList(LHS.Val);
4222 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00004223 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004224 &ZeroOps[0], ZeroOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004225 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004226 &IdxOps[0], IdxOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004227 SDOperand Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004228 &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004229 if (VT != LHS.getValueType()) {
4230 Result = DAG.getNode(ISD::BIT_CONVERT, LHS.getValueType(), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00004231 }
4232 return Result;
4233 }
4234 }
4235 return SDOperand();
4236}
4237
Dan Gohman7f321562007-06-25 16:23:39 +00004238/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
4239SDOperand DAGCombiner::SimplifyVBinOp(SDNode *N) {
4240 // After legalize, the target may be depending on adds and other
4241 // binary ops to provide legal ways to construct constants or other
4242 // things. Simplifying them may result in a loss of legality.
4243 if (AfterLegalize) return SDOperand();
4244
4245 MVT::ValueType VT = N->getValueType(0);
Dan Gohman05d92fe2007-07-13 20:03:40 +00004246 assert(MVT::isVector(VT) && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00004247
4248 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattneredab1b92006-04-02 03:25:57 +00004249 SDOperand LHS = N->getOperand(0);
4250 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00004251 SDOperand Shuffle = XformToShuffleWithZero(N);
4252 if (Shuffle.Val) return Shuffle;
4253
Dan Gohman7f321562007-06-25 16:23:39 +00004254 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00004255 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00004256 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
4257 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004258 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004259 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Chris Lattneredab1b92006-04-02 03:25:57 +00004260 SDOperand LHSOp = LHS.getOperand(i);
4261 SDOperand RHSOp = RHS.getOperand(i);
4262 // If these two elements can't be folded, bail out.
4263 if ((LHSOp.getOpcode() != ISD::UNDEF &&
4264 LHSOp.getOpcode() != ISD::Constant &&
4265 LHSOp.getOpcode() != ISD::ConstantFP) ||
4266 (RHSOp.getOpcode() != ISD::UNDEF &&
4267 RHSOp.getOpcode() != ISD::Constant &&
4268 RHSOp.getOpcode() != ISD::ConstantFP))
4269 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00004270 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00004271 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
4272 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00004273 if ((RHSOp.getOpcode() == ISD::Constant &&
4274 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
4275 (RHSOp.getOpcode() == ISD::ConstantFP &&
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00004276 cast<ConstantFPSDNode>(RHSOp.Val)->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00004277 break;
4278 }
Dan Gohman7f321562007-06-25 16:23:39 +00004279 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00004280 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00004281 assert((Ops.back().getOpcode() == ISD::UNDEF ||
4282 Ops.back().getOpcode() == ISD::Constant ||
4283 Ops.back().getOpcode() == ISD::ConstantFP) &&
4284 "Scalar binop didn't fold!");
4285 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004286
Dan Gohman7f321562007-06-25 16:23:39 +00004287 if (Ops.size() == LHS.getNumOperands()) {
4288 MVT::ValueType VT = LHS.getValueType();
4289 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004290 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004291 }
4292
4293 return SDOperand();
4294}
4295
Nate Begeman44728a72005-09-19 22:34:01 +00004296SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00004297 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
4298
4299 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
4300 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4301 // If we got a simplified select_cc node back from SimplifySelectCC, then
4302 // break it down into a new SETCC node, and a new SELECT node, and then return
4303 // the SELECT node, since we were called with a SELECT node.
4304 if (SCC.Val) {
4305 // Check to see if we got a select_cc back (to turn into setcc/select).
4306 // Otherwise, just return whatever node we got back, like fabs.
4307 if (SCC.getOpcode() == ISD::SELECT_CC) {
4308 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
4309 SCC.getOperand(0), SCC.getOperand(1),
4310 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00004311 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004312 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
4313 SCC.getOperand(3), SETCC);
4314 }
4315 return SCC;
4316 }
Nate Begeman44728a72005-09-19 22:34:01 +00004317 return SDOperand();
4318}
4319
Chris Lattner40c62d52005-10-18 06:04:22 +00004320/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
4321/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00004322/// select. Callers of this should assume that TheSelect is deleted if this
4323/// returns true. As such, they should return the appropriate thing (e.g. the
4324/// node) back to the top-level of the DAG combiner loop to avoid it being
4325/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00004326///
4327bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
4328 SDOperand RHS) {
4329
4330 // If this is a select from two identical things, try to pull the operation
4331 // through the select.
4332 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00004333 // If this is a load and the token chain is identical, replace the select
4334 // of two loads with a load through a select of the address to load from.
4335 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
4336 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00004337 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00004338 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00004339 LHS.getOperand(0) == RHS.getOperand(0)) {
4340 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
4341 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
4342
4343 // If this is an EXTLOAD, the VT's must match.
Evan Cheng2e49f092006-10-11 07:10:22 +00004344 if (LLD->getLoadedVT() == RLD->getLoadedVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004345 // FIXME: this conflates two src values, discarding one. This is not
4346 // the right thing to do, but nothing uses srcvalues now. When they do,
4347 // turn SrcValue into a list of locations.
4348 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004349 if (TheSelect->getOpcode() == ISD::SELECT) {
4350 // Check that the condition doesn't reach either load. If so, folding
4351 // this will induce a cycle into the DAG.
4352 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4353 !RLD->isPredecessor(TheSelect->getOperand(0).Val)) {
4354 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
4355 TheSelect->getOperand(0), LLD->getBasePtr(),
4356 RLD->getBasePtr());
4357 }
4358 } else {
4359 // Check that the condition doesn't reach either load. If so, folding
4360 // this will induce a cycle into the DAG.
4361 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4362 !RLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4363 !LLD->isPredecessor(TheSelect->getOperand(1).Val) &&
4364 !RLD->isPredecessor(TheSelect->getOperand(1).Val)) {
4365 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00004366 TheSelect->getOperand(0),
4367 TheSelect->getOperand(1),
4368 LLD->getBasePtr(), RLD->getBasePtr(),
4369 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004370 }
Evan Cheng466685d2006-10-09 20:57:25 +00004371 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004372
4373 if (Addr.Val) {
4374 SDOperand Load;
4375 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
4376 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
4377 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004378 LLD->getSrcValueOffset(),
4379 LLD->isVolatile(),
4380 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004381 else {
4382 Load = DAG.getExtLoad(LLD->getExtensionType(),
4383 TheSelect->getValueType(0),
4384 LLD->getChain(), Addr, LLD->getSrcValue(),
4385 LLD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004386 LLD->getLoadedVT(),
4387 LLD->isVolatile(),
4388 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004389 }
4390 // Users of the select now use the result of the load.
4391 CombineTo(TheSelect, Load);
4392
4393 // Users of the old loads now use the new load's chain. We know the
4394 // old-load value is dead now.
4395 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
4396 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
4397 return true;
4398 }
Evan Chengc5484282006-10-04 00:56:09 +00004399 }
Chris Lattner40c62d52005-10-18 06:04:22 +00004400 }
4401 }
4402
4403 return false;
4404}
4405
Nate Begeman44728a72005-09-19 22:34:01 +00004406SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
4407 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00004408 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00004409
4410 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00004411 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
4412 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
4413 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
4414
4415 // Determine if the condition we're dealing with is constant
4416 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00004417 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004418 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
4419
4420 // fold select_cc true, x, y -> x
4421 if (SCCC && SCCC->getValue())
4422 return N2;
4423 // fold select_cc false, x, y -> y
4424 if (SCCC && SCCC->getValue() == 0)
4425 return N3;
4426
4427 // Check to see if we can simplify the select into an fabs node
4428 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
4429 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00004430 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00004431 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
4432 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
4433 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
4434 N2 == N3.getOperand(0))
4435 return DAG.getNode(ISD::FABS, VT, N0);
4436
4437 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
4438 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
4439 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
4440 N2.getOperand(0) == N3)
4441 return DAG.getNode(ISD::FABS, VT, N3);
4442 }
4443 }
4444
4445 // Check to see if we can perform the "gzip trick", transforming
4446 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00004447 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00004448 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00004449 MVT::isInteger(N2.getValueType()) &&
4450 (N1C->isNullValue() || // (a < 0) ? b : 0
4451 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00004452 MVT::ValueType XType = N0.getValueType();
4453 MVT::ValueType AType = N2.getValueType();
4454 if (XType >= AType) {
4455 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00004456 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00004457 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
4458 unsigned ShCtV = Log2_64(N2C->getValue());
4459 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
4460 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
4461 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00004462 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004463 if (XType > AType) {
4464 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004465 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004466 }
4467 return DAG.getNode(ISD::AND, AType, Shift, N2);
4468 }
4469 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4470 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4471 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00004472 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004473 if (XType > AType) {
4474 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004475 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004476 }
4477 return DAG.getNode(ISD::AND, AType, Shift, N2);
4478 }
4479 }
Nate Begeman07ed4172005-10-10 21:26:48 +00004480
4481 // fold select C, 16, 0 -> shl C, 4
4482 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
4483 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00004484
4485 // If the caller doesn't want us to simplify this into a zext of a compare,
4486 // don't do it.
4487 if (NotExtCompare && N2C->getValue() == 1)
4488 return SDOperand();
4489
Nate Begeman07ed4172005-10-10 21:26:48 +00004490 // Get a SetCC of the condition
4491 // FIXME: Should probably make sure that setcc is legal if we ever have a
4492 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00004493 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00004494 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00004495 if (AfterLegalize) {
4496 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Chris Lattner555d8d62006-12-07 22:36:47 +00004497 if (N2.getValueType() < SCC.getValueType())
4498 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
4499 else
4500 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004501 } else {
4502 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00004503 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004504 }
Chris Lattner5750df92006-03-01 04:03:14 +00004505 AddToWorkList(SCC.Val);
4506 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00004507
4508 if (N2C->getValue() == 1)
4509 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00004510 // shl setcc result by log2 n2c
4511 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
4512 DAG.getConstant(Log2_64(N2C->getValue()),
4513 TLI.getShiftAmountTy()));
4514 }
4515
Nate Begemanf845b452005-10-08 00:29:44 +00004516 // Check to see if this is the equivalent of setcc
4517 // FIXME: Turn all of these into setcc if setcc if setcc is legal
4518 // otherwise, go ahead with the folds.
4519 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
4520 MVT::ValueType XType = N0.getValueType();
4521 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
4522 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
4523 if (Res.getValueType() != VT)
4524 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
4525 return Res;
4526 }
4527
4528 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
4529 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
4530 TLI.isOperationLegal(ISD::CTLZ, XType)) {
4531 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
4532 return DAG.getNode(ISD::SRL, XType, Ctlz,
4533 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
4534 TLI.getShiftAmountTy()));
4535 }
4536 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
4537 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
4538 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
4539 N0);
4540 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
4541 DAG.getConstant(~0ULL, XType));
4542 return DAG.getNode(ISD::SRL, XType,
4543 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
4544 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4545 TLI.getShiftAmountTy()));
4546 }
4547 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
4548 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
4549 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
4550 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4551 TLI.getShiftAmountTy()));
4552 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
4553 }
4554 }
4555
4556 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
4557 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4558 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00004559 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
4560 N2.getOperand(0) == N1 && MVT::isInteger(N0.getValueType())) {
4561 MVT::ValueType XType = N0.getValueType();
4562 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4563 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4564 TLI.getShiftAmountTy()));
4565 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
4566 AddToWorkList(Shift.Val);
4567 AddToWorkList(Add.Val);
4568 return DAG.getNode(ISD::XOR, XType, Add, Shift);
4569 }
4570 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
4571 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4572 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
4573 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
4574 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Nate Begemanf845b452005-10-08 00:29:44 +00004575 MVT::ValueType XType = N0.getValueType();
4576 if (SubC->isNullValue() && MVT::isInteger(XType)) {
4577 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4578 DAG.getConstant(MVT::getSizeInBits(XType)-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00004579 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00004580 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004581 AddToWorkList(Shift.Val);
4582 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004583 return DAG.getNode(ISD::XOR, XType, Add, Shift);
4584 }
4585 }
4586 }
Chris Lattner1982ef22007-04-11 05:11:38 +00004587
Nate Begeman44728a72005-09-19 22:34:01 +00004588 return SDOperand();
4589}
4590
Evan Chengfa1eb272007-02-08 22:13:59 +00004591/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Nate Begeman452d7be2005-09-16 00:54:12 +00004592SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00004593 SDOperand N1, ISD::CondCode Cond,
4594 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00004595 TargetLowering::DAGCombinerInfo
4596 DagCombineInfo(DAG, !AfterLegalize, false, this);
4597 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00004598}
4599
Nate Begeman69575232005-10-20 02:15:44 +00004600/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
4601/// return a DAG expression to select that will generate the same value by
4602/// multiplying by a magic number. See:
4603/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4604SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004605 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004606 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
4607
Andrew Lenharth232c9102006-06-12 16:07:18 +00004608 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004609 ii != ee; ++ii)
4610 AddToWorkList(*ii);
4611 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004612}
4613
4614/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
4615/// return a DAG expression to select that will generate the same value by
4616/// multiplying by a magic number. See:
4617/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4618SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004619 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004620 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00004621
Andrew Lenharth232c9102006-06-12 16:07:18 +00004622 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004623 ii != ee; ++ii)
4624 AddToWorkList(*ii);
4625 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004626}
4627
Jim Laskey71382342006-10-07 23:37:56 +00004628/// FindBaseOffset - Return true if base is known not to alias with anything
4629/// but itself. Provides base object and offset as results.
4630static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
4631 // Assume it is a primitive operation.
4632 Base = Ptr; Offset = 0;
4633
4634 // If it's an adding a simple constant then integrate the offset.
4635 if (Base.getOpcode() == ISD::ADD) {
4636 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
4637 Base = Base.getOperand(0);
4638 Offset += C->getValue();
4639 }
4640 }
4641
4642 // If it's any of the following then it can't alias with anything but itself.
4643 return isa<FrameIndexSDNode>(Base) ||
4644 isa<ConstantPoolSDNode>(Base) ||
4645 isa<GlobalAddressSDNode>(Base);
4646}
4647
4648/// isAlias - Return true if there is any possibility that the two addresses
4649/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00004650bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
4651 const Value *SrcValue1, int SrcValueOffset1,
4652 SDOperand Ptr2, int64_t Size2,
4653 const Value *SrcValue2, int SrcValueOffset2)
4654{
Jim Laskey71382342006-10-07 23:37:56 +00004655 // If they are the same then they must be aliases.
4656 if (Ptr1 == Ptr2) return true;
4657
4658 // Gather base node and offset information.
4659 SDOperand Base1, Base2;
4660 int64_t Offset1, Offset2;
4661 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
4662 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
4663
4664 // If they have a same base address then...
4665 if (Base1 == Base2) {
4666 // Check to see if the addresses overlap.
4667 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
4668 }
4669
Jim Laskey096c22e2006-10-18 12:29:57 +00004670 // If we know both bases then they can't alias.
4671 if (KnownBase1 && KnownBase2) return false;
4672
Jim Laskey07a27092006-10-18 19:08:31 +00004673 if (CombinerGlobalAA) {
4674 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00004675 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
4676 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
4677 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00004678 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00004679 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00004680 if (AAResult == AliasAnalysis::NoAlias)
4681 return false;
4682 }
Jim Laskey096c22e2006-10-18 12:29:57 +00004683
4684 // Otherwise we have to assume they alias.
4685 return true;
Jim Laskey71382342006-10-07 23:37:56 +00004686}
4687
4688/// FindAliasInfo - Extracts the relevant alias information from the memory
4689/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00004690bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00004691 SDOperand &Ptr, int64_t &Size,
4692 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004693 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4694 Ptr = LD->getBasePtr();
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004695 Size = MVT::getSizeInBits(LD->getLoadedVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004696 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004697 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00004698 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004699 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004700 Ptr = ST->getBasePtr();
Evan Cheng8b2794a2006-10-13 21:14:26 +00004701 Size = MVT::getSizeInBits(ST->getStoredVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004702 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004703 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00004704 } else {
Jim Laskey71382342006-10-07 23:37:56 +00004705 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00004706 }
4707
4708 return false;
4709}
4710
Jim Laskey6ff23e52006-10-04 16:53:27 +00004711/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
4712/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00004713void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00004714 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004715 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00004716 std::set<SDNode *> Visited; // Visited node set.
4717
Jim Laskey279f0532006-09-25 16:29:54 +00004718 // Get alias information for node.
4719 SDOperand Ptr;
4720 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004721 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004722 int SrcValueOffset;
4723 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00004724
Jim Laskey6ff23e52006-10-04 16:53:27 +00004725 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00004726 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00004727
Jim Laskeybc588b82006-10-05 15:07:25 +00004728 // Look at each chain and determine if it is an alias. If so, add it to the
4729 // aliases list. If not, then continue up the chain looking for the next
4730 // candidate.
4731 while (!Chains.empty()) {
4732 SDOperand Chain = Chains.back();
4733 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00004734
Jim Laskeybc588b82006-10-05 15:07:25 +00004735 // Don't bother if we've been before.
4736 if (Visited.find(Chain.Val) != Visited.end()) continue;
4737 Visited.insert(Chain.Val);
4738
4739 switch (Chain.getOpcode()) {
4740 case ISD::EntryToken:
4741 // Entry token is ideal chain operand, but handled in FindBetterChain.
4742 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00004743
Jim Laskeybc588b82006-10-05 15:07:25 +00004744 case ISD::LOAD:
4745 case ISD::STORE: {
4746 // Get alias information for Chain.
4747 SDOperand OpPtr;
4748 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004749 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004750 int OpSrcValueOffset;
4751 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
4752 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00004753
4754 // If chain is alias then stop here.
4755 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00004756 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
4757 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004758 Aliases.push_back(Chain);
4759 } else {
4760 // Look further up the chain.
4761 Chains.push_back(Chain.getOperand(0));
4762 // Clean up old chain.
4763 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00004764 }
Jim Laskeybc588b82006-10-05 15:07:25 +00004765 break;
4766 }
4767
4768 case ISD::TokenFactor:
4769 // We have to check each of the operands of the token factor, so we queue
4770 // then up. Adding the operands to the queue (stack) in reverse order
4771 // maintains the original order and increases the likelihood that getNode
4772 // will find a matching token factor (CSE.)
4773 for (unsigned n = Chain.getNumOperands(); n;)
4774 Chains.push_back(Chain.getOperand(--n));
4775 // Eliminate the token factor if we can.
4776 AddToWorkList(Chain.Val);
4777 break;
4778
4779 default:
4780 // For all other instructions we will just have to take what we can get.
4781 Aliases.push_back(Chain);
4782 break;
Jim Laskey279f0532006-09-25 16:29:54 +00004783 }
4784 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004785}
4786
4787/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
4788/// for a better chain (aliasing node.)
4789SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
4790 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00004791
Jim Laskey6ff23e52006-10-04 16:53:27 +00004792 // Accumulate all the aliases to this node.
4793 GatherAllAliases(N, OldChain, Aliases);
4794
4795 if (Aliases.size() == 0) {
4796 // If no operands then chain to entry token.
4797 return DAG.getEntryNode();
4798 } else if (Aliases.size() == 1) {
4799 // If a single operand then chain to it. We don't need to revisit it.
4800 return Aliases[0];
4801 }
4802
4803 // Construct a custom tailored token factor.
4804 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4805 &Aliases[0], Aliases.size());
4806
4807 // Make sure the old chain gets cleaned up.
4808 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
4809
4810 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00004811}
4812
Nate Begeman1d4d4142005-09-01 00:19:25 +00004813// SelectionDAG::Combine - This is the entry point for the file.
4814//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004815void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00004816 if (!RunningAfterLegalize && ViewDAGCombine1)
4817 viewGraph();
4818 if (RunningAfterLegalize && ViewDAGCombine2)
4819 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004820 /// run - This is the main entry point to this class.
4821 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004822 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004823}