blob: 90df43524394b87946e21b8e1a29a2853e4f3445 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
12//
13// FIXME: Missing folds
14// sdiv, udiv, srem, urem (X, const) where X is an integer can be expanded into
15// a sequence of multiplies, shifts, and adds. This should be controlled by
16// some kind of hint from the target that int div is expensive.
17// various folds of mulh[s,u] by constants such as -1, powers of 2, etc.
18//
Nate Begeman44728a72005-09-19 22:34:01 +000019// FIXME: select C, pow2, pow2 -> something smart
20// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
Nate Begeman44728a72005-09-19 22:34:01 +000021// FIXME: Dead stores -> nuke
Chris Lattner40c62d52005-10-18 06:04:22 +000022// FIXME: shr X, (and Y,31) -> shr X, Y (TRICKY!)
Nate Begeman1d4d4142005-09-01 00:19:25 +000023// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000024// FIXME: undef values
Nate Begeman646d7e22005-09-02 21:18:40 +000025// FIXME: divide by zero is currently left unfolded. do we want to turn this
26// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000027// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Nate Begeman1d4d4142005-09-01 00:19:25 +000028//
29//===----------------------------------------------------------------------===//
30
31#define DEBUG_TYPE "dagcombine"
32#include "llvm/ADT/Statistic.h"
33#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000034#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000035#include "llvm/Support/MathExtras.h"
36#include "llvm/Target/TargetLowering.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000037#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000038#include "llvm/Support/CommandLine.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000039#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000040#include <cmath>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000041#include <iostream>
Jim Laskey279f0532006-09-25 16:29:54 +000042#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000043using namespace llvm;
44
45namespace {
Andrew Lenharthae6153f2006-07-20 17:43:27 +000046 static Statistic<> NodesCombined ("dagcombiner",
47 "Number of dag nodes combined");
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000048
Nate Begeman1d4d4142005-09-01 00:19:25 +000049
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000050static cl::opt<bool>
51 CombinerAA("combiner-alias-analysis", cl::Hidden,
52 cl::desc("Turn on alias analysis turning testing"));
Jim Laskey6ff23e52006-10-04 16:53:27 +000053
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000054class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000055 SelectionDAG &DAG;
56 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000057 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000058
59 // Worklist of all of the nodes that need to be simplified.
60 std::vector<SDNode*> WorkList;
61
62 /// AddUsersToWorkList - When an instruction is simplified, add all users of
63 /// the instruction to the work lists because they might get more simplified
64 /// now.
65 ///
66 void AddUsersToWorkList(SDNode *N) {
67 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000068 UI != UE; ++UI)
Jim Laskey6ff23e52006-10-04 16:53:27 +000069 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000070 }
71
72 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000073 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000074 void removeFromWorkList(SDNode *N) {
75 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
76 WorkList.end());
77 }
78
Chris Lattner24664722006-03-01 04:53:38 +000079 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000080 /// AddToWorkList - Add to the work list making sure it's instance is at the
81 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000082 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000083 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000084 WorkList.push_back(N);
85 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000086
Chris Lattner3577e382006-08-11 17:56:38 +000087 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo) {
88 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
Chris Lattner87514ca2005-10-10 22:31:19 +000089 ++NodesCombined;
Jim Laskey6ff23e52006-10-04 16:53:27 +000090 DEBUG(std::cerr << "\nReplacing.1 "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +000091 std::cerr << "\nWith: "; To[0].Val->dump(&DAG);
Chris Lattner3577e382006-08-11 17:56:38 +000092 std::cerr << " and " << NumTo-1 << " other values\n");
Chris Lattner01a22022005-10-10 22:04:48 +000093 std::vector<SDNode*> NowDead;
Chris Lattner3577e382006-08-11 17:56:38 +000094 DAG.ReplaceAllUsesWith(N, To, &NowDead);
Chris Lattner01a22022005-10-10 22:04:48 +000095
96 // Push the new nodes and any users onto the worklist
Chris Lattner3577e382006-08-11 17:56:38 +000097 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000098 AddToWorkList(To[i].Val);
Chris Lattner01a22022005-10-10 22:04:48 +000099 AddUsersToWorkList(To[i].Val);
100 }
101
Jim Laskey6ff23e52006-10-04 16:53:27 +0000102 // Nodes can be reintroduced into the worklist. Make sure we do not
103 // process a node that has been replaced.
Chris Lattner01a22022005-10-10 22:04:48 +0000104 removeFromWorkList(N);
105 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
106 removeFromWorkList(NowDead[i]);
107
108 // Finally, since the node is now dead, remove it from the graph.
109 DAG.DeleteNode(N);
110 return SDOperand(N, 0);
111 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000112
Chris Lattner24664722006-03-01 04:53:38 +0000113 SDOperand CombineTo(SDNode *N, SDOperand Res) {
Chris Lattner3577e382006-08-11 17:56:38 +0000114 return CombineTo(N, &Res, 1);
Chris Lattner24664722006-03-01 04:53:38 +0000115 }
116
117 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
Chris Lattner3577e382006-08-11 17:56:38 +0000118 SDOperand To[] = { Res0, Res1 };
119 return CombineTo(N, To, 2);
Chris Lattner24664722006-03-01 04:53:38 +0000120 }
121 private:
122
Chris Lattner012f2412006-02-17 21:58:01 +0000123 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000124 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000125 /// propagation. If so, return true.
126 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000127 TargetLowering::TargetLoweringOpt TLO(DAG);
128 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000129 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
130 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
131 return false;
132
133 // Revisit the node.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000134 AddToWorkList(Op.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000135
136 // Replace the old value with the new one.
137 ++NodesCombined;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000138 DEBUG(std::cerr << "\nReplacing.2 "; TLO.Old.Val->dump();
Jim Laskey279f0532006-09-25 16:29:54 +0000139 std::cerr << "\nWith: "; TLO.New.Val->dump(&DAG);
140 std::cerr << '\n');
Chris Lattner012f2412006-02-17 21:58:01 +0000141
142 std::vector<SDNode*> NowDead;
143 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
144
Chris Lattner7d20d392006-02-20 06:51:04 +0000145 // Push the new node and any (possibly new) users onto the worklist.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000146 AddToWorkList(TLO.New.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000147 AddUsersToWorkList(TLO.New.Val);
148
149 // Nodes can end up on the worklist more than once. Make sure we do
150 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000151 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
152 removeFromWorkList(NowDead[i]);
153
Chris Lattner7d20d392006-02-20 06:51:04 +0000154 // Finally, if the node is now dead, remove it from the graph. The node
155 // may not be dead if the replacement process recursively simplified to
156 // something else needing this node.
157 if (TLO.Old.Val->use_empty()) {
158 removeFromWorkList(TLO.Old.Val);
159 DAG.DeleteNode(TLO.Old.Val);
160 }
Chris Lattner012f2412006-02-17 21:58:01 +0000161 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000162 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000163
Nate Begeman1d4d4142005-09-01 00:19:25 +0000164 /// visit - call the node-specific routine that knows how to fold each
165 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000166 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000167
168 // Visitation implementation - Implement dag node combining for different
169 // node types. The semantics are as follows:
170 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000171 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000172 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000173 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000174 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000175 SDOperand visitTokenFactor(SDNode *N);
176 SDOperand visitADD(SDNode *N);
177 SDOperand visitSUB(SDNode *N);
178 SDOperand visitMUL(SDNode *N);
179 SDOperand visitSDIV(SDNode *N);
180 SDOperand visitUDIV(SDNode *N);
181 SDOperand visitSREM(SDNode *N);
182 SDOperand visitUREM(SDNode *N);
183 SDOperand visitMULHU(SDNode *N);
184 SDOperand visitMULHS(SDNode *N);
185 SDOperand visitAND(SDNode *N);
186 SDOperand visitOR(SDNode *N);
187 SDOperand visitXOR(SDNode *N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000188 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000189 SDOperand visitSHL(SDNode *N);
190 SDOperand visitSRA(SDNode *N);
191 SDOperand visitSRL(SDNode *N);
192 SDOperand visitCTLZ(SDNode *N);
193 SDOperand visitCTTZ(SDNode *N);
194 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000195 SDOperand visitSELECT(SDNode *N);
196 SDOperand visitSELECT_CC(SDNode *N);
197 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000198 SDOperand visitSIGN_EXTEND(SDNode *N);
199 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000200 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000201 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
202 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000203 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000204 SDOperand visitVBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000205 SDOperand visitFADD(SDNode *N);
206 SDOperand visitFSUB(SDNode *N);
207 SDOperand visitFMUL(SDNode *N);
208 SDOperand visitFDIV(SDNode *N);
209 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000210 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000211 SDOperand visitSINT_TO_FP(SDNode *N);
212 SDOperand visitUINT_TO_FP(SDNode *N);
213 SDOperand visitFP_TO_SINT(SDNode *N);
214 SDOperand visitFP_TO_UINT(SDNode *N);
215 SDOperand visitFP_ROUND(SDNode *N);
216 SDOperand visitFP_ROUND_INREG(SDNode *N);
217 SDOperand visitFP_EXTEND(SDNode *N);
218 SDOperand visitFNEG(SDNode *N);
219 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000220 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000221 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000222 SDOperand visitLOAD(SDNode *N);
Evan Chengc5484282006-10-04 00:56:09 +0000223 SDOperand visitLOADX(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000224 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000225 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
226 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000227 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000228 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000229 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000230
Evan Cheng44f1f092006-04-20 08:56:16 +0000231 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000232 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
233
Chris Lattner40c62d52005-10-18 06:04:22 +0000234 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000235 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000236 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
237 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
238 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000239 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000240 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000241 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000242 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000243 SDOperand BuildUDIV(SDNode *N);
244 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Jim Laskey279f0532006-09-25 16:29:54 +0000245
Jim Laskey6ff23e52006-10-04 16:53:27 +0000246 /// FindBaseOffset - Return true if base is known not to alias with anything
247 /// but itself. Provides base object and offset as results.
Jim Laskey279f0532006-09-25 16:29:54 +0000248 static bool FindBaseOffset(SDOperand Ptr,
249 SDOperand &Object, int64_t &Offset);
250
Jim Laskey6ff23e52006-10-04 16:53:27 +0000251 /// isAlias - Return true if there is any possibility that the two addresses
Jim Laskey279f0532006-09-25 16:29:54 +0000252 /// overlap.
253 static bool isAlias(SDOperand Ptr1, int64_t Size1, SDOperand SrcValue1,
254 SDOperand Ptr2, int64_t Size2, SDOperand SrcValue2);
255
256 /// FindAliasInfo - Extracts the relevant alias information from the memory
257 /// node.
258 static void FindAliasInfo(SDNode *N,
259 SDOperand &Ptr, int64_t &Size, SDOperand &SrcValue);
260
Jim Laskey6ff23e52006-10-04 16:53:27 +0000261 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
262 /// looking for aliasing nodes and adding them to the Aliases vector.
263 void GatherAllAliases(SDNode *N, SDOperand Chain,
264 SmallVector<SDOperand, 8> &Aliases);
265
Jim Laskey279f0532006-09-25 16:29:54 +0000266 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000267 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000268 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
269
Nate Begeman1d4d4142005-09-01 00:19:25 +0000270public:
271 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000272 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000273
274 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000275 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000276 };
277}
278
Chris Lattner24664722006-03-01 04:53:38 +0000279//===----------------------------------------------------------------------===//
280// TargetLowering::DAGCombinerInfo implementation
281//===----------------------------------------------------------------------===//
282
283void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
284 ((DAGCombiner*)DC)->AddToWorkList(N);
285}
286
287SDOperand TargetLowering::DAGCombinerInfo::
288CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000289 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000290}
291
292SDOperand TargetLowering::DAGCombinerInfo::
293CombineTo(SDNode *N, SDOperand Res) {
294 return ((DAGCombiner*)DC)->CombineTo(N, Res);
295}
296
297
298SDOperand TargetLowering::DAGCombinerInfo::
299CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
300 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
301}
302
303
304
305
306//===----------------------------------------------------------------------===//
307
308
Nate Begeman4ebd8052005-09-01 23:24:04 +0000309// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
310// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000311// Also, set the incoming LHS, RHS, and CC references to the appropriate
312// nodes based on the type of node we are checking. This simplifies life a
313// bit for the callers.
314static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
315 SDOperand &CC) {
316 if (N.getOpcode() == ISD::SETCC) {
317 LHS = N.getOperand(0);
318 RHS = N.getOperand(1);
319 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000320 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000321 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000322 if (N.getOpcode() == ISD::SELECT_CC &&
323 N.getOperand(2).getOpcode() == ISD::Constant &&
324 N.getOperand(3).getOpcode() == ISD::Constant &&
325 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000326 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
327 LHS = N.getOperand(0);
328 RHS = N.getOperand(1);
329 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000330 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000331 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000332 return false;
333}
334
Nate Begeman99801192005-09-07 23:25:52 +0000335// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
336// one use. If this is true, it allows the users to invert the operation for
337// free when it is profitable to do so.
338static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000339 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000340 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000341 return true;
342 return false;
343}
344
Nate Begemancd4d58c2006-02-03 06:46:56 +0000345SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
346 MVT::ValueType VT = N0.getValueType();
347 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
348 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
349 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
350 if (isa<ConstantSDNode>(N1)) {
351 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000352 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000353 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
354 } else if (N0.hasOneUse()) {
355 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000356 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000357 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
358 }
359 }
360 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
361 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
362 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
363 if (isa<ConstantSDNode>(N0)) {
364 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000365 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000366 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
367 } else if (N1.hasOneUse()) {
368 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000369 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000370 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
371 }
372 }
373 return SDOperand();
374}
375
Nate Begeman4ebd8052005-09-01 23:24:04 +0000376void DAGCombiner::Run(bool RunningAfterLegalize) {
377 // set the instance variable, so that the various visit routines may use it.
378 AfterLegalize = RunningAfterLegalize;
379
Nate Begeman646d7e22005-09-02 21:18:40 +0000380 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000381 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
382 E = DAG.allnodes_end(); I != E; ++I)
383 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000384
Chris Lattner95038592005-10-05 06:35:28 +0000385 // Create a dummy node (which is not added to allnodes), that adds a reference
386 // to the root node, preventing it from being deleted, and tracking any
387 // changes of the root.
388 HandleSDNode Dummy(DAG.getRoot());
389
Chris Lattner24664722006-03-01 04:53:38 +0000390
391 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
392 TargetLowering::DAGCombinerInfo
393 DagCombineInfo(DAG, !RunningAfterLegalize, this);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000394
Nate Begeman1d4d4142005-09-01 00:19:25 +0000395 // while the worklist isn't empty, inspect the node on the end of it and
396 // try and combine it.
397 while (!WorkList.empty()) {
398 SDNode *N = WorkList.back();
399 WorkList.pop_back();
400
401 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000402 // N is deleted from the DAG, since they too may now be dead or may have a
403 // reduced number of uses, allowing other xforms.
404 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000405 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000406 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000407
Chris Lattner95038592005-10-05 06:35:28 +0000408 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000409 continue;
410 }
411
Nate Begeman83e75ec2005-09-06 04:43:02 +0000412 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000413
414 // If nothing happened, try a target-specific DAG combine.
415 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000416 assert(N->getOpcode() != ISD::DELETED_NODE &&
417 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000418 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
419 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
420 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
421 }
422
Nate Begeman83e75ec2005-09-06 04:43:02 +0000423 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000424 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000425 // If we get back the same node we passed in, rather than a new node or
426 // zero, we know that the node must have defined multiple values and
427 // CombineTo was used. Since CombineTo takes care of the worklist
428 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000429 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000430 assert(N->getOpcode() != ISD::DELETED_NODE &&
431 RV.Val->getOpcode() != ISD::DELETED_NODE &&
432 "Node was deleted but visit returned new node!");
433
Jim Laskey6ff23e52006-10-04 16:53:27 +0000434 DEBUG(std::cerr << "\nReplacing.3 "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000435 std::cerr << "\nWith: "; RV.Val->dump(&DAG);
Nate Begeman2300f552005-09-07 00:15:36 +0000436 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000437 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000438 if (N->getNumValues() == RV.Val->getNumValues())
439 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
440 else {
441 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
442 SDOperand OpV = RV;
443 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
444 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000445
446 // Push the new node and any users onto the worklist
Jim Laskey6ff23e52006-10-04 16:53:27 +0000447 AddToWorkList(RV.Val);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000448 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000449
Jim Laskey6ff23e52006-10-04 16:53:27 +0000450 // Nodes can be reintroduced into the worklist. Make sure we do not
451 // process a node that has been replaced.
Nate Begeman646d7e22005-09-02 21:18:40 +0000452 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000453 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
454 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000455
456 // Finally, since the node is now dead, remove it from the graph.
457 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000458 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000459 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000460
461// DetectCycle();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000462 }
Chris Lattner95038592005-10-05 06:35:28 +0000463
464 // If the root changed (e.g. it was a dead load, update the root).
465 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000466}
467
Nate Begeman83e75ec2005-09-06 04:43:02 +0000468SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000469 switch(N->getOpcode()) {
470 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000471 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000472 case ISD::ADD: return visitADD(N);
473 case ISD::SUB: return visitSUB(N);
474 case ISD::MUL: return visitMUL(N);
475 case ISD::SDIV: return visitSDIV(N);
476 case ISD::UDIV: return visitUDIV(N);
477 case ISD::SREM: return visitSREM(N);
478 case ISD::UREM: return visitUREM(N);
479 case ISD::MULHU: return visitMULHU(N);
480 case ISD::MULHS: return visitMULHS(N);
481 case ISD::AND: return visitAND(N);
482 case ISD::OR: return visitOR(N);
483 case ISD::XOR: return visitXOR(N);
484 case ISD::SHL: return visitSHL(N);
485 case ISD::SRA: return visitSRA(N);
486 case ISD::SRL: return visitSRL(N);
487 case ISD::CTLZ: return visitCTLZ(N);
488 case ISD::CTTZ: return visitCTTZ(N);
489 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000490 case ISD::SELECT: return visitSELECT(N);
491 case ISD::SELECT_CC: return visitSELECT_CC(N);
492 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000493 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
494 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000495 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000496 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
497 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000498 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000499 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000500 case ISD::FADD: return visitFADD(N);
501 case ISD::FSUB: return visitFSUB(N);
502 case ISD::FMUL: return visitFMUL(N);
503 case ISD::FDIV: return visitFDIV(N);
504 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000505 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000506 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
507 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
508 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
509 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
510 case ISD::FP_ROUND: return visitFP_ROUND(N);
511 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
512 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
513 case ISD::FNEG: return visitFNEG(N);
514 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000515 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000516 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000517 case ISD::LOAD: return visitLOAD(N);
Evan Chengc5484282006-10-04 00:56:09 +0000518 case ISD::LOADX: return visitLOADX(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000519 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000520 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
521 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000522 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000523 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000524 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000525 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
526 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
527 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
528 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
529 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
530 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
531 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
532 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000533 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000534 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000535}
536
Nate Begeman83e75ec2005-09-06 04:43:02 +0000537SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000538 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Jim Laskey279f0532006-09-25 16:29:54 +0000539 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000540 bool Changed = false; // If we should replace this token factor.
541 std::set<SDNode *> Visited; // Visited node set.
542
543 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000544 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000545
Jim Laskey6ff23e52006-10-04 16:53:27 +0000546 while (!TFs.empty()) {
547 SDNode *TF = TFs.back();
548 TFs.pop_back();
549
550 // Check each of the operands.
551 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
552 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000553
Jim Laskey6ff23e52006-10-04 16:53:27 +0000554 // Don't bother if we've seen this node before.
555 if (Visited.find(Op.Val) != Visited.end()) continue;
556 Visited.insert(Op.Val);
Jim Laskey279f0532006-09-25 16:29:54 +0000557
Jim Laskey6ff23e52006-10-04 16:53:27 +0000558 switch (Op.getOpcode()) {
559 case ISD::EntryToken:
560 // Entry tokens don't need to be added to the list (picked up later.)
561 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000562
Jim Laskey6ff23e52006-10-04 16:53:27 +0000563 case ISD::TokenFactor:
564 // FIXME - Old code only merged when use of one.
565 if (CombinerAA || Op.hasOneUse()) {
566 // Queue up for processing.
567 TFs.push_back(Op.Val);
568 // Clean up in case the token factor is removed.
569 AddToWorkList(Op.Val);
570 Changed = true;
571 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000572 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000573 // Fall thru
574
575 default:
576 Ops.push_back(Op);
Jim Laskey279f0532006-09-25 16:29:54 +0000577 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000578 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000579 }
580 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000581 }
582
583 SDOperand Result;
584
585 // If we've change things around then replace token factor.
586 if (Changed) {
587 if (Ops.size() == 0) {
588 // The entry token is the only possible outcome.
589 Result = DAG.getEntryNode();
590 } else {
591 // New and improved token factor.
592 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000593 }
594 }
Jim Laskey279f0532006-09-25 16:29:54 +0000595
Jim Laskey6ff23e52006-10-04 16:53:27 +0000596 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000597}
598
Nate Begeman83e75ec2005-09-06 04:43:02 +0000599SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000600 SDOperand N0 = N->getOperand(0);
601 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000602 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
603 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000604 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000605
606 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000607 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000608 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000609 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000610 if (N0C && !N1C)
611 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000612 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000613 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000614 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000615 // fold ((c1-A)+c2) -> (c1+c2)-A
616 if (N1C && N0.getOpcode() == ISD::SUB)
617 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
618 return DAG.getNode(ISD::SUB, VT,
619 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
620 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000621 // reassociate add
622 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
623 if (RADD.Val != 0)
624 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000625 // fold ((0-A) + B) -> B-A
626 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
627 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000628 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000629 // fold (A + (0-B)) -> A-B
630 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
631 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000632 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000633 // fold (A+(B-A)) -> B
634 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000635 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000636
Evan Cheng860771d2006-03-01 01:09:54 +0000637 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000638 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000639
640 // fold (a+b) -> (a|b) iff a and b share no bits.
641 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
642 uint64_t LHSZero, LHSOne;
643 uint64_t RHSZero, RHSOne;
644 uint64_t Mask = MVT::getIntVTBitMask(VT);
645 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
646 if (LHSZero) {
647 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
648
649 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
650 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
651 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
652 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
653 return DAG.getNode(ISD::OR, VT, N0, N1);
654 }
655 }
656
Nate Begeman83e75ec2005-09-06 04:43:02 +0000657 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000658}
659
Nate Begeman83e75ec2005-09-06 04:43:02 +0000660SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000661 SDOperand N0 = N->getOperand(0);
662 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000663 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
664 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000665 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000666
Chris Lattner854077d2005-10-17 01:07:11 +0000667 // fold (sub x, x) -> 0
668 if (N0 == N1)
669 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000670 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000671 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000672 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000673 // fold (sub x, c) -> (add x, -c)
674 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000675 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000676 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000677 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000678 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000679 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000680 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000681 return N0.getOperand(0);
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::visitMUL(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);
689 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000690 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000691
692 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000693 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000694 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000695 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000696 if (N0C && !N1C)
697 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000698 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000699 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000700 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000701 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000702 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000703 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000704 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000705 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000706 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000707 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000708 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000709 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
710 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
711 // FIXME: If the input is something that is easily negated (e.g. a
712 // single-use add), we should put the negate there.
713 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
714 DAG.getNode(ISD::SHL, VT, N0,
715 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
716 TLI.getShiftAmountTy())));
717 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +0000718
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000719 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
720 if (N1C && N0.getOpcode() == ISD::SHL &&
721 isa<ConstantSDNode>(N0.getOperand(1))) {
722 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000723 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000724 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
725 }
726
727 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
728 // use.
729 {
730 SDOperand Sh(0,0), Y(0,0);
731 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
732 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
733 N0.Val->hasOneUse()) {
734 Sh = N0; Y = N1;
735 } else if (N1.getOpcode() == ISD::SHL &&
736 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
737 Sh = N1; Y = N0;
738 }
739 if (Sh.Val) {
740 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
741 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
742 }
743 }
Chris Lattnera1deca32006-03-04 23:33:26 +0000744 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
745 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
746 isa<ConstantSDNode>(N0.getOperand(1))) {
747 return DAG.getNode(ISD::ADD, VT,
748 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
749 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
750 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000751
Nate Begemancd4d58c2006-02-03 06:46:56 +0000752 // reassociate mul
753 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
754 if (RMUL.Val != 0)
755 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000756 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000757}
758
Nate Begeman83e75ec2005-09-06 04:43:02 +0000759SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000760 SDOperand N0 = N->getOperand(0);
761 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000762 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
763 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000764 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000765
766 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000767 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000768 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000769 // fold (sdiv X, 1) -> X
770 if (N1C && N1C->getSignExtended() == 1LL)
771 return N0;
772 // fold (sdiv X, -1) -> 0-X
773 if (N1C && N1C->isAllOnesValue())
774 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000775 // If we know the sign bits of both operands are zero, strength reduce to a
776 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
777 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000778 if (TLI.MaskedValueIsZero(N1, SignBit) &&
779 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000780 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000781 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000782 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000783 (isPowerOf2_64(N1C->getSignExtended()) ||
784 isPowerOf2_64(-N1C->getSignExtended()))) {
785 // If dividing by powers of two is cheap, then don't perform the following
786 // fold.
787 if (TLI.isPow2DivCheap())
788 return SDOperand();
789 int64_t pow2 = N1C->getSignExtended();
790 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000791 unsigned lg2 = Log2_64(abs2);
792 // Splat the sign bit into the register
793 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000794 DAG.getConstant(MVT::getSizeInBits(VT)-1,
795 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +0000796 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000797 // Add (N0 < 0) ? abs2 - 1 : 0;
798 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
799 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000800 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000801 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +0000802 AddToWorkList(SRL.Val);
803 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +0000804 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
805 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000806 // If we're dividing by a positive value, we're done. Otherwise, we must
807 // negate the result.
808 if (pow2 > 0)
809 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +0000810 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000811 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
812 }
Nate Begeman69575232005-10-20 02:15:44 +0000813 // if integer divide is expensive and we satisfy the requirements, emit an
814 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000815 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000816 !TLI.isIntDivCheap()) {
817 SDOperand Op = BuildSDIV(N);
818 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000819 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000820 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000821}
822
Nate Begeman83e75ec2005-09-06 04:43:02 +0000823SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000824 SDOperand N0 = N->getOperand(0);
825 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000826 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
827 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000828 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000829
830 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000831 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000832 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000833 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000834 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000835 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000836 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000837 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000838 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
839 if (N1.getOpcode() == ISD::SHL) {
840 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
841 if (isPowerOf2_64(SHC->getValue())) {
842 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000843 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
844 DAG.getConstant(Log2_64(SHC->getValue()),
845 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +0000846 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000847 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000848 }
849 }
850 }
Nate Begeman69575232005-10-20 02:15:44 +0000851 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000852 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
853 SDOperand Op = BuildUDIV(N);
854 if (Op.Val) return Op;
855 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000856 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000857}
858
Nate Begeman83e75ec2005-09-06 04:43:02 +0000859SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000860 SDOperand N0 = N->getOperand(0);
861 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000862 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
863 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000864 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000865
866 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000867 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000868 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000869 // If we know the sign bits of both operands are zero, strength reduce to a
870 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
871 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000872 if (TLI.MaskedValueIsZero(N1, SignBit) &&
873 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000874 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000875 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000876}
877
Nate Begeman83e75ec2005-09-06 04:43:02 +0000878SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000879 SDOperand N0 = N->getOperand(0);
880 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000881 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
882 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000883 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000884
885 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000886 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000887 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000888 // fold (urem x, pow2) -> (and x, pow2-1)
889 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000890 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000891 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
892 if (N1.getOpcode() == ISD::SHL) {
893 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
894 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000895 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +0000896 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000897 return DAG.getNode(ISD::AND, VT, N0, Add);
898 }
899 }
900 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000901 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000902}
903
Nate Begeman83e75ec2005-09-06 04:43:02 +0000904SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000905 SDOperand N0 = N->getOperand(0);
906 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000907 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000908
909 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000910 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000911 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000912 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000913 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000914 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
915 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000916 TLI.getShiftAmountTy()));
917 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000918}
919
Nate Begeman83e75ec2005-09-06 04:43:02 +0000920SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000921 SDOperand N0 = N->getOperand(0);
922 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000923 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000924
925 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000926 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000927 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000928 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000929 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000930 return DAG.getConstant(0, N0.getValueType());
931 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000932}
933
Chris Lattner35e5c142006-05-05 05:51:50 +0000934/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
935/// two operands of the same opcode, try to simplify it.
936SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
937 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
938 MVT::ValueType VT = N0.getValueType();
939 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
940
Chris Lattner540121f2006-05-05 06:31:05 +0000941 // For each of OP in AND/OR/XOR:
942 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
943 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
944 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +0000945 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +0000946 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +0000947 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000948 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
949 SDOperand ORNode = DAG.getNode(N->getOpcode(),
950 N0.getOperand(0).getValueType(),
951 N0.getOperand(0), N1.getOperand(0));
952 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +0000953 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +0000954 }
955
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000956 // For each of OP in SHL/SRL/SRA/AND...
957 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
958 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
959 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +0000960 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000961 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000962 N0.getOperand(1) == N1.getOperand(1)) {
963 SDOperand ORNode = DAG.getNode(N->getOpcode(),
964 N0.getOperand(0).getValueType(),
965 N0.getOperand(0), N1.getOperand(0));
966 AddToWorkList(ORNode.Val);
967 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
968 }
969
970 return SDOperand();
971}
972
Nate Begeman83e75ec2005-09-06 04:43:02 +0000973SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000974 SDOperand N0 = N->getOperand(0);
975 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +0000976 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000977 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
978 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000979 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000980 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000981
982 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000983 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000984 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000985 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000986 if (N0C && !N1C)
987 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000988 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000989 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000990 return N0;
991 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +0000992 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000993 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000994 // reassociate and
995 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
996 if (RAND.Val != 0)
997 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000998 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +0000999 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001000 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001001 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001002 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001003 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1004 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001005 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +00001006 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001007 ~N1C->getValue() & InMask)) {
1008 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1009 N0.getOperand(0));
1010
1011 // Replace uses of the AND with uses of the Zero extend node.
1012 CombineTo(N, Zext);
1013
Chris Lattner3603cd62006-02-02 07:17:31 +00001014 // We actually want to replace all uses of the any_extend with the
1015 // zero_extend, to avoid duplicating things. This will later cause this
1016 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001017 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001018 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001019 }
1020 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001021 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1022 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1023 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1024 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1025
1026 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1027 MVT::isInteger(LL.getValueType())) {
1028 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1029 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1030 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001031 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001032 return DAG.getSetCC(VT, ORNode, LR, Op1);
1033 }
1034 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1035 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1036 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001037 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001038 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1039 }
1040 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1041 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1042 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001043 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001044 return DAG.getSetCC(VT, ORNode, LR, Op1);
1045 }
1046 }
1047 // canonicalize equivalent to ll == rl
1048 if (LL == RR && LR == RL) {
1049 Op1 = ISD::getSetCCSwappedOperands(Op1);
1050 std::swap(RL, RR);
1051 }
1052 if (LL == RL && LR == RR) {
1053 bool isInteger = MVT::isInteger(LL.getValueType());
1054 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1055 if (Result != ISD::SETCC_INVALID)
1056 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1057 }
1058 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001059
1060 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1061 if (N0.getOpcode() == N1.getOpcode()) {
1062 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1063 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001064 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001065
Nate Begemande996292006-02-03 22:24:05 +00001066 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1067 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001068 if (!MVT::isVector(VT) &&
1069 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001070 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001071 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Chengc5484282006-10-04 00:56:09 +00001072 if (ISD::isEXTLoad(N0.Val)) {
Nate Begemanded49632005-10-13 03:11:28 +00001073 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001074 // If we zero all the possible extended bits, then we can turn this into
1075 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001076 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001077 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001078 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1079 N0.getOperand(1), N0.getOperand(2),
1080 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001081 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001082 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001083 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001084 }
1085 }
1086 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Chengc5484282006-10-04 00:56:09 +00001087 if (ISD::isSEXTLoad(N0.Val) && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001088 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001089 // If we zero all the possible extended bits, then we can turn this into
1090 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001091 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001092 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001093 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1094 N0.getOperand(1), N0.getOperand(2),
1095 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001096 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001097 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001098 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001099 }
1100 }
Chris Lattner15045b62006-02-28 06:35:35 +00001101
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001102 // fold (and (load x), 255) -> (zextload x, i8)
1103 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1104 if (N1C &&
Evan Chengc5484282006-10-04 00:56:09 +00001105 (N0.getOpcode() == ISD::LOAD || ISD::isEXTLoad(N0.Val) ||
1106 ISD::isZEXTLoad(N0.Val)) &&
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001107 N0.hasOneUse()) {
1108 MVT::ValueType EVT, LoadedVT;
Chris Lattner15045b62006-02-28 06:35:35 +00001109 if (N1C->getValue() == 255)
1110 EVT = MVT::i8;
1111 else if (N1C->getValue() == 65535)
1112 EVT = MVT::i16;
1113 else if (N1C->getValue() == ~0U)
1114 EVT = MVT::i32;
1115 else
1116 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001117
1118 LoadedVT = N0.getOpcode() == ISD::LOAD ? VT :
1119 cast<VTSDNode>(N0.getOperand(3))->getVT();
Chris Lattnere44be602006-04-04 17:39:18 +00001120 if (EVT != MVT::Other && LoadedVT > EVT &&
Evan Chengc5484282006-10-04 00:56:09 +00001121 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Chris Lattner15045b62006-02-28 06:35:35 +00001122 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1123 // For big endian targets, we need to add an offset to the pointer to load
1124 // the correct bytes. For little endian systems, we merely need to read
1125 // fewer bytes from the same pointer.
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001126 unsigned PtrOff =
1127 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1128 SDOperand NewPtr = N0.getOperand(1);
1129 if (!TLI.isLittleEndian())
1130 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1131 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001132 AddToWorkList(NewPtr.Val);
Chris Lattner15045b62006-02-28 06:35:35 +00001133 SDOperand Load =
1134 DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
1135 N0.getOperand(2), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001136 AddToWorkList(N);
Chris Lattner15045b62006-02-28 06:35:35 +00001137 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001138 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner15045b62006-02-28 06:35:35 +00001139 }
1140 }
1141
Nate Begeman83e75ec2005-09-06 04:43:02 +00001142 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001143}
1144
Nate Begeman83e75ec2005-09-06 04:43:02 +00001145SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001146 SDOperand N0 = N->getOperand(0);
1147 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001148 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001149 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1150 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001151 MVT::ValueType VT = N1.getValueType();
1152 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001153
1154 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001155 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001156 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001157 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001158 if (N0C && !N1C)
1159 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001160 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001161 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001162 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001163 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001164 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001165 return N1;
1166 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001167 if (N1C &&
1168 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001169 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001170 // reassociate or
1171 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1172 if (ROR.Val != 0)
1173 return ROR;
1174 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1175 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001176 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001177 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1178 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1179 N1),
1180 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001181 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001182 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1183 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1184 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1185 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1186
1187 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1188 MVT::isInteger(LL.getValueType())) {
1189 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1190 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1191 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1192 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1193 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001194 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001195 return DAG.getSetCC(VT, ORNode, LR, Op1);
1196 }
1197 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1198 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1199 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1200 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1201 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001202 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001203 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1204 }
1205 }
1206 // canonicalize equivalent to ll == rl
1207 if (LL == RR && LR == RL) {
1208 Op1 = ISD::getSetCCSwappedOperands(Op1);
1209 std::swap(RL, RR);
1210 }
1211 if (LL == RL && LR == RR) {
1212 bool isInteger = MVT::isInteger(LL.getValueType());
1213 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1214 if (Result != ISD::SETCC_INVALID)
1215 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1216 }
1217 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001218
1219 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1220 if (N0.getOpcode() == N1.getOpcode()) {
1221 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1222 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001223 }
Chris Lattner516b9622006-09-14 20:50:57 +00001224
Chris Lattner1ec72732006-09-14 21:11:37 +00001225 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1226 if (N0.getOpcode() == ISD::AND &&
1227 N1.getOpcode() == ISD::AND &&
1228 N0.getOperand(1).getOpcode() == ISD::Constant &&
1229 N1.getOperand(1).getOpcode() == ISD::Constant &&
1230 // Don't increase # computations.
1231 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1232 // We can only do this xform if we know that bits from X that are set in C2
1233 // but not in C1 are already zero. Likewise for Y.
1234 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1235 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1236
1237 if (TLI.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1238 TLI.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
1239 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1240 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1241 }
1242 }
1243
1244
Chris Lattner516b9622006-09-14 20:50:57 +00001245 // See if this is some rotate idiom.
1246 if (SDNode *Rot = MatchRotate(N0, N1))
1247 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001248
Nate Begeman83e75ec2005-09-06 04:43:02 +00001249 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001250}
1251
Chris Lattner516b9622006-09-14 20:50:57 +00001252
1253/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1254static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1255 if (Op.getOpcode() == ISD::AND) {
1256 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1257 Mask = Op.getOperand(1);
1258 Op = Op.getOperand(0);
1259 } else {
1260 return false;
1261 }
1262 }
1263
1264 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1265 Shift = Op;
1266 return true;
1267 }
1268 return false;
1269}
1270
1271
1272// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1273// idioms for rotate, and if the target supports rotation instructions, generate
1274// a rot[lr].
1275SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1276 // Must be a legal type. Expanded an promoted things won't work with rotates.
1277 MVT::ValueType VT = LHS.getValueType();
1278 if (!TLI.isTypeLegal(VT)) return 0;
1279
1280 // The target must have at least one rotate flavor.
1281 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1282 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1283 if (!HasROTL && !HasROTR) return 0;
1284
1285 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1286 SDOperand LHSShift; // The shift.
1287 SDOperand LHSMask; // AND value if any.
1288 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1289 return 0; // Not part of a rotate.
1290
1291 SDOperand RHSShift; // The shift.
1292 SDOperand RHSMask; // AND value if any.
1293 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1294 return 0; // Not part of a rotate.
1295
1296 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1297 return 0; // Not shifting the same value.
1298
1299 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1300 return 0; // Shifts must disagree.
1301
1302 // Canonicalize shl to left side in a shl/srl pair.
1303 if (RHSShift.getOpcode() == ISD::SHL) {
1304 std::swap(LHS, RHS);
1305 std::swap(LHSShift, RHSShift);
1306 std::swap(LHSMask , RHSMask );
1307 }
1308
1309 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1310
1311 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1312 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
1313 if (LHSShift.getOperand(1).getOpcode() == ISD::Constant &&
1314 RHSShift.getOperand(1).getOpcode() == ISD::Constant) {
1315 uint64_t LShVal = cast<ConstantSDNode>(LHSShift.getOperand(1))->getValue();
1316 uint64_t RShVal = cast<ConstantSDNode>(RHSShift.getOperand(1))->getValue();
1317 if ((LShVal + RShVal) != OpSizeInBits)
1318 return 0;
1319
1320 SDOperand Rot;
1321 if (HasROTL)
1322 Rot = DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1323 LHSShift.getOperand(1));
1324 else
1325 Rot = DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1326 RHSShift.getOperand(1));
1327
1328 // If there is an AND of either shifted operand, apply it to the result.
1329 if (LHSMask.Val || RHSMask.Val) {
1330 uint64_t Mask = MVT::getIntVTBitMask(VT);
1331
1332 if (LHSMask.Val) {
1333 uint64_t RHSBits = (1ULL << LShVal)-1;
1334 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1335 }
1336 if (RHSMask.Val) {
1337 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1338 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1339 }
1340
1341 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1342 }
1343
1344 return Rot.Val;
1345 }
1346
1347 // If there is a mask here, and we have a variable shift, we can't be sure
1348 // that we're masking out the right stuff.
1349 if (LHSMask.Val || RHSMask.Val)
1350 return 0;
1351
1352 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1353 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
1354 if (RHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1355 LHSShift.getOperand(1) == RHSShift.getOperand(1).getOperand(1)) {
1356 if (ConstantSDNode *SUBC =
1357 dyn_cast<ConstantSDNode>(RHSShift.getOperand(1).getOperand(0))) {
1358 if (SUBC->getValue() == OpSizeInBits)
1359 if (HasROTL)
1360 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1361 LHSShift.getOperand(1)).Val;
1362 else
1363 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1364 LHSShift.getOperand(1)).Val;
1365 }
1366 }
1367
1368 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1369 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
1370 if (LHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1371 RHSShift.getOperand(1) == LHSShift.getOperand(1).getOperand(1)) {
1372 if (ConstantSDNode *SUBC =
1373 dyn_cast<ConstantSDNode>(LHSShift.getOperand(1).getOperand(0))) {
1374 if (SUBC->getValue() == OpSizeInBits)
1375 if (HasROTL)
1376 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1377 LHSShift.getOperand(1)).Val;
1378 else
1379 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1380 RHSShift.getOperand(1)).Val;
1381 }
1382 }
1383
1384 return 0;
1385}
1386
1387
Nate Begeman83e75ec2005-09-06 04:43:02 +00001388SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001389 SDOperand N0 = N->getOperand(0);
1390 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001391 SDOperand LHS, RHS, CC;
1392 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1393 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001394 MVT::ValueType VT = N0.getValueType();
1395
1396 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001397 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001398 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001399 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001400 if (N0C && !N1C)
1401 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001402 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001403 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001404 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001405 // reassociate xor
1406 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1407 if (RXOR.Val != 0)
1408 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001409 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001410 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1411 bool isInt = MVT::isInteger(LHS.getValueType());
1412 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1413 isInt);
1414 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001415 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001416 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001417 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001418 assert(0 && "Unhandled SetCC Equivalent!");
1419 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001420 }
Nate Begeman99801192005-09-07 23:25:52 +00001421 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1422 if (N1C && N1C->getValue() == 1 &&
1423 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001424 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001425 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1426 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001427 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1428 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001429 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001430 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001431 }
1432 }
Nate Begeman99801192005-09-07 23:25:52 +00001433 // fold !(x or y) -> (!x and !y) iff x or y are constants
1434 if (N1C && N1C->isAllOnesValue() &&
1435 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001436 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001437 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1438 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001439 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1440 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001441 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001442 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001443 }
1444 }
Nate Begeman223df222005-09-08 20:18:10 +00001445 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1446 if (N1C && N0.getOpcode() == ISD::XOR) {
1447 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1448 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1449 if (N00C)
1450 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1451 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1452 if (N01C)
1453 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1454 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1455 }
1456 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001457 if (N0 == N1) {
1458 if (!MVT::isVector(VT)) {
1459 return DAG.getConstant(0, VT);
1460 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1461 // Produce a vector of zeros.
1462 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1463 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001464 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001465 }
1466 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001467
1468 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1469 if (N0.getOpcode() == N1.getOpcode()) {
1470 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1471 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001472 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001473
Chris Lattner3e104b12006-04-08 04:15:24 +00001474 // Simplify the expression using non-local knowledge.
1475 if (!MVT::isVector(VT) &&
1476 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001477 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001478
Nate Begeman83e75ec2005-09-06 04:43:02 +00001479 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001480}
1481
Nate Begeman83e75ec2005-09-06 04:43:02 +00001482SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001483 SDOperand N0 = N->getOperand(0);
1484 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001485 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1486 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001487 MVT::ValueType VT = N0.getValueType();
1488 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1489
1490 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001491 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001492 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001493 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001494 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001495 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001496 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001497 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001498 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001499 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001500 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001501 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001502 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001503 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001504 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001505 if (SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001506 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001507 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001508 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001509 N0.getOperand(1).getOpcode() == ISD::Constant) {
1510 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001511 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001512 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001513 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001514 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001515 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001516 }
1517 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1518 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001519 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001520 N0.getOperand(1).getOpcode() == ISD::Constant) {
1521 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001522 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001523 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1524 DAG.getConstant(~0ULL << c1, VT));
1525 if (c2 > c1)
1526 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001527 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001528 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001529 return DAG.getNode(ISD::SRL, VT, Mask,
1530 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001531 }
1532 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001533 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001534 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001535 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnercac70592006-03-05 19:53:55 +00001536 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1537 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1538 isa<ConstantSDNode>(N0.getOperand(1))) {
1539 return DAG.getNode(ISD::ADD, VT,
1540 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1541 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1542 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001543 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001544}
1545
Nate Begeman83e75ec2005-09-06 04:43:02 +00001546SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001547 SDOperand N0 = N->getOperand(0);
1548 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001549 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1550 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001551 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001552
1553 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001554 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001555 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001556 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001557 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001558 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001559 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001560 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001561 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001562 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001563 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001564 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001565 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001566 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001567 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001568 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1569 // sext_inreg.
1570 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1571 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1572 MVT::ValueType EVT;
1573 switch (LowBits) {
1574 default: EVT = MVT::Other; break;
1575 case 1: EVT = MVT::i1; break;
1576 case 8: EVT = MVT::i8; break;
1577 case 16: EVT = MVT::i16; break;
1578 case 32: EVT = MVT::i32; break;
1579 }
1580 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1581 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1582 DAG.getValueType(EVT));
1583 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001584
1585 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1586 if (N1C && N0.getOpcode() == ISD::SRA) {
1587 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1588 unsigned Sum = N1C->getValue() + C1->getValue();
1589 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1590 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1591 DAG.getConstant(Sum, N1C->getValueType(0)));
1592 }
1593 }
1594
Chris Lattnera8504462006-05-08 20:51:54 +00001595 // Simplify, based on bits shifted out of the LHS.
1596 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
1597 return SDOperand(N, 0);
1598
1599
Nate Begeman1d4d4142005-09-01 00:19:25 +00001600 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001601 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001602 return DAG.getNode(ISD::SRL, VT, N0, N1);
1603 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001604}
1605
Nate Begeman83e75ec2005-09-06 04:43:02 +00001606SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001607 SDOperand N0 = N->getOperand(0);
1608 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001609 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1610 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001611 MVT::ValueType VT = N0.getValueType();
1612 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1613
1614 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001615 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001616 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001617 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001618 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001619 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001620 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001621 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001622 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001623 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001624 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001625 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001626 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001627 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001628 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001629 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001630 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001631 N0.getOperand(1).getOpcode() == ISD::Constant) {
1632 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001633 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001634 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001635 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001636 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001637 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001638 }
Chris Lattner350bec02006-04-02 06:11:11 +00001639
Chris Lattner06afe072006-05-05 22:53:17 +00001640 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
1641 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1642 // Shifting in all undef bits?
1643 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
1644 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
1645 return DAG.getNode(ISD::UNDEF, VT);
1646
1647 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
1648 AddToWorkList(SmallShift.Val);
1649 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
1650 }
1651
Chris Lattner350bec02006-04-02 06:11:11 +00001652 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1653 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1654 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1655 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1656 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
1657
1658 // If any of the input bits are KnownOne, then the input couldn't be all
1659 // zeros, thus the result of the srl will always be zero.
1660 if (KnownOne) return DAG.getConstant(0, VT);
1661
1662 // If all of the bits input the to ctlz node are known to be zero, then
1663 // the result of the ctlz is "32" and the result of the shift is one.
1664 uint64_t UnknownBits = ~KnownZero & Mask;
1665 if (UnknownBits == 0) return DAG.getConstant(1, VT);
1666
1667 // Otherwise, check to see if there is exactly one bit input to the ctlz.
1668 if ((UnknownBits & (UnknownBits-1)) == 0) {
1669 // Okay, we know that only that the single bit specified by UnknownBits
1670 // could be set on input to the CTLZ node. If this bit is set, the SRL
1671 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
1672 // to an SRL,XOR pair, which is likely to simplify more.
1673 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
1674 SDOperand Op = N0.getOperand(0);
1675 if (ShAmt) {
1676 Op = DAG.getNode(ISD::SRL, VT, Op,
1677 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
1678 AddToWorkList(Op.Val);
1679 }
1680 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
1681 }
1682 }
1683
Nate Begeman83e75ec2005-09-06 04:43:02 +00001684 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001685}
1686
Nate Begeman83e75ec2005-09-06 04:43:02 +00001687SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001688 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001689 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001690
1691 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001692 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001693 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001694 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001695}
1696
Nate Begeman83e75ec2005-09-06 04:43:02 +00001697SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001698 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001699 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001700
1701 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001702 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001703 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001704 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001705}
1706
Nate Begeman83e75ec2005-09-06 04:43:02 +00001707SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001708 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001709 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001710
1711 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001712 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001713 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001714 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001715}
1716
Nate Begeman452d7be2005-09-16 00:54:12 +00001717SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1718 SDOperand N0 = N->getOperand(0);
1719 SDOperand N1 = N->getOperand(1);
1720 SDOperand N2 = N->getOperand(2);
1721 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1722 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1723 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1724 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001725
Nate Begeman452d7be2005-09-16 00:54:12 +00001726 // fold select C, X, X -> X
1727 if (N1 == N2)
1728 return N1;
1729 // fold select true, X, Y -> X
1730 if (N0C && !N0C->isNullValue())
1731 return N1;
1732 // fold select false, X, Y -> Y
1733 if (N0C && N0C->isNullValue())
1734 return N2;
1735 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001736 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001737 return DAG.getNode(ISD::OR, VT, N0, N2);
1738 // fold select C, 0, X -> ~C & X
1739 // FIXME: this should check for C type == X type, not i1?
1740 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1741 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001742 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001743 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1744 }
1745 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001746 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001747 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001748 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001749 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1750 }
1751 // fold select C, X, 0 -> C & X
1752 // FIXME: this should check for C type == X type, not i1?
1753 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1754 return DAG.getNode(ISD::AND, VT, N0, N1);
1755 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1756 if (MVT::i1 == VT && N0 == N1)
1757 return DAG.getNode(ISD::OR, VT, N0, N2);
1758 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1759 if (MVT::i1 == VT && N0 == N2)
1760 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00001761
Chris Lattner40c62d52005-10-18 06:04:22 +00001762 // If we can fold this based on the true/false value, do so.
1763 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00001764 return SDOperand(N, 0); // Don't revisit N.
1765
Nate Begeman44728a72005-09-19 22:34:01 +00001766 // fold selects based on a setcc into other things, such as min/max/abs
1767 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001768 // FIXME:
1769 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1770 // having to say they don't support SELECT_CC on every type the DAG knows
1771 // about, since there is no way to mark an opcode illegal at all value types
1772 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1773 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1774 N1, N2, N0.getOperand(2));
1775 else
1776 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001777 return SDOperand();
1778}
1779
1780SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001781 SDOperand N0 = N->getOperand(0);
1782 SDOperand N1 = N->getOperand(1);
1783 SDOperand N2 = N->getOperand(2);
1784 SDOperand N3 = N->getOperand(3);
1785 SDOperand N4 = N->getOperand(4);
1786 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1787 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1788 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1789 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1790
Nate Begeman44728a72005-09-19 22:34:01 +00001791 // fold select_cc lhs, rhs, x, x, cc -> x
1792 if (N2 == N3)
1793 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001794
Chris Lattner5f42a242006-09-20 06:19:26 +00001795 // Determine if the condition we're dealing with is constant
1796 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
1797
1798 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
1799 if (SCCC->getValue())
1800 return N2; // cond always true -> true val
1801 else
1802 return N3; // cond always false -> false val
1803 }
1804
1805 // Fold to a simpler select_cc
1806 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
1807 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
1808 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
1809 SCC.getOperand(2));
1810
Chris Lattner40c62d52005-10-18 06:04:22 +00001811 // If we can fold this based on the true/false value, do so.
1812 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00001813 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00001814
Nate Begeman44728a72005-09-19 22:34:01 +00001815 // fold select_cc into other things, such as min/max/abs
1816 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001817}
1818
1819SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1820 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1821 cast<CondCodeSDNode>(N->getOperand(2))->get());
1822}
1823
Nate Begeman83e75ec2005-09-06 04:43:02 +00001824SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001825 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001826 MVT::ValueType VT = N->getValueType(0);
1827
Nate Begeman1d4d4142005-09-01 00:19:25 +00001828 // fold (sext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001829 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001830 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00001831
Nate Begeman1d4d4142005-09-01 00:19:25 +00001832 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001833 // fold (sext (aext x)) -> (sext x)
1834 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001835 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00001836
Chris Lattner6007b842006-09-21 06:00:20 +00001837 // fold (sext (truncate x)) -> (sextinreg x).
1838 if (N0.getOpcode() == ISD::TRUNCATE &&
Chris Lattnerbf370872006-09-21 06:17:39 +00001839 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
1840 N0.getValueType()))) {
Chris Lattner6007b842006-09-21 06:00:20 +00001841 SDOperand Op = N0.getOperand(0);
1842 if (Op.getValueType() < VT) {
1843 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
1844 } else if (Op.getValueType() > VT) {
1845 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
1846 }
1847 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001848 DAG.getValueType(N0.getValueType()));
Chris Lattner6007b842006-09-21 06:00:20 +00001849 }
Chris Lattner310b5782006-05-06 23:06:26 +00001850
Evan Cheng110dec22005-12-14 02:19:23 +00001851 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001852 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00001853 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001854 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1855 N0.getOperand(1), N0.getOperand(2),
1856 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001857 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001858 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1859 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001860 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00001861 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001862
1863 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1864 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Chengc5484282006-10-04 00:56:09 +00001865 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001866 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1867 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1868 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001869 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001870 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1871 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001872 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001873 }
1874
Nate Begeman83e75ec2005-09-06 04:43:02 +00001875 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001876}
1877
Nate Begeman83e75ec2005-09-06 04:43:02 +00001878SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001879 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001880 MVT::ValueType VT = N->getValueType(0);
1881
Nate Begeman1d4d4142005-09-01 00:19:25 +00001882 // fold (zext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001883 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001884 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001885 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001886 // fold (zext (aext x)) -> (zext x)
1887 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001888 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00001889
1890 // fold (zext (truncate x)) -> (and x, mask)
1891 if (N0.getOpcode() == ISD::TRUNCATE &&
1892 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
1893 SDOperand Op = N0.getOperand(0);
1894 if (Op.getValueType() < VT) {
1895 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
1896 } else if (Op.getValueType() > VT) {
1897 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
1898 }
1899 return DAG.getZeroExtendInReg(Op, N0.getValueType());
1900 }
1901
Chris Lattner111c2282006-09-21 06:14:31 +00001902 // fold (zext (and (trunc x), cst)) -> (and x, cst).
1903 if (N0.getOpcode() == ISD::AND &&
1904 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1905 N0.getOperand(1).getOpcode() == ISD::Constant) {
1906 SDOperand X = N0.getOperand(0).getOperand(0);
1907 if (X.getValueType() < VT) {
1908 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
1909 } else if (X.getValueType() > VT) {
1910 X = DAG.getNode(ISD::TRUNCATE, VT, X);
1911 }
1912 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1913 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
1914 }
1915
Evan Cheng110dec22005-12-14 02:19:23 +00001916 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001917 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00001918 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng110dec22005-12-14 02:19:23 +00001919 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1920 N0.getOperand(1), N0.getOperand(2),
1921 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001922 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001923 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1924 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001925 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00001926 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001927
1928 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1929 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Chengc5484282006-10-04 00:56:09 +00001930 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001931 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1932 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1933 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001934 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001935 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1936 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001937 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001938 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001939 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001940}
1941
Chris Lattner5ffc0662006-05-05 05:58:59 +00001942SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
1943 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001944 MVT::ValueType VT = N->getValueType(0);
1945
1946 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001947 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00001948 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
1949 // fold (aext (aext x)) -> (aext x)
1950 // fold (aext (zext x)) -> (zext x)
1951 // fold (aext (sext x)) -> (sext x)
1952 if (N0.getOpcode() == ISD::ANY_EXTEND ||
1953 N0.getOpcode() == ISD::ZERO_EXTEND ||
1954 N0.getOpcode() == ISD::SIGN_EXTEND)
1955 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
1956
Chris Lattner84750582006-09-20 06:29:17 +00001957 // fold (aext (truncate x))
1958 if (N0.getOpcode() == ISD::TRUNCATE) {
1959 SDOperand TruncOp = N0.getOperand(0);
1960 if (TruncOp.getValueType() == VT)
1961 return TruncOp; // x iff x size == zext size.
1962 if (TruncOp.getValueType() > VT)
1963 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
1964 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
1965 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00001966
1967 // fold (aext (and (trunc x), cst)) -> (and x, cst).
1968 if (N0.getOpcode() == ISD::AND &&
1969 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1970 N0.getOperand(1).getOpcode() == ISD::Constant) {
1971 SDOperand X = N0.getOperand(0).getOperand(0);
1972 if (X.getValueType() < VT) {
1973 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
1974 } else if (X.getValueType() > VT) {
1975 X = DAG.getNode(ISD::TRUNCATE, VT, X);
1976 }
1977 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1978 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
1979 }
1980
Chris Lattner5ffc0662006-05-05 05:58:59 +00001981 // fold (aext (load x)) -> (aext (truncate (extload x)))
1982 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00001983 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Chris Lattner5ffc0662006-05-05 05:58:59 +00001984 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0),
1985 N0.getOperand(1), N0.getOperand(2),
1986 N0.getValueType());
1987 CombineTo(N, ExtLoad);
1988 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1989 ExtLoad.getValue(1));
1990 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1991 }
1992
1993 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
1994 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
1995 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Chengc5484282006-10-04 00:56:09 +00001996 if (N0.getOpcode() == ISD::LOADX && N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001997 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Evan Chengc5484282006-10-04 00:56:09 +00001998 unsigned LType = N0.getConstantOperandVal(4);
1999 SDOperand ExtLoad = DAG.getExtLoad((ISD::LoadExtType)LType, VT,
2000 N0.getOperand(0), N0.getOperand(1),
2001 N0.getOperand(2), EVT);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002002 CombineTo(N, ExtLoad);
2003 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2004 ExtLoad.getValue(1));
2005 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2006 }
2007 return SDOperand();
2008}
2009
2010
Nate Begeman83e75ec2005-09-06 04:43:02 +00002011SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002012 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002013 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002014 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002015 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00002016 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002017
Nate Begeman1d4d4142005-09-01 00:19:25 +00002018 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00002019 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00002020 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00002021
Chris Lattner541a24f2006-05-06 22:43:44 +00002022 // If the input is already sign extended, just drop the extension.
Chris Lattneree4ea922006-05-06 09:30:03 +00002023 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
2024 return N0;
2025
Nate Begeman646d7e22005-09-02 21:18:40 +00002026 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
2027 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2028 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00002029 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002030 }
Chris Lattner4b37e872006-05-08 21:18:59 +00002031
Nate Begeman07ed4172005-10-10 21:26:48 +00002032 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00002033 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00002034 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00002035
2036 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
2037 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
2038 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
2039 if (N0.getOpcode() == ISD::SRL) {
2040 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2041 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
2042 // We can turn this into an SRA iff the input to the SRL is already sign
2043 // extended enough.
2044 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
2045 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
2046 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
2047 }
2048 }
2049
Nate Begemanded49632005-10-13 03:11:28 +00002050 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00002051 if (ISD::isEXTLoad(N0.Val) &&
Nate Begemanded49632005-10-13 03:11:28 +00002052 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002053 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00002054 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
2055 N0.getOperand(1), N0.getOperand(2),
2056 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002057 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002058 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002059 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002060 }
2061 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Chengc5484282006-10-04 00:56:09 +00002062 if (ISD::isZEXTLoad(N0.Val) && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00002063 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002064 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00002065 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
2066 N0.getOperand(1), N0.getOperand(2),
2067 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002068 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002069 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002070 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002071 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002072 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002073}
2074
Nate Begeman83e75ec2005-09-06 04:43:02 +00002075SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002076 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002077 MVT::ValueType VT = N->getValueType(0);
2078
2079 // noop truncate
2080 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002081 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002082 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002083 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002084 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002085 // fold (truncate (truncate x)) -> (truncate x)
2086 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002087 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002088 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00002089 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2090 N0.getOpcode() == ISD::ANY_EXTEND) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002091 if (N0.getValueType() < VT)
2092 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00002093 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002094 else if (N0.getValueType() > VT)
2095 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002096 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002097 else
2098 // if the source and dest are the same type, we can drop both the extend
2099 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002100 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002101 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002102 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00002103 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00002104 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
2105 "Cannot truncate to larger type!");
2106 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00002107 // For big endian targets, we need to add an offset to the pointer to load
2108 // the correct bytes. For little endian systems, we merely need to read
2109 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00002110 uint64_t PtrOff =
2111 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00002112 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
2113 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
2114 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00002115 AddToWorkList(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00002116 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00002117 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00002118 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002119 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002120 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002121 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002122}
2123
Chris Lattner94683772005-12-23 05:30:37 +00002124SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2125 SDOperand N0 = N->getOperand(0);
2126 MVT::ValueType VT = N->getValueType(0);
2127
2128 // If the input is a constant, let getNode() fold it.
2129 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2130 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2131 if (Res.Val != N) return Res;
2132 }
2133
Chris Lattnerc8547d82005-12-23 05:37:50 +00002134 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2135 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00002136
Chris Lattner57104102005-12-23 05:44:41 +00002137 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002138 // FIXME: These xforms need to know that the resultant load doesn't need a
2139 // higher alignment than the original!
2140 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00002141 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
2142 N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00002143 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00002144 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2145 Load.getValue(1));
2146 return Load;
2147 }
2148
Chris Lattner94683772005-12-23 05:30:37 +00002149 return SDOperand();
2150}
2151
Chris Lattner6258fb22006-04-02 02:53:43 +00002152SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2153 SDOperand N0 = N->getOperand(0);
2154 MVT::ValueType VT = N->getValueType(0);
2155
2156 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2157 // First check to see if this is all constant.
2158 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2159 VT == MVT::Vector) {
2160 bool isSimple = true;
2161 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2162 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2163 N0.getOperand(i).getOpcode() != ISD::Constant &&
2164 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2165 isSimple = false;
2166 break;
2167 }
2168
Chris Lattner97c20732006-04-03 17:29:28 +00002169 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2170 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002171 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2172 }
2173 }
2174
2175 return SDOperand();
2176}
2177
2178/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2179/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2180/// destination element value type.
2181SDOperand DAGCombiner::
2182ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2183 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2184
2185 // If this is already the right type, we're done.
2186 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2187
2188 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2189 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2190
2191 // If this is a conversion of N elements of one type to N elements of another
2192 // type, convert each element. This handles FP<->INT cases.
2193 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002194 SmallVector<SDOperand, 8> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00002195 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002196 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002197 AddToWorkList(Ops.back().Val);
2198 }
Chris Lattner6258fb22006-04-02 02:53:43 +00002199 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2200 Ops.push_back(DAG.getValueType(DstEltVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002201 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002202 }
2203
2204 // Otherwise, we're growing or shrinking the elements. To avoid having to
2205 // handle annoying details of growing/shrinking FP values, we convert them to
2206 // int first.
2207 if (MVT::isFloatingPoint(SrcEltVT)) {
2208 // Convert the input float vector to a int vector where the elements are the
2209 // same sizes.
2210 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2211 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2212 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2213 SrcEltVT = IntVT;
2214 }
2215
2216 // Now we know the input is an integer vector. If the output is a FP type,
2217 // convert to integer first, then to FP of the right size.
2218 if (MVT::isFloatingPoint(DstEltVT)) {
2219 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2220 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2221 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2222
2223 // Next, convert to FP elements of the same size.
2224 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2225 }
2226
2227 // Okay, we know the src/dst types are both integers of differing types.
2228 // Handling growing first.
2229 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2230 if (SrcBitSize < DstBitSize) {
2231 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2232
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002233 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002234 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2235 i += NumInputsPerOutput) {
2236 bool isLE = TLI.isLittleEndian();
2237 uint64_t NewBits = 0;
2238 bool EltIsUndef = true;
2239 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2240 // Shift the previously computed bits over.
2241 NewBits <<= SrcBitSize;
2242 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2243 if (Op.getOpcode() == ISD::UNDEF) continue;
2244 EltIsUndef = false;
2245
2246 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2247 }
2248
2249 if (EltIsUndef)
2250 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2251 else
2252 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2253 }
2254
2255 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2256 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002257 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002258 }
2259
2260 // Finally, this must be the case where we are shrinking elements: each input
2261 // turns into multiple outputs.
2262 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002263 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002264 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2265 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2266 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2267 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2268 continue;
2269 }
2270 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2271
2272 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2273 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2274 OpVal >>= DstBitSize;
2275 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2276 }
2277
2278 // For big endian targets, swap the order of the pieces of each element.
2279 if (!TLI.isLittleEndian())
2280 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2281 }
2282 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2283 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002284 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002285}
2286
2287
2288
Chris Lattner01b3d732005-09-28 22:28:18 +00002289SDOperand DAGCombiner::visitFADD(SDNode *N) {
2290 SDOperand N0 = N->getOperand(0);
2291 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002292 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2293 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002294 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002295
2296 // fold (fadd c1, c2) -> c1+c2
2297 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002298 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002299 // canonicalize constant to RHS
2300 if (N0CFP && !N1CFP)
2301 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002302 // fold (A + (-B)) -> A-B
2303 if (N1.getOpcode() == ISD::FNEG)
2304 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002305 // fold ((-A) + B) -> B-A
2306 if (N0.getOpcode() == ISD::FNEG)
2307 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002308 return SDOperand();
2309}
2310
2311SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2312 SDOperand N0 = N->getOperand(0);
2313 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002314 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2315 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002316 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002317
2318 // fold (fsub c1, c2) -> c1-c2
2319 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002320 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002321 // fold (A-(-B)) -> A+B
2322 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00002323 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002324 return SDOperand();
2325}
2326
2327SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2328 SDOperand N0 = N->getOperand(0);
2329 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002330 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2331 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002332 MVT::ValueType VT = N->getValueType(0);
2333
Nate Begeman11af4ea2005-10-17 20:40:11 +00002334 // fold (fmul c1, c2) -> c1*c2
2335 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002336 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002337 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002338 if (N0CFP && !N1CFP)
2339 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002340 // fold (fmul X, 2.0) -> (fadd X, X)
2341 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2342 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002343 return SDOperand();
2344}
2345
2346SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2347 SDOperand N0 = N->getOperand(0);
2348 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002349 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2350 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002351 MVT::ValueType VT = N->getValueType(0);
2352
Nate Begemana148d982006-01-18 22:35:16 +00002353 // fold (fdiv c1, c2) -> c1/c2
2354 if (N0CFP && N1CFP)
2355 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002356 return SDOperand();
2357}
2358
2359SDOperand DAGCombiner::visitFREM(SDNode *N) {
2360 SDOperand N0 = N->getOperand(0);
2361 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002362 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2363 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002364 MVT::ValueType VT = N->getValueType(0);
2365
Nate Begemana148d982006-01-18 22:35:16 +00002366 // fold (frem c1, c2) -> fmod(c1,c2)
2367 if (N0CFP && N1CFP)
2368 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002369 return SDOperand();
2370}
2371
Chris Lattner12d83032006-03-05 05:30:57 +00002372SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2373 SDOperand N0 = N->getOperand(0);
2374 SDOperand N1 = N->getOperand(1);
2375 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2376 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2377 MVT::ValueType VT = N->getValueType(0);
2378
2379 if (N0CFP && N1CFP) // Constant fold
2380 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2381
2382 if (N1CFP) {
2383 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2384 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2385 union {
2386 double d;
2387 int64_t i;
2388 } u;
2389 u.d = N1CFP->getValue();
2390 if (u.i >= 0)
2391 return DAG.getNode(ISD::FABS, VT, N0);
2392 else
2393 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2394 }
2395
2396 // copysign(fabs(x), y) -> copysign(x, y)
2397 // copysign(fneg(x), y) -> copysign(x, y)
2398 // copysign(copysign(x,z), y) -> copysign(x, y)
2399 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2400 N0.getOpcode() == ISD::FCOPYSIGN)
2401 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2402
2403 // copysign(x, abs(y)) -> abs(x)
2404 if (N1.getOpcode() == ISD::FABS)
2405 return DAG.getNode(ISD::FABS, VT, N0);
2406
2407 // copysign(x, copysign(y,z)) -> copysign(x, z)
2408 if (N1.getOpcode() == ISD::FCOPYSIGN)
2409 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2410
2411 // copysign(x, fp_extend(y)) -> copysign(x, y)
2412 // copysign(x, fp_round(y)) -> copysign(x, y)
2413 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2414 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2415
2416 return SDOperand();
2417}
2418
2419
Chris Lattner01b3d732005-09-28 22:28:18 +00002420
Nate Begeman83e75ec2005-09-06 04:43:02 +00002421SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002422 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002423 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002424 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002425
2426 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002427 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002428 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002429 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002430}
2431
Nate Begeman83e75ec2005-09-06 04:43:02 +00002432SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002433 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002434 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002435 MVT::ValueType VT = N->getValueType(0);
2436
Nate Begeman1d4d4142005-09-01 00:19:25 +00002437 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002438 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002439 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002440 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002441}
2442
Nate Begeman83e75ec2005-09-06 04:43:02 +00002443SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002444 SDOperand N0 = N->getOperand(0);
2445 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2446 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002447
2448 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002449 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002450 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002451 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002452}
2453
Nate Begeman83e75ec2005-09-06 04:43:02 +00002454SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002455 SDOperand N0 = N->getOperand(0);
2456 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2457 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002458
2459 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002460 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002461 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002462 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002463}
2464
Nate Begeman83e75ec2005-09-06 04:43:02 +00002465SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002466 SDOperand N0 = N->getOperand(0);
2467 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2468 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002469
2470 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002471 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002472 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002473
2474 // fold (fp_round (fp_extend x)) -> x
2475 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2476 return N0.getOperand(0);
2477
2478 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2479 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2480 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2481 AddToWorkList(Tmp.Val);
2482 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2483 }
2484
Nate Begeman83e75ec2005-09-06 04:43:02 +00002485 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002486}
2487
Nate Begeman83e75ec2005-09-06 04:43:02 +00002488SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002489 SDOperand N0 = N->getOperand(0);
2490 MVT::ValueType VT = N->getValueType(0);
2491 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002492 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002493
Nate Begeman1d4d4142005-09-01 00:19:25 +00002494 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002495 if (N0CFP) {
2496 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002497 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002498 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002499 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002500}
2501
Nate Begeman83e75ec2005-09-06 04:43:02 +00002502SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002503 SDOperand N0 = N->getOperand(0);
2504 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2505 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002506
2507 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002508 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002509 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00002510
2511 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
2512 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002513 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Chris Lattnere564dbb2006-05-05 21:34:35 +00002514 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0),
2515 N0.getOperand(1), N0.getOperand(2),
2516 N0.getValueType());
2517 CombineTo(N, ExtLoad);
2518 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
2519 ExtLoad.getValue(1));
2520 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2521 }
2522
2523
Nate Begeman83e75ec2005-09-06 04:43:02 +00002524 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002525}
2526
Nate Begeman83e75ec2005-09-06 04:43:02 +00002527SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002528 SDOperand N0 = N->getOperand(0);
2529 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2530 MVT::ValueType VT = N->getValueType(0);
2531
2532 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002533 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002534 return DAG.getNode(ISD::FNEG, VT, N0);
2535 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002536 if (N0.getOpcode() == ISD::SUB)
2537 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002538 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002539 if (N0.getOpcode() == ISD::FNEG)
2540 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002541 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002542}
2543
Nate Begeman83e75ec2005-09-06 04:43:02 +00002544SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002545 SDOperand N0 = N->getOperand(0);
2546 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2547 MVT::ValueType VT = N->getValueType(0);
2548
Nate Begeman1d4d4142005-09-01 00:19:25 +00002549 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002550 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002551 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002552 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002553 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002554 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002555 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002556 // fold (fabs (fcopysign x, y)) -> (fabs x)
2557 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2558 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2559
Nate Begeman83e75ec2005-09-06 04:43:02 +00002560 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002561}
2562
Nate Begeman44728a72005-09-19 22:34:01 +00002563SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2564 SDOperand Chain = N->getOperand(0);
2565 SDOperand N1 = N->getOperand(1);
2566 SDOperand N2 = N->getOperand(2);
2567 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2568
2569 // never taken branch, fold to chain
2570 if (N1C && N1C->isNullValue())
2571 return Chain;
2572 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002573 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002574 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002575 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2576 // on the target.
2577 if (N1.getOpcode() == ISD::SETCC &&
2578 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2579 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2580 N1.getOperand(0), N1.getOperand(1), N2);
2581 }
Nate Begeman44728a72005-09-19 22:34:01 +00002582 return SDOperand();
2583}
2584
Chris Lattner3ea0b472005-10-05 06:47:48 +00002585// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2586//
Nate Begeman44728a72005-09-19 22:34:01 +00002587SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002588 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2589 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2590
2591 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002592 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2593 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2594
2595 // fold br_cc true, dest -> br dest (unconditional branch)
2596 if (SCCC && SCCC->getValue())
2597 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2598 N->getOperand(4));
2599 // fold br_cc false, dest -> unconditional fall through
2600 if (SCCC && SCCC->isNullValue())
2601 return N->getOperand(0);
2602 // fold to a simpler setcc
2603 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2604 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2605 Simp.getOperand(2), Simp.getOperand(0),
2606 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002607 return SDOperand();
2608}
2609
Chris Lattner01a22022005-10-10 22:04:48 +00002610SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2611 SDOperand Chain = N->getOperand(0);
2612 SDOperand Ptr = N->getOperand(1);
2613 SDOperand SrcValue = N->getOperand(2);
Jim Laskey6ff23e52006-10-04 16:53:27 +00002614
Chris Lattnere4b95392006-03-31 18:06:18 +00002615 // If there are no uses of the loaded value, change uses of the chain value
2616 // into uses of the chain input (i.e. delete the dead load).
2617 if (N->hasNUsesOfValue(0, 0))
2618 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
Chris Lattner01a22022005-10-10 22:04:48 +00002619
2620 // If this load is directly stored, replace the load value with the stored
2621 // value.
2622 // TODO: Handle store large -> read small portion.
2623 // TODO: Handle TRUNCSTORE/EXTLOAD
2624 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2625 Chain.getOperand(1).getValueType() == N->getValueType(0))
2626 return CombineTo(N, Chain.getOperand(1), Chain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00002627
Jim Laskeybb151852006-09-26 17:44:58 +00002628 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00002629 // Walk up chain skipping non-aliasing memory nodes.
2630 SDOperand BetterChain = FindBetterChain(N, Chain);
2631
Jim Laskey6ff23e52006-10-04 16:53:27 +00002632 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00002633 if (Chain != BetterChain) {
2634 // Replace the chain to void dependency.
2635 SDOperand ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
2636 SrcValue);
2637
Jim Laskey6ff23e52006-10-04 16:53:27 +00002638 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00002639 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
2640 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00002641
2642 // Replace uses with load result and token factor.
2643 return CombineTo(N, ReplLoad.getValue(0), Token);
Jim Laskey279f0532006-09-25 16:29:54 +00002644 }
2645 }
2646
Chris Lattner01a22022005-10-10 22:04:48 +00002647 return SDOperand();
2648}
2649
Evan Chengc5484282006-10-04 00:56:09 +00002650/// visitLOADX - Handle EXTLOAD/ZEXTLOAD/SEXTLOAD.
2651SDOperand DAGCombiner::visitLOADX(SDNode *N) {
Chris Lattner29cd7db2006-03-31 18:10:41 +00002652 SDOperand Chain = N->getOperand(0);
2653 SDOperand Ptr = N->getOperand(1);
2654 SDOperand SrcValue = N->getOperand(2);
2655 SDOperand EVT = N->getOperand(3);
2656
2657 // If there are no uses of the loaded value, change uses of the chain value
2658 // into uses of the chain input (i.e. delete the dead load).
2659 if (N->hasNUsesOfValue(0, 0))
2660 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
2661
2662 return SDOperand();
2663}
2664
Chris Lattner87514ca2005-10-10 22:31:19 +00002665SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2666 SDOperand Chain = N->getOperand(0);
2667 SDOperand Value = N->getOperand(1);
2668 SDOperand Ptr = N->getOperand(2);
2669 SDOperand SrcValue = N->getOperand(3);
2670
2671 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002672 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002673 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2674 // Make sure that these stores are the same value type:
2675 // FIXME: we really care that the second store is >= size of the first.
2676 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002677 // Create a new store of Value that replaces both stores.
2678 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002679 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2680 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002681 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2682 PrevStore->getOperand(0), Value, Ptr,
2683 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002684 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002685 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002686 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002687 }
2688
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002689 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002690 // FIXME: This needs to know that the resultant store does not need a
2691 // higher alignment than the original.
Jim Laskey14fbcbf2006-09-25 21:11:32 +00002692 if (0 && Value.getOpcode() == ISD::BIT_CONVERT) {
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002693 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2694 Ptr, SrcValue);
Jim Laskey279f0532006-09-25 16:29:54 +00002695 }
2696
2697 if (CombinerAA) {
Jim Laskey288af5e2006-09-25 19:32:58 +00002698 // If the store ptr is a frame index and the frame index has a use of one
2699 // and this is a return block, then the store is redundant.
2700 if (Ptr.hasOneUse() && isa<FrameIndexSDNode>(Ptr) &&
2701 DAG.getRoot().getOpcode() == ISD::RET) {
2702 return Chain;
2703 }
2704
Jim Laskey279f0532006-09-25 16:29:54 +00002705 // Walk up chain skipping non-aliasing memory nodes.
2706 SDOperand BetterChain = FindBetterChain(N, Chain);
2707
Jim Laskey6ff23e52006-10-04 16:53:27 +00002708 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00002709 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00002710 // Replace the chain to avoid dependency.
Jim Laskey279f0532006-09-25 16:29:54 +00002711 SDOperand ReplStore = DAG.getNode(ISD::STORE, MVT::Other,
2712 BetterChain, Value, Ptr,
2713 SrcValue);
2714 // Create token to keep both nodes around.
Jim Laskey6ff23e52006-10-04 16:53:27 +00002715 return DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
Jim Laskey279f0532006-09-25 16:29:54 +00002716 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00002717 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002718
Chris Lattner87514ca2005-10-10 22:31:19 +00002719 return SDOperand();
2720}
2721
Chris Lattnerca242442006-03-19 01:27:56 +00002722SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2723 SDOperand InVec = N->getOperand(0);
2724 SDOperand InVal = N->getOperand(1);
2725 SDOperand EltNo = N->getOperand(2);
2726
2727 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2728 // vector with the inserted element.
2729 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2730 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002731 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002732 if (Elt < Ops.size())
2733 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002734 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
2735 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002736 }
2737
2738 return SDOperand();
2739}
2740
2741SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2742 SDOperand InVec = N->getOperand(0);
2743 SDOperand InVal = N->getOperand(1);
2744 SDOperand EltNo = N->getOperand(2);
2745 SDOperand NumElts = N->getOperand(3);
2746 SDOperand EltType = N->getOperand(4);
2747
2748 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2749 // vector with the inserted element.
2750 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2751 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002752 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002753 if (Elt < Ops.size()-2)
2754 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002755 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
2756 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002757 }
2758
2759 return SDOperand();
2760}
2761
Chris Lattnerd7648c82006-03-28 20:28:38 +00002762SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
2763 unsigned NumInScalars = N->getNumOperands()-2;
2764 SDOperand NumElts = N->getOperand(NumInScalars);
2765 SDOperand EltType = N->getOperand(NumInScalars+1);
2766
2767 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
2768 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
2769 // two distinct vectors, turn this into a shuffle node.
2770 SDOperand VecIn1, VecIn2;
2771 for (unsigned i = 0; i != NumInScalars; ++i) {
2772 // Ignore undef inputs.
2773 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
2774
2775 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
2776 // constant index, bail out.
2777 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
2778 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
2779 VecIn1 = VecIn2 = SDOperand(0, 0);
2780 break;
2781 }
2782
2783 // If the input vector type disagrees with the result of the vbuild_vector,
2784 // we can't make a shuffle.
2785 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
2786 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
2787 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
2788 VecIn1 = VecIn2 = SDOperand(0, 0);
2789 break;
2790 }
2791
2792 // Otherwise, remember this. We allow up to two distinct input vectors.
2793 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
2794 continue;
2795
2796 if (VecIn1.Val == 0) {
2797 VecIn1 = ExtractedFromVec;
2798 } else if (VecIn2.Val == 0) {
2799 VecIn2 = ExtractedFromVec;
2800 } else {
2801 // Too many inputs.
2802 VecIn1 = VecIn2 = SDOperand(0, 0);
2803 break;
2804 }
2805 }
2806
2807 // If everything is good, we can make a shuffle operation.
2808 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002809 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00002810 for (unsigned i = 0; i != NumInScalars; ++i) {
2811 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
2812 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2813 continue;
2814 }
2815
2816 SDOperand Extract = N->getOperand(i);
2817
2818 // If extracting from the first vector, just use the index directly.
2819 if (Extract.getOperand(0) == VecIn1) {
2820 BuildVecIndices.push_back(Extract.getOperand(1));
2821 continue;
2822 }
2823
2824 // Otherwise, use InIdx + VecSize
2825 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
2826 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
2827 }
2828
2829 // Add count and size info.
2830 BuildVecIndices.push_back(NumElts);
2831 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
2832
2833 // Return the new VVECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002834 SDOperand Ops[5];
2835 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00002836 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002837 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00002838 } else {
2839 // Use an undef vbuild_vector as input for the second operand.
2840 std::vector<SDOperand> UnOps(NumInScalars,
2841 DAG.getNode(ISD::UNDEF,
2842 cast<VTSDNode>(EltType)->getVT()));
2843 UnOps.push_back(NumElts);
2844 UnOps.push_back(EltType);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002845 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2846 &UnOps[0], UnOps.size());
2847 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00002848 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002849 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2850 &BuildVecIndices[0], BuildVecIndices.size());
2851 Ops[3] = NumElts;
2852 Ops[4] = EltType;
2853 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
Chris Lattnerd7648c82006-03-28 20:28:38 +00002854 }
2855
2856 return SDOperand();
2857}
2858
Chris Lattner66445d32006-03-28 22:11:53 +00002859SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002860 SDOperand ShufMask = N->getOperand(2);
2861 unsigned NumElts = ShufMask.getNumOperands();
2862
2863 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2864 bool isIdentity = true;
2865 for (unsigned i = 0; i != NumElts; ++i) {
2866 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2867 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2868 isIdentity = false;
2869 break;
2870 }
2871 }
2872 if (isIdentity) return N->getOperand(0);
2873
2874 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2875 isIdentity = true;
2876 for (unsigned i = 0; i != NumElts; ++i) {
2877 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2878 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2879 isIdentity = false;
2880 break;
2881 }
2882 }
2883 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00002884
2885 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
2886 // needed at all.
2887 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00002888 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002889 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00002890 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002891 for (unsigned i = 0; i != NumElts; ++i)
2892 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
2893 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
2894 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00002895 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00002896 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00002897 BaseIdx = Idx;
2898 } else {
2899 if (BaseIdx != Idx)
2900 isSplat = false;
2901 if (VecNum != V) {
2902 isUnary = false;
2903 break;
2904 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00002905 }
2906 }
2907
2908 SDOperand N0 = N->getOperand(0);
2909 SDOperand N1 = N->getOperand(1);
2910 // Normalize unary shuffle so the RHS is undef.
2911 if (isUnary && VecNum == 1)
2912 std::swap(N0, N1);
2913
Evan Cheng917ec982006-07-21 08:25:53 +00002914 // If it is a splat, check if the argument vector is a build_vector with
2915 // all scalar elements the same.
2916 if (isSplat) {
2917 SDNode *V = N0.Val;
2918 if (V->getOpcode() == ISD::BIT_CONVERT)
2919 V = V->getOperand(0).Val;
2920 if (V->getOpcode() == ISD::BUILD_VECTOR) {
2921 unsigned NumElems = V->getNumOperands()-2;
2922 if (NumElems > BaseIdx) {
2923 SDOperand Base;
2924 bool AllSame = true;
2925 for (unsigned i = 0; i != NumElems; ++i) {
2926 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
2927 Base = V->getOperand(i);
2928 break;
2929 }
2930 }
2931 // Splat of <u, u, u, u>, return <u, u, u, u>
2932 if (!Base.Val)
2933 return N0;
2934 for (unsigned i = 0; i != NumElems; ++i) {
2935 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
2936 V->getOperand(i) != Base) {
2937 AllSame = false;
2938 break;
2939 }
2940 }
2941 // Splat of <x, x, x, x>, return <x, x, x, x>
2942 if (AllSame)
2943 return N0;
2944 }
2945 }
2946 }
2947
Evan Chenge7bec0d2006-07-20 22:44:41 +00002948 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
2949 // into an undef.
2950 if (isUnary || N0 == N1) {
2951 if (N0.getOpcode() == ISD::UNDEF)
Evan Chengc04766a2006-04-06 23:20:43 +00002952 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00002953 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2954 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002955 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00002956 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00002957 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2958 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2959 MappedOps.push_back(ShufMask.getOperand(i));
2960 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00002961 unsigned NewIdx =
2962 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2963 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00002964 }
2965 }
2966 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002967 &MappedOps[0], MappedOps.size());
Chris Lattner3e104b12006-04-08 04:15:24 +00002968 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00002969 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
Evan Chenge7bec0d2006-07-20 22:44:41 +00002970 N0,
Chris Lattner66445d32006-03-28 22:11:53 +00002971 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
2972 ShufMask);
2973 }
2974
2975 return SDOperand();
2976}
2977
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002978SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
2979 SDOperand ShufMask = N->getOperand(2);
2980 unsigned NumElts = ShufMask.getNumOperands()-2;
2981
2982 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2983 bool isIdentity = true;
2984 for (unsigned i = 0; i != NumElts; ++i) {
2985 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2986 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2987 isIdentity = false;
2988 break;
2989 }
2990 }
2991 if (isIdentity) return N->getOperand(0);
2992
2993 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2994 isIdentity = true;
2995 for (unsigned i = 0; i != NumElts; ++i) {
2996 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2997 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2998 isIdentity = false;
2999 break;
3000 }
3001 }
3002 if (isIdentity) return N->getOperand(1);
3003
Evan Chenge7bec0d2006-07-20 22:44:41 +00003004 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3005 // needed at all.
3006 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003007 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003008 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003009 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003010 for (unsigned i = 0; i != NumElts; ++i)
3011 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3012 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3013 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003014 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003015 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003016 BaseIdx = Idx;
3017 } else {
3018 if (BaseIdx != Idx)
3019 isSplat = false;
3020 if (VecNum != V) {
3021 isUnary = false;
3022 break;
3023 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003024 }
3025 }
3026
3027 SDOperand N0 = N->getOperand(0);
3028 SDOperand N1 = N->getOperand(1);
3029 // Normalize unary shuffle so the RHS is undef.
3030 if (isUnary && VecNum == 1)
3031 std::swap(N0, N1);
3032
Evan Cheng917ec982006-07-21 08:25:53 +00003033 // If it is a splat, check if the argument vector is a build_vector with
3034 // all scalar elements the same.
3035 if (isSplat) {
3036 SDNode *V = N0.Val;
3037 if (V->getOpcode() == ISD::VBIT_CONVERT)
3038 V = V->getOperand(0).Val;
3039 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
3040 unsigned NumElems = V->getNumOperands()-2;
3041 if (NumElems > BaseIdx) {
3042 SDOperand Base;
3043 bool AllSame = true;
3044 for (unsigned i = 0; i != NumElems; ++i) {
3045 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3046 Base = V->getOperand(i);
3047 break;
3048 }
3049 }
3050 // Splat of <u, u, u, u>, return <u, u, u, u>
3051 if (!Base.Val)
3052 return N0;
3053 for (unsigned i = 0; i != NumElems; ++i) {
3054 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3055 V->getOperand(i) != Base) {
3056 AllSame = false;
3057 break;
3058 }
3059 }
3060 // Splat of <x, x, x, x>, return <x, x, x, x>
3061 if (AllSame)
3062 return N0;
3063 }
3064 }
3065 }
3066
Evan Chenge7bec0d2006-07-20 22:44:41 +00003067 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3068 // into an undef.
3069 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00003070 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3071 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003072 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00003073 for (unsigned i = 0; i != NumElts; ++i) {
3074 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3075 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3076 MappedOps.push_back(ShufMask.getOperand(i));
3077 } else {
3078 unsigned NewIdx =
3079 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3080 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
3081 }
3082 }
3083 // Add the type/#elts values.
3084 MappedOps.push_back(ShufMask.getOperand(NumElts));
3085 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
3086
3087 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003088 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003089 AddToWorkList(ShufMask.Val);
3090
3091 // Build the undef vector.
3092 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
3093 for (unsigned i = 0; i != NumElts; ++i)
3094 MappedOps[i] = UDVal;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003095 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
3096 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003097 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3098 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003099
3100 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
Evan Chenge7bec0d2006-07-20 22:44:41 +00003101 N0, UDVal, ShufMask,
Chris Lattner17614ea2006-04-08 05:34:25 +00003102 MappedOps[NumElts], MappedOps[NumElts+1]);
3103 }
3104
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003105 return SDOperand();
3106}
3107
Evan Cheng44f1f092006-04-20 08:56:16 +00003108/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
3109/// a VAND to a vector_shuffle with the destination vector and a zero vector.
3110/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
3111/// vector_shuffle V, Zero, <0, 4, 2, 4>
3112SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
3113 SDOperand LHS = N->getOperand(0);
3114 SDOperand RHS = N->getOperand(1);
3115 if (N->getOpcode() == ISD::VAND) {
3116 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
3117 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
3118 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
3119 RHS = RHS.getOperand(0);
3120 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
3121 std::vector<SDOperand> IdxOps;
3122 unsigned NumOps = RHS.getNumOperands();
3123 unsigned NumElts = NumOps-2;
3124 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
3125 for (unsigned i = 0; i != NumElts; ++i) {
3126 SDOperand Elt = RHS.getOperand(i);
3127 if (!isa<ConstantSDNode>(Elt))
3128 return SDOperand();
3129 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
3130 IdxOps.push_back(DAG.getConstant(i, EVT));
3131 else if (cast<ConstantSDNode>(Elt)->isNullValue())
3132 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
3133 else
3134 return SDOperand();
3135 }
3136
3137 // Let's see if the target supports this vector_shuffle.
3138 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
3139 return SDOperand();
3140
3141 // Return the new VVECTOR_SHUFFLE node.
3142 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
3143 SDOperand EVTNode = DAG.getValueType(EVT);
3144 std::vector<SDOperand> Ops;
Chris Lattner516b9622006-09-14 20:50:57 +00003145 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode,
3146 EVTNode);
Evan Cheng44f1f092006-04-20 08:56:16 +00003147 Ops.push_back(LHS);
3148 AddToWorkList(LHS.Val);
3149 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
3150 ZeroOps.push_back(NumEltsNode);
3151 ZeroOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003152 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3153 &ZeroOps[0], ZeroOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003154 IdxOps.push_back(NumEltsNode);
3155 IdxOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003156 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3157 &IdxOps[0], IdxOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003158 Ops.push_back(NumEltsNode);
3159 Ops.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003160 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
3161 &Ops[0], Ops.size());
Evan Cheng44f1f092006-04-20 08:56:16 +00003162 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
3163 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
3164 DstVecSize, DstVecEVT);
3165 }
3166 return Result;
3167 }
3168 }
3169 return SDOperand();
3170}
3171
Chris Lattneredab1b92006-04-02 03:25:57 +00003172/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
3173/// the scalar operation of the vop if it is operating on an integer vector
3174/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
3175SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
3176 ISD::NodeType FPOp) {
3177 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
3178 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
3179 SDOperand LHS = N->getOperand(0);
3180 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00003181 SDOperand Shuffle = XformToShuffleWithZero(N);
3182 if (Shuffle.Val) return Shuffle;
3183
Chris Lattneredab1b92006-04-02 03:25:57 +00003184 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
3185 // this operation.
3186 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
3187 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003188 SmallVector<SDOperand, 8> Ops;
Chris Lattneredab1b92006-04-02 03:25:57 +00003189 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
3190 SDOperand LHSOp = LHS.getOperand(i);
3191 SDOperand RHSOp = RHS.getOperand(i);
3192 // If these two elements can't be folded, bail out.
3193 if ((LHSOp.getOpcode() != ISD::UNDEF &&
3194 LHSOp.getOpcode() != ISD::Constant &&
3195 LHSOp.getOpcode() != ISD::ConstantFP) ||
3196 (RHSOp.getOpcode() != ISD::UNDEF &&
3197 RHSOp.getOpcode() != ISD::Constant &&
3198 RHSOp.getOpcode() != ISD::ConstantFP))
3199 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00003200 // Can't fold divide by zero.
3201 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
3202 if ((RHSOp.getOpcode() == ISD::Constant &&
3203 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
3204 (RHSOp.getOpcode() == ISD::ConstantFP &&
3205 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
3206 break;
3207 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003208 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00003209 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00003210 assert((Ops.back().getOpcode() == ISD::UNDEF ||
3211 Ops.back().getOpcode() == ISD::Constant ||
3212 Ops.back().getOpcode() == ISD::ConstantFP) &&
3213 "Scalar binop didn't fold!");
3214 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003215
3216 if (Ops.size() == LHS.getNumOperands()-2) {
3217 Ops.push_back(*(LHS.Val->op_end()-2));
3218 Ops.push_back(*(LHS.Val->op_end()-1));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003219 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003220 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003221 }
3222
3223 return SDOperand();
3224}
3225
Nate Begeman44728a72005-09-19 22:34:01 +00003226SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00003227 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
3228
3229 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
3230 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3231 // If we got a simplified select_cc node back from SimplifySelectCC, then
3232 // break it down into a new SETCC node, and a new SELECT node, and then return
3233 // the SELECT node, since we were called with a SELECT node.
3234 if (SCC.Val) {
3235 // Check to see if we got a select_cc back (to turn into setcc/select).
3236 // Otherwise, just return whatever node we got back, like fabs.
3237 if (SCC.getOpcode() == ISD::SELECT_CC) {
3238 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
3239 SCC.getOperand(0), SCC.getOperand(1),
3240 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00003241 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003242 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
3243 SCC.getOperand(3), SETCC);
3244 }
3245 return SCC;
3246 }
Nate Begeman44728a72005-09-19 22:34:01 +00003247 return SDOperand();
3248}
3249
Chris Lattner40c62d52005-10-18 06:04:22 +00003250/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
3251/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00003252/// select. Callers of this should assume that TheSelect is deleted if this
3253/// returns true. As such, they should return the appropriate thing (e.g. the
3254/// node) back to the top-level of the DAG combiner loop to avoid it being
3255/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00003256///
3257bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
3258 SDOperand RHS) {
3259
3260 // If this is a select from two identical things, try to pull the operation
3261 // through the select.
3262 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
3263#if 0
3264 std::cerr << "SELECT: ["; LHS.Val->dump();
3265 std::cerr << "] ["; RHS.Val->dump();
3266 std::cerr << "]\n";
3267#endif
3268
3269 // If this is a load and the token chain is identical, replace the select
3270 // of two loads with a load through a select of the address to load from.
3271 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
3272 // constants have been dropped into the constant pool.
3273 if ((LHS.getOpcode() == ISD::LOAD ||
Evan Chengc5484282006-10-04 00:56:09 +00003274 LHS.getOpcode() == ISD::LOADX ) &&
Chris Lattner40c62d52005-10-18 06:04:22 +00003275 // Token chains must be identical.
3276 LHS.getOperand(0) == RHS.getOperand(0) &&
3277 // If this is an EXTLOAD, the VT's must match.
3278 (LHS.getOpcode() == ISD::LOAD ||
3279 LHS.getOperand(3) == RHS.getOperand(3))) {
3280 // FIXME: this conflates two src values, discarding one. This is not
3281 // the right thing to do, but nothing uses srcvalues now. When they do,
3282 // turn SrcValue into a list of locations.
3283 SDOperand Addr;
3284 if (TheSelect->getOpcode() == ISD::SELECT)
3285 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
3286 TheSelect->getOperand(0), LHS.getOperand(1),
3287 RHS.getOperand(1));
3288 else
3289 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
3290 TheSelect->getOperand(0),
3291 TheSelect->getOperand(1),
3292 LHS.getOperand(1), RHS.getOperand(1),
3293 TheSelect->getOperand(4));
3294
3295 SDOperand Load;
3296 if (LHS.getOpcode() == ISD::LOAD)
3297 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
3298 Addr, LHS.getOperand(2));
Evan Chengc5484282006-10-04 00:56:09 +00003299 else {
3300 unsigned LType = LHS.getConstantOperandVal(4);
3301 Load = DAG.getExtLoad((ISD::LoadExtType)LType,
3302 TheSelect->getValueType(0),
Chris Lattner40c62d52005-10-18 06:04:22 +00003303 LHS.getOperand(0), Addr, LHS.getOperand(2),
3304 cast<VTSDNode>(LHS.getOperand(3))->getVT());
Evan Chengc5484282006-10-04 00:56:09 +00003305 }
Chris Lattner40c62d52005-10-18 06:04:22 +00003306 // Users of the select now use the result of the load.
3307 CombineTo(TheSelect, Load);
3308
3309 // Users of the old loads now use the new load's chain. We know the
3310 // old-load value is dead now.
3311 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
3312 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
3313 return true;
3314 }
3315 }
3316
3317 return false;
3318}
3319
Nate Begeman44728a72005-09-19 22:34:01 +00003320SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
3321 SDOperand N2, SDOperand N3,
3322 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00003323
3324 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00003325 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
3326 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
3327 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
3328
3329 // Determine if the condition we're dealing with is constant
3330 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
3331 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
3332
3333 // fold select_cc true, x, y -> x
3334 if (SCCC && SCCC->getValue())
3335 return N2;
3336 // fold select_cc false, x, y -> y
3337 if (SCCC && SCCC->getValue() == 0)
3338 return N3;
3339
3340 // Check to see if we can simplify the select into an fabs node
3341 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
3342 // Allow either -0.0 or 0.0
3343 if (CFP->getValue() == 0.0) {
3344 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
3345 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
3346 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
3347 N2 == N3.getOperand(0))
3348 return DAG.getNode(ISD::FABS, VT, N0);
3349
3350 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
3351 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3352 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3353 N2.getOperand(0) == N3)
3354 return DAG.getNode(ISD::FABS, VT, N3);
3355 }
3356 }
3357
3358 // Check to see if we can perform the "gzip trick", transforming
3359 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00003360 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00003361 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00003362 MVT::isInteger(N2.getValueType()) &&
3363 (N1C->isNullValue() || // (a < 0) ? b : 0
3364 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00003365 MVT::ValueType XType = N0.getValueType();
3366 MVT::ValueType AType = N2.getValueType();
3367 if (XType >= AType) {
3368 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00003369 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00003370 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3371 unsigned ShCtV = Log2_64(N2C->getValue());
3372 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3373 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3374 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00003375 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003376 if (XType > AType) {
3377 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003378 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003379 }
3380 return DAG.getNode(ISD::AND, AType, Shift, N2);
3381 }
3382 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3383 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3384 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003385 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003386 if (XType > AType) {
3387 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003388 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003389 }
3390 return DAG.getNode(ISD::AND, AType, Shift, N2);
3391 }
3392 }
Nate Begeman07ed4172005-10-10 21:26:48 +00003393
3394 // fold select C, 16, 0 -> shl C, 4
3395 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3396 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3397 // Get a SetCC of the condition
3398 // FIXME: Should probably make sure that setcc is legal if we ever have a
3399 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00003400 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00003401 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00003402 if (AfterLegalize) {
3403 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003404 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00003405 } else {
3406 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003407 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00003408 }
Chris Lattner5750df92006-03-01 04:03:14 +00003409 AddToWorkList(SCC.Val);
3410 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00003411 // shl setcc result by log2 n2c
3412 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3413 DAG.getConstant(Log2_64(N2C->getValue()),
3414 TLI.getShiftAmountTy()));
3415 }
3416
Nate Begemanf845b452005-10-08 00:29:44 +00003417 // Check to see if this is the equivalent of setcc
3418 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3419 // otherwise, go ahead with the folds.
3420 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3421 MVT::ValueType XType = N0.getValueType();
3422 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3423 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3424 if (Res.getValueType() != VT)
3425 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3426 return Res;
3427 }
3428
3429 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3430 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3431 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3432 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3433 return DAG.getNode(ISD::SRL, XType, Ctlz,
3434 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3435 TLI.getShiftAmountTy()));
3436 }
3437 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3438 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3439 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3440 N0);
3441 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3442 DAG.getConstant(~0ULL, XType));
3443 return DAG.getNode(ISD::SRL, XType,
3444 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3445 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3446 TLI.getShiftAmountTy()));
3447 }
3448 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3449 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3450 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3451 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3452 TLI.getShiftAmountTy()));
3453 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3454 }
3455 }
3456
3457 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3458 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3459 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3460 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3461 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3462 MVT::ValueType XType = N0.getValueType();
3463 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3464 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3465 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3466 TLI.getShiftAmountTy()));
3467 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003468 AddToWorkList(Shift.Val);
3469 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003470 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3471 }
3472 }
3473 }
3474
Nate Begeman44728a72005-09-19 22:34:01 +00003475 return SDOperand();
3476}
3477
Nate Begeman452d7be2005-09-16 00:54:12 +00003478SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00003479 SDOperand N1, ISD::CondCode Cond,
3480 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003481 // These setcc operations always fold.
3482 switch (Cond) {
3483 default: break;
3484 case ISD::SETFALSE:
3485 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3486 case ISD::SETTRUE:
3487 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3488 }
3489
3490 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3491 uint64_t C1 = N1C->getValue();
3492 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
3493 uint64_t C0 = N0C->getValue();
3494
3495 // Sign extend the operands if required
3496 if (ISD::isSignedIntSetCC(Cond)) {
3497 C0 = N0C->getSignExtended();
3498 C1 = N1C->getSignExtended();
3499 }
3500
3501 switch (Cond) {
3502 default: assert(0 && "Unknown integer setcc!");
3503 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3504 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3505 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
3506 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
3507 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
3508 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
3509 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
3510 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
3511 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
3512 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
3513 }
3514 } else {
Chris Lattner5f42a242006-09-20 06:19:26 +00003515 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
3516 // equality comparison, then we're just comparing whether X itself is
3517 // zero.
3518 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
3519 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
3520 N0.getOperand(1).getOpcode() == ISD::Constant) {
3521 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
3522 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3523 ShAmt == Log2_32(MVT::getSizeInBits(N0.getValueType()))) {
3524 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
3525 // (srl (ctlz x), 5) == 0 -> X != 0
3526 // (srl (ctlz x), 5) != 1 -> X != 0
3527 Cond = ISD::SETNE;
3528 } else {
3529 // (srl (ctlz x), 5) != 0 -> X == 0
3530 // (srl (ctlz x), 5) == 1 -> X == 0
3531 Cond = ISD::SETEQ;
3532 }
3533 SDOperand Zero = DAG.getConstant(0, N0.getValueType());
3534 return DAG.getSetCC(VT, N0.getOperand(0).getOperand(0),
3535 Zero, Cond);
3536 }
3537 }
3538
Nate Begeman452d7be2005-09-16 00:54:12 +00003539 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3540 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3541 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3542
3543 // If the comparison constant has bits in the upper part, the
3544 // zero-extended value could never match.
3545 if (C1 & (~0ULL << InSize)) {
3546 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3547 switch (Cond) {
3548 case ISD::SETUGT:
3549 case ISD::SETUGE:
3550 case ISD::SETEQ: return DAG.getConstant(0, VT);
3551 case ISD::SETULT:
3552 case ISD::SETULE:
3553 case ISD::SETNE: return DAG.getConstant(1, VT);
3554 case ISD::SETGT:
3555 case ISD::SETGE:
3556 // True if the sign bit of C1 is set.
3557 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3558 case ISD::SETLT:
3559 case ISD::SETLE:
3560 // True if the sign bit of C1 isn't set.
3561 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3562 default:
3563 break;
3564 }
3565 }
3566
3567 // Otherwise, we can perform the comparison with the low bits.
3568 switch (Cond) {
3569 case ISD::SETEQ:
3570 case ISD::SETNE:
3571 case ISD::SETUGT:
3572 case ISD::SETUGE:
3573 case ISD::SETULT:
3574 case ISD::SETULE:
3575 return DAG.getSetCC(VT, N0.getOperand(0),
3576 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3577 Cond);
3578 default:
3579 break; // todo, be more careful with signed comparisons
3580 }
3581 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3582 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3583 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3584 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3585 MVT::ValueType ExtDstTy = N0.getValueType();
3586 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3587
3588 // If the extended part has any inconsistent bits, it cannot ever
3589 // compare equal. In other words, they have to be all ones or all
3590 // zeros.
3591 uint64_t ExtBits =
3592 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3593 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3594 return DAG.getConstant(Cond == ISD::SETNE, VT);
3595
3596 SDOperand ZextOp;
3597 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3598 if (Op0Ty == ExtSrcTy) {
3599 ZextOp = N0.getOperand(0);
3600 } else {
3601 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3602 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3603 DAG.getConstant(Imm, Op0Ty));
3604 }
Chris Lattner5750df92006-03-01 04:03:14 +00003605 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003606 // Otherwise, make this a use of a zext.
3607 return DAG.getSetCC(VT, ZextOp,
3608 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3609 ExtDstTy),
3610 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00003611 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
3612 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3613 (N0.getOpcode() == ISD::XOR ||
3614 (N0.getOpcode() == ISD::AND &&
3615 N0.getOperand(0).getOpcode() == ISD::XOR &&
3616 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3617 isa<ConstantSDNode>(N0.getOperand(1)) &&
3618 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
3619 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
3620 // only do this if the top bits are known zero.
3621 if (TLI.MaskedValueIsZero(N1,
3622 MVT::getIntVTBitMask(N0.getValueType())-1)) {
3623 // Okay, get the un-inverted input value.
3624 SDOperand Val;
3625 if (N0.getOpcode() == ISD::XOR)
3626 Val = N0.getOperand(0);
3627 else {
3628 assert(N0.getOpcode() == ISD::AND &&
3629 N0.getOperand(0).getOpcode() == ISD::XOR);
3630 // ((X^1)&1)^1 -> X & 1
3631 Val = DAG.getNode(ISD::AND, N0.getValueType(),
3632 N0.getOperand(0).getOperand(0), N0.getOperand(1));
3633 }
3634 return DAG.getSetCC(VT, Val, N1,
3635 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
3636 }
Nate Begeman452d7be2005-09-16 00:54:12 +00003637 }
Chris Lattner5c46f742005-10-05 06:11:08 +00003638
Nate Begeman452d7be2005-09-16 00:54:12 +00003639 uint64_t MinVal, MaxVal;
3640 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
3641 if (ISD::isSignedIntSetCC(Cond)) {
3642 MinVal = 1ULL << (OperandBitSize-1);
3643 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
3644 MaxVal = ~0ULL >> (65-OperandBitSize);
3645 else
3646 MaxVal = 0;
3647 } else {
3648 MinVal = 0;
3649 MaxVal = ~0ULL >> (64-OperandBitSize);
3650 }
3651
3652 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
3653 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
3654 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
3655 --C1; // X >= C0 --> X > (C0-1)
3656 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3657 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
3658 }
3659
3660 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
3661 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
3662 ++C1; // X <= C0 --> X < (C0+1)
3663 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3664 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
3665 }
3666
3667 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
3668 return DAG.getConstant(0, VT); // X < MIN --> false
3669
3670 // Canonicalize setgt X, Min --> setne X, Min
3671 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
3672 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00003673 // Canonicalize setlt X, Max --> setne X, Max
3674 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
3675 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00003676
3677 // If we have setult X, 1, turn it into seteq X, 0
3678 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
3679 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
3680 ISD::SETEQ);
3681 // If we have setugt X, Max-1, turn it into seteq X, Max
3682 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
3683 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
3684 ISD::SETEQ);
3685
3686 // If we have "setcc X, C0", check to see if we can shrink the immediate
3687 // by changing cc.
3688
3689 // SETUGT X, SINTMAX -> SETLT X, 0
3690 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
3691 C1 == (~0ULL >> (65-OperandBitSize)))
3692 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
3693 ISD::SETLT);
3694
3695 // FIXME: Implement the rest of these.
3696
3697 // Fold bit comparisons when we can.
3698 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3699 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
3700 if (ConstantSDNode *AndRHS =
3701 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3702 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
3703 // Perform the xform if the AND RHS is a single bit.
3704 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
3705 return DAG.getNode(ISD::SRL, VT, N0,
3706 DAG.getConstant(Log2_64(AndRHS->getValue()),
3707 TLI.getShiftAmountTy()));
3708 }
3709 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
3710 // (X & 8) == 8 --> (X & 8) >> 3
3711 // Perform the xform if C1 is a single bit.
3712 if ((C1 & (C1-1)) == 0) {
3713 return DAG.getNode(ISD::SRL, VT, N0,
Chris Lattner729c6d12006-05-27 00:43:02 +00003714 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
Nate Begeman452d7be2005-09-16 00:54:12 +00003715 }
3716 }
3717 }
3718 }
3719 } else if (isa<ConstantSDNode>(N0.Val)) {
3720 // Ensure that the constant occurs on the RHS.
3721 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3722 }
3723
3724 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
3725 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
3726 double C0 = N0C->getValue(), C1 = N1C->getValue();
3727
3728 switch (Cond) {
3729 default: break; // FIXME: Implement the rest of these!
3730 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3731 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3732 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
3733 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
3734 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
3735 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
3736 }
3737 } else {
3738 // Ensure that the constant occurs on the RHS.
3739 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3740 }
3741
3742 if (N0 == N1) {
3743 // We can always fold X == Y for integer setcc's.
3744 if (MVT::isInteger(N0.getValueType()))
3745 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3746 unsigned UOF = ISD::getUnorderedFlavor(Cond);
3747 if (UOF == 2) // FP operators that are undefined on NaNs.
3748 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3749 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
3750 return DAG.getConstant(UOF, VT);
3751 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
3752 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00003753 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00003754 if (NewCond != Cond)
3755 return DAG.getSetCC(VT, N0, N1, NewCond);
3756 }
3757
3758 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3759 MVT::isInteger(N0.getValueType())) {
3760 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
3761 N0.getOpcode() == ISD::XOR) {
3762 // Simplify (X+Y) == (X+Z) --> Y == Z
3763 if (N0.getOpcode() == N1.getOpcode()) {
3764 if (N0.getOperand(0) == N1.getOperand(0))
3765 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
3766 if (N0.getOperand(1) == N1.getOperand(1))
3767 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
Evan Cheng1efba0e2006-08-29 06:42:35 +00003768 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003769 // If X op Y == Y op X, try other combinations.
3770 if (N0.getOperand(0) == N1.getOperand(1))
3771 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
3772 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00003773 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00003774 }
3775 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003776
3777 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
3778 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3779 // Turn (X+C1) == C2 --> X == C2-C1
3780 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
3781 return DAG.getSetCC(VT, N0.getOperand(0),
3782 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
3783 N0.getValueType()), Cond);
3784 }
3785
3786 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
3787 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00003788 // If we know that all of the inverted bits are zero, don't bother
3789 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003790 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00003791 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003792 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00003793 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003794 }
3795
3796 // Turn (C1-X) == C2 --> X == C1-C2
3797 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
3798 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
3799 return DAG.getSetCC(VT, N0.getOperand(1),
3800 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
3801 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00003802 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003803 }
3804 }
3805
Nate Begeman452d7be2005-09-16 00:54:12 +00003806 // Simplify (X+Z) == X --> Z == 0
3807 if (N0.getOperand(0) == N1)
3808 return DAG.getSetCC(VT, N0.getOperand(1),
3809 DAG.getConstant(0, N0.getValueType()), Cond);
3810 if (N0.getOperand(1) == N1) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00003811 if (DAG.isCommutativeBinOp(N0.getOpcode()))
Nate Begeman452d7be2005-09-16 00:54:12 +00003812 return DAG.getSetCC(VT, N0.getOperand(0),
3813 DAG.getConstant(0, N0.getValueType()), Cond);
3814 else {
3815 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
3816 // (Z-X) == X --> Z == X<<1
3817 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
3818 N1,
3819 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003820 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003821 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
3822 }
3823 }
3824 }
3825
3826 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3827 N1.getOpcode() == ISD::XOR) {
3828 // Simplify X == (X+Z) --> Z == 0
3829 if (N1.getOperand(0) == N0) {
3830 return DAG.getSetCC(VT, N1.getOperand(1),
3831 DAG.getConstant(0, N1.getValueType()), Cond);
3832 } else if (N1.getOperand(1) == N0) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00003833 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003834 return DAG.getSetCC(VT, N1.getOperand(0),
3835 DAG.getConstant(0, N1.getValueType()), Cond);
3836 } else {
3837 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3838 // X == (Z-X) --> X<<1 == Z
3839 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
3840 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003841 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003842 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
3843 }
3844 }
3845 }
3846 }
3847
3848 // Fold away ALL boolean setcc's.
3849 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00003850 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003851 switch (Cond) {
3852 default: assert(0 && "Unknown integer setcc!");
3853 case ISD::SETEQ: // X == Y -> (X^Y)^1
3854 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3855 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00003856 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003857 break;
3858 case ISD::SETNE: // X != Y --> (X^Y)
3859 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3860 break;
3861 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
3862 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
3863 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3864 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003865 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003866 break;
3867 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
3868 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
3869 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3870 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003871 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003872 break;
3873 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
3874 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
3875 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3876 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003877 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003878 break;
3879 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
3880 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
3881 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3882 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
3883 break;
3884 }
3885 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00003886 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003887 // FIXME: If running after legalize, we probably can't do this.
3888 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3889 }
3890 return N0;
3891 }
3892
3893 // Could not fold it.
3894 return SDOperand();
3895}
3896
Nate Begeman69575232005-10-20 02:15:44 +00003897/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3898/// return a DAG expression to select that will generate the same value by
3899/// multiplying by a magic number. See:
3900/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3901SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003902 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003903 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
3904
Andrew Lenharth232c9102006-06-12 16:07:18 +00003905 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003906 ii != ee; ++ii)
3907 AddToWorkList(*ii);
3908 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003909}
3910
3911/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3912/// return a DAG expression to select that will generate the same value by
3913/// multiplying by a magic number. See:
3914/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3915SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003916 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003917 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00003918
Andrew Lenharth232c9102006-06-12 16:07:18 +00003919 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003920 ii != ee; ++ii)
3921 AddToWorkList(*ii);
3922 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003923}
3924
Jim Laskey6ff23e52006-10-04 16:53:27 +00003925/// FindBaseOffset - Return true if base is known not to alias with anything
3926/// but itself. Provides base object and offset as results.
Jim Laskey279f0532006-09-25 16:29:54 +00003927bool DAGCombiner::FindBaseOffset(SDOperand Ptr,
3928 SDOperand &Object, int64_t &Offset) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00003929 // If it's an adding or subtracting a simple constant then add the constant
3930 // to the offset.
3931 if (Ptr.getOpcode() == ISD::ADD) {
Jim Laskey279f0532006-09-25 16:29:54 +00003932 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Ptr.getOperand(1))) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00003933 bool IsNonAliasing = FindBaseOffset(Ptr.getOperand(0), Object, Offset);
Jim Laskey279f0532006-09-25 16:29:54 +00003934 Offset += C->getValue();
Jim Laskey6ff23e52006-10-04 16:53:27 +00003935 return IsNonAliasing;
3936 }
3937 } else if (Ptr.getOpcode() == ISD::SUB) {
3938 // FIXME - Aren't all subtract constants converted to add negative constant.
3939 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Ptr.getOperand(1))) {
3940 bool IsNonAliasing = FindBaseOffset(Ptr.getOperand(0), Object, Offset);
3941 Offset -= C->getValue();
3942 return IsNonAliasing;
Jim Laskey279f0532006-09-25 16:29:54 +00003943 }
3944 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00003945
3946 // Primitive operation.
3947 Object = Ptr; Offset = 0;
3948
3949 // If it's any of the following then it can't alias with anything but itself.
3950 return isa<FrameIndexSDNode>(Ptr) ||
3951 isa<ConstantPoolSDNode>(Ptr) ||
3952 isa<GlobalAddressSDNode>(Ptr);
Jim Laskey279f0532006-09-25 16:29:54 +00003953}
3954
Jim Laskey6ff23e52006-10-04 16:53:27 +00003955/// isAlias - Return true if there is any possibility that the two addresses
Jim Laskey279f0532006-09-25 16:29:54 +00003956/// overlap.
3957bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
3958 SDOperand SrcValue1,
3959 SDOperand Ptr2, int64_t Size2,
3960 SDOperand SrcValue2) {
3961 // If they are the same then they must be aliases.
3962 if (Ptr1 == Ptr2) return true;
3963
Jim Laskey6ff23e52006-10-04 16:53:27 +00003964 // Gather base node and offset information.
Jim Laskey279f0532006-09-25 16:29:54 +00003965 SDOperand Object1, Object2;
3966 int64_t Offset1, Offset2;
Jim Laskey6ff23e52006-10-04 16:53:27 +00003967 bool IsNonAliasing1 = FindBaseOffset(Ptr1, Object1, Offset1);
3968 bool IsNonAliasing2 = FindBaseOffset(Ptr2, Object2, Offset2);
3969
3970 // If they have a same base address then...
3971 if (Object1 == Object2) {
Jim Laskey279f0532006-09-25 16:29:54 +00003972 // Check to see if the addresses overlap.
Jim Laskey6ff23e52006-10-04 16:53:27 +00003973 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Jim Laskey279f0532006-09-25 16:29:54 +00003974 }
3975
Jim Laskey6ff23e52006-10-04 16:53:27 +00003976 // Otherwise they alias if they are both non aliasing.
3977 return !IsNonAliasing1 && IsNonAliasing2;
Jim Laskey279f0532006-09-25 16:29:54 +00003978}
3979
3980/// FindAliasInfo - Extracts the relevant alias information from the memory
3981/// node.
3982void DAGCombiner::FindAliasInfo(SDNode *N,
3983 SDOperand &Ptr, int64_t &Size, SDOperand &SrcValue) {
3984 switch (N->getOpcode()) {
3985 case ISD::LOAD:
3986 Ptr = N->getOperand(1);
Jim Laskey3dd11702006-09-26 08:14:06 +00003987 Size = MVT::getSizeInBits(N->getValueType(0)) >> 3;
Jim Laskey279f0532006-09-25 16:29:54 +00003988 SrcValue = N->getOperand(2);
3989 break;
3990 case ISD::STORE:
3991 Ptr = N->getOperand(2);
3992 Size = MVT::getSizeInBits(N->getOperand(1).getValueType()) >> 3;
3993 SrcValue = N->getOperand(3);
3994 break;
3995 default:
Jim Laskey6ff23e52006-10-04 16:53:27 +00003996 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey279f0532006-09-25 16:29:54 +00003997 }
3998}
3999
Jim Laskey6ff23e52006-10-04 16:53:27 +00004000/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
4001/// looking for aliasing nodes and adding them to the Aliases vector.
4002void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand Chain,
4003 SmallVector<SDOperand, 8> &Aliases) {
4004 SmallVector<SDOperand, 8> Ops; // List of operands to visit.
4005 std::set<SDNode *> Visited; // Visited node set.
4006
Jim Laskey279f0532006-09-25 16:29:54 +00004007 // Get alias information for node.
4008 SDOperand Ptr;
4009 int64_t Size;
4010 SDOperand SrcValue;
4011 FindAliasInfo(N, Ptr, Size, SrcValue);
4012
Jim Laskey6ff23e52006-10-04 16:53:27 +00004013 // Starting off.
4014 Ops.push_back(Chain);
4015
4016 // While there are nodes to process.
4017 while (!Ops.empty()) {
4018 SDOperand Op = Ops.back();
4019 Ops.pop_back();
4020
4021 for (bool Done = false; !Done;) {
4022 // Don't bother if we've been before.
4023 if (Visited.find(Op.Val) != Visited.end()) break;
4024 Visited.insert(Op.Val);
4025
4026 // Assume we're done.
4027 Done = true;
4028
4029 switch (Op.getOpcode()) {
4030 case ISD::EntryToken:
4031 // Entry token is ideal chain operand, but handled in FindBetterChain.
4032 break;
Jim Laskey279f0532006-09-25 16:29:54 +00004033
Jim Laskey6ff23e52006-10-04 16:53:27 +00004034 case ISD::LOAD:
4035 case ISD::STORE: {
4036 // Get alias information for Op.
4037 SDOperand OpPtr;
4038 int64_t OpSize;
4039 SDOperand OpSrcValue;
4040 FindAliasInfo(Op.Val, OpPtr, OpSize, OpSrcValue);
4041
4042 // If chain is alias then stop here.
4043 if (isAlias(Ptr, Size, SrcValue, OpPtr, OpSize, OpSrcValue)) {
4044 Aliases.push_back(Op);
4045 } else {
4046 // Otherwise walk up the chain.
4047 // Clean up old chain.
4048 AddToWorkList(Op.Val);
4049 // Try up further.
4050 Op = Op.getOperand(0);
4051 // We're not done yet.
4052 Done = false;
Jim Laskey279f0532006-09-25 16:29:54 +00004053 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004054 break;
4055 }
4056
4057 case ISD::TokenFactor:
4058 // Queue up operands in reverse order to maintain prior order.
4059 for (unsigned n = Op.getNumOperands(); n;)
4060 Ops.push_back(Op.getOperand(--n));
4061 // Eliminate the token factor if we can.
4062 AddToWorkList(Op.Val);
4063 break;
Jim Laskey279f0532006-09-25 16:29:54 +00004064
Jim Laskey6ff23e52006-10-04 16:53:27 +00004065 default:
4066 // For all other instructions we will just have to take what we can get.
4067 Aliases.push_back(Op);
4068 break;
Jim Laskey279f0532006-09-25 16:29:54 +00004069 }
Jim Laskey279f0532006-09-25 16:29:54 +00004070 }
4071 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004072}
4073
4074/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
4075/// for a better chain (aliasing node.)
4076SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
4077 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00004078
Jim Laskey6ff23e52006-10-04 16:53:27 +00004079 // Accumulate all the aliases to this node.
4080 GatherAllAliases(N, OldChain, Aliases);
4081
4082 if (Aliases.size() == 0) {
4083 // If no operands then chain to entry token.
4084 return DAG.getEntryNode();
4085 } else if (Aliases.size() == 1) {
4086 // If a single operand then chain to it. We don't need to revisit it.
4087 return Aliases[0];
4088 }
4089
4090 // Construct a custom tailored token factor.
4091 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4092 &Aliases[0], Aliases.size());
4093
4094 // Make sure the old chain gets cleaned up.
4095 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
4096
4097 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00004098}
4099
Nate Begeman1d4d4142005-09-01 00:19:25 +00004100// SelectionDAG::Combine - This is the entry point for the file.
4101//
Nate Begeman4ebd8052005-09-01 23:24:04 +00004102void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00004103 /// run - This is the main entry point to this class.
4104 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00004105 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004106}