blob: 69478e3b8460c1804826e9ede43b867e70204369 [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//
19// FIXME: Should add a corresponding version of fold AND with
20// ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
21// we don't have yet.
22//
Nate Begeman44728a72005-09-19 22:34:01 +000023// FIXME: select C, pow2, pow2 -> something smart
24// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
25// FIXME: (select C, load A, load B) -> load (select C, A, B)
Nate Begeman44728a72005-09-19 22:34:01 +000026// FIXME: Dead stores -> nuke
27// FIXME: shr X, (and Y,31) -> shr X, Y
28// FIXME: TRUNC (LOAD) -> EXT_LOAD/LOAD(smaller)
Nate Begeman1d4d4142005-09-01 00:19:25 +000029// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000030// FIXME: undef values
Nate Begeman4ebd8052005-09-01 23:24:04 +000031// FIXME: make truncate see through SIGN_EXTEND and AND
Nate Begeman4ebd8052005-09-01 23:24:04 +000032// FIXME: (sra (sra x, c1), c2) -> (sra x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +000033// FIXME: verify that getNode can't return extends with an operand whose type
34// is >= to that of the extend.
35// FIXME: divide by zero is currently left unfolded. do we want to turn this
36// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000037// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Chris Lattner01a22022005-10-10 22:04:48 +000038// FIXME: reassociate (X+C)+Y into (X+Y)+C if the inner expression has one use
Nate Begeman1d4d4142005-09-01 00:19:25 +000039//
40//===----------------------------------------------------------------------===//
41
42#define DEBUG_TYPE "dagcombine"
43#include "llvm/ADT/Statistic.h"
44#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000045#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000046#include "llvm/Support/MathExtras.h"
47#include "llvm/Target/TargetLowering.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000048#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000049#include <cmath>
50using namespace llvm;
51
52namespace {
53 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
54
55 class DAGCombiner {
56 SelectionDAG &DAG;
57 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000058 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000059
60 // Worklist of all of the nodes that need to be simplified.
61 std::vector<SDNode*> WorkList;
62
63 /// AddUsersToWorkList - When an instruction is simplified, add all users of
64 /// the instruction to the work lists because they might get more simplified
65 /// now.
66 ///
67 void AddUsersToWorkList(SDNode *N) {
68 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000069 UI != UE; ++UI)
70 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000071 }
72
73 /// removeFromWorkList - remove all instances of N from the worklist.
74 void removeFromWorkList(SDNode *N) {
75 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
76 WorkList.end());
77 }
78
Chris Lattner01a22022005-10-10 22:04:48 +000079 SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner87514ca2005-10-10 22:31:19 +000080 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000081 DEBUG(std::cerr << "\nReplacing "; N->dump();
82 std::cerr << "\nWith: "; To[0].Val->dump();
83 std::cerr << " and " << To.size()-1 << " other values\n");
84 std::vector<SDNode*> NowDead;
85 DAG.ReplaceAllUsesWith(N, To, &NowDead);
86
87 // Push the new nodes and any users onto the worklist
88 for (unsigned i = 0, e = To.size(); i != e; ++i) {
89 WorkList.push_back(To[i].Val);
90 AddUsersToWorkList(To[i].Val);
91 }
92
93 // Nodes can end up on the worklist more than once. Make sure we do
94 // not process a node that has been replaced.
95 removeFromWorkList(N);
96 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
97 removeFromWorkList(NowDead[i]);
98
99 // Finally, since the node is now dead, remove it from the graph.
100 DAG.DeleteNode(N);
101 return SDOperand(N, 0);
102 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000103
104 SDOperand CombineTo(SDNode *N, SDOperand Res) {
105 std::vector<SDOperand> To;
106 To.push_back(Res);
107 return CombineTo(N, To);
108 }
Chris Lattner01a22022005-10-10 22:04:48 +0000109
110 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
111 std::vector<SDOperand> To;
112 To.push_back(Res0);
113 To.push_back(Res1);
114 return CombineTo(N, To);
115 }
116
Nate Begeman1d4d4142005-09-01 00:19:25 +0000117 /// visit - call the node-specific routine that knows how to fold each
118 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000119 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000120
121 // Visitation implementation - Implement dag node combining for different
122 // node types. The semantics are as follows:
123 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000124 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000125 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000126 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000127 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000128 SDOperand visitTokenFactor(SDNode *N);
129 SDOperand visitADD(SDNode *N);
130 SDOperand visitSUB(SDNode *N);
131 SDOperand visitMUL(SDNode *N);
132 SDOperand visitSDIV(SDNode *N);
133 SDOperand visitUDIV(SDNode *N);
134 SDOperand visitSREM(SDNode *N);
135 SDOperand visitUREM(SDNode *N);
136 SDOperand visitMULHU(SDNode *N);
137 SDOperand visitMULHS(SDNode *N);
138 SDOperand visitAND(SDNode *N);
139 SDOperand visitOR(SDNode *N);
140 SDOperand visitXOR(SDNode *N);
141 SDOperand visitSHL(SDNode *N);
142 SDOperand visitSRA(SDNode *N);
143 SDOperand visitSRL(SDNode *N);
144 SDOperand visitCTLZ(SDNode *N);
145 SDOperand visitCTTZ(SDNode *N);
146 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000147 SDOperand visitSELECT(SDNode *N);
148 SDOperand visitSELECT_CC(SDNode *N);
149 SDOperand visitSETCC(SDNode *N);
Nate Begeman5054f162005-10-14 01:12:21 +0000150 SDOperand visitADD_PARTS(SDNode *N);
151 SDOperand visitSUB_PARTS(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000152 SDOperand visitSIGN_EXTEND(SDNode *N);
153 SDOperand visitZERO_EXTEND(SDNode *N);
154 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
155 SDOperand visitTRUNCATE(SDNode *N);
Nate Begeman5054f162005-10-14 01:12:21 +0000156
Chris Lattner01b3d732005-09-28 22:28:18 +0000157 SDOperand visitFADD(SDNode *N);
158 SDOperand visitFSUB(SDNode *N);
159 SDOperand visitFMUL(SDNode *N);
160 SDOperand visitFDIV(SDNode *N);
161 SDOperand visitFREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000162 SDOperand visitSINT_TO_FP(SDNode *N);
163 SDOperand visitUINT_TO_FP(SDNode *N);
164 SDOperand visitFP_TO_SINT(SDNode *N);
165 SDOperand visitFP_TO_UINT(SDNode *N);
166 SDOperand visitFP_ROUND(SDNode *N);
167 SDOperand visitFP_ROUND_INREG(SDNode *N);
168 SDOperand visitFP_EXTEND(SDNode *N);
169 SDOperand visitFNEG(SDNode *N);
170 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000171 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000172 SDOperand visitBRCONDTWOWAY(SDNode *N);
173 SDOperand visitBR_CC(SDNode *N);
174 SDOperand visitBRTWOWAY_CC(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000175
Chris Lattner01a22022005-10-10 22:04:48 +0000176 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000177 SDOperand visitSTORE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000178
Nate Begeman44728a72005-09-19 22:34:01 +0000179 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
180 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
181 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000182 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000183 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000184public:
185 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000186 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000187
188 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000189 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000190 };
191}
192
Nate Begeman07ed4172005-10-10 21:26:48 +0000193/// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero. We use
194/// this predicate to simplify operations downstream. Op and Mask are known to
Nate Begeman1d4d4142005-09-01 00:19:25 +0000195/// be the same type.
196static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
197 const TargetLowering &TLI) {
198 unsigned SrcBits;
199 if (Mask == 0) return true;
200
201 // If we know the result of a setcc has the top bits zero, use this info.
202 switch (Op.getOpcode()) {
Nate Begeman4ebd8052005-09-01 23:24:04 +0000203 case ISD::Constant:
204 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
205 case ISD::SETCC:
206 return ((Mask & 1) == 0) &&
207 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
208 case ISD::ZEXTLOAD:
209 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
210 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
211 case ISD::ZERO_EXTEND:
212 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
213 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
214 case ISD::AssertZext:
215 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
216 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
217 case ISD::AND:
Chris Lattneree899e62005-10-09 22:12:36 +0000218 // If either of the operands has zero bits, the result will too.
219 if (MaskedValueIsZero(Op.getOperand(1), Mask, TLI) ||
220 MaskedValueIsZero(Op.getOperand(0), Mask, TLI))
221 return true;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000222 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
223 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
224 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
Chris Lattneree899e62005-10-09 22:12:36 +0000225 return false;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000226 case ISD::OR:
227 case ISD::XOR:
228 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
229 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
230 case ISD::SELECT:
231 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
232 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
233 case ISD::SELECT_CC:
234 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
235 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
236 case ISD::SRL:
237 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
238 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
239 uint64_t NewVal = Mask << ShAmt->getValue();
240 SrcBits = MVT::getSizeInBits(Op.getValueType());
241 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
242 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
243 }
244 return false;
245 case ISD::SHL:
246 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
247 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
248 uint64_t NewVal = Mask >> ShAmt->getValue();
249 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
250 }
251 return false;
Chris Lattnerbba9aa32005-10-10 16:51:40 +0000252 case ISD::ADD:
Chris Lattnerd7390752005-10-10 16:52:03 +0000253 // (add X, Y) & C == 0 iff (X&C)|(Y&C) == 0 and all bits are low bits.
Chris Lattnerbba9aa32005-10-10 16:51:40 +0000254 if ((Mask&(Mask+1)) == 0) { // All low bits
255 if (MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
256 MaskedValueIsZero(Op.getOperand(1), Mask, TLI))
257 return true;
258 }
259 break;
Chris Lattnerc4ced262005-10-07 15:30:32 +0000260 case ISD::SUB:
261 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
262 // We know that the top bits of C-X are clear if X contains less bits
263 // than C (i.e. no wrap-around can happen). For example, 20-X is
264 // positive if we can prove that X is >= 0 and < 16.
265 unsigned Bits = MVT::getSizeInBits(CLHS->getValueType(0));
266 if ((CLHS->getValue() & (1 << (Bits-1))) == 0) { // sign bit clear
267 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
268 uint64_t MaskV = (1ULL << (63-NLZ))-1;
269 if (MaskedValueIsZero(Op.getOperand(1), ~MaskV, TLI)) {
270 // High bits are clear this value is known to be >= C.
271 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
272 if ((Mask & ((1ULL << (64-NLZ2))-1)) == 0)
273 return true;
274 }
275 }
276 }
277 break;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000278 case ISD::CTTZ:
279 case ISD::CTLZ:
280 case ISD::CTPOP:
281 // Bit counting instructions can not set the high bits of the result
282 // register. The max number of bits sets depends on the input.
283 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000284 default: break;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000285 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000286 return false;
287}
288
Nate Begeman4ebd8052005-09-01 23:24:04 +0000289// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
290// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000291// Also, set the incoming LHS, RHS, and CC references to the appropriate
292// nodes based on the type of node we are checking. This simplifies life a
293// bit for the callers.
294static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
295 SDOperand &CC) {
296 if (N.getOpcode() == ISD::SETCC) {
297 LHS = N.getOperand(0);
298 RHS = N.getOperand(1);
299 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000300 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000301 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000302 if (N.getOpcode() == ISD::SELECT_CC &&
303 N.getOperand(2).getOpcode() == ISD::Constant &&
304 N.getOperand(3).getOpcode() == ISD::Constant &&
305 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000306 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
307 LHS = N.getOperand(0);
308 RHS = N.getOperand(1);
309 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000310 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000311 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000312 return false;
313}
314
Nate Begeman99801192005-09-07 23:25:52 +0000315// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
316// one use. If this is true, it allows the users to invert the operation for
317// free when it is profitable to do so.
318static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000319 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000320 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000321 return true;
322 return false;
323}
324
Nate Begeman452d7be2005-09-16 00:54:12 +0000325// FIXME: This should probably go in the ISD class rather than being duplicated
326// in several files.
327static bool isCommutativeBinOp(unsigned Opcode) {
328 switch (Opcode) {
329 case ISD::ADD:
330 case ISD::MUL:
331 case ISD::AND:
332 case ISD::OR:
333 case ISD::XOR: return true;
334 default: return false; // FIXME: Need commutative info for user ops!
335 }
336}
337
Nate Begeman4ebd8052005-09-01 23:24:04 +0000338void DAGCombiner::Run(bool RunningAfterLegalize) {
339 // set the instance variable, so that the various visit routines may use it.
340 AfterLegalize = RunningAfterLegalize;
341
Nate Begeman646d7e22005-09-02 21:18:40 +0000342 // Add all the dag nodes to the worklist.
343 WorkList.insert(WorkList.end(), DAG.allnodes_begin(), DAG.allnodes_end());
Nate Begeman83e75ec2005-09-06 04:43:02 +0000344
Chris Lattner95038592005-10-05 06:35:28 +0000345 // Create a dummy node (which is not added to allnodes), that adds a reference
346 // to the root node, preventing it from being deleted, and tracking any
347 // changes of the root.
348 HandleSDNode Dummy(DAG.getRoot());
349
Nate Begeman1d4d4142005-09-01 00:19:25 +0000350 // while the worklist isn't empty, inspect the node on the end of it and
351 // try and combine it.
352 while (!WorkList.empty()) {
353 SDNode *N = WorkList.back();
354 WorkList.pop_back();
355
356 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000357 // N is deleted from the DAG, since they too may now be dead or may have a
358 // reduced number of uses, allowing other xforms.
359 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000360 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
361 WorkList.push_back(N->getOperand(i).Val);
362
Nate Begeman1d4d4142005-09-01 00:19:25 +0000363 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000364 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000365 continue;
366 }
367
Nate Begeman83e75ec2005-09-06 04:43:02 +0000368 SDOperand RV = visit(N);
369 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000370 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000371 // If we get back the same node we passed in, rather than a new node or
372 // zero, we know that the node must have defined multiple values and
373 // CombineTo was used. Since CombineTo takes care of the worklist
374 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000375 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000376 DEBUG(std::cerr << "\nReplacing "; N->dump();
377 std::cerr << "\nWith: "; RV.Val->dump();
378 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000379 std::vector<SDNode*> NowDead;
380 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000381
382 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000383 WorkList.push_back(RV.Val);
384 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000385
386 // Nodes can end up on the worklist more than once. Make sure we do
387 // not process a node that has been replaced.
388 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000389 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
390 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000391
392 // Finally, since the node is now dead, remove it from the graph.
393 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000394 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000395 }
396 }
Chris Lattner95038592005-10-05 06:35:28 +0000397
398 // If the root changed (e.g. it was a dead load, update the root).
399 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000400}
401
Nate Begeman83e75ec2005-09-06 04:43:02 +0000402SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000403 switch(N->getOpcode()) {
404 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000405 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000406 case ISD::ADD: return visitADD(N);
407 case ISD::SUB: return visitSUB(N);
408 case ISD::MUL: return visitMUL(N);
409 case ISD::SDIV: return visitSDIV(N);
410 case ISD::UDIV: return visitUDIV(N);
411 case ISD::SREM: return visitSREM(N);
412 case ISD::UREM: return visitUREM(N);
413 case ISD::MULHU: return visitMULHU(N);
414 case ISD::MULHS: return visitMULHS(N);
415 case ISD::AND: return visitAND(N);
416 case ISD::OR: return visitOR(N);
417 case ISD::XOR: return visitXOR(N);
418 case ISD::SHL: return visitSHL(N);
419 case ISD::SRA: return visitSRA(N);
420 case ISD::SRL: return visitSRL(N);
421 case ISD::CTLZ: return visitCTLZ(N);
422 case ISD::CTTZ: return visitCTTZ(N);
423 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000424 case ISD::SELECT: return visitSELECT(N);
425 case ISD::SELECT_CC: return visitSELECT_CC(N);
426 case ISD::SETCC: return visitSETCC(N);
Nate Begeman5054f162005-10-14 01:12:21 +0000427 case ISD::ADD_PARTS: return visitADD_PARTS(N);
428 case ISD::SUB_PARTS: return visitSUB_PARTS(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000429 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
430 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
431 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
432 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000433 case ISD::FADD: return visitFADD(N);
434 case ISD::FSUB: return visitFSUB(N);
435 case ISD::FMUL: return visitFMUL(N);
436 case ISD::FDIV: return visitFDIV(N);
437 case ISD::FREM: return visitFREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000438 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
439 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
440 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
441 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
442 case ISD::FP_ROUND: return visitFP_ROUND(N);
443 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
444 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
445 case ISD::FNEG: return visitFNEG(N);
446 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000447 case ISD::BRCOND: return visitBRCOND(N);
448 case ISD::BRCONDTWOWAY: return visitBRCONDTWOWAY(N);
449 case ISD::BR_CC: return visitBR_CC(N);
450 case ISD::BRTWOWAY_CC: return visitBRTWOWAY_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000451 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000452 case ISD::STORE: return visitSTORE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000453 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000454 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000455}
456
Nate Begeman83e75ec2005-09-06 04:43:02 +0000457SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begemanded49632005-10-13 03:11:28 +0000458 std::vector<SDOperand> Ops;
459 bool Changed = false;
460
Nate Begeman1d4d4142005-09-01 00:19:25 +0000461 // If the token factor has two operands and one is the entry token, replace
462 // the token factor with the other operand.
463 if (N->getNumOperands() == 2) {
464 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000465 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000466 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000467 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000468 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000469
Nate Begemanded49632005-10-13 03:11:28 +0000470 // fold (tokenfactor (tokenfactor)) -> tokenfactor
471 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
472 SDOperand Op = N->getOperand(i);
473 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
474 Changed = true;
475 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
476 Ops.push_back(Op.getOperand(j));
477 } else {
478 Ops.push_back(Op);
479 }
480 }
481 if (Changed)
482 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000483 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000484}
485
Nate Begeman83e75ec2005-09-06 04:43:02 +0000486SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000487 SDOperand N0 = N->getOperand(0);
488 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000489 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
490 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000491 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000492
493 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000494 if (N0C && N1C)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000495 return DAG.getConstant(N0C->getValue() + N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000496 // canonicalize constant to RHS
497 if (N0C && !N1C) {
498 std::swap(N0, N1);
499 std::swap(N0C, N1C);
500 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000501 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000502 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000503 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000504 // fold (add (add x, c1), c2) -> (add x, c1+c2)
505 if (N1C && N0.getOpcode() == ISD::ADD) {
506 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
507 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
508 if (N00C)
509 return DAG.getNode(ISD::ADD, VT, N0.getOperand(1),
510 DAG.getConstant(N1C->getValue()+N00C->getValue(), VT));
511 if (N01C)
512 return DAG.getNode(ISD::ADD, VT, N0.getOperand(0),
513 DAG.getConstant(N1C->getValue()+N01C->getValue(), VT));
514 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000515 // fold ((0-A) + B) -> B-A
516 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
517 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000518 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000519 // fold (A + (0-B)) -> A-B
520 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
521 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000522 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000523 // fold (A+(B-A)) -> B
524 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000525 return N1.getOperand(0);
526 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000527}
528
Nate Begeman83e75ec2005-09-06 04:43:02 +0000529SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000530 SDOperand N0 = N->getOperand(0);
531 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000532 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
533 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000534
Chris Lattner854077d2005-10-17 01:07:11 +0000535 // fold (sub x, x) -> 0
536 if (N0 == N1)
537 return DAG.getConstant(0, N->getValueType(0));
538
Nate Begeman1d4d4142005-09-01 00:19:25 +0000539 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000540 if (N0C && N1C)
541 return DAG.getConstant(N0C->getValue() - N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000542 N->getValueType(0));
Chris Lattner05b57432005-10-11 06:07:15 +0000543 // fold (sub x, c) -> (add x, -c)
544 if (N1C)
545 return DAG.getNode(ISD::ADD, N0.getValueType(), N0,
546 DAG.getConstant(-N1C->getValue(), N0.getValueType()));
547
Nate Begeman1d4d4142005-09-01 00:19:25 +0000548 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000549 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000550 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000551 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000552 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000553 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000554 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000555}
556
Nate Begeman83e75ec2005-09-06 04:43:02 +0000557SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000558 SDOperand N0 = N->getOperand(0);
559 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000560 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
561 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000562 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000563
564 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000565 if (N0C && N1C)
566 return DAG.getConstant(N0C->getValue() * N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000567 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000568 // canonicalize constant to RHS
569 if (N0C && !N1C) {
570 std::swap(N0, N1);
571 std::swap(N0C, N1C);
572 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000573 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000574 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000575 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000576 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000577 if (N1C && N1C->isAllOnesValue())
Nate Begeman1d4d4142005-09-01 00:19:25 +0000578 return DAG.getNode(ISD::SUB, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000579 DAG.getConstant(0, N->getValueType(0)), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000580 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000581 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000582 return DAG.getNode(ISD::SHL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000583 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000584 TLI.getShiftAmountTy()));
Nate Begeman223df222005-09-08 20:18:10 +0000585 // fold (mul (mul x, c1), c2) -> (mul x, c1*c2)
586 if (N1C && N0.getOpcode() == ISD::MUL) {
587 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
588 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
589 if (N00C)
590 return DAG.getNode(ISD::MUL, VT, N0.getOperand(1),
591 DAG.getConstant(N1C->getValue()*N00C->getValue(), VT));
592 if (N01C)
593 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0),
594 DAG.getConstant(N1C->getValue()*N01C->getValue(), VT));
595 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000596 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000597}
598
Nate Begeman83e75ec2005-09-06 04:43:02 +0000599SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000600 SDOperand N0 = N->getOperand(0);
601 SDOperand N1 = N->getOperand(1);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000602 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000603 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
604 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000605
606 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000607 if (N0C && N1C && !N1C->isNullValue())
608 return DAG.getConstant(N0C->getSignExtended() / N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000609 N->getValueType(0));
Chris Lattner094c8fc2005-10-07 06:10:46 +0000610 // If we know the sign bits of both operands are zero, strength reduce to a
611 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
612 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
613 if (MaskedValueIsZero(N1, SignBit, TLI) &&
614 MaskedValueIsZero(N0, SignBit, TLI))
615 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000616 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000617}
618
Nate Begeman83e75ec2005-09-06 04:43:02 +0000619SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000620 SDOperand N0 = N->getOperand(0);
621 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000622 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
623 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000624
625 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000626 if (N0C && N1C && !N1C->isNullValue())
627 return DAG.getConstant(N0C->getValue() / N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000628 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000629 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000630 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000631 return DAG.getNode(ISD::SRL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000632 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000633 TLI.getShiftAmountTy()));
634 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000635}
636
Nate Begeman83e75ec2005-09-06 04:43:02 +0000637SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000638 SDOperand N0 = N->getOperand(0);
639 SDOperand N1 = N->getOperand(1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000640 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000641 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
642 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000643
644 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000645 if (N0C && N1C && !N1C->isNullValue())
646 return DAG.getConstant(N0C->getSignExtended() % N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000647 N->getValueType(0));
Nate Begeman07ed4172005-10-10 21:26:48 +0000648 // If we know the sign bits of both operands are zero, strength reduce to a
649 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
650 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
651 if (MaskedValueIsZero(N1, SignBit, TLI) &&
652 MaskedValueIsZero(N0, SignBit, TLI))
653 return DAG.getNode(ISD::UREM, N1.getValueType(), N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000654 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000655}
656
Nate Begeman83e75ec2005-09-06 04:43:02 +0000657SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000658 SDOperand N0 = N->getOperand(0);
659 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000660 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
661 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000662
663 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000664 if (N0C && N1C && !N1C->isNullValue())
665 return DAG.getConstant(N0C->getValue() % N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000666 N->getValueType(0));
Nate Begeman07ed4172005-10-10 21:26:48 +0000667 // fold (urem x, pow2) -> (and x, pow2-1)
668 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
669 return DAG.getNode(ISD::AND, N0.getValueType(), N0,
670 DAG.getConstant(N1C->getValue()-1, N1.getValueType()));
Nate Begeman83e75ec2005-09-06 04:43:02 +0000671 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000672}
673
Nate Begeman83e75ec2005-09-06 04:43:02 +0000674SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000675 SDOperand N0 = N->getOperand(0);
676 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000677 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000678
679 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000680 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000681 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000682 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000683 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000684 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
685 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000686 TLI.getShiftAmountTy()));
687 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000688}
689
Nate Begeman83e75ec2005-09-06 04:43:02 +0000690SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000691 SDOperand N0 = N->getOperand(0);
692 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000693 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000694
695 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000696 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000697 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000698 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000699 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000700 return DAG.getConstant(0, N0.getValueType());
701 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000702}
703
Nate Begeman83e75ec2005-09-06 04:43:02 +0000704SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000705 SDOperand N0 = N->getOperand(0);
706 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000707 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000708 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
709 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000710 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000711 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000712
713 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000714 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000715 return DAG.getConstant(N0C->getValue() & N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000716 // canonicalize constant to RHS
717 if (N0C && !N1C) {
718 std::swap(N0, N1);
719 std::swap(N0C, N1C);
720 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000721 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000722 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000723 return N0;
724 // if (and x, c) is known to be zero, return 0
725 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
726 return DAG.getConstant(0, VT);
727 // fold (and x, c) -> x iff (x & ~c) == 0
728 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
729 TLI))
730 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000731 // fold (and (and x, c1), c2) -> (and x, c1^c2)
732 if (N1C && N0.getOpcode() == ISD::AND) {
733 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
734 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
735 if (N00C)
736 return DAG.getNode(ISD::AND, VT, N0.getOperand(1),
737 DAG.getConstant(N1C->getValue()&N00C->getValue(), VT));
738 if (N01C)
739 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
740 DAG.getConstant(N1C->getValue()&N01C->getValue(), VT));
741 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000742 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
743 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG) {
744 unsigned ExtendBits =
745 MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT());
Nate Begeman646d7e22005-09-02 21:18:40 +0000746 if ((N1C->getValue() & (~0ULL << ExtendBits)) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000747 return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000748 }
749 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Chris Lattnera179ab32005-10-11 17:56:34 +0000750 if (N0.getOpcode() == ISD::OR && N1C)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000751 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000752 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000753 return N1;
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000754 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
755 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
756 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
757 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
758
759 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
760 MVT::isInteger(LL.getValueType())) {
761 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
762 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
763 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
764 WorkList.push_back(ORNode.Val);
765 return DAG.getSetCC(VT, ORNode, LR, Op1);
766 }
767 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
768 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
769 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
770 WorkList.push_back(ANDNode.Val);
771 return DAG.getSetCC(VT, ANDNode, LR, Op1);
772 }
773 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
774 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
775 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
776 WorkList.push_back(ORNode.Val);
777 return DAG.getSetCC(VT, ORNode, LR, Op1);
778 }
779 }
780 // canonicalize equivalent to ll == rl
781 if (LL == RR && LR == RL) {
782 Op1 = ISD::getSetCCSwappedOperands(Op1);
783 std::swap(RL, RR);
784 }
785 if (LL == RL && LR == RR) {
786 bool isInteger = MVT::isInteger(LL.getValueType());
787 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
788 if (Result != ISD::SETCC_INVALID)
789 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
790 }
791 }
792 // fold (and (zext x), (zext y)) -> (zext (and x, y))
793 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
794 N1.getOpcode() == ISD::ZERO_EXTEND &&
795 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
796 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
797 N0.getOperand(0), N1.getOperand(0));
798 WorkList.push_back(ANDNode.Val);
799 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
800 }
Nate Begeman452d7be2005-09-16 00:54:12 +0000801 // fold (and (shl/srl x), (shl/srl y)) -> (shl/srl (and x, y))
802 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
803 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL)) &&
804 N0.getOperand(1) == N1.getOperand(1)) {
805 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
806 N0.getOperand(0), N1.getOperand(0));
807 WorkList.push_back(ANDNode.Val);
808 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
809 }
Chris Lattner85d63bb2005-10-15 22:18:08 +0000810 // fold (and (sra)) -> (and (srl)) when possible.
811 if (N0.getOpcode() == ISD::SRA && N0.Val->hasOneUse())
812 if (ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
813 // If the RHS of the AND has zeros where the sign bits of the SRA will
814 // land, turn the SRA into an SRL.
Chris Lattner750dbd52005-10-15 22:35:40 +0000815 if (MaskedValueIsZero(N1, (~0ULL << (OpSizeInBits-N01C->getValue())) &
Chris Lattner85d63bb2005-10-15 22:18:08 +0000816 (~0ULL>>(64-OpSizeInBits)), TLI)) {
817 WorkList.push_back(N);
818 CombineTo(N0.Val, DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
819 N0.getOperand(1)));
820 return SDOperand();
821 }
822 }
823
Nate Begemanded49632005-10-13 03:11:28 +0000824 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +0000825 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +0000826 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +0000827 // If we zero all the possible extended bits, then we can turn this into
828 // a zextload if we are running before legalize or the operation is legal.
Nate Begeman5054f162005-10-14 01:12:21 +0000829 if (MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT), TLI) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +0000830 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +0000831 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
832 N0.getOperand(1), N0.getOperand(2),
833 EVT);
Nate Begemanded49632005-10-13 03:11:28 +0000834 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +0000835 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +0000836 return SDOperand();
837 }
838 }
839 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Nate Begeman5054f162005-10-14 01:12:21 +0000840 if (N0.getOpcode() == ISD::SEXTLOAD && N0.Val->hasNUsesOfValue(1, 0)) {
Nate Begemanded49632005-10-13 03:11:28 +0000841 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +0000842 // If we zero all the possible extended bits, then we can turn this into
843 // a zextload if we are running before legalize or the operation is legal.
Nate Begeman5054f162005-10-14 01:12:21 +0000844 if (MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT), TLI) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +0000845 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +0000846 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
847 N0.getOperand(1), N0.getOperand(2),
848 EVT);
Nate Begemanded49632005-10-13 03:11:28 +0000849 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +0000850 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +0000851 return SDOperand();
852 }
853 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000854 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000855}
856
Nate Begeman83e75ec2005-09-06 04:43:02 +0000857SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000858 SDOperand N0 = N->getOperand(0);
859 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000860 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000861 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
862 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000863 MVT::ValueType VT = N1.getValueType();
864 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000865
866 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000867 if (N0C && N1C)
868 return DAG.getConstant(N0C->getValue() | N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000869 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000870 // canonicalize constant to RHS
871 if (N0C && !N1C) {
872 std::swap(N0, N1);
873 std::swap(N0C, N1C);
874 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000875 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000876 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000877 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000878 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000879 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000880 return N1;
881 // fold (or x, c) -> c iff (x & ~c) == 0
882 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
883 TLI))
884 return N1;
Nate Begeman223df222005-09-08 20:18:10 +0000885 // fold (or (or x, c1), c2) -> (or x, c1|c2)
886 if (N1C && N0.getOpcode() == ISD::OR) {
887 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
888 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
889 if (N00C)
890 return DAG.getNode(ISD::OR, VT, N0.getOperand(1),
891 DAG.getConstant(N1C->getValue()|N00C->getValue(), VT));
892 if (N01C)
893 return DAG.getNode(ISD::OR, VT, N0.getOperand(0),
894 DAG.getConstant(N1C->getValue()|N01C->getValue(), VT));
895 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000896 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
897 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
898 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
899 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
900
901 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
902 MVT::isInteger(LL.getValueType())) {
903 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
904 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
905 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
906 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
907 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
908 WorkList.push_back(ORNode.Val);
909 return DAG.getSetCC(VT, ORNode, LR, Op1);
910 }
911 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
912 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
913 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
914 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
915 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
916 WorkList.push_back(ANDNode.Val);
917 return DAG.getSetCC(VT, ANDNode, LR, Op1);
918 }
919 }
920 // canonicalize equivalent to ll == rl
921 if (LL == RR && LR == RL) {
922 Op1 = ISD::getSetCCSwappedOperands(Op1);
923 std::swap(RL, RR);
924 }
925 if (LL == RL && LR == RR) {
926 bool isInteger = MVT::isInteger(LL.getValueType());
927 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
928 if (Result != ISD::SETCC_INVALID)
929 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
930 }
931 }
932 // fold (or (zext x), (zext y)) -> (zext (or x, y))
933 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
934 N1.getOpcode() == ISD::ZERO_EXTEND &&
935 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
936 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
937 N0.getOperand(0), N1.getOperand(0));
938 WorkList.push_back(ORNode.Val);
939 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
940 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000941 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000942}
943
Nate Begeman83e75ec2005-09-06 04:43:02 +0000944SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000945 SDOperand N0 = N->getOperand(0);
946 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000947 SDOperand LHS, RHS, CC;
948 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
949 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000950 MVT::ValueType VT = N0.getValueType();
951
952 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000953 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000954 return DAG.getConstant(N0C->getValue() ^ N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000955 // canonicalize constant to RHS
956 if (N0C && !N1C) {
957 std::swap(N0, N1);
958 std::swap(N0C, N1C);
959 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000960 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000961 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000962 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000963 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +0000964 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
965 bool isInt = MVT::isInteger(LHS.getValueType());
966 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
967 isInt);
968 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000969 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000970 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000971 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000972 assert(0 && "Unhandled SetCC Equivalent!");
973 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000974 }
Nate Begeman99801192005-09-07 23:25:52 +0000975 // fold !(x or y) -> (!x and !y) iff x or y are setcc
976 if (N1C && N1C->getValue() == 1 &&
977 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000978 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000979 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
980 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000981 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
982 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000983 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
984 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000985 }
986 }
Nate Begeman99801192005-09-07 23:25:52 +0000987 // fold !(x or y) -> (!x and !y) iff x or y are constants
988 if (N1C && N1C->isAllOnesValue() &&
989 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000990 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000991 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
992 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000993 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
994 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000995 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
996 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000997 }
998 }
Nate Begeman223df222005-09-08 20:18:10 +0000999 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1000 if (N1C && N0.getOpcode() == ISD::XOR) {
1001 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1002 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1003 if (N00C)
1004 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1005 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1006 if (N01C)
1007 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1008 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1009 }
1010 // fold (xor x, x) -> 0
1011 if (N0 == N1)
1012 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001013 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
1014 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1015 N1.getOpcode() == ISD::ZERO_EXTEND &&
1016 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1017 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1018 N0.getOperand(0), N1.getOperand(0));
1019 WorkList.push_back(XORNode.Val);
1020 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
1021 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001022 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001023}
1024
Nate Begeman83e75ec2005-09-06 04:43:02 +00001025SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001026 SDOperand N0 = N->getOperand(0);
1027 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001028 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1029 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001030 MVT::ValueType VT = N0.getValueType();
1031 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1032
1033 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001034 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001035 return DAG.getConstant(N0C->getValue() << N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001036 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001037 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001038 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001039 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001040 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001041 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001042 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001043 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001044 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001045 // if (shl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +00001046 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
1047 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001048 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001049 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001050 N0.getOperand(1).getOpcode() == ISD::Constant) {
1051 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001052 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001053 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001054 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001055 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001056 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001057 }
1058 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1059 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001060 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001061 N0.getOperand(1).getOpcode() == ISD::Constant) {
1062 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001063 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001064 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1065 DAG.getConstant(~0ULL << c1, VT));
1066 if (c2 > c1)
1067 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001068 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001069 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001070 return DAG.getNode(ISD::SRL, VT, Mask,
1071 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001072 }
1073 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001074 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001075 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001076 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1077 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001078}
1079
Nate Begeman83e75ec2005-09-06 04:43:02 +00001080SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001081 SDOperand N0 = N->getOperand(0);
1082 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001083 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1084 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001085 MVT::ValueType VT = N0.getValueType();
1086 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1087
1088 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001089 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001090 return DAG.getConstant(N0C->getSignExtended() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001091 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001092 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001093 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001094 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001095 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001096 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001097 // fold (sra x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001098 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001099 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001100 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001101 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001102 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001103 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begeman3df4d522005-10-12 20:40:40 +00001104 if (MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1)), TLI))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001105 return DAG.getNode(ISD::SRL, VT, N0, N1);
1106 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001107}
1108
Nate Begeman83e75ec2005-09-06 04:43:02 +00001109SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001110 SDOperand N0 = N->getOperand(0);
1111 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001112 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1113 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001114 MVT::ValueType VT = N0.getValueType();
1115 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1116
1117 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001118 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001119 return DAG.getConstant(N0C->getValue() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001120 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001121 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001122 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001123 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001124 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001125 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001126 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001127 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001128 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001129 // if (srl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +00001130 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
1131 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001132 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001133 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001134 N0.getOperand(1).getOpcode() == ISD::Constant) {
1135 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001136 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001137 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001138 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001139 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001140 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001141 }
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::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001146 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001147 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001148
1149 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001150 if (N0C)
1151 return DAG.getConstant(CountLeadingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001152 N0.getValueType());
1153 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001154}
1155
Nate Begeman83e75ec2005-09-06 04:43:02 +00001156SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001157 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001158 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001159
1160 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001161 if (N0C)
1162 return DAG.getConstant(CountTrailingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001163 N0.getValueType());
1164 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001165}
1166
Nate Begeman83e75ec2005-09-06 04:43:02 +00001167SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001168 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001169 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001170
1171 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001172 if (N0C)
1173 return DAG.getConstant(CountPopulation_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001174 N0.getValueType());
1175 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001176}
1177
Nate Begeman452d7be2005-09-16 00:54:12 +00001178SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1179 SDOperand N0 = N->getOperand(0);
1180 SDOperand N1 = N->getOperand(1);
1181 SDOperand N2 = N->getOperand(2);
1182 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1183 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1184 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1185 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001186
Nate Begeman452d7be2005-09-16 00:54:12 +00001187 // fold select C, X, X -> X
1188 if (N1 == N2)
1189 return N1;
1190 // fold select true, X, Y -> X
1191 if (N0C && !N0C->isNullValue())
1192 return N1;
1193 // fold select false, X, Y -> Y
1194 if (N0C && N0C->isNullValue())
1195 return N2;
1196 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001197 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001198 return DAG.getNode(ISD::OR, VT, N0, N2);
1199 // fold select C, 0, X -> ~C & X
1200 // FIXME: this should check for C type == X type, not i1?
1201 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1202 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1203 WorkList.push_back(XORNode.Val);
1204 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1205 }
1206 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001207 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001208 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1209 WorkList.push_back(XORNode.Val);
1210 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1211 }
1212 // fold select C, X, 0 -> C & X
1213 // FIXME: this should check for C type == X type, not i1?
1214 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1215 return DAG.getNode(ISD::AND, VT, N0, N1);
1216 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1217 if (MVT::i1 == VT && N0 == N1)
1218 return DAG.getNode(ISD::OR, VT, N0, N2);
1219 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1220 if (MVT::i1 == VT && N0 == N2)
1221 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman44728a72005-09-19 22:34:01 +00001222 // fold selects based on a setcc into other things, such as min/max/abs
1223 if (N0.getOpcode() == ISD::SETCC)
1224 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001225 return SDOperand();
1226}
1227
1228SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001229 SDOperand N0 = N->getOperand(0);
1230 SDOperand N1 = N->getOperand(1);
1231 SDOperand N2 = N->getOperand(2);
1232 SDOperand N3 = N->getOperand(3);
1233 SDOperand N4 = N->getOperand(4);
1234 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1235 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1236 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1237 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1238
1239 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001240 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001241 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1242
Nate Begeman44728a72005-09-19 22:34:01 +00001243 // fold select_cc lhs, rhs, x, x, cc -> x
1244 if (N2 == N3)
1245 return N2;
Nate Begeman44728a72005-09-19 22:34:01 +00001246 // fold select_cc into other things, such as min/max/abs
1247 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001248}
1249
1250SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1251 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1252 cast<CondCodeSDNode>(N->getOperand(2))->get());
1253}
1254
Nate Begeman5054f162005-10-14 01:12:21 +00001255SDOperand DAGCombiner::visitADD_PARTS(SDNode *N) {
1256 SDOperand LHSLo = N->getOperand(0);
1257 SDOperand RHSLo = N->getOperand(2);
1258 MVT::ValueType VT = LHSLo.getValueType();
1259
1260 // fold (a_Hi, 0) + (b_Hi, b_Lo) -> (b_Hi + a_Hi, b_Lo)
1261 if (MaskedValueIsZero(LHSLo, (1ULL << MVT::getSizeInBits(VT))-1, TLI)) {
1262 SDOperand Hi = DAG.getNode(ISD::ADD, VT, N->getOperand(1),
1263 N->getOperand(3));
1264 WorkList.push_back(Hi.Val);
1265 CombineTo(N, RHSLo, Hi);
1266 return SDOperand();
1267 }
1268 // fold (a_Hi, a_Lo) + (b_Hi, 0) -> (a_Hi + b_Hi, a_Lo)
1269 if (MaskedValueIsZero(RHSLo, (1ULL << MVT::getSizeInBits(VT))-1, TLI)) {
1270 SDOperand Hi = DAG.getNode(ISD::ADD, VT, N->getOperand(1),
1271 N->getOperand(3));
1272 WorkList.push_back(Hi.Val);
1273 CombineTo(N, LHSLo, Hi);
1274 return SDOperand();
1275 }
1276 return SDOperand();
1277}
1278
1279SDOperand DAGCombiner::visitSUB_PARTS(SDNode *N) {
1280 SDOperand LHSLo = N->getOperand(0);
1281 SDOperand RHSLo = N->getOperand(2);
1282 MVT::ValueType VT = LHSLo.getValueType();
1283
1284 // fold (a_Hi, a_Lo) - (b_Hi, 0) -> (a_Hi - b_Hi, a_Lo)
1285 if (MaskedValueIsZero(RHSLo, (1ULL << MVT::getSizeInBits(VT))-1, TLI)) {
1286 SDOperand Hi = DAG.getNode(ISD::SUB, VT, N->getOperand(1),
1287 N->getOperand(3));
1288 WorkList.push_back(Hi.Val);
1289 CombineTo(N, LHSLo, Hi);
1290 return SDOperand();
1291 }
1292 return SDOperand();
1293}
1294
Nate Begeman83e75ec2005-09-06 04:43:02 +00001295SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001296 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001297 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001298 MVT::ValueType VT = N->getValueType(0);
1299
Nate Begeman1d4d4142005-09-01 00:19:25 +00001300 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001301 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001302 return DAG.getConstant(N0C->getSignExtended(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001303 // fold (sext (sext x)) -> (sext x)
1304 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001305 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Nate Begeman765784a2005-10-12 23:18:53 +00001306 // fold (sext (sextload x)) -> (sextload x)
1307 if (N0.getOpcode() == ISD::SEXTLOAD && VT == N0.getValueType())
1308 return N0;
Nate Begeman3df4d522005-10-12 20:40:40 +00001309 // fold (sext (load x)) -> (sextload x)
1310 if (N0.getOpcode() == ISD::LOAD && N0.Val->hasNUsesOfValue(1, 0)) {
1311 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1312 N0.getOperand(1), N0.getOperand(2),
1313 N0.getValueType());
Nate Begeman765784a2005-10-12 23:18:53 +00001314 WorkList.push_back(N);
Chris Lattnerf9884052005-10-13 21:52:31 +00001315 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1316 ExtLoad.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001317 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001318 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001319 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001320}
1321
Nate Begeman83e75ec2005-09-06 04:43:02 +00001322SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001323 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001324 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001325 MVT::ValueType VT = N->getValueType(0);
1326
Nate Begeman1d4d4142005-09-01 00:19:25 +00001327 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001328 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001329 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001330 // fold (zext (zext x)) -> (zext x)
1331 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001332 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
1333 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001334}
1335
Nate Begeman83e75ec2005-09-06 04:43:02 +00001336SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001337 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001338 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001339 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001340 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001341 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001342 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001343
Nate Begeman1d4d4142005-09-01 00:19:25 +00001344 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001345 if (N0C) {
1346 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001347 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001348 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001349 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001350 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman216def82005-10-14 01:29:07 +00001351 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001352 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001353 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001354 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1355 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1356 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001357 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001358 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001359 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1360 if (N0.getOpcode() == ISD::AssertSext &&
1361 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001362 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001363 }
1364 // fold (sext_in_reg (sextload x)) -> (sextload x)
1365 if (N0.getOpcode() == ISD::SEXTLOAD &&
1366 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001367 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001368 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001369 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001370 if (N0.getOpcode() == ISD::SETCC &&
1371 TLI.getSetCCResultContents() ==
1372 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001373 return N0;
Nate Begeman07ed4172005-10-10 21:26:48 +00001374 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
1375 if (MaskedValueIsZero(N0, 1ULL << (EVTBits-1), TLI))
1376 return DAG.getNode(ISD::AND, N0.getValueType(), N0,
1377 DAG.getConstant(~0ULL >> (64-EVTBits), VT));
1378 // fold (sext_in_reg (srl x)) -> sra x
1379 if (N0.getOpcode() == ISD::SRL &&
1380 N0.getOperand(1).getOpcode() == ISD::Constant &&
1381 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) {
1382 return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0),
1383 N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001384 }
Nate Begemanded49632005-10-13 03:11:28 +00001385 // fold (sext_inreg (extload x)) -> (sextload x)
1386 if (N0.getOpcode() == ISD::EXTLOAD &&
1387 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001388 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001389 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1390 N0.getOperand(1), N0.getOperand(2),
1391 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001392 WorkList.push_back(N);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001393 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001394 return SDOperand();
1395 }
1396 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
1397 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.Val->hasNUsesOfValue(1, 0) &&
1398 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001399 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001400 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1401 N0.getOperand(1), N0.getOperand(2),
1402 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001403 WorkList.push_back(N);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001404 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001405 return SDOperand();
1406 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001407 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001408}
1409
Nate Begeman83e75ec2005-09-06 04:43:02 +00001410SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001411 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001412 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001413 MVT::ValueType VT = N->getValueType(0);
1414
1415 // noop truncate
1416 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001417 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001418 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001419 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001420 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001421 // fold (truncate (truncate x)) -> (truncate x)
1422 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001423 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001424 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1425 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1426 if (N0.getValueType() < VT)
1427 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001428 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001429 else if (N0.getValueType() > VT)
1430 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001431 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001432 else
1433 // if the source and dest are the same type, we can drop both the extend
1434 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001435 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001436 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001437 // fold (truncate (load x)) -> (smaller load x)
1438 if (N0.getOpcode() == ISD::LOAD && N0.Val->hasNUsesOfValue(1, 0)) {
1439 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1440 "Cannot truncate to larger type!");
1441 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001442 // For big endian targets, we need to add an offset to the pointer to load
1443 // the correct bytes. For little endian systems, we merely need to read
1444 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001445 uint64_t PtrOff =
1446 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001447 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1448 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1449 DAG.getConstant(PtrOff, PtrType));
1450 WorkList.push_back(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001451 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Nate Begeman765784a2005-10-12 23:18:53 +00001452 WorkList.push_back(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001453 CombineTo(N0.Val, Load, Load.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001454 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001455 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001456 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001457}
1458
Chris Lattner01b3d732005-09-28 22:28:18 +00001459SDOperand DAGCombiner::visitFADD(SDNode *N) {
1460 SDOperand N0 = N->getOperand(0);
1461 SDOperand N1 = N->getOperand(1);
1462 MVT::ValueType VT = N->getValueType(0);
1463
1464 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1465 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1466 // fold floating point (fadd c1, c2)
1467 return DAG.getConstantFP(N0CFP->getValue() + N1CFP->getValue(),
1468 N->getValueType(0));
1469 }
1470 // fold (A + (-B)) -> A-B
1471 if (N1.getOpcode() == ISD::FNEG)
1472 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
1473
1474 // fold ((-A) + B) -> B-A
1475 if (N0.getOpcode() == ISD::FNEG)
1476 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
1477
1478 return SDOperand();
1479}
1480
1481SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1482 SDOperand N0 = N->getOperand(0);
1483 SDOperand N1 = N->getOperand(1);
1484 MVT::ValueType VT = N->getValueType(0);
1485
1486 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1487 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1488 // fold floating point (fsub c1, c2)
1489 return DAG.getConstantFP(N0CFP->getValue() - N1CFP->getValue(),
1490 N->getValueType(0));
1491 }
1492 // fold (A-(-B)) -> A+B
1493 if (N1.getOpcode() == ISD::FNEG)
1494 return DAG.getNode(ISD::FADD, N0.getValueType(), N0, N1.getOperand(0));
1495
1496 return SDOperand();
1497}
1498
1499SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1500 SDOperand N0 = N->getOperand(0);
1501 SDOperand N1 = N->getOperand(1);
1502 MVT::ValueType VT = N->getValueType(0);
1503
1504 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1505 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1506 // fold floating point (fmul c1, c2)
1507 return DAG.getConstantFP(N0CFP->getValue() * N1CFP->getValue(),
1508 N->getValueType(0));
1509 }
1510 return SDOperand();
1511}
1512
1513SDOperand DAGCombiner::visitFDIV(SDNode *N) {
1514 SDOperand N0 = N->getOperand(0);
1515 SDOperand N1 = N->getOperand(1);
1516 MVT::ValueType VT = N->getValueType(0);
1517
1518 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1519 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1520 // fold floating point (fdiv c1, c2)
1521 return DAG.getConstantFP(N0CFP->getValue() / N1CFP->getValue(),
1522 N->getValueType(0));
1523 }
1524 return SDOperand();
1525}
1526
1527SDOperand DAGCombiner::visitFREM(SDNode *N) {
1528 SDOperand N0 = N->getOperand(0);
1529 SDOperand N1 = N->getOperand(1);
1530 MVT::ValueType VT = N->getValueType(0);
1531
1532 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1533 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1534 // fold floating point (frem c1, c2) -> fmod(c1, c2)
1535 return DAG.getConstantFP(fmod(N0CFP->getValue(),N1CFP->getValue()),
1536 N->getValueType(0));
1537 }
1538 return SDOperand();
1539}
1540
1541
Nate Begeman83e75ec2005-09-06 04:43:02 +00001542SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001543 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001544 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001545
1546 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001547 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001548 return DAG.getConstantFP(N0C->getSignExtended(), N->getValueType(0));
1549 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001550}
1551
Nate Begeman83e75ec2005-09-06 04:43:02 +00001552SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001553 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001554 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001555
1556 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001557 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001558 return DAG.getConstantFP(N0C->getValue(), N->getValueType(0));
1559 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001560}
1561
Nate Begeman83e75ec2005-09-06 04:43:02 +00001562SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001563 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001564
1565 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001566 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001567 return DAG.getConstant((int64_t)N0CFP->getValue(), N->getValueType(0));
1568 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001569}
1570
Nate Begeman83e75ec2005-09-06 04:43:02 +00001571SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001572 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001573
1574 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001575 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001576 return DAG.getConstant((uint64_t)N0CFP->getValue(), N->getValueType(0));
1577 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001578}
1579
Nate Begeman83e75ec2005-09-06 04:43:02 +00001580SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001581 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001582
1583 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001584 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001585 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1586 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001587}
1588
Nate Begeman83e75ec2005-09-06 04:43:02 +00001589SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001590 SDOperand N0 = N->getOperand(0);
1591 MVT::ValueType VT = N->getValueType(0);
1592 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00001593 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001594
Nate Begeman1d4d4142005-09-01 00:19:25 +00001595 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001596 if (N0CFP) {
1597 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001598 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001599 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001600 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001601}
1602
Nate Begeman83e75ec2005-09-06 04:43:02 +00001603SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001604 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001605
1606 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001607 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001608 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1609 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001610}
1611
Nate Begeman83e75ec2005-09-06 04:43:02 +00001612SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001613 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001614 // fold (neg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001615 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001616 return DAG.getConstantFP(-N0CFP->getValue(), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001617 // fold (neg (sub x, y)) -> (sub y, x)
1618 if (N->getOperand(0).getOpcode() == ISD::SUB)
1619 return DAG.getNode(ISD::SUB, N->getValueType(0), N->getOperand(1),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001620 N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001621 // fold (neg (neg x)) -> x
1622 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001623 return N->getOperand(0).getOperand(0);
1624 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001625}
1626
Nate Begeman83e75ec2005-09-06 04:43:02 +00001627SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001628 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001629 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001630 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001631 return DAG.getConstantFP(fabs(N0CFP->getValue()), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001632 // fold (fabs (fabs x)) -> (fabs x)
1633 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001634 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001635 // fold (fabs (fneg x)) -> (fabs x)
1636 if (N->getOperand(0).getOpcode() == ISD::FNEG)
1637 return DAG.getNode(ISD::FABS, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001638 N->getOperand(0).getOperand(0));
1639 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001640}
1641
Nate Begeman44728a72005-09-19 22:34:01 +00001642SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
1643 SDOperand Chain = N->getOperand(0);
1644 SDOperand N1 = N->getOperand(1);
1645 SDOperand N2 = N->getOperand(2);
1646 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1647
1648 // never taken branch, fold to chain
1649 if (N1C && N1C->isNullValue())
1650 return Chain;
1651 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00001652 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00001653 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1654 return SDOperand();
1655}
1656
1657SDOperand DAGCombiner::visitBRCONDTWOWAY(SDNode *N) {
1658 SDOperand Chain = N->getOperand(0);
1659 SDOperand N1 = N->getOperand(1);
1660 SDOperand N2 = N->getOperand(2);
1661 SDOperand N3 = N->getOperand(3);
1662 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1663
1664 // unconditional branch to true mbb
1665 if (N1C && N1C->getValue() == 1)
1666 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1667 // unconditional branch to false mbb
1668 if (N1C && N1C->isNullValue())
1669 return DAG.getNode(ISD::BR, MVT::Other, Chain, N3);
1670 return SDOperand();
1671}
1672
Chris Lattner3ea0b472005-10-05 06:47:48 +00001673// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
1674//
Nate Begeman44728a72005-09-19 22:34:01 +00001675SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00001676 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
1677 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
1678
1679 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00001680 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
1681 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
1682
1683 // fold br_cc true, dest -> br dest (unconditional branch)
1684 if (SCCC && SCCC->getValue())
1685 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
1686 N->getOperand(4));
1687 // fold br_cc false, dest -> unconditional fall through
1688 if (SCCC && SCCC->isNullValue())
1689 return N->getOperand(0);
1690 // fold to a simpler setcc
1691 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
1692 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
1693 Simp.getOperand(2), Simp.getOperand(0),
1694 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00001695 return SDOperand();
1696}
1697
1698SDOperand DAGCombiner::visitBRTWOWAY_CC(SDNode *N) {
Nate Begemane17daeb2005-10-05 21:43:42 +00001699 SDOperand Chain = N->getOperand(0);
1700 SDOperand CCN = N->getOperand(1);
1701 SDOperand LHS = N->getOperand(2);
1702 SDOperand RHS = N->getOperand(3);
1703 SDOperand N4 = N->getOperand(4);
1704 SDOperand N5 = N->getOperand(5);
1705
1706 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), LHS, RHS,
1707 cast<CondCodeSDNode>(CCN)->get(), false);
1708 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1709
1710 // fold select_cc lhs, rhs, x, x, cc -> x
1711 if (N4 == N5)
1712 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
1713 // fold select_cc true, x, y -> x
1714 if (SCCC && SCCC->getValue())
1715 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
1716 // fold select_cc false, x, y -> y
1717 if (SCCC && SCCC->isNullValue())
1718 return DAG.getNode(ISD::BR, MVT::Other, Chain, N5);
1719 // fold to a simpler setcc
1720 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
1721 return DAG.getBR2Way_CC(Chain, SCC.getOperand(2), SCC.getOperand(0),
1722 SCC.getOperand(1), N4, N5);
Nate Begeman44728a72005-09-19 22:34:01 +00001723 return SDOperand();
1724}
1725
Chris Lattner01a22022005-10-10 22:04:48 +00001726SDOperand DAGCombiner::visitLOAD(SDNode *N) {
1727 SDOperand Chain = N->getOperand(0);
1728 SDOperand Ptr = N->getOperand(1);
1729 SDOperand SrcValue = N->getOperand(2);
1730
1731 // If this load is directly stored, replace the load value with the stored
1732 // value.
1733 // TODO: Handle store large -> read small portion.
1734 // TODO: Handle TRUNCSTORE/EXTLOAD
1735 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
1736 Chain.getOperand(1).getValueType() == N->getValueType(0))
1737 return CombineTo(N, Chain.getOperand(1), Chain);
1738
1739 return SDOperand();
1740}
1741
Chris Lattner87514ca2005-10-10 22:31:19 +00001742SDOperand DAGCombiner::visitSTORE(SDNode *N) {
1743 SDOperand Chain = N->getOperand(0);
1744 SDOperand Value = N->getOperand(1);
1745 SDOperand Ptr = N->getOperand(2);
1746 SDOperand SrcValue = N->getOperand(3);
1747
1748 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00001749 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
1750 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */) {
Chris Lattner87514ca2005-10-10 22:31:19 +00001751 // Create a new store of Value that replaces both stores.
1752 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00001753 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
1754 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00001755 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
1756 PrevStore->getOperand(0), Value, Ptr,
1757 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00001758 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00001759 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00001760 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00001761 }
1762
1763 return SDOperand();
1764}
1765
Nate Begeman44728a72005-09-19 22:34:01 +00001766SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00001767 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
1768
1769 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
1770 cast<CondCodeSDNode>(N0.getOperand(2))->get());
1771 // If we got a simplified select_cc node back from SimplifySelectCC, then
1772 // break it down into a new SETCC node, and a new SELECT node, and then return
1773 // the SELECT node, since we were called with a SELECT node.
1774 if (SCC.Val) {
1775 // Check to see if we got a select_cc back (to turn into setcc/select).
1776 // Otherwise, just return whatever node we got back, like fabs.
1777 if (SCC.getOpcode() == ISD::SELECT_CC) {
1778 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
1779 SCC.getOperand(0), SCC.getOperand(1),
1780 SCC.getOperand(4));
1781 WorkList.push_back(SETCC.Val);
1782 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
1783 SCC.getOperand(3), SETCC);
1784 }
1785 return SCC;
1786 }
Nate Begeman44728a72005-09-19 22:34:01 +00001787 return SDOperand();
1788}
1789
1790SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
1791 SDOperand N2, SDOperand N3,
1792 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00001793
1794 MVT::ValueType VT = N2.getValueType();
1795 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1796 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1797 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1798 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1799
1800 // Determine if the condition we're dealing with is constant
1801 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
1802 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1803
1804 // fold select_cc true, x, y -> x
1805 if (SCCC && SCCC->getValue())
1806 return N2;
1807 // fold select_cc false, x, y -> y
1808 if (SCCC && SCCC->getValue() == 0)
1809 return N3;
1810
1811 // Check to see if we can simplify the select into an fabs node
1812 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1813 // Allow either -0.0 or 0.0
1814 if (CFP->getValue() == 0.0) {
1815 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
1816 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
1817 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
1818 N2 == N3.getOperand(0))
1819 return DAG.getNode(ISD::FABS, VT, N0);
1820
1821 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
1822 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
1823 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
1824 N2.getOperand(0) == N3)
1825 return DAG.getNode(ISD::FABS, VT, N3);
1826 }
1827 }
1828
1829 // Check to see if we can perform the "gzip trick", transforming
1830 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
1831 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
1832 MVT::isInteger(N0.getValueType()) &&
1833 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
1834 MVT::ValueType XType = N0.getValueType();
1835 MVT::ValueType AType = N2.getValueType();
1836 if (XType >= AType) {
1837 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00001838 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00001839 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
1840 unsigned ShCtV = Log2_64(N2C->getValue());
1841 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
1842 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
1843 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
1844 WorkList.push_back(Shift.Val);
1845 if (XType > AType) {
1846 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
1847 WorkList.push_back(Shift.Val);
1848 }
1849 return DAG.getNode(ISD::AND, AType, Shift, N2);
1850 }
1851 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
1852 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1853 TLI.getShiftAmountTy()));
1854 WorkList.push_back(Shift.Val);
1855 if (XType > AType) {
1856 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
1857 WorkList.push_back(Shift.Val);
1858 }
1859 return DAG.getNode(ISD::AND, AType, Shift, N2);
1860 }
1861 }
Nate Begeman07ed4172005-10-10 21:26:48 +00001862
1863 // fold select C, 16, 0 -> shl C, 4
1864 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
1865 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
1866 // Get a SetCC of the condition
1867 // FIXME: Should probably make sure that setcc is legal if we ever have a
1868 // target where it isn't.
1869 SDOperand Temp, SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
1870 WorkList.push_back(SCC.Val);
1871 // cast from setcc result type to select result type
1872 if (AfterLegalize)
1873 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
1874 else
1875 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
1876 WorkList.push_back(Temp.Val);
1877 // shl setcc result by log2 n2c
1878 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
1879 DAG.getConstant(Log2_64(N2C->getValue()),
1880 TLI.getShiftAmountTy()));
1881 }
1882
Nate Begemanf845b452005-10-08 00:29:44 +00001883 // Check to see if this is the equivalent of setcc
1884 // FIXME: Turn all of these into setcc if setcc if setcc is legal
1885 // otherwise, go ahead with the folds.
1886 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
1887 MVT::ValueType XType = N0.getValueType();
1888 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
1889 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
1890 if (Res.getValueType() != VT)
1891 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
1892 return Res;
1893 }
1894
1895 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
1896 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
1897 TLI.isOperationLegal(ISD::CTLZ, XType)) {
1898 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
1899 return DAG.getNode(ISD::SRL, XType, Ctlz,
1900 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
1901 TLI.getShiftAmountTy()));
1902 }
1903 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
1904 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
1905 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
1906 N0);
1907 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
1908 DAG.getConstant(~0ULL, XType));
1909 return DAG.getNode(ISD::SRL, XType,
1910 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
1911 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1912 TLI.getShiftAmountTy()));
1913 }
1914 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
1915 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
1916 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
1917 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1918 TLI.getShiftAmountTy()));
1919 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
1920 }
1921 }
1922
1923 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
1924 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
1925 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
1926 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
1927 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
1928 MVT::ValueType XType = N0.getValueType();
1929 if (SubC->isNullValue() && MVT::isInteger(XType)) {
1930 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
1931 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1932 TLI.getShiftAmountTy()));
1933 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
1934 WorkList.push_back(Shift.Val);
1935 WorkList.push_back(Add.Val);
1936 return DAG.getNode(ISD::XOR, XType, Add, Shift);
1937 }
1938 }
1939 }
1940
Nate Begeman44728a72005-09-19 22:34:01 +00001941 return SDOperand();
1942}
1943
Nate Begeman452d7be2005-09-16 00:54:12 +00001944SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00001945 SDOperand N1, ISD::CondCode Cond,
1946 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001947 // These setcc operations always fold.
1948 switch (Cond) {
1949 default: break;
1950 case ISD::SETFALSE:
1951 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1952 case ISD::SETTRUE:
1953 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
1954 }
1955
1956 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1957 uint64_t C1 = N1C->getValue();
1958 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
1959 uint64_t C0 = N0C->getValue();
1960
1961 // Sign extend the operands if required
1962 if (ISD::isSignedIntSetCC(Cond)) {
1963 C0 = N0C->getSignExtended();
1964 C1 = N1C->getSignExtended();
1965 }
1966
1967 switch (Cond) {
1968 default: assert(0 && "Unknown integer setcc!");
1969 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
1970 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
1971 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
1972 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
1973 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
1974 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
1975 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
1976 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
1977 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
1978 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
1979 }
1980 } else {
1981 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
1982 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
1983 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
1984
1985 // If the comparison constant has bits in the upper part, the
1986 // zero-extended value could never match.
1987 if (C1 & (~0ULL << InSize)) {
1988 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
1989 switch (Cond) {
1990 case ISD::SETUGT:
1991 case ISD::SETUGE:
1992 case ISD::SETEQ: return DAG.getConstant(0, VT);
1993 case ISD::SETULT:
1994 case ISD::SETULE:
1995 case ISD::SETNE: return DAG.getConstant(1, VT);
1996 case ISD::SETGT:
1997 case ISD::SETGE:
1998 // True if the sign bit of C1 is set.
1999 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
2000 case ISD::SETLT:
2001 case ISD::SETLE:
2002 // True if the sign bit of C1 isn't set.
2003 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
2004 default:
2005 break;
2006 }
2007 }
2008
2009 // Otherwise, we can perform the comparison with the low bits.
2010 switch (Cond) {
2011 case ISD::SETEQ:
2012 case ISD::SETNE:
2013 case ISD::SETUGT:
2014 case ISD::SETUGE:
2015 case ISD::SETULT:
2016 case ISD::SETULE:
2017 return DAG.getSetCC(VT, N0.getOperand(0),
2018 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
2019 Cond);
2020 default:
2021 break; // todo, be more careful with signed comparisons
2022 }
2023 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2024 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2025 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
2026 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
2027 MVT::ValueType ExtDstTy = N0.getValueType();
2028 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
2029
2030 // If the extended part has any inconsistent bits, it cannot ever
2031 // compare equal. In other words, they have to be all ones or all
2032 // zeros.
2033 uint64_t ExtBits =
2034 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
2035 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
2036 return DAG.getConstant(Cond == ISD::SETNE, VT);
2037
2038 SDOperand ZextOp;
2039 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
2040 if (Op0Ty == ExtSrcTy) {
2041 ZextOp = N0.getOperand(0);
2042 } else {
2043 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
2044 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
2045 DAG.getConstant(Imm, Op0Ty));
2046 }
2047 WorkList.push_back(ZextOp.Val);
2048 // Otherwise, make this a use of a zext.
2049 return DAG.getSetCC(VT, ZextOp,
2050 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
2051 ExtDstTy),
2052 Cond);
2053 }
Chris Lattner5c46f742005-10-05 06:11:08 +00002054
Nate Begeman452d7be2005-09-16 00:54:12 +00002055 uint64_t MinVal, MaxVal;
2056 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
2057 if (ISD::isSignedIntSetCC(Cond)) {
2058 MinVal = 1ULL << (OperandBitSize-1);
2059 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
2060 MaxVal = ~0ULL >> (65-OperandBitSize);
2061 else
2062 MaxVal = 0;
2063 } else {
2064 MinVal = 0;
2065 MaxVal = ~0ULL >> (64-OperandBitSize);
2066 }
2067
2068 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
2069 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
2070 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
2071 --C1; // X >= C0 --> X > (C0-1)
2072 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2073 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
2074 }
2075
2076 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
2077 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
2078 ++C1; // X <= C0 --> X < (C0+1)
2079 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2080 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
2081 }
2082
2083 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
2084 return DAG.getConstant(0, VT); // X < MIN --> false
2085
2086 // Canonicalize setgt X, Min --> setne X, Min
2087 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
2088 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
2089
2090 // If we have setult X, 1, turn it into seteq X, 0
2091 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
2092 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
2093 ISD::SETEQ);
2094 // If we have setugt X, Max-1, turn it into seteq X, Max
2095 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
2096 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
2097 ISD::SETEQ);
2098
2099 // If we have "setcc X, C0", check to see if we can shrink the immediate
2100 // by changing cc.
2101
2102 // SETUGT X, SINTMAX -> SETLT X, 0
2103 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
2104 C1 == (~0ULL >> (65-OperandBitSize)))
2105 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
2106 ISD::SETLT);
2107
2108 // FIXME: Implement the rest of these.
2109
2110 // Fold bit comparisons when we can.
2111 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2112 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
2113 if (ConstantSDNode *AndRHS =
2114 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2115 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
2116 // Perform the xform if the AND RHS is a single bit.
2117 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
2118 return DAG.getNode(ISD::SRL, VT, N0,
2119 DAG.getConstant(Log2_64(AndRHS->getValue()),
2120 TLI.getShiftAmountTy()));
2121 }
2122 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
2123 // (X & 8) == 8 --> (X & 8) >> 3
2124 // Perform the xform if C1 is a single bit.
2125 if ((C1 & (C1-1)) == 0) {
2126 return DAG.getNode(ISD::SRL, VT, N0,
2127 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
2128 }
2129 }
2130 }
2131 }
2132 } else if (isa<ConstantSDNode>(N0.Val)) {
2133 // Ensure that the constant occurs on the RHS.
2134 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2135 }
2136
2137 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
2138 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
2139 double C0 = N0C->getValue(), C1 = N1C->getValue();
2140
2141 switch (Cond) {
2142 default: break; // FIXME: Implement the rest of these!
2143 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2144 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2145 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
2146 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
2147 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
2148 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
2149 }
2150 } else {
2151 // Ensure that the constant occurs on the RHS.
2152 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2153 }
2154
2155 if (N0 == N1) {
2156 // We can always fold X == Y for integer setcc's.
2157 if (MVT::isInteger(N0.getValueType()))
2158 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2159 unsigned UOF = ISD::getUnorderedFlavor(Cond);
2160 if (UOF == 2) // FP operators that are undefined on NaNs.
2161 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2162 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2163 return DAG.getConstant(UOF, VT);
2164 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
2165 // if it is not already.
2166 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
2167 if (NewCond != Cond)
2168 return DAG.getSetCC(VT, N0, N1, NewCond);
2169 }
2170
2171 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2172 MVT::isInteger(N0.getValueType())) {
2173 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2174 N0.getOpcode() == ISD::XOR) {
2175 // Simplify (X+Y) == (X+Z) --> Y == Z
2176 if (N0.getOpcode() == N1.getOpcode()) {
2177 if (N0.getOperand(0) == N1.getOperand(0))
2178 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
2179 if (N0.getOperand(1) == N1.getOperand(1))
2180 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
2181 if (isCommutativeBinOp(N0.getOpcode())) {
2182 // If X op Y == Y op X, try other combinations.
2183 if (N0.getOperand(0) == N1.getOperand(1))
2184 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
2185 if (N0.getOperand(1) == N1.getOperand(0))
2186 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
2187 }
2188 }
2189
Chris Lattner5c46f742005-10-05 06:11:08 +00002190 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0. Common for condcodes.
2191 if (N0.getOpcode() == ISD::XOR)
2192 if (ConstantSDNode *XORC = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2193 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2194 // If we know that all of the inverted bits are zero, don't bother
2195 // performing the inversion.
2196 if (MaskedValueIsZero(N0.getOperand(0), ~XORC->getValue(), TLI))
2197 return DAG.getSetCC(VT, N0.getOperand(0),
2198 DAG.getConstant(XORC->getValue()^RHSC->getValue(),
2199 N0.getValueType()), Cond);
2200 }
2201
Nate Begeman452d7be2005-09-16 00:54:12 +00002202 // Simplify (X+Z) == X --> Z == 0
2203 if (N0.getOperand(0) == N1)
2204 return DAG.getSetCC(VT, N0.getOperand(1),
2205 DAG.getConstant(0, N0.getValueType()), Cond);
2206 if (N0.getOperand(1) == N1) {
2207 if (isCommutativeBinOp(N0.getOpcode()))
2208 return DAG.getSetCC(VT, N0.getOperand(0),
2209 DAG.getConstant(0, N0.getValueType()), Cond);
2210 else {
2211 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2212 // (Z-X) == X --> Z == X<<1
2213 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
2214 N1,
2215 DAG.getConstant(1,TLI.getShiftAmountTy()));
2216 WorkList.push_back(SH.Val);
2217 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
2218 }
2219 }
2220 }
2221
2222 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2223 N1.getOpcode() == ISD::XOR) {
2224 // Simplify X == (X+Z) --> Z == 0
2225 if (N1.getOperand(0) == N0) {
2226 return DAG.getSetCC(VT, N1.getOperand(1),
2227 DAG.getConstant(0, N1.getValueType()), Cond);
2228 } else if (N1.getOperand(1) == N0) {
2229 if (isCommutativeBinOp(N1.getOpcode())) {
2230 return DAG.getSetCC(VT, N1.getOperand(0),
2231 DAG.getConstant(0, N1.getValueType()), Cond);
2232 } else {
2233 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2234 // X == (Z-X) --> X<<1 == Z
2235 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
2236 DAG.getConstant(1,TLI.getShiftAmountTy()));
2237 WorkList.push_back(SH.Val);
2238 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
2239 }
2240 }
2241 }
2242 }
2243
2244 // Fold away ALL boolean setcc's.
2245 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00002246 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002247 switch (Cond) {
2248 default: assert(0 && "Unknown integer setcc!");
2249 case ISD::SETEQ: // X == Y -> (X^Y)^1
2250 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2251 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
2252 WorkList.push_back(Temp.Val);
2253 break;
2254 case ISD::SETNE: // X != Y --> (X^Y)
2255 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2256 break;
2257 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
2258 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
2259 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2260 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
2261 WorkList.push_back(Temp.Val);
2262 break;
2263 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
2264 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
2265 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2266 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
2267 WorkList.push_back(Temp.Val);
2268 break;
2269 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
2270 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
2271 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2272 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
2273 WorkList.push_back(Temp.Val);
2274 break;
2275 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
2276 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
2277 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2278 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
2279 break;
2280 }
2281 if (VT != MVT::i1) {
2282 WorkList.push_back(N0.Val);
2283 // FIXME: If running after legalize, we probably can't do this.
2284 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2285 }
2286 return N0;
2287 }
2288
2289 // Could not fold it.
2290 return SDOperand();
2291}
2292
Nate Begeman1d4d4142005-09-01 00:19:25 +00002293// SelectionDAG::Combine - This is the entry point for the file.
2294//
Nate Begeman4ebd8052005-09-01 23:24:04 +00002295void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002296 /// run - This is the main entry point to this class.
2297 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00002298 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002299}