blob: 2c15f39db4a6f4b8c1d48cd241c1b6b73b8442ae [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
12//
13// FIXME: Missing folds
14// sdiv, udiv, srem, urem (X, const) where X is an integer can be expanded into
15// a sequence of multiplies, shifts, and adds. This should be controlled by
16// some kind of hint from the target that int div is expensive.
17// various folds of mulh[s,u] by constants such as -1, powers of 2, etc.
18//
Nate Begeman44728a72005-09-19 22:34:01 +000019// FIXME: select C, pow2, pow2 -> something smart
20// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
Nate Begeman44728a72005-09-19 22:34:01 +000021// FIXME: Dead stores -> nuke
Chris Lattner40c62d52005-10-18 06:04:22 +000022// FIXME: shr X, (and Y,31) -> shr X, Y (TRICKY!)
Nate Begeman1d4d4142005-09-01 00:19:25 +000023// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000024// FIXME: undef values
Nate Begeman646d7e22005-09-02 21:18:40 +000025// FIXME: divide by zero is currently left unfolded. do we want to turn this
26// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000027// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Nate Begeman1d4d4142005-09-01 00:19:25 +000028//
29//===----------------------------------------------------------------------===//
30
31#define DEBUG_TYPE "dagcombine"
32#include "llvm/ADT/Statistic.h"
33#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000034#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000035#include "llvm/Support/MathExtras.h"
36#include "llvm/Target/TargetLowering.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000037#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000038#include "llvm/Support/CommandLine.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000039#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000040#include <cmath>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000041#include <iostream>
Jim Laskey279f0532006-09-25 16:29:54 +000042#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000043using namespace llvm;
44
45namespace {
Andrew Lenharthae6153f2006-07-20 17:43:27 +000046 static Statistic<> NodesCombined ("dagcombiner",
47 "Number of dag nodes combined");
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000048
Jim Laskey71382342006-10-07 23:37:56 +000049 static cl::opt<bool>
50 CombinerAA("combiner-alias-analysis", cl::Hidden,
51 cl::desc("Turn on alias analysis turning testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000052
Jim Laskeybc588b82006-10-05 15:07:25 +000053//------------------------------ DAGCombiner ---------------------------------//
54
Jim Laskey71382342006-10-07 23:37:56 +000055 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000056 SelectionDAG &DAG;
57 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000058 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000059
60 // Worklist of all of the nodes that need to be simplified.
61 std::vector<SDNode*> WorkList;
62
63 /// AddUsersToWorkList - When an instruction is simplified, add all users of
64 /// the instruction to the work lists because they might get more simplified
65 /// now.
66 ///
67 void AddUsersToWorkList(SDNode *N) {
68 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000069 UI != UE; ++UI)
Jim Laskey6ff23e52006-10-04 16:53:27 +000070 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000071 }
72
73 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000074 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000075 void removeFromWorkList(SDNode *N) {
76 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
77 WorkList.end());
78 }
79
Chris Lattner24664722006-03-01 04:53:38 +000080 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000081 /// AddToWorkList - Add to the work list making sure it's instance is at the
82 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000083 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000084 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000085 WorkList.push_back(N);
86 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000087
Chris Lattner3577e382006-08-11 17:56:38 +000088 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo) {
89 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
Chris Lattner87514ca2005-10-10 22:31:19 +000090 ++NodesCombined;
Jim Laskey6ff23e52006-10-04 16:53:27 +000091 DEBUG(std::cerr << "\nReplacing.1 "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +000092 std::cerr << "\nWith: "; To[0].Val->dump(&DAG);
Chris Lattner3577e382006-08-11 17:56:38 +000093 std::cerr << " and " << NumTo-1 << " other values\n");
Chris Lattner01a22022005-10-10 22:04:48 +000094 std::vector<SDNode*> NowDead;
Chris Lattner3577e382006-08-11 17:56:38 +000095 DAG.ReplaceAllUsesWith(N, To, &NowDead);
Chris Lattner01a22022005-10-10 22:04:48 +000096
97 // Push the new nodes and any users onto the worklist
Chris Lattner3577e382006-08-11 17:56:38 +000098 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000099 AddToWorkList(To[i].Val);
Chris Lattner01a22022005-10-10 22:04:48 +0000100 AddUsersToWorkList(To[i].Val);
101 }
102
Jim Laskey6ff23e52006-10-04 16:53:27 +0000103 // Nodes can be reintroduced into the worklist. Make sure we do not
104 // process a node that has been replaced.
Chris Lattner01a22022005-10-10 22:04:48 +0000105 removeFromWorkList(N);
106 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
107 removeFromWorkList(NowDead[i]);
108
109 // Finally, since the node is now dead, remove it from the graph.
110 DAG.DeleteNode(N);
111 return SDOperand(N, 0);
112 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000113
Chris Lattner24664722006-03-01 04:53:38 +0000114 SDOperand CombineTo(SDNode *N, SDOperand Res) {
Chris Lattner3577e382006-08-11 17:56:38 +0000115 return CombineTo(N, &Res, 1);
Chris Lattner24664722006-03-01 04:53:38 +0000116 }
117
118 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
Chris Lattner3577e382006-08-11 17:56:38 +0000119 SDOperand To[] = { Res0, Res1 };
120 return CombineTo(N, To, 2);
Chris Lattner24664722006-03-01 04:53:38 +0000121 }
122 private:
123
Chris Lattner012f2412006-02-17 21:58:01 +0000124 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000125 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000126 /// propagation. If so, return true.
127 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000128 TargetLowering::TargetLoweringOpt TLO(DAG);
129 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000130 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
131 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
132 return false;
133
134 // Revisit the node.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000135 AddToWorkList(Op.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000136
137 // Replace the old value with the new one.
138 ++NodesCombined;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000139 DEBUG(std::cerr << "\nReplacing.2 "; TLO.Old.Val->dump();
Jim Laskey279f0532006-09-25 16:29:54 +0000140 std::cerr << "\nWith: "; TLO.New.Val->dump(&DAG);
141 std::cerr << '\n');
Chris Lattner012f2412006-02-17 21:58:01 +0000142
143 std::vector<SDNode*> NowDead;
144 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
145
Chris Lattner7d20d392006-02-20 06:51:04 +0000146 // Push the new node and any (possibly new) users onto the worklist.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000147 AddToWorkList(TLO.New.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000148 AddUsersToWorkList(TLO.New.Val);
149
150 // Nodes can end up on the worklist more than once. Make sure we do
151 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000152 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
153 removeFromWorkList(NowDead[i]);
154
Chris Lattner7d20d392006-02-20 06:51:04 +0000155 // Finally, if the node is now dead, remove it from the graph. The node
156 // may not be dead if the replacement process recursively simplified to
157 // something else needing this node.
158 if (TLO.Old.Val->use_empty()) {
159 removeFromWorkList(TLO.Old.Val);
160 DAG.DeleteNode(TLO.Old.Val);
161 }
Chris Lattner012f2412006-02-17 21:58:01 +0000162 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000163 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000164
Nate Begeman1d4d4142005-09-01 00:19:25 +0000165 /// visit - call the node-specific routine that knows how to fold each
166 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000167 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000168
169 // Visitation implementation - Implement dag node combining for different
170 // node types. The semantics are as follows:
171 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000172 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000173 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000174 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000175 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000176 SDOperand visitTokenFactor(SDNode *N);
177 SDOperand visitADD(SDNode *N);
178 SDOperand visitSUB(SDNode *N);
179 SDOperand visitMUL(SDNode *N);
180 SDOperand visitSDIV(SDNode *N);
181 SDOperand visitUDIV(SDNode *N);
182 SDOperand visitSREM(SDNode *N);
183 SDOperand visitUREM(SDNode *N);
184 SDOperand visitMULHU(SDNode *N);
185 SDOperand visitMULHS(SDNode *N);
186 SDOperand visitAND(SDNode *N);
187 SDOperand visitOR(SDNode *N);
188 SDOperand visitXOR(SDNode *N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000189 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000190 SDOperand visitSHL(SDNode *N);
191 SDOperand visitSRA(SDNode *N);
192 SDOperand visitSRL(SDNode *N);
193 SDOperand visitCTLZ(SDNode *N);
194 SDOperand visitCTTZ(SDNode *N);
195 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000196 SDOperand visitSELECT(SDNode *N);
197 SDOperand visitSELECT_CC(SDNode *N);
198 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000199 SDOperand visitSIGN_EXTEND(SDNode *N);
200 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000201 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000202 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
203 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000204 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000205 SDOperand visitVBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000206 SDOperand visitFADD(SDNode *N);
207 SDOperand visitFSUB(SDNode *N);
208 SDOperand visitFMUL(SDNode *N);
209 SDOperand visitFDIV(SDNode *N);
210 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000211 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000212 SDOperand visitSINT_TO_FP(SDNode *N);
213 SDOperand visitUINT_TO_FP(SDNode *N);
214 SDOperand visitFP_TO_SINT(SDNode *N);
215 SDOperand visitFP_TO_UINT(SDNode *N);
216 SDOperand visitFP_ROUND(SDNode *N);
217 SDOperand visitFP_ROUND_INREG(SDNode *N);
218 SDOperand visitFP_EXTEND(SDNode *N);
219 SDOperand visitFNEG(SDNode *N);
220 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000221 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000222 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000223 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000224 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000225 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
226 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000227 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000228 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000229 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000230
Evan Cheng44f1f092006-04-20 08:56:16 +0000231 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000232 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
233
Chris Lattner40c62d52005-10-18 06:04:22 +0000234 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000235 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000236 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
237 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
238 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000239 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000240 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000241 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000242 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000243 SDOperand BuildUDIV(SDNode *N);
244 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Jim Laskey279f0532006-09-25 16:29:54 +0000245
Jim Laskey6ff23e52006-10-04 16:53:27 +0000246 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
247 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000248 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000249 SmallVector<SDOperand, 8> &Aliases);
250
Jim Laskey7ca56af2006-10-11 13:47:09 +0000251 /// FindAliasInfo - Extracts the relevant alias information from the memory
252 /// node. Returns true if the operand was a load.
253 bool FindAliasInfo(SDNode *N,
254 SDOperand &Ptr, int64_t &Size, const Value *&SrcValue);
255
Jim Laskey279f0532006-09-25 16:29:54 +0000256 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000257 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000258 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
259
Nate Begeman1d4d4142005-09-01 00:19:25 +0000260public:
261 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000262 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000263
264 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000265 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000266 };
267}
268
Chris Lattner24664722006-03-01 04:53:38 +0000269//===----------------------------------------------------------------------===//
270// TargetLowering::DAGCombinerInfo implementation
271//===----------------------------------------------------------------------===//
272
273void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
274 ((DAGCombiner*)DC)->AddToWorkList(N);
275}
276
277SDOperand TargetLowering::DAGCombinerInfo::
278CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000279 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000280}
281
282SDOperand TargetLowering::DAGCombinerInfo::
283CombineTo(SDNode *N, SDOperand Res) {
284 return ((DAGCombiner*)DC)->CombineTo(N, Res);
285}
286
287
288SDOperand TargetLowering::DAGCombinerInfo::
289CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
290 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
291}
292
293
294
295
296//===----------------------------------------------------------------------===//
297
298
Nate Begeman4ebd8052005-09-01 23:24:04 +0000299// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
300// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000301// Also, set the incoming LHS, RHS, and CC references to the appropriate
302// nodes based on the type of node we are checking. This simplifies life a
303// bit for the callers.
304static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
305 SDOperand &CC) {
306 if (N.getOpcode() == ISD::SETCC) {
307 LHS = N.getOperand(0);
308 RHS = N.getOperand(1);
309 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000310 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000311 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000312 if (N.getOpcode() == ISD::SELECT_CC &&
313 N.getOperand(2).getOpcode() == ISD::Constant &&
314 N.getOperand(3).getOpcode() == ISD::Constant &&
315 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000316 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
317 LHS = N.getOperand(0);
318 RHS = N.getOperand(1);
319 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000320 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000321 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000322 return false;
323}
324
Nate Begeman99801192005-09-07 23:25:52 +0000325// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
326// one use. If this is true, it allows the users to invert the operation for
327// free when it is profitable to do so.
328static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000329 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000330 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000331 return true;
332 return false;
333}
334
Nate Begemancd4d58c2006-02-03 06:46:56 +0000335SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
336 MVT::ValueType VT = N0.getValueType();
337 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
338 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
339 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
340 if (isa<ConstantSDNode>(N1)) {
341 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000342 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000343 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
344 } else if (N0.hasOneUse()) {
345 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000346 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000347 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
348 }
349 }
350 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
351 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
352 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
353 if (isa<ConstantSDNode>(N0)) {
354 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000355 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000356 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
357 } else if (N1.hasOneUse()) {
358 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000359 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000360 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
361 }
362 }
363 return SDOperand();
364}
365
Nate Begeman4ebd8052005-09-01 23:24:04 +0000366void DAGCombiner::Run(bool RunningAfterLegalize) {
367 // set the instance variable, so that the various visit routines may use it.
368 AfterLegalize = RunningAfterLegalize;
369
Nate Begeman646d7e22005-09-02 21:18:40 +0000370 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000371 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
372 E = DAG.allnodes_end(); I != E; ++I)
373 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000374
Chris Lattner95038592005-10-05 06:35:28 +0000375 // Create a dummy node (which is not added to allnodes), that adds a reference
376 // to the root node, preventing it from being deleted, and tracking any
377 // changes of the root.
378 HandleSDNode Dummy(DAG.getRoot());
379
Chris Lattner24664722006-03-01 04:53:38 +0000380
381 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
382 TargetLowering::DAGCombinerInfo
383 DagCombineInfo(DAG, !RunningAfterLegalize, this);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000384
Nate Begeman1d4d4142005-09-01 00:19:25 +0000385 // while the worklist isn't empty, inspect the node on the end of it and
386 // try and combine it.
387 while (!WorkList.empty()) {
388 SDNode *N = WorkList.back();
389 WorkList.pop_back();
390
391 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000392 // N is deleted from the DAG, since they too may now be dead or may have a
393 // reduced number of uses, allowing other xforms.
394 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000395 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000396 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000397
Chris Lattner95038592005-10-05 06:35:28 +0000398 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000399 continue;
400 }
401
Nate Begeman83e75ec2005-09-06 04:43:02 +0000402 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000403
404 // If nothing happened, try a target-specific DAG combine.
405 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000406 assert(N->getOpcode() != ISD::DELETED_NODE &&
407 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000408 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
409 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
410 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
411 }
412
Nate Begeman83e75ec2005-09-06 04:43:02 +0000413 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000414 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000415 // If we get back the same node we passed in, rather than a new node or
416 // zero, we know that the node must have defined multiple values and
417 // CombineTo was used. Since CombineTo takes care of the worklist
418 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000419 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000420 assert(N->getOpcode() != ISD::DELETED_NODE &&
421 RV.Val->getOpcode() != ISD::DELETED_NODE &&
422 "Node was deleted but visit returned new node!");
423
Jim Laskey6ff23e52006-10-04 16:53:27 +0000424 DEBUG(std::cerr << "\nReplacing.3 "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000425 std::cerr << "\nWith: "; RV.Val->dump(&DAG);
Nate Begeman2300f552005-09-07 00:15:36 +0000426 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000427 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000428 if (N->getNumValues() == RV.Val->getNumValues())
429 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
430 else {
431 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
432 SDOperand OpV = RV;
433 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
434 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000435
436 // Push the new node and any users onto the worklist
Jim Laskey6ff23e52006-10-04 16:53:27 +0000437 AddToWorkList(RV.Val);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000438 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000439
Jim Laskey6ff23e52006-10-04 16:53:27 +0000440 // Nodes can be reintroduced into the worklist. Make sure we do not
441 // process a node that has been replaced.
Nate Begeman646d7e22005-09-02 21:18:40 +0000442 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000443 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
444 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000445
446 // Finally, since the node is now dead, remove it from the graph.
447 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000448 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000449 }
450 }
Chris Lattner95038592005-10-05 06:35:28 +0000451
452 // If the root changed (e.g. it was a dead load, update the root).
453 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000454}
455
Nate Begeman83e75ec2005-09-06 04:43:02 +0000456SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000457 switch(N->getOpcode()) {
458 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000459 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000460 case ISD::ADD: return visitADD(N);
461 case ISD::SUB: return visitSUB(N);
462 case ISD::MUL: return visitMUL(N);
463 case ISD::SDIV: return visitSDIV(N);
464 case ISD::UDIV: return visitUDIV(N);
465 case ISD::SREM: return visitSREM(N);
466 case ISD::UREM: return visitUREM(N);
467 case ISD::MULHU: return visitMULHU(N);
468 case ISD::MULHS: return visitMULHS(N);
469 case ISD::AND: return visitAND(N);
470 case ISD::OR: return visitOR(N);
471 case ISD::XOR: return visitXOR(N);
472 case ISD::SHL: return visitSHL(N);
473 case ISD::SRA: return visitSRA(N);
474 case ISD::SRL: return visitSRL(N);
475 case ISD::CTLZ: return visitCTLZ(N);
476 case ISD::CTTZ: return visitCTTZ(N);
477 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000478 case ISD::SELECT: return visitSELECT(N);
479 case ISD::SELECT_CC: return visitSELECT_CC(N);
480 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000481 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
482 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000483 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000484 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
485 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000486 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000487 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000488 case ISD::FADD: return visitFADD(N);
489 case ISD::FSUB: return visitFSUB(N);
490 case ISD::FMUL: return visitFMUL(N);
491 case ISD::FDIV: return visitFDIV(N);
492 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000493 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000494 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
495 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
496 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
497 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
498 case ISD::FP_ROUND: return visitFP_ROUND(N);
499 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
500 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
501 case ISD::FNEG: return visitFNEG(N);
502 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000503 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000504 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000505 case ISD::LOAD: return visitLOAD(N);
Jim Laskey7aed46c2006-10-11 18:55:16 +0000506 // FIXME - Switch over after StoreSDNode comes online.
Jim Laskey3ad175b2006-10-12 15:22:24 +0000507 case ISD::TRUNCSTORE: // Fall thru
Chris Lattner87514ca2005-10-10 22:31:19 +0000508 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000509 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
510 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000511 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000512 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000513 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000514 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
515 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
516 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
517 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
518 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
519 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
520 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
521 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000522 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000523 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000524}
525
Chris Lattner6270f682006-10-08 22:57:01 +0000526/// getInputChainForNode - Given a node, return its input chain if it has one,
527/// otherwise return a null sd operand.
528static SDOperand getInputChainForNode(SDNode *N) {
529 if (unsigned NumOps = N->getNumOperands()) {
530 if (N->getOperand(0).getValueType() == MVT::Other)
531 return N->getOperand(0);
532 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
533 return N->getOperand(NumOps-1);
534 for (unsigned i = 1; i < NumOps-1; ++i)
535 if (N->getOperand(i).getValueType() == MVT::Other)
536 return N->getOperand(i);
537 }
538 return SDOperand(0, 0);
539}
540
Nate Begeman83e75ec2005-09-06 04:43:02 +0000541SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000542 // If N has two operands, where one has an input chain equal to the other,
543 // the 'other' chain is redundant.
544 if (N->getNumOperands() == 2) {
545 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
546 return N->getOperand(0);
547 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
548 return N->getOperand(1);
549 }
550
551
Jim Laskey6ff23e52006-10-04 16:53:27 +0000552 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Jim Laskey279f0532006-09-25 16:29:54 +0000553 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000554 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000555
556 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000557 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000558
Jim Laskey71382342006-10-07 23:37:56 +0000559 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000560 // encountered.
561 for (unsigned i = 0; i < TFs.size(); ++i) {
562 SDNode *TF = TFs[i];
563
Jim Laskey6ff23e52006-10-04 16:53:27 +0000564 // Check each of the operands.
565 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
566 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000567
Jim Laskey6ff23e52006-10-04 16:53:27 +0000568 switch (Op.getOpcode()) {
569 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000570 // Entry tokens don't need to be added to the list. They are
571 // rededundant.
572 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000573 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000574
Jim Laskey6ff23e52006-10-04 16:53:27 +0000575 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000576 if ((CombinerAA || Op.hasOneUse()) &&
577 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000578 // Queue up for processing.
579 TFs.push_back(Op.Val);
580 // Clean up in case the token factor is removed.
581 AddToWorkList(Op.Val);
582 Changed = true;
583 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000584 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000585 // Fall thru
586
587 default:
Jim Laskeybc588b82006-10-05 15:07:25 +0000588 // Only add if not there prior.
589 if (std::find(Ops.begin(), Ops.end(), Op) == Ops.end())
590 Ops.push_back(Op);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000591 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000592 }
593 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000594 }
595
596 SDOperand Result;
597
598 // If we've change things around then replace token factor.
599 if (Changed) {
600 if (Ops.size() == 0) {
601 // The entry token is the only possible outcome.
602 Result = DAG.getEntryNode();
603 } else {
604 // New and improved token factor.
605 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000606 }
607 }
Jim Laskey279f0532006-09-25 16:29:54 +0000608
Jim Laskey6ff23e52006-10-04 16:53:27 +0000609 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000610}
611
Nate Begeman83e75ec2005-09-06 04:43:02 +0000612SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000613 SDOperand N0 = N->getOperand(0);
614 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000615 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
616 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000617 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000618
619 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000620 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000621 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000622 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000623 if (N0C && !N1C)
624 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000625 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000626 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000627 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000628 // fold ((c1-A)+c2) -> (c1+c2)-A
629 if (N1C && N0.getOpcode() == ISD::SUB)
630 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
631 return DAG.getNode(ISD::SUB, VT,
632 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
633 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000634 // reassociate add
635 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
636 if (RADD.Val != 0)
637 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000638 // fold ((0-A) + B) -> B-A
639 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
640 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000641 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000642 // fold (A + (0-B)) -> A-B
643 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
644 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000645 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000646 // fold (A+(B-A)) -> B
647 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000648 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000649
Evan Cheng860771d2006-03-01 01:09:54 +0000650 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000651 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000652
653 // fold (a+b) -> (a|b) iff a and b share no bits.
654 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
655 uint64_t LHSZero, LHSOne;
656 uint64_t RHSZero, RHSOne;
657 uint64_t Mask = MVT::getIntVTBitMask(VT);
658 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
659 if (LHSZero) {
660 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
661
662 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
663 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
664 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
665 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
666 return DAG.getNode(ISD::OR, VT, N0, N1);
667 }
668 }
669
Nate Begeman83e75ec2005-09-06 04:43:02 +0000670 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000671}
672
Nate Begeman83e75ec2005-09-06 04:43:02 +0000673SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000674 SDOperand N0 = N->getOperand(0);
675 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000676 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
677 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000678 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000679
Chris Lattner854077d2005-10-17 01:07:11 +0000680 // fold (sub x, x) -> 0
681 if (N0 == N1)
682 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000683 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000684 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000685 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000686 // fold (sub x, c) -> (add x, -c)
687 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000688 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000689 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000690 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000691 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000692 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000693 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000694 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000695 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000696}
697
Nate Begeman83e75ec2005-09-06 04:43:02 +0000698SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000699 SDOperand N0 = N->getOperand(0);
700 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000701 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
702 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000703 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000704
705 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000706 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000707 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000708 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000709 if (N0C && !N1C)
710 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000711 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000712 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000713 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000714 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000715 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000716 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000717 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000718 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000719 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000720 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000721 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000722 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
723 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
724 // FIXME: If the input is something that is easily negated (e.g. a
725 // single-use add), we should put the negate there.
726 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
727 DAG.getNode(ISD::SHL, VT, N0,
728 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
729 TLI.getShiftAmountTy())));
730 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +0000731
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000732 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
733 if (N1C && N0.getOpcode() == ISD::SHL &&
734 isa<ConstantSDNode>(N0.getOperand(1))) {
735 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000736 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000737 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
738 }
739
740 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
741 // use.
742 {
743 SDOperand Sh(0,0), Y(0,0);
744 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
745 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
746 N0.Val->hasOneUse()) {
747 Sh = N0; Y = N1;
748 } else if (N1.getOpcode() == ISD::SHL &&
749 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
750 Sh = N1; Y = N0;
751 }
752 if (Sh.Val) {
753 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
754 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
755 }
756 }
Chris Lattnera1deca32006-03-04 23:33:26 +0000757 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
758 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
759 isa<ConstantSDNode>(N0.getOperand(1))) {
760 return DAG.getNode(ISD::ADD, VT,
761 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
762 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
763 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000764
Nate Begemancd4d58c2006-02-03 06:46:56 +0000765 // reassociate mul
766 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
767 if (RMUL.Val != 0)
768 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000769 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000770}
771
Nate Begeman83e75ec2005-09-06 04:43:02 +0000772SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000773 SDOperand N0 = N->getOperand(0);
774 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000775 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
776 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000777 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000778
779 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000780 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000781 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000782 // fold (sdiv X, 1) -> X
783 if (N1C && N1C->getSignExtended() == 1LL)
784 return N0;
785 // fold (sdiv X, -1) -> 0-X
786 if (N1C && N1C->isAllOnesValue())
787 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000788 // If we know the sign bits of both operands are zero, strength reduce to a
789 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
790 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000791 if (TLI.MaskedValueIsZero(N1, SignBit) &&
792 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000793 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000794 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000795 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000796 (isPowerOf2_64(N1C->getSignExtended()) ||
797 isPowerOf2_64(-N1C->getSignExtended()))) {
798 // If dividing by powers of two is cheap, then don't perform the following
799 // fold.
800 if (TLI.isPow2DivCheap())
801 return SDOperand();
802 int64_t pow2 = N1C->getSignExtended();
803 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000804 unsigned lg2 = Log2_64(abs2);
805 // Splat the sign bit into the register
806 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000807 DAG.getConstant(MVT::getSizeInBits(VT)-1,
808 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +0000809 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000810 // Add (N0 < 0) ? abs2 - 1 : 0;
811 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
812 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000813 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000814 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +0000815 AddToWorkList(SRL.Val);
816 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +0000817 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
818 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000819 // If we're dividing by a positive value, we're done. Otherwise, we must
820 // negate the result.
821 if (pow2 > 0)
822 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +0000823 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000824 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
825 }
Nate Begeman69575232005-10-20 02:15:44 +0000826 // if integer divide is expensive and we satisfy the requirements, emit an
827 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000828 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000829 !TLI.isIntDivCheap()) {
830 SDOperand Op = BuildSDIV(N);
831 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000832 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000833 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000834}
835
Nate Begeman83e75ec2005-09-06 04:43:02 +0000836SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000837 SDOperand N0 = N->getOperand(0);
838 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000839 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
840 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000841 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000842
843 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000844 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000845 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000846 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000847 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000848 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000849 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000850 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000851 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
852 if (N1.getOpcode() == ISD::SHL) {
853 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
854 if (isPowerOf2_64(SHC->getValue())) {
855 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000856 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
857 DAG.getConstant(Log2_64(SHC->getValue()),
858 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +0000859 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000860 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000861 }
862 }
863 }
Nate Begeman69575232005-10-20 02:15:44 +0000864 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000865 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
866 SDOperand Op = BuildUDIV(N);
867 if (Op.Val) return Op;
868 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000869 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000870}
871
Nate Begeman83e75ec2005-09-06 04:43:02 +0000872SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000873 SDOperand N0 = N->getOperand(0);
874 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000875 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
876 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000877 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000878
879 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000880 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000881 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000882 // If we know the sign bits of both operands are zero, strength reduce to a
883 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
884 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000885 if (TLI.MaskedValueIsZero(N1, SignBit) &&
886 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000887 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000888 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000889}
890
Nate Begeman83e75ec2005-09-06 04:43:02 +0000891SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000892 SDOperand N0 = N->getOperand(0);
893 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000894 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
895 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000896 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000897
898 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000899 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000900 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000901 // fold (urem x, pow2) -> (and x, pow2-1)
902 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000903 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000904 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
905 if (N1.getOpcode() == ISD::SHL) {
906 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
907 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000908 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +0000909 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000910 return DAG.getNode(ISD::AND, VT, N0, Add);
911 }
912 }
913 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000914 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000915}
916
Nate Begeman83e75ec2005-09-06 04:43:02 +0000917SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000918 SDOperand N0 = N->getOperand(0);
919 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000920 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000921
922 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000923 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000924 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000925 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000926 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000927 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
928 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000929 TLI.getShiftAmountTy()));
930 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000931}
932
Nate Begeman83e75ec2005-09-06 04:43:02 +0000933SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000934 SDOperand N0 = N->getOperand(0);
935 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000936 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000937
938 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000939 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000940 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000941 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000942 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000943 return DAG.getConstant(0, N0.getValueType());
944 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000945}
946
Chris Lattner35e5c142006-05-05 05:51:50 +0000947/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
948/// two operands of the same opcode, try to simplify it.
949SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
950 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
951 MVT::ValueType VT = N0.getValueType();
952 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
953
Chris Lattner540121f2006-05-05 06:31:05 +0000954 // For each of OP in AND/OR/XOR:
955 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
956 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
957 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +0000958 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +0000959 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +0000960 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000961 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
962 SDOperand ORNode = DAG.getNode(N->getOpcode(),
963 N0.getOperand(0).getValueType(),
964 N0.getOperand(0), N1.getOperand(0));
965 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +0000966 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +0000967 }
968
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000969 // For each of OP in SHL/SRL/SRA/AND...
970 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
971 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
972 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +0000973 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000974 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000975 N0.getOperand(1) == N1.getOperand(1)) {
976 SDOperand ORNode = DAG.getNode(N->getOpcode(),
977 N0.getOperand(0).getValueType(),
978 N0.getOperand(0), N1.getOperand(0));
979 AddToWorkList(ORNode.Val);
980 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
981 }
982
983 return SDOperand();
984}
985
Nate Begeman83e75ec2005-09-06 04:43:02 +0000986SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000987 SDOperand N0 = N->getOperand(0);
988 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +0000989 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000990 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
991 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000992 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000993 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000994
995 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000996 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000997 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000998 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000999 if (N0C && !N1C)
1000 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001001 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001002 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001003 return N0;
1004 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +00001005 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001006 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001007 // reassociate and
1008 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1009 if (RAND.Val != 0)
1010 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001011 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001012 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001013 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001014 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001015 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001016 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1017 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001018 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +00001019 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001020 ~N1C->getValue() & InMask)) {
1021 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1022 N0.getOperand(0));
1023
1024 // Replace uses of the AND with uses of the Zero extend node.
1025 CombineTo(N, Zext);
1026
Chris Lattner3603cd62006-02-02 07:17:31 +00001027 // We actually want to replace all uses of the any_extend with the
1028 // zero_extend, to avoid duplicating things. This will later cause this
1029 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001030 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001031 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001032 }
1033 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001034 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1035 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1036 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1037 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1038
1039 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1040 MVT::isInteger(LL.getValueType())) {
1041 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1042 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1043 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001044 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001045 return DAG.getSetCC(VT, ORNode, LR, Op1);
1046 }
1047 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1048 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1049 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001050 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001051 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1052 }
1053 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1054 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1055 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001056 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001057 return DAG.getSetCC(VT, ORNode, LR, Op1);
1058 }
1059 }
1060 // canonicalize equivalent to ll == rl
1061 if (LL == RR && LR == RL) {
1062 Op1 = ISD::getSetCCSwappedOperands(Op1);
1063 std::swap(RL, RR);
1064 }
1065 if (LL == RL && LR == RR) {
1066 bool isInteger = MVT::isInteger(LL.getValueType());
1067 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1068 if (Result != ISD::SETCC_INVALID)
1069 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1070 }
1071 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001072
1073 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1074 if (N0.getOpcode() == N1.getOpcode()) {
1075 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1076 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001077 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001078
Nate Begemande996292006-02-03 22:24:05 +00001079 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1080 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001081 if (!MVT::isVector(VT) &&
1082 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001083 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001084 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Chengc5484282006-10-04 00:56:09 +00001085 if (ISD::isEXTLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001086 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001087 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001088 // If we zero all the possible extended bits, then we can turn this into
1089 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001090 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001091 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001092 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1093 LN0->getBasePtr(), LN0->getSrcValue(),
1094 LN0->getSrcValueOffset(), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001095 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001096 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001097 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001098 }
1099 }
1100 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Chengc5484282006-10-04 00:56:09 +00001101 if (ISD::isSEXTLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001102 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001103 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001104 // If we zero all the possible extended bits, then we can turn this into
1105 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001106 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001107 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001108 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1109 LN0->getBasePtr(), LN0->getSrcValue(),
1110 LN0->getSrcValueOffset(), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001111 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001112 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001113 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001114 }
1115 }
Chris Lattner15045b62006-02-28 06:35:35 +00001116
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001117 // fold (and (load x), 255) -> (zextload x, i8)
1118 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001119 if (N1C && N0.getOpcode() == ISD::LOAD) {
1120 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1121 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
1122 N0.hasOneUse()) {
1123 MVT::ValueType EVT, LoadedVT;
1124 if (N1C->getValue() == 255)
1125 EVT = MVT::i8;
1126 else if (N1C->getValue() == 65535)
1127 EVT = MVT::i16;
1128 else if (N1C->getValue() == ~0U)
1129 EVT = MVT::i32;
1130 else
1131 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001132
Evan Cheng2e49f092006-10-11 07:10:22 +00001133 LoadedVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001134 if (EVT != MVT::Other && LoadedVT > EVT &&
1135 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1136 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1137 // For big endian targets, we need to add an offset to the pointer to
1138 // load the correct bytes. For little endian systems, we merely need to
1139 // read fewer bytes from the same pointer.
1140 unsigned PtrOff =
1141 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1142 SDOperand NewPtr = LN0->getBasePtr();
1143 if (!TLI.isLittleEndian())
1144 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1145 DAG.getConstant(PtrOff, PtrType));
1146 AddToWorkList(NewPtr.Val);
1147 SDOperand Load =
1148 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
1149 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT);
1150 AddToWorkList(N);
1151 CombineTo(N0.Val, Load, Load.getValue(1));
1152 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1153 }
Chris Lattner15045b62006-02-28 06:35:35 +00001154 }
1155 }
1156
Nate Begeman83e75ec2005-09-06 04:43:02 +00001157 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001158}
1159
Nate Begeman83e75ec2005-09-06 04:43:02 +00001160SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001161 SDOperand N0 = N->getOperand(0);
1162 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001163 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001164 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1165 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001166 MVT::ValueType VT = N1.getValueType();
1167 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001168
1169 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001170 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001171 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001172 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001173 if (N0C && !N1C)
1174 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001175 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001176 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001177 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001178 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001179 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001180 return N1;
1181 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001182 if (N1C &&
1183 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001184 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001185 // reassociate or
1186 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1187 if (ROR.Val != 0)
1188 return ROR;
1189 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1190 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001191 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001192 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1193 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1194 N1),
1195 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001196 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001197 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1198 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1199 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1200 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1201
1202 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1203 MVT::isInteger(LL.getValueType())) {
1204 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1205 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1206 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1207 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1208 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001209 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001210 return DAG.getSetCC(VT, ORNode, LR, Op1);
1211 }
1212 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1213 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1214 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1215 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1216 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001217 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001218 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1219 }
1220 }
1221 // canonicalize equivalent to ll == rl
1222 if (LL == RR && LR == RL) {
1223 Op1 = ISD::getSetCCSwappedOperands(Op1);
1224 std::swap(RL, RR);
1225 }
1226 if (LL == RL && LR == RR) {
1227 bool isInteger = MVT::isInteger(LL.getValueType());
1228 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1229 if (Result != ISD::SETCC_INVALID)
1230 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1231 }
1232 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001233
1234 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1235 if (N0.getOpcode() == N1.getOpcode()) {
1236 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1237 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001238 }
Chris Lattner516b9622006-09-14 20:50:57 +00001239
Chris Lattner1ec72732006-09-14 21:11:37 +00001240 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1241 if (N0.getOpcode() == ISD::AND &&
1242 N1.getOpcode() == ISD::AND &&
1243 N0.getOperand(1).getOpcode() == ISD::Constant &&
1244 N1.getOperand(1).getOpcode() == ISD::Constant &&
1245 // Don't increase # computations.
1246 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1247 // We can only do this xform if we know that bits from X that are set in C2
1248 // but not in C1 are already zero. Likewise for Y.
1249 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1250 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1251
1252 if (TLI.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1253 TLI.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
1254 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1255 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1256 }
1257 }
1258
1259
Chris Lattner516b9622006-09-14 20:50:57 +00001260 // See if this is some rotate idiom.
1261 if (SDNode *Rot = MatchRotate(N0, N1))
1262 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001263
Nate Begeman83e75ec2005-09-06 04:43:02 +00001264 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001265}
1266
Chris Lattner516b9622006-09-14 20:50:57 +00001267
1268/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1269static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1270 if (Op.getOpcode() == ISD::AND) {
1271 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1272 Mask = Op.getOperand(1);
1273 Op = Op.getOperand(0);
1274 } else {
1275 return false;
1276 }
1277 }
1278
1279 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1280 Shift = Op;
1281 return true;
1282 }
1283 return false;
1284}
1285
1286
1287// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1288// idioms for rotate, and if the target supports rotation instructions, generate
1289// a rot[lr].
1290SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1291 // Must be a legal type. Expanded an promoted things won't work with rotates.
1292 MVT::ValueType VT = LHS.getValueType();
1293 if (!TLI.isTypeLegal(VT)) return 0;
1294
1295 // The target must have at least one rotate flavor.
1296 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1297 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1298 if (!HasROTL && !HasROTR) return 0;
1299
1300 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1301 SDOperand LHSShift; // The shift.
1302 SDOperand LHSMask; // AND value if any.
1303 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1304 return 0; // Not part of a rotate.
1305
1306 SDOperand RHSShift; // The shift.
1307 SDOperand RHSMask; // AND value if any.
1308 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1309 return 0; // Not part of a rotate.
1310
1311 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1312 return 0; // Not shifting the same value.
1313
1314 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1315 return 0; // Shifts must disagree.
1316
1317 // Canonicalize shl to left side in a shl/srl pair.
1318 if (RHSShift.getOpcode() == ISD::SHL) {
1319 std::swap(LHS, RHS);
1320 std::swap(LHSShift, RHSShift);
1321 std::swap(LHSMask , RHSMask );
1322 }
1323
1324 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1325
1326 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1327 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
1328 if (LHSShift.getOperand(1).getOpcode() == ISD::Constant &&
1329 RHSShift.getOperand(1).getOpcode() == ISD::Constant) {
1330 uint64_t LShVal = cast<ConstantSDNode>(LHSShift.getOperand(1))->getValue();
1331 uint64_t RShVal = cast<ConstantSDNode>(RHSShift.getOperand(1))->getValue();
1332 if ((LShVal + RShVal) != OpSizeInBits)
1333 return 0;
1334
1335 SDOperand Rot;
1336 if (HasROTL)
1337 Rot = DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1338 LHSShift.getOperand(1));
1339 else
1340 Rot = DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1341 RHSShift.getOperand(1));
1342
1343 // If there is an AND of either shifted operand, apply it to the result.
1344 if (LHSMask.Val || RHSMask.Val) {
1345 uint64_t Mask = MVT::getIntVTBitMask(VT);
1346
1347 if (LHSMask.Val) {
1348 uint64_t RHSBits = (1ULL << LShVal)-1;
1349 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1350 }
1351 if (RHSMask.Val) {
1352 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1353 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1354 }
1355
1356 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1357 }
1358
1359 return Rot.Val;
1360 }
1361
1362 // If there is a mask here, and we have a variable shift, we can't be sure
1363 // that we're masking out the right stuff.
1364 if (LHSMask.Val || RHSMask.Val)
1365 return 0;
1366
1367 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1368 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
1369 if (RHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1370 LHSShift.getOperand(1) == RHSShift.getOperand(1).getOperand(1)) {
1371 if (ConstantSDNode *SUBC =
1372 dyn_cast<ConstantSDNode>(RHSShift.getOperand(1).getOperand(0))) {
1373 if (SUBC->getValue() == OpSizeInBits)
1374 if (HasROTL)
1375 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1376 LHSShift.getOperand(1)).Val;
1377 else
1378 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1379 LHSShift.getOperand(1)).Val;
1380 }
1381 }
1382
1383 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1384 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
1385 if (LHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1386 RHSShift.getOperand(1) == LHSShift.getOperand(1).getOperand(1)) {
1387 if (ConstantSDNode *SUBC =
1388 dyn_cast<ConstantSDNode>(LHSShift.getOperand(1).getOperand(0))) {
1389 if (SUBC->getValue() == OpSizeInBits)
1390 if (HasROTL)
1391 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1392 LHSShift.getOperand(1)).Val;
1393 else
1394 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1395 RHSShift.getOperand(1)).Val;
1396 }
1397 }
1398
1399 return 0;
1400}
1401
1402
Nate Begeman83e75ec2005-09-06 04:43:02 +00001403SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001404 SDOperand N0 = N->getOperand(0);
1405 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001406 SDOperand LHS, RHS, CC;
1407 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1408 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001409 MVT::ValueType VT = N0.getValueType();
1410
1411 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001412 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001413 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001414 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001415 if (N0C && !N1C)
1416 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001417 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001418 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001419 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001420 // reassociate xor
1421 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1422 if (RXOR.Val != 0)
1423 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001424 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001425 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1426 bool isInt = MVT::isInteger(LHS.getValueType());
1427 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1428 isInt);
1429 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001430 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001431 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001432 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001433 assert(0 && "Unhandled SetCC Equivalent!");
1434 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001435 }
Nate Begeman99801192005-09-07 23:25:52 +00001436 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1437 if (N1C && N1C->getValue() == 1 &&
1438 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001439 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001440 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1441 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001442 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1443 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001444 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001445 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001446 }
1447 }
Nate Begeman99801192005-09-07 23:25:52 +00001448 // fold !(x or y) -> (!x and !y) iff x or y are constants
1449 if (N1C && N1C->isAllOnesValue() &&
1450 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001451 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001452 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1453 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001454 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1455 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001456 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001457 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001458 }
1459 }
Nate Begeman223df222005-09-08 20:18:10 +00001460 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1461 if (N1C && N0.getOpcode() == ISD::XOR) {
1462 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1463 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1464 if (N00C)
1465 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1466 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1467 if (N01C)
1468 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1469 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1470 }
1471 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001472 if (N0 == N1) {
1473 if (!MVT::isVector(VT)) {
1474 return DAG.getConstant(0, VT);
1475 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1476 // Produce a vector of zeros.
1477 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1478 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001479 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001480 }
1481 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001482
1483 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1484 if (N0.getOpcode() == N1.getOpcode()) {
1485 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1486 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001487 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001488
Chris Lattner3e104b12006-04-08 04:15:24 +00001489 // Simplify the expression using non-local knowledge.
1490 if (!MVT::isVector(VT) &&
1491 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001492 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001493
Nate Begeman83e75ec2005-09-06 04:43:02 +00001494 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001495}
1496
Nate Begeman83e75ec2005-09-06 04:43:02 +00001497SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001498 SDOperand N0 = N->getOperand(0);
1499 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001500 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1501 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001502 MVT::ValueType VT = N0.getValueType();
1503 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1504
1505 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001506 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001507 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001508 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001509 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001510 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001511 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001512 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001513 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001514 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001515 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001516 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001517 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001518 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001519 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001520 if (SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001521 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001522 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001523 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001524 N0.getOperand(1).getOpcode() == ISD::Constant) {
1525 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001526 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001527 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001528 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001529 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001530 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001531 }
1532 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1533 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001534 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001535 N0.getOperand(1).getOpcode() == ISD::Constant) {
1536 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001537 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001538 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1539 DAG.getConstant(~0ULL << c1, VT));
1540 if (c2 > c1)
1541 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001542 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001543 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001544 return DAG.getNode(ISD::SRL, VT, Mask,
1545 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001546 }
1547 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001548 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001549 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001550 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnercac70592006-03-05 19:53:55 +00001551 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1552 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1553 isa<ConstantSDNode>(N0.getOperand(1))) {
1554 return DAG.getNode(ISD::ADD, VT,
1555 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1556 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1557 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001558 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001559}
1560
Nate Begeman83e75ec2005-09-06 04:43:02 +00001561SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001562 SDOperand N0 = N->getOperand(0);
1563 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001564 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1565 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001566 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001567
1568 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001569 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001570 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001571 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001572 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001573 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001574 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001575 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001576 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001577 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001578 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001579 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001580 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001581 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001582 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001583 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1584 // sext_inreg.
1585 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1586 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1587 MVT::ValueType EVT;
1588 switch (LowBits) {
1589 default: EVT = MVT::Other; break;
1590 case 1: EVT = MVT::i1; break;
1591 case 8: EVT = MVT::i8; break;
1592 case 16: EVT = MVT::i16; break;
1593 case 32: EVT = MVT::i32; break;
1594 }
1595 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1596 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1597 DAG.getValueType(EVT));
1598 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001599
1600 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1601 if (N1C && N0.getOpcode() == ISD::SRA) {
1602 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1603 unsigned Sum = N1C->getValue() + C1->getValue();
1604 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1605 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1606 DAG.getConstant(Sum, N1C->getValueType(0)));
1607 }
1608 }
1609
Chris Lattnera8504462006-05-08 20:51:54 +00001610 // Simplify, based on bits shifted out of the LHS.
1611 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
1612 return SDOperand(N, 0);
1613
1614
Nate Begeman1d4d4142005-09-01 00:19:25 +00001615 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001616 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001617 return DAG.getNode(ISD::SRL, VT, N0, N1);
1618 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001619}
1620
Nate Begeman83e75ec2005-09-06 04:43:02 +00001621SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001622 SDOperand N0 = N->getOperand(0);
1623 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001624 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1625 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001626 MVT::ValueType VT = N0.getValueType();
1627 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1628
1629 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001630 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001631 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001632 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001633 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001634 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001635 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001636 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001637 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001638 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001639 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001640 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001641 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001642 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001643 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001644 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001645 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001646 N0.getOperand(1).getOpcode() == ISD::Constant) {
1647 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001648 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001649 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001650 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001651 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001652 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001653 }
Chris Lattner350bec02006-04-02 06:11:11 +00001654
Chris Lattner06afe072006-05-05 22:53:17 +00001655 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
1656 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1657 // Shifting in all undef bits?
1658 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
1659 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
1660 return DAG.getNode(ISD::UNDEF, VT);
1661
1662 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
1663 AddToWorkList(SmallShift.Val);
1664 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
1665 }
1666
Chris Lattner350bec02006-04-02 06:11:11 +00001667 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1668 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1669 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1670 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1671 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
1672
1673 // If any of the input bits are KnownOne, then the input couldn't be all
1674 // zeros, thus the result of the srl will always be zero.
1675 if (KnownOne) return DAG.getConstant(0, VT);
1676
1677 // If all of the bits input the to ctlz node are known to be zero, then
1678 // the result of the ctlz is "32" and the result of the shift is one.
1679 uint64_t UnknownBits = ~KnownZero & Mask;
1680 if (UnknownBits == 0) return DAG.getConstant(1, VT);
1681
1682 // Otherwise, check to see if there is exactly one bit input to the ctlz.
1683 if ((UnknownBits & (UnknownBits-1)) == 0) {
1684 // Okay, we know that only that the single bit specified by UnknownBits
1685 // could be set on input to the CTLZ node. If this bit is set, the SRL
1686 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
1687 // to an SRL,XOR pair, which is likely to simplify more.
1688 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
1689 SDOperand Op = N0.getOperand(0);
1690 if (ShAmt) {
1691 Op = DAG.getNode(ISD::SRL, VT, Op,
1692 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
1693 AddToWorkList(Op.Val);
1694 }
1695 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
1696 }
1697 }
1698
Nate Begeman83e75ec2005-09-06 04:43:02 +00001699 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001700}
1701
Nate Begeman83e75ec2005-09-06 04:43:02 +00001702SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001703 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001704 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001705
1706 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001707 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001708 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001709 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001710}
1711
Nate Begeman83e75ec2005-09-06 04:43:02 +00001712SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001713 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001714 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001715
1716 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001717 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001718 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001719 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001720}
1721
Nate Begeman83e75ec2005-09-06 04:43:02 +00001722SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001723 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001724 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001725
1726 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001727 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001728 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001729 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001730}
1731
Nate Begeman452d7be2005-09-16 00:54:12 +00001732SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1733 SDOperand N0 = N->getOperand(0);
1734 SDOperand N1 = N->getOperand(1);
1735 SDOperand N2 = N->getOperand(2);
1736 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1737 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1738 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1739 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001740
Nate Begeman452d7be2005-09-16 00:54:12 +00001741 // fold select C, X, X -> X
1742 if (N1 == N2)
1743 return N1;
1744 // fold select true, X, Y -> X
1745 if (N0C && !N0C->isNullValue())
1746 return N1;
1747 // fold select false, X, Y -> Y
1748 if (N0C && N0C->isNullValue())
1749 return N2;
1750 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001751 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001752 return DAG.getNode(ISD::OR, VT, N0, N2);
1753 // fold select C, 0, X -> ~C & X
1754 // FIXME: this should check for C type == X type, not i1?
1755 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1756 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001757 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001758 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1759 }
1760 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001761 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001762 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001763 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001764 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1765 }
1766 // fold select C, X, 0 -> C & X
1767 // FIXME: this should check for C type == X type, not i1?
1768 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1769 return DAG.getNode(ISD::AND, VT, N0, N1);
1770 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1771 if (MVT::i1 == VT && N0 == N1)
1772 return DAG.getNode(ISD::OR, VT, N0, N2);
1773 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1774 if (MVT::i1 == VT && N0 == N2)
1775 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00001776
Chris Lattner40c62d52005-10-18 06:04:22 +00001777 // If we can fold this based on the true/false value, do so.
1778 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00001779 return SDOperand(N, 0); // Don't revisit N.
1780
Nate Begeman44728a72005-09-19 22:34:01 +00001781 // fold selects based on a setcc into other things, such as min/max/abs
1782 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001783 // FIXME:
1784 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1785 // having to say they don't support SELECT_CC on every type the DAG knows
1786 // about, since there is no way to mark an opcode illegal at all value types
1787 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1788 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1789 N1, N2, N0.getOperand(2));
1790 else
1791 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001792 return SDOperand();
1793}
1794
1795SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001796 SDOperand N0 = N->getOperand(0);
1797 SDOperand N1 = N->getOperand(1);
1798 SDOperand N2 = N->getOperand(2);
1799 SDOperand N3 = N->getOperand(3);
1800 SDOperand N4 = N->getOperand(4);
1801 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1802 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1803 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1804 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1805
Nate Begeman44728a72005-09-19 22:34:01 +00001806 // fold select_cc lhs, rhs, x, x, cc -> x
1807 if (N2 == N3)
1808 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001809
Chris Lattner5f42a242006-09-20 06:19:26 +00001810 // Determine if the condition we're dealing with is constant
1811 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
1812
1813 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
1814 if (SCCC->getValue())
1815 return N2; // cond always true -> true val
1816 else
1817 return N3; // cond always false -> false val
1818 }
1819
1820 // Fold to a simpler select_cc
1821 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
1822 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
1823 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
1824 SCC.getOperand(2));
1825
Chris Lattner40c62d52005-10-18 06:04:22 +00001826 // If we can fold this based on the true/false value, do so.
1827 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00001828 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00001829
Nate Begeman44728a72005-09-19 22:34:01 +00001830 // fold select_cc into other things, such as min/max/abs
1831 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001832}
1833
1834SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1835 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1836 cast<CondCodeSDNode>(N->getOperand(2))->get());
1837}
1838
Nate Begeman83e75ec2005-09-06 04:43:02 +00001839SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001840 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001841 MVT::ValueType VT = N->getValueType(0);
1842
Nate Begeman1d4d4142005-09-01 00:19:25 +00001843 // fold (sext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001844 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001845 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00001846
Nate Begeman1d4d4142005-09-01 00:19:25 +00001847 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001848 // fold (sext (aext x)) -> (sext x)
1849 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001850 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00001851
Chris Lattner6007b842006-09-21 06:00:20 +00001852 // fold (sext (truncate x)) -> (sextinreg x).
1853 if (N0.getOpcode() == ISD::TRUNCATE &&
Chris Lattnerbf370872006-09-21 06:17:39 +00001854 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
1855 N0.getValueType()))) {
Chris Lattner6007b842006-09-21 06:00:20 +00001856 SDOperand Op = N0.getOperand(0);
1857 if (Op.getValueType() < VT) {
1858 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
1859 } else if (Op.getValueType() > VT) {
1860 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
1861 }
1862 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001863 DAG.getValueType(N0.getValueType()));
Chris Lattner6007b842006-09-21 06:00:20 +00001864 }
Chris Lattner310b5782006-05-06 23:06:26 +00001865
Evan Cheng110dec22005-12-14 02:19:23 +00001866 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00001867 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00001868 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng466685d2006-10-09 20:57:25 +00001869 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1870 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
1871 LN0->getBasePtr(), LN0->getSrcValue(),
1872 LN0->getSrcValueOffset(),
Nate Begeman3df4d522005-10-12 20:40:40 +00001873 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001874 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001875 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1876 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001877 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00001878 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001879
1880 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1881 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Chengc5484282006-10-04 00:56:09 +00001882 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001883 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001884 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001885 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
1886 LN0->getBasePtr(), LN0->getSrcValue(),
1887 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001888 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001889 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1890 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001891 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001892 }
1893
Nate Begeman83e75ec2005-09-06 04:43:02 +00001894 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001895}
1896
Nate Begeman83e75ec2005-09-06 04:43:02 +00001897SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001898 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001899 MVT::ValueType VT = N->getValueType(0);
1900
Nate Begeman1d4d4142005-09-01 00:19:25 +00001901 // fold (zext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001902 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001903 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001904 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001905 // fold (zext (aext x)) -> (zext x)
1906 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001907 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00001908
1909 // fold (zext (truncate x)) -> (and x, mask)
1910 if (N0.getOpcode() == ISD::TRUNCATE &&
1911 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
1912 SDOperand Op = N0.getOperand(0);
1913 if (Op.getValueType() < VT) {
1914 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
1915 } else if (Op.getValueType() > VT) {
1916 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
1917 }
1918 return DAG.getZeroExtendInReg(Op, N0.getValueType());
1919 }
1920
Chris Lattner111c2282006-09-21 06:14:31 +00001921 // fold (zext (and (trunc x), cst)) -> (and x, cst).
1922 if (N0.getOpcode() == ISD::AND &&
1923 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1924 N0.getOperand(1).getOpcode() == ISD::Constant) {
1925 SDOperand X = N0.getOperand(0).getOperand(0);
1926 if (X.getValueType() < VT) {
1927 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
1928 } else if (X.getValueType() > VT) {
1929 X = DAG.getNode(ISD::TRUNCATE, VT, X);
1930 }
1931 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1932 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
1933 }
1934
Evan Cheng110dec22005-12-14 02:19:23 +00001935 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00001936 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00001937 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001938 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1939 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1940 LN0->getBasePtr(), LN0->getSrcValue(),
1941 LN0->getSrcValueOffset(),
Evan Cheng110dec22005-12-14 02:19:23 +00001942 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001943 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001944 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1945 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001946 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00001947 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001948
1949 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1950 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Chengc5484282006-10-04 00:56:09 +00001951 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001952 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001953 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001954 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1955 LN0->getBasePtr(), LN0->getSrcValue(),
1956 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001957 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001958 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1959 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001960 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001961 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001962 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001963}
1964
Chris Lattner5ffc0662006-05-05 05:58:59 +00001965SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
1966 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001967 MVT::ValueType VT = N->getValueType(0);
1968
1969 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001970 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00001971 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
1972 // fold (aext (aext x)) -> (aext x)
1973 // fold (aext (zext x)) -> (zext x)
1974 // fold (aext (sext x)) -> (sext x)
1975 if (N0.getOpcode() == ISD::ANY_EXTEND ||
1976 N0.getOpcode() == ISD::ZERO_EXTEND ||
1977 N0.getOpcode() == ISD::SIGN_EXTEND)
1978 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
1979
Chris Lattner84750582006-09-20 06:29:17 +00001980 // fold (aext (truncate x))
1981 if (N0.getOpcode() == ISD::TRUNCATE) {
1982 SDOperand TruncOp = N0.getOperand(0);
1983 if (TruncOp.getValueType() == VT)
1984 return TruncOp; // x iff x size == zext size.
1985 if (TruncOp.getValueType() > VT)
1986 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
1987 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
1988 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00001989
1990 // fold (aext (and (trunc x), cst)) -> (and x, cst).
1991 if (N0.getOpcode() == ISD::AND &&
1992 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1993 N0.getOperand(1).getOpcode() == ISD::Constant) {
1994 SDOperand X = N0.getOperand(0).getOperand(0);
1995 if (X.getValueType() < VT) {
1996 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
1997 } else if (X.getValueType() > VT) {
1998 X = DAG.getNode(ISD::TRUNCATE, VT, X);
1999 }
2000 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2001 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2002 }
2003
Chris Lattner5ffc0662006-05-05 05:58:59 +00002004 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002005 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002006 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002007 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2008 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2009 LN0->getBasePtr(), LN0->getSrcValue(),
2010 LN0->getSrcValueOffset(),
Chris Lattner5ffc0662006-05-05 05:58:59 +00002011 N0.getValueType());
2012 CombineTo(N, ExtLoad);
2013 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2014 ExtLoad.getValue(1));
2015 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2016 }
2017
2018 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2019 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2020 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002021 if (N0.getOpcode() == ISD::LOAD && !ISD::isNON_EXTLoad(N0.Val) &&
2022 N0.hasOneUse()) {
2023 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002024 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002025 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
2026 LN0->getChain(), LN0->getBasePtr(),
2027 LN0->getSrcValue(),
2028 LN0->getSrcValueOffset(), EVT);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002029 CombineTo(N, ExtLoad);
2030 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2031 ExtLoad.getValue(1));
2032 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2033 }
2034 return SDOperand();
2035}
2036
2037
Nate Begeman83e75ec2005-09-06 04:43:02 +00002038SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002039 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002040 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002041 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002042 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00002043 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002044
Nate Begeman1d4d4142005-09-01 00:19:25 +00002045 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00002046 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00002047 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00002048
Chris Lattner541a24f2006-05-06 22:43:44 +00002049 // If the input is already sign extended, just drop the extension.
Chris Lattneree4ea922006-05-06 09:30:03 +00002050 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
2051 return N0;
2052
Nate Begeman646d7e22005-09-02 21:18:40 +00002053 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
2054 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2055 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00002056 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002057 }
Chris Lattner4b37e872006-05-08 21:18:59 +00002058
Nate Begeman07ed4172005-10-10 21:26:48 +00002059 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00002060 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00002061 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00002062
2063 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
2064 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
2065 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
2066 if (N0.getOpcode() == ISD::SRL) {
2067 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2068 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
2069 // We can turn this into an SRA iff the input to the SRL is already sign
2070 // extended enough.
2071 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
2072 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
2073 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
2074 }
2075 }
2076
Nate Begemanded49632005-10-13 03:11:28 +00002077 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00002078 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002079 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002080 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002081 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2082 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2083 LN0->getBasePtr(), LN0->getSrcValue(),
2084 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002085 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002086 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002087 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002088 }
2089 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Chengc5484282006-10-04 00:56:09 +00002090 if (ISD::isZEXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002091 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002092 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002093 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2094 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2095 LN0->getBasePtr(), LN0->getSrcValue(),
2096 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002097 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002098 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002099 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002100 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002101 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002102}
2103
Nate Begeman83e75ec2005-09-06 04:43:02 +00002104SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002105 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002106 MVT::ValueType VT = N->getValueType(0);
2107
2108 // noop truncate
2109 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002110 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002111 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002112 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002113 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002114 // fold (truncate (truncate x)) -> (truncate x)
2115 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002116 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002117 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00002118 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2119 N0.getOpcode() == ISD::ANY_EXTEND) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002120 if (N0.getValueType() < VT)
2121 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00002122 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002123 else if (N0.getValueType() > VT)
2124 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002125 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002126 else
2127 // if the source and dest are the same type, we can drop both the extend
2128 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002129 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002130 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002131 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng466685d2006-10-09 20:57:25 +00002132 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00002133 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
2134 "Cannot truncate to larger type!");
Evan Cheng466685d2006-10-09 20:57:25 +00002135 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Nate Begeman3df4d522005-10-12 20:40:40 +00002136 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00002137 // For big endian targets, we need to add an offset to the pointer to load
2138 // the correct bytes. For little endian systems, we merely need to read
2139 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00002140 uint64_t PtrOff =
2141 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Evan Cheng466685d2006-10-09 20:57:25 +00002142 SDOperand NewPtr = TLI.isLittleEndian() ? LN0->getBasePtr() :
2143 DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Nate Begeman765784a2005-10-12 23:18:53 +00002144 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00002145 AddToWorkList(NewPtr.Val);
Evan Cheng466685d2006-10-09 20:57:25 +00002146 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), NewPtr,
2147 LN0->getSrcValue(), LN0->getSrcValueOffset());
Chris Lattner5750df92006-03-01 04:03:14 +00002148 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00002149 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002150 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002151 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002152 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002153}
2154
Chris Lattner94683772005-12-23 05:30:37 +00002155SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2156 SDOperand N0 = N->getOperand(0);
2157 MVT::ValueType VT = N->getValueType(0);
2158
2159 // If the input is a constant, let getNode() fold it.
2160 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2161 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2162 if (Res.Val != N) return Res;
2163 }
2164
Chris Lattnerc8547d82005-12-23 05:37:50 +00002165 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2166 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00002167
Chris Lattner57104102005-12-23 05:44:41 +00002168 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002169 // FIXME: These xforms need to know that the resultant load doesn't need a
2170 // higher alignment than the original!
Evan Cheng466685d2006-10-09 20:57:25 +00002171 if (0 && ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse()) {
2172 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2173 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
2174 LN0->getSrcValue(), LN0->getSrcValueOffset());
Chris Lattner5750df92006-03-01 04:03:14 +00002175 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00002176 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2177 Load.getValue(1));
2178 return Load;
2179 }
2180
Chris Lattner94683772005-12-23 05:30:37 +00002181 return SDOperand();
2182}
2183
Chris Lattner6258fb22006-04-02 02:53:43 +00002184SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2185 SDOperand N0 = N->getOperand(0);
2186 MVT::ValueType VT = N->getValueType(0);
2187
2188 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2189 // First check to see if this is all constant.
2190 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2191 VT == MVT::Vector) {
2192 bool isSimple = true;
2193 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2194 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2195 N0.getOperand(i).getOpcode() != ISD::Constant &&
2196 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2197 isSimple = false;
2198 break;
2199 }
2200
Chris Lattner97c20732006-04-03 17:29:28 +00002201 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2202 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002203 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2204 }
2205 }
2206
2207 return SDOperand();
2208}
2209
2210/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2211/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2212/// destination element value type.
2213SDOperand DAGCombiner::
2214ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2215 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2216
2217 // If this is already the right type, we're done.
2218 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2219
2220 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2221 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2222
2223 // If this is a conversion of N elements of one type to N elements of another
2224 // type, convert each element. This handles FP<->INT cases.
2225 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002226 SmallVector<SDOperand, 8> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00002227 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002228 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002229 AddToWorkList(Ops.back().Val);
2230 }
Chris Lattner6258fb22006-04-02 02:53:43 +00002231 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2232 Ops.push_back(DAG.getValueType(DstEltVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002233 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002234 }
2235
2236 // Otherwise, we're growing or shrinking the elements. To avoid having to
2237 // handle annoying details of growing/shrinking FP values, we convert them to
2238 // int first.
2239 if (MVT::isFloatingPoint(SrcEltVT)) {
2240 // Convert the input float vector to a int vector where the elements are the
2241 // same sizes.
2242 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2243 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2244 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2245 SrcEltVT = IntVT;
2246 }
2247
2248 // Now we know the input is an integer vector. If the output is a FP type,
2249 // convert to integer first, then to FP of the right size.
2250 if (MVT::isFloatingPoint(DstEltVT)) {
2251 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2252 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2253 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2254
2255 // Next, convert to FP elements of the same size.
2256 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2257 }
2258
2259 // Okay, we know the src/dst types are both integers of differing types.
2260 // Handling growing first.
2261 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2262 if (SrcBitSize < DstBitSize) {
2263 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2264
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002265 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002266 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2267 i += NumInputsPerOutput) {
2268 bool isLE = TLI.isLittleEndian();
2269 uint64_t NewBits = 0;
2270 bool EltIsUndef = true;
2271 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2272 // Shift the previously computed bits over.
2273 NewBits <<= SrcBitSize;
2274 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2275 if (Op.getOpcode() == ISD::UNDEF) continue;
2276 EltIsUndef = false;
2277
2278 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2279 }
2280
2281 if (EltIsUndef)
2282 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2283 else
2284 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2285 }
2286
2287 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2288 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002289 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002290 }
2291
2292 // Finally, this must be the case where we are shrinking elements: each input
2293 // turns into multiple outputs.
2294 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002295 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002296 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2297 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2298 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2299 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2300 continue;
2301 }
2302 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2303
2304 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2305 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2306 OpVal >>= DstBitSize;
2307 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2308 }
2309
2310 // For big endian targets, swap the order of the pieces of each element.
2311 if (!TLI.isLittleEndian())
2312 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2313 }
2314 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2315 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002316 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002317}
2318
2319
2320
Chris Lattner01b3d732005-09-28 22:28:18 +00002321SDOperand DAGCombiner::visitFADD(SDNode *N) {
2322 SDOperand N0 = N->getOperand(0);
2323 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002324 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2325 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002326 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002327
2328 // fold (fadd c1, c2) -> c1+c2
2329 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002330 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002331 // canonicalize constant to RHS
2332 if (N0CFP && !N1CFP)
2333 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002334 // fold (A + (-B)) -> A-B
2335 if (N1.getOpcode() == ISD::FNEG)
2336 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002337 // fold ((-A) + B) -> B-A
2338 if (N0.getOpcode() == ISD::FNEG)
2339 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002340 return SDOperand();
2341}
2342
2343SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2344 SDOperand N0 = N->getOperand(0);
2345 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002346 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2347 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002348 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002349
2350 // fold (fsub c1, c2) -> c1-c2
2351 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002352 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002353 // fold (A-(-B)) -> A+B
2354 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00002355 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002356 return SDOperand();
2357}
2358
2359SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2360 SDOperand N0 = N->getOperand(0);
2361 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002362 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2363 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002364 MVT::ValueType VT = N->getValueType(0);
2365
Nate Begeman11af4ea2005-10-17 20:40:11 +00002366 // fold (fmul c1, c2) -> c1*c2
2367 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002368 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002369 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002370 if (N0CFP && !N1CFP)
2371 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002372 // fold (fmul X, 2.0) -> (fadd X, X)
2373 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2374 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002375 return SDOperand();
2376}
2377
2378SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2379 SDOperand N0 = N->getOperand(0);
2380 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002381 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2382 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002383 MVT::ValueType VT = N->getValueType(0);
2384
Nate Begemana148d982006-01-18 22:35:16 +00002385 // fold (fdiv c1, c2) -> c1/c2
2386 if (N0CFP && N1CFP)
2387 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002388 return SDOperand();
2389}
2390
2391SDOperand DAGCombiner::visitFREM(SDNode *N) {
2392 SDOperand N0 = N->getOperand(0);
2393 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002394 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2395 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002396 MVT::ValueType VT = N->getValueType(0);
2397
Nate Begemana148d982006-01-18 22:35:16 +00002398 // fold (frem c1, c2) -> fmod(c1,c2)
2399 if (N0CFP && N1CFP)
2400 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002401 return SDOperand();
2402}
2403
Chris Lattner12d83032006-03-05 05:30:57 +00002404SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2405 SDOperand N0 = N->getOperand(0);
2406 SDOperand N1 = N->getOperand(1);
2407 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2408 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2409 MVT::ValueType VT = N->getValueType(0);
2410
2411 if (N0CFP && N1CFP) // Constant fold
2412 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2413
2414 if (N1CFP) {
2415 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2416 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2417 union {
2418 double d;
2419 int64_t i;
2420 } u;
2421 u.d = N1CFP->getValue();
2422 if (u.i >= 0)
2423 return DAG.getNode(ISD::FABS, VT, N0);
2424 else
2425 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2426 }
2427
2428 // copysign(fabs(x), y) -> copysign(x, y)
2429 // copysign(fneg(x), y) -> copysign(x, y)
2430 // copysign(copysign(x,z), y) -> copysign(x, y)
2431 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2432 N0.getOpcode() == ISD::FCOPYSIGN)
2433 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2434
2435 // copysign(x, abs(y)) -> abs(x)
2436 if (N1.getOpcode() == ISD::FABS)
2437 return DAG.getNode(ISD::FABS, VT, N0);
2438
2439 // copysign(x, copysign(y,z)) -> copysign(x, z)
2440 if (N1.getOpcode() == ISD::FCOPYSIGN)
2441 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2442
2443 // copysign(x, fp_extend(y)) -> copysign(x, y)
2444 // copysign(x, fp_round(y)) -> copysign(x, y)
2445 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2446 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2447
2448 return SDOperand();
2449}
2450
2451
Chris Lattner01b3d732005-09-28 22:28:18 +00002452
Nate Begeman83e75ec2005-09-06 04:43:02 +00002453SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002454 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002455 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002456 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002457
2458 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002459 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002460 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002461 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002462}
2463
Nate Begeman83e75ec2005-09-06 04:43:02 +00002464SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002465 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002466 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002467 MVT::ValueType VT = N->getValueType(0);
2468
Nate Begeman1d4d4142005-09-01 00:19:25 +00002469 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002470 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002471 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002472 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002473}
2474
Nate Begeman83e75ec2005-09-06 04:43:02 +00002475SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002476 SDOperand N0 = N->getOperand(0);
2477 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2478 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002479
2480 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002481 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002482 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002483 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002484}
2485
Nate Begeman83e75ec2005-09-06 04:43:02 +00002486SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002487 SDOperand N0 = N->getOperand(0);
2488 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2489 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002490
2491 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002492 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002493 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002494 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002495}
2496
Nate Begeman83e75ec2005-09-06 04:43:02 +00002497SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002498 SDOperand N0 = N->getOperand(0);
2499 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2500 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002501
2502 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002503 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002504 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002505
2506 // fold (fp_round (fp_extend x)) -> x
2507 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2508 return N0.getOperand(0);
2509
2510 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2511 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2512 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2513 AddToWorkList(Tmp.Val);
2514 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2515 }
2516
Nate Begeman83e75ec2005-09-06 04:43:02 +00002517 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002518}
2519
Nate Begeman83e75ec2005-09-06 04:43:02 +00002520SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002521 SDOperand N0 = N->getOperand(0);
2522 MVT::ValueType VT = N->getValueType(0);
2523 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002524 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002525
Nate Begeman1d4d4142005-09-01 00:19:25 +00002526 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002527 if (N0CFP) {
2528 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002529 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002530 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002531 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002532}
2533
Nate Begeman83e75ec2005-09-06 04:43:02 +00002534SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002535 SDOperand N0 = N->getOperand(0);
2536 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2537 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002538
2539 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002540 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002541 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00002542
2543 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002544 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002545 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002546 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2547 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2548 LN0->getBasePtr(), LN0->getSrcValue(),
2549 LN0->getSrcValueOffset(),
Chris Lattnere564dbb2006-05-05 21:34:35 +00002550 N0.getValueType());
2551 CombineTo(N, ExtLoad);
2552 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
2553 ExtLoad.getValue(1));
2554 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2555 }
2556
2557
Nate Begeman83e75ec2005-09-06 04:43:02 +00002558 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002559}
2560
Nate Begeman83e75ec2005-09-06 04:43:02 +00002561SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002562 SDOperand N0 = N->getOperand(0);
2563 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2564 MVT::ValueType VT = N->getValueType(0);
2565
2566 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002567 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002568 return DAG.getNode(ISD::FNEG, VT, N0);
2569 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002570 if (N0.getOpcode() == ISD::SUB)
2571 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002572 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002573 if (N0.getOpcode() == ISD::FNEG)
2574 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002575 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002576}
2577
Nate Begeman83e75ec2005-09-06 04:43:02 +00002578SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002579 SDOperand N0 = N->getOperand(0);
2580 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2581 MVT::ValueType VT = N->getValueType(0);
2582
Nate Begeman1d4d4142005-09-01 00:19:25 +00002583 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002584 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002585 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002586 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002587 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002588 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002589 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002590 // fold (fabs (fcopysign x, y)) -> (fabs x)
2591 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2592 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2593
Nate Begeman83e75ec2005-09-06 04:43:02 +00002594 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002595}
2596
Nate Begeman44728a72005-09-19 22:34:01 +00002597SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2598 SDOperand Chain = N->getOperand(0);
2599 SDOperand N1 = N->getOperand(1);
2600 SDOperand N2 = N->getOperand(2);
2601 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2602
2603 // never taken branch, fold to chain
2604 if (N1C && N1C->isNullValue())
2605 return Chain;
2606 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002607 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002608 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002609 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2610 // on the target.
2611 if (N1.getOpcode() == ISD::SETCC &&
2612 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2613 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2614 N1.getOperand(0), N1.getOperand(1), N2);
2615 }
Nate Begeman44728a72005-09-19 22:34:01 +00002616 return SDOperand();
2617}
2618
Chris Lattner3ea0b472005-10-05 06:47:48 +00002619// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2620//
Nate Begeman44728a72005-09-19 22:34:01 +00002621SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002622 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2623 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2624
2625 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002626 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2627 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2628
2629 // fold br_cc true, dest -> br dest (unconditional branch)
2630 if (SCCC && SCCC->getValue())
2631 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2632 N->getOperand(4));
2633 // fold br_cc false, dest -> unconditional fall through
2634 if (SCCC && SCCC->isNullValue())
2635 return N->getOperand(0);
2636 // fold to a simpler setcc
2637 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2638 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2639 Simp.getOperand(2), Simp.getOperand(0),
2640 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002641 return SDOperand();
2642}
2643
Chris Lattner01a22022005-10-10 22:04:48 +00002644SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00002645 LoadSDNode *LD = cast<LoadSDNode>(N);
2646 SDOperand Chain = LD->getChain();
2647 SDOperand Ptr = LD->getBasePtr();
Jim Laskey6ff23e52006-10-04 16:53:27 +00002648
Chris Lattnere4b95392006-03-31 18:06:18 +00002649 // If there are no uses of the loaded value, change uses of the chain value
2650 // into uses of the chain input (i.e. delete the dead load).
2651 if (N->hasNUsesOfValue(0, 0))
2652 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
Chris Lattner01a22022005-10-10 22:04:48 +00002653
2654 // If this load is directly stored, replace the load value with the stored
2655 // value.
2656 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002657 // TODO: Handle TRUNCSTORE/LOADEXT
2658 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
2659 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2660 Chain.getOperand(1).getValueType() == N->getValueType(0))
2661 return CombineTo(N, Chain.getOperand(1), Chain);
2662 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00002663
Jim Laskey7ca56af2006-10-11 13:47:09 +00002664 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00002665 // Walk up chain skipping non-aliasing memory nodes.
2666 SDOperand BetterChain = FindBetterChain(N, Chain);
2667
Jim Laskey6ff23e52006-10-04 16:53:27 +00002668 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00002669 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002670 SDOperand ReplLoad;
2671
Jim Laskey279f0532006-09-25 16:29:54 +00002672 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002673 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
2674 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
2675 LD->getSrcValue(), LD->getSrcValueOffset());
2676 } else {
2677 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
2678 LD->getValueType(0),
2679 BetterChain, Ptr, LD->getSrcValue(),
2680 LD->getSrcValueOffset(),
2681 LD->getLoadedVT());
2682 }
Jim Laskey279f0532006-09-25 16:29:54 +00002683
Jim Laskey6ff23e52006-10-04 16:53:27 +00002684 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00002685 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
2686 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00002687
2688 // Replace uses with load result and token factor.
2689 return CombineTo(N, ReplLoad.getValue(0), Token);
Jim Laskey279f0532006-09-25 16:29:54 +00002690 }
2691 }
2692
Chris Lattner01a22022005-10-10 22:04:48 +00002693 return SDOperand();
2694}
2695
Chris Lattner87514ca2005-10-10 22:31:19 +00002696SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2697 SDOperand Chain = N->getOperand(0);
2698 SDOperand Value = N->getOperand(1);
2699 SDOperand Ptr = N->getOperand(2);
2700 SDOperand SrcValue = N->getOperand(3);
Jim Laskey7aed46c2006-10-11 18:55:16 +00002701
2702 // FIXME - Switch over after StoreSDNode comes online.
2703 if (N->getOpcode() == ISD::TRUNCSTORE) {
2704 if (CombinerAA) {
2705 // Walk up chain skipping non-aliasing memory nodes.
2706 SDOperand BetterChain = FindBetterChain(N, Chain);
2707
2708 // If there is a better chain.
2709 if (Chain != BetterChain) {
2710 // Replace the chain to avoid dependency.
Jim Laskey3ad175b2006-10-12 15:22:24 +00002711 SDOperand ReplTStore = DAG.getNode(ISD::TRUNCSTORE, MVT::Other,
2712 BetterChain, Value, Ptr, SrcValue,
2713 N->getOperand(4));
2714
Jim Laskey7aed46c2006-10-11 18:55:16 +00002715 // Create token to keep both nodes around.
2716 return DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplTStore);
2717 }
2718 }
2719
2720 return SDOperand();
2721 }
Chris Lattner87514ca2005-10-10 22:31:19 +00002722
2723 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002724 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002725 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2726 // Make sure that these stores are the same value type:
2727 // FIXME: we really care that the second store is >= size of the first.
2728 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002729 // Create a new store of Value that replaces both stores.
2730 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002731 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2732 return Chain;
Evan Cheng786225a2006-10-05 23:01:46 +00002733 SDOperand NewStore = DAG.getStore(PrevStore->getOperand(0), Value, Ptr,
2734 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002735 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002736 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002737 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002738 }
2739
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002740 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002741 // FIXME: This needs to know that the resultant store does not need a
2742 // higher alignment than the original.
Jim Laskey14fbcbf2006-09-25 21:11:32 +00002743 if (0 && Value.getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng786225a2006-10-05 23:01:46 +00002744 return DAG.getStore(Chain, Value.getOperand(0), Ptr, SrcValue);
Jim Laskey279f0532006-09-25 16:29:54 +00002745 }
2746
2747 if (CombinerAA) {
Jim Laskey288af5e2006-09-25 19:32:58 +00002748 // If the store ptr is a frame index and the frame index has a use of one
2749 // and this is a return block, then the store is redundant.
2750 if (Ptr.hasOneUse() && isa<FrameIndexSDNode>(Ptr) &&
2751 DAG.getRoot().getOpcode() == ISD::RET) {
2752 return Chain;
2753 }
2754
Jim Laskey279f0532006-09-25 16:29:54 +00002755 // Walk up chain skipping non-aliasing memory nodes.
2756 SDOperand BetterChain = FindBetterChain(N, Chain);
2757
Jim Laskey6ff23e52006-10-04 16:53:27 +00002758 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00002759 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00002760 // Replace the chain to avoid dependency.
Evan Cheng786225a2006-10-05 23:01:46 +00002761 SDOperand ReplStore = DAG.getStore(BetterChain, Value, Ptr, SrcValue);
Jim Laskey279f0532006-09-25 16:29:54 +00002762 // Create token to keep both nodes around.
Jim Laskey6ff23e52006-10-04 16:53:27 +00002763 return DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
Jim Laskey279f0532006-09-25 16:29:54 +00002764 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00002765 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002766
Chris Lattner87514ca2005-10-10 22:31:19 +00002767 return SDOperand();
2768}
2769
Chris Lattnerca242442006-03-19 01:27:56 +00002770SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2771 SDOperand InVec = N->getOperand(0);
2772 SDOperand InVal = N->getOperand(1);
2773 SDOperand EltNo = N->getOperand(2);
2774
2775 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2776 // vector with the inserted element.
2777 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2778 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002779 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002780 if (Elt < Ops.size())
2781 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002782 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
2783 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002784 }
2785
2786 return SDOperand();
2787}
2788
2789SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2790 SDOperand InVec = N->getOperand(0);
2791 SDOperand InVal = N->getOperand(1);
2792 SDOperand EltNo = N->getOperand(2);
2793 SDOperand NumElts = N->getOperand(3);
2794 SDOperand EltType = N->getOperand(4);
2795
2796 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2797 // vector with the inserted element.
2798 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2799 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002800 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002801 if (Elt < Ops.size()-2)
2802 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002803 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
2804 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002805 }
2806
2807 return SDOperand();
2808}
2809
Chris Lattnerd7648c82006-03-28 20:28:38 +00002810SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
2811 unsigned NumInScalars = N->getNumOperands()-2;
2812 SDOperand NumElts = N->getOperand(NumInScalars);
2813 SDOperand EltType = N->getOperand(NumInScalars+1);
2814
2815 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
2816 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
2817 // two distinct vectors, turn this into a shuffle node.
2818 SDOperand VecIn1, VecIn2;
2819 for (unsigned i = 0; i != NumInScalars; ++i) {
2820 // Ignore undef inputs.
2821 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
2822
2823 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
2824 // constant index, bail out.
2825 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
2826 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
2827 VecIn1 = VecIn2 = SDOperand(0, 0);
2828 break;
2829 }
2830
2831 // If the input vector type disagrees with the result of the vbuild_vector,
2832 // we can't make a shuffle.
2833 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
2834 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
2835 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
2836 VecIn1 = VecIn2 = SDOperand(0, 0);
2837 break;
2838 }
2839
2840 // Otherwise, remember this. We allow up to two distinct input vectors.
2841 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
2842 continue;
2843
2844 if (VecIn1.Val == 0) {
2845 VecIn1 = ExtractedFromVec;
2846 } else if (VecIn2.Val == 0) {
2847 VecIn2 = ExtractedFromVec;
2848 } else {
2849 // Too many inputs.
2850 VecIn1 = VecIn2 = SDOperand(0, 0);
2851 break;
2852 }
2853 }
2854
2855 // If everything is good, we can make a shuffle operation.
2856 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002857 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00002858 for (unsigned i = 0; i != NumInScalars; ++i) {
2859 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
2860 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2861 continue;
2862 }
2863
2864 SDOperand Extract = N->getOperand(i);
2865
2866 // If extracting from the first vector, just use the index directly.
2867 if (Extract.getOperand(0) == VecIn1) {
2868 BuildVecIndices.push_back(Extract.getOperand(1));
2869 continue;
2870 }
2871
2872 // Otherwise, use InIdx + VecSize
2873 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
2874 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
2875 }
2876
2877 // Add count and size info.
2878 BuildVecIndices.push_back(NumElts);
2879 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
2880
2881 // Return the new VVECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002882 SDOperand Ops[5];
2883 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00002884 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002885 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00002886 } else {
2887 // Use an undef vbuild_vector as input for the second operand.
2888 std::vector<SDOperand> UnOps(NumInScalars,
2889 DAG.getNode(ISD::UNDEF,
2890 cast<VTSDNode>(EltType)->getVT()));
2891 UnOps.push_back(NumElts);
2892 UnOps.push_back(EltType);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002893 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2894 &UnOps[0], UnOps.size());
2895 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00002896 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002897 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2898 &BuildVecIndices[0], BuildVecIndices.size());
2899 Ops[3] = NumElts;
2900 Ops[4] = EltType;
2901 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
Chris Lattnerd7648c82006-03-28 20:28:38 +00002902 }
2903
2904 return SDOperand();
2905}
2906
Chris Lattner66445d32006-03-28 22:11:53 +00002907SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002908 SDOperand ShufMask = N->getOperand(2);
2909 unsigned NumElts = ShufMask.getNumOperands();
2910
2911 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2912 bool isIdentity = true;
2913 for (unsigned i = 0; i != NumElts; ++i) {
2914 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2915 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2916 isIdentity = false;
2917 break;
2918 }
2919 }
2920 if (isIdentity) return N->getOperand(0);
2921
2922 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2923 isIdentity = true;
2924 for (unsigned i = 0; i != NumElts; ++i) {
2925 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2926 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2927 isIdentity = false;
2928 break;
2929 }
2930 }
2931 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00002932
2933 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
2934 // needed at all.
2935 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00002936 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002937 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00002938 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002939 for (unsigned i = 0; i != NumElts; ++i)
2940 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
2941 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
2942 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00002943 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00002944 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00002945 BaseIdx = Idx;
2946 } else {
2947 if (BaseIdx != Idx)
2948 isSplat = false;
2949 if (VecNum != V) {
2950 isUnary = false;
2951 break;
2952 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00002953 }
2954 }
2955
2956 SDOperand N0 = N->getOperand(0);
2957 SDOperand N1 = N->getOperand(1);
2958 // Normalize unary shuffle so the RHS is undef.
2959 if (isUnary && VecNum == 1)
2960 std::swap(N0, N1);
2961
Evan Cheng917ec982006-07-21 08:25:53 +00002962 // If it is a splat, check if the argument vector is a build_vector with
2963 // all scalar elements the same.
2964 if (isSplat) {
2965 SDNode *V = N0.Val;
2966 if (V->getOpcode() == ISD::BIT_CONVERT)
2967 V = V->getOperand(0).Val;
2968 if (V->getOpcode() == ISD::BUILD_VECTOR) {
2969 unsigned NumElems = V->getNumOperands()-2;
2970 if (NumElems > BaseIdx) {
2971 SDOperand Base;
2972 bool AllSame = true;
2973 for (unsigned i = 0; i != NumElems; ++i) {
2974 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
2975 Base = V->getOperand(i);
2976 break;
2977 }
2978 }
2979 // Splat of <u, u, u, u>, return <u, u, u, u>
2980 if (!Base.Val)
2981 return N0;
2982 for (unsigned i = 0; i != NumElems; ++i) {
2983 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
2984 V->getOperand(i) != Base) {
2985 AllSame = false;
2986 break;
2987 }
2988 }
2989 // Splat of <x, x, x, x>, return <x, x, x, x>
2990 if (AllSame)
2991 return N0;
2992 }
2993 }
2994 }
2995
Evan Chenge7bec0d2006-07-20 22:44:41 +00002996 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
2997 // into an undef.
2998 if (isUnary || N0 == N1) {
2999 if (N0.getOpcode() == ISD::UNDEF)
Evan Chengc04766a2006-04-06 23:20:43 +00003000 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00003001 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3002 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003003 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00003004 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00003005 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3006 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3007 MappedOps.push_back(ShufMask.getOperand(i));
3008 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00003009 unsigned NewIdx =
3010 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3011 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00003012 }
3013 }
3014 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003015 &MappedOps[0], MappedOps.size());
Chris Lattner3e104b12006-04-08 04:15:24 +00003016 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00003017 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
Evan Chenge7bec0d2006-07-20 22:44:41 +00003018 N0,
Chris Lattner66445d32006-03-28 22:11:53 +00003019 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
3020 ShufMask);
3021 }
3022
3023 return SDOperand();
3024}
3025
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003026SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
3027 SDOperand ShufMask = N->getOperand(2);
3028 unsigned NumElts = ShufMask.getNumOperands()-2;
3029
3030 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3031 bool isIdentity = true;
3032 for (unsigned i = 0; i != NumElts; ++i) {
3033 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3034 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3035 isIdentity = false;
3036 break;
3037 }
3038 }
3039 if (isIdentity) return N->getOperand(0);
3040
3041 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3042 isIdentity = true;
3043 for (unsigned i = 0; i != NumElts; ++i) {
3044 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3045 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3046 isIdentity = false;
3047 break;
3048 }
3049 }
3050 if (isIdentity) return N->getOperand(1);
3051
Evan Chenge7bec0d2006-07-20 22:44:41 +00003052 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3053 // needed at all.
3054 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003055 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003056 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003057 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003058 for (unsigned i = 0; i != NumElts; ++i)
3059 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3060 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3061 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003062 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003063 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003064 BaseIdx = Idx;
3065 } else {
3066 if (BaseIdx != Idx)
3067 isSplat = false;
3068 if (VecNum != V) {
3069 isUnary = false;
3070 break;
3071 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003072 }
3073 }
3074
3075 SDOperand N0 = N->getOperand(0);
3076 SDOperand N1 = N->getOperand(1);
3077 // Normalize unary shuffle so the RHS is undef.
3078 if (isUnary && VecNum == 1)
3079 std::swap(N0, N1);
3080
Evan Cheng917ec982006-07-21 08:25:53 +00003081 // If it is a splat, check if the argument vector is a build_vector with
3082 // all scalar elements the same.
3083 if (isSplat) {
3084 SDNode *V = N0.Val;
3085 if (V->getOpcode() == ISD::VBIT_CONVERT)
3086 V = V->getOperand(0).Val;
3087 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
3088 unsigned NumElems = V->getNumOperands()-2;
3089 if (NumElems > BaseIdx) {
3090 SDOperand Base;
3091 bool AllSame = true;
3092 for (unsigned i = 0; i != NumElems; ++i) {
3093 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3094 Base = V->getOperand(i);
3095 break;
3096 }
3097 }
3098 // Splat of <u, u, u, u>, return <u, u, u, u>
3099 if (!Base.Val)
3100 return N0;
3101 for (unsigned i = 0; i != NumElems; ++i) {
3102 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3103 V->getOperand(i) != Base) {
3104 AllSame = false;
3105 break;
3106 }
3107 }
3108 // Splat of <x, x, x, x>, return <x, x, x, x>
3109 if (AllSame)
3110 return N0;
3111 }
3112 }
3113 }
3114
Evan Chenge7bec0d2006-07-20 22:44:41 +00003115 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3116 // into an undef.
3117 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00003118 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3119 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003120 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00003121 for (unsigned i = 0; i != NumElts; ++i) {
3122 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3123 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3124 MappedOps.push_back(ShufMask.getOperand(i));
3125 } else {
3126 unsigned NewIdx =
3127 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3128 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
3129 }
3130 }
3131 // Add the type/#elts values.
3132 MappedOps.push_back(ShufMask.getOperand(NumElts));
3133 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
3134
3135 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003136 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003137 AddToWorkList(ShufMask.Val);
3138
3139 // Build the undef vector.
3140 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
3141 for (unsigned i = 0; i != NumElts; ++i)
3142 MappedOps[i] = UDVal;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003143 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
3144 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003145 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3146 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003147
3148 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
Evan Chenge7bec0d2006-07-20 22:44:41 +00003149 N0, UDVal, ShufMask,
Chris Lattner17614ea2006-04-08 05:34:25 +00003150 MappedOps[NumElts], MappedOps[NumElts+1]);
3151 }
3152
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003153 return SDOperand();
3154}
3155
Evan Cheng44f1f092006-04-20 08:56:16 +00003156/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
3157/// a VAND to a vector_shuffle with the destination vector and a zero vector.
3158/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
3159/// vector_shuffle V, Zero, <0, 4, 2, 4>
3160SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
3161 SDOperand LHS = N->getOperand(0);
3162 SDOperand RHS = N->getOperand(1);
3163 if (N->getOpcode() == ISD::VAND) {
3164 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
3165 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
3166 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
3167 RHS = RHS.getOperand(0);
3168 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
3169 std::vector<SDOperand> IdxOps;
3170 unsigned NumOps = RHS.getNumOperands();
3171 unsigned NumElts = NumOps-2;
3172 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
3173 for (unsigned i = 0; i != NumElts; ++i) {
3174 SDOperand Elt = RHS.getOperand(i);
3175 if (!isa<ConstantSDNode>(Elt))
3176 return SDOperand();
3177 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
3178 IdxOps.push_back(DAG.getConstant(i, EVT));
3179 else if (cast<ConstantSDNode>(Elt)->isNullValue())
3180 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
3181 else
3182 return SDOperand();
3183 }
3184
3185 // Let's see if the target supports this vector_shuffle.
3186 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
3187 return SDOperand();
3188
3189 // Return the new VVECTOR_SHUFFLE node.
3190 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
3191 SDOperand EVTNode = DAG.getValueType(EVT);
3192 std::vector<SDOperand> Ops;
Chris Lattner516b9622006-09-14 20:50:57 +00003193 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode,
3194 EVTNode);
Evan Cheng44f1f092006-04-20 08:56:16 +00003195 Ops.push_back(LHS);
3196 AddToWorkList(LHS.Val);
3197 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
3198 ZeroOps.push_back(NumEltsNode);
3199 ZeroOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003200 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3201 &ZeroOps[0], ZeroOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003202 IdxOps.push_back(NumEltsNode);
3203 IdxOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003204 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3205 &IdxOps[0], IdxOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003206 Ops.push_back(NumEltsNode);
3207 Ops.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003208 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
3209 &Ops[0], Ops.size());
Evan Cheng44f1f092006-04-20 08:56:16 +00003210 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
3211 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
3212 DstVecSize, DstVecEVT);
3213 }
3214 return Result;
3215 }
3216 }
3217 return SDOperand();
3218}
3219
Chris Lattneredab1b92006-04-02 03:25:57 +00003220/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
3221/// the scalar operation of the vop if it is operating on an integer vector
3222/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
3223SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
3224 ISD::NodeType FPOp) {
3225 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
3226 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
3227 SDOperand LHS = N->getOperand(0);
3228 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00003229 SDOperand Shuffle = XformToShuffleWithZero(N);
3230 if (Shuffle.Val) return Shuffle;
3231
Chris Lattneredab1b92006-04-02 03:25:57 +00003232 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
3233 // this operation.
3234 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
3235 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003236 SmallVector<SDOperand, 8> Ops;
Chris Lattneredab1b92006-04-02 03:25:57 +00003237 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
3238 SDOperand LHSOp = LHS.getOperand(i);
3239 SDOperand RHSOp = RHS.getOperand(i);
3240 // If these two elements can't be folded, bail out.
3241 if ((LHSOp.getOpcode() != ISD::UNDEF &&
3242 LHSOp.getOpcode() != ISD::Constant &&
3243 LHSOp.getOpcode() != ISD::ConstantFP) ||
3244 (RHSOp.getOpcode() != ISD::UNDEF &&
3245 RHSOp.getOpcode() != ISD::Constant &&
3246 RHSOp.getOpcode() != ISD::ConstantFP))
3247 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00003248 // Can't fold divide by zero.
3249 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
3250 if ((RHSOp.getOpcode() == ISD::Constant &&
3251 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
3252 (RHSOp.getOpcode() == ISD::ConstantFP &&
3253 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
3254 break;
3255 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003256 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00003257 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00003258 assert((Ops.back().getOpcode() == ISD::UNDEF ||
3259 Ops.back().getOpcode() == ISD::Constant ||
3260 Ops.back().getOpcode() == ISD::ConstantFP) &&
3261 "Scalar binop didn't fold!");
3262 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003263
3264 if (Ops.size() == LHS.getNumOperands()-2) {
3265 Ops.push_back(*(LHS.Val->op_end()-2));
3266 Ops.push_back(*(LHS.Val->op_end()-1));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003267 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003268 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003269 }
3270
3271 return SDOperand();
3272}
3273
Nate Begeman44728a72005-09-19 22:34:01 +00003274SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00003275 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
3276
3277 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
3278 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3279 // If we got a simplified select_cc node back from SimplifySelectCC, then
3280 // break it down into a new SETCC node, and a new SELECT node, and then return
3281 // the SELECT node, since we were called with a SELECT node.
3282 if (SCC.Val) {
3283 // Check to see if we got a select_cc back (to turn into setcc/select).
3284 // Otherwise, just return whatever node we got back, like fabs.
3285 if (SCC.getOpcode() == ISD::SELECT_CC) {
3286 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
3287 SCC.getOperand(0), SCC.getOperand(1),
3288 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00003289 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003290 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
3291 SCC.getOperand(3), SETCC);
3292 }
3293 return SCC;
3294 }
Nate Begeman44728a72005-09-19 22:34:01 +00003295 return SDOperand();
3296}
3297
Chris Lattner40c62d52005-10-18 06:04:22 +00003298/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
3299/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00003300/// select. Callers of this should assume that TheSelect is deleted if this
3301/// returns true. As such, they should return the appropriate thing (e.g. the
3302/// node) back to the top-level of the DAG combiner loop to avoid it being
3303/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00003304///
3305bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
3306 SDOperand RHS) {
3307
3308 // If this is a select from two identical things, try to pull the operation
3309 // through the select.
3310 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00003311 // If this is a load and the token chain is identical, replace the select
3312 // of two loads with a load through a select of the address to load from.
3313 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
3314 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00003315 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00003316 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00003317 LHS.getOperand(0) == RHS.getOperand(0)) {
3318 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
3319 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
3320
3321 // If this is an EXTLOAD, the VT's must match.
Evan Cheng2e49f092006-10-11 07:10:22 +00003322 if (LLD->getLoadedVT() == RLD->getLoadedVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003323 // FIXME: this conflates two src values, discarding one. This is not
3324 // the right thing to do, but nothing uses srcvalues now. When they do,
3325 // turn SrcValue into a list of locations.
3326 SDOperand Addr;
3327 if (TheSelect->getOpcode() == ISD::SELECT)
3328 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
3329 TheSelect->getOperand(0), LLD->getBasePtr(),
3330 RLD->getBasePtr());
3331 else
3332 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
3333 TheSelect->getOperand(0),
3334 TheSelect->getOperand(1),
3335 LLD->getBasePtr(), RLD->getBasePtr(),
3336 TheSelect->getOperand(4));
Chris Lattner40c62d52005-10-18 06:04:22 +00003337
Evan Cheng466685d2006-10-09 20:57:25 +00003338 SDOperand Load;
3339 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
3340 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
3341 Addr,LLD->getSrcValue(), LLD->getSrcValueOffset());
3342 else {
3343 Load = DAG.getExtLoad(LLD->getExtensionType(),
3344 TheSelect->getValueType(0),
3345 LLD->getChain(), Addr, LLD->getSrcValue(),
3346 LLD->getSrcValueOffset(),
Evan Cheng2e49f092006-10-11 07:10:22 +00003347 LLD->getLoadedVT());
Evan Cheng466685d2006-10-09 20:57:25 +00003348 }
3349 // Users of the select now use the result of the load.
3350 CombineTo(TheSelect, Load);
3351
3352 // Users of the old loads now use the new load's chain. We know the
3353 // old-load value is dead now.
3354 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
3355 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
3356 return true;
Evan Chengc5484282006-10-04 00:56:09 +00003357 }
Chris Lattner40c62d52005-10-18 06:04:22 +00003358 }
3359 }
3360
3361 return false;
3362}
3363
Nate Begeman44728a72005-09-19 22:34:01 +00003364SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
3365 SDOperand N2, SDOperand N3,
3366 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00003367
3368 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00003369 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
3370 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
3371 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
3372
3373 // Determine if the condition we're dealing with is constant
3374 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
3375 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
3376
3377 // fold select_cc true, x, y -> x
3378 if (SCCC && SCCC->getValue())
3379 return N2;
3380 // fold select_cc false, x, y -> y
3381 if (SCCC && SCCC->getValue() == 0)
3382 return N3;
3383
3384 // Check to see if we can simplify the select into an fabs node
3385 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
3386 // Allow either -0.0 or 0.0
3387 if (CFP->getValue() == 0.0) {
3388 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
3389 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
3390 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
3391 N2 == N3.getOperand(0))
3392 return DAG.getNode(ISD::FABS, VT, N0);
3393
3394 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
3395 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3396 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3397 N2.getOperand(0) == N3)
3398 return DAG.getNode(ISD::FABS, VT, N3);
3399 }
3400 }
3401
3402 // Check to see if we can perform the "gzip trick", transforming
3403 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00003404 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00003405 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00003406 MVT::isInteger(N2.getValueType()) &&
3407 (N1C->isNullValue() || // (a < 0) ? b : 0
3408 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00003409 MVT::ValueType XType = N0.getValueType();
3410 MVT::ValueType AType = N2.getValueType();
3411 if (XType >= AType) {
3412 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00003413 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00003414 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3415 unsigned ShCtV = Log2_64(N2C->getValue());
3416 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3417 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3418 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00003419 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003420 if (XType > AType) {
3421 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003422 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003423 }
3424 return DAG.getNode(ISD::AND, AType, Shift, N2);
3425 }
3426 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3427 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3428 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003429 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003430 if (XType > AType) {
3431 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003432 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003433 }
3434 return DAG.getNode(ISD::AND, AType, Shift, N2);
3435 }
3436 }
Nate Begeman07ed4172005-10-10 21:26:48 +00003437
3438 // fold select C, 16, 0 -> shl C, 4
3439 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3440 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3441 // Get a SetCC of the condition
3442 // FIXME: Should probably make sure that setcc is legal if we ever have a
3443 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00003444 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00003445 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00003446 if (AfterLegalize) {
3447 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003448 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00003449 } else {
3450 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003451 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00003452 }
Chris Lattner5750df92006-03-01 04:03:14 +00003453 AddToWorkList(SCC.Val);
3454 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00003455 // shl setcc result by log2 n2c
3456 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3457 DAG.getConstant(Log2_64(N2C->getValue()),
3458 TLI.getShiftAmountTy()));
3459 }
3460
Nate Begemanf845b452005-10-08 00:29:44 +00003461 // Check to see if this is the equivalent of setcc
3462 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3463 // otherwise, go ahead with the folds.
3464 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3465 MVT::ValueType XType = N0.getValueType();
3466 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3467 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3468 if (Res.getValueType() != VT)
3469 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3470 return Res;
3471 }
3472
3473 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3474 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3475 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3476 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3477 return DAG.getNode(ISD::SRL, XType, Ctlz,
3478 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3479 TLI.getShiftAmountTy()));
3480 }
3481 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3482 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3483 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3484 N0);
3485 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3486 DAG.getConstant(~0ULL, XType));
3487 return DAG.getNode(ISD::SRL, XType,
3488 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3489 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3490 TLI.getShiftAmountTy()));
3491 }
3492 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3493 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3494 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3495 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3496 TLI.getShiftAmountTy()));
3497 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3498 }
3499 }
3500
3501 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3502 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3503 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3504 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3505 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3506 MVT::ValueType XType = N0.getValueType();
3507 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3508 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3509 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3510 TLI.getShiftAmountTy()));
3511 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003512 AddToWorkList(Shift.Val);
3513 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003514 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3515 }
3516 }
3517 }
3518
Nate Begeman44728a72005-09-19 22:34:01 +00003519 return SDOperand();
3520}
3521
Nate Begeman452d7be2005-09-16 00:54:12 +00003522SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00003523 SDOperand N1, ISD::CondCode Cond,
3524 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003525 // These setcc operations always fold.
3526 switch (Cond) {
3527 default: break;
3528 case ISD::SETFALSE:
3529 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3530 case ISD::SETTRUE:
3531 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3532 }
3533
3534 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3535 uint64_t C1 = N1C->getValue();
3536 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
3537 uint64_t C0 = N0C->getValue();
3538
3539 // Sign extend the operands if required
3540 if (ISD::isSignedIntSetCC(Cond)) {
3541 C0 = N0C->getSignExtended();
3542 C1 = N1C->getSignExtended();
3543 }
3544
3545 switch (Cond) {
3546 default: assert(0 && "Unknown integer setcc!");
3547 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3548 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3549 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
3550 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
3551 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
3552 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
3553 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
3554 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
3555 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
3556 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
3557 }
3558 } else {
Chris Lattner5f42a242006-09-20 06:19:26 +00003559 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
3560 // equality comparison, then we're just comparing whether X itself is
3561 // zero.
3562 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
3563 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
3564 N0.getOperand(1).getOpcode() == ISD::Constant) {
3565 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
3566 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3567 ShAmt == Log2_32(MVT::getSizeInBits(N0.getValueType()))) {
3568 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
3569 // (srl (ctlz x), 5) == 0 -> X != 0
3570 // (srl (ctlz x), 5) != 1 -> X != 0
3571 Cond = ISD::SETNE;
3572 } else {
3573 // (srl (ctlz x), 5) != 0 -> X == 0
3574 // (srl (ctlz x), 5) == 1 -> X == 0
3575 Cond = ISD::SETEQ;
3576 }
3577 SDOperand Zero = DAG.getConstant(0, N0.getValueType());
3578 return DAG.getSetCC(VT, N0.getOperand(0).getOperand(0),
3579 Zero, Cond);
3580 }
3581 }
3582
Nate Begeman452d7be2005-09-16 00:54:12 +00003583 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3584 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3585 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3586
3587 // If the comparison constant has bits in the upper part, the
3588 // zero-extended value could never match.
3589 if (C1 & (~0ULL << InSize)) {
3590 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3591 switch (Cond) {
3592 case ISD::SETUGT:
3593 case ISD::SETUGE:
3594 case ISD::SETEQ: return DAG.getConstant(0, VT);
3595 case ISD::SETULT:
3596 case ISD::SETULE:
3597 case ISD::SETNE: return DAG.getConstant(1, VT);
3598 case ISD::SETGT:
3599 case ISD::SETGE:
3600 // True if the sign bit of C1 is set.
3601 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3602 case ISD::SETLT:
3603 case ISD::SETLE:
3604 // True if the sign bit of C1 isn't set.
3605 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3606 default:
3607 break;
3608 }
3609 }
3610
3611 // Otherwise, we can perform the comparison with the low bits.
3612 switch (Cond) {
3613 case ISD::SETEQ:
3614 case ISD::SETNE:
3615 case ISD::SETUGT:
3616 case ISD::SETUGE:
3617 case ISD::SETULT:
3618 case ISD::SETULE:
3619 return DAG.getSetCC(VT, N0.getOperand(0),
3620 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3621 Cond);
3622 default:
3623 break; // todo, be more careful with signed comparisons
3624 }
3625 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3626 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3627 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3628 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3629 MVT::ValueType ExtDstTy = N0.getValueType();
3630 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3631
3632 // If the extended part has any inconsistent bits, it cannot ever
3633 // compare equal. In other words, they have to be all ones or all
3634 // zeros.
3635 uint64_t ExtBits =
3636 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3637 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3638 return DAG.getConstant(Cond == ISD::SETNE, VT);
3639
3640 SDOperand ZextOp;
3641 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3642 if (Op0Ty == ExtSrcTy) {
3643 ZextOp = N0.getOperand(0);
3644 } else {
3645 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3646 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3647 DAG.getConstant(Imm, Op0Ty));
3648 }
Chris Lattner5750df92006-03-01 04:03:14 +00003649 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003650 // Otherwise, make this a use of a zext.
3651 return DAG.getSetCC(VT, ZextOp,
3652 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3653 ExtDstTy),
3654 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00003655 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
3656 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3657 (N0.getOpcode() == ISD::XOR ||
3658 (N0.getOpcode() == ISD::AND &&
3659 N0.getOperand(0).getOpcode() == ISD::XOR &&
3660 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3661 isa<ConstantSDNode>(N0.getOperand(1)) &&
3662 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
3663 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
3664 // only do this if the top bits are known zero.
3665 if (TLI.MaskedValueIsZero(N1,
3666 MVT::getIntVTBitMask(N0.getValueType())-1)) {
3667 // Okay, get the un-inverted input value.
3668 SDOperand Val;
3669 if (N0.getOpcode() == ISD::XOR)
3670 Val = N0.getOperand(0);
3671 else {
3672 assert(N0.getOpcode() == ISD::AND &&
3673 N0.getOperand(0).getOpcode() == ISD::XOR);
3674 // ((X^1)&1)^1 -> X & 1
3675 Val = DAG.getNode(ISD::AND, N0.getValueType(),
3676 N0.getOperand(0).getOperand(0), N0.getOperand(1));
3677 }
3678 return DAG.getSetCC(VT, Val, N1,
3679 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
3680 }
Nate Begeman452d7be2005-09-16 00:54:12 +00003681 }
Chris Lattner5c46f742005-10-05 06:11:08 +00003682
Nate Begeman452d7be2005-09-16 00:54:12 +00003683 uint64_t MinVal, MaxVal;
3684 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
3685 if (ISD::isSignedIntSetCC(Cond)) {
3686 MinVal = 1ULL << (OperandBitSize-1);
3687 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
3688 MaxVal = ~0ULL >> (65-OperandBitSize);
3689 else
3690 MaxVal = 0;
3691 } else {
3692 MinVal = 0;
3693 MaxVal = ~0ULL >> (64-OperandBitSize);
3694 }
3695
3696 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
3697 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
3698 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
3699 --C1; // X >= C0 --> X > (C0-1)
3700 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3701 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
3702 }
3703
3704 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
3705 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
3706 ++C1; // X <= C0 --> X < (C0+1)
3707 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3708 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
3709 }
3710
3711 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
3712 return DAG.getConstant(0, VT); // X < MIN --> false
3713
3714 // Canonicalize setgt X, Min --> setne X, Min
3715 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
3716 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00003717 // Canonicalize setlt X, Max --> setne X, Max
3718 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
3719 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00003720
3721 // If we have setult X, 1, turn it into seteq X, 0
3722 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
3723 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
3724 ISD::SETEQ);
3725 // If we have setugt X, Max-1, turn it into seteq X, Max
3726 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
3727 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
3728 ISD::SETEQ);
3729
3730 // If we have "setcc X, C0", check to see if we can shrink the immediate
3731 // by changing cc.
3732
3733 // SETUGT X, SINTMAX -> SETLT X, 0
3734 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
3735 C1 == (~0ULL >> (65-OperandBitSize)))
3736 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
3737 ISD::SETLT);
3738
3739 // FIXME: Implement the rest of these.
3740
3741 // Fold bit comparisons when we can.
3742 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3743 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
3744 if (ConstantSDNode *AndRHS =
3745 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3746 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
3747 // Perform the xform if the AND RHS is a single bit.
3748 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
3749 return DAG.getNode(ISD::SRL, VT, N0,
3750 DAG.getConstant(Log2_64(AndRHS->getValue()),
3751 TLI.getShiftAmountTy()));
3752 }
3753 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
3754 // (X & 8) == 8 --> (X & 8) >> 3
3755 // Perform the xform if C1 is a single bit.
3756 if ((C1 & (C1-1)) == 0) {
3757 return DAG.getNode(ISD::SRL, VT, N0,
Chris Lattner729c6d12006-05-27 00:43:02 +00003758 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
Nate Begeman452d7be2005-09-16 00:54:12 +00003759 }
3760 }
3761 }
3762 }
3763 } else if (isa<ConstantSDNode>(N0.Val)) {
3764 // Ensure that the constant occurs on the RHS.
3765 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3766 }
3767
3768 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
3769 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
3770 double C0 = N0C->getValue(), C1 = N1C->getValue();
3771
3772 switch (Cond) {
3773 default: break; // FIXME: Implement the rest of these!
3774 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3775 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3776 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
3777 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
3778 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
3779 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
3780 }
3781 } else {
3782 // Ensure that the constant occurs on the RHS.
3783 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3784 }
3785
3786 if (N0 == N1) {
3787 // We can always fold X == Y for integer setcc's.
3788 if (MVT::isInteger(N0.getValueType()))
3789 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3790 unsigned UOF = ISD::getUnorderedFlavor(Cond);
3791 if (UOF == 2) // FP operators that are undefined on NaNs.
3792 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3793 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
3794 return DAG.getConstant(UOF, VT);
3795 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
3796 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00003797 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00003798 if (NewCond != Cond)
3799 return DAG.getSetCC(VT, N0, N1, NewCond);
3800 }
3801
3802 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3803 MVT::isInteger(N0.getValueType())) {
3804 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
3805 N0.getOpcode() == ISD::XOR) {
3806 // Simplify (X+Y) == (X+Z) --> Y == Z
3807 if (N0.getOpcode() == N1.getOpcode()) {
3808 if (N0.getOperand(0) == N1.getOperand(0))
3809 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
3810 if (N0.getOperand(1) == N1.getOperand(1))
3811 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
Evan Cheng1efba0e2006-08-29 06:42:35 +00003812 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003813 // If X op Y == Y op X, try other combinations.
3814 if (N0.getOperand(0) == N1.getOperand(1))
3815 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
3816 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00003817 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00003818 }
3819 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003820
3821 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
3822 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3823 // Turn (X+C1) == C2 --> X == C2-C1
3824 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
3825 return DAG.getSetCC(VT, N0.getOperand(0),
3826 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
3827 N0.getValueType()), Cond);
3828 }
3829
3830 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
3831 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00003832 // If we know that all of the inverted bits are zero, don't bother
3833 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003834 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00003835 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003836 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00003837 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003838 }
3839
3840 // Turn (C1-X) == C2 --> X == C1-C2
3841 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
3842 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
3843 return DAG.getSetCC(VT, N0.getOperand(1),
3844 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
3845 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00003846 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003847 }
3848 }
3849
Nate Begeman452d7be2005-09-16 00:54:12 +00003850 // Simplify (X+Z) == X --> Z == 0
3851 if (N0.getOperand(0) == N1)
3852 return DAG.getSetCC(VT, N0.getOperand(1),
3853 DAG.getConstant(0, N0.getValueType()), Cond);
3854 if (N0.getOperand(1) == N1) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00003855 if (DAG.isCommutativeBinOp(N0.getOpcode()))
Nate Begeman452d7be2005-09-16 00:54:12 +00003856 return DAG.getSetCC(VT, N0.getOperand(0),
3857 DAG.getConstant(0, N0.getValueType()), Cond);
3858 else {
3859 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
3860 // (Z-X) == X --> Z == X<<1
3861 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
3862 N1,
3863 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003864 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003865 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
3866 }
3867 }
3868 }
3869
3870 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3871 N1.getOpcode() == ISD::XOR) {
3872 // Simplify X == (X+Z) --> Z == 0
3873 if (N1.getOperand(0) == N0) {
3874 return DAG.getSetCC(VT, N1.getOperand(1),
3875 DAG.getConstant(0, N1.getValueType()), Cond);
3876 } else if (N1.getOperand(1) == N0) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00003877 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003878 return DAG.getSetCC(VT, N1.getOperand(0),
3879 DAG.getConstant(0, N1.getValueType()), Cond);
3880 } else {
3881 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3882 // X == (Z-X) --> X<<1 == Z
3883 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
3884 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003885 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003886 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
3887 }
3888 }
3889 }
3890 }
3891
3892 // Fold away ALL boolean setcc's.
3893 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00003894 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003895 switch (Cond) {
3896 default: assert(0 && "Unknown integer setcc!");
3897 case ISD::SETEQ: // X == Y -> (X^Y)^1
3898 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3899 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00003900 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003901 break;
3902 case ISD::SETNE: // X != Y --> (X^Y)
3903 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3904 break;
3905 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
3906 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
3907 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3908 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003909 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003910 break;
3911 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
3912 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
3913 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3914 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003915 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003916 break;
3917 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
3918 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
3919 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3920 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003921 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003922 break;
3923 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
3924 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
3925 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3926 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
3927 break;
3928 }
3929 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00003930 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003931 // FIXME: If running after legalize, we probably can't do this.
3932 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3933 }
3934 return N0;
3935 }
3936
3937 // Could not fold it.
3938 return SDOperand();
3939}
3940
Nate Begeman69575232005-10-20 02:15:44 +00003941/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3942/// return a DAG expression to select that will generate the same value by
3943/// multiplying by a magic number. See:
3944/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3945SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003946 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003947 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
3948
Andrew Lenharth232c9102006-06-12 16:07:18 +00003949 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003950 ii != ee; ++ii)
3951 AddToWorkList(*ii);
3952 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003953}
3954
3955/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3956/// return a DAG expression to select that will generate the same value by
3957/// multiplying by a magic number. See:
3958/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3959SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003960 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003961 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00003962
Andrew Lenharth232c9102006-06-12 16:07:18 +00003963 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003964 ii != ee; ++ii)
3965 AddToWorkList(*ii);
3966 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003967}
3968
Jim Laskey71382342006-10-07 23:37:56 +00003969/// FindBaseOffset - Return true if base is known not to alias with anything
3970/// but itself. Provides base object and offset as results.
3971static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
3972 // Assume it is a primitive operation.
3973 Base = Ptr; Offset = 0;
3974
3975 // If it's an adding a simple constant then integrate the offset.
3976 if (Base.getOpcode() == ISD::ADD) {
3977 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
3978 Base = Base.getOperand(0);
3979 Offset += C->getValue();
3980 }
3981 }
3982
3983 // If it's any of the following then it can't alias with anything but itself.
3984 return isa<FrameIndexSDNode>(Base) ||
3985 isa<ConstantPoolSDNode>(Base) ||
3986 isa<GlobalAddressSDNode>(Base);
3987}
3988
3989/// isAlias - Return true if there is any possibility that the two addresses
3990/// overlap.
Jim Laskey7ca56af2006-10-11 13:47:09 +00003991static bool isAlias(SDOperand Ptr1, int64_t Size1, const Value *SrcValue1,
3992 SDOperand Ptr2, int64_t Size2, const Value *SrcValue2) {
Jim Laskey71382342006-10-07 23:37:56 +00003993 // If they are the same then they must be aliases.
3994 if (Ptr1 == Ptr2) return true;
3995
3996 // Gather base node and offset information.
3997 SDOperand Base1, Base2;
3998 int64_t Offset1, Offset2;
3999 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
4000 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
4001
4002 // If they have a same base address then...
4003 if (Base1 == Base2) {
4004 // Check to see if the addresses overlap.
4005 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
4006 }
4007
4008 // Otherwise they alias if either is unknown.
4009 return !KnownBase1 || !KnownBase2;
4010}
4011
4012/// FindAliasInfo - Extracts the relevant alias information from the memory
4013/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00004014bool DAGCombiner::FindAliasInfo(SDNode *N,
4015 SDOperand &Ptr, int64_t &Size, const Value *&SrcValue) {
4016 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4017 Ptr = LD->getBasePtr();
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004018 Size = MVT::getSizeInBits(LD->getLoadedVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004019 SrcValue = LD->getSrcValue();
Jim Laskey71382342006-10-07 23:37:56 +00004020 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004021 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7aed46c2006-10-11 18:55:16 +00004022#if 1 // FIXME - Switch over after StoreSDNode comes online.
Jim Laskey7ca56af2006-10-11 13:47:09 +00004023 Ptr = ST->getOperand(2);
4024 Size = MVT::getSizeInBits(ST->getOperand(1).getValueType()) >> 3;
4025 SrcValue = 0;
4026#else
4027 Ptr = ST->getBasePtr();
4028 Size = MVT::getSizeInBits(ST->getOperand(1).getValueType()) >> 3;
4029 SrcValue = ST->getSrcValue();
4030#endif
Jim Laskey7aed46c2006-10-11 18:55:16 +00004031 // FIXME - Switch over after StoreSDNode comes online.
4032 } else if (N->getOpcode() == ISD::TRUNCSTORE) {
4033 Ptr = N->getOperand(2);
4034 Size = MVT::getSizeInBits(cast<VTSDNode>(N->getOperand(4))->getVT()) >> 3;
4035 SrcValue = 0;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004036 } else {
Jim Laskey71382342006-10-07 23:37:56 +00004037 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00004038 }
4039
4040 return false;
4041}
4042
Jim Laskey6ff23e52006-10-04 16:53:27 +00004043/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
4044/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00004045void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00004046 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004047 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00004048 std::set<SDNode *> Visited; // Visited node set.
4049
Jim Laskey279f0532006-09-25 16:29:54 +00004050 // Get alias information for node.
4051 SDOperand Ptr;
4052 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004053 const Value *SrcValue;
4054 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue);
Jim Laskey279f0532006-09-25 16:29:54 +00004055
Jim Laskey6ff23e52006-10-04 16:53:27 +00004056 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00004057 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00004058
Jim Laskeybc588b82006-10-05 15:07:25 +00004059 // Look at each chain and determine if it is an alias. If so, add it to the
4060 // aliases list. If not, then continue up the chain looking for the next
4061 // candidate.
4062 while (!Chains.empty()) {
4063 SDOperand Chain = Chains.back();
4064 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00004065
Jim Laskeybc588b82006-10-05 15:07:25 +00004066 // Don't bother if we've been before.
4067 if (Visited.find(Chain.Val) != Visited.end()) continue;
4068 Visited.insert(Chain.Val);
4069
4070 switch (Chain.getOpcode()) {
4071 case ISD::EntryToken:
4072 // Entry token is ideal chain operand, but handled in FindBetterChain.
4073 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00004074
Jim Laskeybc588b82006-10-05 15:07:25 +00004075 case ISD::LOAD:
Jim Laskey7aed46c2006-10-11 18:55:16 +00004076 // FIXME - Switch over after StoreSDNode comes online.
4077 case ISD::TRUNCSTORE:
Jim Laskeybc588b82006-10-05 15:07:25 +00004078 case ISD::STORE: {
4079 // Get alias information for Chain.
4080 SDOperand OpPtr;
4081 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004082 const Value *OpSrcValue;
4083 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize, OpSrcValue);
Jim Laskeybc588b82006-10-05 15:07:25 +00004084
4085 // If chain is alias then stop here.
4086 if (!(IsLoad && IsOpLoad) &&
4087 isAlias(Ptr, Size, SrcValue, OpPtr, OpSize, OpSrcValue)) {
4088 Aliases.push_back(Chain);
4089 } else {
4090 // Look further up the chain.
4091 Chains.push_back(Chain.getOperand(0));
4092 // Clean up old chain.
4093 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00004094 }
Jim Laskeybc588b82006-10-05 15:07:25 +00004095 break;
4096 }
4097
4098 case ISD::TokenFactor:
4099 // We have to check each of the operands of the token factor, so we queue
4100 // then up. Adding the operands to the queue (stack) in reverse order
4101 // maintains the original order and increases the likelihood that getNode
4102 // will find a matching token factor (CSE.)
4103 for (unsigned n = Chain.getNumOperands(); n;)
4104 Chains.push_back(Chain.getOperand(--n));
4105 // Eliminate the token factor if we can.
4106 AddToWorkList(Chain.Val);
4107 break;
4108
4109 default:
4110 // For all other instructions we will just have to take what we can get.
4111 Aliases.push_back(Chain);
4112 break;
Jim Laskey279f0532006-09-25 16:29:54 +00004113 }
4114 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004115}
4116
4117/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
4118/// for a better chain (aliasing node.)
4119SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
4120 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00004121
Jim Laskey6ff23e52006-10-04 16:53:27 +00004122 // Accumulate all the aliases to this node.
4123 GatherAllAliases(N, OldChain, Aliases);
4124
4125 if (Aliases.size() == 0) {
4126 // If no operands then chain to entry token.
4127 return DAG.getEntryNode();
4128 } else if (Aliases.size() == 1) {
4129 // If a single operand then chain to it. We don't need to revisit it.
4130 return Aliases[0];
4131 }
4132
4133 // Construct a custom tailored token factor.
4134 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4135 &Aliases[0], Aliases.size());
4136
4137 // Make sure the old chain gets cleaned up.
4138 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
4139
4140 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00004141}
4142
Nate Begeman1d4d4142005-09-01 00:19:25 +00004143// SelectionDAG::Combine - This is the entry point for the file.
4144//
Nate Begeman4ebd8052005-09-01 23:24:04 +00004145void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00004146 /// run - This is the main entry point to this class.
4147 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00004148 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004149}