blob: b4299f4ff85394791afc1e1eb8092ae41047146c [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"
Duncan Sandsdc846502007-10-28 12:59:45 +000040#include "llvm/Support/Alignment.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000041#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000042#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000043#include "llvm/Support/Debug.h"
44#include "llvm/Support/MathExtras.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000045#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000046using namespace llvm;
47
Chris Lattnercd3245a2006-12-19 22:41:21 +000048STATISTIC(NodesCombined , "Number of dag nodes combined");
49STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
50STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
51
Nate Begeman1d4d4142005-09-01 00:19:25 +000052namespace {
Chris Lattner938ab022007-01-16 04:55:25 +000053#ifndef NDEBUG
54 static cl::opt<bool>
55 ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden,
56 cl::desc("Pop up a window to show dags before the first "
57 "dag combine pass"));
58 static cl::opt<bool>
59 ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden,
60 cl::desc("Pop up a window to show dags before the second "
61 "dag combine pass"));
62#else
63 static const bool ViewDAGCombine1 = false;
64 static const bool ViewDAGCombine2 = false;
65#endif
66
Jim Laskey71382342006-10-07 23:37:56 +000067 static cl::opt<bool>
68 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000069 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000070
Jim Laskey07a27092006-10-18 19:08:31 +000071 static cl::opt<bool>
72 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
73 cl::desc("Include global information in alias analysis"));
74
Jim Laskeybc588b82006-10-05 15:07:25 +000075//------------------------------ DAGCombiner ---------------------------------//
76
Jim Laskey71382342006-10-07 23:37:56 +000077 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000078 SelectionDAG &DAG;
79 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000080 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000081
82 // Worklist of all of the nodes that need to be simplified.
83 std::vector<SDNode*> WorkList;
84
Jim Laskeyc7c3f112006-10-16 20:52:31 +000085 // AA - Used for DAG load/store alias analysis.
86 AliasAnalysis &AA;
87
Nate Begeman1d4d4142005-09-01 00:19:25 +000088 /// AddUsersToWorkList - When an instruction is simplified, add all users of
89 /// the instruction to the work lists because they might get more simplified
90 /// now.
91 ///
92 void AddUsersToWorkList(SDNode *N) {
93 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000094 UI != UE; ++UI)
Jim Laskey6ff23e52006-10-04 16:53:27 +000095 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000096 }
97
98 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000099 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +0000100 void removeFromWorkList(SDNode *N) {
101 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
102 WorkList.end());
103 }
104
Dan Gohman389079b2007-10-08 17:57:15 +0000105 /// visit - call the node-specific routine that knows how to fold each
106 /// particular type of node.
107 SDOperand visit(SDNode *N);
108
Chris Lattner24664722006-03-01 04:53:38 +0000109 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +0000110 /// AddToWorkList - Add to the work list making sure it's instance is at the
111 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +0000112 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000113 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +0000114 WorkList.push_back(N);
115 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000116
Jim Laskey274062c2006-10-13 23:32:28 +0000117 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
118 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000119 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
Chris Lattner87514ca2005-10-10 22:31:19 +0000120 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000121 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +0000122 DOUT << "\nWith: "; DEBUG(To[0].Val->dump(&DAG));
123 DOUT << " and " << NumTo-1 << " other values\n";
Chris Lattner01a22022005-10-10 22:04:48 +0000124 std::vector<SDNode*> NowDead;
Chris Lattner3577e382006-08-11 17:56:38 +0000125 DAG.ReplaceAllUsesWith(N, To, &NowDead);
Chris Lattner01a22022005-10-10 22:04:48 +0000126
Jim Laskey274062c2006-10-13 23:32:28 +0000127 if (AddTo) {
128 // Push the new nodes and any users onto the worklist
129 for (unsigned i = 0, e = NumTo; i != e; ++i) {
130 AddToWorkList(To[i].Val);
131 AddUsersToWorkList(To[i].Val);
132 }
Chris Lattner01a22022005-10-10 22:04:48 +0000133 }
134
Jim Laskey6ff23e52006-10-04 16:53:27 +0000135 // Nodes can be reintroduced into the worklist. Make sure we do not
136 // process a node that has been replaced.
Chris Lattner01a22022005-10-10 22:04:48 +0000137 removeFromWorkList(N);
138 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
139 removeFromWorkList(NowDead[i]);
140
141 // Finally, since the node is now dead, remove it from the graph.
142 DAG.DeleteNode(N);
143 return SDOperand(N, 0);
144 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000145
Jim Laskey274062c2006-10-13 23:32:28 +0000146 SDOperand CombineTo(SDNode *N, SDOperand Res, bool AddTo = true) {
147 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000148 }
149
Jim Laskey274062c2006-10-13 23:32:28 +0000150 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1,
151 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000152 SDOperand To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000153 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000154 }
155 private:
156
Chris Lattner012f2412006-02-17 21:58:01 +0000157 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000158 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000159 /// propagation. If so, return true.
Chris Lattnere33544c2007-10-13 06:58:48 +0000160 bool SimplifyDemandedBits(SDOperand Op, uint64_t Demanded = ~0ULL) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000161 TargetLowering::TargetLoweringOpt TLO(DAG);
162 uint64_t KnownZero, KnownOne;
Chris Lattnere33544c2007-10-13 06:58:48 +0000163 Demanded &= MVT::getIntVTBitMask(Op.getValueType());
Chris Lattner012f2412006-02-17 21:58:01 +0000164 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
165 return false;
166
167 // Revisit the node.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000168 AddToWorkList(Op.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000169
170 // Replace the old value with the new one.
171 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000172 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.Val->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +0000173 DOUT << "\nWith: "; DEBUG(TLO.New.Val->dump(&DAG));
174 DOUT << '\n';
Chris Lattner012f2412006-02-17 21:58:01 +0000175
176 std::vector<SDNode*> NowDead;
Chris Lattner01d029b2007-10-15 06:10:22 +0000177 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &NowDead);
Chris Lattner012f2412006-02-17 21:58:01 +0000178
Chris Lattner7d20d392006-02-20 06:51:04 +0000179 // Push the new node and any (possibly new) users onto the worklist.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000180 AddToWorkList(TLO.New.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000181 AddUsersToWorkList(TLO.New.Val);
182
183 // Nodes can end up on the worklist more than once. Make sure we do
184 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000185 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
186 removeFromWorkList(NowDead[i]);
187
Chris Lattner7d20d392006-02-20 06:51:04 +0000188 // Finally, if the node is now dead, remove it from the graph. The node
189 // may not be dead if the replacement process recursively simplified to
190 // something else needing this node.
191 if (TLO.Old.Val->use_empty()) {
192 removeFromWorkList(TLO.Old.Val);
Chris Lattnerec06e9a2007-04-18 03:05:22 +0000193
194 // If the operands of this node are only used by the node, they will now
195 // be dead. Make sure to visit them first to delete dead nodes early.
196 for (unsigned i = 0, e = TLO.Old.Val->getNumOperands(); i != e; ++i)
197 if (TLO.Old.Val->getOperand(i).Val->hasOneUse())
198 AddToWorkList(TLO.Old.Val->getOperand(i).Val);
199
Chris Lattner7d20d392006-02-20 06:51:04 +0000200 DAG.DeleteNode(TLO.Old.Val);
201 }
Chris Lattner012f2412006-02-17 21:58:01 +0000202 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000203 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000204
Chris Lattner448f2192006-11-11 00:39:41 +0000205 bool CombineToPreIndexedLoadStore(SDNode *N);
206 bool CombineToPostIndexedLoadStore(SDNode *N);
207
208
Dan Gohman389079b2007-10-08 17:57:15 +0000209 /// combine - call the node-specific routine that knows how to fold each
210 /// particular type of node. If that doesn't do anything, try the
211 /// target-specific DAG combines.
212 SDOperand combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000213
214 // Visitation implementation - Implement dag node combining for different
215 // node types. The semantics are as follows:
216 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000217 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000218 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000219 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000220 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000221 SDOperand visitTokenFactor(SDNode *N);
222 SDOperand visitADD(SDNode *N);
223 SDOperand visitSUB(SDNode *N);
Chris Lattner91153682007-03-04 20:03:15 +0000224 SDOperand visitADDC(SDNode *N);
225 SDOperand visitADDE(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000226 SDOperand visitMUL(SDNode *N);
227 SDOperand visitSDIV(SDNode *N);
228 SDOperand visitUDIV(SDNode *N);
229 SDOperand visitSREM(SDNode *N);
230 SDOperand visitUREM(SDNode *N);
231 SDOperand visitMULHU(SDNode *N);
232 SDOperand visitMULHS(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +0000233 SDOperand visitSMUL_LOHI(SDNode *N);
234 SDOperand visitUMUL_LOHI(SDNode *N);
235 SDOperand visitSDIVREM(SDNode *N);
236 SDOperand visitUDIVREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000237 SDOperand visitAND(SDNode *N);
238 SDOperand visitOR(SDNode *N);
239 SDOperand visitXOR(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000240 SDOperand SimplifyVBinOp(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000241 SDOperand visitSHL(SDNode *N);
242 SDOperand visitSRA(SDNode *N);
243 SDOperand visitSRL(SDNode *N);
244 SDOperand visitCTLZ(SDNode *N);
245 SDOperand visitCTTZ(SDNode *N);
246 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000247 SDOperand visitSELECT(SDNode *N);
248 SDOperand visitSELECT_CC(SDNode *N);
249 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000250 SDOperand visitSIGN_EXTEND(SDNode *N);
251 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000252 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000253 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
254 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000255 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000256 SDOperand visitFADD(SDNode *N);
257 SDOperand visitFSUB(SDNode *N);
258 SDOperand visitFMUL(SDNode *N);
259 SDOperand visitFDIV(SDNode *N);
260 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000261 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000262 SDOperand visitSINT_TO_FP(SDNode *N);
263 SDOperand visitUINT_TO_FP(SDNode *N);
264 SDOperand visitFP_TO_SINT(SDNode *N);
265 SDOperand visitFP_TO_UINT(SDNode *N);
266 SDOperand visitFP_ROUND(SDNode *N);
267 SDOperand visitFP_ROUND_INREG(SDNode *N);
268 SDOperand visitFP_EXTEND(SDNode *N);
269 SDOperand visitFNEG(SDNode *N);
270 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000271 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000272 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000273 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000274 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000275 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
Evan Cheng513da432007-10-06 08:19:55 +0000276 SDOperand visitEXTRACT_VECTOR_ELT(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000277 SDOperand visitBUILD_VECTOR(SDNode *N);
278 SDOperand visitCONCAT_VECTORS(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000279 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000280
Evan Cheng44f1f092006-04-20 08:56:16 +0000281 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000282 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
283
Chris Lattner40c62d52005-10-18 06:04:22 +0000284 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000285 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000286 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
287 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000288 SDOperand N3, ISD::CondCode CC,
289 bool NotExtCompare = false);
Nate Begeman452d7be2005-09-16 00:54:12 +0000290 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000291 ISD::CondCode Cond, bool foldBooleans = true);
Dan Gohman389079b2007-10-08 17:57:15 +0000292 bool SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp, unsigned HiOp);
Dan Gohman7f321562007-06-25 16:23:39 +0000293 SDOperand ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000294 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000295 SDOperand BuildUDIV(SDNode *N);
296 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Evan Chengc88138f2007-03-22 01:54:19 +0000297 SDOperand ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000298
Chris Lattner2b4c2792007-10-13 06:35:54 +0000299 SDOperand GetDemandedBits(SDOperand V, uint64_t Mask);
300
Jim Laskey6ff23e52006-10-04 16:53:27 +0000301 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
302 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000303 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000304 SmallVector<SDOperand, 8> &Aliases);
305
Jim Laskey096c22e2006-10-18 12:29:57 +0000306 /// isAlias - Return true if there is any possibility that the two addresses
307 /// overlap.
308 bool isAlias(SDOperand Ptr1, int64_t Size1,
309 const Value *SrcValue1, int SrcValueOffset1,
310 SDOperand Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000311 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000312
Jim Laskey7ca56af2006-10-11 13:47:09 +0000313 /// FindAliasInfo - Extracts the relevant alias information from the memory
314 /// node. Returns true if the operand was a load.
315 bool FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +0000316 SDOperand &Ptr, int64_t &Size,
317 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000318
Jim Laskey279f0532006-09-25 16:29:54 +0000319 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000320 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000321 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
322
Nate Begeman1d4d4142005-09-01 00:19:25 +0000323public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000324 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
325 : DAG(D),
326 TLI(D.getTargetLoweringInfo()),
327 AfterLegalize(false),
328 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000329
330 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000331 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000332 };
333}
334
Chris Lattner24664722006-03-01 04:53:38 +0000335//===----------------------------------------------------------------------===//
336// TargetLowering::DAGCombinerInfo implementation
337//===----------------------------------------------------------------------===//
338
339void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
340 ((DAGCombiner*)DC)->AddToWorkList(N);
341}
342
343SDOperand TargetLowering::DAGCombinerInfo::
344CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000345 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000346}
347
348SDOperand TargetLowering::DAGCombinerInfo::
349CombineTo(SDNode *N, SDOperand Res) {
350 return ((DAGCombiner*)DC)->CombineTo(N, Res);
351}
352
353
354SDOperand TargetLowering::DAGCombinerInfo::
355CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
356 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
357}
358
359
Chris Lattner24664722006-03-01 04:53:38 +0000360//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000361// Helper Functions
362//===----------------------------------------------------------------------===//
363
364/// isNegatibleForFree - Return 1 if we can compute the negated form of the
365/// specified expression for the same cost as the expression itself, or 2 if we
366/// can compute the negated form more cheaply than the expression itself.
Chris Lattner501fee72007-05-23 07:35:22 +0000367static char isNegatibleForFree(SDOperand Op, unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000368 // No compile time optimizations on this type.
369 if (Op.getValueType() == MVT::ppcf128)
370 return 0;
371
Chris Lattner29446522007-05-14 22:04:50 +0000372 // fneg is removable even if it has multiple uses.
373 if (Op.getOpcode() == ISD::FNEG) return 2;
374
375 // Don't allow anything with multiple uses.
376 if (!Op.hasOneUse()) return 0;
377
Chris Lattner3adf9512007-05-25 02:19:06 +0000378 // Don't recurse exponentially.
379 if (Depth > 6) return 0;
380
Chris Lattner29446522007-05-14 22:04:50 +0000381 switch (Op.getOpcode()) {
382 default: return false;
383 case ISD::ConstantFP:
384 return 1;
385 case ISD::FADD:
386 // FIXME: determine better conditions for this xform.
387 if (!UnsafeFPMath) return 0;
388
389 // -(A+B) -> -A - B
Chris Lattner501fee72007-05-23 07:35:22 +0000390 if (char V = isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000391 return V;
392 // -(A+B) -> -B - A
Chris Lattner501fee72007-05-23 07:35:22 +0000393 return isNegatibleForFree(Op.getOperand(1), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000394 case ISD::FSUB:
395 // We can't turn -(A-B) into B-A when we honor signed zeros.
396 if (!UnsafeFPMath) return 0;
397
398 // -(A-B) -> B-A
399 return 1;
400
401 case ISD::FMUL:
402 case ISD::FDIV:
403 if (HonorSignDependentRoundingFPMath()) return 0;
404
405 // -(X*Y) -> (-X * Y) or (X*-Y)
Chris Lattner501fee72007-05-23 07:35:22 +0000406 if (char V = isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000407 return V;
408
Chris Lattner501fee72007-05-23 07:35:22 +0000409 return isNegatibleForFree(Op.getOperand(1), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000410
411 case ISD::FP_EXTEND:
412 case ISD::FP_ROUND:
413 case ISD::FSIN:
Chris Lattner501fee72007-05-23 07:35:22 +0000414 return isNegatibleForFree(Op.getOperand(0), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000415 }
416}
417
418/// GetNegatedExpression - If isNegatibleForFree returns true, this function
419/// returns the newly negated expression.
Chris Lattner3adf9512007-05-25 02:19:06 +0000420static SDOperand GetNegatedExpression(SDOperand Op, SelectionDAG &DAG,
421 unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000422 // fneg is removable even if it has multiple uses.
423 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
424
425 // Don't allow anything with multiple uses.
426 assert(Op.hasOneUse() && "Unknown reuse!");
427
Chris Lattner3adf9512007-05-25 02:19:06 +0000428 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000429 switch (Op.getOpcode()) {
430 default: assert(0 && "Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000431 case ISD::ConstantFP: {
432 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
433 V.changeSign();
434 return DAG.getConstantFP(V, Op.getValueType());
435 }
Chris Lattner29446522007-05-14 22:04:50 +0000436 case ISD::FADD:
437 // FIXME: determine better conditions for this xform.
438 assert(UnsafeFPMath);
439
440 // -(A+B) -> -A - B
Chris Lattner3adf9512007-05-25 02:19:06 +0000441 if (isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000442 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000443 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000444 Op.getOperand(1));
445 // -(A+B) -> -B - A
446 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000447 GetNegatedExpression(Op.getOperand(1), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000448 Op.getOperand(0));
449 case ISD::FSUB:
450 // We can't turn -(A-B) into B-A when we honor signed zeros.
451 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000452
453 // -(0-B) -> B
454 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000455 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000456 return Op.getOperand(1);
Chris Lattner29446522007-05-14 22:04:50 +0000457
458 // -(A-B) -> B-A
459 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
460 Op.getOperand(0));
461
462 case ISD::FMUL:
463 case ISD::FDIV:
464 assert(!HonorSignDependentRoundingFPMath());
465
466 // -(X*Y) -> -X * Y
Chris Lattner3adf9512007-05-25 02:19:06 +0000467 if (isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000468 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000469 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000470 Op.getOperand(1));
471
472 // -(X*Y) -> X * -Y
473 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
474 Op.getOperand(0),
Chris Lattner3adf9512007-05-25 02:19:06 +0000475 GetNegatedExpression(Op.getOperand(1), DAG, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000476
477 case ISD::FP_EXTEND:
478 case ISD::FP_ROUND:
479 case ISD::FSIN:
480 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000481 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000482 }
483}
Chris Lattner24664722006-03-01 04:53:38 +0000484
485
Nate Begeman4ebd8052005-09-01 23:24:04 +0000486// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
487// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000488// Also, set the incoming LHS, RHS, and CC references to the appropriate
489// nodes based on the type of node we are checking. This simplifies life a
490// bit for the callers.
491static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
492 SDOperand &CC) {
493 if (N.getOpcode() == ISD::SETCC) {
494 LHS = N.getOperand(0);
495 RHS = N.getOperand(1);
496 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000497 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000498 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000499 if (N.getOpcode() == ISD::SELECT_CC &&
500 N.getOperand(2).getOpcode() == ISD::Constant &&
501 N.getOperand(3).getOpcode() == ISD::Constant &&
502 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000503 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
504 LHS = N.getOperand(0);
505 RHS = N.getOperand(1);
506 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000507 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000508 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000509 return false;
510}
511
Nate Begeman99801192005-09-07 23:25:52 +0000512// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
513// one use. If this is true, it allows the users to invert the operation for
514// free when it is profitable to do so.
515static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000516 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000517 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000518 return true;
519 return false;
520}
521
Nate Begemancd4d58c2006-02-03 06:46:56 +0000522SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
523 MVT::ValueType VT = N0.getValueType();
524 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
525 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
526 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
527 if (isa<ConstantSDNode>(N1)) {
528 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
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, N0.getOperand(0));
531 } else if (N0.hasOneUse()) {
532 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000533 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000534 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
535 }
536 }
537 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
538 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
539 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
540 if (isa<ConstantSDNode>(N0)) {
541 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000542 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000543 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
544 } else if (N1.hasOneUse()) {
545 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000546 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000547 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
548 }
549 }
550 return SDOperand();
551}
552
Chris Lattner29446522007-05-14 22:04:50 +0000553//===----------------------------------------------------------------------===//
554// Main DAG Combiner implementation
555//===----------------------------------------------------------------------===//
556
Nate Begeman4ebd8052005-09-01 23:24:04 +0000557void DAGCombiner::Run(bool RunningAfterLegalize) {
558 // set the instance variable, so that the various visit routines may use it.
559 AfterLegalize = RunningAfterLegalize;
560
Nate Begeman646d7e22005-09-02 21:18:40 +0000561 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000562 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
563 E = DAG.allnodes_end(); I != E; ++I)
564 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000565
Chris Lattner95038592005-10-05 06:35:28 +0000566 // Create a dummy node (which is not added to allnodes), that adds a reference
567 // to the root node, preventing it from being deleted, and tracking any
568 // changes of the root.
569 HandleSDNode Dummy(DAG.getRoot());
570
Jim Laskey26f7fa72006-10-17 19:33:52 +0000571 // The root of the dag may dangle to deleted nodes until the dag combiner is
572 // done. Set it to null to avoid confusion.
573 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000574
Nate Begeman1d4d4142005-09-01 00:19:25 +0000575 // while the worklist isn't empty, inspect the node on the end of it and
576 // try and combine it.
577 while (!WorkList.empty()) {
578 SDNode *N = WorkList.back();
579 WorkList.pop_back();
580
581 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000582 // N is deleted from the DAG, since they too may now be dead or may have a
583 // reduced number of uses, allowing other xforms.
584 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000585 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000586 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000587
Chris Lattner95038592005-10-05 06:35:28 +0000588 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000589 continue;
590 }
591
Dan Gohman389079b2007-10-08 17:57:15 +0000592 SDOperand RV = combine(N);
Chris Lattner24664722006-03-01 04:53:38 +0000593
Nate Begeman83e75ec2005-09-06 04:43:02 +0000594 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000595 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000596 // If we get back the same node we passed in, rather than a new node or
597 // zero, we know that the node must have defined multiple values and
598 // CombineTo was used. Since CombineTo takes care of the worklist
599 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000600 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000601 assert(N->getOpcode() != ISD::DELETED_NODE &&
602 RV.Val->getOpcode() != ISD::DELETED_NODE &&
603 "Node was deleted but visit returned new node!");
604
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000605 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +0000606 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
607 DOUT << '\n';
Chris Lattner01a22022005-10-10 22:04:48 +0000608 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000609 if (N->getNumValues() == RV.Val->getNumValues())
610 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
611 else {
612 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
613 SDOperand OpV = RV;
614 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
615 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000616
617 // Push the new node and any users onto the worklist
Jim Laskey6ff23e52006-10-04 16:53:27 +0000618 AddToWorkList(RV.Val);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000619 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000620
Jim Laskey6ff23e52006-10-04 16:53:27 +0000621 // Nodes can be reintroduced into the worklist. Make sure we do not
622 // process a node that has been replaced.
Nate Begeman646d7e22005-09-02 21:18:40 +0000623 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000624 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
625 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000626
627 // Finally, since the node is now dead, remove it from the graph.
628 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000629 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000630 }
631 }
Chris Lattner95038592005-10-05 06:35:28 +0000632
633 // If the root changed (e.g. it was a dead load, update the root).
634 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000635}
636
Nate Begeman83e75ec2005-09-06 04:43:02 +0000637SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000638 switch(N->getOpcode()) {
639 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000640 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000641 case ISD::ADD: return visitADD(N);
642 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000643 case ISD::ADDC: return visitADDC(N);
644 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000645 case ISD::MUL: return visitMUL(N);
646 case ISD::SDIV: return visitSDIV(N);
647 case ISD::UDIV: return visitUDIV(N);
648 case ISD::SREM: return visitSREM(N);
649 case ISD::UREM: return visitUREM(N);
650 case ISD::MULHU: return visitMULHU(N);
651 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000652 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
653 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
654 case ISD::SDIVREM: return visitSDIVREM(N);
655 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000656 case ISD::AND: return visitAND(N);
657 case ISD::OR: return visitOR(N);
658 case ISD::XOR: return visitXOR(N);
659 case ISD::SHL: return visitSHL(N);
660 case ISD::SRA: return visitSRA(N);
661 case ISD::SRL: return visitSRL(N);
662 case ISD::CTLZ: return visitCTLZ(N);
663 case ISD::CTTZ: return visitCTTZ(N);
664 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000665 case ISD::SELECT: return visitSELECT(N);
666 case ISD::SELECT_CC: return visitSELECT_CC(N);
667 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000668 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
669 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000670 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000671 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
672 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000673 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000674 case ISD::FADD: return visitFADD(N);
675 case ISD::FSUB: return visitFSUB(N);
676 case ISD::FMUL: return visitFMUL(N);
677 case ISD::FDIV: return visitFDIV(N);
678 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000679 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000680 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
681 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
682 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
683 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
684 case ISD::FP_ROUND: return visitFP_ROUND(N);
685 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
686 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
687 case ISD::FNEG: return visitFNEG(N);
688 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000689 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000690 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000691 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000692 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000693 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000694 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000695 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
696 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000697 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000698 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000699 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000700}
701
Dan Gohman389079b2007-10-08 17:57:15 +0000702SDOperand DAGCombiner::combine(SDNode *N) {
703
704 SDOperand RV = visit(N);
705
706 // If nothing happened, try a target-specific DAG combine.
707 if (RV.Val == 0) {
708 assert(N->getOpcode() != ISD::DELETED_NODE &&
709 "Node was deleted but visit returned NULL!");
710
711 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
712 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
713
714 // Expose the DAG combiner to the target combiner impls.
715 TargetLowering::DAGCombinerInfo
716 DagCombineInfo(DAG, !AfterLegalize, false, this);
717
718 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
719 }
720 }
721
722 return RV;
723}
724
Chris Lattner6270f682006-10-08 22:57:01 +0000725/// getInputChainForNode - Given a node, return its input chain if it has one,
726/// otherwise return a null sd operand.
727static SDOperand getInputChainForNode(SDNode *N) {
728 if (unsigned NumOps = N->getNumOperands()) {
729 if (N->getOperand(0).getValueType() == MVT::Other)
730 return N->getOperand(0);
731 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
732 return N->getOperand(NumOps-1);
733 for (unsigned i = 1; i < NumOps-1; ++i)
734 if (N->getOperand(i).getValueType() == MVT::Other)
735 return N->getOperand(i);
736 }
737 return SDOperand(0, 0);
738}
739
Nate Begeman83e75ec2005-09-06 04:43:02 +0000740SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000741 // If N has two operands, where one has an input chain equal to the other,
742 // the 'other' chain is redundant.
743 if (N->getNumOperands() == 2) {
744 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
745 return N->getOperand(0);
746 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
747 return N->getOperand(1);
748 }
749
Chris Lattnerc76d4412007-05-16 06:37:59 +0000750 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
751 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
752 SmallPtrSet<SDNode*, 16> SeenOps;
753 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000754
755 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000756 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000757
Jim Laskey71382342006-10-07 23:37:56 +0000758 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000759 // encountered.
760 for (unsigned i = 0; i < TFs.size(); ++i) {
761 SDNode *TF = TFs[i];
762
Jim Laskey6ff23e52006-10-04 16:53:27 +0000763 // Check each of the operands.
764 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
765 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000766
Jim Laskey6ff23e52006-10-04 16:53:27 +0000767 switch (Op.getOpcode()) {
768 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000769 // Entry tokens don't need to be added to the list. They are
770 // rededundant.
771 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000772 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000773
Jim Laskey6ff23e52006-10-04 16:53:27 +0000774 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000775 if ((CombinerAA || Op.hasOneUse()) &&
776 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000777 // Queue up for processing.
778 TFs.push_back(Op.Val);
779 // Clean up in case the token factor is removed.
780 AddToWorkList(Op.Val);
781 Changed = true;
782 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000783 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000784 // Fall thru
785
786 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000787 // Only add if it isn't already in the list.
788 if (SeenOps.insert(Op.Val))
Jim Laskeybc588b82006-10-05 15:07:25 +0000789 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000790 else
791 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000792 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000793 }
794 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000795 }
796
797 SDOperand Result;
798
799 // If we've change things around then replace token factor.
800 if (Changed) {
801 if (Ops.size() == 0) {
802 // The entry token is the only possible outcome.
803 Result = DAG.getEntryNode();
804 } else {
805 // New and improved token factor.
806 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000807 }
Jim Laskey274062c2006-10-13 23:32:28 +0000808
809 // Don't add users to work list.
810 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000811 }
Jim Laskey279f0532006-09-25 16:29:54 +0000812
Jim Laskey6ff23e52006-10-04 16:53:27 +0000813 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000814}
815
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000816static
817SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
818 MVT::ValueType VT = N0.getValueType();
819 SDOperand N00 = N0.getOperand(0);
820 SDOperand N01 = N0.getOperand(1);
821 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
822 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
823 isa<ConstantSDNode>(N00.getOperand(1))) {
824 N0 = DAG.getNode(ISD::ADD, VT,
825 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
826 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
827 return DAG.getNode(ISD::ADD, VT, N0, N1);
828 }
829 return SDOperand();
830}
831
Evan Chengb13cdbd2007-06-21 07:39:16 +0000832static
833SDOperand combineSelectAndUse(SDNode *N, SDOperand Slct, SDOperand OtherOp,
834 SelectionDAG &DAG) {
835 MVT::ValueType VT = N->getValueType(0);
836 unsigned Opc = N->getOpcode();
837 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
838 SDOperand LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
839 SDOperand RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
840 ISD::CondCode CC = ISD::SETCC_INVALID;
841 if (isSlctCC)
842 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
843 else {
844 SDOperand CCOp = Slct.getOperand(0);
845 if (CCOp.getOpcode() == ISD::SETCC)
846 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
847 }
848
849 bool DoXform = false;
850 bool InvCC = false;
851 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
852 "Bad input!");
853 if (LHS.getOpcode() == ISD::Constant &&
854 cast<ConstantSDNode>(LHS)->isNullValue())
855 DoXform = true;
856 else if (CC != ISD::SETCC_INVALID &&
857 RHS.getOpcode() == ISD::Constant &&
858 cast<ConstantSDNode>(RHS)->isNullValue()) {
859 std::swap(LHS, RHS);
860 bool isInt = MVT::isInteger(isSlctCC ? Slct.getOperand(0).getValueType()
861 : Slct.getOperand(0).getOperand(0).getValueType());
862 CC = ISD::getSetCCInverse(CC, isInt);
863 DoXform = true;
864 InvCC = true;
865 }
866
867 if (DoXform) {
868 SDOperand Result = DAG.getNode(Opc, VT, OtherOp, RHS);
869 if (isSlctCC)
870 return DAG.getSelectCC(OtherOp, Result,
871 Slct.getOperand(0), Slct.getOperand(1), CC);
872 SDOperand CCOp = Slct.getOperand(0);
873 if (InvCC)
874 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
875 CCOp.getOperand(1), CC);
876 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
877 }
878 return SDOperand();
879}
880
Nate Begeman83e75ec2005-09-06 04:43:02 +0000881SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000882 SDOperand N0 = N->getOperand(0);
883 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000884 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
885 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000886 MVT::ValueType VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000887
888 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +0000889 if (MVT::isVector(VT)) {
890 SDOperand FoldedVOp = SimplifyVBinOp(N);
891 if (FoldedVOp.Val) return FoldedVOp;
892 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000893
Dan Gohman613e0d82007-07-03 14:03:57 +0000894 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000895 if (N0.getOpcode() == ISD::UNDEF)
896 return N0;
897 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000898 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000899 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000900 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000901 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000902 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000903 if (N0C && !N1C)
904 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000905 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000906 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000907 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000908 // fold ((c1-A)+c2) -> (c1+c2)-A
909 if (N1C && N0.getOpcode() == ISD::SUB)
910 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
911 return DAG.getNode(ISD::SUB, VT,
912 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
913 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000914 // reassociate add
915 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
916 if (RADD.Val != 0)
917 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000918 // fold ((0-A) + B) -> B-A
919 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
920 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000921 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000922 // fold (A + (0-B)) -> A-B
923 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
924 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000925 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000926 // fold (A+(B-A)) -> B
927 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000928 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000929
Evan Cheng860771d2006-03-01 01:09:54 +0000930 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000931 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000932
933 // fold (a+b) -> (a|b) iff a and b share no bits.
934 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
935 uint64_t LHSZero, LHSOne;
936 uint64_t RHSZero, RHSOne;
937 uint64_t Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +0000938 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000939 if (LHSZero) {
Dan Gohmanea859be2007-06-22 14:59:07 +0000940 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000941
942 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
943 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
944 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
945 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
946 return DAG.getNode(ISD::OR, VT, N0, N1);
947 }
948 }
Evan Cheng3ef554d2006-11-06 08:14:30 +0000949
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000950 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
951 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
952 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
953 if (Result.Val) return Result;
954 }
955 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
956 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
957 if (Result.Val) return Result;
958 }
959
Evan Chengb13cdbd2007-06-21 07:39:16 +0000960 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
961 if (N0.getOpcode() == ISD::SELECT && N0.Val->hasOneUse()) {
962 SDOperand Result = combineSelectAndUse(N, N0, N1, DAG);
963 if (Result.Val) return Result;
964 }
965 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
966 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
967 if (Result.Val) return Result;
968 }
969
Nate Begeman83e75ec2005-09-06 04:43:02 +0000970 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000971}
972
Chris Lattner91153682007-03-04 20:03:15 +0000973SDOperand DAGCombiner::visitADDC(SDNode *N) {
974 SDOperand N0 = N->getOperand(0);
975 SDOperand N1 = N->getOperand(1);
976 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
977 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
978 MVT::ValueType VT = N0.getValueType();
979
980 // If the flag result is dead, turn this into an ADD.
981 if (N->hasNUsesOfValue(0, 1))
982 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +0000983 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +0000984
985 // canonicalize constant to RHS.
Chris Lattnerbcf24842007-03-04 20:08:45 +0000986 if (N0C && !N1C) {
987 SDOperand Ops[] = { N1, N0 };
988 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
989 }
Chris Lattner91153682007-03-04 20:03:15 +0000990
Chris Lattnerb6541762007-03-04 20:40:38 +0000991 // fold (addc x, 0) -> x + no carry out
992 if (N1C && N1C->isNullValue())
993 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
994
995 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
996 uint64_t LHSZero, LHSOne;
997 uint64_t RHSZero, RHSOne;
998 uint64_t Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +0000999 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001000 if (LHSZero) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001001 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001002
1003 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1004 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1005 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1006 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1007 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1008 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1009 }
Chris Lattner91153682007-03-04 20:03:15 +00001010
1011 return SDOperand();
1012}
1013
1014SDOperand DAGCombiner::visitADDE(SDNode *N) {
1015 SDOperand N0 = N->getOperand(0);
1016 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +00001017 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001018 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1019 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Chris Lattnerbcf24842007-03-04 20:08:45 +00001020 //MVT::ValueType VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001021
1022 // canonicalize constant to RHS
Chris Lattnerbcf24842007-03-04 20:08:45 +00001023 if (N0C && !N1C) {
Chris Lattnerb6541762007-03-04 20:40:38 +00001024 SDOperand Ops[] = { N1, N0, CarryIn };
Chris Lattnerbcf24842007-03-04 20:08:45 +00001025 return DAG.getNode(ISD::ADDE, N->getVTList(), Ops, 3);
1026 }
Chris Lattner91153682007-03-04 20:03:15 +00001027
Chris Lattnerb6541762007-03-04 20:40:38 +00001028 // fold (adde x, y, false) -> (addc x, y)
1029 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) {
1030 SDOperand Ops[] = { N1, N0 };
1031 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
1032 }
Chris Lattner91153682007-03-04 20:03:15 +00001033
1034 return SDOperand();
1035}
1036
1037
1038
Nate Begeman83e75ec2005-09-06 04:43:02 +00001039SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001040 SDOperand N0 = N->getOperand(0);
1041 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001042 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1043 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001044 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001045
Dan Gohman7f321562007-06-25 16:23:39 +00001046 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001047 if (MVT::isVector(VT)) {
1048 SDOperand FoldedVOp = SimplifyVBinOp(N);
1049 if (FoldedVOp.Val) return FoldedVOp;
1050 }
Dan Gohman7f321562007-06-25 16:23:39 +00001051
Chris Lattner854077d2005-10-17 01:07:11 +00001052 // fold (sub x, x) -> 0
1053 if (N0 == N1)
1054 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001055 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001056 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001057 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001058 // fold (sub x, c) -> (add x, -c)
1059 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001060 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001061 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001062 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001063 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001064 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001065 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001066 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001067 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1068 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1069 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1070 if (Result.Val) return Result;
1071 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001072 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001073 if (N0.getOpcode() == ISD::UNDEF)
1074 return N0;
1075 if (N1.getOpcode() == ISD::UNDEF)
1076 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001077
Nate Begeman83e75ec2005-09-06 04:43:02 +00001078 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001079}
1080
Nate Begeman83e75ec2005-09-06 04:43:02 +00001081SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001082 SDOperand N0 = N->getOperand(0);
1083 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001084 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1085 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +00001086 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001087
Dan Gohman7f321562007-06-25 16:23:39 +00001088 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001089 if (MVT::isVector(VT)) {
1090 SDOperand FoldedVOp = SimplifyVBinOp(N);
1091 if (FoldedVOp.Val) return FoldedVOp;
1092 }
Dan Gohman7f321562007-06-25 16:23:39 +00001093
Dan Gohman613e0d82007-07-03 14:03:57 +00001094 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001095 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001096 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001097 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001098 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001099 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001100 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001101 if (N0C && !N1C)
1102 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001103 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001104 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001105 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001106 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001107 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001108 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001109 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +00001110 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +00001111 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001112 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001113 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001114 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1115 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1116 // FIXME: If the input is something that is easily negated (e.g. a
1117 // single-use add), we should put the negate there.
1118 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1119 DAG.getNode(ISD::SHL, VT, N0,
1120 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1121 TLI.getShiftAmountTy())));
1122 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001123
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001124 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1125 if (N1C && N0.getOpcode() == ISD::SHL &&
1126 isa<ConstantSDNode>(N0.getOperand(1))) {
1127 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001128 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001129 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1130 }
1131
1132 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1133 // use.
1134 {
1135 SDOperand Sh(0,0), Y(0,0);
1136 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1137 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1138 N0.Val->hasOneUse()) {
1139 Sh = N0; Y = N1;
1140 } else if (N1.getOpcode() == ISD::SHL &&
1141 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1142 Sh = N1; Y = N0;
1143 }
1144 if (Sh.Val) {
1145 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1146 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1147 }
1148 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001149 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1150 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1151 isa<ConstantSDNode>(N0.getOperand(1))) {
1152 return DAG.getNode(ISD::ADD, VT,
1153 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1154 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1155 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001156
Nate Begemancd4d58c2006-02-03 06:46:56 +00001157 // reassociate mul
1158 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1159 if (RMUL.Val != 0)
1160 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001161
Nate Begeman83e75ec2005-09-06 04:43:02 +00001162 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001163}
1164
Nate Begeman83e75ec2005-09-06 04:43:02 +00001165SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001166 SDOperand N0 = N->getOperand(0);
1167 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001168 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1169 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001170 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001171
Dan Gohman7f321562007-06-25 16:23:39 +00001172 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001173 if (MVT::isVector(VT)) {
1174 SDOperand FoldedVOp = SimplifyVBinOp(N);
1175 if (FoldedVOp.Val) return FoldedVOp;
1176 }
Dan Gohman7f321562007-06-25 16:23:39 +00001177
Nate Begeman1d4d4142005-09-01 00:19:25 +00001178 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001179 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001180 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001181 // fold (sdiv X, 1) -> X
1182 if (N1C && N1C->getSignExtended() == 1LL)
1183 return N0;
1184 // fold (sdiv X, -1) -> 0-X
1185 if (N1C && N1C->isAllOnesValue())
1186 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001187 // If we know the sign bits of both operands are zero, strength reduce to a
1188 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
1189 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001190 if (DAG.MaskedValueIsZero(N1, SignBit) &&
1191 DAG.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +00001192 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001193 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +00001194 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001195 (isPowerOf2_64(N1C->getSignExtended()) ||
1196 isPowerOf2_64(-N1C->getSignExtended()))) {
1197 // If dividing by powers of two is cheap, then don't perform the following
1198 // fold.
1199 if (TLI.isPow2DivCheap())
1200 return SDOperand();
1201 int64_t pow2 = N1C->getSignExtended();
1202 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001203 unsigned lg2 = Log2_64(abs2);
1204 // Splat the sign bit into the register
1205 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001206 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1207 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001208 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001209 // Add (N0 < 0) ? abs2 - 1 : 0;
1210 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1211 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001212 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001213 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001214 AddToWorkList(SRL.Val);
1215 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001216 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1217 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001218 // If we're dividing by a positive value, we're done. Otherwise, we must
1219 // negate the result.
1220 if (pow2 > 0)
1221 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001222 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001223 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1224 }
Nate Begeman69575232005-10-20 02:15:44 +00001225 // if integer divide is expensive and we satisfy the requirements, emit an
1226 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001227 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001228 !TLI.isIntDivCheap()) {
1229 SDOperand Op = BuildSDIV(N);
1230 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001231 }
Dan Gohman7f321562007-06-25 16:23:39 +00001232
Dan Gohman613e0d82007-07-03 14:03:57 +00001233 // undef / X -> 0
1234 if (N0.getOpcode() == ISD::UNDEF)
1235 return DAG.getConstant(0, VT);
1236 // X / undef -> undef
1237 if (N1.getOpcode() == ISD::UNDEF)
1238 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001239
Nate Begeman83e75ec2005-09-06 04:43:02 +00001240 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001241}
1242
Nate Begeman83e75ec2005-09-06 04:43:02 +00001243SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001244 SDOperand N0 = N->getOperand(0);
1245 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001246 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1247 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001248 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001249
Dan Gohman7f321562007-06-25 16:23:39 +00001250 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001251 if (MVT::isVector(VT)) {
1252 SDOperand FoldedVOp = SimplifyVBinOp(N);
1253 if (FoldedVOp.Val) return FoldedVOp;
1254 }
Dan Gohman7f321562007-06-25 16:23:39 +00001255
Nate Begeman1d4d4142005-09-01 00:19:25 +00001256 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001257 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001258 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001259 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +00001260 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001261 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001262 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001263 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001264 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1265 if (N1.getOpcode() == ISD::SHL) {
1266 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1267 if (isPowerOf2_64(SHC->getValue())) {
1268 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001269 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
1270 DAG.getConstant(Log2_64(SHC->getValue()),
1271 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001272 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001273 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001274 }
1275 }
1276 }
Nate Begeman69575232005-10-20 02:15:44 +00001277 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +00001278 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
1279 SDOperand Op = BuildUDIV(N);
1280 if (Op.Val) return Op;
1281 }
Dan Gohman7f321562007-06-25 16:23:39 +00001282
Dan Gohman613e0d82007-07-03 14:03:57 +00001283 // undef / X -> 0
1284 if (N0.getOpcode() == ISD::UNDEF)
1285 return DAG.getConstant(0, VT);
1286 // X / undef -> undef
1287 if (N1.getOpcode() == ISD::UNDEF)
1288 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001289
Nate Begeman83e75ec2005-09-06 04:43:02 +00001290 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001291}
1292
Nate Begeman83e75ec2005-09-06 04:43:02 +00001293SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001294 SDOperand N0 = N->getOperand(0);
1295 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001296 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1297 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001298 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001299
1300 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001301 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001302 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001303 // If we know the sign bits of both operands are zero, strength reduce to a
1304 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
1305 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001306 if (DAG.MaskedValueIsZero(N1, SignBit) &&
1307 DAG.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +00001308 return DAG.getNode(ISD::UREM, VT, N0, N1);
Chris Lattner26d29902006-10-12 20:58:32 +00001309
1310 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1311 // the remainder operation.
1312 if (N1C && !N1C->isNullValue()) {
1313 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
1314 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1315 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1316 AddToWorkList(Div.Val);
1317 AddToWorkList(Mul.Val);
1318 return Sub;
1319 }
1320
Dan Gohman613e0d82007-07-03 14:03:57 +00001321 // undef % X -> 0
1322 if (N0.getOpcode() == ISD::UNDEF)
1323 return DAG.getConstant(0, VT);
1324 // X % undef -> undef
1325 if (N1.getOpcode() == ISD::UNDEF)
1326 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001327
Nate Begeman83e75ec2005-09-06 04:43:02 +00001328 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001329}
1330
Nate Begeman83e75ec2005-09-06 04:43:02 +00001331SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001332 SDOperand N0 = N->getOperand(0);
1333 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001334 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1335 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001336 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001337
1338 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001339 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001340 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001341 // fold (urem x, pow2) -> (and x, pow2-1)
1342 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +00001343 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001344 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1345 if (N1.getOpcode() == ISD::SHL) {
1346 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1347 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +00001348 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001349 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001350 return DAG.getNode(ISD::AND, VT, N0, Add);
1351 }
1352 }
1353 }
Chris Lattner26d29902006-10-12 20:58:32 +00001354
1355 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1356 // the remainder operation.
1357 if (N1C && !N1C->isNullValue()) {
1358 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
1359 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1360 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1361 AddToWorkList(Div.Val);
1362 AddToWorkList(Mul.Val);
1363 return Sub;
1364 }
1365
Dan Gohman613e0d82007-07-03 14:03:57 +00001366 // undef % X -> 0
1367 if (N0.getOpcode() == ISD::UNDEF)
1368 return DAG.getConstant(0, VT);
1369 // X % undef -> undef
1370 if (N1.getOpcode() == ISD::UNDEF)
1371 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001372
Nate Begeman83e75ec2005-09-06 04:43:02 +00001373 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001374}
1375
Nate Begeman83e75ec2005-09-06 04:43:02 +00001376SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001377 SDOperand N0 = N->getOperand(0);
1378 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001379 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001380 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001381
1382 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001383 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001384 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001385 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001386 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001387 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1388 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001389 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001390 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001391 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001392 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001393
Nate Begeman83e75ec2005-09-06 04:43:02 +00001394 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001395}
1396
Nate Begeman83e75ec2005-09-06 04:43:02 +00001397SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001398 SDOperand N0 = N->getOperand(0);
1399 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001400 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001401 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001402
1403 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001404 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001405 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001406 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001407 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001408 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001409 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001410 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001411 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001412
Nate Begeman83e75ec2005-09-06 04:43:02 +00001413 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001414}
1415
Dan Gohman389079b2007-10-08 17:57:15 +00001416/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1417/// compute two values. LoOp and HiOp give the opcodes for the two computations
1418/// that are being performed. Return true if a simplification was made.
1419///
1420bool DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N,
1421 unsigned LoOp, unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001422 // If the high half is not needed, just compute the low half.
1423 if (!N->hasAnyUseOfValue(1) &&
1424 (!AfterLegalize ||
1425 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
1426 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0),
1427 DAG.getNode(LoOp, N->getValueType(0),
1428 N->op_begin(),
Chris Lattner01d029b2007-10-15 06:10:22 +00001429 N->getNumOperands()));
Dan Gohman389079b2007-10-08 17:57:15 +00001430 return true;
1431 }
1432
1433 // If the low half is not needed, just compute the high half.
1434 if (!N->hasAnyUseOfValue(0) &&
1435 (!AfterLegalize ||
1436 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
1437 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1),
1438 DAG.getNode(HiOp, N->getValueType(1),
1439 N->op_begin(),
Chris Lattner01d029b2007-10-15 06:10:22 +00001440 N->getNumOperands()));
Dan Gohman389079b2007-10-08 17:57:15 +00001441 return true;
1442 }
1443
1444 // If the two computed results can be siplified separately, separate them.
1445 SDOperand Lo = DAG.getNode(LoOp, N->getValueType(0),
1446 N->op_begin(), N->getNumOperands());
1447 SDOperand Hi = DAG.getNode(HiOp, N->getValueType(1),
1448 N->op_begin(), N->getNumOperands());
1449 unsigned LoExists = !Lo.use_empty();
1450 unsigned HiExists = !Hi.use_empty();
1451 SDOperand LoOpt = Lo;
1452 SDOperand HiOpt = Hi;
1453 if (!LoExists || !HiExists) {
1454 SDOperand Pair = DAG.getNode(ISD::BUILD_PAIR, MVT::Other, Lo, Hi);
1455 assert(Pair.use_empty() && "Pair with type MVT::Other already exists!");
1456 LoOpt = combine(Lo.Val);
1457 HiOpt = combine(Hi.Val);
1458 if (!LoOpt.Val)
1459 LoOpt = Pair.getOperand(0);
1460 if (!HiOpt.Val)
1461 HiOpt = Pair.getOperand(1);
1462 DAG.DeleteNode(Pair.Val);
1463 }
1464 if ((LoExists || LoOpt != Lo) &&
1465 (HiExists || HiOpt != Hi) &&
1466 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType()) &&
1467 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())) {
Chris Lattner01d029b2007-10-15 06:10:22 +00001468 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), LoOpt);
1469 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), HiOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001470 return true;
1471 }
1472
1473 return false;
1474}
1475
1476SDOperand DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1477
1478 if (SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS))
1479 return SDOperand();
1480
1481 return SDOperand();
1482}
1483
1484SDOperand DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1485
1486 if (SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU))
1487 return SDOperand();
1488
1489 return SDOperand();
1490}
1491
1492SDOperand DAGCombiner::visitSDIVREM(SDNode *N) {
1493
1494 if (SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM))
1495 return SDOperand();
1496
1497 return SDOperand();
1498}
1499
1500SDOperand DAGCombiner::visitUDIVREM(SDNode *N) {
1501
1502 if (SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM))
1503 return SDOperand();
1504
1505 return SDOperand();
1506}
1507
Chris Lattner35e5c142006-05-05 05:51:50 +00001508/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1509/// two operands of the same opcode, try to simplify it.
1510SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1511 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1512 MVT::ValueType VT = N0.getValueType();
1513 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1514
Chris Lattner540121f2006-05-05 06:31:05 +00001515 // For each of OP in AND/OR/XOR:
1516 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1517 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1518 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001519 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001520 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001521 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001522 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1523 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1524 N0.getOperand(0).getValueType(),
1525 N0.getOperand(0), N1.getOperand(0));
1526 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001527 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001528 }
1529
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001530 // For each of OP in SHL/SRL/SRA/AND...
1531 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1532 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1533 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001534 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001535 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001536 N0.getOperand(1) == N1.getOperand(1)) {
1537 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1538 N0.getOperand(0).getValueType(),
1539 N0.getOperand(0), N1.getOperand(0));
1540 AddToWorkList(ORNode.Val);
1541 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1542 }
1543
1544 return SDOperand();
1545}
1546
Nate Begeman83e75ec2005-09-06 04:43:02 +00001547SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001548 SDOperand N0 = N->getOperand(0);
1549 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001550 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001551 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1552 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001553 MVT::ValueType VT = N1.getValueType();
1554
Dan Gohman7f321562007-06-25 16:23:39 +00001555 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001556 if (MVT::isVector(VT)) {
1557 SDOperand FoldedVOp = SimplifyVBinOp(N);
1558 if (FoldedVOp.Val) return FoldedVOp;
1559 }
Dan Gohman7f321562007-06-25 16:23:39 +00001560
Dan Gohman613e0d82007-07-03 14:03:57 +00001561 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001562 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001563 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001564 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001565 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001566 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001567 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001568 if (N0C && !N1C)
1569 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001570 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001571 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001572 return N0;
1573 // if (and x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001574 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001575 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001576 // reassociate and
1577 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1578 if (RAND.Val != 0)
1579 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001580 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001581 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001582 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001583 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001584 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001585 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1586 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001587 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Dan Gohmanea859be2007-06-22 14:59:07 +00001588 if (DAG.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001589 ~N1C->getValue() & InMask)) {
1590 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1591 N0.getOperand(0));
1592
1593 // Replace uses of the AND with uses of the Zero extend node.
1594 CombineTo(N, Zext);
1595
Chris Lattner3603cd62006-02-02 07:17:31 +00001596 // We actually want to replace all uses of the any_extend with the
1597 // zero_extend, to avoid duplicating things. This will later cause this
1598 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001599 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001600 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001601 }
1602 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001603 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1604 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1605 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1606 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1607
1608 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1609 MVT::isInteger(LL.getValueType())) {
1610 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1611 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1612 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001613 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001614 return DAG.getSetCC(VT, ORNode, LR, Op1);
1615 }
1616 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1617 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1618 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001619 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001620 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1621 }
1622 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1623 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1624 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001625 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001626 return DAG.getSetCC(VT, ORNode, LR, Op1);
1627 }
1628 }
1629 // canonicalize equivalent to ll == rl
1630 if (LL == RR && LR == RL) {
1631 Op1 = ISD::getSetCCSwappedOperands(Op1);
1632 std::swap(RL, RR);
1633 }
1634 if (LL == RL && LR == RR) {
1635 bool isInteger = MVT::isInteger(LL.getValueType());
1636 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1637 if (Result != ISD::SETCC_INVALID)
1638 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1639 }
1640 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001641
1642 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1643 if (N0.getOpcode() == N1.getOpcode()) {
1644 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1645 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001646 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001647
Nate Begemande996292006-02-03 22:24:05 +00001648 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1649 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001650 if (!MVT::isVector(VT) &&
1651 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001652 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001653 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001654 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001655 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001656 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001657 // If we zero all the possible extended bits, then we can turn this into
1658 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmanea859be2007-06-22 14:59:07 +00001659 if (DAG.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001660 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001661 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1662 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001663 LN0->getSrcValueOffset(), EVT,
1664 LN0->isVolatile(),
1665 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001666 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001667 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001668 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001669 }
1670 }
1671 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001672 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1673 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001674 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001675 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001676 // If we zero all the possible extended bits, then we can turn this into
1677 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmanea859be2007-06-22 14:59:07 +00001678 if (DAG.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001679 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001680 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1681 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001682 LN0->getSrcValueOffset(), EVT,
1683 LN0->isVolatile(),
1684 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001685 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001686 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001687 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001688 }
1689 }
Chris Lattner15045b62006-02-28 06:35:35 +00001690
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001691 // fold (and (load x), 255) -> (zextload x, i8)
1692 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001693 if (N1C && N0.getOpcode() == ISD::LOAD) {
1694 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1695 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Evan Cheng83060c52007-03-07 08:07:03 +00001696 LN0->getAddressingMode() == ISD::UNINDEXED &&
Evan Cheng466685d2006-10-09 20:57:25 +00001697 N0.hasOneUse()) {
1698 MVT::ValueType EVT, LoadedVT;
1699 if (N1C->getValue() == 255)
1700 EVT = MVT::i8;
1701 else if (N1C->getValue() == 65535)
1702 EVT = MVT::i16;
1703 else if (N1C->getValue() == ~0U)
1704 EVT = MVT::i32;
1705 else
1706 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001707
Evan Cheng2e49f092006-10-11 07:10:22 +00001708 LoadedVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001709 if (EVT != MVT::Other && LoadedVT > EVT &&
1710 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1711 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1712 // For big endian targets, we need to add an offset to the pointer to
1713 // load the correct bytes. For little endian systems, we merely need to
1714 // read fewer bytes from the same pointer.
1715 unsigned PtrOff =
1716 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00001717 unsigned Alignment = LN0->getAlignment();
Evan Cheng466685d2006-10-09 20:57:25 +00001718 SDOperand NewPtr = LN0->getBasePtr();
Duncan Sandsdc846502007-10-28 12:59:45 +00001719 if (!TLI.isLittleEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001720 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1721 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001722 Alignment = MinAlign(Alignment, PtrOff);
1723 }
Evan Cheng466685d2006-10-09 20:57:25 +00001724 AddToWorkList(NewPtr.Val);
1725 SDOperand Load =
1726 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001727 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001728 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001729 AddToWorkList(N);
1730 CombineTo(N0.Val, Load, Load.getValue(1));
1731 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1732 }
Chris Lattner15045b62006-02-28 06:35:35 +00001733 }
1734 }
1735
Nate Begeman83e75ec2005-09-06 04:43:02 +00001736 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001737}
1738
Nate Begeman83e75ec2005-09-06 04:43:02 +00001739SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001740 SDOperand N0 = N->getOperand(0);
1741 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001742 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001743 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1744 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001745 MVT::ValueType VT = N1.getValueType();
1746 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001747
Dan Gohman7f321562007-06-25 16:23:39 +00001748 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001749 if (MVT::isVector(VT)) {
1750 SDOperand FoldedVOp = SimplifyVBinOp(N);
1751 if (FoldedVOp.Val) return FoldedVOp;
1752 }
Dan Gohman7f321562007-06-25 16:23:39 +00001753
Dan Gohman613e0d82007-07-03 14:03:57 +00001754 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001755 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001756 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001757 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001758 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001759 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001760 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001761 if (N0C && !N1C)
1762 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001763 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001764 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001765 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001766 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001767 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001768 return N1;
1769 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001770 if (N1C &&
Dan Gohmanea859be2007-06-22 14:59:07 +00001771 DAG.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001772 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001773 // reassociate or
1774 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1775 if (ROR.Val != 0)
1776 return ROR;
1777 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1778 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001779 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001780 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1781 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1782 N1),
1783 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001784 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001785 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1786 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1787 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1788 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1789
1790 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1791 MVT::isInteger(LL.getValueType())) {
1792 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1793 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1794 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1795 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1796 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001797 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001798 return DAG.getSetCC(VT, ORNode, LR, Op1);
1799 }
1800 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1801 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1802 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1803 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1804 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001805 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001806 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1807 }
1808 }
1809 // canonicalize equivalent to ll == rl
1810 if (LL == RR && LR == RL) {
1811 Op1 = ISD::getSetCCSwappedOperands(Op1);
1812 std::swap(RL, RR);
1813 }
1814 if (LL == RL && LR == RR) {
1815 bool isInteger = MVT::isInteger(LL.getValueType());
1816 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1817 if (Result != ISD::SETCC_INVALID)
1818 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1819 }
1820 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001821
1822 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1823 if (N0.getOpcode() == N1.getOpcode()) {
1824 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1825 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001826 }
Chris Lattner516b9622006-09-14 20:50:57 +00001827
Chris Lattner1ec72732006-09-14 21:11:37 +00001828 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1829 if (N0.getOpcode() == ISD::AND &&
1830 N1.getOpcode() == ISD::AND &&
1831 N0.getOperand(1).getOpcode() == ISD::Constant &&
1832 N1.getOperand(1).getOpcode() == ISD::Constant &&
1833 // Don't increase # computations.
1834 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1835 // We can only do this xform if we know that bits from X that are set in C2
1836 // but not in C1 are already zero. Likewise for Y.
1837 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1838 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1839
Dan Gohmanea859be2007-06-22 14:59:07 +00001840 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1841 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001842 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1843 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1844 }
1845 }
1846
1847
Chris Lattner516b9622006-09-14 20:50:57 +00001848 // See if this is some rotate idiom.
1849 if (SDNode *Rot = MatchRotate(N0, N1))
1850 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001851
Nate Begeman83e75ec2005-09-06 04:43:02 +00001852 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001853}
1854
Chris Lattner516b9622006-09-14 20:50:57 +00001855
1856/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1857static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1858 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001859 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001860 Mask = Op.getOperand(1);
1861 Op = Op.getOperand(0);
1862 } else {
1863 return false;
1864 }
1865 }
1866
1867 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1868 Shift = Op;
1869 return true;
1870 }
1871 return false;
1872}
1873
1874
1875// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1876// idioms for rotate, and if the target supports rotation instructions, generate
1877// a rot[lr].
1878SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1879 // Must be a legal type. Expanded an promoted things won't work with rotates.
1880 MVT::ValueType VT = LHS.getValueType();
1881 if (!TLI.isTypeLegal(VT)) return 0;
1882
1883 // The target must have at least one rotate flavor.
1884 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1885 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1886 if (!HasROTL && !HasROTR) return 0;
1887
1888 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1889 SDOperand LHSShift; // The shift.
1890 SDOperand LHSMask; // AND value if any.
1891 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1892 return 0; // Not part of a rotate.
1893
1894 SDOperand RHSShift; // The shift.
1895 SDOperand RHSMask; // AND value if any.
1896 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1897 return 0; // Not part of a rotate.
1898
1899 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1900 return 0; // Not shifting the same value.
1901
1902 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1903 return 0; // Shifts must disagree.
1904
1905 // Canonicalize shl to left side in a shl/srl pair.
1906 if (RHSShift.getOpcode() == ISD::SHL) {
1907 std::swap(LHS, RHS);
1908 std::swap(LHSShift, RHSShift);
1909 std::swap(LHSMask , RHSMask );
1910 }
1911
1912 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Scott Michelc9dc1142007-04-02 21:36:32 +00001913 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1914 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1915 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001916
1917 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1918 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001919 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1920 RHSShiftAmt.getOpcode() == ISD::Constant) {
1921 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
1922 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00001923 if ((LShVal + RShVal) != OpSizeInBits)
1924 return 0;
1925
1926 SDOperand Rot;
1927 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001928 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001929 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001930 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001931
1932 // If there is an AND of either shifted operand, apply it to the result.
1933 if (LHSMask.Val || RHSMask.Val) {
1934 uint64_t Mask = MVT::getIntVTBitMask(VT);
1935
1936 if (LHSMask.Val) {
1937 uint64_t RHSBits = (1ULL << LShVal)-1;
1938 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1939 }
1940 if (RHSMask.Val) {
1941 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1942 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1943 }
1944
1945 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1946 }
1947
1948 return Rot.Val;
1949 }
1950
1951 // If there is a mask here, and we have a variable shift, we can't be sure
1952 // that we're masking out the right stuff.
1953 if (LHSMask.Val || RHSMask.Val)
1954 return 0;
1955
1956 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1957 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001958 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
1959 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001960 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001961 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001962 if (SUBC->getValue() == OpSizeInBits)
1963 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001964 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001965 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001966 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001967 }
1968 }
1969
1970 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1971 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001972 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
1973 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001974 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001975 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001976 if (SUBC->getValue() == OpSizeInBits)
1977 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001978 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001979 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001980 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
1981 }
1982 }
1983
1984 // Look for sign/zext/any-extended cases:
1985 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1986 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1987 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
1988 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1989 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1990 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
1991 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
1992 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
1993 if (RExtOp0.getOpcode() == ISD::SUB &&
1994 RExtOp0.getOperand(1) == LExtOp0) {
1995 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
1996 // (rotr x, y)
1997 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
1998 // (rotl x, (sub 32, y))
1999 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
2000 if (SUBC->getValue() == OpSizeInBits) {
2001 if (HasROTL)
2002 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2003 else
2004 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
2005 }
2006 }
2007 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2008 RExtOp0 == LExtOp0.getOperand(1)) {
2009 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2010 // (rotl x, y)
2011 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2012 // (rotr x, (sub 32, y))
2013 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
2014 if (SUBC->getValue() == OpSizeInBits) {
2015 if (HasROTL)
2016 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
2017 else
2018 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2019 }
2020 }
Chris Lattner516b9622006-09-14 20:50:57 +00002021 }
2022 }
2023
2024 return 0;
2025}
2026
2027
Nate Begeman83e75ec2005-09-06 04:43:02 +00002028SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002029 SDOperand N0 = N->getOperand(0);
2030 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002031 SDOperand LHS, RHS, CC;
2032 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2033 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002034 MVT::ValueType VT = N0.getValueType();
2035
Dan Gohman7f321562007-06-25 16:23:39 +00002036 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00002037 if (MVT::isVector(VT)) {
2038 SDOperand FoldedVOp = SimplifyVBinOp(N);
2039 if (FoldedVOp.Val) return FoldedVOp;
2040 }
Dan Gohman7f321562007-06-25 16:23:39 +00002041
Dan Gohman613e0d82007-07-03 14:03:57 +00002042 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002043 if (N0.getOpcode() == ISD::UNDEF)
2044 return N0;
2045 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002046 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002047 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002048 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002049 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00002050 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002051 if (N0C && !N1C)
2052 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002053 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002054 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002055 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002056 // reassociate xor
2057 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
2058 if (RXOR.Val != 0)
2059 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002060 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00002061 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
2062 bool isInt = MVT::isInteger(LHS.getValueType());
2063 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2064 isInt);
2065 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002066 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002067 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002068 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002069 assert(0 && "Unhandled SetCC Equivalent!");
2070 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002071 }
Chris Lattner61c5ff42007-09-10 21:39:07 +00002072 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
2073 if (N1C && N1C->getValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
2074 N0.Val->hasOneUse() && isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
2075 SDOperand V = N0.getOperand(0);
2076 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002077 DAG.getConstant(1, V.getValueType()));
Chris Lattner61c5ff42007-09-10 21:39:07 +00002078 AddToWorkList(V.Val);
2079 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2080 }
2081
Nate Begeman99801192005-09-07 23:25:52 +00002082 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Chris Lattner734c91d2006-11-10 21:37:15 +00002083 if (N1C && N1C->getValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002084 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002085 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002086 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2087 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002088 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2089 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002090 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002091 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002092 }
2093 }
Nate Begeman99801192005-09-07 23:25:52 +00002094 // fold !(x or y) -> (!x and !y) iff x or y are constants
2095 if (N1C && N1C->isAllOnesValue() &&
2096 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002097 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002098 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2099 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002100 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2101 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002102 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002103 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002104 }
2105 }
Nate Begeman223df222005-09-08 20:18:10 +00002106 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2107 if (N1C && N0.getOpcode() == ISD::XOR) {
2108 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2109 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2110 if (N00C)
2111 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
2112 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
2113 if (N01C)
2114 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
2115 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
2116 }
2117 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002118 if (N0 == N1) {
2119 if (!MVT::isVector(VT)) {
2120 return DAG.getConstant(0, VT);
2121 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2122 // Produce a vector of zeros.
Dan Gohman51eaa862007-06-14 22:58:02 +00002123 SDOperand El = DAG.getConstant(0, MVT::getVectorElementType(VT));
Chris Lattner4fbdd592006-03-28 19:11:05 +00002124 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002125 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002126 }
2127 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002128
2129 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2130 if (N0.getOpcode() == N1.getOpcode()) {
2131 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
2132 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002133 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002134
Chris Lattner3e104b12006-04-08 04:15:24 +00002135 // Simplify the expression using non-local knowledge.
2136 if (!MVT::isVector(VT) &&
2137 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002138 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002139
Nate Begeman83e75ec2005-09-06 04:43:02 +00002140 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002141}
2142
Nate Begeman83e75ec2005-09-06 04:43:02 +00002143SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002144 SDOperand N0 = N->getOperand(0);
2145 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002146 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2147 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002148 MVT::ValueType VT = N0.getValueType();
2149 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2150
2151 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002152 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002153 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002154 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002155 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002156 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002157 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002158 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002159 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002160 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002161 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002162 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002163 // if (shl x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002164 if (DAG.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002165 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00002166 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002167 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002168 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002169 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002170 N0.getOperand(1).getOpcode() == ISD::Constant) {
2171 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002172 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002173 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002174 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002175 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002176 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002177 }
2178 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2179 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002180 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002181 N0.getOperand(1).getOpcode() == ISD::Constant) {
2182 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002183 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002184 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
2185 DAG.getConstant(~0ULL << c1, VT));
2186 if (c2 > c1)
2187 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002188 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002189 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002190 return DAG.getNode(ISD::SRL, VT, Mask,
2191 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002192 }
2193 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002194 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002195 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002196 DAG.getConstant(~0ULL << N1C->getValue(), VT));
2197 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002198}
2199
Nate Begeman83e75ec2005-09-06 04:43:02 +00002200SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002201 SDOperand N0 = N->getOperand(0);
2202 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002203 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2204 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002205 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002206
2207 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002208 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002209 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002210 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002211 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002212 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002213 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002214 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002215 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002216 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00002217 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002218 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002219 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002220 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002221 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002222 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2223 // sext_inreg.
2224 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
2225 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
2226 MVT::ValueType EVT;
2227 switch (LowBits) {
2228 default: EVT = MVT::Other; break;
2229 case 1: EVT = MVT::i1; break;
2230 case 8: EVT = MVT::i8; break;
2231 case 16: EVT = MVT::i16; break;
2232 case 32: EVT = MVT::i32; break;
2233 }
2234 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
2235 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2236 DAG.getValueType(EVT));
2237 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002238
2239 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2240 if (N1C && N0.getOpcode() == ISD::SRA) {
2241 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2242 unsigned Sum = N1C->getValue() + C1->getValue();
2243 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
2244 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2245 DAG.getConstant(Sum, N1C->getValueType(0)));
2246 }
2247 }
2248
Chris Lattnera8504462006-05-08 20:51:54 +00002249 // Simplify, based on bits shifted out of the LHS.
2250 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2251 return SDOperand(N, 0);
2252
2253
Nate Begeman1d4d4142005-09-01 00:19:25 +00002254 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohmanea859be2007-06-22 14:59:07 +00002255 if (DAG.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002256 return DAG.getNode(ISD::SRL, VT, N0, N1);
2257 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002258}
2259
Nate Begeman83e75ec2005-09-06 04:43:02 +00002260SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002261 SDOperand N0 = N->getOperand(0);
2262 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002263 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2264 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002265 MVT::ValueType VT = N0.getValueType();
2266 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2267
2268 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002269 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002270 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002271 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002272 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002273 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002274 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002275 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002276 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002277 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002278 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002279 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002280 // if (srl x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002281 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002282 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002283
Nate Begeman1d4d4142005-09-01 00:19:25 +00002284 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002285 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002286 N0.getOperand(1).getOpcode() == ISD::Constant) {
2287 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002288 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002289 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002290 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002291 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002292 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002293 }
Chris Lattner350bec02006-04-02 06:11:11 +00002294
Chris Lattner06afe072006-05-05 22:53:17 +00002295 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2296 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2297 // Shifting in all undef bits?
2298 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
2299 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
2300 return DAG.getNode(ISD::UNDEF, VT);
2301
2302 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
2303 AddToWorkList(SmallShift.Val);
2304 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2305 }
2306
Chris Lattner3657ffe2006-10-12 20:23:19 +00002307 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2308 // bit, which is unmodified by sra.
2309 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
2310 if (N0.getOpcode() == ISD::SRA)
2311 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2312 }
2313
Chris Lattner350bec02006-04-02 06:11:11 +00002314 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2315 if (N1C && N0.getOpcode() == ISD::CTLZ &&
2316 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
2317 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002318 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002319
2320 // If any of the input bits are KnownOne, then the input couldn't be all
2321 // zeros, thus the result of the srl will always be zero.
2322 if (KnownOne) return DAG.getConstant(0, VT);
2323
2324 // If all of the bits input the to ctlz node are known to be zero, then
2325 // the result of the ctlz is "32" and the result of the shift is one.
2326 uint64_t UnknownBits = ~KnownZero & Mask;
2327 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2328
2329 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2330 if ((UnknownBits & (UnknownBits-1)) == 0) {
2331 // Okay, we know that only that the single bit specified by UnknownBits
2332 // could be set on input to the CTLZ node. If this bit is set, the SRL
2333 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2334 // to an SRL,XOR pair, which is likely to simplify more.
2335 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
2336 SDOperand Op = N0.getOperand(0);
2337 if (ShAmt) {
2338 Op = DAG.getNode(ISD::SRL, VT, Op,
2339 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2340 AddToWorkList(Op.Val);
2341 }
2342 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2343 }
2344 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002345
2346 // fold operands of srl based on knowledge that the low bits are not
2347 // demanded.
2348 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2349 return SDOperand(N, 0);
2350
Nate Begeman83e75ec2005-09-06 04:43:02 +00002351 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002352}
2353
Nate Begeman83e75ec2005-09-06 04:43:02 +00002354SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002355 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002356 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002357
2358 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002359 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002360 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002361 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002362}
2363
Nate Begeman83e75ec2005-09-06 04:43:02 +00002364SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002365 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002366 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002367
2368 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002369 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002370 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002371 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002372}
2373
Nate Begeman83e75ec2005-09-06 04:43:02 +00002374SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002375 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002376 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002377
2378 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002379 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002380 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002381 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002382}
2383
Nate Begeman452d7be2005-09-16 00:54:12 +00002384SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2385 SDOperand N0 = N->getOperand(0);
2386 SDOperand N1 = N->getOperand(1);
2387 SDOperand N2 = N->getOperand(2);
2388 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2389 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2390 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2391 MVT::ValueType VT = N->getValueType(0);
Evan Cheng571c4782007-08-18 05:57:05 +00002392 MVT::ValueType VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002393
Nate Begeman452d7be2005-09-16 00:54:12 +00002394 // fold select C, X, X -> X
2395 if (N1 == N2)
2396 return N1;
2397 // fold select true, X, Y -> X
2398 if (N0C && !N0C->isNullValue())
2399 return N1;
2400 // fold select false, X, Y -> Y
2401 if (N0C && N0C->isNullValue())
2402 return N2;
2403 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002404 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002405 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002406 // fold select C, 0, 1 -> ~C
2407 if (MVT::isInteger(VT) && MVT::isInteger(VT0) &&
2408 N1C && N2C && N1C->isNullValue() && N2C->getValue() == 1) {
2409 SDOperand XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
2410 if (VT == VT0)
2411 return XORNode;
2412 AddToWorkList(XORNode.Val);
2413 if (MVT::getSizeInBits(VT) > MVT::getSizeInBits(VT0))
2414 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2415 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2416 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002417 // fold select C, 0, X -> ~C & X
Evan Cheng571c4782007-08-18 05:57:05 +00002418 if (VT == VT0 && N1C && N1C->isNullValue()) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002419 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002420 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002421 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2422 }
2423 // fold select C, X, 1 -> ~C | X
Evan Cheng571c4782007-08-18 05:57:05 +00002424 if (VT == VT0 && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002425 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002426 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002427 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2428 }
2429 // fold select C, X, 0 -> C & X
2430 // FIXME: this should check for C type == X type, not i1?
2431 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2432 return DAG.getNode(ISD::AND, VT, N0, N1);
2433 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2434 if (MVT::i1 == VT && N0 == N1)
2435 return DAG.getNode(ISD::OR, VT, N0, N2);
2436 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2437 if (MVT::i1 == VT && N0 == N2)
2438 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002439
Chris Lattner40c62d52005-10-18 06:04:22 +00002440 // If we can fold this based on the true/false value, do so.
2441 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002442 return SDOperand(N, 0); // Don't revisit N.
2443
Nate Begeman44728a72005-09-19 22:34:01 +00002444 // fold selects based on a setcc into other things, such as min/max/abs
2445 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00002446 // FIXME:
2447 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2448 // having to say they don't support SELECT_CC on every type the DAG knows
2449 // about, since there is no way to mark an opcode illegal at all value types
2450 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2451 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2452 N1, N2, N0.getOperand(2));
2453 else
2454 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002455 return SDOperand();
2456}
2457
2458SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002459 SDOperand N0 = N->getOperand(0);
2460 SDOperand N1 = N->getOperand(1);
2461 SDOperand N2 = N->getOperand(2);
2462 SDOperand N3 = N->getOperand(3);
2463 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002464 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2465
Nate Begeman44728a72005-09-19 22:34:01 +00002466 // fold select_cc lhs, rhs, x, x, cc -> x
2467 if (N2 == N3)
2468 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002469
Chris Lattner5f42a242006-09-20 06:19:26 +00002470 // Determine if the condition we're dealing with is constant
2471 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002472 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002473
2474 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
2475 if (SCCC->getValue())
2476 return N2; // cond always true -> true val
2477 else
2478 return N3; // cond always false -> false val
2479 }
2480
2481 // Fold to a simpler select_cc
2482 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2483 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2484 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2485 SCC.getOperand(2));
2486
Chris Lattner40c62d52005-10-18 06:04:22 +00002487 // If we can fold this based on the true/false value, do so.
2488 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002489 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002490
Nate Begeman44728a72005-09-19 22:34:01 +00002491 // fold select_cc into other things, such as min/max/abs
2492 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002493}
2494
2495SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2496 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2497 cast<CondCodeSDNode>(N->getOperand(2))->get());
2498}
2499
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002500// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2501// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2502// transformation. Returns true if extension are possible and the above
2503// mentioned transformation is profitable.
2504static bool ExtendUsesToFormExtLoad(SDNode *N, SDOperand N0,
2505 unsigned ExtOpc,
2506 SmallVector<SDNode*, 4> &ExtendNodes,
2507 TargetLowering &TLI) {
2508 bool HasCopyToRegUses = false;
2509 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
2510 for (SDNode::use_iterator UI = N0.Val->use_begin(), UE = N0.Val->use_end();
2511 UI != UE; ++UI) {
2512 SDNode *User = *UI;
2513 if (User == N)
2514 continue;
2515 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2516 if (User->getOpcode() == ISD::SETCC) {
2517 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2518 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2519 // Sign bits will be lost after a zext.
2520 return false;
2521 bool Add = false;
2522 for (unsigned i = 0; i != 2; ++i) {
2523 SDOperand UseOp = User->getOperand(i);
2524 if (UseOp == N0)
2525 continue;
2526 if (!isa<ConstantSDNode>(UseOp))
2527 return false;
2528 Add = true;
2529 }
2530 if (Add)
2531 ExtendNodes.push_back(User);
2532 } else {
2533 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2534 SDOperand UseOp = User->getOperand(i);
2535 if (UseOp == N0) {
2536 // If truncate from extended type to original load type is free
2537 // on this target, then it's ok to extend a CopyToReg.
2538 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2539 HasCopyToRegUses = true;
2540 else
2541 return false;
2542 }
2543 }
2544 }
2545 }
2546
2547 if (HasCopyToRegUses) {
2548 bool BothLiveOut = false;
2549 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2550 UI != UE; ++UI) {
2551 SDNode *User = *UI;
2552 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2553 SDOperand UseOp = User->getOperand(i);
2554 if (UseOp.Val == N && UseOp.ResNo == 0) {
2555 BothLiveOut = true;
2556 break;
2557 }
2558 }
2559 }
2560 if (BothLiveOut)
2561 // Both unextended and extended values are live out. There had better be
2562 // good a reason for the transformation.
2563 return ExtendNodes.size();
2564 }
2565 return true;
2566}
2567
Nate Begeman83e75ec2005-09-06 04:43:02 +00002568SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002569 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002570 MVT::ValueType VT = N->getValueType(0);
2571
Nate Begeman1d4d4142005-09-01 00:19:25 +00002572 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002573 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002574 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002575
Nate Begeman1d4d4142005-09-01 00:19:25 +00002576 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002577 // fold (sext (aext x)) -> (sext x)
2578 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002579 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002580
Evan Chengc88138f2007-03-22 01:54:19 +00002581 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2582 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Chris Lattner22558872007-02-26 03:13:59 +00002583 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002584 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002585 if (NarrowLoad.Val) {
2586 if (NarrowLoad.Val != N0.Val)
2587 CombineTo(N0.Val, NarrowLoad);
2588 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2589 }
Evan Chengc88138f2007-03-22 01:54:19 +00002590 }
2591
2592 // See if the value being truncated is already sign extended. If so, just
2593 // eliminate the trunc/sext pair.
2594 if (N0.getOpcode() == ISD::TRUNCATE) {
Chris Lattner6007b842006-09-21 06:00:20 +00002595 SDOperand Op = N0.getOperand(0);
Chris Lattner22558872007-02-26 03:13:59 +00002596 unsigned OpBits = MVT::getSizeInBits(Op.getValueType());
2597 unsigned MidBits = MVT::getSizeInBits(N0.getValueType());
2598 unsigned DestBits = MVT::getSizeInBits(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002599 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002600
2601 if (OpBits == DestBits) {
2602 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2603 // bits, it is already ready.
2604 if (NumSignBits > DestBits-MidBits)
2605 return Op;
2606 } else if (OpBits < DestBits) {
2607 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2608 // bits, just sext from i32.
2609 if (NumSignBits > OpBits-MidBits)
2610 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2611 } else {
2612 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2613 // bits, just truncate to i32.
2614 if (NumSignBits > OpBits-MidBits)
2615 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002616 }
Chris Lattner22558872007-02-26 03:13:59 +00002617
2618 // fold (sext (truncate x)) -> (sextinreg x).
2619 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2620 N0.getValueType())) {
2621 if (Op.getValueType() < VT)
2622 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2623 else if (Op.getValueType() > VT)
2624 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2625 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2626 DAG.getValueType(N0.getValueType()));
2627 }
Chris Lattner6007b842006-09-21 06:00:20 +00002628 }
Chris Lattner310b5782006-05-06 23:06:26 +00002629
Evan Cheng110dec22005-12-14 02:19:23 +00002630 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002631 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002632 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002633 bool DoXform = true;
2634 SmallVector<SDNode*, 4> SetCCs;
2635 if (!N0.hasOneUse())
2636 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2637 if (DoXform) {
2638 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2639 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2640 LN0->getBasePtr(), LN0->getSrcValue(),
2641 LN0->getSrcValueOffset(),
2642 N0.getValueType(),
2643 LN0->isVolatile(),
2644 LN0->getAlignment());
2645 CombineTo(N, ExtLoad);
2646 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2647 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2648 // Extend SetCC uses if necessary.
2649 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2650 SDNode *SetCC = SetCCs[i];
2651 SmallVector<SDOperand, 4> Ops;
2652 for (unsigned j = 0; j != 2; ++j) {
2653 SDOperand SOp = SetCC->getOperand(j);
2654 if (SOp == Trunc)
2655 Ops.push_back(ExtLoad);
2656 else
2657 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2658 }
2659 Ops.push_back(SetCC->getOperand(2));
2660 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2661 &Ops[0], Ops.size()));
2662 }
2663 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2664 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002665 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002666
2667 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2668 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002669 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2670 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002671 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002672 MVT::ValueType EVT = LN0->getLoadedVT();
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002673 if (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
2674 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2675 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002676 LN0->getSrcValueOffset(), EVT,
2677 LN0->isVolatile(),
2678 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002679 CombineTo(N, ExtLoad);
2680 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2681 ExtLoad.getValue(1));
2682 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2683 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002684 }
2685
Chris Lattner20a35c32007-04-11 05:32:27 +00002686 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2687 if (N0.getOpcode() == ISD::SETCC) {
2688 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002689 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2690 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2691 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2692 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002693 }
2694
Nate Begeman83e75ec2005-09-06 04:43:02 +00002695 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002696}
2697
Nate Begeman83e75ec2005-09-06 04:43:02 +00002698SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002699 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002700 MVT::ValueType VT = N->getValueType(0);
2701
Nate Begeman1d4d4142005-09-01 00:19:25 +00002702 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002703 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002704 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002705 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002706 // fold (zext (aext x)) -> (zext x)
2707 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002708 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002709
Evan Chengc88138f2007-03-22 01:54:19 +00002710 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2711 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002712 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002713 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002714 if (NarrowLoad.Val) {
2715 if (NarrowLoad.Val != N0.Val)
2716 CombineTo(N0.Val, NarrowLoad);
2717 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2718 }
Evan Chengc88138f2007-03-22 01:54:19 +00002719 }
2720
Chris Lattner6007b842006-09-21 06:00:20 +00002721 // fold (zext (truncate x)) -> (and x, mask)
2722 if (N0.getOpcode() == ISD::TRUNCATE &&
2723 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2724 SDOperand Op = N0.getOperand(0);
2725 if (Op.getValueType() < VT) {
2726 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2727 } else if (Op.getValueType() > VT) {
2728 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2729 }
2730 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2731 }
2732
Chris Lattner111c2282006-09-21 06:14:31 +00002733 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2734 if (N0.getOpcode() == ISD::AND &&
2735 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2736 N0.getOperand(1).getOpcode() == ISD::Constant) {
2737 SDOperand X = N0.getOperand(0).getOperand(0);
2738 if (X.getValueType() < VT) {
2739 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2740 } else if (X.getValueType() > VT) {
2741 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2742 }
2743 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2744 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2745 }
2746
Evan Cheng110dec22005-12-14 02:19:23 +00002747 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002748 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002749 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002750 bool DoXform = true;
2751 SmallVector<SDNode*, 4> SetCCs;
2752 if (!N0.hasOneUse())
2753 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
2754 if (DoXform) {
2755 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2756 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2757 LN0->getBasePtr(), LN0->getSrcValue(),
2758 LN0->getSrcValueOffset(),
2759 N0.getValueType(),
2760 LN0->isVolatile(),
2761 LN0->getAlignment());
2762 CombineTo(N, ExtLoad);
2763 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2764 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2765 // Extend SetCC uses if necessary.
2766 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2767 SDNode *SetCC = SetCCs[i];
2768 SmallVector<SDOperand, 4> Ops;
2769 for (unsigned j = 0; j != 2; ++j) {
2770 SDOperand SOp = SetCC->getOperand(j);
2771 if (SOp == Trunc)
2772 Ops.push_back(ExtLoad);
2773 else
2774 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2775 }
2776 Ops.push_back(SetCC->getOperand(2));
2777 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2778 &Ops[0], Ops.size()));
2779 }
2780 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2781 }
Evan Cheng110dec22005-12-14 02:19:23 +00002782 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002783
2784 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2785 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002786 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2787 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002788 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002789 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002790 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2791 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002792 LN0->getSrcValueOffset(), EVT,
2793 LN0->isVolatile(),
2794 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002795 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002796 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2797 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002798 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002799 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002800
2801 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2802 if (N0.getOpcode() == ISD::SETCC) {
2803 SDOperand SCC =
2804 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2805 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00002806 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2807 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002808 }
2809
Nate Begeman83e75ec2005-09-06 04:43:02 +00002810 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002811}
2812
Chris Lattner5ffc0662006-05-05 05:58:59 +00002813SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2814 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002815 MVT::ValueType VT = N->getValueType(0);
2816
2817 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002818 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002819 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2820 // fold (aext (aext x)) -> (aext x)
2821 // fold (aext (zext x)) -> (zext x)
2822 // fold (aext (sext x)) -> (sext x)
2823 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2824 N0.getOpcode() == ISD::ZERO_EXTEND ||
2825 N0.getOpcode() == ISD::SIGN_EXTEND)
2826 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2827
Evan Chengc88138f2007-03-22 01:54:19 +00002828 // fold (aext (truncate (load x))) -> (aext (smaller load x))
2829 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
2830 if (N0.getOpcode() == ISD::TRUNCATE) {
2831 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002832 if (NarrowLoad.Val) {
2833 if (NarrowLoad.Val != N0.Val)
2834 CombineTo(N0.Val, NarrowLoad);
2835 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
2836 }
Evan Chengc88138f2007-03-22 01:54:19 +00002837 }
2838
Chris Lattner84750582006-09-20 06:29:17 +00002839 // fold (aext (truncate x))
2840 if (N0.getOpcode() == ISD::TRUNCATE) {
2841 SDOperand TruncOp = N0.getOperand(0);
2842 if (TruncOp.getValueType() == VT)
2843 return TruncOp; // x iff x size == zext size.
2844 if (TruncOp.getValueType() > VT)
2845 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2846 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2847 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002848
2849 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2850 if (N0.getOpcode() == ISD::AND &&
2851 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2852 N0.getOperand(1).getOpcode() == ISD::Constant) {
2853 SDOperand X = N0.getOperand(0).getOperand(0);
2854 if (X.getValueType() < VT) {
2855 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2856 } else if (X.getValueType() > VT) {
2857 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2858 }
2859 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2860 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2861 }
2862
Chris Lattner5ffc0662006-05-05 05:58:59 +00002863 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002864 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002865 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002866 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2867 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2868 LN0->getBasePtr(), LN0->getSrcValue(),
2869 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002870 N0.getValueType(),
2871 LN0->isVolatile(),
2872 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002873 CombineTo(N, ExtLoad);
2874 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2875 ExtLoad.getValue(1));
2876 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2877 }
2878
2879 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2880 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2881 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002882 if (N0.getOpcode() == ISD::LOAD &&
2883 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00002884 N0.hasOneUse()) {
2885 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002886 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002887 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
2888 LN0->getChain(), LN0->getBasePtr(),
2889 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002890 LN0->getSrcValueOffset(), EVT,
2891 LN0->isVolatile(),
2892 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002893 CombineTo(N, ExtLoad);
2894 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2895 ExtLoad.getValue(1));
2896 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2897 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002898
2899 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2900 if (N0.getOpcode() == ISD::SETCC) {
2901 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002902 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2903 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00002904 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00002905 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00002906 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002907 }
2908
Chris Lattner5ffc0662006-05-05 05:58:59 +00002909 return SDOperand();
2910}
2911
Chris Lattner2b4c2792007-10-13 06:35:54 +00002912/// GetDemandedBits - See if the specified operand can be simplified with the
2913/// knowledge that only the bits specified by Mask are used. If so, return the
2914/// simpler operand, otherwise return a null SDOperand.
2915SDOperand DAGCombiner::GetDemandedBits(SDOperand V, uint64_t Mask) {
2916 switch (V.getOpcode()) {
2917 default: break;
2918 case ISD::OR:
2919 case ISD::XOR:
2920 // If the LHS or RHS don't contribute bits to the or, drop them.
2921 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
2922 return V.getOperand(1);
2923 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
2924 return V.getOperand(0);
2925 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00002926 case ISD::SRL:
2927 // Only look at single-use SRLs.
2928 if (!V.Val->hasOneUse())
2929 break;
2930 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
2931 // See if we can recursively simplify the LHS.
2932 unsigned Amt = RHSC->getValue();
2933 Mask = (Mask << Amt) & MVT::getIntVTBitMask(V.getValueType());
2934 SDOperand SimplifyLHS = GetDemandedBits(V.getOperand(0), Mask);
2935 if (SimplifyLHS.Val) {
2936 return DAG.getNode(ISD::SRL, V.getValueType(),
2937 SimplifyLHS, V.getOperand(1));
2938 }
2939 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00002940 }
2941 return SDOperand();
2942}
2943
Evan Chengc88138f2007-03-22 01:54:19 +00002944/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
2945/// bits and then truncated to a narrower type and where N is a multiple
2946/// of number of bits of the narrower type, transform it to a narrower load
2947/// from address + N / num of bits of new type. If the result is to be
2948/// extended, also fold the extension to form a extending load.
2949SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
2950 unsigned Opc = N->getOpcode();
2951 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
2952 SDOperand N0 = N->getOperand(0);
2953 MVT::ValueType VT = N->getValueType(0);
2954 MVT::ValueType EVT = N->getValueType(0);
2955
Evan Chenge177e302007-03-23 22:13:36 +00002956 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
2957 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00002958 if (Opc == ISD::SIGN_EXTEND_INREG) {
2959 ExtType = ISD::SEXTLOAD;
2960 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00002961 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
2962 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00002963 }
2964
2965 unsigned EVTBits = MVT::getSizeInBits(EVT);
2966 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00002967 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00002968 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
2969 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2970 ShAmt = N01->getValue();
2971 // Is the shift amount a multiple of size of VT?
2972 if ((ShAmt & (EVTBits-1)) == 0) {
2973 N0 = N0.getOperand(0);
2974 if (MVT::getSizeInBits(N0.getValueType()) <= EVTBits)
2975 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00002976 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00002977 }
2978 }
2979 }
2980
2981 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
2982 // Do not allow folding to i1 here. i1 is implicitly stored in memory in
2983 // zero extended form: by shrinking the load, we lose track of the fact
2984 // that it is already zero extended.
2985 // FIXME: This should be reevaluated.
2986 VT != MVT::i1) {
2987 assert(MVT::getSizeInBits(N0.getValueType()) > EVTBits &&
2988 "Cannot truncate to larger type!");
2989 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2990 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00002991 // For big endian targets, we need to adjust the offset to the pointer to
2992 // load the correct bytes.
2993 if (!TLI.isLittleEndian())
2994 ShAmt = MVT::getSizeInBits(N0.getValueType()) - ShAmt - EVTBits;
2995 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00002996 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Evan Chengc88138f2007-03-22 01:54:19 +00002997 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
2998 DAG.getConstant(PtrOff, PtrType));
2999 AddToWorkList(NewPtr.Val);
3000 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
3001 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003002 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Duncan Sandsdc846502007-10-28 12:59:45 +00003003 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003004 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003005 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00003006 LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003007 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003008 if (CombineSRL) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003009 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
Evan Chengb37b80c2007-03-23 20:55:21 +00003010 CombineTo(N->getOperand(0).Val, Load);
3011 } else
3012 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003013 if (ShAmt) {
3014 if (Opc == ISD::SIGN_EXTEND_INREG)
3015 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3016 else
3017 return DAG.getNode(Opc, VT, Load);
3018 }
Evan Chengc88138f2007-03-22 01:54:19 +00003019 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3020 }
3021
3022 return SDOperand();
3023}
3024
Chris Lattner5ffc0662006-05-05 05:58:59 +00003025
Nate Begeman83e75ec2005-09-06 04:43:02 +00003026SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003027 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003028 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003029 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003030 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00003031 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003032
Nate Begeman1d4d4142005-09-01 00:19:25 +00003033 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003034 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003035 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003036
Chris Lattner541a24f2006-05-06 22:43:44 +00003037 // If the input is already sign extended, just drop the extension.
Dan Gohmanea859be2007-06-22 14:59:07 +00003038 if (DAG.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003039 return N0;
3040
Nate Begeman646d7e22005-09-02 21:18:40 +00003041 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3042 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3043 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003044 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003045 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003046
Chris Lattner95a5e052007-04-17 19:03:21 +00003047 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohmanea859be2007-06-22 14:59:07 +00003048 if (DAG.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00003049 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003050
Chris Lattner95a5e052007-04-17 19:03:21 +00003051 // fold operands of sext_in_reg based on knowledge that the top bits are not
3052 // demanded.
3053 if (SimplifyDemandedBits(SDOperand(N, 0)))
3054 return SDOperand(N, 0);
3055
Evan Chengc88138f2007-03-22 01:54:19 +00003056 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3057 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
3058 SDOperand NarrowLoad = ReduceLoadWidth(N);
3059 if (NarrowLoad.Val)
3060 return NarrowLoad;
3061
Chris Lattner4b37e872006-05-08 21:18:59 +00003062 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3063 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3064 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3065 if (N0.getOpcode() == ISD::SRL) {
3066 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
3067 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
3068 // We can turn this into an SRA iff the input to the SRL is already sign
3069 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003070 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Chris Lattner4b37e872006-05-08 21:18:59 +00003071 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
3072 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3073 }
3074 }
Evan Chengc88138f2007-03-22 01:54:19 +00003075
Nate Begemanded49632005-10-13 03:11:28 +00003076 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00003077 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003078 ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng2e49f092006-10-11 07:10:22 +00003079 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003080 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003081 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3082 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3083 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003084 LN0->getSrcValueOffset(), EVT,
3085 LN0->isVolatile(),
3086 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003087 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003088 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003089 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003090 }
3091 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00003092 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
3093 N0.hasOneUse() &&
Evan Cheng2e49f092006-10-11 07:10:22 +00003094 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003095 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003096 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3097 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3098 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003099 LN0->getSrcValueOffset(), EVT,
3100 LN0->isVolatile(),
3101 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003102 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003103 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003104 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003105 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003106 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003107}
3108
Nate Begeman83e75ec2005-09-06 04:43:02 +00003109SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003110 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003111 MVT::ValueType VT = N->getValueType(0);
3112
3113 // noop truncate
3114 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003115 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003116 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003117 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003118 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003119 // fold (truncate (truncate x)) -> (truncate x)
3120 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003121 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003122 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003123 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3124 N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003125 if (N0.getOperand(0).getValueType() < VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003126 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003127 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003128 else if (N0.getOperand(0).getValueType() > VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003129 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003130 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003131 else
3132 // if the source and dest are the same type, we can drop both the extend
3133 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003134 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003135 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003136
Chris Lattner2b4c2792007-10-13 06:35:54 +00003137 // See if we can simplify the input to this truncate through knowledge that
3138 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3139 // -> trunc y
3140 SDOperand Shorter = GetDemandedBits(N0, MVT::getIntVTBitMask(VT));
3141 if (Shorter.Val)
3142 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3143
Nate Begeman3df4d522005-10-12 20:40:40 +00003144 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003145 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003146 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003147}
3148
Chris Lattner94683772005-12-23 05:30:37 +00003149SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3150 SDOperand N0 = N->getOperand(0);
3151 MVT::ValueType VT = N->getValueType(0);
3152
Dan Gohman7f321562007-06-25 16:23:39 +00003153 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3154 // Only do this before legalize, since afterward the target may be depending
3155 // on the bitconvert.
3156 // First check to see if this is all constant.
3157 if (!AfterLegalize &&
3158 N0.getOpcode() == ISD::BUILD_VECTOR && N0.Val->hasOneUse() &&
3159 MVT::isVector(VT)) {
3160 bool isSimple = true;
3161 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3162 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3163 N0.getOperand(i).getOpcode() != ISD::Constant &&
3164 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3165 isSimple = false;
3166 break;
3167 }
3168
3169 MVT::ValueType DestEltVT = MVT::getVectorElementType(N->getValueType(0));
3170 assert(!MVT::isVector(DestEltVT) &&
3171 "Element type of vector ValueType must not be vector!");
3172 if (isSimple) {
3173 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.Val, DestEltVT);
3174 }
3175 }
3176
Chris Lattner94683772005-12-23 05:30:37 +00003177 // If the input is a constant, let getNode() fold it.
3178 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
3179 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3180 if (Res.Val != N) return Res;
3181 }
3182
Chris Lattnerc8547d82005-12-23 05:37:50 +00003183 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3184 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003185
Chris Lattner57104102005-12-23 05:44:41 +00003186 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003187 // If the resultant load doesn't need a higher alignment than the original!
3188 if (ISD::isNormalLoad(N0.Val) && N0.hasOneUse() &&
Evan Cheng59d5b682007-05-07 21:27:48 +00003189 TLI.isOperationLegal(ISD::LOAD, VT)) {
Evan Cheng466685d2006-10-09 20:57:25 +00003190 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00003191 unsigned Align = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00003192 getABITypeAlignment(MVT::getTypeForValueType(VT));
Evan Cheng59d5b682007-05-07 21:27:48 +00003193 unsigned OrigAlign = LN0->getAlignment();
3194 if (Align <= OrigAlign) {
3195 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
3196 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003197 LN0->isVolatile(), Align);
Evan Cheng59d5b682007-05-07 21:27:48 +00003198 AddToWorkList(N);
3199 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
3200 Load.getValue(1));
3201 return Load;
3202 }
Chris Lattner57104102005-12-23 05:44:41 +00003203 }
3204
Chris Lattner94683772005-12-23 05:30:37 +00003205 return SDOperand();
3206}
3207
Dan Gohman7f321562007-06-25 16:23:39 +00003208/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003209/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3210/// destination element value type.
3211SDOperand DAGCombiner::
Dan Gohman7f321562007-06-25 16:23:39 +00003212ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003213 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
3214
3215 // If this is already the right type, we're done.
3216 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
3217
3218 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
3219 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
3220
3221 // If this is a conversion of N elements of one type to N elements of another
3222 // type, convert each element. This handles FP<->INT cases.
3223 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003224 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003225 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003226 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00003227 AddToWorkList(Ops.back().Val);
3228 }
Dan Gohman7f321562007-06-25 16:23:39 +00003229 MVT::ValueType VT =
3230 MVT::getVectorType(DstEltVT,
3231 MVT::getVectorNumElements(BV->getValueType(0)));
3232 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003233 }
3234
3235 // Otherwise, we're growing or shrinking the elements. To avoid having to
3236 // handle annoying details of growing/shrinking FP values, we convert them to
3237 // int first.
3238 if (MVT::isFloatingPoint(SrcEltVT)) {
3239 // Convert the input float vector to a int vector where the elements are the
3240 // same sizes.
3241 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
3242 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003243 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003244 SrcEltVT = IntVT;
3245 }
3246
3247 // Now we know the input is an integer vector. If the output is a FP type,
3248 // convert to integer first, then to FP of the right size.
3249 if (MVT::isFloatingPoint(DstEltVT)) {
3250 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
3251 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003252 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003253
3254 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003255 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003256 }
3257
3258 // Okay, we know the src/dst types are both integers of differing types.
3259 // Handling growing first.
3260 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
3261 if (SrcBitSize < DstBitSize) {
3262 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3263
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003264 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003265 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003266 i += NumInputsPerOutput) {
3267 bool isLE = TLI.isLittleEndian();
3268 uint64_t NewBits = 0;
3269 bool EltIsUndef = true;
3270 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3271 // Shift the previously computed bits over.
3272 NewBits <<= SrcBitSize;
3273 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
3274 if (Op.getOpcode() == ISD::UNDEF) continue;
3275 EltIsUndef = false;
3276
3277 NewBits |= cast<ConstantSDNode>(Op)->getValue();
3278 }
3279
3280 if (EltIsUndef)
3281 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3282 else
3283 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3284 }
3285
Dan Gohman7f321562007-06-25 16:23:39 +00003286 MVT::ValueType VT = MVT::getVectorType(DstEltVT,
3287 Ops.size());
3288 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003289 }
3290
3291 // Finally, this must be the case where we are shrinking elements: each input
3292 // turns into multiple outputs.
3293 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003294 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003295 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003296 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3297 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3298 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3299 continue;
3300 }
3301 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
3302
3303 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
3304 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
3305 OpVal >>= DstBitSize;
3306 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
3307 }
3308
3309 // For big endian targets, swap the order of the pieces of each element.
3310 if (!TLI.isLittleEndian())
3311 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3312 }
Dan Gohman7f321562007-06-25 16:23:39 +00003313 MVT::ValueType VT = MVT::getVectorType(DstEltVT, Ops.size());
3314 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003315}
3316
3317
3318
Chris Lattner01b3d732005-09-28 22:28:18 +00003319SDOperand DAGCombiner::visitFADD(SDNode *N) {
3320 SDOperand N0 = N->getOperand(0);
3321 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003322 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3323 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003324 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003325
Dan Gohman7f321562007-06-25 16:23:39 +00003326 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003327 if (MVT::isVector(VT)) {
3328 SDOperand FoldedVOp = SimplifyVBinOp(N);
3329 if (FoldedVOp.Val) return FoldedVOp;
3330 }
Dan Gohman7f321562007-06-25 16:23:39 +00003331
Nate Begemana0e221d2005-10-18 00:28:13 +00003332 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003333 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003334 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003335 // canonicalize constant to RHS
3336 if (N0CFP && !N1CFP)
3337 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003338 // fold (A + (-B)) -> A-B
Chris Lattner29446522007-05-14 22:04:50 +00003339 if (isNegatibleForFree(N1) == 2)
3340 return DAG.getNode(ISD::FSUB, VT, N0, GetNegatedExpression(N1, DAG));
Chris Lattner01b3d732005-09-28 22:28:18 +00003341 // fold ((-A) + B) -> B-A
Chris Lattner29446522007-05-14 22:04:50 +00003342 if (isNegatibleForFree(N0) == 2)
3343 return DAG.getNode(ISD::FSUB, VT, N1, GetNegatedExpression(N0, DAG));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003344
3345 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3346 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
3347 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3348 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3349 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3350
Chris Lattner01b3d732005-09-28 22:28:18 +00003351 return SDOperand();
3352}
3353
3354SDOperand DAGCombiner::visitFSUB(SDNode *N) {
3355 SDOperand N0 = N->getOperand(0);
3356 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003357 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3358 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003359 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003360
Dan Gohman7f321562007-06-25 16:23:39 +00003361 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003362 if (MVT::isVector(VT)) {
3363 SDOperand FoldedVOp = SimplifyVBinOp(N);
3364 if (FoldedVOp.Val) return FoldedVOp;
3365 }
Dan Gohman7f321562007-06-25 16:23:39 +00003366
Nate Begemana0e221d2005-10-18 00:28:13 +00003367 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003368 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003369 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003370 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003371 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Dan Gohman23ff1822007-07-02 15:48:56 +00003372 if (isNegatibleForFree(N1))
3373 return GetNegatedExpression(N1, DAG);
3374 return DAG.getNode(ISD::FNEG, VT, N1);
3375 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003376 // fold (A-(-B)) -> A+B
Chris Lattner29446522007-05-14 22:04:50 +00003377 if (isNegatibleForFree(N1))
3378 return DAG.getNode(ISD::FADD, VT, N0, GetNegatedExpression(N1, DAG));
3379
Chris Lattner01b3d732005-09-28 22:28:18 +00003380 return SDOperand();
3381}
3382
3383SDOperand DAGCombiner::visitFMUL(SDNode *N) {
3384 SDOperand N0 = N->getOperand(0);
3385 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003386 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3387 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003388 MVT::ValueType VT = N->getValueType(0);
3389
Dan Gohman7f321562007-06-25 16:23:39 +00003390 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003391 if (MVT::isVector(VT)) {
3392 SDOperand FoldedVOp = SimplifyVBinOp(N);
3393 if (FoldedVOp.Val) return FoldedVOp;
3394 }
Dan Gohman7f321562007-06-25 16:23:39 +00003395
Nate Begeman11af4ea2005-10-17 20:40:11 +00003396 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003397 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003398 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003399 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003400 if (N0CFP && !N1CFP)
3401 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003402 // fold (fmul X, 2.0) -> (fadd X, X)
3403 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3404 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003405 // fold (fmul X, -1.0) -> (fneg X)
3406 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3407 return DAG.getNode(ISD::FNEG, VT, N0);
3408
3409 // -X * -Y -> X*Y
3410 if (char LHSNeg = isNegatibleForFree(N0)) {
3411 if (char RHSNeg = isNegatibleForFree(N1)) {
3412 // Both can be negated for free, check to see if at least one is cheaper
3413 // negated.
3414 if (LHSNeg == 2 || RHSNeg == 2)
3415 return DAG.getNode(ISD::FMUL, VT, GetNegatedExpression(N0, DAG),
3416 GetNegatedExpression(N1, DAG));
3417 }
3418 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003419
3420 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3421 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
3422 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3423 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3424 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3425
Chris Lattner01b3d732005-09-28 22:28:18 +00003426 return SDOperand();
3427}
3428
3429SDOperand DAGCombiner::visitFDIV(SDNode *N) {
3430 SDOperand N0 = N->getOperand(0);
3431 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003432 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3433 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003434 MVT::ValueType VT = N->getValueType(0);
3435
Dan Gohman7f321562007-06-25 16:23:39 +00003436 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003437 if (MVT::isVector(VT)) {
3438 SDOperand FoldedVOp = SimplifyVBinOp(N);
3439 if (FoldedVOp.Val) return FoldedVOp;
3440 }
Dan Gohman7f321562007-06-25 16:23:39 +00003441
Nate Begemana148d982006-01-18 22:35:16 +00003442 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003443 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003444 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003445
3446
3447 // -X / -Y -> X*Y
3448 if (char LHSNeg = isNegatibleForFree(N0)) {
3449 if (char RHSNeg = isNegatibleForFree(N1)) {
3450 // Both can be negated for free, check to see if at least one is cheaper
3451 // negated.
3452 if (LHSNeg == 2 || RHSNeg == 2)
3453 return DAG.getNode(ISD::FDIV, VT, GetNegatedExpression(N0, DAG),
3454 GetNegatedExpression(N1, DAG));
3455 }
3456 }
3457
Chris Lattner01b3d732005-09-28 22:28:18 +00003458 return SDOperand();
3459}
3460
3461SDOperand DAGCombiner::visitFREM(SDNode *N) {
3462 SDOperand N0 = N->getOperand(0);
3463 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003464 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3465 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003466 MVT::ValueType VT = N->getValueType(0);
3467
Nate Begemana148d982006-01-18 22:35:16 +00003468 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003469 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003470 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003471
Chris Lattner01b3d732005-09-28 22:28:18 +00003472 return SDOperand();
3473}
3474
Chris Lattner12d83032006-03-05 05:30:57 +00003475SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3476 SDOperand N0 = N->getOperand(0);
3477 SDOperand N1 = N->getOperand(1);
3478 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3479 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3480 MVT::ValueType VT = N->getValueType(0);
3481
Dale Johannesendb44bf82007-10-16 23:38:29 +00003482 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003483 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3484
3485 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003486 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003487 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3488 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003489 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003490 return DAG.getNode(ISD::FABS, VT, N0);
3491 else
3492 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3493 }
3494
3495 // copysign(fabs(x), y) -> copysign(x, y)
3496 // copysign(fneg(x), y) -> copysign(x, y)
3497 // copysign(copysign(x,z), y) -> copysign(x, y)
3498 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3499 N0.getOpcode() == ISD::FCOPYSIGN)
3500 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3501
3502 // copysign(x, abs(y)) -> abs(x)
3503 if (N1.getOpcode() == ISD::FABS)
3504 return DAG.getNode(ISD::FABS, VT, N0);
3505
3506 // copysign(x, copysign(y,z)) -> copysign(x, z)
3507 if (N1.getOpcode() == ISD::FCOPYSIGN)
3508 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3509
3510 // copysign(x, fp_extend(y)) -> copysign(x, y)
3511 // copysign(x, fp_round(y)) -> copysign(x, y)
3512 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3513 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3514
3515 return SDOperand();
3516}
3517
3518
Chris Lattner01b3d732005-09-28 22:28:18 +00003519
Nate Begeman83e75ec2005-09-06 04:43:02 +00003520SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003521 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003522 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003523 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003524
3525 // fold (sint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003526 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003527 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003528 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003529}
3530
Nate Begeman83e75ec2005-09-06 04:43:02 +00003531SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003532 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003533 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003534 MVT::ValueType VT = N->getValueType(0);
3535
Nate Begeman1d4d4142005-09-01 00:19:25 +00003536 // fold (uint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003537 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003538 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003539 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003540}
3541
Nate Begeman83e75ec2005-09-06 04:43:02 +00003542SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003543 SDOperand N0 = N->getOperand(0);
3544 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3545 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003546
3547 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003548 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003549 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003550 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003551}
3552
Nate Begeman83e75ec2005-09-06 04:43:02 +00003553SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003554 SDOperand N0 = N->getOperand(0);
3555 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3556 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003557
3558 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00003559 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003560 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003561 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003562}
3563
Nate Begeman83e75ec2005-09-06 04:43:02 +00003564SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003565 SDOperand N0 = N->getOperand(0);
3566 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3567 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003568
3569 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003570 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003571 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00003572
3573 // fold (fp_round (fp_extend x)) -> x
3574 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3575 return N0.getOperand(0);
3576
3577 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3578 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
3579 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
3580 AddToWorkList(Tmp.Val);
3581 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3582 }
3583
Nate Begeman83e75ec2005-09-06 04:43:02 +00003584 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003585}
3586
Nate Begeman83e75ec2005-09-06 04:43:02 +00003587SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003588 SDOperand N0 = N->getOperand(0);
3589 MVT::ValueType VT = N->getValueType(0);
3590 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003591 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003592
Nate Begeman1d4d4142005-09-01 00:19:25 +00003593 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003594 if (N0CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003595 SDOperand Round = DAG.getConstantFP(N0CFP->getValueAPF(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003596 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003597 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003598 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003599}
3600
Nate Begeman83e75ec2005-09-06 04:43:02 +00003601SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003602 SDOperand N0 = N->getOperand(0);
3603 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3604 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003605
3606 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003607 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003608 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00003609
3610 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
Dale Johannesenb6210fc2007-10-19 20:29:00 +00003611 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003612 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003613 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3614 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3615 LN0->getBasePtr(), LN0->getSrcValue(),
3616 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003617 N0.getValueType(),
3618 LN0->isVolatile(),
3619 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003620 CombineTo(N, ExtLoad);
3621 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
3622 ExtLoad.getValue(1));
3623 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3624 }
3625
3626
Nate Begeman83e75ec2005-09-06 04:43:02 +00003627 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003628}
3629
Nate Begeman83e75ec2005-09-06 04:43:02 +00003630SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003631 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003632
Dan Gohman23ff1822007-07-02 15:48:56 +00003633 if (isNegatibleForFree(N0))
3634 return GetNegatedExpression(N0, DAG);
3635
Nate Begeman83e75ec2005-09-06 04:43:02 +00003636 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003637}
3638
Nate Begeman83e75ec2005-09-06 04:43:02 +00003639SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003640 SDOperand N0 = N->getOperand(0);
3641 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3642 MVT::ValueType VT = N->getValueType(0);
3643
Nate Begeman1d4d4142005-09-01 00:19:25 +00003644 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003645 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003646 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003647 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003648 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003649 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003650 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003651 // fold (fabs (fcopysign x, y)) -> (fabs x)
3652 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
3653 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
3654
Nate Begeman83e75ec2005-09-06 04:43:02 +00003655 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003656}
3657
Nate Begeman44728a72005-09-19 22:34:01 +00003658SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
3659 SDOperand Chain = N->getOperand(0);
3660 SDOperand N1 = N->getOperand(1);
3661 SDOperand N2 = N->getOperand(2);
3662 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3663
3664 // never taken branch, fold to chain
3665 if (N1C && N1C->isNullValue())
3666 return Chain;
3667 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00003668 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00003669 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003670 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
3671 // on the target.
3672 if (N1.getOpcode() == ISD::SETCC &&
3673 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
3674 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
3675 N1.getOperand(0), N1.getOperand(1), N2);
3676 }
Nate Begeman44728a72005-09-19 22:34:01 +00003677 return SDOperand();
3678}
3679
Chris Lattner3ea0b472005-10-05 06:47:48 +00003680// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
3681//
Nate Begeman44728a72005-09-19 22:34:01 +00003682SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00003683 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
3684 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
3685
3686 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00003687 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003688 if (Simp.Val) AddToWorkList(Simp.Val);
3689
Nate Begemane17daeb2005-10-05 21:43:42 +00003690 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
3691
3692 // fold br_cc true, dest -> br dest (unconditional branch)
3693 if (SCCC && SCCC->getValue())
3694 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
3695 N->getOperand(4));
3696 // fold br_cc false, dest -> unconditional fall through
3697 if (SCCC && SCCC->isNullValue())
3698 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00003699
Nate Begemane17daeb2005-10-05 21:43:42 +00003700 // fold to a simpler setcc
3701 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
3702 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
3703 Simp.getOperand(2), Simp.getOperand(0),
3704 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00003705 return SDOperand();
3706}
3707
Chris Lattner448f2192006-11-11 00:39:41 +00003708
3709/// CombineToPreIndexedLoadStore - Try turning a load / store and a
3710/// pre-indexed load / store when the base pointer is a add or subtract
3711/// and it has other uses besides the load / store. After the
3712/// transformation, the new indexed load / store has effectively folded
3713/// the add / subtract in and all of its other uses are redirected to the
3714/// new load / store.
3715bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
3716 if (!AfterLegalize)
3717 return false;
3718
3719 bool isLoad = true;
3720 SDOperand Ptr;
3721 MVT::ValueType VT;
3722 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003723 if (LD->getAddressingMode() != ISD::UNINDEXED)
3724 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003725 VT = LD->getLoadedVT();
Evan Cheng83060c52007-03-07 08:07:03 +00003726 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00003727 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
3728 return false;
3729 Ptr = LD->getBasePtr();
3730 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003731 if (ST->getAddressingMode() != ISD::UNINDEXED)
3732 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003733 VT = ST->getStoredVT();
3734 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
3735 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
3736 return false;
3737 Ptr = ST->getBasePtr();
3738 isLoad = false;
3739 } else
3740 return false;
3741
Chris Lattner9f1794e2006-11-11 00:56:29 +00003742 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
3743 // out. There is no reason to make this a preinc/predec.
3744 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
3745 Ptr.Val->hasOneUse())
3746 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003747
Chris Lattner9f1794e2006-11-11 00:56:29 +00003748 // Ask the target to do addressing mode selection.
3749 SDOperand BasePtr;
3750 SDOperand Offset;
3751 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3752 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
3753 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00003754 // Don't create a indexed load / store with zero offset.
3755 if (isa<ConstantSDNode>(Offset) &&
3756 cast<ConstantSDNode>(Offset)->getValue() == 0)
3757 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00003758
Chris Lattner41e53fd2006-11-11 01:00:15 +00003759 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00003760 // 1) The new base ptr is a frame index.
3761 // 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 +00003762 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00003763 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00003764 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00003765 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00003766
Chris Lattner41e53fd2006-11-11 01:00:15 +00003767 // Check #1. Preinc'ing a frame index would require copying the stack pointer
3768 // (plus the implicit offset) to a register to preinc anyway.
3769 if (isa<FrameIndexSDNode>(BasePtr))
3770 return false;
3771
3772 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003773 if (!isLoad) {
3774 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Chengc843abe2007-05-24 02:35:39 +00003775 if (Val == BasePtr || BasePtr.Val->isPredecessor(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00003776 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003777 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003778
Evan Chengc843abe2007-05-24 02:35:39 +00003779 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003780 bool RealUse = false;
3781 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3782 E = Ptr.Val->use_end(); I != E; ++I) {
3783 SDNode *Use = *I;
3784 if (Use == N)
3785 continue;
3786 if (Use->isPredecessor(N))
3787 return false;
3788
3789 if (!((Use->getOpcode() == ISD::LOAD &&
3790 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
3791 (Use->getOpcode() == ISD::STORE) &&
3792 cast<StoreSDNode>(Use)->getBasePtr() == Ptr))
3793 RealUse = true;
3794 }
3795 if (!RealUse)
3796 return false;
3797
3798 SDOperand Result;
3799 if (isLoad)
3800 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
3801 else
3802 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
3803 ++PreIndexedNodes;
3804 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00003805 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00003806 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
3807 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00003808 std::vector<SDNode*> NowDead;
3809 if (isLoad) {
3810 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattner01d029b2007-10-15 06:10:22 +00003811 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003812 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattner01d029b2007-10-15 06:10:22 +00003813 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003814 } else {
3815 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattner01d029b2007-10-15 06:10:22 +00003816 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003817 }
3818
3819 // Nodes can end up on the worklist more than once. Make sure we do
3820 // not process a node that has been replaced.
3821 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3822 removeFromWorkList(NowDead[i]);
3823 // Finally, since the node is now dead, remove it from the graph.
3824 DAG.DeleteNode(N);
3825
3826 // Replace the uses of Ptr with uses of the updated base value.
3827 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattner01d029b2007-10-15 06:10:22 +00003828 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003829 removeFromWorkList(Ptr.Val);
3830 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3831 removeFromWorkList(NowDead[i]);
3832 DAG.DeleteNode(Ptr.Val);
3833
3834 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00003835}
3836
3837/// CombineToPostIndexedLoadStore - Try combine a load / store with a
3838/// add / sub of the base pointer node into a post-indexed load / store.
3839/// The transformation folded the add / subtract into the new indexed
3840/// load / store effectively and all of its uses are redirected to the
3841/// new load / store.
3842bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
3843 if (!AfterLegalize)
3844 return false;
3845
3846 bool isLoad = true;
3847 SDOperand Ptr;
3848 MVT::ValueType VT;
3849 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003850 if (LD->getAddressingMode() != ISD::UNINDEXED)
3851 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003852 VT = LD->getLoadedVT();
3853 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
3854 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
3855 return false;
3856 Ptr = LD->getBasePtr();
3857 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003858 if (ST->getAddressingMode() != ISD::UNINDEXED)
3859 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003860 VT = ST->getStoredVT();
3861 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
3862 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
3863 return false;
3864 Ptr = ST->getBasePtr();
3865 isLoad = false;
3866 } else
3867 return false;
3868
Evan Chengcc470212006-11-16 00:08:20 +00003869 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00003870 return false;
3871
3872 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3873 E = Ptr.Val->use_end(); I != E; ++I) {
3874 SDNode *Op = *I;
3875 if (Op == N ||
3876 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
3877 continue;
3878
3879 SDOperand BasePtr;
3880 SDOperand Offset;
3881 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3882 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
3883 if (Ptr == Offset)
3884 std::swap(BasePtr, Offset);
3885 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00003886 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00003887 // Don't create a indexed load / store with zero offset.
3888 if (isa<ConstantSDNode>(Offset) &&
3889 cast<ConstantSDNode>(Offset)->getValue() == 0)
3890 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00003891
Chris Lattner9f1794e2006-11-11 00:56:29 +00003892 // Try turning it into a post-indexed load / store except when
3893 // 1) All uses are load / store ops that use it as base ptr.
3894 // 2) Op must be independent of N, i.e. Op is neither a predecessor
3895 // nor a successor of N. Otherwise, if Op is folded that would
3896 // create a cycle.
3897
3898 // Check for #1.
3899 bool TryNext = false;
3900 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
3901 EE = BasePtr.Val->use_end(); II != EE; ++II) {
3902 SDNode *Use = *II;
3903 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00003904 continue;
3905
Chris Lattner9f1794e2006-11-11 00:56:29 +00003906 // If all the uses are load / store addresses, then don't do the
3907 // transformation.
3908 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
3909 bool RealUse = false;
3910 for (SDNode::use_iterator III = Use->use_begin(),
3911 EEE = Use->use_end(); III != EEE; ++III) {
3912 SDNode *UseUse = *III;
3913 if (!((UseUse->getOpcode() == ISD::LOAD &&
3914 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
3915 (UseUse->getOpcode() == ISD::STORE) &&
3916 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use))
3917 RealUse = true;
3918 }
Chris Lattner448f2192006-11-11 00:39:41 +00003919
Chris Lattner9f1794e2006-11-11 00:56:29 +00003920 if (!RealUse) {
3921 TryNext = true;
3922 break;
Chris Lattner448f2192006-11-11 00:39:41 +00003923 }
3924 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003925 }
3926 if (TryNext)
3927 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00003928
Chris Lattner9f1794e2006-11-11 00:56:29 +00003929 // Check for #2
3930 if (!Op->isPredecessor(N) && !N->isPredecessor(Op)) {
3931 SDOperand Result = isLoad
3932 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
3933 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
3934 ++PostIndexedNodes;
3935 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00003936 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00003937 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
3938 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00003939 std::vector<SDNode*> NowDead;
3940 if (isLoad) {
3941 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattner01d029b2007-10-15 06:10:22 +00003942 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003943 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattner01d029b2007-10-15 06:10:22 +00003944 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003945 } else {
3946 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattner01d029b2007-10-15 06:10:22 +00003947 &NowDead);
Chris Lattner448f2192006-11-11 00:39:41 +00003948 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003949
3950 // Nodes can end up on the worklist more than once. Make sure we do
3951 // not process a node that has been replaced.
3952 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3953 removeFromWorkList(NowDead[i]);
3954 // Finally, since the node is now dead, remove it from the graph.
3955 DAG.DeleteNode(N);
3956
3957 // Replace the uses of Use with uses of the updated base value.
3958 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
3959 Result.getValue(isLoad ? 1 : 0),
Chris Lattner01d029b2007-10-15 06:10:22 +00003960 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003961 removeFromWorkList(Op);
3962 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3963 removeFromWorkList(NowDead[i]);
3964 DAG.DeleteNode(Op);
3965
3966 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00003967 }
3968 }
3969 }
3970 return false;
3971}
3972
3973
Chris Lattner01a22022005-10-10 22:04:48 +00003974SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00003975 LoadSDNode *LD = cast<LoadSDNode>(N);
3976 SDOperand Chain = LD->getChain();
3977 SDOperand Ptr = LD->getBasePtr();
Evan Cheng45a7ca92007-05-01 00:38:21 +00003978
3979 // If load is not volatile and there are no uses of the loaded value (and
3980 // the updated indexed value in case of indexed loads), change uses of the
3981 // chain value into uses of the chain input (i.e. delete the dead load).
3982 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00003983 if (N->getValueType(1) == MVT::Other) {
3984 // Unindexed loads.
3985 if (N->hasNUsesOfValue(0, 0))
3986 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
3987 } else {
3988 // Indexed loads.
3989 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
3990 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
3991 SDOperand Undef0 = DAG.getNode(ISD::UNDEF, N->getValueType(0));
3992 SDOperand Undef1 = DAG.getNode(ISD::UNDEF, N->getValueType(1));
3993 SDOperand To[] = { Undef0, Undef1, Chain };
3994 return CombineTo(N, To, 3);
Evan Cheng45a7ca92007-05-01 00:38:21 +00003995 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00003996 }
3997 }
Chris Lattner01a22022005-10-10 22:04:48 +00003998
3999 // If this load is directly stored, replace the load value with the stored
4000 // value.
4001 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004002 // TODO: Handle TRUNCSTORE/LOADEXT
4003 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004004 if (ISD::isNON_TRUNCStore(Chain.Val)) {
4005 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4006 if (PrevST->getBasePtr() == Ptr &&
4007 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004008 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004009 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004010 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004011
Jim Laskey7ca56af2006-10-11 13:47:09 +00004012 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004013 // Walk up chain skipping non-aliasing memory nodes.
4014 SDOperand BetterChain = FindBetterChain(N, Chain);
4015
Jim Laskey6ff23e52006-10-04 16:53:27 +00004016 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004017 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004018 SDOperand ReplLoad;
4019
Jim Laskey279f0532006-09-25 16:29:54 +00004020 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004021 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4022 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004023 LD->getSrcValue(), LD->getSrcValueOffset(),
4024 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004025 } else {
4026 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4027 LD->getValueType(0),
4028 BetterChain, Ptr, LD->getSrcValue(),
4029 LD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004030 LD->getLoadedVT(),
4031 LD->isVolatile(),
4032 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004033 }
Jim Laskey279f0532006-09-25 16:29:54 +00004034
Jim Laskey6ff23e52006-10-04 16:53:27 +00004035 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00004036 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
4037 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004038
Jim Laskey274062c2006-10-13 23:32:28 +00004039 // Replace uses with load result and token factor. Don't add users
4040 // to work list.
4041 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004042 }
4043 }
4044
Evan Cheng7fc033a2006-11-03 03:06:21 +00004045 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004046 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00004047 return SDOperand(N, 0);
4048
Chris Lattner01a22022005-10-10 22:04:48 +00004049 return SDOperand();
4050}
4051
Chris Lattner87514ca2005-10-10 22:31:19 +00004052SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004053 StoreSDNode *ST = cast<StoreSDNode>(N);
4054 SDOperand Chain = ST->getChain();
4055 SDOperand Value = ST->getValue();
4056 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004057
Evan Cheng59d5b682007-05-07 21:27:48 +00004058 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004059 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004060 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
4061 ST->getAddressingMode() == ISD::UNINDEXED) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004062 unsigned Align = ST->getAlignment();
4063 MVT::ValueType SVT = Value.getOperand(0).getValueType();
4064 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00004065 getABITypeAlignment(MVT::getTypeForValueType(SVT));
Evan Chengc2cd2b22007-05-07 21:36:06 +00004066 if (Align <= OrigAlign && TLI.isOperationLegal(ISD::STORE, SVT))
Evan Cheng59d5b682007-05-07 21:27:48 +00004067 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004068 ST->getSrcValueOffset(), ST->isVolatile(), Align);
Jim Laskey279f0532006-09-25 16:29:54 +00004069 }
4070
Nate Begeman2cbba892006-12-11 02:23:46 +00004071 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004072 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Evan Cheng25ece662006-12-11 17:25:19 +00004073 if (Value.getOpcode() != ISD::TargetConstantFP) {
4074 SDOperand Tmp;
Chris Lattner62be1a72006-12-12 04:16:14 +00004075 switch (CFP->getValueType(0)) {
4076 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004077 case MVT::f80: // We don't do this for these yet.
4078 case MVT::f128:
4079 case MVT::ppcf128:
4080 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004081 case MVT::f32:
4082 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004083 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
4084 convertToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004085 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004086 ST->getSrcValueOffset(), ST->isVolatile(),
4087 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004088 }
4089 break;
4090 case MVT::f64:
4091 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004092 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
4093 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004094 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004095 ST->getSrcValueOffset(), ST->isVolatile(),
4096 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004097 } else if (TLI.isTypeLegal(MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004098 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004099 // argument passing. Since this is so common, custom legalize the
4100 // 64-bit integer store into two 32-bit stores.
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004101 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
Chris Lattner62be1a72006-12-12 04:16:14 +00004102 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4103 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
4104 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
4105
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004106 int SVOffset = ST->getSrcValueOffset();
4107 unsigned Alignment = ST->getAlignment();
4108 bool isVolatile = ST->isVolatile();
4109
Chris Lattner62be1a72006-12-12 04:16:14 +00004110 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004111 ST->getSrcValueOffset(),
4112 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004113 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4114 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004115 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004116 Alignment = MinAlign(Alignment, 4U);
Chris Lattner62be1a72006-12-12 04:16:14 +00004117 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004118 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004119 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4120 }
4121 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004122 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004123 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004124 }
4125
Jim Laskey279f0532006-09-25 16:29:54 +00004126 if (CombinerAA) {
4127 // Walk up chain skipping non-aliasing memory nodes.
4128 SDOperand BetterChain = FindBetterChain(N, Chain);
4129
Jim Laskey6ff23e52006-10-04 16:53:27 +00004130 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004131 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004132 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004133 SDOperand ReplStore;
4134 if (ST->isTruncatingStore()) {
4135 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004136 ST->getSrcValue(), ST->getSrcValueOffset(), ST->getStoredVT(),
4137 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004138 } else {
4139 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004140 ST->getSrcValue(), ST->getSrcValueOffset(),
4141 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004142 }
4143
Jim Laskey279f0532006-09-25 16:29:54 +00004144 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00004145 SDOperand Token =
4146 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4147
4148 // Don't add users to work list.
4149 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004150 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004151 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004152
Evan Cheng33dbedc2006-11-05 09:31:14 +00004153 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004154 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00004155 return SDOperand(N, 0);
4156
Chris Lattner2b4c2792007-10-13 06:35:54 +00004157 // FIXME: is there such a think as a truncating indexed store?
4158 if (ST->isTruncatingStore() && ST->getAddressingMode() == ISD::UNINDEXED &&
4159 MVT::isInteger(Value.getValueType())) {
4160 // See if we can simplify the input to this truncstore with knowledge that
4161 // only the low bits are being used. For example:
4162 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
4163 SDOperand Shorter =
4164 GetDemandedBits(Value, MVT::getIntVTBitMask(ST->getStoredVT()));
4165 AddToWorkList(Value.Val);
4166 if (Shorter.Val)
4167 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
4168 ST->getSrcValueOffset(), ST->getStoredVT(),
4169 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004170
4171 // Otherwise, see if we can simplify the operation with
4172 // SimplifyDemandedBits, which only works if the value has a single use.
4173 if (SimplifyDemandedBits(Value, MVT::getIntVTBitMask(ST->getStoredVT())))
4174 return SDOperand(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004175 }
4176
Chris Lattner87514ca2005-10-10 22:31:19 +00004177 return SDOperand();
4178}
4179
Chris Lattnerca242442006-03-19 01:27:56 +00004180SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4181 SDOperand InVec = N->getOperand(0);
4182 SDOperand InVal = N->getOperand(1);
4183 SDOperand EltNo = N->getOperand(2);
4184
4185 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4186 // vector with the inserted element.
4187 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
4188 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004189 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004190 if (Elt < Ops.size())
4191 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004192 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4193 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004194 }
4195
4196 return SDOperand();
4197}
4198
Evan Cheng513da432007-10-06 08:19:55 +00004199SDOperand DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
4200 SDOperand InVec = N->getOperand(0);
4201 SDOperand EltNo = N->getOperand(1);
4202
4203 // (vextract (v4f32 s2v (f32 load $addr)), 0) -> (f32 load $addr)
4204 // (vextract (v4i32 bc (v4f32 s2v (f32 load $addr))), 0) -> (i32 load $addr)
4205 if (isa<ConstantSDNode>(EltNo)) {
4206 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
4207 bool NewLoad = false;
4208 if (Elt == 0) {
4209 MVT::ValueType VT = InVec.getValueType();
4210 MVT::ValueType EVT = MVT::getVectorElementType(VT);
4211 MVT::ValueType LVT = EVT;
4212 unsigned NumElts = MVT::getVectorNumElements(VT);
4213 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
4214 MVT::ValueType BCVT = InVec.getOperand(0).getValueType();
4215 if (NumElts != MVT::getVectorNumElements(BCVT))
4216 return SDOperand();
4217 InVec = InVec.getOperand(0);
4218 EVT = MVT::getVectorElementType(BCVT);
4219 NewLoad = true;
4220 }
4221 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4222 InVec.getOperand(0).getValueType() == EVT &&
4223 ISD::isNormalLoad(InVec.getOperand(0).Val) &&
4224 InVec.getOperand(0).hasOneUse()) {
4225 LoadSDNode *LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4226 unsigned Align = LN0->getAlignment();
4227 if (NewLoad) {
4228 // Check the resultant load doesn't need a higher alignment than the
4229 // original load.
4230 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
4231 getABITypeAlignment(MVT::getTypeForValueType(LVT));
4232 if (!TLI.isOperationLegal(ISD::LOAD, LVT) || NewAlign > Align)
4233 return SDOperand();
4234 Align = NewAlign;
4235 }
4236
4237 return DAG.getLoad(LVT, LN0->getChain(), LN0->getBasePtr(),
4238 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4239 LN0->isVolatile(), Align);
4240 }
4241 }
4242 }
4243 return SDOperand();
4244}
4245
4246
Dan Gohman7f321562007-06-25 16:23:39 +00004247SDOperand DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
4248 unsigned NumInScalars = N->getNumOperands();
4249 MVT::ValueType VT = N->getValueType(0);
4250 unsigned NumElts = MVT::getVectorNumElements(VT);
4251 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattnerca242442006-03-19 01:27:56 +00004252
Dan Gohman7f321562007-06-25 16:23:39 +00004253 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4254 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4255 // at most two distinct vectors, turn this into a shuffle node.
Chris Lattnerd7648c82006-03-28 20:28:38 +00004256 SDOperand VecIn1, VecIn2;
4257 for (unsigned i = 0; i != NumInScalars; ++i) {
4258 // Ignore undef inputs.
4259 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4260
Dan Gohman7f321562007-06-25 16:23:39 +00004261 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004262 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004263 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004264 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
4265 VecIn1 = VecIn2 = SDOperand(0, 0);
4266 break;
4267 }
4268
Dan Gohman7f321562007-06-25 16:23:39 +00004269 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004270 // we can't make a shuffle.
4271 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004272 if (ExtractedFromVec.getValueType() != VT) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004273 VecIn1 = VecIn2 = SDOperand(0, 0);
4274 break;
4275 }
4276
4277 // Otherwise, remember this. We allow up to two distinct input vectors.
4278 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4279 continue;
4280
4281 if (VecIn1.Val == 0) {
4282 VecIn1 = ExtractedFromVec;
4283 } else if (VecIn2.Val == 0) {
4284 VecIn2 = ExtractedFromVec;
4285 } else {
4286 // Too many inputs.
4287 VecIn1 = VecIn2 = SDOperand(0, 0);
4288 break;
4289 }
4290 }
4291
4292 // If everything is good, we can make a shuffle operation.
4293 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004294 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004295 for (unsigned i = 0; i != NumInScalars; ++i) {
4296 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004297 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004298 continue;
4299 }
4300
4301 SDOperand Extract = N->getOperand(i);
4302
4303 // If extracting from the first vector, just use the index directly.
4304 if (Extract.getOperand(0) == VecIn1) {
4305 BuildVecIndices.push_back(Extract.getOperand(1));
4306 continue;
4307 }
4308
4309 // Otherwise, use InIdx + VecSize
4310 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Evan Cheng597a3bd2007-01-20 10:10:26 +00004311 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars,
4312 TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004313 }
4314
4315 // Add count and size info.
Dan Gohman7f321562007-06-25 16:23:39 +00004316 MVT::ValueType BuildVecVT =
4317 MVT::getVectorType(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004318
Dan Gohman7f321562007-06-25 16:23:39 +00004319 // Return the new VECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004320 SDOperand Ops[5];
4321 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00004322 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004323 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004324 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004325 // Use an undef build_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00004326 std::vector<SDOperand> UnOps(NumInScalars,
4327 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004328 EltType));
4329 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004330 &UnOps[0], UnOps.size());
4331 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00004332 }
Dan Gohman7f321562007-06-25 16:23:39 +00004333 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004334 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004335 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004336 }
4337
4338 return SDOperand();
4339}
4340
Dan Gohman7f321562007-06-25 16:23:39 +00004341SDOperand DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
4342 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
4343 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
4344 // inputs come from at most two distinct vectors, turn this into a shuffle
4345 // node.
4346
4347 // If we only have one input vector, we don't need to do any concatenation.
4348 if (N->getNumOperands() == 1) {
4349 return N->getOperand(0);
4350 }
4351
4352 return SDOperand();
4353}
4354
Chris Lattner66445d32006-03-28 22:11:53 +00004355SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004356 SDOperand ShufMask = N->getOperand(2);
4357 unsigned NumElts = ShufMask.getNumOperands();
4358
4359 // If the shuffle mask is an identity operation on the LHS, return the LHS.
4360 bool isIdentity = true;
4361 for (unsigned i = 0; i != NumElts; ++i) {
4362 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4363 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
4364 isIdentity = false;
4365 break;
4366 }
4367 }
4368 if (isIdentity) return N->getOperand(0);
4369
4370 // If the shuffle mask is an identity operation on the RHS, return the RHS.
4371 isIdentity = true;
4372 for (unsigned i = 0; i != NumElts; ++i) {
4373 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4374 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
4375 isIdentity = false;
4376 break;
4377 }
4378 }
4379 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004380
4381 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
4382 // needed at all.
4383 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00004384 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004385 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00004386 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004387 for (unsigned i = 0; i != NumElts; ++i)
4388 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
4389 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
4390 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00004391 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004392 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004393 BaseIdx = Idx;
4394 } else {
4395 if (BaseIdx != Idx)
4396 isSplat = false;
4397 if (VecNum != V) {
4398 isUnary = false;
4399 break;
4400 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004401 }
4402 }
4403
4404 SDOperand N0 = N->getOperand(0);
4405 SDOperand N1 = N->getOperand(1);
4406 // Normalize unary shuffle so the RHS is undef.
4407 if (isUnary && VecNum == 1)
4408 std::swap(N0, N1);
4409
Evan Cheng917ec982006-07-21 08:25:53 +00004410 // If it is a splat, check if the argument vector is a build_vector with
4411 // all scalar elements the same.
4412 if (isSplat) {
4413 SDNode *V = N0.Val;
Evan Cheng917ec982006-07-21 08:25:53 +00004414
Dan Gohman7f321562007-06-25 16:23:39 +00004415 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004416 // not the number of vector elements, look through it. Be careful not to
4417 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004418 if (V->getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng59569222006-10-16 22:49:37 +00004419 SDOperand ConvInput = V->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004420 if (MVT::getVectorNumElements(ConvInput.getValueType()) == NumElts)
Evan Cheng59569222006-10-16 22:49:37 +00004421 V = ConvInput.Val;
4422 }
4423
Dan Gohman7f321562007-06-25 16:23:39 +00004424 if (V->getOpcode() == ISD::BUILD_VECTOR) {
4425 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00004426 if (NumElems > BaseIdx) {
4427 SDOperand Base;
4428 bool AllSame = true;
4429 for (unsigned i = 0; i != NumElems; ++i) {
4430 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4431 Base = V->getOperand(i);
4432 break;
4433 }
4434 }
4435 // Splat of <u, u, u, u>, return <u, u, u, u>
4436 if (!Base.Val)
4437 return N0;
4438 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00004439 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00004440 AllSame = false;
4441 break;
4442 }
4443 }
4444 // Splat of <x, x, x, x>, return <x, x, x, x>
4445 if (AllSame)
4446 return N0;
4447 }
4448 }
4449 }
4450
Evan Chenge7bec0d2006-07-20 22:44:41 +00004451 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4452 // into an undef.
4453 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004454 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4455 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004456 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004457 for (unsigned i = 0; i != NumElts; ++i) {
4458 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4459 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4460 MappedOps.push_back(ShufMask.getOperand(i));
4461 } else {
4462 unsigned NewIdx =
4463 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
4464 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
4465 }
4466 }
Dan Gohman7f321562007-06-25 16:23:39 +00004467 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004468 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004469 AddToWorkList(ShufMask.Val);
Dan Gohman7f321562007-06-25 16:23:39 +00004470 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
4471 N0,
4472 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
4473 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00004474 }
Dan Gohman7f321562007-06-25 16:23:39 +00004475
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004476 return SDOperand();
4477}
4478
Evan Cheng44f1f092006-04-20 08:56:16 +00004479/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00004480/// an AND to a vector_shuffle with the destination vector and a zero vector.
4481/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00004482/// vector_shuffle V, Zero, <0, 4, 2, 4>
4483SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
4484 SDOperand LHS = N->getOperand(0);
4485 SDOperand RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00004486 if (N->getOpcode() == ISD::AND) {
4487 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00004488 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004489 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Evan Cheng44f1f092006-04-20 08:56:16 +00004490 std::vector<SDOperand> IdxOps;
4491 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00004492 unsigned NumElts = NumOps;
4493 MVT::ValueType EVT = MVT::getVectorElementType(RHS.getValueType());
Evan Cheng44f1f092006-04-20 08:56:16 +00004494 for (unsigned i = 0; i != NumElts; ++i) {
4495 SDOperand Elt = RHS.getOperand(i);
4496 if (!isa<ConstantSDNode>(Elt))
4497 return SDOperand();
4498 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
4499 IdxOps.push_back(DAG.getConstant(i, EVT));
4500 else if (cast<ConstantSDNode>(Elt)->isNullValue())
4501 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
4502 else
4503 return SDOperand();
4504 }
4505
4506 // Let's see if the target supports this vector_shuffle.
4507 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
4508 return SDOperand();
4509
Dan Gohman7f321562007-06-25 16:23:39 +00004510 // Return the new VECTOR_SHUFFLE node.
4511 MVT::ValueType VT = MVT::getVectorType(EVT, NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00004512 std::vector<SDOperand> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004513 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00004514 Ops.push_back(LHS);
4515 AddToWorkList(LHS.Val);
4516 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00004517 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004518 &ZeroOps[0], ZeroOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004519 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004520 &IdxOps[0], IdxOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004521 SDOperand Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004522 &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004523 if (VT != LHS.getValueType()) {
4524 Result = DAG.getNode(ISD::BIT_CONVERT, LHS.getValueType(), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00004525 }
4526 return Result;
4527 }
4528 }
4529 return SDOperand();
4530}
4531
Dan Gohman7f321562007-06-25 16:23:39 +00004532/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
4533SDOperand DAGCombiner::SimplifyVBinOp(SDNode *N) {
4534 // After legalize, the target may be depending on adds and other
4535 // binary ops to provide legal ways to construct constants or other
4536 // things. Simplifying them may result in a loss of legality.
4537 if (AfterLegalize) return SDOperand();
4538
4539 MVT::ValueType VT = N->getValueType(0);
Dan Gohman05d92fe2007-07-13 20:03:40 +00004540 assert(MVT::isVector(VT) && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00004541
4542 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattneredab1b92006-04-02 03:25:57 +00004543 SDOperand LHS = N->getOperand(0);
4544 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00004545 SDOperand Shuffle = XformToShuffleWithZero(N);
4546 if (Shuffle.Val) return Shuffle;
4547
Dan Gohman7f321562007-06-25 16:23:39 +00004548 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00004549 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00004550 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
4551 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004552 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004553 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Chris Lattneredab1b92006-04-02 03:25:57 +00004554 SDOperand LHSOp = LHS.getOperand(i);
4555 SDOperand RHSOp = RHS.getOperand(i);
4556 // If these two elements can't be folded, bail out.
4557 if ((LHSOp.getOpcode() != ISD::UNDEF &&
4558 LHSOp.getOpcode() != ISD::Constant &&
4559 LHSOp.getOpcode() != ISD::ConstantFP) ||
4560 (RHSOp.getOpcode() != ISD::UNDEF &&
4561 RHSOp.getOpcode() != ISD::Constant &&
4562 RHSOp.getOpcode() != ISD::ConstantFP))
4563 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00004564 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00004565 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
4566 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00004567 if ((RHSOp.getOpcode() == ISD::Constant &&
4568 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
4569 (RHSOp.getOpcode() == ISD::ConstantFP &&
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00004570 cast<ConstantFPSDNode>(RHSOp.Val)->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00004571 break;
4572 }
Dan Gohman7f321562007-06-25 16:23:39 +00004573 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00004574 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00004575 assert((Ops.back().getOpcode() == ISD::UNDEF ||
4576 Ops.back().getOpcode() == ISD::Constant ||
4577 Ops.back().getOpcode() == ISD::ConstantFP) &&
4578 "Scalar binop didn't fold!");
4579 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004580
Dan Gohman7f321562007-06-25 16:23:39 +00004581 if (Ops.size() == LHS.getNumOperands()) {
4582 MVT::ValueType VT = LHS.getValueType();
4583 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004584 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004585 }
4586
4587 return SDOperand();
4588}
4589
Nate Begeman44728a72005-09-19 22:34:01 +00004590SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00004591 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
4592
4593 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
4594 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4595 // If we got a simplified select_cc node back from SimplifySelectCC, then
4596 // break it down into a new SETCC node, and a new SELECT node, and then return
4597 // the SELECT node, since we were called with a SELECT node.
4598 if (SCC.Val) {
4599 // Check to see if we got a select_cc back (to turn into setcc/select).
4600 // Otherwise, just return whatever node we got back, like fabs.
4601 if (SCC.getOpcode() == ISD::SELECT_CC) {
4602 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
4603 SCC.getOperand(0), SCC.getOperand(1),
4604 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00004605 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004606 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
4607 SCC.getOperand(3), SETCC);
4608 }
4609 return SCC;
4610 }
Nate Begeman44728a72005-09-19 22:34:01 +00004611 return SDOperand();
4612}
4613
Chris Lattner40c62d52005-10-18 06:04:22 +00004614/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
4615/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00004616/// select. Callers of this should assume that TheSelect is deleted if this
4617/// returns true. As such, they should return the appropriate thing (e.g. the
4618/// node) back to the top-level of the DAG combiner loop to avoid it being
4619/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00004620///
4621bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
4622 SDOperand RHS) {
4623
4624 // If this is a select from two identical things, try to pull the operation
4625 // through the select.
4626 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00004627 // If this is a load and the token chain is identical, replace the select
4628 // of two loads with a load through a select of the address to load from.
4629 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
4630 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00004631 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00004632 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00004633 LHS.getOperand(0) == RHS.getOperand(0)) {
4634 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
4635 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
4636
4637 // If this is an EXTLOAD, the VT's must match.
Evan Cheng2e49f092006-10-11 07:10:22 +00004638 if (LLD->getLoadedVT() == RLD->getLoadedVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004639 // FIXME: this conflates two src values, discarding one. This is not
4640 // the right thing to do, but nothing uses srcvalues now. When they do,
4641 // turn SrcValue into a list of locations.
4642 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004643 if (TheSelect->getOpcode() == ISD::SELECT) {
4644 // Check that the condition doesn't reach either load. If so, folding
4645 // this will induce a cycle into the DAG.
4646 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4647 !RLD->isPredecessor(TheSelect->getOperand(0).Val)) {
4648 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
4649 TheSelect->getOperand(0), LLD->getBasePtr(),
4650 RLD->getBasePtr());
4651 }
4652 } else {
4653 // Check that the condition doesn't reach either load. If so, folding
4654 // this will induce a cycle into the DAG.
4655 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4656 !RLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4657 !LLD->isPredecessor(TheSelect->getOperand(1).Val) &&
4658 !RLD->isPredecessor(TheSelect->getOperand(1).Val)) {
4659 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00004660 TheSelect->getOperand(0),
4661 TheSelect->getOperand(1),
4662 LLD->getBasePtr(), RLD->getBasePtr(),
4663 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004664 }
Evan Cheng466685d2006-10-09 20:57:25 +00004665 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004666
4667 if (Addr.Val) {
4668 SDOperand Load;
4669 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
4670 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
4671 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004672 LLD->getSrcValueOffset(),
4673 LLD->isVolatile(),
4674 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004675 else {
4676 Load = DAG.getExtLoad(LLD->getExtensionType(),
4677 TheSelect->getValueType(0),
4678 LLD->getChain(), Addr, LLD->getSrcValue(),
4679 LLD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004680 LLD->getLoadedVT(),
4681 LLD->isVolatile(),
4682 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004683 }
4684 // Users of the select now use the result of the load.
4685 CombineTo(TheSelect, Load);
4686
4687 // Users of the old loads now use the new load's chain. We know the
4688 // old-load value is dead now.
4689 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
4690 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
4691 return true;
4692 }
Evan Chengc5484282006-10-04 00:56:09 +00004693 }
Chris Lattner40c62d52005-10-18 06:04:22 +00004694 }
4695 }
4696
4697 return false;
4698}
4699
Nate Begeman44728a72005-09-19 22:34:01 +00004700SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
4701 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00004702 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00004703
4704 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00004705 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
4706 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
4707 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
4708
4709 // Determine if the condition we're dealing with is constant
4710 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00004711 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004712 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
4713
4714 // fold select_cc true, x, y -> x
4715 if (SCCC && SCCC->getValue())
4716 return N2;
4717 // fold select_cc false, x, y -> y
4718 if (SCCC && SCCC->getValue() == 0)
4719 return N3;
4720
4721 // Check to see if we can simplify the select into an fabs node
4722 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
4723 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00004724 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00004725 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
4726 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
4727 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
4728 N2 == N3.getOperand(0))
4729 return DAG.getNode(ISD::FABS, VT, N0);
4730
4731 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
4732 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
4733 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
4734 N2.getOperand(0) == N3)
4735 return DAG.getNode(ISD::FABS, VT, N3);
4736 }
4737 }
4738
4739 // Check to see if we can perform the "gzip trick", transforming
4740 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00004741 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00004742 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00004743 MVT::isInteger(N2.getValueType()) &&
4744 (N1C->isNullValue() || // (a < 0) ? b : 0
4745 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00004746 MVT::ValueType XType = N0.getValueType();
4747 MVT::ValueType AType = N2.getValueType();
4748 if (XType >= AType) {
4749 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00004750 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00004751 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
4752 unsigned ShCtV = Log2_64(N2C->getValue());
4753 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
4754 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
4755 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00004756 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004757 if (XType > AType) {
4758 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004759 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004760 }
4761 return DAG.getNode(ISD::AND, AType, Shift, N2);
4762 }
4763 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4764 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4765 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00004766 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004767 if (XType > AType) {
4768 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004769 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004770 }
4771 return DAG.getNode(ISD::AND, AType, Shift, N2);
4772 }
4773 }
Nate Begeman07ed4172005-10-10 21:26:48 +00004774
4775 // fold select C, 16, 0 -> shl C, 4
4776 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
4777 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00004778
4779 // If the caller doesn't want us to simplify this into a zext of a compare,
4780 // don't do it.
4781 if (NotExtCompare && N2C->getValue() == 1)
4782 return SDOperand();
4783
Nate Begeman07ed4172005-10-10 21:26:48 +00004784 // Get a SetCC of the condition
4785 // FIXME: Should probably make sure that setcc is legal if we ever have a
4786 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00004787 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00004788 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00004789 if (AfterLegalize) {
4790 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Chris Lattner555d8d62006-12-07 22:36:47 +00004791 if (N2.getValueType() < SCC.getValueType())
4792 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
4793 else
4794 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004795 } else {
4796 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00004797 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004798 }
Chris Lattner5750df92006-03-01 04:03:14 +00004799 AddToWorkList(SCC.Val);
4800 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00004801
4802 if (N2C->getValue() == 1)
4803 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00004804 // shl setcc result by log2 n2c
4805 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
4806 DAG.getConstant(Log2_64(N2C->getValue()),
4807 TLI.getShiftAmountTy()));
4808 }
4809
Nate Begemanf845b452005-10-08 00:29:44 +00004810 // Check to see if this is the equivalent of setcc
4811 // FIXME: Turn all of these into setcc if setcc if setcc is legal
4812 // otherwise, go ahead with the folds.
4813 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
4814 MVT::ValueType XType = N0.getValueType();
4815 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
4816 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
4817 if (Res.getValueType() != VT)
4818 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
4819 return Res;
4820 }
4821
4822 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
4823 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
4824 TLI.isOperationLegal(ISD::CTLZ, XType)) {
4825 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
4826 return DAG.getNode(ISD::SRL, XType, Ctlz,
4827 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
4828 TLI.getShiftAmountTy()));
4829 }
4830 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
4831 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
4832 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
4833 N0);
4834 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
4835 DAG.getConstant(~0ULL, XType));
4836 return DAG.getNode(ISD::SRL, XType,
4837 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
4838 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4839 TLI.getShiftAmountTy()));
4840 }
4841 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
4842 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
4843 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
4844 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4845 TLI.getShiftAmountTy()));
4846 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
4847 }
4848 }
4849
4850 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
4851 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4852 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00004853 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
4854 N2.getOperand(0) == N1 && MVT::isInteger(N0.getValueType())) {
4855 MVT::ValueType XType = N0.getValueType();
4856 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4857 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4858 TLI.getShiftAmountTy()));
4859 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
4860 AddToWorkList(Shift.Val);
4861 AddToWorkList(Add.Val);
4862 return DAG.getNode(ISD::XOR, XType, Add, Shift);
4863 }
4864 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
4865 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4866 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
4867 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
4868 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Nate Begemanf845b452005-10-08 00:29:44 +00004869 MVT::ValueType XType = N0.getValueType();
4870 if (SubC->isNullValue() && MVT::isInteger(XType)) {
4871 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4872 DAG.getConstant(MVT::getSizeInBits(XType)-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00004873 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00004874 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004875 AddToWorkList(Shift.Val);
4876 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004877 return DAG.getNode(ISD::XOR, XType, Add, Shift);
4878 }
4879 }
4880 }
Chris Lattner1982ef22007-04-11 05:11:38 +00004881
Nate Begeman44728a72005-09-19 22:34:01 +00004882 return SDOperand();
4883}
4884
Evan Chengfa1eb272007-02-08 22:13:59 +00004885/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Nate Begeman452d7be2005-09-16 00:54:12 +00004886SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00004887 SDOperand N1, ISD::CondCode Cond,
4888 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00004889 TargetLowering::DAGCombinerInfo
4890 DagCombineInfo(DAG, !AfterLegalize, false, this);
4891 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00004892}
4893
Nate Begeman69575232005-10-20 02:15:44 +00004894/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
4895/// return a DAG expression to select that will generate the same value by
4896/// multiplying by a magic number. See:
4897/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4898SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004899 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004900 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
4901
Andrew Lenharth232c9102006-06-12 16:07:18 +00004902 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004903 ii != ee; ++ii)
4904 AddToWorkList(*ii);
4905 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004906}
4907
4908/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
4909/// return a DAG expression to select that will generate the same value by
4910/// multiplying by a magic number. See:
4911/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4912SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004913 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004914 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00004915
Andrew Lenharth232c9102006-06-12 16:07:18 +00004916 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004917 ii != ee; ++ii)
4918 AddToWorkList(*ii);
4919 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004920}
4921
Jim Laskey71382342006-10-07 23:37:56 +00004922/// FindBaseOffset - Return true if base is known not to alias with anything
4923/// but itself. Provides base object and offset as results.
4924static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
4925 // Assume it is a primitive operation.
4926 Base = Ptr; Offset = 0;
4927
4928 // If it's an adding a simple constant then integrate the offset.
4929 if (Base.getOpcode() == ISD::ADD) {
4930 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
4931 Base = Base.getOperand(0);
4932 Offset += C->getValue();
4933 }
4934 }
4935
4936 // If it's any of the following then it can't alias with anything but itself.
4937 return isa<FrameIndexSDNode>(Base) ||
4938 isa<ConstantPoolSDNode>(Base) ||
4939 isa<GlobalAddressSDNode>(Base);
4940}
4941
4942/// isAlias - Return true if there is any possibility that the two addresses
4943/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00004944bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
4945 const Value *SrcValue1, int SrcValueOffset1,
4946 SDOperand Ptr2, int64_t Size2,
4947 const Value *SrcValue2, int SrcValueOffset2)
4948{
Jim Laskey71382342006-10-07 23:37:56 +00004949 // If they are the same then they must be aliases.
4950 if (Ptr1 == Ptr2) return true;
4951
4952 // Gather base node and offset information.
4953 SDOperand Base1, Base2;
4954 int64_t Offset1, Offset2;
4955 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
4956 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
4957
4958 // If they have a same base address then...
4959 if (Base1 == Base2) {
4960 // Check to see if the addresses overlap.
4961 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
4962 }
4963
Jim Laskey096c22e2006-10-18 12:29:57 +00004964 // If we know both bases then they can't alias.
4965 if (KnownBase1 && KnownBase2) return false;
4966
Jim Laskey07a27092006-10-18 19:08:31 +00004967 if (CombinerGlobalAA) {
4968 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00004969 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
4970 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
4971 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00004972 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00004973 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00004974 if (AAResult == AliasAnalysis::NoAlias)
4975 return false;
4976 }
Jim Laskey096c22e2006-10-18 12:29:57 +00004977
4978 // Otherwise we have to assume they alias.
4979 return true;
Jim Laskey71382342006-10-07 23:37:56 +00004980}
4981
4982/// FindAliasInfo - Extracts the relevant alias information from the memory
4983/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00004984bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00004985 SDOperand &Ptr, int64_t &Size,
4986 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004987 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4988 Ptr = LD->getBasePtr();
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004989 Size = MVT::getSizeInBits(LD->getLoadedVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004990 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004991 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00004992 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004993 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004994 Ptr = ST->getBasePtr();
Evan Cheng8b2794a2006-10-13 21:14:26 +00004995 Size = MVT::getSizeInBits(ST->getStoredVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004996 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004997 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00004998 } else {
Jim Laskey71382342006-10-07 23:37:56 +00004999 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005000 }
5001
5002 return false;
5003}
5004
Jim Laskey6ff23e52006-10-04 16:53:27 +00005005/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5006/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00005007void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005008 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005009 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005010 std::set<SDNode *> Visited; // Visited node set.
5011
Jim Laskey279f0532006-09-25 16:29:54 +00005012 // Get alias information for node.
5013 SDOperand Ptr;
5014 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005015 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005016 int SrcValueOffset;
5017 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005018
Jim Laskey6ff23e52006-10-04 16:53:27 +00005019 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005020 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005021
Jim Laskeybc588b82006-10-05 15:07:25 +00005022 // Look at each chain and determine if it is an alias. If so, add it to the
5023 // aliases list. If not, then continue up the chain looking for the next
5024 // candidate.
5025 while (!Chains.empty()) {
5026 SDOperand Chain = Chains.back();
5027 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005028
Jim Laskeybc588b82006-10-05 15:07:25 +00005029 // Don't bother if we've been before.
5030 if (Visited.find(Chain.Val) != Visited.end()) continue;
5031 Visited.insert(Chain.Val);
5032
5033 switch (Chain.getOpcode()) {
5034 case ISD::EntryToken:
5035 // Entry token is ideal chain operand, but handled in FindBetterChain.
5036 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005037
Jim Laskeybc588b82006-10-05 15:07:25 +00005038 case ISD::LOAD:
5039 case ISD::STORE: {
5040 // Get alias information for Chain.
5041 SDOperand OpPtr;
5042 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005043 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005044 int OpSrcValueOffset;
5045 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
5046 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005047
5048 // If chain is alias then stop here.
5049 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005050 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5051 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005052 Aliases.push_back(Chain);
5053 } else {
5054 // Look further up the chain.
5055 Chains.push_back(Chain.getOperand(0));
5056 // Clean up old chain.
5057 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00005058 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005059 break;
5060 }
5061
5062 case ISD::TokenFactor:
5063 // We have to check each of the operands of the token factor, so we queue
5064 // then up. Adding the operands to the queue (stack) in reverse order
5065 // maintains the original order and increases the likelihood that getNode
5066 // will find a matching token factor (CSE.)
5067 for (unsigned n = Chain.getNumOperands(); n;)
5068 Chains.push_back(Chain.getOperand(--n));
5069 // Eliminate the token factor if we can.
5070 AddToWorkList(Chain.Val);
5071 break;
5072
5073 default:
5074 // For all other instructions we will just have to take what we can get.
5075 Aliases.push_back(Chain);
5076 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005077 }
5078 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005079}
5080
5081/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5082/// for a better chain (aliasing node.)
5083SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
5084 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005085
Jim Laskey6ff23e52006-10-04 16:53:27 +00005086 // Accumulate all the aliases to this node.
5087 GatherAllAliases(N, OldChain, Aliases);
5088
5089 if (Aliases.size() == 0) {
5090 // If no operands then chain to entry token.
5091 return DAG.getEntryNode();
5092 } else if (Aliases.size() == 1) {
5093 // If a single operand then chain to it. We don't need to revisit it.
5094 return Aliases[0];
5095 }
5096
5097 // Construct a custom tailored token factor.
5098 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5099 &Aliases[0], Aliases.size());
5100
5101 // Make sure the old chain gets cleaned up.
5102 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
5103
5104 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005105}
5106
Nate Begeman1d4d4142005-09-01 00:19:25 +00005107// SelectionDAG::Combine - This is the entry point for the file.
5108//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005109void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00005110 if (!RunningAfterLegalize && ViewDAGCombine1)
5111 viewGraph();
5112 if (RunningAfterLegalize && ViewDAGCombine2)
5113 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005114 /// run - This is the main entry point to this class.
5115 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005116 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005117}