blob: a27adba33b02e4a853b121aca410d711da85bc64 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
12//
13// FIXME: Missing folds
14// sdiv, udiv, srem, urem (X, const) where X is an integer can be expanded into
15// a sequence of multiplies, shifts, and adds. This should be controlled by
16// some kind of hint from the target that int div is expensive.
17// various folds of mulh[s,u] by constants such as -1, powers of 2, etc.
18//
Nate Begeman44728a72005-09-19 22:34:01 +000019// FIXME: select C, pow2, pow2 -> something smart
20// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
Nate Begeman44728a72005-09-19 22:34:01 +000021// FIXME: Dead stores -> nuke
Chris Lattner40c62d52005-10-18 06:04:22 +000022// FIXME: shr X, (and Y,31) -> shr X, Y (TRICKY!)
Nate Begeman1d4d4142005-09-01 00:19:25 +000023// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000024// FIXME: undef values
Nate Begeman646d7e22005-09-02 21:18:40 +000025// FIXME: divide by zero is currently left unfolded. do we want to turn this
26// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000027// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Nate Begeman1d4d4142005-09-01 00:19:25 +000028//
29//===----------------------------------------------------------------------===//
30
31#define DEBUG_TYPE "dagcombine"
32#include "llvm/ADT/Statistic.h"
Jim Laskeyc7c3f112006-10-16 20:52:31 +000033#include "llvm/Analysis/AliasAnalysis.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000034#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000035#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000036#include "llvm/Support/MathExtras.h"
37#include "llvm/Target/TargetLowering.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000038#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000039#include "llvm/Support/CommandLine.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000040#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000041#include <cmath>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000042#include <iostream>
Jim Laskey279f0532006-09-25 16:29:54 +000043#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000044using namespace llvm;
45
46namespace {
Andrew Lenharthae6153f2006-07-20 17:43:27 +000047 static Statistic<> NodesCombined ("dagcombiner",
48 "Number of dag nodes combined");
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000049
Jim Laskey71382342006-10-07 23:37:56 +000050 static cl::opt<bool>
51 CombinerAA("combiner-alias-analysis", cl::Hidden,
52 cl::desc("Turn on alias analysis turning testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000053
Jim Laskeybc588b82006-10-05 15:07:25 +000054//------------------------------ DAGCombiner ---------------------------------//
55
Jim Laskey71382342006-10-07 23:37:56 +000056 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000057 SelectionDAG &DAG;
58 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000059 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000060
61 // Worklist of all of the nodes that need to be simplified.
62 std::vector<SDNode*> WorkList;
63
Jim Laskeyc7c3f112006-10-16 20:52:31 +000064 // AA - Used for DAG load/store alias analysis.
65 AliasAnalysis &AA;
66
Nate Begeman1d4d4142005-09-01 00:19:25 +000067 /// AddUsersToWorkList - When an instruction is simplified, add all users of
68 /// the instruction to the work lists because they might get more simplified
69 /// now.
70 ///
71 void AddUsersToWorkList(SDNode *N) {
72 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000073 UI != UE; ++UI)
Jim Laskey6ff23e52006-10-04 16:53:27 +000074 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000075 }
76
77 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000078 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000079 void removeFromWorkList(SDNode *N) {
80 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
81 WorkList.end());
82 }
83
Chris Lattner24664722006-03-01 04:53:38 +000084 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000085 /// AddToWorkList - Add to the work list making sure it's instance is at the
86 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000087 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000088 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000089 WorkList.push_back(N);
90 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000091
Jim Laskey274062c2006-10-13 23:32:28 +000092 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
93 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +000094 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
Chris Lattner87514ca2005-10-10 22:31:19 +000095 ++NodesCombined;
Jim Laskey6ff23e52006-10-04 16:53:27 +000096 DEBUG(std::cerr << "\nReplacing.1 "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +000097 std::cerr << "\nWith: "; To[0].Val->dump(&DAG);
Chris Lattner3577e382006-08-11 17:56:38 +000098 std::cerr << " and " << NumTo-1 << " other values\n");
Chris Lattner01a22022005-10-10 22:04:48 +000099 std::vector<SDNode*> NowDead;
Chris Lattner3577e382006-08-11 17:56:38 +0000100 DAG.ReplaceAllUsesWith(N, To, &NowDead);
Chris Lattner01a22022005-10-10 22:04:48 +0000101
Jim Laskey274062c2006-10-13 23:32:28 +0000102 if (AddTo) {
103 // Push the new nodes and any users onto the worklist
104 for (unsigned i = 0, e = NumTo; i != e; ++i) {
105 AddToWorkList(To[i].Val);
106 AddUsersToWorkList(To[i].Val);
107 }
Chris Lattner01a22022005-10-10 22:04:48 +0000108 }
109
Jim Laskey6ff23e52006-10-04 16:53:27 +0000110 // Nodes can be reintroduced into the worklist. Make sure we do not
111 // process a node that has been replaced.
Chris Lattner01a22022005-10-10 22:04:48 +0000112 removeFromWorkList(N);
113 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
114 removeFromWorkList(NowDead[i]);
115
116 // Finally, since the node is now dead, remove it from the graph.
117 DAG.DeleteNode(N);
118 return SDOperand(N, 0);
119 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000120
Jim Laskey274062c2006-10-13 23:32:28 +0000121 SDOperand CombineTo(SDNode *N, SDOperand Res, bool AddTo = true) {
122 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000123 }
124
Jim Laskey274062c2006-10-13 23:32:28 +0000125 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1,
126 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000127 SDOperand To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000128 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000129 }
130 private:
131
Chris Lattner012f2412006-02-17 21:58:01 +0000132 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000133 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000134 /// propagation. If so, return true.
135 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000136 TargetLowering::TargetLoweringOpt TLO(DAG);
137 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000138 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
139 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
140 return false;
141
142 // Revisit the node.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000143 AddToWorkList(Op.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000144
145 // Replace the old value with the new one.
146 ++NodesCombined;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000147 DEBUG(std::cerr << "\nReplacing.2 "; TLO.Old.Val->dump();
Jim Laskey279f0532006-09-25 16:29:54 +0000148 std::cerr << "\nWith: "; TLO.New.Val->dump(&DAG);
149 std::cerr << '\n');
Chris Lattner012f2412006-02-17 21:58:01 +0000150
151 std::vector<SDNode*> NowDead;
152 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
153
Chris Lattner7d20d392006-02-20 06:51:04 +0000154 // Push the new node and any (possibly new) users onto the worklist.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000155 AddToWorkList(TLO.New.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000156 AddUsersToWorkList(TLO.New.Val);
157
158 // Nodes can end up on the worklist more than once. Make sure we do
159 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000160 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
161 removeFromWorkList(NowDead[i]);
162
Chris Lattner7d20d392006-02-20 06:51:04 +0000163 // Finally, if the node is now dead, remove it from the graph. The node
164 // may not be dead if the replacement process recursively simplified to
165 // something else needing this node.
166 if (TLO.Old.Val->use_empty()) {
167 removeFromWorkList(TLO.Old.Val);
168 DAG.DeleteNode(TLO.Old.Val);
169 }
Chris Lattner012f2412006-02-17 21:58:01 +0000170 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000171 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000172
Nate Begeman1d4d4142005-09-01 00:19:25 +0000173 /// visit - call the node-specific routine that knows how to fold each
174 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000175 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000176
177 // Visitation implementation - Implement dag node combining for different
178 // node types. The semantics are as follows:
179 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000180 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000181 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000182 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000183 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000184 SDOperand visitTokenFactor(SDNode *N);
185 SDOperand visitADD(SDNode *N);
186 SDOperand visitSUB(SDNode *N);
187 SDOperand visitMUL(SDNode *N);
188 SDOperand visitSDIV(SDNode *N);
189 SDOperand visitUDIV(SDNode *N);
190 SDOperand visitSREM(SDNode *N);
191 SDOperand visitUREM(SDNode *N);
192 SDOperand visitMULHU(SDNode *N);
193 SDOperand visitMULHS(SDNode *N);
194 SDOperand visitAND(SDNode *N);
195 SDOperand visitOR(SDNode *N);
196 SDOperand visitXOR(SDNode *N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000197 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000198 SDOperand visitSHL(SDNode *N);
199 SDOperand visitSRA(SDNode *N);
200 SDOperand visitSRL(SDNode *N);
201 SDOperand visitCTLZ(SDNode *N);
202 SDOperand visitCTTZ(SDNode *N);
203 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000204 SDOperand visitSELECT(SDNode *N);
205 SDOperand visitSELECT_CC(SDNode *N);
206 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000207 SDOperand visitSIGN_EXTEND(SDNode *N);
208 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000209 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000210 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
211 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000212 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000213 SDOperand visitVBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000214 SDOperand visitFADD(SDNode *N);
215 SDOperand visitFSUB(SDNode *N);
216 SDOperand visitFMUL(SDNode *N);
217 SDOperand visitFDIV(SDNode *N);
218 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000219 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000220 SDOperand visitSINT_TO_FP(SDNode *N);
221 SDOperand visitUINT_TO_FP(SDNode *N);
222 SDOperand visitFP_TO_SINT(SDNode *N);
223 SDOperand visitFP_TO_UINT(SDNode *N);
224 SDOperand visitFP_ROUND(SDNode *N);
225 SDOperand visitFP_ROUND_INREG(SDNode *N);
226 SDOperand visitFP_EXTEND(SDNode *N);
227 SDOperand visitFNEG(SDNode *N);
228 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000229 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000230 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000231 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000232 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000233 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
234 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000235 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000236 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000237 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000238
Evan Cheng44f1f092006-04-20 08:56:16 +0000239 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000240 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
241
Chris Lattner40c62d52005-10-18 06:04:22 +0000242 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000243 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000244 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
245 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
246 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000247 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000248 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000249 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000250 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000251 SDOperand BuildUDIV(SDNode *N);
252 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Jim Laskey279f0532006-09-25 16:29:54 +0000253
Jim Laskey6ff23e52006-10-04 16:53:27 +0000254 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
255 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000256 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000257 SmallVector<SDOperand, 8> &Aliases);
258
Jim Laskey7ca56af2006-10-11 13:47:09 +0000259 /// FindAliasInfo - Extracts the relevant alias information from the memory
260 /// node. Returns true if the operand was a load.
261 bool FindAliasInfo(SDNode *N,
262 SDOperand &Ptr, int64_t &Size, const Value *&SrcValue);
263
Jim Laskey279f0532006-09-25 16:29:54 +0000264 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000265 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000266 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
267
Nate Begeman1d4d4142005-09-01 00:19:25 +0000268public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000269 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
270 : DAG(D),
271 TLI(D.getTargetLoweringInfo()),
272 AfterLegalize(false),
273 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000274
275 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000276 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000277 };
278}
279
Chris Lattner24664722006-03-01 04:53:38 +0000280//===----------------------------------------------------------------------===//
281// TargetLowering::DAGCombinerInfo implementation
282//===----------------------------------------------------------------------===//
283
284void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
285 ((DAGCombiner*)DC)->AddToWorkList(N);
286}
287
288SDOperand TargetLowering::DAGCombinerInfo::
289CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000290 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000291}
292
293SDOperand TargetLowering::DAGCombinerInfo::
294CombineTo(SDNode *N, SDOperand Res) {
295 return ((DAGCombiner*)DC)->CombineTo(N, Res);
296}
297
298
299SDOperand TargetLowering::DAGCombinerInfo::
300CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
301 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
302}
303
304
305
306
307//===----------------------------------------------------------------------===//
308
309
Nate Begeman4ebd8052005-09-01 23:24:04 +0000310// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
311// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000312// Also, set the incoming LHS, RHS, and CC references to the appropriate
313// nodes based on the type of node we are checking. This simplifies life a
314// bit for the callers.
315static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
316 SDOperand &CC) {
317 if (N.getOpcode() == ISD::SETCC) {
318 LHS = N.getOperand(0);
319 RHS = N.getOperand(1);
320 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000321 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000322 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000323 if (N.getOpcode() == ISD::SELECT_CC &&
324 N.getOperand(2).getOpcode() == ISD::Constant &&
325 N.getOperand(3).getOpcode() == ISD::Constant &&
326 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000327 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
328 LHS = N.getOperand(0);
329 RHS = N.getOperand(1);
330 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000331 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000332 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000333 return false;
334}
335
Nate Begeman99801192005-09-07 23:25:52 +0000336// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
337// one use. If this is true, it allows the users to invert the operation for
338// free when it is profitable to do so.
339static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000340 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000341 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000342 return true;
343 return false;
344}
345
Nate Begemancd4d58c2006-02-03 06:46:56 +0000346SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
347 MVT::ValueType VT = N0.getValueType();
348 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
349 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
350 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
351 if (isa<ConstantSDNode>(N1)) {
352 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000353 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000354 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
355 } else if (N0.hasOneUse()) {
356 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000357 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000358 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
359 }
360 }
361 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
362 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
363 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
364 if (isa<ConstantSDNode>(N0)) {
365 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000366 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000367 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
368 } else if (N1.hasOneUse()) {
369 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000370 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000371 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
372 }
373 }
374 return SDOperand();
375}
376
Nate Begeman4ebd8052005-09-01 23:24:04 +0000377void DAGCombiner::Run(bool RunningAfterLegalize) {
378 // set the instance variable, so that the various visit routines may use it.
379 AfterLegalize = RunningAfterLegalize;
380
Nate Begeman646d7e22005-09-02 21:18:40 +0000381 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000382 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
383 E = DAG.allnodes_end(); I != E; ++I)
384 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000385
Chris Lattner95038592005-10-05 06:35:28 +0000386 // Create a dummy node (which is not added to allnodes), that adds a reference
387 // to the root node, preventing it from being deleted, and tracking any
388 // changes of the root.
389 HandleSDNode Dummy(DAG.getRoot());
390
Chris Lattner24664722006-03-01 04:53:38 +0000391
392 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
393 TargetLowering::DAGCombinerInfo
394 DagCombineInfo(DAG, !RunningAfterLegalize, this);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000395
Nate Begeman1d4d4142005-09-01 00:19:25 +0000396 // while the worklist isn't empty, inspect the node on the end of it and
397 // try and combine it.
398 while (!WorkList.empty()) {
399 SDNode *N = WorkList.back();
400 WorkList.pop_back();
401
402 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000403 // N is deleted from the DAG, since they too may now be dead or may have a
404 // reduced number of uses, allowing other xforms.
405 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000406 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000407 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000408
Chris Lattner95038592005-10-05 06:35:28 +0000409 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000410 continue;
411 }
412
Nate Begeman83e75ec2005-09-06 04:43:02 +0000413 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000414
415 // If nothing happened, try a target-specific DAG combine.
416 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000417 assert(N->getOpcode() != ISD::DELETED_NODE &&
418 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000419 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
420 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
421 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
422 }
423
Nate Begeman83e75ec2005-09-06 04:43:02 +0000424 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000425 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000426 // If we get back the same node we passed in, rather than a new node or
427 // zero, we know that the node must have defined multiple values and
428 // CombineTo was used. Since CombineTo takes care of the worklist
429 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000430 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000431 assert(N->getOpcode() != ISD::DELETED_NODE &&
432 RV.Val->getOpcode() != ISD::DELETED_NODE &&
433 "Node was deleted but visit returned new node!");
434
Jim Laskey6ff23e52006-10-04 16:53:27 +0000435 DEBUG(std::cerr << "\nReplacing.3 "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000436 std::cerr << "\nWith: "; RV.Val->dump(&DAG);
Nate Begeman2300f552005-09-07 00:15:36 +0000437 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000438 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000439 if (N->getNumValues() == RV.Val->getNumValues())
440 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
441 else {
442 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
443 SDOperand OpV = RV;
444 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
445 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000446
447 // Push the new node and any users onto the worklist
Jim Laskey6ff23e52006-10-04 16:53:27 +0000448 AddToWorkList(RV.Val);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000449 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000450
Jim Laskey6ff23e52006-10-04 16:53:27 +0000451 // Nodes can be reintroduced into the worklist. Make sure we do not
452 // process a node that has been replaced.
Nate Begeman646d7e22005-09-02 21:18:40 +0000453 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000454 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
455 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000456
457 // Finally, since the node is now dead, remove it from the graph.
458 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000459 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000460 }
461 }
Chris Lattner95038592005-10-05 06:35:28 +0000462
463 // If the root changed (e.g. it was a dead load, update the root).
464 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000465}
466
Nate Begeman83e75ec2005-09-06 04:43:02 +0000467SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000468 switch(N->getOpcode()) {
469 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000470 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000471 case ISD::ADD: return visitADD(N);
472 case ISD::SUB: return visitSUB(N);
473 case ISD::MUL: return visitMUL(N);
474 case ISD::SDIV: return visitSDIV(N);
475 case ISD::UDIV: return visitUDIV(N);
476 case ISD::SREM: return visitSREM(N);
477 case ISD::UREM: return visitUREM(N);
478 case ISD::MULHU: return visitMULHU(N);
479 case ISD::MULHS: return visitMULHS(N);
480 case ISD::AND: return visitAND(N);
481 case ISD::OR: return visitOR(N);
482 case ISD::XOR: return visitXOR(N);
483 case ISD::SHL: return visitSHL(N);
484 case ISD::SRA: return visitSRA(N);
485 case ISD::SRL: return visitSRL(N);
486 case ISD::CTLZ: return visitCTLZ(N);
487 case ISD::CTTZ: return visitCTTZ(N);
488 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000489 case ISD::SELECT: return visitSELECT(N);
490 case ISD::SELECT_CC: return visitSELECT_CC(N);
491 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000492 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
493 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000494 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000495 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
496 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000497 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000498 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000499 case ISD::FADD: return visitFADD(N);
500 case ISD::FSUB: return visitFSUB(N);
501 case ISD::FMUL: return visitFMUL(N);
502 case ISD::FDIV: return visitFDIV(N);
503 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000504 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000505 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
506 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
507 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
508 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
509 case ISD::FP_ROUND: return visitFP_ROUND(N);
510 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
511 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
512 case ISD::FNEG: return visitFNEG(N);
513 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000514 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000515 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000516 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000517 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000518 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
519 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000520 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000521 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000522 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000523 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
524 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
525 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
526 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
527 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
528 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
529 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
530 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000531 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000532 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000533}
534
Chris Lattner6270f682006-10-08 22:57:01 +0000535/// getInputChainForNode - Given a node, return its input chain if it has one,
536/// otherwise return a null sd operand.
537static SDOperand getInputChainForNode(SDNode *N) {
538 if (unsigned NumOps = N->getNumOperands()) {
539 if (N->getOperand(0).getValueType() == MVT::Other)
540 return N->getOperand(0);
541 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
542 return N->getOperand(NumOps-1);
543 for (unsigned i = 1; i < NumOps-1; ++i)
544 if (N->getOperand(i).getValueType() == MVT::Other)
545 return N->getOperand(i);
546 }
547 return SDOperand(0, 0);
548}
549
Nate Begeman83e75ec2005-09-06 04:43:02 +0000550SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000551 // If N has two operands, where one has an input chain equal to the other,
552 // the 'other' chain is redundant.
553 if (N->getNumOperands() == 2) {
554 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
555 return N->getOperand(0);
556 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
557 return N->getOperand(1);
558 }
559
560
Jim Laskey6ff23e52006-10-04 16:53:27 +0000561 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Jim Laskey279f0532006-09-25 16:29:54 +0000562 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000563 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000564
565 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000566 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000567
Jim Laskey71382342006-10-07 23:37:56 +0000568 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000569 // encountered.
570 for (unsigned i = 0; i < TFs.size(); ++i) {
571 SDNode *TF = TFs[i];
572
Jim Laskey6ff23e52006-10-04 16:53:27 +0000573 // Check each of the operands.
574 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
575 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000576
Jim Laskey6ff23e52006-10-04 16:53:27 +0000577 switch (Op.getOpcode()) {
578 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000579 // Entry tokens don't need to be added to the list. They are
580 // rededundant.
581 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000582 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000583
Jim Laskey6ff23e52006-10-04 16:53:27 +0000584 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000585 if ((CombinerAA || Op.hasOneUse()) &&
586 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000587 // Queue up for processing.
588 TFs.push_back(Op.Val);
589 // Clean up in case the token factor is removed.
590 AddToWorkList(Op.Val);
591 Changed = true;
592 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000593 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000594 // Fall thru
595
596 default:
Jim Laskeybc588b82006-10-05 15:07:25 +0000597 // Only add if not there prior.
598 if (std::find(Ops.begin(), Ops.end(), Op) == Ops.end())
599 Ops.push_back(Op);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000600 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000601 }
602 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000603 }
604
605 SDOperand Result;
606
607 // If we've change things around then replace token factor.
608 if (Changed) {
609 if (Ops.size() == 0) {
610 // The entry token is the only possible outcome.
611 Result = DAG.getEntryNode();
612 } else {
613 // New and improved token factor.
614 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000615 }
Jim Laskey274062c2006-10-13 23:32:28 +0000616
617 // Don't add users to work list.
618 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000619 }
Jim Laskey279f0532006-09-25 16:29:54 +0000620
Jim Laskey6ff23e52006-10-04 16:53:27 +0000621 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000622}
623
Nate Begeman83e75ec2005-09-06 04:43:02 +0000624SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000625 SDOperand N0 = N->getOperand(0);
626 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000627 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
628 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000629 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000630
631 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000632 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000633 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000634 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000635 if (N0C && !N1C)
636 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000637 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000638 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000639 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000640 // fold ((c1-A)+c2) -> (c1+c2)-A
641 if (N1C && N0.getOpcode() == ISD::SUB)
642 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
643 return DAG.getNode(ISD::SUB, VT,
644 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
645 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000646 // reassociate add
647 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
648 if (RADD.Val != 0)
649 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000650 // fold ((0-A) + B) -> B-A
651 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
652 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000653 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000654 // fold (A + (0-B)) -> A-B
655 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
656 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000657 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000658 // fold (A+(B-A)) -> B
659 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000660 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000661
Evan Cheng860771d2006-03-01 01:09:54 +0000662 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000663 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000664
665 // fold (a+b) -> (a|b) iff a and b share no bits.
666 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
667 uint64_t LHSZero, LHSOne;
668 uint64_t RHSZero, RHSOne;
669 uint64_t Mask = MVT::getIntVTBitMask(VT);
670 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
671 if (LHSZero) {
672 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
673
674 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
675 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
676 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
677 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
678 return DAG.getNode(ISD::OR, VT, N0, N1);
679 }
680 }
681
Nate Begeman83e75ec2005-09-06 04:43:02 +0000682 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000683}
684
Nate Begeman83e75ec2005-09-06 04:43:02 +0000685SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000686 SDOperand N0 = N->getOperand(0);
687 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000688 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
689 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000690 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000691
Chris Lattner854077d2005-10-17 01:07:11 +0000692 // fold (sub x, x) -> 0
693 if (N0 == N1)
694 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000695 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000696 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000697 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000698 // fold (sub x, c) -> (add x, -c)
699 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000700 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000701 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000702 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000703 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000704 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000705 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000706 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000707 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000708}
709
Nate Begeman83e75ec2005-09-06 04:43:02 +0000710SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000711 SDOperand N0 = N->getOperand(0);
712 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000713 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
714 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000715 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000716
717 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000718 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000719 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000720 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000721 if (N0C && !N1C)
722 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000723 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000724 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000725 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000726 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000727 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000728 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000729 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000730 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000731 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000732 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000733 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000734 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
735 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
736 // FIXME: If the input is something that is easily negated (e.g. a
737 // single-use add), we should put the negate there.
738 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
739 DAG.getNode(ISD::SHL, VT, N0,
740 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
741 TLI.getShiftAmountTy())));
742 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +0000743
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000744 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
745 if (N1C && N0.getOpcode() == ISD::SHL &&
746 isa<ConstantSDNode>(N0.getOperand(1))) {
747 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000748 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000749 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
750 }
751
752 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
753 // use.
754 {
755 SDOperand Sh(0,0), Y(0,0);
756 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
757 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
758 N0.Val->hasOneUse()) {
759 Sh = N0; Y = N1;
760 } else if (N1.getOpcode() == ISD::SHL &&
761 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
762 Sh = N1; Y = N0;
763 }
764 if (Sh.Val) {
765 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
766 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
767 }
768 }
Chris Lattnera1deca32006-03-04 23:33:26 +0000769 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
770 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
771 isa<ConstantSDNode>(N0.getOperand(1))) {
772 return DAG.getNode(ISD::ADD, VT,
773 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
774 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
775 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000776
Nate Begemancd4d58c2006-02-03 06:46:56 +0000777 // reassociate mul
778 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
779 if (RMUL.Val != 0)
780 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000781 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000782}
783
Nate Begeman83e75ec2005-09-06 04:43:02 +0000784SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000785 SDOperand N0 = N->getOperand(0);
786 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000787 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
788 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000789 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000790
791 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000792 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000793 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000794 // fold (sdiv X, 1) -> X
795 if (N1C && N1C->getSignExtended() == 1LL)
796 return N0;
797 // fold (sdiv X, -1) -> 0-X
798 if (N1C && N1C->isAllOnesValue())
799 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000800 // If we know the sign bits of both operands are zero, strength reduce to a
801 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
802 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000803 if (TLI.MaskedValueIsZero(N1, SignBit) &&
804 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000805 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000806 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000807 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000808 (isPowerOf2_64(N1C->getSignExtended()) ||
809 isPowerOf2_64(-N1C->getSignExtended()))) {
810 // If dividing by powers of two is cheap, then don't perform the following
811 // fold.
812 if (TLI.isPow2DivCheap())
813 return SDOperand();
814 int64_t pow2 = N1C->getSignExtended();
815 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000816 unsigned lg2 = Log2_64(abs2);
817 // Splat the sign bit into the register
818 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000819 DAG.getConstant(MVT::getSizeInBits(VT)-1,
820 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +0000821 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000822 // Add (N0 < 0) ? abs2 - 1 : 0;
823 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
824 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000825 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000826 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +0000827 AddToWorkList(SRL.Val);
828 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +0000829 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
830 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000831 // If we're dividing by a positive value, we're done. Otherwise, we must
832 // negate the result.
833 if (pow2 > 0)
834 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +0000835 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000836 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
837 }
Nate Begeman69575232005-10-20 02:15:44 +0000838 // if integer divide is expensive and we satisfy the requirements, emit an
839 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000840 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000841 !TLI.isIntDivCheap()) {
842 SDOperand Op = BuildSDIV(N);
843 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000844 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000845 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000846}
847
Nate Begeman83e75ec2005-09-06 04:43:02 +0000848SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000849 SDOperand N0 = N->getOperand(0);
850 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000851 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
852 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000853 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000854
855 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000856 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000857 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000858 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000859 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000860 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000861 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000862 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000863 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
864 if (N1.getOpcode() == ISD::SHL) {
865 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
866 if (isPowerOf2_64(SHC->getValue())) {
867 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000868 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
869 DAG.getConstant(Log2_64(SHC->getValue()),
870 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +0000871 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000872 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000873 }
874 }
875 }
Nate Begeman69575232005-10-20 02:15:44 +0000876 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000877 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
878 SDOperand Op = BuildUDIV(N);
879 if (Op.Val) return Op;
880 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000881 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000882}
883
Nate Begeman83e75ec2005-09-06 04:43:02 +0000884SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000885 SDOperand N0 = N->getOperand(0);
886 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000887 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
888 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000889 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000890
891 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000892 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000893 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000894 // If we know the sign bits of both operands are zero, strength reduce to a
895 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
896 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000897 if (TLI.MaskedValueIsZero(N1, SignBit) &&
898 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000899 return DAG.getNode(ISD::UREM, VT, N0, N1);
Chris Lattner26d29902006-10-12 20:58:32 +0000900
901 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
902 // the remainder operation.
903 if (N1C && !N1C->isNullValue()) {
904 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
905 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
906 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
907 AddToWorkList(Div.Val);
908 AddToWorkList(Mul.Val);
909 return Sub;
910 }
911
Nate Begeman83e75ec2005-09-06 04:43:02 +0000912 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000913}
914
Nate Begeman83e75ec2005-09-06 04:43:02 +0000915SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000916 SDOperand N0 = N->getOperand(0);
917 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000918 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
919 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000920 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000921
922 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000923 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000924 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000925 // fold (urem x, pow2) -> (and x, pow2-1)
926 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000927 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000928 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
929 if (N1.getOpcode() == ISD::SHL) {
930 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
931 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000932 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +0000933 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000934 return DAG.getNode(ISD::AND, VT, N0, Add);
935 }
936 }
937 }
Chris Lattner26d29902006-10-12 20:58:32 +0000938
939 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
940 // the remainder operation.
941 if (N1C && !N1C->isNullValue()) {
942 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
943 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
944 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
945 AddToWorkList(Div.Val);
946 AddToWorkList(Mul.Val);
947 return Sub;
948 }
949
Nate Begeman83e75ec2005-09-06 04:43:02 +0000950 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000951}
952
Nate Begeman83e75ec2005-09-06 04:43:02 +0000953SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000954 SDOperand N0 = N->getOperand(0);
955 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000956 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000957
958 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000959 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000960 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000961 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000962 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000963 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
964 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000965 TLI.getShiftAmountTy()));
966 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000967}
968
Nate Begeman83e75ec2005-09-06 04:43:02 +0000969SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000970 SDOperand N0 = N->getOperand(0);
971 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000972 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000973
974 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000975 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000976 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000977 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000978 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000979 return DAG.getConstant(0, N0.getValueType());
980 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000981}
982
Chris Lattner35e5c142006-05-05 05:51:50 +0000983/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
984/// two operands of the same opcode, try to simplify it.
985SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
986 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
987 MVT::ValueType VT = N0.getValueType();
988 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
989
Chris Lattner540121f2006-05-05 06:31:05 +0000990 // For each of OP in AND/OR/XOR:
991 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
992 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
993 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +0000994 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +0000995 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +0000996 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000997 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
998 SDOperand ORNode = DAG.getNode(N->getOpcode(),
999 N0.getOperand(0).getValueType(),
1000 N0.getOperand(0), N1.getOperand(0));
1001 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001002 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001003 }
1004
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001005 // For each of OP in SHL/SRL/SRA/AND...
1006 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1007 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1008 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001009 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001010 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001011 N0.getOperand(1) == N1.getOperand(1)) {
1012 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1013 N0.getOperand(0).getValueType(),
1014 N0.getOperand(0), N1.getOperand(0));
1015 AddToWorkList(ORNode.Val);
1016 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1017 }
1018
1019 return SDOperand();
1020}
1021
Nate Begeman83e75ec2005-09-06 04:43:02 +00001022SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001023 SDOperand N0 = N->getOperand(0);
1024 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001025 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001026 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1027 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001028 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +00001029 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001030
1031 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001032 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001033 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001034 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001035 if (N0C && !N1C)
1036 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001037 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001038 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001039 return N0;
1040 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +00001041 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001042 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001043 // reassociate and
1044 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1045 if (RAND.Val != 0)
1046 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001047 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001048 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001049 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001050 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001051 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001052 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1053 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001054 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +00001055 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001056 ~N1C->getValue() & InMask)) {
1057 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1058 N0.getOperand(0));
1059
1060 // Replace uses of the AND with uses of the Zero extend node.
1061 CombineTo(N, Zext);
1062
Chris Lattner3603cd62006-02-02 07:17:31 +00001063 // We actually want to replace all uses of the any_extend with the
1064 // zero_extend, to avoid duplicating things. This will later cause this
1065 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001066 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001067 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001068 }
1069 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001070 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1071 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1072 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1073 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1074
1075 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1076 MVT::isInteger(LL.getValueType())) {
1077 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1078 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1079 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001080 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001081 return DAG.getSetCC(VT, ORNode, LR, Op1);
1082 }
1083 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1084 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1085 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001086 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001087 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1088 }
1089 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1090 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1091 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001092 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001093 return DAG.getSetCC(VT, ORNode, LR, Op1);
1094 }
1095 }
1096 // canonicalize equivalent to ll == rl
1097 if (LL == RR && LR == RL) {
1098 Op1 = ISD::getSetCCSwappedOperands(Op1);
1099 std::swap(RL, RR);
1100 }
1101 if (LL == RL && LR == RR) {
1102 bool isInteger = MVT::isInteger(LL.getValueType());
1103 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1104 if (Result != ISD::SETCC_INVALID)
1105 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1106 }
1107 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001108
1109 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1110 if (N0.getOpcode() == N1.getOpcode()) {
1111 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1112 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001113 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001114
Nate Begemande996292006-02-03 22:24:05 +00001115 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1116 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001117 if (!MVT::isVector(VT) &&
1118 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001119 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001120 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Chengc5484282006-10-04 00:56:09 +00001121 if (ISD::isEXTLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001122 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001123 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001124 // If we zero all the possible extended bits, then we can turn this into
1125 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001126 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001127 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001128 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1129 LN0->getBasePtr(), LN0->getSrcValue(),
1130 LN0->getSrcValueOffset(), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001131 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001132 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001133 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001134 }
1135 }
1136 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Chengc5484282006-10-04 00:56:09 +00001137 if (ISD::isSEXTLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001138 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001139 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001140 // If we zero all the possible extended bits, then we can turn this into
1141 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001142 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001143 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001144 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1145 LN0->getBasePtr(), LN0->getSrcValue(),
1146 LN0->getSrcValueOffset(), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001147 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001148 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001149 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001150 }
1151 }
Chris Lattner15045b62006-02-28 06:35:35 +00001152
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001153 // fold (and (load x), 255) -> (zextload x, i8)
1154 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001155 if (N1C && N0.getOpcode() == ISD::LOAD) {
1156 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1157 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
1158 N0.hasOneUse()) {
1159 MVT::ValueType EVT, LoadedVT;
1160 if (N1C->getValue() == 255)
1161 EVT = MVT::i8;
1162 else if (N1C->getValue() == 65535)
1163 EVT = MVT::i16;
1164 else if (N1C->getValue() == ~0U)
1165 EVT = MVT::i32;
1166 else
1167 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001168
Evan Cheng2e49f092006-10-11 07:10:22 +00001169 LoadedVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001170 if (EVT != MVT::Other && LoadedVT > EVT &&
1171 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1172 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1173 // For big endian targets, we need to add an offset to the pointer to
1174 // load the correct bytes. For little endian systems, we merely need to
1175 // read fewer bytes from the same pointer.
1176 unsigned PtrOff =
1177 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1178 SDOperand NewPtr = LN0->getBasePtr();
1179 if (!TLI.isLittleEndian())
1180 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1181 DAG.getConstant(PtrOff, PtrType));
1182 AddToWorkList(NewPtr.Val);
1183 SDOperand Load =
1184 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
1185 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT);
1186 AddToWorkList(N);
1187 CombineTo(N0.Val, Load, Load.getValue(1));
1188 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1189 }
Chris Lattner15045b62006-02-28 06:35:35 +00001190 }
1191 }
1192
Nate Begeman83e75ec2005-09-06 04:43:02 +00001193 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001194}
1195
Nate Begeman83e75ec2005-09-06 04:43:02 +00001196SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001197 SDOperand N0 = N->getOperand(0);
1198 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001199 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001200 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1201 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001202 MVT::ValueType VT = N1.getValueType();
1203 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001204
1205 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001206 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001207 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001208 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001209 if (N0C && !N1C)
1210 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001211 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001212 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001213 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001214 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001215 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001216 return N1;
1217 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001218 if (N1C &&
1219 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001220 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001221 // reassociate or
1222 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1223 if (ROR.Val != 0)
1224 return ROR;
1225 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1226 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001227 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001228 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1229 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1230 N1),
1231 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001232 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001233 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1234 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1235 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1236 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1237
1238 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1239 MVT::isInteger(LL.getValueType())) {
1240 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1241 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1242 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1243 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1244 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001245 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001246 return DAG.getSetCC(VT, ORNode, LR, Op1);
1247 }
1248 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1249 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1250 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1251 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1252 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001253 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001254 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1255 }
1256 }
1257 // canonicalize equivalent to ll == rl
1258 if (LL == RR && LR == RL) {
1259 Op1 = ISD::getSetCCSwappedOperands(Op1);
1260 std::swap(RL, RR);
1261 }
1262 if (LL == RL && LR == RR) {
1263 bool isInteger = MVT::isInteger(LL.getValueType());
1264 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1265 if (Result != ISD::SETCC_INVALID)
1266 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1267 }
1268 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001269
1270 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1271 if (N0.getOpcode() == N1.getOpcode()) {
1272 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1273 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001274 }
Chris Lattner516b9622006-09-14 20:50:57 +00001275
Chris Lattner1ec72732006-09-14 21:11:37 +00001276 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1277 if (N0.getOpcode() == ISD::AND &&
1278 N1.getOpcode() == ISD::AND &&
1279 N0.getOperand(1).getOpcode() == ISD::Constant &&
1280 N1.getOperand(1).getOpcode() == ISD::Constant &&
1281 // Don't increase # computations.
1282 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1283 // We can only do this xform if we know that bits from X that are set in C2
1284 // but not in C1 are already zero. Likewise for Y.
1285 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1286 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1287
1288 if (TLI.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1289 TLI.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
1290 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1291 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1292 }
1293 }
1294
1295
Chris Lattner516b9622006-09-14 20:50:57 +00001296 // See if this is some rotate idiom.
1297 if (SDNode *Rot = MatchRotate(N0, N1))
1298 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001299
Nate Begeman83e75ec2005-09-06 04:43:02 +00001300 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001301}
1302
Chris Lattner516b9622006-09-14 20:50:57 +00001303
1304/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1305static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1306 if (Op.getOpcode() == ISD::AND) {
1307 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1308 Mask = Op.getOperand(1);
1309 Op = Op.getOperand(0);
1310 } else {
1311 return false;
1312 }
1313 }
1314
1315 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1316 Shift = Op;
1317 return true;
1318 }
1319 return false;
1320}
1321
1322
1323// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1324// idioms for rotate, and if the target supports rotation instructions, generate
1325// a rot[lr].
1326SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1327 // Must be a legal type. Expanded an promoted things won't work with rotates.
1328 MVT::ValueType VT = LHS.getValueType();
1329 if (!TLI.isTypeLegal(VT)) return 0;
1330
1331 // The target must have at least one rotate flavor.
1332 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1333 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1334 if (!HasROTL && !HasROTR) return 0;
1335
1336 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1337 SDOperand LHSShift; // The shift.
1338 SDOperand LHSMask; // AND value if any.
1339 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1340 return 0; // Not part of a rotate.
1341
1342 SDOperand RHSShift; // The shift.
1343 SDOperand RHSMask; // AND value if any.
1344 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1345 return 0; // Not part of a rotate.
1346
1347 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1348 return 0; // Not shifting the same value.
1349
1350 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1351 return 0; // Shifts must disagree.
1352
1353 // Canonicalize shl to left side in a shl/srl pair.
1354 if (RHSShift.getOpcode() == ISD::SHL) {
1355 std::swap(LHS, RHS);
1356 std::swap(LHSShift, RHSShift);
1357 std::swap(LHSMask , RHSMask );
1358 }
1359
1360 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1361
1362 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1363 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
1364 if (LHSShift.getOperand(1).getOpcode() == ISD::Constant &&
1365 RHSShift.getOperand(1).getOpcode() == ISD::Constant) {
1366 uint64_t LShVal = cast<ConstantSDNode>(LHSShift.getOperand(1))->getValue();
1367 uint64_t RShVal = cast<ConstantSDNode>(RHSShift.getOperand(1))->getValue();
1368 if ((LShVal + RShVal) != OpSizeInBits)
1369 return 0;
1370
1371 SDOperand Rot;
1372 if (HasROTL)
1373 Rot = DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1374 LHSShift.getOperand(1));
1375 else
1376 Rot = DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1377 RHSShift.getOperand(1));
1378
1379 // If there is an AND of either shifted operand, apply it to the result.
1380 if (LHSMask.Val || RHSMask.Val) {
1381 uint64_t Mask = MVT::getIntVTBitMask(VT);
1382
1383 if (LHSMask.Val) {
1384 uint64_t RHSBits = (1ULL << LShVal)-1;
1385 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1386 }
1387 if (RHSMask.Val) {
1388 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1389 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1390 }
1391
1392 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1393 }
1394
1395 return Rot.Val;
1396 }
1397
1398 // If there is a mask here, and we have a variable shift, we can't be sure
1399 // that we're masking out the right stuff.
1400 if (LHSMask.Val || RHSMask.Val)
1401 return 0;
1402
1403 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1404 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
1405 if (RHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1406 LHSShift.getOperand(1) == RHSShift.getOperand(1).getOperand(1)) {
1407 if (ConstantSDNode *SUBC =
1408 dyn_cast<ConstantSDNode>(RHSShift.getOperand(1).getOperand(0))) {
1409 if (SUBC->getValue() == OpSizeInBits)
1410 if (HasROTL)
1411 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1412 LHSShift.getOperand(1)).Val;
1413 else
1414 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1415 LHSShift.getOperand(1)).Val;
1416 }
1417 }
1418
1419 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1420 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
1421 if (LHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1422 RHSShift.getOperand(1) == LHSShift.getOperand(1).getOperand(1)) {
1423 if (ConstantSDNode *SUBC =
1424 dyn_cast<ConstantSDNode>(LHSShift.getOperand(1).getOperand(0))) {
1425 if (SUBC->getValue() == OpSizeInBits)
1426 if (HasROTL)
1427 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1428 LHSShift.getOperand(1)).Val;
1429 else
1430 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1431 RHSShift.getOperand(1)).Val;
1432 }
1433 }
1434
1435 return 0;
1436}
1437
1438
Nate Begeman83e75ec2005-09-06 04:43:02 +00001439SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001440 SDOperand N0 = N->getOperand(0);
1441 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001442 SDOperand LHS, RHS, CC;
1443 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1444 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001445 MVT::ValueType VT = N0.getValueType();
1446
1447 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001448 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001449 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001450 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001451 if (N0C && !N1C)
1452 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001453 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001454 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001455 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001456 // reassociate xor
1457 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1458 if (RXOR.Val != 0)
1459 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001460 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001461 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1462 bool isInt = MVT::isInteger(LHS.getValueType());
1463 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1464 isInt);
1465 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001466 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001467 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001468 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001469 assert(0 && "Unhandled SetCC Equivalent!");
1470 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001471 }
Nate Begeman99801192005-09-07 23:25:52 +00001472 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1473 if (N1C && N1C->getValue() == 1 &&
1474 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001475 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001476 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1477 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001478 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1479 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001480 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001481 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001482 }
1483 }
Nate Begeman99801192005-09-07 23:25:52 +00001484 // fold !(x or y) -> (!x and !y) iff x or y are constants
1485 if (N1C && N1C->isAllOnesValue() &&
1486 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001487 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001488 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1489 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001490 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1491 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001492 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001493 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001494 }
1495 }
Nate Begeman223df222005-09-08 20:18:10 +00001496 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1497 if (N1C && N0.getOpcode() == ISD::XOR) {
1498 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1499 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1500 if (N00C)
1501 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1502 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1503 if (N01C)
1504 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1505 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1506 }
1507 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001508 if (N0 == N1) {
1509 if (!MVT::isVector(VT)) {
1510 return DAG.getConstant(0, VT);
1511 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1512 // Produce a vector of zeros.
1513 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1514 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001515 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001516 }
1517 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001518
1519 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1520 if (N0.getOpcode() == N1.getOpcode()) {
1521 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1522 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001523 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001524
Chris Lattner3e104b12006-04-08 04:15:24 +00001525 // Simplify the expression using non-local knowledge.
1526 if (!MVT::isVector(VT) &&
1527 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001528 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001529
Nate Begeman83e75ec2005-09-06 04:43:02 +00001530 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001531}
1532
Nate Begeman83e75ec2005-09-06 04:43:02 +00001533SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001534 SDOperand N0 = N->getOperand(0);
1535 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001536 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1537 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001538 MVT::ValueType VT = N0.getValueType();
1539 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1540
1541 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001542 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001543 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001544 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001545 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001546 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001547 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001548 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001549 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001550 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001551 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001552 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001553 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001554 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001555 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001556 if (SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001557 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001558 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001559 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001560 N0.getOperand(1).getOpcode() == ISD::Constant) {
1561 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001562 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001563 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001564 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001565 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001566 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001567 }
1568 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1569 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001570 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001571 N0.getOperand(1).getOpcode() == ISD::Constant) {
1572 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001573 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001574 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1575 DAG.getConstant(~0ULL << c1, VT));
1576 if (c2 > c1)
1577 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001578 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001579 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001580 return DAG.getNode(ISD::SRL, VT, Mask,
1581 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001582 }
1583 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001584 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001585 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001586 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnercac70592006-03-05 19:53:55 +00001587 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1588 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1589 isa<ConstantSDNode>(N0.getOperand(1))) {
1590 return DAG.getNode(ISD::ADD, VT,
1591 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1592 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1593 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001594 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001595}
1596
Nate Begeman83e75ec2005-09-06 04:43:02 +00001597SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001598 SDOperand N0 = N->getOperand(0);
1599 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001600 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1601 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001602 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001603
1604 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001605 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001606 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001607 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001608 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001609 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001610 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001611 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001612 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001613 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001614 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001615 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001616 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001617 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001618 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001619 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1620 // sext_inreg.
1621 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1622 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1623 MVT::ValueType EVT;
1624 switch (LowBits) {
1625 default: EVT = MVT::Other; break;
1626 case 1: EVT = MVT::i1; break;
1627 case 8: EVT = MVT::i8; break;
1628 case 16: EVT = MVT::i16; break;
1629 case 32: EVT = MVT::i32; break;
1630 }
1631 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1632 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1633 DAG.getValueType(EVT));
1634 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001635
1636 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1637 if (N1C && N0.getOpcode() == ISD::SRA) {
1638 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1639 unsigned Sum = N1C->getValue() + C1->getValue();
1640 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1641 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1642 DAG.getConstant(Sum, N1C->getValueType(0)));
1643 }
1644 }
1645
Chris Lattnera8504462006-05-08 20:51:54 +00001646 // Simplify, based on bits shifted out of the LHS.
1647 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
1648 return SDOperand(N, 0);
1649
1650
Nate Begeman1d4d4142005-09-01 00:19:25 +00001651 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001652 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001653 return DAG.getNode(ISD::SRL, VT, N0, N1);
1654 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001655}
1656
Nate Begeman83e75ec2005-09-06 04:43:02 +00001657SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001658 SDOperand N0 = N->getOperand(0);
1659 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001660 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1661 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001662 MVT::ValueType VT = N0.getValueType();
1663 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1664
1665 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001666 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001667 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001668 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001669 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001670 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001671 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001672 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001673 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001674 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001675 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001676 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001677 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001678 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001679 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001680 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001681 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001682 N0.getOperand(1).getOpcode() == ISD::Constant) {
1683 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001684 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001685 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001686 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001687 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001688 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001689 }
Chris Lattner350bec02006-04-02 06:11:11 +00001690
Chris Lattner06afe072006-05-05 22:53:17 +00001691 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
1692 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1693 // Shifting in all undef bits?
1694 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
1695 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
1696 return DAG.getNode(ISD::UNDEF, VT);
1697
1698 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
1699 AddToWorkList(SmallShift.Val);
1700 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
1701 }
1702
Chris Lattner3657ffe2006-10-12 20:23:19 +00001703 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
1704 // bit, which is unmodified by sra.
1705 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
1706 if (N0.getOpcode() == ISD::SRA)
1707 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
1708 }
1709
Chris Lattner350bec02006-04-02 06:11:11 +00001710 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1711 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1712 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1713 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1714 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
1715
1716 // If any of the input bits are KnownOne, then the input couldn't be all
1717 // zeros, thus the result of the srl will always be zero.
1718 if (KnownOne) return DAG.getConstant(0, VT);
1719
1720 // If all of the bits input the to ctlz node are known to be zero, then
1721 // the result of the ctlz is "32" and the result of the shift is one.
1722 uint64_t UnknownBits = ~KnownZero & Mask;
1723 if (UnknownBits == 0) return DAG.getConstant(1, VT);
1724
1725 // Otherwise, check to see if there is exactly one bit input to the ctlz.
1726 if ((UnknownBits & (UnknownBits-1)) == 0) {
1727 // Okay, we know that only that the single bit specified by UnknownBits
1728 // could be set on input to the CTLZ node. If this bit is set, the SRL
1729 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
1730 // to an SRL,XOR pair, which is likely to simplify more.
1731 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
1732 SDOperand Op = N0.getOperand(0);
1733 if (ShAmt) {
1734 Op = DAG.getNode(ISD::SRL, VT, Op,
1735 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
1736 AddToWorkList(Op.Val);
1737 }
1738 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
1739 }
1740 }
1741
Nate Begeman83e75ec2005-09-06 04:43:02 +00001742 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001743}
1744
Nate Begeman83e75ec2005-09-06 04:43:02 +00001745SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001746 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001747 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001748
1749 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001750 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001751 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001752 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001753}
1754
Nate Begeman83e75ec2005-09-06 04:43:02 +00001755SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001756 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001757 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001758
1759 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001760 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001761 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001762 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001763}
1764
Nate Begeman83e75ec2005-09-06 04:43:02 +00001765SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001766 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001767 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001768
1769 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001770 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001771 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001772 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001773}
1774
Nate Begeman452d7be2005-09-16 00:54:12 +00001775SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1776 SDOperand N0 = N->getOperand(0);
1777 SDOperand N1 = N->getOperand(1);
1778 SDOperand N2 = N->getOperand(2);
1779 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1780 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1781 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1782 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001783
Nate Begeman452d7be2005-09-16 00:54:12 +00001784 // fold select C, X, X -> X
1785 if (N1 == N2)
1786 return N1;
1787 // fold select true, X, Y -> X
1788 if (N0C && !N0C->isNullValue())
1789 return N1;
1790 // fold select false, X, Y -> Y
1791 if (N0C && N0C->isNullValue())
1792 return N2;
1793 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001794 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001795 return DAG.getNode(ISD::OR, VT, N0, N2);
1796 // fold select C, 0, X -> ~C & X
1797 // FIXME: this should check for C type == X type, not i1?
1798 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1799 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001800 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001801 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1802 }
1803 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001804 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001805 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001806 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001807 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1808 }
1809 // fold select C, X, 0 -> C & X
1810 // FIXME: this should check for C type == X type, not i1?
1811 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1812 return DAG.getNode(ISD::AND, VT, N0, N1);
1813 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1814 if (MVT::i1 == VT && N0 == N1)
1815 return DAG.getNode(ISD::OR, VT, N0, N2);
1816 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1817 if (MVT::i1 == VT && N0 == N2)
1818 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00001819
Chris Lattner40c62d52005-10-18 06:04:22 +00001820 // If we can fold this based on the true/false value, do so.
1821 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00001822 return SDOperand(N, 0); // Don't revisit N.
1823
Nate Begeman44728a72005-09-19 22:34:01 +00001824 // fold selects based on a setcc into other things, such as min/max/abs
1825 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001826 // FIXME:
1827 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1828 // having to say they don't support SELECT_CC on every type the DAG knows
1829 // about, since there is no way to mark an opcode illegal at all value types
1830 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1831 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1832 N1, N2, N0.getOperand(2));
1833 else
1834 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001835 return SDOperand();
1836}
1837
1838SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001839 SDOperand N0 = N->getOperand(0);
1840 SDOperand N1 = N->getOperand(1);
1841 SDOperand N2 = N->getOperand(2);
1842 SDOperand N3 = N->getOperand(3);
1843 SDOperand N4 = N->getOperand(4);
1844 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1845 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1846 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1847 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1848
Nate Begeman44728a72005-09-19 22:34:01 +00001849 // fold select_cc lhs, rhs, x, x, cc -> x
1850 if (N2 == N3)
1851 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001852
Chris Lattner5f42a242006-09-20 06:19:26 +00001853 // Determine if the condition we're dealing with is constant
1854 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00001855 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00001856
1857 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
1858 if (SCCC->getValue())
1859 return N2; // cond always true -> true val
1860 else
1861 return N3; // cond always false -> false val
1862 }
1863
1864 // Fold to a simpler select_cc
1865 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
1866 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
1867 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
1868 SCC.getOperand(2));
1869
Chris Lattner40c62d52005-10-18 06:04:22 +00001870 // If we can fold this based on the true/false value, do so.
1871 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00001872 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00001873
Nate Begeman44728a72005-09-19 22:34:01 +00001874 // fold select_cc into other things, such as min/max/abs
1875 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001876}
1877
1878SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1879 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1880 cast<CondCodeSDNode>(N->getOperand(2))->get());
1881}
1882
Nate Begeman83e75ec2005-09-06 04:43:02 +00001883SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001884 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001885 MVT::ValueType VT = N->getValueType(0);
1886
Nate Begeman1d4d4142005-09-01 00:19:25 +00001887 // fold (sext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001888 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001889 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00001890
Nate Begeman1d4d4142005-09-01 00:19:25 +00001891 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001892 // fold (sext (aext x)) -> (sext x)
1893 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001894 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00001895
Chris Lattner6007b842006-09-21 06:00:20 +00001896 // fold (sext (truncate x)) -> (sextinreg x).
1897 if (N0.getOpcode() == ISD::TRUNCATE &&
Chris Lattnerbf370872006-09-21 06:17:39 +00001898 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
1899 N0.getValueType()))) {
Chris Lattner6007b842006-09-21 06:00:20 +00001900 SDOperand Op = N0.getOperand(0);
1901 if (Op.getValueType() < VT) {
1902 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
1903 } else if (Op.getValueType() > VT) {
1904 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
1905 }
1906 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001907 DAG.getValueType(N0.getValueType()));
Chris Lattner6007b842006-09-21 06:00:20 +00001908 }
Chris Lattner310b5782006-05-06 23:06:26 +00001909
Evan Cheng110dec22005-12-14 02:19:23 +00001910 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00001911 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00001912 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng466685d2006-10-09 20:57:25 +00001913 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1914 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
1915 LN0->getBasePtr(), LN0->getSrcValue(),
1916 LN0->getSrcValueOffset(),
Nate Begeman3df4d522005-10-12 20:40:40 +00001917 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001918 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001919 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1920 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001921 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00001922 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001923
1924 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1925 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Chengc5484282006-10-04 00:56:09 +00001926 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001927 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001928 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001929 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
1930 LN0->getBasePtr(), LN0->getSrcValue(),
1931 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001932 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001933 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1934 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001935 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001936 }
1937
Nate Begeman83e75ec2005-09-06 04:43:02 +00001938 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001939}
1940
Nate Begeman83e75ec2005-09-06 04:43:02 +00001941SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001942 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001943 MVT::ValueType VT = N->getValueType(0);
1944
Nate Begeman1d4d4142005-09-01 00:19:25 +00001945 // fold (zext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001946 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001947 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001948 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001949 // fold (zext (aext x)) -> (zext x)
1950 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001951 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00001952
1953 // fold (zext (truncate x)) -> (and x, mask)
1954 if (N0.getOpcode() == ISD::TRUNCATE &&
1955 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
1956 SDOperand Op = N0.getOperand(0);
1957 if (Op.getValueType() < VT) {
1958 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
1959 } else if (Op.getValueType() > VT) {
1960 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
1961 }
1962 return DAG.getZeroExtendInReg(Op, N0.getValueType());
1963 }
1964
Chris Lattner111c2282006-09-21 06:14:31 +00001965 // fold (zext (and (trunc x), cst)) -> (and x, cst).
1966 if (N0.getOpcode() == ISD::AND &&
1967 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1968 N0.getOperand(1).getOpcode() == ISD::Constant) {
1969 SDOperand X = N0.getOperand(0).getOperand(0);
1970 if (X.getValueType() < VT) {
1971 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
1972 } else if (X.getValueType() > VT) {
1973 X = DAG.getNode(ISD::TRUNCATE, VT, X);
1974 }
1975 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1976 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
1977 }
1978
Evan Cheng110dec22005-12-14 02:19:23 +00001979 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00001980 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00001981 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001982 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1983 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1984 LN0->getBasePtr(), LN0->getSrcValue(),
1985 LN0->getSrcValueOffset(),
Evan Cheng110dec22005-12-14 02:19:23 +00001986 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001987 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001988 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1989 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001990 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00001991 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001992
1993 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1994 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Chengc5484282006-10-04 00:56:09 +00001995 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001996 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001997 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001998 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1999 LN0->getBasePtr(), LN0->getSrcValue(),
2000 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002001 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002002 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2003 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002004 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002005 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002006 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002007}
2008
Chris Lattner5ffc0662006-05-05 05:58:59 +00002009SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2010 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002011 MVT::ValueType VT = N->getValueType(0);
2012
2013 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002014 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002015 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2016 // fold (aext (aext x)) -> (aext x)
2017 // fold (aext (zext x)) -> (zext x)
2018 // fold (aext (sext x)) -> (sext x)
2019 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2020 N0.getOpcode() == ISD::ZERO_EXTEND ||
2021 N0.getOpcode() == ISD::SIGN_EXTEND)
2022 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2023
Chris Lattner84750582006-09-20 06:29:17 +00002024 // fold (aext (truncate x))
2025 if (N0.getOpcode() == ISD::TRUNCATE) {
2026 SDOperand TruncOp = N0.getOperand(0);
2027 if (TruncOp.getValueType() == VT)
2028 return TruncOp; // x iff x size == zext size.
2029 if (TruncOp.getValueType() > VT)
2030 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2031 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2032 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002033
2034 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2035 if (N0.getOpcode() == ISD::AND &&
2036 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2037 N0.getOperand(1).getOpcode() == ISD::Constant) {
2038 SDOperand X = N0.getOperand(0).getOperand(0);
2039 if (X.getValueType() < VT) {
2040 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2041 } else if (X.getValueType() > VT) {
2042 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2043 }
2044 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2045 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2046 }
2047
Chris Lattner5ffc0662006-05-05 05:58:59 +00002048 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002049 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002050 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002051 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2052 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2053 LN0->getBasePtr(), LN0->getSrcValue(),
2054 LN0->getSrcValueOffset(),
Chris Lattner5ffc0662006-05-05 05:58:59 +00002055 N0.getValueType());
2056 CombineTo(N, ExtLoad);
2057 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2058 ExtLoad.getValue(1));
2059 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2060 }
2061
2062 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2063 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2064 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002065 if (N0.getOpcode() == ISD::LOAD && !ISD::isNON_EXTLoad(N0.Val) &&
2066 N0.hasOneUse()) {
2067 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002068 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002069 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
2070 LN0->getChain(), LN0->getBasePtr(),
2071 LN0->getSrcValue(),
2072 LN0->getSrcValueOffset(), EVT);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002073 CombineTo(N, ExtLoad);
2074 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2075 ExtLoad.getValue(1));
2076 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2077 }
2078 return SDOperand();
2079}
2080
2081
Nate Begeman83e75ec2005-09-06 04:43:02 +00002082SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002083 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002084 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002085 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002086 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00002087 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002088
Nate Begeman1d4d4142005-09-01 00:19:25 +00002089 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00002090 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00002091 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00002092
Chris Lattner541a24f2006-05-06 22:43:44 +00002093 // If the input is already sign extended, just drop the extension.
Chris Lattneree4ea922006-05-06 09:30:03 +00002094 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
2095 return N0;
2096
Nate Begeman646d7e22005-09-02 21:18:40 +00002097 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
2098 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2099 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00002100 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002101 }
Chris Lattner4b37e872006-05-08 21:18:59 +00002102
Nate Begeman07ed4172005-10-10 21:26:48 +00002103 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00002104 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00002105 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00002106
2107 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
2108 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
2109 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
2110 if (N0.getOpcode() == ISD::SRL) {
2111 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2112 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
2113 // We can turn this into an SRA iff the input to the SRL is already sign
2114 // extended enough.
2115 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
2116 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
2117 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
2118 }
2119 }
2120
Nate Begemanded49632005-10-13 03:11:28 +00002121 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00002122 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002123 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002124 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002125 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2126 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2127 LN0->getBasePtr(), LN0->getSrcValue(),
2128 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002129 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002130 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002131 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002132 }
2133 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Chengc5484282006-10-04 00:56:09 +00002134 if (ISD::isZEXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002135 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002136 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002137 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2138 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2139 LN0->getBasePtr(), LN0->getSrcValue(),
2140 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002141 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002142 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002143 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002144 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002145 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002146}
2147
Nate Begeman83e75ec2005-09-06 04:43:02 +00002148SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002149 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002150 MVT::ValueType VT = N->getValueType(0);
2151
2152 // noop truncate
2153 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002154 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002155 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002156 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002157 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002158 // fold (truncate (truncate x)) -> (truncate x)
2159 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002160 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002161 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00002162 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2163 N0.getOpcode() == ISD::ANY_EXTEND) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002164 if (N0.getValueType() < VT)
2165 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00002166 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002167 else if (N0.getValueType() > VT)
2168 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002169 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002170 else
2171 // if the source and dest are the same type, we can drop both the extend
2172 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002173 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002174 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002175 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng466685d2006-10-09 20:57:25 +00002176 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00002177 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
2178 "Cannot truncate to larger type!");
Evan Cheng466685d2006-10-09 20:57:25 +00002179 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Nate Begeman3df4d522005-10-12 20:40:40 +00002180 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00002181 // For big endian targets, we need to add an offset to the pointer to load
2182 // the correct bytes. For little endian systems, we merely need to read
2183 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00002184 uint64_t PtrOff =
2185 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Evan Cheng466685d2006-10-09 20:57:25 +00002186 SDOperand NewPtr = TLI.isLittleEndian() ? LN0->getBasePtr() :
2187 DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Nate Begeman765784a2005-10-12 23:18:53 +00002188 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00002189 AddToWorkList(NewPtr.Val);
Evan Cheng466685d2006-10-09 20:57:25 +00002190 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), NewPtr,
2191 LN0->getSrcValue(), LN0->getSrcValueOffset());
Chris Lattner5750df92006-03-01 04:03:14 +00002192 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00002193 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002194 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002195 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002196 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002197}
2198
Chris Lattner94683772005-12-23 05:30:37 +00002199SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2200 SDOperand N0 = N->getOperand(0);
2201 MVT::ValueType VT = N->getValueType(0);
2202
2203 // If the input is a constant, let getNode() fold it.
2204 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2205 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2206 if (Res.Val != N) return Res;
2207 }
2208
Chris Lattnerc8547d82005-12-23 05:37:50 +00002209 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2210 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00002211
Chris Lattner57104102005-12-23 05:44:41 +00002212 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002213 // FIXME: These xforms need to know that the resultant load doesn't need a
2214 // higher alignment than the original!
Evan Cheng466685d2006-10-09 20:57:25 +00002215 if (0 && ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse()) {
2216 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2217 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
2218 LN0->getSrcValue(), LN0->getSrcValueOffset());
Chris Lattner5750df92006-03-01 04:03:14 +00002219 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00002220 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2221 Load.getValue(1));
2222 return Load;
2223 }
2224
Chris Lattner94683772005-12-23 05:30:37 +00002225 return SDOperand();
2226}
2227
Chris Lattner6258fb22006-04-02 02:53:43 +00002228SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2229 SDOperand N0 = N->getOperand(0);
2230 MVT::ValueType VT = N->getValueType(0);
2231
2232 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2233 // First check to see if this is all constant.
2234 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2235 VT == MVT::Vector) {
2236 bool isSimple = true;
2237 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2238 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2239 N0.getOperand(i).getOpcode() != ISD::Constant &&
2240 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2241 isSimple = false;
2242 break;
2243 }
2244
Chris Lattner97c20732006-04-03 17:29:28 +00002245 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2246 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002247 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2248 }
2249 }
2250
2251 return SDOperand();
2252}
2253
2254/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2255/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2256/// destination element value type.
2257SDOperand DAGCombiner::
2258ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2259 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2260
2261 // If this is already the right type, we're done.
2262 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2263
2264 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2265 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2266
2267 // If this is a conversion of N elements of one type to N elements of another
2268 // type, convert each element. This handles FP<->INT cases.
2269 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002270 SmallVector<SDOperand, 8> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00002271 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002272 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002273 AddToWorkList(Ops.back().Val);
2274 }
Chris Lattner6258fb22006-04-02 02:53:43 +00002275 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2276 Ops.push_back(DAG.getValueType(DstEltVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002277 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002278 }
2279
2280 // Otherwise, we're growing or shrinking the elements. To avoid having to
2281 // handle annoying details of growing/shrinking FP values, we convert them to
2282 // int first.
2283 if (MVT::isFloatingPoint(SrcEltVT)) {
2284 // Convert the input float vector to a int vector where the elements are the
2285 // same sizes.
2286 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2287 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2288 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2289 SrcEltVT = IntVT;
2290 }
2291
2292 // Now we know the input is an integer vector. If the output is a FP type,
2293 // convert to integer first, then to FP of the right size.
2294 if (MVT::isFloatingPoint(DstEltVT)) {
2295 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2296 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2297 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2298
2299 // Next, convert to FP elements of the same size.
2300 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2301 }
2302
2303 // Okay, we know the src/dst types are both integers of differing types.
2304 // Handling growing first.
2305 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2306 if (SrcBitSize < DstBitSize) {
2307 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2308
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002309 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002310 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2311 i += NumInputsPerOutput) {
2312 bool isLE = TLI.isLittleEndian();
2313 uint64_t NewBits = 0;
2314 bool EltIsUndef = true;
2315 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2316 // Shift the previously computed bits over.
2317 NewBits <<= SrcBitSize;
2318 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2319 if (Op.getOpcode() == ISD::UNDEF) continue;
2320 EltIsUndef = false;
2321
2322 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2323 }
2324
2325 if (EltIsUndef)
2326 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2327 else
2328 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2329 }
2330
2331 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2332 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002333 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002334 }
2335
2336 // Finally, this must be the case where we are shrinking elements: each input
2337 // turns into multiple outputs.
2338 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002339 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002340 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2341 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2342 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2343 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2344 continue;
2345 }
2346 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2347
2348 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2349 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2350 OpVal >>= DstBitSize;
2351 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2352 }
2353
2354 // For big endian targets, swap the order of the pieces of each element.
2355 if (!TLI.isLittleEndian())
2356 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2357 }
2358 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2359 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002360 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002361}
2362
2363
2364
Chris Lattner01b3d732005-09-28 22:28:18 +00002365SDOperand DAGCombiner::visitFADD(SDNode *N) {
2366 SDOperand N0 = N->getOperand(0);
2367 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002368 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2369 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002370 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002371
2372 // fold (fadd c1, c2) -> c1+c2
2373 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002374 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002375 // canonicalize constant to RHS
2376 if (N0CFP && !N1CFP)
2377 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002378 // fold (A + (-B)) -> A-B
2379 if (N1.getOpcode() == ISD::FNEG)
2380 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002381 // fold ((-A) + B) -> B-A
2382 if (N0.getOpcode() == ISD::FNEG)
2383 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002384 return SDOperand();
2385}
2386
2387SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2388 SDOperand N0 = N->getOperand(0);
2389 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002390 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2391 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002392 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002393
2394 // fold (fsub c1, c2) -> c1-c2
2395 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002396 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002397 // fold (A-(-B)) -> A+B
2398 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00002399 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002400 return SDOperand();
2401}
2402
2403SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2404 SDOperand N0 = N->getOperand(0);
2405 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002406 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2407 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002408 MVT::ValueType VT = N->getValueType(0);
2409
Nate Begeman11af4ea2005-10-17 20:40:11 +00002410 // fold (fmul c1, c2) -> c1*c2
2411 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002412 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002413 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002414 if (N0CFP && !N1CFP)
2415 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002416 // fold (fmul X, 2.0) -> (fadd X, X)
2417 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2418 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002419 return SDOperand();
2420}
2421
2422SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2423 SDOperand N0 = N->getOperand(0);
2424 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002425 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2426 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002427 MVT::ValueType VT = N->getValueType(0);
2428
Nate Begemana148d982006-01-18 22:35:16 +00002429 // fold (fdiv c1, c2) -> c1/c2
2430 if (N0CFP && N1CFP)
2431 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002432 return SDOperand();
2433}
2434
2435SDOperand DAGCombiner::visitFREM(SDNode *N) {
2436 SDOperand N0 = N->getOperand(0);
2437 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002438 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2439 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002440 MVT::ValueType VT = N->getValueType(0);
2441
Nate Begemana148d982006-01-18 22:35:16 +00002442 // fold (frem c1, c2) -> fmod(c1,c2)
2443 if (N0CFP && N1CFP)
2444 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002445 return SDOperand();
2446}
2447
Chris Lattner12d83032006-03-05 05:30:57 +00002448SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2449 SDOperand N0 = N->getOperand(0);
2450 SDOperand N1 = N->getOperand(1);
2451 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2452 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2453 MVT::ValueType VT = N->getValueType(0);
2454
2455 if (N0CFP && N1CFP) // Constant fold
2456 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2457
2458 if (N1CFP) {
2459 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2460 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2461 union {
2462 double d;
2463 int64_t i;
2464 } u;
2465 u.d = N1CFP->getValue();
2466 if (u.i >= 0)
2467 return DAG.getNode(ISD::FABS, VT, N0);
2468 else
2469 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2470 }
2471
2472 // copysign(fabs(x), y) -> copysign(x, y)
2473 // copysign(fneg(x), y) -> copysign(x, y)
2474 // copysign(copysign(x,z), y) -> copysign(x, y)
2475 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2476 N0.getOpcode() == ISD::FCOPYSIGN)
2477 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2478
2479 // copysign(x, abs(y)) -> abs(x)
2480 if (N1.getOpcode() == ISD::FABS)
2481 return DAG.getNode(ISD::FABS, VT, N0);
2482
2483 // copysign(x, copysign(y,z)) -> copysign(x, z)
2484 if (N1.getOpcode() == ISD::FCOPYSIGN)
2485 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2486
2487 // copysign(x, fp_extend(y)) -> copysign(x, y)
2488 // copysign(x, fp_round(y)) -> copysign(x, y)
2489 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2490 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2491
2492 return SDOperand();
2493}
2494
2495
Chris Lattner01b3d732005-09-28 22:28:18 +00002496
Nate Begeman83e75ec2005-09-06 04:43:02 +00002497SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002498 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002499 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002500 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002501
2502 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002503 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002504 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002505 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002506}
2507
Nate Begeman83e75ec2005-09-06 04:43:02 +00002508SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002509 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002510 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002511 MVT::ValueType VT = N->getValueType(0);
2512
Nate Begeman1d4d4142005-09-01 00:19:25 +00002513 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002514 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002515 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002516 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002517}
2518
Nate Begeman83e75ec2005-09-06 04:43:02 +00002519SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002520 SDOperand N0 = N->getOperand(0);
2521 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2522 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002523
2524 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002525 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002526 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002527 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002528}
2529
Nate Begeman83e75ec2005-09-06 04:43:02 +00002530SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002531 SDOperand N0 = N->getOperand(0);
2532 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2533 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002534
2535 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002536 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002537 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002538 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002539}
2540
Nate Begeman83e75ec2005-09-06 04:43:02 +00002541SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002542 SDOperand N0 = N->getOperand(0);
2543 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2544 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002545
2546 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002547 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002548 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002549
2550 // fold (fp_round (fp_extend x)) -> x
2551 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2552 return N0.getOperand(0);
2553
2554 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2555 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2556 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2557 AddToWorkList(Tmp.Val);
2558 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2559 }
2560
Nate Begeman83e75ec2005-09-06 04:43:02 +00002561 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002562}
2563
Nate Begeman83e75ec2005-09-06 04:43:02 +00002564SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002565 SDOperand N0 = N->getOperand(0);
2566 MVT::ValueType VT = N->getValueType(0);
2567 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002568 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002569
Nate Begeman1d4d4142005-09-01 00:19:25 +00002570 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002571 if (N0CFP) {
2572 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002573 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002574 }
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::visitFP_EXTEND(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);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002582
2583 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002584 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002585 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00002586
2587 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002588 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002589 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002590 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2591 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2592 LN0->getBasePtr(), LN0->getSrcValue(),
2593 LN0->getSrcValueOffset(),
Chris Lattnere564dbb2006-05-05 21:34:35 +00002594 N0.getValueType());
2595 CombineTo(N, ExtLoad);
2596 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
2597 ExtLoad.getValue(1));
2598 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2599 }
2600
2601
Nate Begeman83e75ec2005-09-06 04:43:02 +00002602 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002603}
2604
Nate Begeman83e75ec2005-09-06 04:43:02 +00002605SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002606 SDOperand N0 = N->getOperand(0);
2607 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2608 MVT::ValueType VT = N->getValueType(0);
2609
2610 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002611 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002612 return DAG.getNode(ISD::FNEG, VT, N0);
2613 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002614 if (N0.getOpcode() == ISD::SUB)
2615 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002616 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002617 if (N0.getOpcode() == ISD::FNEG)
2618 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002619 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002620}
2621
Nate Begeman83e75ec2005-09-06 04:43:02 +00002622SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002623 SDOperand N0 = N->getOperand(0);
2624 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2625 MVT::ValueType VT = N->getValueType(0);
2626
Nate Begeman1d4d4142005-09-01 00:19:25 +00002627 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002628 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002629 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002630 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002631 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002632 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002633 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002634 // fold (fabs (fcopysign x, y)) -> (fabs x)
2635 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2636 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2637
Nate Begeman83e75ec2005-09-06 04:43:02 +00002638 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002639}
2640
Nate Begeman44728a72005-09-19 22:34:01 +00002641SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2642 SDOperand Chain = N->getOperand(0);
2643 SDOperand N1 = N->getOperand(1);
2644 SDOperand N2 = N->getOperand(2);
2645 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2646
2647 // never taken branch, fold to chain
2648 if (N1C && N1C->isNullValue())
2649 return Chain;
2650 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002651 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002652 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002653 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2654 // on the target.
2655 if (N1.getOpcode() == ISD::SETCC &&
2656 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2657 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2658 N1.getOperand(0), N1.getOperand(1), N2);
2659 }
Nate Begeman44728a72005-09-19 22:34:01 +00002660 return SDOperand();
2661}
2662
Chris Lattner3ea0b472005-10-05 06:47:48 +00002663// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2664//
Nate Begeman44728a72005-09-19 22:34:01 +00002665SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002666 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2667 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2668
2669 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002670 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002671 if (Simp.Val) AddToWorkList(Simp.Val);
2672
Nate Begemane17daeb2005-10-05 21:43:42 +00002673 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2674
2675 // fold br_cc true, dest -> br dest (unconditional branch)
2676 if (SCCC && SCCC->getValue())
2677 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2678 N->getOperand(4));
2679 // fold br_cc false, dest -> unconditional fall through
2680 if (SCCC && SCCC->isNullValue())
2681 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00002682
Nate Begemane17daeb2005-10-05 21:43:42 +00002683 // fold to a simpler setcc
2684 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2685 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2686 Simp.getOperand(2), Simp.getOperand(0),
2687 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002688 return SDOperand();
2689}
2690
Chris Lattner01a22022005-10-10 22:04:48 +00002691SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00002692 LoadSDNode *LD = cast<LoadSDNode>(N);
2693 SDOperand Chain = LD->getChain();
2694 SDOperand Ptr = LD->getBasePtr();
Jim Laskey6ff23e52006-10-04 16:53:27 +00002695
Chris Lattnere4b95392006-03-31 18:06:18 +00002696 // If there are no uses of the loaded value, change uses of the chain value
2697 // into uses of the chain input (i.e. delete the dead load).
2698 if (N->hasNUsesOfValue(0, 0))
2699 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
Chris Lattner01a22022005-10-10 22:04:48 +00002700
2701 // If this load is directly stored, replace the load value with the stored
2702 // value.
2703 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002704 // TODO: Handle TRUNCSTORE/LOADEXT
2705 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002706 if (ISD::isNON_TRUNCStore(Chain.Val)) {
2707 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
2708 if (PrevST->getBasePtr() == Ptr &&
2709 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002710 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002711 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002712 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00002713
Jim Laskey7ca56af2006-10-11 13:47:09 +00002714 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00002715 // Walk up chain skipping non-aliasing memory nodes.
2716 SDOperand BetterChain = FindBetterChain(N, Chain);
2717
Jim Laskey6ff23e52006-10-04 16:53:27 +00002718 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00002719 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002720 SDOperand ReplLoad;
2721
Jim Laskey279f0532006-09-25 16:29:54 +00002722 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00002723 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
2724 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
2725 LD->getSrcValue(), LD->getSrcValueOffset());
2726 } else {
2727 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
2728 LD->getValueType(0),
2729 BetterChain, Ptr, LD->getSrcValue(),
2730 LD->getSrcValueOffset(),
2731 LD->getLoadedVT());
2732 }
Jim Laskey279f0532006-09-25 16:29:54 +00002733
Jim Laskey6ff23e52006-10-04 16:53:27 +00002734 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00002735 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
2736 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00002737
Jim Laskey274062c2006-10-13 23:32:28 +00002738 // Replace uses with load result and token factor. Don't add users
2739 // to work list.
2740 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00002741 }
2742 }
2743
Chris Lattner01a22022005-10-10 22:04:48 +00002744 return SDOperand();
2745}
2746
Chris Lattner87514ca2005-10-10 22:31:19 +00002747SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002748 StoreSDNode *ST = cast<StoreSDNode>(N);
2749 SDOperand Chain = ST->getChain();
2750 SDOperand Value = ST->getValue();
2751 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00002752
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002753 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002754 // FIXME: This needs to know that the resultant store does not need a
2755 // higher alignment than the original.
Jim Laskey14fbcbf2006-09-25 21:11:32 +00002756 if (0 && Value.getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002757 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
2758 ST->getSrcValueOffset());
Jim Laskey279f0532006-09-25 16:29:54 +00002759 }
2760
2761 if (CombinerAA) {
Jim Laskey288af5e2006-09-25 19:32:58 +00002762 // If the store ptr is a frame index and the frame index has a use of one
2763 // and this is a return block, then the store is redundant.
2764 if (Ptr.hasOneUse() && isa<FrameIndexSDNode>(Ptr) &&
2765 DAG.getRoot().getOpcode() == ISD::RET) {
2766 return Chain;
2767 }
2768
Jim Laskey279f0532006-09-25 16:29:54 +00002769 // Walk up chain skipping non-aliasing memory nodes.
2770 SDOperand BetterChain = FindBetterChain(N, Chain);
2771
Jim Laskey6ff23e52006-10-04 16:53:27 +00002772 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00002773 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00002774 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00002775 SDOperand ReplStore;
2776 if (ST->isTruncatingStore()) {
2777 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
2778 ST->getSrcValue(),ST->getSrcValueOffset(), ST->getStoredVT());
2779 } else {
2780 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
2781 ST->getSrcValue(), ST->getSrcValueOffset());
2782 }
2783
Jim Laskey279f0532006-09-25 16:29:54 +00002784 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00002785 SDOperand Token =
2786 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
2787
2788 // Don't add users to work list.
2789 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00002790 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00002791 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002792
Chris Lattner87514ca2005-10-10 22:31:19 +00002793 return SDOperand();
2794}
2795
Chris Lattnerca242442006-03-19 01:27:56 +00002796SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2797 SDOperand InVec = N->getOperand(0);
2798 SDOperand InVal = N->getOperand(1);
2799 SDOperand EltNo = N->getOperand(2);
2800
2801 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2802 // vector with the inserted element.
2803 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2804 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002805 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002806 if (Elt < Ops.size())
2807 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002808 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
2809 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002810 }
2811
2812 return SDOperand();
2813}
2814
2815SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2816 SDOperand InVec = N->getOperand(0);
2817 SDOperand InVal = N->getOperand(1);
2818 SDOperand EltNo = N->getOperand(2);
2819 SDOperand NumElts = N->getOperand(3);
2820 SDOperand EltType = N->getOperand(4);
2821
2822 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2823 // vector with the inserted element.
2824 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2825 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002826 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002827 if (Elt < Ops.size()-2)
2828 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002829 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
2830 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002831 }
2832
2833 return SDOperand();
2834}
2835
Chris Lattnerd7648c82006-03-28 20:28:38 +00002836SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
2837 unsigned NumInScalars = N->getNumOperands()-2;
2838 SDOperand NumElts = N->getOperand(NumInScalars);
2839 SDOperand EltType = N->getOperand(NumInScalars+1);
2840
2841 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
2842 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
2843 // two distinct vectors, turn this into a shuffle node.
2844 SDOperand VecIn1, VecIn2;
2845 for (unsigned i = 0; i != NumInScalars; ++i) {
2846 // Ignore undef inputs.
2847 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
2848
2849 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
2850 // constant index, bail out.
2851 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
2852 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
2853 VecIn1 = VecIn2 = SDOperand(0, 0);
2854 break;
2855 }
2856
2857 // If the input vector type disagrees with the result of the vbuild_vector,
2858 // we can't make a shuffle.
2859 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
2860 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
2861 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
2862 VecIn1 = VecIn2 = SDOperand(0, 0);
2863 break;
2864 }
2865
2866 // Otherwise, remember this. We allow up to two distinct input vectors.
2867 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
2868 continue;
2869
2870 if (VecIn1.Val == 0) {
2871 VecIn1 = ExtractedFromVec;
2872 } else if (VecIn2.Val == 0) {
2873 VecIn2 = ExtractedFromVec;
2874 } else {
2875 // Too many inputs.
2876 VecIn1 = VecIn2 = SDOperand(0, 0);
2877 break;
2878 }
2879 }
2880
2881 // If everything is good, we can make a shuffle operation.
2882 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002883 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00002884 for (unsigned i = 0; i != NumInScalars; ++i) {
2885 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
2886 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2887 continue;
2888 }
2889
2890 SDOperand Extract = N->getOperand(i);
2891
2892 // If extracting from the first vector, just use the index directly.
2893 if (Extract.getOperand(0) == VecIn1) {
2894 BuildVecIndices.push_back(Extract.getOperand(1));
2895 continue;
2896 }
2897
2898 // Otherwise, use InIdx + VecSize
2899 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
2900 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
2901 }
2902
2903 // Add count and size info.
2904 BuildVecIndices.push_back(NumElts);
2905 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
2906
2907 // Return the new VVECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002908 SDOperand Ops[5];
2909 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00002910 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002911 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00002912 } else {
2913 // Use an undef vbuild_vector as input for the second operand.
2914 std::vector<SDOperand> UnOps(NumInScalars,
2915 DAG.getNode(ISD::UNDEF,
2916 cast<VTSDNode>(EltType)->getVT()));
2917 UnOps.push_back(NumElts);
2918 UnOps.push_back(EltType);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002919 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2920 &UnOps[0], UnOps.size());
2921 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00002922 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002923 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2924 &BuildVecIndices[0], BuildVecIndices.size());
2925 Ops[3] = NumElts;
2926 Ops[4] = EltType;
2927 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
Chris Lattnerd7648c82006-03-28 20:28:38 +00002928 }
2929
2930 return SDOperand();
2931}
2932
Chris Lattner66445d32006-03-28 22:11:53 +00002933SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002934 SDOperand ShufMask = N->getOperand(2);
2935 unsigned NumElts = ShufMask.getNumOperands();
2936
2937 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2938 bool isIdentity = true;
2939 for (unsigned i = 0; i != NumElts; ++i) {
2940 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2941 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2942 isIdentity = false;
2943 break;
2944 }
2945 }
2946 if (isIdentity) return N->getOperand(0);
2947
2948 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2949 isIdentity = true;
2950 for (unsigned i = 0; i != NumElts; ++i) {
2951 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2952 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2953 isIdentity = false;
2954 break;
2955 }
2956 }
2957 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00002958
2959 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
2960 // needed at all.
2961 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00002962 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002963 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00002964 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002965 for (unsigned i = 0; i != NumElts; ++i)
2966 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
2967 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
2968 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00002969 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00002970 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00002971 BaseIdx = Idx;
2972 } else {
2973 if (BaseIdx != Idx)
2974 isSplat = false;
2975 if (VecNum != V) {
2976 isUnary = false;
2977 break;
2978 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00002979 }
2980 }
2981
2982 SDOperand N0 = N->getOperand(0);
2983 SDOperand N1 = N->getOperand(1);
2984 // Normalize unary shuffle so the RHS is undef.
2985 if (isUnary && VecNum == 1)
2986 std::swap(N0, N1);
2987
Evan Cheng917ec982006-07-21 08:25:53 +00002988 // If it is a splat, check if the argument vector is a build_vector with
2989 // all scalar elements the same.
2990 if (isSplat) {
2991 SDNode *V = N0.Val;
2992 if (V->getOpcode() == ISD::BIT_CONVERT)
2993 V = V->getOperand(0).Val;
2994 if (V->getOpcode() == ISD::BUILD_VECTOR) {
2995 unsigned NumElems = V->getNumOperands()-2;
2996 if (NumElems > BaseIdx) {
2997 SDOperand Base;
2998 bool AllSame = true;
2999 for (unsigned i = 0; i != NumElems; ++i) {
3000 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3001 Base = V->getOperand(i);
3002 break;
3003 }
3004 }
3005 // Splat of <u, u, u, u>, return <u, u, u, u>
3006 if (!Base.Val)
3007 return N0;
3008 for (unsigned i = 0; i != NumElems; ++i) {
3009 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3010 V->getOperand(i) != Base) {
3011 AllSame = false;
3012 break;
3013 }
3014 }
3015 // Splat of <x, x, x, x>, return <x, x, x, x>
3016 if (AllSame)
3017 return N0;
3018 }
3019 }
3020 }
3021
Evan Chenge7bec0d2006-07-20 22:44:41 +00003022 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3023 // into an undef.
3024 if (isUnary || N0 == N1) {
3025 if (N0.getOpcode() == ISD::UNDEF)
Evan Chengc04766a2006-04-06 23:20:43 +00003026 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00003027 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3028 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003029 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00003030 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00003031 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3032 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3033 MappedOps.push_back(ShufMask.getOperand(i));
3034 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00003035 unsigned NewIdx =
3036 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3037 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00003038 }
3039 }
3040 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003041 &MappedOps[0], MappedOps.size());
Chris Lattner3e104b12006-04-08 04:15:24 +00003042 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00003043 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
Evan Chenge7bec0d2006-07-20 22:44:41 +00003044 N0,
Chris Lattner66445d32006-03-28 22:11:53 +00003045 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
3046 ShufMask);
3047 }
3048
3049 return SDOperand();
3050}
3051
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003052SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
3053 SDOperand ShufMask = N->getOperand(2);
3054 unsigned NumElts = ShufMask.getNumOperands()-2;
3055
3056 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3057 bool isIdentity = true;
3058 for (unsigned i = 0; i != NumElts; ++i) {
3059 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3060 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3061 isIdentity = false;
3062 break;
3063 }
3064 }
3065 if (isIdentity) return N->getOperand(0);
3066
3067 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3068 isIdentity = true;
3069 for (unsigned i = 0; i != NumElts; ++i) {
3070 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3071 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3072 isIdentity = false;
3073 break;
3074 }
3075 }
3076 if (isIdentity) return N->getOperand(1);
3077
Evan Chenge7bec0d2006-07-20 22:44:41 +00003078 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3079 // needed at all.
3080 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003081 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003082 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003083 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003084 for (unsigned i = 0; i != NumElts; ++i)
3085 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3086 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3087 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003088 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003089 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003090 BaseIdx = Idx;
3091 } else {
3092 if (BaseIdx != Idx)
3093 isSplat = false;
3094 if (VecNum != V) {
3095 isUnary = false;
3096 break;
3097 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003098 }
3099 }
3100
3101 SDOperand N0 = N->getOperand(0);
3102 SDOperand N1 = N->getOperand(1);
3103 // Normalize unary shuffle so the RHS is undef.
3104 if (isUnary && VecNum == 1)
3105 std::swap(N0, N1);
3106
Evan Cheng917ec982006-07-21 08:25:53 +00003107 // If it is a splat, check if the argument vector is a build_vector with
3108 // all scalar elements the same.
3109 if (isSplat) {
3110 SDNode *V = N0.Val;
Evan Cheng59569222006-10-16 22:49:37 +00003111
3112 // If this is a vbit convert that changes the element type of the vector but
3113 // not the number of vector elements, look through it. Be careful not to
3114 // look though conversions that change things like v4f32 to v2f64.
3115 if (V->getOpcode() == ISD::VBIT_CONVERT) {
3116 SDOperand ConvInput = V->getOperand(0);
3117 if (NumElts ==
3118 ConvInput.getConstantOperandVal(ConvInput.getNumOperands()-2))
3119 V = ConvInput.Val;
3120 }
3121
Evan Cheng917ec982006-07-21 08:25:53 +00003122 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
3123 unsigned NumElems = V->getNumOperands()-2;
3124 if (NumElems > BaseIdx) {
3125 SDOperand Base;
3126 bool AllSame = true;
3127 for (unsigned i = 0; i != NumElems; ++i) {
3128 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3129 Base = V->getOperand(i);
3130 break;
3131 }
3132 }
3133 // Splat of <u, u, u, u>, return <u, u, u, u>
3134 if (!Base.Val)
3135 return N0;
3136 for (unsigned i = 0; i != NumElems; ++i) {
3137 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3138 V->getOperand(i) != Base) {
3139 AllSame = false;
3140 break;
3141 }
3142 }
3143 // Splat of <x, x, x, x>, return <x, x, x, x>
3144 if (AllSame)
3145 return N0;
3146 }
3147 }
3148 }
3149
Evan Chenge7bec0d2006-07-20 22:44:41 +00003150 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3151 // into an undef.
3152 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00003153 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3154 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003155 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00003156 for (unsigned i = 0; i != NumElts; ++i) {
3157 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3158 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3159 MappedOps.push_back(ShufMask.getOperand(i));
3160 } else {
3161 unsigned NewIdx =
3162 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3163 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
3164 }
3165 }
3166 // Add the type/#elts values.
3167 MappedOps.push_back(ShufMask.getOperand(NumElts));
3168 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
3169
3170 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003171 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003172 AddToWorkList(ShufMask.Val);
3173
3174 // Build the undef vector.
3175 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
3176 for (unsigned i = 0; i != NumElts; ++i)
3177 MappedOps[i] = UDVal;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003178 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
3179 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003180 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3181 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003182
3183 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
Evan Chenge7bec0d2006-07-20 22:44:41 +00003184 N0, UDVal, ShufMask,
Chris Lattner17614ea2006-04-08 05:34:25 +00003185 MappedOps[NumElts], MappedOps[NumElts+1]);
3186 }
3187
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003188 return SDOperand();
3189}
3190
Evan Cheng44f1f092006-04-20 08:56:16 +00003191/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
3192/// a VAND to a vector_shuffle with the destination vector and a zero vector.
3193/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
3194/// vector_shuffle V, Zero, <0, 4, 2, 4>
3195SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
3196 SDOperand LHS = N->getOperand(0);
3197 SDOperand RHS = N->getOperand(1);
3198 if (N->getOpcode() == ISD::VAND) {
3199 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
3200 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
3201 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
3202 RHS = RHS.getOperand(0);
3203 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
3204 std::vector<SDOperand> IdxOps;
3205 unsigned NumOps = RHS.getNumOperands();
3206 unsigned NumElts = NumOps-2;
3207 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
3208 for (unsigned i = 0; i != NumElts; ++i) {
3209 SDOperand Elt = RHS.getOperand(i);
3210 if (!isa<ConstantSDNode>(Elt))
3211 return SDOperand();
3212 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
3213 IdxOps.push_back(DAG.getConstant(i, EVT));
3214 else if (cast<ConstantSDNode>(Elt)->isNullValue())
3215 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
3216 else
3217 return SDOperand();
3218 }
3219
3220 // Let's see if the target supports this vector_shuffle.
3221 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
3222 return SDOperand();
3223
3224 // Return the new VVECTOR_SHUFFLE node.
3225 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
3226 SDOperand EVTNode = DAG.getValueType(EVT);
3227 std::vector<SDOperand> Ops;
Chris Lattner516b9622006-09-14 20:50:57 +00003228 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode,
3229 EVTNode);
Evan Cheng44f1f092006-04-20 08:56:16 +00003230 Ops.push_back(LHS);
3231 AddToWorkList(LHS.Val);
3232 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
3233 ZeroOps.push_back(NumEltsNode);
3234 ZeroOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003235 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3236 &ZeroOps[0], ZeroOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003237 IdxOps.push_back(NumEltsNode);
3238 IdxOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003239 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3240 &IdxOps[0], IdxOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003241 Ops.push_back(NumEltsNode);
3242 Ops.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003243 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
3244 &Ops[0], Ops.size());
Evan Cheng44f1f092006-04-20 08:56:16 +00003245 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
3246 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
3247 DstVecSize, DstVecEVT);
3248 }
3249 return Result;
3250 }
3251 }
3252 return SDOperand();
3253}
3254
Chris Lattneredab1b92006-04-02 03:25:57 +00003255/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
3256/// the scalar operation of the vop if it is operating on an integer vector
3257/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
3258SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
3259 ISD::NodeType FPOp) {
3260 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
3261 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
3262 SDOperand LHS = N->getOperand(0);
3263 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00003264 SDOperand Shuffle = XformToShuffleWithZero(N);
3265 if (Shuffle.Val) return Shuffle;
3266
Chris Lattneredab1b92006-04-02 03:25:57 +00003267 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
3268 // this operation.
3269 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
3270 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003271 SmallVector<SDOperand, 8> Ops;
Chris Lattneredab1b92006-04-02 03:25:57 +00003272 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
3273 SDOperand LHSOp = LHS.getOperand(i);
3274 SDOperand RHSOp = RHS.getOperand(i);
3275 // If these two elements can't be folded, bail out.
3276 if ((LHSOp.getOpcode() != ISD::UNDEF &&
3277 LHSOp.getOpcode() != ISD::Constant &&
3278 LHSOp.getOpcode() != ISD::ConstantFP) ||
3279 (RHSOp.getOpcode() != ISD::UNDEF &&
3280 RHSOp.getOpcode() != ISD::Constant &&
3281 RHSOp.getOpcode() != ISD::ConstantFP))
3282 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00003283 // Can't fold divide by zero.
3284 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
3285 if ((RHSOp.getOpcode() == ISD::Constant &&
3286 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
3287 (RHSOp.getOpcode() == ISD::ConstantFP &&
3288 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
3289 break;
3290 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003291 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00003292 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00003293 assert((Ops.back().getOpcode() == ISD::UNDEF ||
3294 Ops.back().getOpcode() == ISD::Constant ||
3295 Ops.back().getOpcode() == ISD::ConstantFP) &&
3296 "Scalar binop didn't fold!");
3297 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003298
3299 if (Ops.size() == LHS.getNumOperands()-2) {
3300 Ops.push_back(*(LHS.Val->op_end()-2));
3301 Ops.push_back(*(LHS.Val->op_end()-1));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003302 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003303 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003304 }
3305
3306 return SDOperand();
3307}
3308
Nate Begeman44728a72005-09-19 22:34:01 +00003309SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00003310 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
3311
3312 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
3313 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3314 // If we got a simplified select_cc node back from SimplifySelectCC, then
3315 // break it down into a new SETCC node, and a new SELECT node, and then return
3316 // the SELECT node, since we were called with a SELECT node.
3317 if (SCC.Val) {
3318 // Check to see if we got a select_cc back (to turn into setcc/select).
3319 // Otherwise, just return whatever node we got back, like fabs.
3320 if (SCC.getOpcode() == ISD::SELECT_CC) {
3321 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
3322 SCC.getOperand(0), SCC.getOperand(1),
3323 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00003324 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003325 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
3326 SCC.getOperand(3), SETCC);
3327 }
3328 return SCC;
3329 }
Nate Begeman44728a72005-09-19 22:34:01 +00003330 return SDOperand();
3331}
3332
Chris Lattner40c62d52005-10-18 06:04:22 +00003333/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
3334/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00003335/// select. Callers of this should assume that TheSelect is deleted if this
3336/// returns true. As such, they should return the appropriate thing (e.g. the
3337/// node) back to the top-level of the DAG combiner loop to avoid it being
3338/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00003339///
3340bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
3341 SDOperand RHS) {
3342
3343 // If this is a select from two identical things, try to pull the operation
3344 // through the select.
3345 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00003346 // If this is a load and the token chain is identical, replace the select
3347 // of two loads with a load through a select of the address to load from.
3348 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
3349 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00003350 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00003351 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00003352 LHS.getOperand(0) == RHS.getOperand(0)) {
3353 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
3354 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
3355
3356 // If this is an EXTLOAD, the VT's must match.
Evan Cheng2e49f092006-10-11 07:10:22 +00003357 if (LLD->getLoadedVT() == RLD->getLoadedVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003358 // FIXME: this conflates two src values, discarding one. This is not
3359 // the right thing to do, but nothing uses srcvalues now. When they do,
3360 // turn SrcValue into a list of locations.
3361 SDOperand Addr;
3362 if (TheSelect->getOpcode() == ISD::SELECT)
3363 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
3364 TheSelect->getOperand(0), LLD->getBasePtr(),
3365 RLD->getBasePtr());
3366 else
3367 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
3368 TheSelect->getOperand(0),
3369 TheSelect->getOperand(1),
3370 LLD->getBasePtr(), RLD->getBasePtr(),
3371 TheSelect->getOperand(4));
Chris Lattner40c62d52005-10-18 06:04:22 +00003372
Evan Cheng466685d2006-10-09 20:57:25 +00003373 SDOperand Load;
3374 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
3375 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
3376 Addr,LLD->getSrcValue(), LLD->getSrcValueOffset());
3377 else {
3378 Load = DAG.getExtLoad(LLD->getExtensionType(),
3379 TheSelect->getValueType(0),
3380 LLD->getChain(), Addr, LLD->getSrcValue(),
3381 LLD->getSrcValueOffset(),
Evan Cheng2e49f092006-10-11 07:10:22 +00003382 LLD->getLoadedVT());
Evan Cheng466685d2006-10-09 20:57:25 +00003383 }
3384 // Users of the select now use the result of the load.
3385 CombineTo(TheSelect, Load);
3386
3387 // Users of the old loads now use the new load's chain. We know the
3388 // old-load value is dead now.
3389 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
3390 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
3391 return true;
Evan Chengc5484282006-10-04 00:56:09 +00003392 }
Chris Lattner40c62d52005-10-18 06:04:22 +00003393 }
3394 }
3395
3396 return false;
3397}
3398
Nate Begeman44728a72005-09-19 22:34:01 +00003399SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
3400 SDOperand N2, SDOperand N3,
3401 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00003402
3403 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00003404 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
3405 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
3406 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
3407
3408 // Determine if the condition we're dealing with is constant
3409 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003410 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003411 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
3412
3413 // fold select_cc true, x, y -> x
3414 if (SCCC && SCCC->getValue())
3415 return N2;
3416 // fold select_cc false, x, y -> y
3417 if (SCCC && SCCC->getValue() == 0)
3418 return N3;
3419
3420 // Check to see if we can simplify the select into an fabs node
3421 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
3422 // Allow either -0.0 or 0.0
3423 if (CFP->getValue() == 0.0) {
3424 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
3425 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
3426 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
3427 N2 == N3.getOperand(0))
3428 return DAG.getNode(ISD::FABS, VT, N0);
3429
3430 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
3431 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3432 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3433 N2.getOperand(0) == N3)
3434 return DAG.getNode(ISD::FABS, VT, N3);
3435 }
3436 }
3437
3438 // Check to see if we can perform the "gzip trick", transforming
3439 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00003440 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00003441 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00003442 MVT::isInteger(N2.getValueType()) &&
3443 (N1C->isNullValue() || // (a < 0) ? b : 0
3444 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00003445 MVT::ValueType XType = N0.getValueType();
3446 MVT::ValueType AType = N2.getValueType();
3447 if (XType >= AType) {
3448 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00003449 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00003450 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3451 unsigned ShCtV = Log2_64(N2C->getValue());
3452 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3453 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3454 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00003455 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003456 if (XType > AType) {
3457 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003458 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003459 }
3460 return DAG.getNode(ISD::AND, AType, Shift, N2);
3461 }
3462 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3463 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3464 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003465 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003466 if (XType > AType) {
3467 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003468 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003469 }
3470 return DAG.getNode(ISD::AND, AType, Shift, N2);
3471 }
3472 }
Nate Begeman07ed4172005-10-10 21:26:48 +00003473
3474 // fold select C, 16, 0 -> shl C, 4
3475 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3476 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3477 // Get a SetCC of the condition
3478 // FIXME: Should probably make sure that setcc is legal if we ever have a
3479 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00003480 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00003481 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00003482 if (AfterLegalize) {
3483 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003484 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00003485 } else {
3486 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003487 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00003488 }
Chris Lattner5750df92006-03-01 04:03:14 +00003489 AddToWorkList(SCC.Val);
3490 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00003491 // shl setcc result by log2 n2c
3492 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3493 DAG.getConstant(Log2_64(N2C->getValue()),
3494 TLI.getShiftAmountTy()));
3495 }
3496
Nate Begemanf845b452005-10-08 00:29:44 +00003497 // Check to see if this is the equivalent of setcc
3498 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3499 // otherwise, go ahead with the folds.
3500 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3501 MVT::ValueType XType = N0.getValueType();
3502 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3503 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3504 if (Res.getValueType() != VT)
3505 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3506 return Res;
3507 }
3508
3509 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3510 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3511 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3512 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3513 return DAG.getNode(ISD::SRL, XType, Ctlz,
3514 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3515 TLI.getShiftAmountTy()));
3516 }
3517 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3518 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3519 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3520 N0);
3521 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3522 DAG.getConstant(~0ULL, XType));
3523 return DAG.getNode(ISD::SRL, XType,
3524 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3525 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3526 TLI.getShiftAmountTy()));
3527 }
3528 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3529 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3530 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3531 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3532 TLI.getShiftAmountTy()));
3533 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3534 }
3535 }
3536
3537 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3538 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3539 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3540 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3541 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3542 MVT::ValueType XType = N0.getValueType();
3543 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3544 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3545 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3546 TLI.getShiftAmountTy()));
3547 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003548 AddToWorkList(Shift.Val);
3549 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003550 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3551 }
3552 }
3553 }
3554
Nate Begeman44728a72005-09-19 22:34:01 +00003555 return SDOperand();
3556}
3557
Nate Begeman452d7be2005-09-16 00:54:12 +00003558SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00003559 SDOperand N1, ISD::CondCode Cond,
3560 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003561 // These setcc operations always fold.
3562 switch (Cond) {
3563 default: break;
3564 case ISD::SETFALSE:
3565 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3566 case ISD::SETTRUE:
3567 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3568 }
3569
3570 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3571 uint64_t C1 = N1C->getValue();
3572 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
Chris Lattner51dabfb2006-10-14 00:41:01 +00003573 return DAG.FoldSetCC(VT, N0, N1, Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00003574 } else {
Chris Lattner5f42a242006-09-20 06:19:26 +00003575 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
3576 // equality comparison, then we're just comparing whether X itself is
3577 // zero.
3578 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
3579 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
3580 N0.getOperand(1).getOpcode() == ISD::Constant) {
3581 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
3582 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3583 ShAmt == Log2_32(MVT::getSizeInBits(N0.getValueType()))) {
3584 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
3585 // (srl (ctlz x), 5) == 0 -> X != 0
3586 // (srl (ctlz x), 5) != 1 -> X != 0
3587 Cond = ISD::SETNE;
3588 } else {
3589 // (srl (ctlz x), 5) != 0 -> X == 0
3590 // (srl (ctlz x), 5) == 1 -> X == 0
3591 Cond = ISD::SETEQ;
3592 }
3593 SDOperand Zero = DAG.getConstant(0, N0.getValueType());
3594 return DAG.getSetCC(VT, N0.getOperand(0).getOperand(0),
3595 Zero, Cond);
3596 }
3597 }
3598
Nate Begeman452d7be2005-09-16 00:54:12 +00003599 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3600 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3601 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3602
3603 // If the comparison constant has bits in the upper part, the
3604 // zero-extended value could never match.
3605 if (C1 & (~0ULL << InSize)) {
3606 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3607 switch (Cond) {
3608 case ISD::SETUGT:
3609 case ISD::SETUGE:
3610 case ISD::SETEQ: return DAG.getConstant(0, VT);
3611 case ISD::SETULT:
3612 case ISD::SETULE:
3613 case ISD::SETNE: return DAG.getConstant(1, VT);
3614 case ISD::SETGT:
3615 case ISD::SETGE:
3616 // True if the sign bit of C1 is set.
3617 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3618 case ISD::SETLT:
3619 case ISD::SETLE:
3620 // True if the sign bit of C1 isn't set.
3621 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3622 default:
3623 break;
3624 }
3625 }
3626
3627 // Otherwise, we can perform the comparison with the low bits.
3628 switch (Cond) {
3629 case ISD::SETEQ:
3630 case ISD::SETNE:
3631 case ISD::SETUGT:
3632 case ISD::SETUGE:
3633 case ISD::SETULT:
3634 case ISD::SETULE:
3635 return DAG.getSetCC(VT, N0.getOperand(0),
3636 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3637 Cond);
3638 default:
3639 break; // todo, be more careful with signed comparisons
3640 }
3641 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3642 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3643 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3644 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3645 MVT::ValueType ExtDstTy = N0.getValueType();
3646 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3647
3648 // If the extended part has any inconsistent bits, it cannot ever
3649 // compare equal. In other words, they have to be all ones or all
3650 // zeros.
3651 uint64_t ExtBits =
3652 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3653 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3654 return DAG.getConstant(Cond == ISD::SETNE, VT);
3655
3656 SDOperand ZextOp;
3657 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3658 if (Op0Ty == ExtSrcTy) {
3659 ZextOp = N0.getOperand(0);
3660 } else {
3661 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3662 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3663 DAG.getConstant(Imm, Op0Ty));
3664 }
Chris Lattner5750df92006-03-01 04:03:14 +00003665 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003666 // Otherwise, make this a use of a zext.
3667 return DAG.getSetCC(VT, ZextOp,
3668 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3669 ExtDstTy),
3670 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00003671 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
Chris Lattner8ac9d0e2006-10-14 01:02:29 +00003672 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3673
3674 // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC
3675 if (N0.getOpcode() == ISD::SETCC) {
3676 bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getValue() != 1);
3677 if (TrueWhenTrue)
3678 return N0;
3679
3680 // Invert the condition.
3681 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
3682 CC = ISD::getSetCCInverse(CC,
3683 MVT::isInteger(N0.getOperand(0).getValueType()));
3684 return DAG.getSetCC(VT, N0.getOperand(0), N0.getOperand(1), CC);
3685 }
3686
3687 if ((N0.getOpcode() == ISD::XOR ||
3688 (N0.getOpcode() == ISD::AND &&
3689 N0.getOperand(0).getOpcode() == ISD::XOR &&
3690 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3691 isa<ConstantSDNode>(N0.getOperand(1)) &&
3692 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
3693 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We
3694 // can only do this if the top bits are known zero.
3695 if (TLI.MaskedValueIsZero(N1,
3696 MVT::getIntVTBitMask(N0.getValueType())-1)){
3697 // Okay, get the un-inverted input value.
3698 SDOperand Val;
3699 if (N0.getOpcode() == ISD::XOR)
3700 Val = N0.getOperand(0);
3701 else {
3702 assert(N0.getOpcode() == ISD::AND &&
3703 N0.getOperand(0).getOpcode() == ISD::XOR);
3704 // ((X^1)&1)^1 -> X & 1
3705 Val = DAG.getNode(ISD::AND, N0.getValueType(),
3706 N0.getOperand(0).getOperand(0),
3707 N0.getOperand(1));
3708 }
3709 return DAG.getSetCC(VT, Val, N1,
3710 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
Chris Lattner3391bcd2006-02-08 02:13:15 +00003711 }
Chris Lattner3391bcd2006-02-08 02:13:15 +00003712 }
Nate Begeman452d7be2005-09-16 00:54:12 +00003713 }
Chris Lattner5c46f742005-10-05 06:11:08 +00003714
Nate Begeman452d7be2005-09-16 00:54:12 +00003715 uint64_t MinVal, MaxVal;
3716 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
3717 if (ISD::isSignedIntSetCC(Cond)) {
3718 MinVal = 1ULL << (OperandBitSize-1);
3719 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
3720 MaxVal = ~0ULL >> (65-OperandBitSize);
3721 else
3722 MaxVal = 0;
3723 } else {
3724 MinVal = 0;
3725 MaxVal = ~0ULL >> (64-OperandBitSize);
3726 }
3727
3728 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
3729 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
3730 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
3731 --C1; // X >= C0 --> X > (C0-1)
3732 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3733 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
3734 }
3735
3736 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
3737 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
3738 ++C1; // X <= C0 --> X < (C0+1)
3739 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3740 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
3741 }
3742
3743 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
3744 return DAG.getConstant(0, VT); // X < MIN --> false
3745
3746 // Canonicalize setgt X, Min --> setne X, Min
3747 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
3748 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00003749 // Canonicalize setlt X, Max --> setne X, Max
3750 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
3751 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00003752
3753 // If we have setult X, 1, turn it into seteq X, 0
3754 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
3755 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
3756 ISD::SETEQ);
3757 // If we have setugt X, Max-1, turn it into seteq X, Max
3758 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
3759 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
3760 ISD::SETEQ);
3761
3762 // If we have "setcc X, C0", check to see if we can shrink the immediate
3763 // by changing cc.
3764
3765 // SETUGT X, SINTMAX -> SETLT X, 0
3766 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
3767 C1 == (~0ULL >> (65-OperandBitSize)))
3768 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
3769 ISD::SETLT);
3770
3771 // FIXME: Implement the rest of these.
3772
3773 // Fold bit comparisons when we can.
3774 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3775 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
3776 if (ConstantSDNode *AndRHS =
3777 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3778 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
3779 // Perform the xform if the AND RHS is a single bit.
Chris Lattner51dabfb2006-10-14 00:41:01 +00003780 if (isPowerOf2_64(AndRHS->getValue())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003781 return DAG.getNode(ISD::SRL, VT, N0,
3782 DAG.getConstant(Log2_64(AndRHS->getValue()),
3783 TLI.getShiftAmountTy()));
3784 }
3785 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
3786 // (X & 8) == 8 --> (X & 8) >> 3
3787 // Perform the xform if C1 is a single bit.
Chris Lattner51dabfb2006-10-14 00:41:01 +00003788 if (isPowerOf2_64(C1)) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003789 return DAG.getNode(ISD::SRL, VT, N0,
Chris Lattner729c6d12006-05-27 00:43:02 +00003790 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
Nate Begeman452d7be2005-09-16 00:54:12 +00003791 }
3792 }
3793 }
3794 }
3795 } else if (isa<ConstantSDNode>(N0.Val)) {
3796 // Ensure that the constant occurs on the RHS.
3797 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3798 }
3799
Chris Lattner51dabfb2006-10-14 00:41:01 +00003800 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val)) {
3801 // Constant fold or commute setcc.
3802 SDOperand O = DAG.FoldSetCC(VT, N0, N1, Cond);
3803 if (O.Val) return O;
3804 }
Nate Begeman452d7be2005-09-16 00:54:12 +00003805
3806 if (N0 == N1) {
Chris Lattner8ac9d0e2006-10-14 01:02:29 +00003807 // We can always fold X == X for integer setcc's.
Nate Begeman452d7be2005-09-16 00:54:12 +00003808 if (MVT::isInteger(N0.getValueType()))
3809 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3810 unsigned UOF = ISD::getUnorderedFlavor(Cond);
3811 if (UOF == 2) // FP operators that are undefined on NaNs.
3812 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3813 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
3814 return DAG.getConstant(UOF, VT);
3815 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
3816 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00003817 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00003818 if (NewCond != Cond)
3819 return DAG.getSetCC(VT, N0, N1, NewCond);
3820 }
3821
3822 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3823 MVT::isInteger(N0.getValueType())) {
3824 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
3825 N0.getOpcode() == ISD::XOR) {
3826 // Simplify (X+Y) == (X+Z) --> Y == Z
3827 if (N0.getOpcode() == N1.getOpcode()) {
3828 if (N0.getOperand(0) == N1.getOperand(0))
3829 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
3830 if (N0.getOperand(1) == N1.getOperand(1))
3831 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
Evan Cheng1efba0e2006-08-29 06:42:35 +00003832 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003833 // If X op Y == Y op X, try other combinations.
3834 if (N0.getOperand(0) == N1.getOperand(1))
3835 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
3836 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00003837 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00003838 }
3839 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003840
3841 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
3842 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3843 // Turn (X+C1) == C2 --> X == C2-C1
3844 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
3845 return DAG.getSetCC(VT, N0.getOperand(0),
3846 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
3847 N0.getValueType()), Cond);
3848 }
3849
3850 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
3851 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00003852 // If we know that all of the inverted bits are zero, don't bother
3853 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003854 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00003855 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003856 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00003857 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003858 }
3859
3860 // Turn (C1-X) == C2 --> X == C1-C2
3861 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
3862 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
3863 return DAG.getSetCC(VT, N0.getOperand(1),
3864 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
3865 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00003866 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003867 }
3868 }
3869
Nate Begeman452d7be2005-09-16 00:54:12 +00003870 // Simplify (X+Z) == X --> Z == 0
3871 if (N0.getOperand(0) == N1)
3872 return DAG.getSetCC(VT, N0.getOperand(1),
3873 DAG.getConstant(0, N0.getValueType()), Cond);
3874 if (N0.getOperand(1) == N1) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00003875 if (DAG.isCommutativeBinOp(N0.getOpcode()))
Nate Begeman452d7be2005-09-16 00:54:12 +00003876 return DAG.getSetCC(VT, N0.getOperand(0),
3877 DAG.getConstant(0, N0.getValueType()), Cond);
3878 else {
3879 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
3880 // (Z-X) == X --> Z == X<<1
3881 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
3882 N1,
3883 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003884 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003885 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
3886 }
3887 }
3888 }
3889
3890 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3891 N1.getOpcode() == ISD::XOR) {
3892 // Simplify X == (X+Z) --> Z == 0
3893 if (N1.getOperand(0) == N0) {
3894 return DAG.getSetCC(VT, N1.getOperand(1),
3895 DAG.getConstant(0, N1.getValueType()), Cond);
3896 } else if (N1.getOperand(1) == N0) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00003897 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003898 return DAG.getSetCC(VT, N1.getOperand(0),
3899 DAG.getConstant(0, N1.getValueType()), Cond);
3900 } else {
3901 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3902 // X == (Z-X) --> X<<1 == Z
3903 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
3904 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003905 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003906 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
3907 }
3908 }
3909 }
3910 }
3911
3912 // Fold away ALL boolean setcc's.
3913 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00003914 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003915 switch (Cond) {
3916 default: assert(0 && "Unknown integer setcc!");
3917 case ISD::SETEQ: // X == Y -> (X^Y)^1
3918 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3919 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00003920 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003921 break;
3922 case ISD::SETNE: // X != Y --> (X^Y)
3923 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3924 break;
3925 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
3926 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
3927 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3928 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003929 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003930 break;
3931 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
3932 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
3933 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3934 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003935 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003936 break;
3937 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
3938 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
3939 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3940 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003941 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003942 break;
3943 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
3944 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
3945 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3946 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
3947 break;
3948 }
3949 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00003950 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003951 // FIXME: If running after legalize, we probably can't do this.
3952 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3953 }
3954 return N0;
3955 }
3956
3957 // Could not fold it.
3958 return SDOperand();
3959}
3960
Nate Begeman69575232005-10-20 02:15:44 +00003961/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3962/// return a DAG expression to select that will generate the same value by
3963/// multiplying by a magic number. See:
3964/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3965SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003966 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003967 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
3968
Andrew Lenharth232c9102006-06-12 16:07:18 +00003969 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003970 ii != ee; ++ii)
3971 AddToWorkList(*ii);
3972 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003973}
3974
3975/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3976/// return a DAG expression to select that will generate the same value by
3977/// multiplying by a magic number. See:
3978/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3979SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003980 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003981 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00003982
Andrew Lenharth232c9102006-06-12 16:07:18 +00003983 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003984 ii != ee; ++ii)
3985 AddToWorkList(*ii);
3986 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003987}
3988
Jim Laskey71382342006-10-07 23:37:56 +00003989/// FindBaseOffset - Return true if base is known not to alias with anything
3990/// but itself. Provides base object and offset as results.
3991static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
3992 // Assume it is a primitive operation.
3993 Base = Ptr; Offset = 0;
3994
3995 // If it's an adding a simple constant then integrate the offset.
3996 if (Base.getOpcode() == ISD::ADD) {
3997 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
3998 Base = Base.getOperand(0);
3999 Offset += C->getValue();
4000 }
4001 }
4002
4003 // If it's any of the following then it can't alias with anything but itself.
4004 return isa<FrameIndexSDNode>(Base) ||
4005 isa<ConstantPoolSDNode>(Base) ||
4006 isa<GlobalAddressSDNode>(Base);
4007}
4008
4009/// isAlias - Return true if there is any possibility that the two addresses
4010/// overlap.
Jim Laskey7ca56af2006-10-11 13:47:09 +00004011static bool isAlias(SDOperand Ptr1, int64_t Size1, const Value *SrcValue1,
4012 SDOperand Ptr2, int64_t Size2, const Value *SrcValue2) {
Jim Laskey71382342006-10-07 23:37:56 +00004013 // If they are the same then they must be aliases.
4014 if (Ptr1 == Ptr2) return true;
4015
4016 // Gather base node and offset information.
4017 SDOperand Base1, Base2;
4018 int64_t Offset1, Offset2;
4019 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
4020 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
4021
4022 // If they have a same base address then...
4023 if (Base1 == Base2) {
4024 // Check to see if the addresses overlap.
4025 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
4026 }
4027
4028 // Otherwise they alias if either is unknown.
4029 return !KnownBase1 || !KnownBase2;
4030}
4031
4032/// FindAliasInfo - Extracts the relevant alias information from the memory
4033/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00004034bool DAGCombiner::FindAliasInfo(SDNode *N,
4035 SDOperand &Ptr, int64_t &Size, const Value *&SrcValue) {
4036 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4037 Ptr = LD->getBasePtr();
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004038 Size = MVT::getSizeInBits(LD->getLoadedVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004039 SrcValue = LD->getSrcValue();
Jim Laskey71382342006-10-07 23:37:56 +00004040 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004041 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004042 Ptr = ST->getBasePtr();
Evan Cheng8b2794a2006-10-13 21:14:26 +00004043 Size = MVT::getSizeInBits(ST->getStoredVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004044 SrcValue = ST->getSrcValue();
Jim Laskey7ca56af2006-10-11 13:47:09 +00004045 } else {
Jim Laskey71382342006-10-07 23:37:56 +00004046 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00004047 }
4048
4049 return false;
4050}
4051
Jim Laskey6ff23e52006-10-04 16:53:27 +00004052/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
4053/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00004054void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00004055 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004056 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00004057 std::set<SDNode *> Visited; // Visited node set.
4058
Jim Laskey279f0532006-09-25 16:29:54 +00004059 // Get alias information for node.
4060 SDOperand Ptr;
4061 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004062 const Value *SrcValue;
4063 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue);
Jim Laskey279f0532006-09-25 16:29:54 +00004064
Jim Laskey6ff23e52006-10-04 16:53:27 +00004065 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00004066 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00004067
Jim Laskeybc588b82006-10-05 15:07:25 +00004068 // Look at each chain and determine if it is an alias. If so, add it to the
4069 // aliases list. If not, then continue up the chain looking for the next
4070 // candidate.
4071 while (!Chains.empty()) {
4072 SDOperand Chain = Chains.back();
4073 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00004074
Jim Laskeybc588b82006-10-05 15:07:25 +00004075 // Don't bother if we've been before.
4076 if (Visited.find(Chain.Val) != Visited.end()) continue;
4077 Visited.insert(Chain.Val);
4078
4079 switch (Chain.getOpcode()) {
4080 case ISD::EntryToken:
4081 // Entry token is ideal chain operand, but handled in FindBetterChain.
4082 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00004083
Jim Laskeybc588b82006-10-05 15:07:25 +00004084 case ISD::LOAD:
4085 case ISD::STORE: {
4086 // Get alias information for Chain.
4087 SDOperand OpPtr;
4088 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004089 const Value *OpSrcValue;
4090 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize, OpSrcValue);
Jim Laskeybc588b82006-10-05 15:07:25 +00004091
4092 // If chain is alias then stop here.
4093 if (!(IsLoad && IsOpLoad) &&
4094 isAlias(Ptr, Size, SrcValue, OpPtr, OpSize, OpSrcValue)) {
4095 Aliases.push_back(Chain);
4096 } else {
4097 // Look further up the chain.
4098 Chains.push_back(Chain.getOperand(0));
4099 // Clean up old chain.
4100 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00004101 }
Jim Laskeybc588b82006-10-05 15:07:25 +00004102 break;
4103 }
4104
4105 case ISD::TokenFactor:
4106 // We have to check each of the operands of the token factor, so we queue
4107 // then up. Adding the operands to the queue (stack) in reverse order
4108 // maintains the original order and increases the likelihood that getNode
4109 // will find a matching token factor (CSE.)
4110 for (unsigned n = Chain.getNumOperands(); n;)
4111 Chains.push_back(Chain.getOperand(--n));
4112 // Eliminate the token factor if we can.
4113 AddToWorkList(Chain.Val);
4114 break;
4115
4116 default:
4117 // For all other instructions we will just have to take what we can get.
4118 Aliases.push_back(Chain);
4119 break;
Jim Laskey279f0532006-09-25 16:29:54 +00004120 }
4121 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004122}
4123
4124/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
4125/// for a better chain (aliasing node.)
4126SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
4127 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00004128
Jim Laskey6ff23e52006-10-04 16:53:27 +00004129 // Accumulate all the aliases to this node.
4130 GatherAllAliases(N, OldChain, Aliases);
4131
4132 if (Aliases.size() == 0) {
4133 // If no operands then chain to entry token.
4134 return DAG.getEntryNode();
4135 } else if (Aliases.size() == 1) {
4136 // If a single operand then chain to it. We don't need to revisit it.
4137 return Aliases[0];
4138 }
4139
4140 // Construct a custom tailored token factor.
4141 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4142 &Aliases[0], Aliases.size());
4143
4144 // Make sure the old chain gets cleaned up.
4145 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
4146
4147 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00004148}
4149
Nate Begeman1d4d4142005-09-01 00:19:25 +00004150// SelectionDAG::Combine - This is the entry point for the file.
4151//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004152void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00004153 /// run - This is the main entry point to this class.
4154 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004155 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004156}