blob: e428f0485dc46c86f2bcae72af337014d5b2687d [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)
Nate Begeman44728a72005-09-19 22:34:01 +000025// FIXME: Dead stores -> nuke
Chris Lattner40c62d52005-10-18 06:04:22 +000026// FIXME: shr X, (and Y,31) -> shr X, Y (TRICKY!)
Nate Begeman1d4d4142005-09-01 00:19:25 +000027// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000028// FIXME: undef values
Nate Begeman4ebd8052005-09-01 23:24:04 +000029// FIXME: make truncate see through SIGN_EXTEND and AND
Nate Begeman4ebd8052005-09-01 23:24:04 +000030// FIXME: (sra (sra x, c1), c2) -> (sra x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +000031// FIXME: verify that getNode can't return extends with an operand whose type
32// is >= to that of the extend.
33// FIXME: divide by zero is currently left unfolded. do we want to turn this
34// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000035// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Chris Lattner01a22022005-10-10 22:04:48 +000036// FIXME: reassociate (X+C)+Y into (X+Y)+C if the inner expression has one use
Nate Begeman1d4d4142005-09-01 00:19:25 +000037//
38//===----------------------------------------------------------------------===//
39
40#define DEBUG_TYPE "dagcombine"
41#include "llvm/ADT/Statistic.h"
42#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000043#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000044#include "llvm/Support/MathExtras.h"
45#include "llvm/Target/TargetLowering.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000046#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000047#include <cmath>
48using namespace llvm;
49
50namespace {
51 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
52
53 class DAGCombiner {
54 SelectionDAG &DAG;
55 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000056 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000057
58 // Worklist of all of the nodes that need to be simplified.
59 std::vector<SDNode*> WorkList;
60
61 /// AddUsersToWorkList - When an instruction is simplified, add all users of
62 /// the instruction to the work lists because they might get more simplified
63 /// now.
64 ///
65 void AddUsersToWorkList(SDNode *N) {
66 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000067 UI != UE; ++UI)
68 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000069 }
70
71 /// removeFromWorkList - remove all instances of N from the worklist.
72 void removeFromWorkList(SDNode *N) {
73 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
74 WorkList.end());
75 }
76
Chris Lattner01a22022005-10-10 22:04:48 +000077 SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner87514ca2005-10-10 22:31:19 +000078 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000079 DEBUG(std::cerr << "\nReplacing "; N->dump();
80 std::cerr << "\nWith: "; To[0].Val->dump();
81 std::cerr << " and " << To.size()-1 << " other values\n");
82 std::vector<SDNode*> NowDead;
83 DAG.ReplaceAllUsesWith(N, To, &NowDead);
84
85 // Push the new nodes and any users onto the worklist
86 for (unsigned i = 0, e = To.size(); i != e; ++i) {
87 WorkList.push_back(To[i].Val);
88 AddUsersToWorkList(To[i].Val);
89 }
90
91 // Nodes can end up on the worklist more than once. Make sure we do
92 // not process a node that has been replaced.
93 removeFromWorkList(N);
94 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
95 removeFromWorkList(NowDead[i]);
96
97 // Finally, since the node is now dead, remove it from the graph.
98 DAG.DeleteNode(N);
99 return SDOperand(N, 0);
100 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000101
102 SDOperand CombineTo(SDNode *N, SDOperand Res) {
103 std::vector<SDOperand> To;
104 To.push_back(Res);
105 return CombineTo(N, To);
106 }
Chris Lattner01a22022005-10-10 22:04:48 +0000107
108 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
109 std::vector<SDOperand> To;
110 To.push_back(Res0);
111 To.push_back(Res1);
112 return CombineTo(N, To);
113 }
114
Nate Begeman1d4d4142005-09-01 00:19:25 +0000115 /// visit - call the node-specific routine that knows how to fold each
116 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000117 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000118
119 // Visitation implementation - Implement dag node combining for different
120 // node types. The semantics are as follows:
121 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000122 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000123 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000124 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000125 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000126 SDOperand visitTokenFactor(SDNode *N);
127 SDOperand visitADD(SDNode *N);
128 SDOperand visitSUB(SDNode *N);
129 SDOperand visitMUL(SDNode *N);
130 SDOperand visitSDIV(SDNode *N);
131 SDOperand visitUDIV(SDNode *N);
132 SDOperand visitSREM(SDNode *N);
133 SDOperand visitUREM(SDNode *N);
134 SDOperand visitMULHU(SDNode *N);
135 SDOperand visitMULHS(SDNode *N);
136 SDOperand visitAND(SDNode *N);
137 SDOperand visitOR(SDNode *N);
138 SDOperand visitXOR(SDNode *N);
139 SDOperand visitSHL(SDNode *N);
140 SDOperand visitSRA(SDNode *N);
141 SDOperand visitSRL(SDNode *N);
142 SDOperand visitCTLZ(SDNode *N);
143 SDOperand visitCTTZ(SDNode *N);
144 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000145 SDOperand visitSELECT(SDNode *N);
146 SDOperand visitSELECT_CC(SDNode *N);
147 SDOperand visitSETCC(SDNode *N);
Nate Begeman5054f162005-10-14 01:12:21 +0000148 SDOperand visitADD_PARTS(SDNode *N);
149 SDOperand visitSUB_PARTS(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000150 SDOperand visitSIGN_EXTEND(SDNode *N);
151 SDOperand visitZERO_EXTEND(SDNode *N);
152 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
153 SDOperand visitTRUNCATE(SDNode *N);
Nate Begeman5054f162005-10-14 01:12:21 +0000154
Chris Lattner01b3d732005-09-28 22:28:18 +0000155 SDOperand visitFADD(SDNode *N);
156 SDOperand visitFSUB(SDNode *N);
157 SDOperand visitFMUL(SDNode *N);
158 SDOperand visitFDIV(SDNode *N);
159 SDOperand visitFREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000160 SDOperand visitSINT_TO_FP(SDNode *N);
161 SDOperand visitUINT_TO_FP(SDNode *N);
162 SDOperand visitFP_TO_SINT(SDNode *N);
163 SDOperand visitFP_TO_UINT(SDNode *N);
164 SDOperand visitFP_ROUND(SDNode *N);
165 SDOperand visitFP_ROUND_INREG(SDNode *N);
166 SDOperand visitFP_EXTEND(SDNode *N);
167 SDOperand visitFNEG(SDNode *N);
168 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000169 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000170 SDOperand visitBRCONDTWOWAY(SDNode *N);
171 SDOperand visitBR_CC(SDNode *N);
172 SDOperand visitBRTWOWAY_CC(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000173
Chris Lattner01a22022005-10-10 22:04:48 +0000174 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000175 SDOperand visitSTORE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000176
Chris Lattner40c62d52005-10-18 06:04:22 +0000177 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Nate Begeman44728a72005-09-19 22:34:01 +0000178 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
179 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
180 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000181 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000182 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000183public:
184 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000185 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000186
187 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000188 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000189 };
190}
191
Nate Begeman07ed4172005-10-10 21:26:48 +0000192/// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero. We use
193/// this predicate to simplify operations downstream. Op and Mask are known to
Nate Begeman1d4d4142005-09-01 00:19:25 +0000194/// be the same type.
195static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
196 const TargetLowering &TLI) {
197 unsigned SrcBits;
198 if (Mask == 0) return true;
199
200 // If we know the result of a setcc has the top bits zero, use this info.
201 switch (Op.getOpcode()) {
Nate Begeman4ebd8052005-09-01 23:24:04 +0000202 case ISD::Constant:
203 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
204 case ISD::SETCC:
205 return ((Mask & 1) == 0) &&
206 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
207 case ISD::ZEXTLOAD:
208 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
209 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
210 case ISD::ZERO_EXTEND:
211 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
212 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
213 case ISD::AssertZext:
214 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
215 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
216 case ISD::AND:
Chris Lattneree899e62005-10-09 22:12:36 +0000217 // If either of the operands has zero bits, the result will too.
218 if (MaskedValueIsZero(Op.getOperand(1), Mask, TLI) ||
219 MaskedValueIsZero(Op.getOperand(0), Mask, TLI))
220 return true;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000221 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
222 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
223 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
Chris Lattneree899e62005-10-09 22:12:36 +0000224 return false;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000225 case ISD::OR:
226 case ISD::XOR:
227 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
228 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
229 case ISD::SELECT:
230 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
231 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
232 case ISD::SELECT_CC:
233 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
234 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
235 case ISD::SRL:
236 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
237 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
238 uint64_t NewVal = Mask << ShAmt->getValue();
239 SrcBits = MVT::getSizeInBits(Op.getValueType());
240 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
241 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
242 }
243 return false;
244 case ISD::SHL:
245 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
246 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
247 uint64_t NewVal = Mask >> ShAmt->getValue();
248 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
249 }
250 return false;
Chris Lattnerbba9aa32005-10-10 16:51:40 +0000251 case ISD::ADD:
Chris Lattnerd7390752005-10-10 16:52:03 +0000252 // (add X, Y) & C == 0 iff (X&C)|(Y&C) == 0 and all bits are low bits.
Chris Lattnerbba9aa32005-10-10 16:51:40 +0000253 if ((Mask&(Mask+1)) == 0) { // All low bits
254 if (MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
255 MaskedValueIsZero(Op.getOperand(1), Mask, TLI))
256 return true;
257 }
258 break;
Chris Lattnerc4ced262005-10-07 15:30:32 +0000259 case ISD::SUB:
260 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
261 // We know that the top bits of C-X are clear if X contains less bits
262 // than C (i.e. no wrap-around can happen). For example, 20-X is
263 // positive if we can prove that X is >= 0 and < 16.
264 unsigned Bits = MVT::getSizeInBits(CLHS->getValueType(0));
265 if ((CLHS->getValue() & (1 << (Bits-1))) == 0) { // sign bit clear
266 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
267 uint64_t MaskV = (1ULL << (63-NLZ))-1;
268 if (MaskedValueIsZero(Op.getOperand(1), ~MaskV, TLI)) {
269 // High bits are clear this value is known to be >= C.
270 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
271 if ((Mask & ((1ULL << (64-NLZ2))-1)) == 0)
272 return true;
273 }
274 }
275 }
276 break;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000277 case ISD::CTTZ:
278 case ISD::CTLZ:
279 case ISD::CTPOP:
280 // Bit counting instructions can not set the high bits of the result
281 // register. The max number of bits sets depends on the input.
282 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000283 default: break;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000284 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000285 return false;
286}
287
Nate Begeman4ebd8052005-09-01 23:24:04 +0000288// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
289// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000290// Also, set the incoming LHS, RHS, and CC references to the appropriate
291// nodes based on the type of node we are checking. This simplifies life a
292// bit for the callers.
293static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
294 SDOperand &CC) {
295 if (N.getOpcode() == ISD::SETCC) {
296 LHS = N.getOperand(0);
297 RHS = N.getOperand(1);
298 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000299 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000300 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000301 if (N.getOpcode() == ISD::SELECT_CC &&
302 N.getOperand(2).getOpcode() == ISD::Constant &&
303 N.getOperand(3).getOpcode() == ISD::Constant &&
304 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000305 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
306 LHS = N.getOperand(0);
307 RHS = N.getOperand(1);
308 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000309 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000310 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000311 return false;
312}
313
Nate Begeman99801192005-09-07 23:25:52 +0000314// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
315// one use. If this is true, it allows the users to invert the operation for
316// free when it is profitable to do so.
317static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000318 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000319 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000320 return true;
321 return false;
322}
323
Nate Begeman452d7be2005-09-16 00:54:12 +0000324// FIXME: This should probably go in the ISD class rather than being duplicated
325// in several files.
326static bool isCommutativeBinOp(unsigned Opcode) {
327 switch (Opcode) {
328 case ISD::ADD:
329 case ISD::MUL:
330 case ISD::AND:
331 case ISD::OR:
332 case ISD::XOR: return true;
333 default: return false; // FIXME: Need commutative info for user ops!
334 }
335}
336
Nate Begeman4ebd8052005-09-01 23:24:04 +0000337void DAGCombiner::Run(bool RunningAfterLegalize) {
338 // set the instance variable, so that the various visit routines may use it.
339 AfterLegalize = RunningAfterLegalize;
340
Nate Begeman646d7e22005-09-02 21:18:40 +0000341 // Add all the dag nodes to the worklist.
342 WorkList.insert(WorkList.end(), DAG.allnodes_begin(), DAG.allnodes_end());
Nate Begeman83e75ec2005-09-06 04:43:02 +0000343
Chris Lattner95038592005-10-05 06:35:28 +0000344 // Create a dummy node (which is not added to allnodes), that adds a reference
345 // to the root node, preventing it from being deleted, and tracking any
346 // changes of the root.
347 HandleSDNode Dummy(DAG.getRoot());
348
Nate Begeman1d4d4142005-09-01 00:19:25 +0000349 // while the worklist isn't empty, inspect the node on the end of it and
350 // try and combine it.
351 while (!WorkList.empty()) {
352 SDNode *N = WorkList.back();
353 WorkList.pop_back();
354
355 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000356 // N is deleted from the DAG, since they too may now be dead or may have a
357 // reduced number of uses, allowing other xforms.
358 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000359 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
360 WorkList.push_back(N->getOperand(i).Val);
361
Nate Begeman1d4d4142005-09-01 00:19:25 +0000362 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000363 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000364 continue;
365 }
366
Nate Begeman83e75ec2005-09-06 04:43:02 +0000367 SDOperand RV = visit(N);
368 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000369 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000370 // If we get back the same node we passed in, rather than a new node or
371 // zero, we know that the node must have defined multiple values and
372 // CombineTo was used. Since CombineTo takes care of the worklist
373 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000374 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000375 DEBUG(std::cerr << "\nReplacing "; N->dump();
376 std::cerr << "\nWith: "; RV.Val->dump();
377 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000378 std::vector<SDNode*> NowDead;
379 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000380
381 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000382 WorkList.push_back(RV.Val);
383 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000384
385 // Nodes can end up on the worklist more than once. Make sure we do
386 // not process a node that has been replaced.
387 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000388 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
389 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000390
391 // Finally, since the node is now dead, remove it from the graph.
392 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000393 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000394 }
395 }
Chris Lattner95038592005-10-05 06:35:28 +0000396
397 // If the root changed (e.g. it was a dead load, update the root).
398 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000399}
400
Nate Begeman83e75ec2005-09-06 04:43:02 +0000401SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000402 switch(N->getOpcode()) {
403 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000404 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000405 case ISD::ADD: return visitADD(N);
406 case ISD::SUB: return visitSUB(N);
407 case ISD::MUL: return visitMUL(N);
408 case ISD::SDIV: return visitSDIV(N);
409 case ISD::UDIV: return visitUDIV(N);
410 case ISD::SREM: return visitSREM(N);
411 case ISD::UREM: return visitUREM(N);
412 case ISD::MULHU: return visitMULHU(N);
413 case ISD::MULHS: return visitMULHS(N);
414 case ISD::AND: return visitAND(N);
415 case ISD::OR: return visitOR(N);
416 case ISD::XOR: return visitXOR(N);
417 case ISD::SHL: return visitSHL(N);
418 case ISD::SRA: return visitSRA(N);
419 case ISD::SRL: return visitSRL(N);
420 case ISD::CTLZ: return visitCTLZ(N);
421 case ISD::CTTZ: return visitCTTZ(N);
422 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000423 case ISD::SELECT: return visitSELECT(N);
424 case ISD::SELECT_CC: return visitSELECT_CC(N);
425 case ISD::SETCC: return visitSETCC(N);
Nate Begeman5054f162005-10-14 01:12:21 +0000426 case ISD::ADD_PARTS: return visitADD_PARTS(N);
427 case ISD::SUB_PARTS: return visitSUB_PARTS(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000428 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
429 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
430 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
431 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000432 case ISD::FADD: return visitFADD(N);
433 case ISD::FSUB: return visitFSUB(N);
434 case ISD::FMUL: return visitFMUL(N);
435 case ISD::FDIV: return visitFDIV(N);
436 case ISD::FREM: return visitFREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000437 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
438 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
439 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
440 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
441 case ISD::FP_ROUND: return visitFP_ROUND(N);
442 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
443 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
444 case ISD::FNEG: return visitFNEG(N);
445 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000446 case ISD::BRCOND: return visitBRCOND(N);
447 case ISD::BRCONDTWOWAY: return visitBRCONDTWOWAY(N);
448 case ISD::BR_CC: return visitBR_CC(N);
449 case ISD::BRTWOWAY_CC: return visitBRTWOWAY_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000450 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000451 case ISD::STORE: return visitSTORE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000452 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000453 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000454}
455
Nate Begeman83e75ec2005-09-06 04:43:02 +0000456SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begemanded49632005-10-13 03:11:28 +0000457 std::vector<SDOperand> Ops;
458 bool Changed = false;
459
Nate Begeman1d4d4142005-09-01 00:19:25 +0000460 // If the token factor has two operands and one is the entry token, replace
461 // the token factor with the other operand.
462 if (N->getNumOperands() == 2) {
463 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000464 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000465 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000466 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000467 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000468
Nate Begemanded49632005-10-13 03:11:28 +0000469 // fold (tokenfactor (tokenfactor)) -> tokenfactor
470 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
471 SDOperand Op = N->getOperand(i);
472 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
473 Changed = true;
474 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
475 Ops.push_back(Op.getOperand(j));
476 } else {
477 Ops.push_back(Op);
478 }
479 }
480 if (Changed)
481 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000482 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000483}
484
Nate Begeman83e75ec2005-09-06 04:43:02 +0000485SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000486 SDOperand N0 = N->getOperand(0);
487 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000488 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
489 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000490 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000491
492 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000493 if (N0C && N1C)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000494 return DAG.getConstant(N0C->getValue() + N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000495 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000496 if (N0C && !N1C)
497 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000498 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000499 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000500 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000501 // fold (add (add x, c1), c2) -> (add x, c1+c2)
502 if (N1C && N0.getOpcode() == ISD::ADD) {
503 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
504 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
505 if (N00C)
506 return DAG.getNode(ISD::ADD, VT, N0.getOperand(1),
507 DAG.getConstant(N1C->getValue()+N00C->getValue(), VT));
508 if (N01C)
509 return DAG.getNode(ISD::ADD, VT, N0.getOperand(0),
510 DAG.getConstant(N1C->getValue()+N01C->getValue(), VT));
511 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000512 // fold ((0-A) + B) -> B-A
513 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
514 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000515 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000516 // fold (A + (0-B)) -> A-B
517 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
518 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000519 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000520 // fold (A+(B-A)) -> B
521 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000522 return N1.getOperand(0);
523 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000524}
525
Nate Begeman83e75ec2005-09-06 04:43:02 +0000526SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000527 SDOperand N0 = N->getOperand(0);
528 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000529 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
530 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000531
Chris Lattner854077d2005-10-17 01:07:11 +0000532 // fold (sub x, x) -> 0
533 if (N0 == N1)
534 return DAG.getConstant(0, N->getValueType(0));
535
Nate Begeman1d4d4142005-09-01 00:19:25 +0000536 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000537 if (N0C && N1C)
538 return DAG.getConstant(N0C->getValue() - N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000539 N->getValueType(0));
Chris Lattner05b57432005-10-11 06:07:15 +0000540 // fold (sub x, c) -> (add x, -c)
541 if (N1C)
542 return DAG.getNode(ISD::ADD, N0.getValueType(), N0,
543 DAG.getConstant(-N1C->getValue(), N0.getValueType()));
544
Nate Begeman1d4d4142005-09-01 00:19:25 +0000545 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000546 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000547 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000548 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000549 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000550 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000551 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000552}
553
Nate Begeman83e75ec2005-09-06 04:43:02 +0000554SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000555 SDOperand N0 = N->getOperand(0);
556 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000557 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
558 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000559 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000560
561 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000562 if (N0C && N1C)
563 return DAG.getConstant(N0C->getValue() * N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000564 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000565 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000566 if (N0C && !N1C)
567 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000568 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000569 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000570 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000571 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000572 if (N1C && N1C->isAllOnesValue())
Nate Begeman1d4d4142005-09-01 00:19:25 +0000573 return DAG.getNode(ISD::SUB, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000574 DAG.getConstant(0, N->getValueType(0)), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000575 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000576 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000577 return DAG.getNode(ISD::SHL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000578 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000579 TLI.getShiftAmountTy()));
Nate Begeman223df222005-09-08 20:18:10 +0000580 // fold (mul (mul x, c1), c2) -> (mul x, c1*c2)
581 if (N1C && N0.getOpcode() == ISD::MUL) {
582 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
583 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
584 if (N00C)
585 return DAG.getNode(ISD::MUL, VT, N0.getOperand(1),
586 DAG.getConstant(N1C->getValue()*N00C->getValue(), VT));
587 if (N01C)
588 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0),
589 DAG.getConstant(N1C->getValue()*N01C->getValue(), VT));
590 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000591 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000592}
593
Nate Begeman83e75ec2005-09-06 04:43:02 +0000594SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000595 SDOperand N0 = N->getOperand(0);
596 SDOperand N1 = N->getOperand(1);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000597 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000598 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
599 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000600
601 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000602 if (N0C && N1C && !N1C->isNullValue())
603 return DAG.getConstant(N0C->getSignExtended() / N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000604 N->getValueType(0));
Chris Lattner094c8fc2005-10-07 06:10:46 +0000605 // If we know the sign bits of both operands are zero, strength reduce to a
606 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
607 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
608 if (MaskedValueIsZero(N1, SignBit, TLI) &&
609 MaskedValueIsZero(N0, SignBit, TLI))
610 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000611 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000612}
613
Nate Begeman83e75ec2005-09-06 04:43:02 +0000614SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000615 SDOperand N0 = N->getOperand(0);
616 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000617 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
618 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000619
620 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000621 if (N0C && N1C && !N1C->isNullValue())
622 return DAG.getConstant(N0C->getValue() / N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000623 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000624 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000625 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000626 return DAG.getNode(ISD::SRL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000627 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000628 TLI.getShiftAmountTy()));
629 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000630}
631
Nate Begeman83e75ec2005-09-06 04:43:02 +0000632SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000633 SDOperand N0 = N->getOperand(0);
634 SDOperand N1 = N->getOperand(1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000635 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000636 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
637 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000638
639 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000640 if (N0C && N1C && !N1C->isNullValue())
641 return DAG.getConstant(N0C->getSignExtended() % N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000642 N->getValueType(0));
Nate Begeman07ed4172005-10-10 21:26:48 +0000643 // If we know the sign bits of both operands are zero, strength reduce to a
644 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
645 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
646 if (MaskedValueIsZero(N1, SignBit, TLI) &&
647 MaskedValueIsZero(N0, SignBit, TLI))
648 return DAG.getNode(ISD::UREM, N1.getValueType(), N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000649 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000650}
651
Nate Begeman83e75ec2005-09-06 04:43:02 +0000652SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000653 SDOperand N0 = N->getOperand(0);
654 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000655 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
656 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000657
658 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000659 if (N0C && N1C && !N1C->isNullValue())
660 return DAG.getConstant(N0C->getValue() % N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000661 N->getValueType(0));
Nate Begeman07ed4172005-10-10 21:26:48 +0000662 // fold (urem x, pow2) -> (and x, pow2-1)
663 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
664 return DAG.getNode(ISD::AND, N0.getValueType(), N0,
665 DAG.getConstant(N1C->getValue()-1, N1.getValueType()));
Nate Begeman83e75ec2005-09-06 04:43:02 +0000666 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000667}
668
Nate Begeman83e75ec2005-09-06 04:43:02 +0000669SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000670 SDOperand N0 = N->getOperand(0);
671 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000672 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000673
674 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000675 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000676 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000677 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000678 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000679 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
680 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000681 TLI.getShiftAmountTy()));
682 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000683}
684
Nate Begeman83e75ec2005-09-06 04:43:02 +0000685SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000686 SDOperand N0 = N->getOperand(0);
687 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000688 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000689
690 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000691 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000692 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000693 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000694 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000695 return DAG.getConstant(0, N0.getValueType());
696 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000697}
698
Nate Begeman83e75ec2005-09-06 04:43:02 +0000699SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000700 SDOperand N0 = N->getOperand(0);
701 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000702 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000703 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
704 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000705 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000706 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000707
708 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000709 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000710 return DAG.getConstant(N0C->getValue() & N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000711 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000712 if (N0C && !N1C)
713 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000714 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000715 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000716 return N0;
717 // if (and x, c) is known to be zero, return 0
718 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
719 return DAG.getConstant(0, VT);
720 // fold (and x, c) -> x iff (x & ~c) == 0
721 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
722 TLI))
723 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000724 // fold (and (and x, c1), c2) -> (and x, c1^c2)
725 if (N1C && N0.getOpcode() == ISD::AND) {
726 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
727 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
728 if (N00C)
729 return DAG.getNode(ISD::AND, VT, N0.getOperand(1),
730 DAG.getConstant(N1C->getValue()&N00C->getValue(), VT));
731 if (N01C)
732 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
733 DAG.getConstant(N1C->getValue()&N01C->getValue(), VT));
734 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000735 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
736 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG) {
737 unsigned ExtendBits =
738 MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT());
Nate Begeman646d7e22005-09-02 21:18:40 +0000739 if ((N1C->getValue() & (~0ULL << ExtendBits)) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000740 return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000741 }
742 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Chris Lattnera179ab32005-10-11 17:56:34 +0000743 if (N0.getOpcode() == ISD::OR && N1C)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000744 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000745 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000746 return N1;
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000747 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
748 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
749 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
750 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
751
752 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
753 MVT::isInteger(LL.getValueType())) {
754 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
755 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
756 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
757 WorkList.push_back(ORNode.Val);
758 return DAG.getSetCC(VT, ORNode, LR, Op1);
759 }
760 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
761 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
762 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
763 WorkList.push_back(ANDNode.Val);
764 return DAG.getSetCC(VT, ANDNode, LR, Op1);
765 }
766 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
767 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
768 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
769 WorkList.push_back(ORNode.Val);
770 return DAG.getSetCC(VT, ORNode, LR, Op1);
771 }
772 }
773 // canonicalize equivalent to ll == rl
774 if (LL == RR && LR == RL) {
775 Op1 = ISD::getSetCCSwappedOperands(Op1);
776 std::swap(RL, RR);
777 }
778 if (LL == RL && LR == RR) {
779 bool isInteger = MVT::isInteger(LL.getValueType());
780 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
781 if (Result != ISD::SETCC_INVALID)
782 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
783 }
784 }
785 // fold (and (zext x), (zext y)) -> (zext (and x, y))
786 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
787 N1.getOpcode() == ISD::ZERO_EXTEND &&
788 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
789 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
790 N0.getOperand(0), N1.getOperand(0));
791 WorkList.push_back(ANDNode.Val);
792 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
793 }
Nate Begeman452d7be2005-09-16 00:54:12 +0000794 // fold (and (shl/srl x), (shl/srl y)) -> (shl/srl (and x, y))
795 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
796 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL)) &&
797 N0.getOperand(1) == N1.getOperand(1)) {
798 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
799 N0.getOperand(0), N1.getOperand(0));
800 WorkList.push_back(ANDNode.Val);
801 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
802 }
Chris Lattner85d63bb2005-10-15 22:18:08 +0000803 // fold (and (sra)) -> (and (srl)) when possible.
804 if (N0.getOpcode() == ISD::SRA && N0.Val->hasOneUse())
805 if (ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
806 // If the RHS of the AND has zeros where the sign bits of the SRA will
807 // land, turn the SRA into an SRL.
Chris Lattner750dbd52005-10-15 22:35:40 +0000808 if (MaskedValueIsZero(N1, (~0ULL << (OpSizeInBits-N01C->getValue())) &
Chris Lattner85d63bb2005-10-15 22:18:08 +0000809 (~0ULL>>(64-OpSizeInBits)), TLI)) {
810 WorkList.push_back(N);
811 CombineTo(N0.Val, DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
812 N0.getOperand(1)));
813 return SDOperand();
814 }
815 }
816
Nate Begemanded49632005-10-13 03:11:28 +0000817 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +0000818 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +0000819 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +0000820 // If we zero all the possible extended bits, then we can turn this into
821 // a zextload if we are running before legalize or the operation is legal.
Nate Begeman5054f162005-10-14 01:12:21 +0000822 if (MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT), TLI) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +0000823 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +0000824 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
825 N0.getOperand(1), N0.getOperand(2),
826 EVT);
Nate Begemanded49632005-10-13 03:11:28 +0000827 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +0000828 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +0000829 return SDOperand();
830 }
831 }
832 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +0000833 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +0000834 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +0000835 // If we zero all the possible extended bits, then we can turn this into
836 // a zextload if we are running before legalize or the operation is legal.
Nate Begeman5054f162005-10-14 01:12:21 +0000837 if (MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT), TLI) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +0000838 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +0000839 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
840 N0.getOperand(1), N0.getOperand(2),
841 EVT);
Nate Begemanded49632005-10-13 03:11:28 +0000842 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +0000843 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +0000844 return SDOperand();
845 }
846 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000847 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000848}
849
Nate Begeman83e75ec2005-09-06 04:43:02 +0000850SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000851 SDOperand N0 = N->getOperand(0);
852 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000853 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000854 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
855 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000856 MVT::ValueType VT = N1.getValueType();
857 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000858
859 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000860 if (N0C && N1C)
861 return DAG.getConstant(N0C->getValue() | N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000862 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000863 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000864 if (N0C && !N1C)
865 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000866 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000867 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000868 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000869 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000870 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000871 return N1;
872 // fold (or x, c) -> c iff (x & ~c) == 0
873 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
874 TLI))
875 return N1;
Nate Begeman223df222005-09-08 20:18:10 +0000876 // fold (or (or x, c1), c2) -> (or x, c1|c2)
877 if (N1C && N0.getOpcode() == ISD::OR) {
878 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
879 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
880 if (N00C)
881 return DAG.getNode(ISD::OR, VT, N0.getOperand(1),
882 DAG.getConstant(N1C->getValue()|N00C->getValue(), VT));
883 if (N01C)
884 return DAG.getNode(ISD::OR, VT, N0.getOperand(0),
885 DAG.getConstant(N1C->getValue()|N01C->getValue(), VT));
886 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000887 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
888 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
889 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
890 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
891
892 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
893 MVT::isInteger(LL.getValueType())) {
894 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
895 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
896 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
897 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
898 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
899 WorkList.push_back(ORNode.Val);
900 return DAG.getSetCC(VT, ORNode, LR, Op1);
901 }
902 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
903 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
904 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
905 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
906 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
907 WorkList.push_back(ANDNode.Val);
908 return DAG.getSetCC(VT, ANDNode, LR, Op1);
909 }
910 }
911 // canonicalize equivalent to ll == rl
912 if (LL == RR && LR == RL) {
913 Op1 = ISD::getSetCCSwappedOperands(Op1);
914 std::swap(RL, RR);
915 }
916 if (LL == RL && LR == RR) {
917 bool isInteger = MVT::isInteger(LL.getValueType());
918 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
919 if (Result != ISD::SETCC_INVALID)
920 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
921 }
922 }
923 // fold (or (zext x), (zext y)) -> (zext (or x, y))
924 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
925 N1.getOpcode() == ISD::ZERO_EXTEND &&
926 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
927 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
928 N0.getOperand(0), N1.getOperand(0));
929 WorkList.push_back(ORNode.Val);
930 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
931 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000932 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000933}
934
Nate Begeman83e75ec2005-09-06 04:43:02 +0000935SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000936 SDOperand N0 = N->getOperand(0);
937 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000938 SDOperand LHS, RHS, CC;
939 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
940 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000941 MVT::ValueType VT = N0.getValueType();
942
943 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000944 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000945 return DAG.getConstant(N0C->getValue() ^ N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000946 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000947 if (N0C && !N1C)
948 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000949 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000950 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000951 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000952 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +0000953 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
954 bool isInt = MVT::isInteger(LHS.getValueType());
955 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
956 isInt);
957 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000958 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000959 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000960 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000961 assert(0 && "Unhandled SetCC Equivalent!");
962 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000963 }
Nate Begeman99801192005-09-07 23:25:52 +0000964 // fold !(x or y) -> (!x and !y) iff x or y are setcc
965 if (N1C && N1C->getValue() == 1 &&
966 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000967 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000968 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
969 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000970 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
971 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000972 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
973 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000974 }
975 }
Nate Begeman99801192005-09-07 23:25:52 +0000976 // fold !(x or y) -> (!x and !y) iff x or y are constants
977 if (N1C && N1C->isAllOnesValue() &&
978 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000979 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000980 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
981 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000982 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
983 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000984 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
985 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000986 }
987 }
Nate Begeman223df222005-09-08 20:18:10 +0000988 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
989 if (N1C && N0.getOpcode() == ISD::XOR) {
990 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
991 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
992 if (N00C)
993 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
994 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
995 if (N01C)
996 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
997 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
998 }
999 // fold (xor x, x) -> 0
1000 if (N0 == N1)
1001 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001002 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
1003 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1004 N1.getOpcode() == ISD::ZERO_EXTEND &&
1005 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1006 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1007 N0.getOperand(0), N1.getOperand(0));
1008 WorkList.push_back(XORNode.Val);
1009 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
1010 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001011 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001012}
1013
Nate Begeman83e75ec2005-09-06 04:43:02 +00001014SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001015 SDOperand N0 = N->getOperand(0);
1016 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001017 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1018 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001019 MVT::ValueType VT = N0.getValueType();
1020 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1021
1022 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001023 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001024 return DAG.getConstant(N0C->getValue() << N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001025 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001026 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001027 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001028 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001029 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001030 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001031 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001032 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001033 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001034 // if (shl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +00001035 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
1036 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001037 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001038 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001039 N0.getOperand(1).getOpcode() == ISD::Constant) {
1040 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001041 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001042 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001043 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001044 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001045 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001046 }
1047 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1048 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001049 if (N1C && N0.getOpcode() == ISD::SRL &&
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 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1054 DAG.getConstant(~0ULL << c1, VT));
1055 if (c2 > c1)
1056 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001057 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001058 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001059 return DAG.getNode(ISD::SRL, VT, Mask,
1060 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001061 }
1062 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001063 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001064 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001065 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1066 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001067}
1068
Nate Begeman83e75ec2005-09-06 04:43:02 +00001069SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001070 SDOperand N0 = N->getOperand(0);
1071 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001072 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1073 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001074 MVT::ValueType VT = N0.getValueType();
1075 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1076
1077 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001078 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001079 return DAG.getConstant(N0C->getSignExtended() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001080 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001081 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001082 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001083 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001084 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001085 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001086 // fold (sra x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001087 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001088 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001089 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001090 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001091 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001092 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begeman3df4d522005-10-12 20:40:40 +00001093 if (MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1)), TLI))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001094 return DAG.getNode(ISD::SRL, VT, N0, N1);
1095 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001096}
1097
Nate Begeman83e75ec2005-09-06 04:43:02 +00001098SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001099 SDOperand N0 = N->getOperand(0);
1100 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001101 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1102 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001103 MVT::ValueType VT = N0.getValueType();
1104 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1105
1106 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001107 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001108 return DAG.getConstant(N0C->getValue() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001109 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001110 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001111 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001112 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001113 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001114 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001115 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001116 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001117 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001118 // if (srl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +00001119 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
1120 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001121 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001122 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001123 N0.getOperand(1).getOpcode() == ISD::Constant) {
1124 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001125 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001126 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001127 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001128 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001129 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001130 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001131 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001132}
1133
Nate Begeman83e75ec2005-09-06 04:43:02 +00001134SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001135 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001136 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001137
1138 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001139 if (N0C)
1140 return DAG.getConstant(CountLeadingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001141 N0.getValueType());
1142 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001143}
1144
Nate Begeman83e75ec2005-09-06 04:43:02 +00001145SDOperand DAGCombiner::visitCTTZ(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 (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001150 if (N0C)
1151 return DAG.getConstant(CountTrailingZeros_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::visitCTPOP(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 (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001161 if (N0C)
1162 return DAG.getConstant(CountPopulation_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 Begeman452d7be2005-09-16 00:54:12 +00001167SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1168 SDOperand N0 = N->getOperand(0);
1169 SDOperand N1 = N->getOperand(1);
1170 SDOperand N2 = N->getOperand(2);
1171 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1172 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1173 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1174 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001175
Nate Begeman452d7be2005-09-16 00:54:12 +00001176 // fold select C, X, X -> X
1177 if (N1 == N2)
1178 return N1;
1179 // fold select true, X, Y -> X
1180 if (N0C && !N0C->isNullValue())
1181 return N1;
1182 // fold select false, X, Y -> Y
1183 if (N0C && N0C->isNullValue())
1184 return N2;
1185 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001186 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001187 return DAG.getNode(ISD::OR, VT, N0, N2);
1188 // fold select C, 0, X -> ~C & X
1189 // FIXME: this should check for C type == X type, not i1?
1190 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1191 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1192 WorkList.push_back(XORNode.Val);
1193 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1194 }
1195 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001196 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001197 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1198 WorkList.push_back(XORNode.Val);
1199 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1200 }
1201 // fold select C, X, 0 -> C & X
1202 // FIXME: this should check for C type == X type, not i1?
1203 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1204 return DAG.getNode(ISD::AND, VT, N0, N1);
1205 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1206 if (MVT::i1 == VT && N0 == N1)
1207 return DAG.getNode(ISD::OR, VT, N0, N2);
1208 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1209 if (MVT::i1 == VT && N0 == N2)
1210 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner40c62d52005-10-18 06:04:22 +00001211
1212 // If we can fold this based on the true/false value, do so.
1213 if (SimplifySelectOps(N, N1, N2))
1214 return SDOperand();
1215
Nate Begeman44728a72005-09-19 22:34:01 +00001216 // fold selects based on a setcc into other things, such as min/max/abs
1217 if (N0.getOpcode() == ISD::SETCC)
1218 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001219 return SDOperand();
1220}
1221
1222SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001223 SDOperand N0 = N->getOperand(0);
1224 SDOperand N1 = N->getOperand(1);
1225 SDOperand N2 = N->getOperand(2);
1226 SDOperand N3 = N->getOperand(3);
1227 SDOperand N4 = N->getOperand(4);
1228 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1229 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1230 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1231 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1232
1233 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001234 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001235 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1236
Nate Begeman44728a72005-09-19 22:34:01 +00001237 // fold select_cc lhs, rhs, x, x, cc -> x
1238 if (N2 == N3)
1239 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001240
1241 // If we can fold this based on the true/false value, do so.
1242 if (SimplifySelectOps(N, N2, N3))
1243 return SDOperand();
1244
Nate Begeman44728a72005-09-19 22:34:01 +00001245 // fold select_cc into other things, such as min/max/abs
1246 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001247}
1248
1249SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1250 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1251 cast<CondCodeSDNode>(N->getOperand(2))->get());
1252}
1253
Nate Begeman5054f162005-10-14 01:12:21 +00001254SDOperand DAGCombiner::visitADD_PARTS(SDNode *N) {
1255 SDOperand LHSLo = N->getOperand(0);
1256 SDOperand RHSLo = N->getOperand(2);
1257 MVT::ValueType VT = LHSLo.getValueType();
1258
1259 // fold (a_Hi, 0) + (b_Hi, b_Lo) -> (b_Hi + a_Hi, b_Lo)
1260 if (MaskedValueIsZero(LHSLo, (1ULL << MVT::getSizeInBits(VT))-1, TLI)) {
1261 SDOperand Hi = DAG.getNode(ISD::ADD, VT, N->getOperand(1),
1262 N->getOperand(3));
1263 WorkList.push_back(Hi.Val);
1264 CombineTo(N, RHSLo, Hi);
1265 return SDOperand();
1266 }
1267 // fold (a_Hi, a_Lo) + (b_Hi, 0) -> (a_Hi + b_Hi, a_Lo)
1268 if (MaskedValueIsZero(RHSLo, (1ULL << MVT::getSizeInBits(VT))-1, TLI)) {
1269 SDOperand Hi = DAG.getNode(ISD::ADD, VT, N->getOperand(1),
1270 N->getOperand(3));
1271 WorkList.push_back(Hi.Val);
1272 CombineTo(N, LHSLo, Hi);
1273 return SDOperand();
1274 }
1275 return SDOperand();
1276}
1277
1278SDOperand DAGCombiner::visitSUB_PARTS(SDNode *N) {
1279 SDOperand LHSLo = N->getOperand(0);
1280 SDOperand RHSLo = N->getOperand(2);
1281 MVT::ValueType VT = LHSLo.getValueType();
1282
1283 // fold (a_Hi, a_Lo) - (b_Hi, 0) -> (a_Hi - b_Hi, a_Lo)
1284 if (MaskedValueIsZero(RHSLo, (1ULL << MVT::getSizeInBits(VT))-1, TLI)) {
1285 SDOperand Hi = DAG.getNode(ISD::SUB, VT, N->getOperand(1),
1286 N->getOperand(3));
1287 WorkList.push_back(Hi.Val);
1288 CombineTo(N, LHSLo, Hi);
1289 return SDOperand();
1290 }
1291 return SDOperand();
1292}
1293
Nate Begeman83e75ec2005-09-06 04:43:02 +00001294SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001295 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001296 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001297 MVT::ValueType VT = N->getValueType(0);
1298
Nate Begeman1d4d4142005-09-01 00:19:25 +00001299 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001300 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001301 return DAG.getConstant(N0C->getSignExtended(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001302 // fold (sext (sext x)) -> (sext x)
1303 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001304 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Nate Begeman765784a2005-10-12 23:18:53 +00001305 // fold (sext (sextload x)) -> (sextload x)
1306 if (N0.getOpcode() == ISD::SEXTLOAD && VT == N0.getValueType())
1307 return N0;
Nate Begeman3df4d522005-10-12 20:40:40 +00001308 // fold (sext (load x)) -> (sextload x)
Chris Lattner40c62d52005-10-18 06:04:22 +00001309 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00001310 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1311 N0.getOperand(1), N0.getOperand(2),
1312 N0.getValueType());
Nate Begeman765784a2005-10-12 23:18:53 +00001313 WorkList.push_back(N);
Chris Lattnerf9884052005-10-13 21:52:31 +00001314 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1315 ExtLoad.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001316 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001317 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001318 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001319}
1320
Nate Begeman83e75ec2005-09-06 04:43:02 +00001321SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001322 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001323 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001324 MVT::ValueType VT = N->getValueType(0);
1325
Nate Begeman1d4d4142005-09-01 00:19:25 +00001326 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001327 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001328 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001329 // fold (zext (zext x)) -> (zext x)
1330 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001331 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
1332 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001333}
1334
Nate Begeman83e75ec2005-09-06 04:43:02 +00001335SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001336 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001337 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001338 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001339 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001340 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001341 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001342
Nate Begeman1d4d4142005-09-01 00:19:25 +00001343 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001344 if (N0C) {
1345 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001346 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001347 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001348 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001349 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman216def82005-10-14 01:29:07 +00001350 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001351 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001352 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001353 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1354 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1355 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001356 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001357 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001358 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1359 if (N0.getOpcode() == ISD::AssertSext &&
1360 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001361 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001362 }
1363 // fold (sext_in_reg (sextload x)) -> (sextload x)
1364 if (N0.getOpcode() == ISD::SEXTLOAD &&
1365 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001366 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001367 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001368 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001369 if (N0.getOpcode() == ISD::SETCC &&
1370 TLI.getSetCCResultContents() ==
1371 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001372 return N0;
Nate Begeman07ed4172005-10-10 21:26:48 +00001373 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
1374 if (MaskedValueIsZero(N0, 1ULL << (EVTBits-1), TLI))
1375 return DAG.getNode(ISD::AND, N0.getValueType(), N0,
1376 DAG.getConstant(~0ULL >> (64-EVTBits), VT));
1377 // fold (sext_in_reg (srl x)) -> sra x
1378 if (N0.getOpcode() == ISD::SRL &&
1379 N0.getOperand(1).getOpcode() == ISD::Constant &&
1380 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) {
1381 return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0),
1382 N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001383 }
Nate Begemanded49632005-10-13 03:11:28 +00001384 // fold (sext_inreg (extload x)) -> (sextload x)
1385 if (N0.getOpcode() == ISD::EXTLOAD &&
1386 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001387 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001388 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1389 N0.getOperand(1), N0.getOperand(2),
1390 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001391 WorkList.push_back(N);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001392 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001393 return SDOperand();
1394 }
1395 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001396 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00001397 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001398 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001399 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1400 N0.getOperand(1), N0.getOperand(2),
1401 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001402 WorkList.push_back(N);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001403 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001404 return SDOperand();
1405 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001406 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001407}
1408
Nate Begeman83e75ec2005-09-06 04:43:02 +00001409SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001410 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001411 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001412 MVT::ValueType VT = N->getValueType(0);
1413
1414 // noop truncate
1415 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001416 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001417 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001418 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001419 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001420 // fold (truncate (truncate x)) -> (truncate x)
1421 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001422 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001423 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1424 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1425 if (N0.getValueType() < VT)
1426 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001427 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001428 else if (N0.getValueType() > VT)
1429 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001430 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001431 else
1432 // if the source and dest are the same type, we can drop both the extend
1433 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001434 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001435 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001436 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00001437 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00001438 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1439 "Cannot truncate to larger type!");
1440 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001441 // For big endian targets, we need to add an offset to the pointer to load
1442 // the correct bytes. For little endian systems, we merely need to read
1443 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001444 uint64_t PtrOff =
1445 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001446 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1447 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1448 DAG.getConstant(PtrOff, PtrType));
1449 WorkList.push_back(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001450 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Nate Begeman765784a2005-10-12 23:18:53 +00001451 WorkList.push_back(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001452 CombineTo(N0.Val, Load, Load.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001453 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001454 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001455 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001456}
1457
Chris Lattner01b3d732005-09-28 22:28:18 +00001458SDOperand DAGCombiner::visitFADD(SDNode *N) {
1459 SDOperand N0 = N->getOperand(0);
1460 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001461 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1462 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001463 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001464
1465 // fold (fadd c1, c2) -> c1+c2
1466 if (N0CFP && N1CFP)
1467 return DAG.getConstantFP(N0CFP->getValue() + N1CFP->getValue(), VT);
1468 // canonicalize constant to RHS
1469 if (N0CFP && !N1CFP)
1470 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001471 // fold (A + (-B)) -> A-B
1472 if (N1.getOpcode() == ISD::FNEG)
1473 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001474 // fold ((-A) + B) -> B-A
1475 if (N0.getOpcode() == ISD::FNEG)
1476 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001477 return SDOperand();
1478}
1479
1480SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1481 SDOperand N0 = N->getOperand(0);
1482 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001483 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1484 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001485 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001486
1487 // fold (fsub c1, c2) -> c1-c2
1488 if (N0CFP && N1CFP)
1489 return DAG.getConstantFP(N0CFP->getValue() - N1CFP->getValue(), VT);
Chris Lattner01b3d732005-09-28 22:28:18 +00001490 // fold (A-(-B)) -> A+B
1491 if (N1.getOpcode() == ISD::FNEG)
1492 return DAG.getNode(ISD::FADD, N0.getValueType(), N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001493 return SDOperand();
1494}
1495
1496SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1497 SDOperand N0 = N->getOperand(0);
1498 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001499 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1500 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001501 MVT::ValueType VT = N->getValueType(0);
1502
Nate Begeman11af4ea2005-10-17 20:40:11 +00001503 // fold (fmul c1, c2) -> c1*c2
1504 if (N0CFP && N1CFP)
1505 return DAG.getConstantFP(N0CFP->getValue() * N1CFP->getValue(), VT);
1506 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001507 if (N0CFP && !N1CFP)
1508 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001509 // fold (fmul X, 2.0) -> (fadd X, X)
1510 if (N1CFP && N1CFP->isExactlyValue(+2.0))
1511 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001512 return SDOperand();
1513}
1514
1515SDOperand DAGCombiner::visitFDIV(SDNode *N) {
1516 SDOperand N0 = N->getOperand(0);
1517 SDOperand N1 = N->getOperand(1);
1518 MVT::ValueType VT = N->getValueType(0);
1519
1520 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1521 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1522 // fold floating point (fdiv c1, c2)
Nate Begeman11af4ea2005-10-17 20:40:11 +00001523 return DAG.getConstantFP(N0CFP->getValue() / N1CFP->getValue(), VT);
Chris Lattner01b3d732005-09-28 22:28:18 +00001524 }
1525 return SDOperand();
1526}
1527
1528SDOperand DAGCombiner::visitFREM(SDNode *N) {
1529 SDOperand N0 = N->getOperand(0);
1530 SDOperand N1 = N->getOperand(1);
1531 MVT::ValueType VT = N->getValueType(0);
1532
1533 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1534 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1535 // fold floating point (frem c1, c2) -> fmod(c1, c2)
Nate Begeman11af4ea2005-10-17 20:40:11 +00001536 return DAG.getConstantFP(fmod(N0CFP->getValue(),N1CFP->getValue()), VT);
Chris Lattner01b3d732005-09-28 22:28:18 +00001537 }
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
Chris Lattner40c62d52005-10-18 06:04:22 +00001790/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
1791/// are the two values being selected between, see if we can simplify the
1792/// select.
1793///
1794bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
1795 SDOperand RHS) {
1796
1797 // If this is a select from two identical things, try to pull the operation
1798 // through the select.
1799 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
1800#if 0
1801 std::cerr << "SELECT: ["; LHS.Val->dump();
1802 std::cerr << "] ["; RHS.Val->dump();
1803 std::cerr << "]\n";
1804#endif
1805
1806 // If this is a load and the token chain is identical, replace the select
1807 // of two loads with a load through a select of the address to load from.
1808 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
1809 // constants have been dropped into the constant pool.
1810 if ((LHS.getOpcode() == ISD::LOAD ||
1811 LHS.getOpcode() == ISD::EXTLOAD ||
1812 LHS.getOpcode() == ISD::ZEXTLOAD ||
1813 LHS.getOpcode() == ISD::SEXTLOAD) &&
1814 // Token chains must be identical.
1815 LHS.getOperand(0) == RHS.getOperand(0) &&
1816 // If this is an EXTLOAD, the VT's must match.
1817 (LHS.getOpcode() == ISD::LOAD ||
1818 LHS.getOperand(3) == RHS.getOperand(3))) {
1819 // FIXME: this conflates two src values, discarding one. This is not
1820 // the right thing to do, but nothing uses srcvalues now. When they do,
1821 // turn SrcValue into a list of locations.
1822 SDOperand Addr;
1823 if (TheSelect->getOpcode() == ISD::SELECT)
1824 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
1825 TheSelect->getOperand(0), LHS.getOperand(1),
1826 RHS.getOperand(1));
1827 else
1828 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
1829 TheSelect->getOperand(0),
1830 TheSelect->getOperand(1),
1831 LHS.getOperand(1), RHS.getOperand(1),
1832 TheSelect->getOperand(4));
1833
1834 SDOperand Load;
1835 if (LHS.getOpcode() == ISD::LOAD)
1836 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
1837 Addr, LHS.getOperand(2));
1838 else
1839 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
1840 LHS.getOperand(0), Addr, LHS.getOperand(2),
1841 cast<VTSDNode>(LHS.getOperand(3))->getVT());
1842 // Users of the select now use the result of the load.
1843 CombineTo(TheSelect, Load);
1844
1845 // Users of the old loads now use the new load's chain. We know the
1846 // old-load value is dead now.
1847 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
1848 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
1849 return true;
1850 }
1851 }
1852
1853 return false;
1854}
1855
Nate Begeman44728a72005-09-19 22:34:01 +00001856SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
1857 SDOperand N2, SDOperand N3,
1858 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00001859
1860 MVT::ValueType VT = N2.getValueType();
1861 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1862 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1863 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1864 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1865
1866 // Determine if the condition we're dealing with is constant
1867 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
1868 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1869
1870 // fold select_cc true, x, y -> x
1871 if (SCCC && SCCC->getValue())
1872 return N2;
1873 // fold select_cc false, x, y -> y
1874 if (SCCC && SCCC->getValue() == 0)
1875 return N3;
1876
1877 // Check to see if we can simplify the select into an fabs node
1878 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1879 // Allow either -0.0 or 0.0
1880 if (CFP->getValue() == 0.0) {
1881 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
1882 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
1883 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
1884 N2 == N3.getOperand(0))
1885 return DAG.getNode(ISD::FABS, VT, N0);
1886
1887 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
1888 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
1889 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
1890 N2.getOperand(0) == N3)
1891 return DAG.getNode(ISD::FABS, VT, N3);
1892 }
1893 }
1894
1895 // Check to see if we can perform the "gzip trick", transforming
1896 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
1897 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
1898 MVT::isInteger(N0.getValueType()) &&
1899 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
1900 MVT::ValueType XType = N0.getValueType();
1901 MVT::ValueType AType = N2.getValueType();
1902 if (XType >= AType) {
1903 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00001904 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00001905 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
1906 unsigned ShCtV = Log2_64(N2C->getValue());
1907 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
1908 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
1909 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
1910 WorkList.push_back(Shift.Val);
1911 if (XType > AType) {
1912 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
1913 WorkList.push_back(Shift.Val);
1914 }
1915 return DAG.getNode(ISD::AND, AType, Shift, N2);
1916 }
1917 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
1918 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1919 TLI.getShiftAmountTy()));
1920 WorkList.push_back(Shift.Val);
1921 if (XType > AType) {
1922 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
1923 WorkList.push_back(Shift.Val);
1924 }
1925 return DAG.getNode(ISD::AND, AType, Shift, N2);
1926 }
1927 }
Nate Begeman07ed4172005-10-10 21:26:48 +00001928
1929 // fold select C, 16, 0 -> shl C, 4
1930 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
1931 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
1932 // Get a SetCC of the condition
1933 // FIXME: Should probably make sure that setcc is legal if we ever have a
1934 // target where it isn't.
1935 SDOperand Temp, SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
1936 WorkList.push_back(SCC.Val);
1937 // cast from setcc result type to select result type
1938 if (AfterLegalize)
1939 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
1940 else
1941 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
1942 WorkList.push_back(Temp.Val);
1943 // shl setcc result by log2 n2c
1944 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
1945 DAG.getConstant(Log2_64(N2C->getValue()),
1946 TLI.getShiftAmountTy()));
1947 }
1948
Nate Begemanf845b452005-10-08 00:29:44 +00001949 // Check to see if this is the equivalent of setcc
1950 // FIXME: Turn all of these into setcc if setcc if setcc is legal
1951 // otherwise, go ahead with the folds.
1952 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
1953 MVT::ValueType XType = N0.getValueType();
1954 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
1955 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
1956 if (Res.getValueType() != VT)
1957 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
1958 return Res;
1959 }
1960
1961 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
1962 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
1963 TLI.isOperationLegal(ISD::CTLZ, XType)) {
1964 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
1965 return DAG.getNode(ISD::SRL, XType, Ctlz,
1966 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
1967 TLI.getShiftAmountTy()));
1968 }
1969 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
1970 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
1971 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
1972 N0);
1973 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
1974 DAG.getConstant(~0ULL, XType));
1975 return DAG.getNode(ISD::SRL, XType,
1976 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
1977 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1978 TLI.getShiftAmountTy()));
1979 }
1980 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
1981 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
1982 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
1983 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1984 TLI.getShiftAmountTy()));
1985 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
1986 }
1987 }
1988
1989 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
1990 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
1991 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
1992 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
1993 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
1994 MVT::ValueType XType = N0.getValueType();
1995 if (SubC->isNullValue() && MVT::isInteger(XType)) {
1996 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
1997 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1998 TLI.getShiftAmountTy()));
1999 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
2000 WorkList.push_back(Shift.Val);
2001 WorkList.push_back(Add.Val);
2002 return DAG.getNode(ISD::XOR, XType, Add, Shift);
2003 }
2004 }
2005 }
2006
Nate Begeman44728a72005-09-19 22:34:01 +00002007 return SDOperand();
2008}
2009
Nate Begeman452d7be2005-09-16 00:54:12 +00002010SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00002011 SDOperand N1, ISD::CondCode Cond,
2012 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002013 // These setcc operations always fold.
2014 switch (Cond) {
2015 default: break;
2016 case ISD::SETFALSE:
2017 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
2018 case ISD::SETTRUE:
2019 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
2020 }
2021
2022 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
2023 uint64_t C1 = N1C->getValue();
2024 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
2025 uint64_t C0 = N0C->getValue();
2026
2027 // Sign extend the operands if required
2028 if (ISD::isSignedIntSetCC(Cond)) {
2029 C0 = N0C->getSignExtended();
2030 C1 = N1C->getSignExtended();
2031 }
2032
2033 switch (Cond) {
2034 default: assert(0 && "Unknown integer setcc!");
2035 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2036 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2037 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
2038 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
2039 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
2040 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
2041 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
2042 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
2043 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
2044 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
2045 }
2046 } else {
2047 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
2048 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
2049 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
2050
2051 // If the comparison constant has bits in the upper part, the
2052 // zero-extended value could never match.
2053 if (C1 & (~0ULL << InSize)) {
2054 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
2055 switch (Cond) {
2056 case ISD::SETUGT:
2057 case ISD::SETUGE:
2058 case ISD::SETEQ: return DAG.getConstant(0, VT);
2059 case ISD::SETULT:
2060 case ISD::SETULE:
2061 case ISD::SETNE: return DAG.getConstant(1, VT);
2062 case ISD::SETGT:
2063 case ISD::SETGE:
2064 // True if the sign bit of C1 is set.
2065 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
2066 case ISD::SETLT:
2067 case ISD::SETLE:
2068 // True if the sign bit of C1 isn't set.
2069 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
2070 default:
2071 break;
2072 }
2073 }
2074
2075 // Otherwise, we can perform the comparison with the low bits.
2076 switch (Cond) {
2077 case ISD::SETEQ:
2078 case ISD::SETNE:
2079 case ISD::SETUGT:
2080 case ISD::SETUGE:
2081 case ISD::SETULT:
2082 case ISD::SETULE:
2083 return DAG.getSetCC(VT, N0.getOperand(0),
2084 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
2085 Cond);
2086 default:
2087 break; // todo, be more careful with signed comparisons
2088 }
2089 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2090 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2091 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
2092 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
2093 MVT::ValueType ExtDstTy = N0.getValueType();
2094 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
2095
2096 // If the extended part has any inconsistent bits, it cannot ever
2097 // compare equal. In other words, they have to be all ones or all
2098 // zeros.
2099 uint64_t ExtBits =
2100 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
2101 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
2102 return DAG.getConstant(Cond == ISD::SETNE, VT);
2103
2104 SDOperand ZextOp;
2105 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
2106 if (Op0Ty == ExtSrcTy) {
2107 ZextOp = N0.getOperand(0);
2108 } else {
2109 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
2110 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
2111 DAG.getConstant(Imm, Op0Ty));
2112 }
2113 WorkList.push_back(ZextOp.Val);
2114 // Otherwise, make this a use of a zext.
2115 return DAG.getSetCC(VT, ZextOp,
2116 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
2117 ExtDstTy),
2118 Cond);
2119 }
Chris Lattner5c46f742005-10-05 06:11:08 +00002120
Nate Begeman452d7be2005-09-16 00:54:12 +00002121 uint64_t MinVal, MaxVal;
2122 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
2123 if (ISD::isSignedIntSetCC(Cond)) {
2124 MinVal = 1ULL << (OperandBitSize-1);
2125 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
2126 MaxVal = ~0ULL >> (65-OperandBitSize);
2127 else
2128 MaxVal = 0;
2129 } else {
2130 MinVal = 0;
2131 MaxVal = ~0ULL >> (64-OperandBitSize);
2132 }
2133
2134 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
2135 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
2136 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
2137 --C1; // X >= C0 --> X > (C0-1)
2138 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2139 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
2140 }
2141
2142 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
2143 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
2144 ++C1; // X <= C0 --> X < (C0+1)
2145 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2146 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
2147 }
2148
2149 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
2150 return DAG.getConstant(0, VT); // X < MIN --> false
2151
2152 // Canonicalize setgt X, Min --> setne X, Min
2153 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
2154 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
2155
2156 // If we have setult X, 1, turn it into seteq X, 0
2157 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
2158 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
2159 ISD::SETEQ);
2160 // If we have setugt X, Max-1, turn it into seteq X, Max
2161 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
2162 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
2163 ISD::SETEQ);
2164
2165 // If we have "setcc X, C0", check to see if we can shrink the immediate
2166 // by changing cc.
2167
2168 // SETUGT X, SINTMAX -> SETLT X, 0
2169 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
2170 C1 == (~0ULL >> (65-OperandBitSize)))
2171 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
2172 ISD::SETLT);
2173
2174 // FIXME: Implement the rest of these.
2175
2176 // Fold bit comparisons when we can.
2177 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2178 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
2179 if (ConstantSDNode *AndRHS =
2180 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2181 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
2182 // Perform the xform if the AND RHS is a single bit.
2183 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
2184 return DAG.getNode(ISD::SRL, VT, N0,
2185 DAG.getConstant(Log2_64(AndRHS->getValue()),
2186 TLI.getShiftAmountTy()));
2187 }
2188 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
2189 // (X & 8) == 8 --> (X & 8) >> 3
2190 // Perform the xform if C1 is a single bit.
2191 if ((C1 & (C1-1)) == 0) {
2192 return DAG.getNode(ISD::SRL, VT, N0,
2193 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
2194 }
2195 }
2196 }
2197 }
2198 } else if (isa<ConstantSDNode>(N0.Val)) {
2199 // Ensure that the constant occurs on the RHS.
2200 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2201 }
2202
2203 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
2204 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
2205 double C0 = N0C->getValue(), C1 = N1C->getValue();
2206
2207 switch (Cond) {
2208 default: break; // FIXME: Implement the rest of these!
2209 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2210 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2211 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
2212 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
2213 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
2214 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
2215 }
2216 } else {
2217 // Ensure that the constant occurs on the RHS.
2218 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2219 }
2220
2221 if (N0 == N1) {
2222 // We can always fold X == Y for integer setcc's.
2223 if (MVT::isInteger(N0.getValueType()))
2224 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2225 unsigned UOF = ISD::getUnorderedFlavor(Cond);
2226 if (UOF == 2) // FP operators that are undefined on NaNs.
2227 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2228 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2229 return DAG.getConstant(UOF, VT);
2230 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
2231 // if it is not already.
2232 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
2233 if (NewCond != Cond)
2234 return DAG.getSetCC(VT, N0, N1, NewCond);
2235 }
2236
2237 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2238 MVT::isInteger(N0.getValueType())) {
2239 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2240 N0.getOpcode() == ISD::XOR) {
2241 // Simplify (X+Y) == (X+Z) --> Y == Z
2242 if (N0.getOpcode() == N1.getOpcode()) {
2243 if (N0.getOperand(0) == N1.getOperand(0))
2244 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
2245 if (N0.getOperand(1) == N1.getOperand(1))
2246 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
2247 if (isCommutativeBinOp(N0.getOpcode())) {
2248 // If X op Y == Y op X, try other combinations.
2249 if (N0.getOperand(0) == N1.getOperand(1))
2250 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
2251 if (N0.getOperand(1) == N1.getOperand(0))
2252 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
2253 }
2254 }
2255
Chris Lattner5c46f742005-10-05 06:11:08 +00002256 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0. Common for condcodes.
2257 if (N0.getOpcode() == ISD::XOR)
2258 if (ConstantSDNode *XORC = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2259 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2260 // If we know that all of the inverted bits are zero, don't bother
2261 // performing the inversion.
2262 if (MaskedValueIsZero(N0.getOperand(0), ~XORC->getValue(), TLI))
2263 return DAG.getSetCC(VT, N0.getOperand(0),
2264 DAG.getConstant(XORC->getValue()^RHSC->getValue(),
2265 N0.getValueType()), Cond);
2266 }
2267
Nate Begeman452d7be2005-09-16 00:54:12 +00002268 // Simplify (X+Z) == X --> Z == 0
2269 if (N0.getOperand(0) == N1)
2270 return DAG.getSetCC(VT, N0.getOperand(1),
2271 DAG.getConstant(0, N0.getValueType()), Cond);
2272 if (N0.getOperand(1) == N1) {
2273 if (isCommutativeBinOp(N0.getOpcode()))
2274 return DAG.getSetCC(VT, N0.getOperand(0),
2275 DAG.getConstant(0, N0.getValueType()), Cond);
2276 else {
2277 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2278 // (Z-X) == X --> Z == X<<1
2279 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
2280 N1,
2281 DAG.getConstant(1,TLI.getShiftAmountTy()));
2282 WorkList.push_back(SH.Val);
2283 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
2284 }
2285 }
2286 }
2287
2288 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2289 N1.getOpcode() == ISD::XOR) {
2290 // Simplify X == (X+Z) --> Z == 0
2291 if (N1.getOperand(0) == N0) {
2292 return DAG.getSetCC(VT, N1.getOperand(1),
2293 DAG.getConstant(0, N1.getValueType()), Cond);
2294 } else if (N1.getOperand(1) == N0) {
2295 if (isCommutativeBinOp(N1.getOpcode())) {
2296 return DAG.getSetCC(VT, N1.getOperand(0),
2297 DAG.getConstant(0, N1.getValueType()), Cond);
2298 } else {
2299 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2300 // X == (Z-X) --> X<<1 == Z
2301 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
2302 DAG.getConstant(1,TLI.getShiftAmountTy()));
2303 WorkList.push_back(SH.Val);
2304 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
2305 }
2306 }
2307 }
2308 }
2309
2310 // Fold away ALL boolean setcc's.
2311 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00002312 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002313 switch (Cond) {
2314 default: assert(0 && "Unknown integer setcc!");
2315 case ISD::SETEQ: // X == Y -> (X^Y)^1
2316 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2317 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
2318 WorkList.push_back(Temp.Val);
2319 break;
2320 case ISD::SETNE: // X != Y --> (X^Y)
2321 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2322 break;
2323 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
2324 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
2325 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2326 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
2327 WorkList.push_back(Temp.Val);
2328 break;
2329 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
2330 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
2331 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2332 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
2333 WorkList.push_back(Temp.Val);
2334 break;
2335 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
2336 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
2337 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2338 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
2339 WorkList.push_back(Temp.Val);
2340 break;
2341 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
2342 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
2343 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2344 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
2345 break;
2346 }
2347 if (VT != MVT::i1) {
2348 WorkList.push_back(N0.Val);
2349 // FIXME: If running after legalize, we probably can't do this.
2350 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2351 }
2352 return N0;
2353 }
2354
2355 // Could not fold it.
2356 return SDOperand();
2357}
2358
Nate Begeman1d4d4142005-09-01 00:19:25 +00002359// SelectionDAG::Combine - This is the entry point for the file.
2360//
Nate Begeman4ebd8052005-09-01 23:24:04 +00002361void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002362 /// run - This is the main entry point to this class.
2363 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00002364 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002365}