blob: 64558044ec18962eb72eedf68aaceca39586d681 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
12//
13// FIXME: Missing folds
14// sdiv, udiv, srem, urem (X, const) where X is an integer can be expanded into
15// a sequence of multiplies, shifts, and adds. This should be controlled by
16// some kind of hint from the target that int div is expensive.
17// various folds of mulh[s,u] by constants such as -1, powers of 2, etc.
18//
19// FIXME: Should add a corresponding version of fold AND with
20// ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
21// we don't have yet.
22//
Nate Begeman44728a72005-09-19 22:34:01 +000023// FIXME: select C, pow2, pow2 -> something smart
24// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
25// FIXME: (select C, load A, load B) -> load (select C, A, B)
Nate Begeman44728a72005-09-19 22:34:01 +000026// FIXME: Dead stores -> nuke
27// FIXME: shr X, (and Y,31) -> shr X, Y
28// FIXME: TRUNC (LOAD) -> EXT_LOAD/LOAD(smaller)
Nate Begeman1d4d4142005-09-01 00:19:25 +000029// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000030// FIXME: undef values
Nate Begeman4ebd8052005-09-01 23:24:04 +000031// FIXME: make truncate see through SIGN_EXTEND and AND
Nate Begeman4ebd8052005-09-01 23:24:04 +000032// FIXME: (sra (sra x, c1), c2) -> (sra x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +000033// FIXME: verify that getNode can't return extends with an operand whose type
34// is >= to that of the extend.
35// FIXME: divide by zero is currently left unfolded. do we want to turn this
36// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000037// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Chris Lattner01a22022005-10-10 22:04:48 +000038// FIXME: reassociate (X+C)+Y into (X+Y)+C if the inner expression has one use
Nate Begeman1d4d4142005-09-01 00:19:25 +000039//
40//===----------------------------------------------------------------------===//
41
42#define DEBUG_TYPE "dagcombine"
43#include "llvm/ADT/Statistic.h"
44#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000045#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000046#include "llvm/Support/MathExtras.h"
47#include "llvm/Target/TargetLowering.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000048#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000049#include <cmath>
50using namespace llvm;
51
52namespace {
53 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
54
55 class DAGCombiner {
56 SelectionDAG &DAG;
57 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000058 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000059
60 // Worklist of all of the nodes that need to be simplified.
61 std::vector<SDNode*> WorkList;
62
63 /// AddUsersToWorkList - When an instruction is simplified, add all users of
64 /// the instruction to the work lists because they might get more simplified
65 /// now.
66 ///
67 void AddUsersToWorkList(SDNode *N) {
68 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000069 UI != UE; ++UI)
70 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000071 }
72
73 /// removeFromWorkList - remove all instances of N from the worklist.
74 void removeFromWorkList(SDNode *N) {
75 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
76 WorkList.end());
77 }
78
Chris Lattner01a22022005-10-10 22:04:48 +000079 SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner87514ca2005-10-10 22:31:19 +000080 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000081 DEBUG(std::cerr << "\nReplacing "; N->dump();
82 std::cerr << "\nWith: "; To[0].Val->dump();
83 std::cerr << " and " << To.size()-1 << " other values\n");
84 std::vector<SDNode*> NowDead;
85 DAG.ReplaceAllUsesWith(N, To, &NowDead);
86
87 // Push the new nodes and any users onto the worklist
88 for (unsigned i = 0, e = To.size(); i != e; ++i) {
89 WorkList.push_back(To[i].Val);
90 AddUsersToWorkList(To[i].Val);
91 }
92
93 // Nodes can end up on the worklist more than once. Make sure we do
94 // not process a node that has been replaced.
95 removeFromWorkList(N);
96 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
97 removeFromWorkList(NowDead[i]);
98
99 // Finally, since the node is now dead, remove it from the graph.
100 DAG.DeleteNode(N);
101 return SDOperand(N, 0);
102 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000103
104 SDOperand CombineTo(SDNode *N, SDOperand Res) {
105 std::vector<SDOperand> To;
106 To.push_back(Res);
107 return CombineTo(N, To);
108 }
Chris Lattner01a22022005-10-10 22:04:48 +0000109
110 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
111 std::vector<SDOperand> To;
112 To.push_back(Res0);
113 To.push_back(Res1);
114 return CombineTo(N, To);
115 }
116
Nate Begeman1d4d4142005-09-01 00:19:25 +0000117 /// visit - call the node-specific routine that knows how to fold each
118 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000119 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000120
121 // Visitation implementation - Implement dag node combining for different
122 // node types. The semantics are as follows:
123 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000124 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000125 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000126 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000127 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000128 SDOperand visitTokenFactor(SDNode *N);
129 SDOperand visitADD(SDNode *N);
130 SDOperand visitSUB(SDNode *N);
131 SDOperand visitMUL(SDNode *N);
132 SDOperand visitSDIV(SDNode *N);
133 SDOperand visitUDIV(SDNode *N);
134 SDOperand visitSREM(SDNode *N);
135 SDOperand visitUREM(SDNode *N);
136 SDOperand visitMULHU(SDNode *N);
137 SDOperand visitMULHS(SDNode *N);
138 SDOperand visitAND(SDNode *N);
139 SDOperand visitOR(SDNode *N);
140 SDOperand visitXOR(SDNode *N);
141 SDOperand visitSHL(SDNode *N);
142 SDOperand visitSRA(SDNode *N);
143 SDOperand visitSRL(SDNode *N);
144 SDOperand visitCTLZ(SDNode *N);
145 SDOperand visitCTTZ(SDNode *N);
146 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000147 SDOperand visitSELECT(SDNode *N);
148 SDOperand visitSELECT_CC(SDNode *N);
149 SDOperand visitSETCC(SDNode *N);
Nate Begeman5054f162005-10-14 01:12:21 +0000150 SDOperand visitADD_PARTS(SDNode *N);
151 SDOperand visitSUB_PARTS(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000152 SDOperand visitSIGN_EXTEND(SDNode *N);
153 SDOperand visitZERO_EXTEND(SDNode *N);
154 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
155 SDOperand visitTRUNCATE(SDNode *N);
Nate Begeman5054f162005-10-14 01:12:21 +0000156
Chris Lattner01b3d732005-09-28 22:28:18 +0000157 SDOperand visitFADD(SDNode *N);
158 SDOperand visitFSUB(SDNode *N);
159 SDOperand visitFMUL(SDNode *N);
160 SDOperand visitFDIV(SDNode *N);
161 SDOperand visitFREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000162 SDOperand visitSINT_TO_FP(SDNode *N);
163 SDOperand visitUINT_TO_FP(SDNode *N);
164 SDOperand visitFP_TO_SINT(SDNode *N);
165 SDOperand visitFP_TO_UINT(SDNode *N);
166 SDOperand visitFP_ROUND(SDNode *N);
167 SDOperand visitFP_ROUND_INREG(SDNode *N);
168 SDOperand visitFP_EXTEND(SDNode *N);
169 SDOperand visitFNEG(SDNode *N);
170 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000171 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000172 SDOperand visitBRCONDTWOWAY(SDNode *N);
173 SDOperand visitBR_CC(SDNode *N);
174 SDOperand visitBRTWOWAY_CC(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000175
Chris Lattner01a22022005-10-10 22:04:48 +0000176 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000177 SDOperand visitSTORE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000178
Nate Begeman44728a72005-09-19 22:34:01 +0000179 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
180 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
181 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000182 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000183 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000184public:
185 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000186 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000187
188 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000189 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000190 };
191}
192
Nate Begeman07ed4172005-10-10 21:26:48 +0000193/// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero. We use
194/// this predicate to simplify operations downstream. Op and Mask are known to
Nate Begeman1d4d4142005-09-01 00:19:25 +0000195/// be the same type.
196static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
197 const TargetLowering &TLI) {
198 unsigned SrcBits;
199 if (Mask == 0) return true;
200
201 // If we know the result of a setcc has the top bits zero, use this info.
202 switch (Op.getOpcode()) {
Nate Begeman4ebd8052005-09-01 23:24:04 +0000203 case ISD::Constant:
204 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
205 case ISD::SETCC:
206 return ((Mask & 1) == 0) &&
207 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
208 case ISD::ZEXTLOAD:
209 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
210 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
211 case ISD::ZERO_EXTEND:
212 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
213 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
214 case ISD::AssertZext:
215 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
216 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
217 case ISD::AND:
Chris Lattneree899e62005-10-09 22:12:36 +0000218 // If either of the operands has zero bits, the result will too.
219 if (MaskedValueIsZero(Op.getOperand(1), Mask, TLI) ||
220 MaskedValueIsZero(Op.getOperand(0), Mask, TLI))
221 return true;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000222 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
223 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
224 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
Chris Lattneree899e62005-10-09 22:12:36 +0000225 return false;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000226 case ISD::OR:
227 case ISD::XOR:
228 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
229 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
230 case ISD::SELECT:
231 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
232 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
233 case ISD::SELECT_CC:
234 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
235 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
236 case ISD::SRL:
237 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
238 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
239 uint64_t NewVal = Mask << ShAmt->getValue();
240 SrcBits = MVT::getSizeInBits(Op.getValueType());
241 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
242 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
243 }
244 return false;
245 case ISD::SHL:
246 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
247 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
248 uint64_t NewVal = Mask >> ShAmt->getValue();
249 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
250 }
251 return false;
Chris Lattnerbba9aa32005-10-10 16:51:40 +0000252 case ISD::ADD:
Chris Lattnerd7390752005-10-10 16:52:03 +0000253 // (add X, Y) & C == 0 iff (X&C)|(Y&C) == 0 and all bits are low bits.
Chris Lattnerbba9aa32005-10-10 16:51:40 +0000254 if ((Mask&(Mask+1)) == 0) { // All low bits
255 if (MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
256 MaskedValueIsZero(Op.getOperand(1), Mask, TLI))
257 return true;
258 }
259 break;
Chris Lattnerc4ced262005-10-07 15:30:32 +0000260 case ISD::SUB:
261 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
262 // We know that the top bits of C-X are clear if X contains less bits
263 // than C (i.e. no wrap-around can happen). For example, 20-X is
264 // positive if we can prove that X is >= 0 and < 16.
265 unsigned Bits = MVT::getSizeInBits(CLHS->getValueType(0));
266 if ((CLHS->getValue() & (1 << (Bits-1))) == 0) { // sign bit clear
267 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
268 uint64_t MaskV = (1ULL << (63-NLZ))-1;
269 if (MaskedValueIsZero(Op.getOperand(1), ~MaskV, TLI)) {
270 // High bits are clear this value is known to be >= C.
271 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
272 if ((Mask & ((1ULL << (64-NLZ2))-1)) == 0)
273 return true;
274 }
275 }
276 }
277 break;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000278 case ISD::CTTZ:
279 case ISD::CTLZ:
280 case ISD::CTPOP:
281 // Bit counting instructions can not set the high bits of the result
282 // register. The max number of bits sets depends on the input.
283 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000284 default: break;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000285 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000286 return false;
287}
288
Nate Begeman4ebd8052005-09-01 23:24:04 +0000289// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
290// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000291// Also, set the incoming LHS, RHS, and CC references to the appropriate
292// nodes based on the type of node we are checking. This simplifies life a
293// bit for the callers.
294static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
295 SDOperand &CC) {
296 if (N.getOpcode() == ISD::SETCC) {
297 LHS = N.getOperand(0);
298 RHS = N.getOperand(1);
299 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000300 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000301 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000302 if (N.getOpcode() == ISD::SELECT_CC &&
303 N.getOperand(2).getOpcode() == ISD::Constant &&
304 N.getOperand(3).getOpcode() == ISD::Constant &&
305 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000306 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
307 LHS = N.getOperand(0);
308 RHS = N.getOperand(1);
309 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000310 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000311 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000312 return false;
313}
314
Nate Begeman99801192005-09-07 23:25:52 +0000315// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
316// one use. If this is true, it allows the users to invert the operation for
317// free when it is profitable to do so.
318static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000319 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000320 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000321 return true;
322 return false;
323}
324
Nate Begeman452d7be2005-09-16 00:54:12 +0000325// FIXME: This should probably go in the ISD class rather than being duplicated
326// in several files.
327static bool isCommutativeBinOp(unsigned Opcode) {
328 switch (Opcode) {
329 case ISD::ADD:
330 case ISD::MUL:
331 case ISD::AND:
332 case ISD::OR:
333 case ISD::XOR: return true;
334 default: return false; // FIXME: Need commutative info for user ops!
335 }
336}
337
Nate Begeman4ebd8052005-09-01 23:24:04 +0000338void DAGCombiner::Run(bool RunningAfterLegalize) {
339 // set the instance variable, so that the various visit routines may use it.
340 AfterLegalize = RunningAfterLegalize;
341
Nate Begeman646d7e22005-09-02 21:18:40 +0000342 // Add all the dag nodes to the worklist.
343 WorkList.insert(WorkList.end(), DAG.allnodes_begin(), DAG.allnodes_end());
Nate Begeman83e75ec2005-09-06 04:43:02 +0000344
Chris Lattner95038592005-10-05 06:35:28 +0000345 // Create a dummy node (which is not added to allnodes), that adds a reference
346 // to the root node, preventing it from being deleted, and tracking any
347 // changes of the root.
348 HandleSDNode Dummy(DAG.getRoot());
349
Nate Begeman1d4d4142005-09-01 00:19:25 +0000350 // while the worklist isn't empty, inspect the node on the end of it and
351 // try and combine it.
352 while (!WorkList.empty()) {
353 SDNode *N = WorkList.back();
354 WorkList.pop_back();
355
356 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000357 // N is deleted from the DAG, since they too may now be dead or may have a
358 // reduced number of uses, allowing other xforms.
359 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000360 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
361 WorkList.push_back(N->getOperand(i).Val);
362
Nate Begeman1d4d4142005-09-01 00:19:25 +0000363 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000364 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000365 continue;
366 }
367
Nate Begeman83e75ec2005-09-06 04:43:02 +0000368 SDOperand RV = visit(N);
369 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000370 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000371 // If we get back the same node we passed in, rather than a new node or
372 // zero, we know that the node must have defined multiple values and
373 // CombineTo was used. Since CombineTo takes care of the worklist
374 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000375 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000376 DEBUG(std::cerr << "\nReplacing "; N->dump();
377 std::cerr << "\nWith: "; RV.Val->dump();
378 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000379 std::vector<SDNode*> NowDead;
380 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000381
382 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000383 WorkList.push_back(RV.Val);
384 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000385
386 // Nodes can end up on the worklist more than once. Make sure we do
387 // not process a node that has been replaced.
388 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000389 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
390 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000391
392 // Finally, since the node is now dead, remove it from the graph.
393 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000394 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000395 }
396 }
Chris Lattner95038592005-10-05 06:35:28 +0000397
398 // If the root changed (e.g. it was a dead load, update the root).
399 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000400}
401
Nate Begeman83e75ec2005-09-06 04:43:02 +0000402SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000403 switch(N->getOpcode()) {
404 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000405 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000406 case ISD::ADD: return visitADD(N);
407 case ISD::SUB: return visitSUB(N);
408 case ISD::MUL: return visitMUL(N);
409 case ISD::SDIV: return visitSDIV(N);
410 case ISD::UDIV: return visitUDIV(N);
411 case ISD::SREM: return visitSREM(N);
412 case ISD::UREM: return visitUREM(N);
413 case ISD::MULHU: return visitMULHU(N);
414 case ISD::MULHS: return visitMULHS(N);
415 case ISD::AND: return visitAND(N);
416 case ISD::OR: return visitOR(N);
417 case ISD::XOR: return visitXOR(N);
418 case ISD::SHL: return visitSHL(N);
419 case ISD::SRA: return visitSRA(N);
420 case ISD::SRL: return visitSRL(N);
421 case ISD::CTLZ: return visitCTLZ(N);
422 case ISD::CTTZ: return visitCTTZ(N);
423 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000424 case ISD::SELECT: return visitSELECT(N);
425 case ISD::SELECT_CC: return visitSELECT_CC(N);
426 case ISD::SETCC: return visitSETCC(N);
Nate Begeman5054f162005-10-14 01:12:21 +0000427 case ISD::ADD_PARTS: return visitADD_PARTS(N);
428 case ISD::SUB_PARTS: return visitSUB_PARTS(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000429 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
430 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
431 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
432 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000433 case ISD::FADD: return visitFADD(N);
434 case ISD::FSUB: return visitFSUB(N);
435 case ISD::FMUL: return visitFMUL(N);
436 case ISD::FDIV: return visitFDIV(N);
437 case ISD::FREM: return visitFREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000438 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
439 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
440 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
441 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
442 case ISD::FP_ROUND: return visitFP_ROUND(N);
443 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
444 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
445 case ISD::FNEG: return visitFNEG(N);
446 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000447 case ISD::BRCOND: return visitBRCOND(N);
448 case ISD::BRCONDTWOWAY: return visitBRCONDTWOWAY(N);
449 case ISD::BR_CC: return visitBR_CC(N);
450 case ISD::BRTWOWAY_CC: return visitBRTWOWAY_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000451 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000452 case ISD::STORE: return visitSTORE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000453 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000454 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000455}
456
Nate Begeman83e75ec2005-09-06 04:43:02 +0000457SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begemanded49632005-10-13 03:11:28 +0000458 std::vector<SDOperand> Ops;
459 bool Changed = false;
460
Nate Begeman1d4d4142005-09-01 00:19:25 +0000461 // If the token factor has two operands and one is the entry token, replace
462 // the token factor with the other operand.
463 if (N->getNumOperands() == 2) {
464 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000465 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000466 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000467 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000468 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000469
Nate Begemanded49632005-10-13 03:11:28 +0000470 // fold (tokenfactor (tokenfactor)) -> tokenfactor
471 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
472 SDOperand Op = N->getOperand(i);
473 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
474 Changed = true;
475 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
476 Ops.push_back(Op.getOperand(j));
477 } else {
478 Ops.push_back(Op);
479 }
480 }
481 if (Changed)
482 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000483 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000484}
485
Nate Begeman83e75ec2005-09-06 04:43:02 +0000486SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000487 SDOperand N0 = N->getOperand(0);
488 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000489 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
490 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000491 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000492
493 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000494 if (N0C && N1C)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000495 return DAG.getConstant(N0C->getValue() + N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000496 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000497 if (N0C && !N1C)
498 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000499 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000500 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000501 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000502 // fold (add (add x, c1), c2) -> (add x, c1+c2)
503 if (N1C && N0.getOpcode() == ISD::ADD) {
504 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
505 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
506 if (N00C)
507 return DAG.getNode(ISD::ADD, VT, N0.getOperand(1),
508 DAG.getConstant(N1C->getValue()+N00C->getValue(), VT));
509 if (N01C)
510 return DAG.getNode(ISD::ADD, VT, N0.getOperand(0),
511 DAG.getConstant(N1C->getValue()+N01C->getValue(), VT));
512 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000513 // fold ((0-A) + B) -> B-A
514 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
515 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000516 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000517 // fold (A + (0-B)) -> A-B
518 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
519 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000520 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000521 // fold (A+(B-A)) -> B
522 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000523 return N1.getOperand(0);
524 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000525}
526
Nate Begeman83e75ec2005-09-06 04:43:02 +0000527SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000528 SDOperand N0 = N->getOperand(0);
529 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000530 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
531 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000532
Chris Lattner854077d2005-10-17 01:07:11 +0000533 // fold (sub x, x) -> 0
534 if (N0 == N1)
535 return DAG.getConstant(0, N->getValueType(0));
536
Nate Begeman1d4d4142005-09-01 00:19:25 +0000537 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000538 if (N0C && N1C)
539 return DAG.getConstant(N0C->getValue() - N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000540 N->getValueType(0));
Chris Lattner05b57432005-10-11 06:07:15 +0000541 // fold (sub x, c) -> (add x, -c)
542 if (N1C)
543 return DAG.getNode(ISD::ADD, N0.getValueType(), N0,
544 DAG.getConstant(-N1C->getValue(), N0.getValueType()));
545
Nate Begeman1d4d4142005-09-01 00:19:25 +0000546 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000547 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000548 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000549 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000550 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000551 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000552 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000553}
554
Nate Begeman83e75ec2005-09-06 04:43:02 +0000555SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000556 SDOperand N0 = N->getOperand(0);
557 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000558 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
559 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000560 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000561
562 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000563 if (N0C && N1C)
564 return DAG.getConstant(N0C->getValue() * N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000565 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000566 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000567 if (N0C && !N1C)
568 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000569 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000570 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000571 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000572 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000573 if (N1C && N1C->isAllOnesValue())
Nate Begeman1d4d4142005-09-01 00:19:25 +0000574 return DAG.getNode(ISD::SUB, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000575 DAG.getConstant(0, N->getValueType(0)), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000576 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000577 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000578 return DAG.getNode(ISD::SHL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000579 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000580 TLI.getShiftAmountTy()));
Nate Begeman223df222005-09-08 20:18:10 +0000581 // fold (mul (mul x, c1), c2) -> (mul x, c1*c2)
582 if (N1C && N0.getOpcode() == ISD::MUL) {
583 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
584 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
585 if (N00C)
586 return DAG.getNode(ISD::MUL, VT, N0.getOperand(1),
587 DAG.getConstant(N1C->getValue()*N00C->getValue(), VT));
588 if (N01C)
589 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0),
590 DAG.getConstant(N1C->getValue()*N01C->getValue(), VT));
591 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000592 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000593}
594
Nate Begeman83e75ec2005-09-06 04:43:02 +0000595SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000596 SDOperand N0 = N->getOperand(0);
597 SDOperand N1 = N->getOperand(1);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000598 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000599 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
600 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000601
602 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000603 if (N0C && N1C && !N1C->isNullValue())
604 return DAG.getConstant(N0C->getSignExtended() / N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000605 N->getValueType(0));
Chris Lattner094c8fc2005-10-07 06:10:46 +0000606 // If we know the sign bits of both operands are zero, strength reduce to a
607 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
608 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
609 if (MaskedValueIsZero(N1, SignBit, TLI) &&
610 MaskedValueIsZero(N0, SignBit, TLI))
611 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000612 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000613}
614
Nate Begeman83e75ec2005-09-06 04:43:02 +0000615SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000616 SDOperand N0 = N->getOperand(0);
617 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000618 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
619 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000620
621 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000622 if (N0C && N1C && !N1C->isNullValue())
623 return DAG.getConstant(N0C->getValue() / N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000624 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000625 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000626 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000627 return DAG.getNode(ISD::SRL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000628 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000629 TLI.getShiftAmountTy()));
630 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000631}
632
Nate Begeman83e75ec2005-09-06 04:43:02 +0000633SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000634 SDOperand N0 = N->getOperand(0);
635 SDOperand N1 = N->getOperand(1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000636 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000637 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
638 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000639
640 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000641 if (N0C && N1C && !N1C->isNullValue())
642 return DAG.getConstant(N0C->getSignExtended() % N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000643 N->getValueType(0));
Nate Begeman07ed4172005-10-10 21:26:48 +0000644 // If we know the sign bits of both operands are zero, strength reduce to a
645 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
646 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
647 if (MaskedValueIsZero(N1, SignBit, TLI) &&
648 MaskedValueIsZero(N0, SignBit, TLI))
649 return DAG.getNode(ISD::UREM, N1.getValueType(), N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000650 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000651}
652
Nate Begeman83e75ec2005-09-06 04:43:02 +0000653SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000654 SDOperand N0 = N->getOperand(0);
655 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000656 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
657 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000658
659 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000660 if (N0C && N1C && !N1C->isNullValue())
661 return DAG.getConstant(N0C->getValue() % N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000662 N->getValueType(0));
Nate Begeman07ed4172005-10-10 21:26:48 +0000663 // fold (urem x, pow2) -> (and x, pow2-1)
664 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
665 return DAG.getNode(ISD::AND, N0.getValueType(), N0,
666 DAG.getConstant(N1C->getValue()-1, N1.getValueType()));
Nate Begeman83e75ec2005-09-06 04:43:02 +0000667 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000668}
669
Nate Begeman83e75ec2005-09-06 04:43:02 +0000670SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000671 SDOperand N0 = N->getOperand(0);
672 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000673 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000674
675 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000676 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000677 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000678 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000679 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000680 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
681 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000682 TLI.getShiftAmountTy()));
683 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000684}
685
Nate Begeman83e75ec2005-09-06 04:43:02 +0000686SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000687 SDOperand N0 = N->getOperand(0);
688 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000689 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000690
691 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000692 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000693 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000694 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000695 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000696 return DAG.getConstant(0, N0.getValueType());
697 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000698}
699
Nate Begeman83e75ec2005-09-06 04:43:02 +0000700SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000701 SDOperand N0 = N->getOperand(0);
702 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000703 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000704 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
705 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000706 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000707 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000708
709 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000710 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000711 return DAG.getConstant(N0C->getValue() & N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000712 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000713 if (N0C && !N1C)
714 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000715 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000716 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000717 return N0;
718 // if (and x, c) is known to be zero, return 0
719 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
720 return DAG.getConstant(0, VT);
721 // fold (and x, c) -> x iff (x & ~c) == 0
722 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
723 TLI))
724 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000725 // fold (and (and x, c1), c2) -> (and x, c1^c2)
726 if (N1C && N0.getOpcode() == ISD::AND) {
727 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
728 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
729 if (N00C)
730 return DAG.getNode(ISD::AND, VT, N0.getOperand(1),
731 DAG.getConstant(N1C->getValue()&N00C->getValue(), VT));
732 if (N01C)
733 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
734 DAG.getConstant(N1C->getValue()&N01C->getValue(), VT));
735 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000736 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
737 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG) {
738 unsigned ExtendBits =
739 MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT());
Nate Begeman646d7e22005-09-02 21:18:40 +0000740 if ((N1C->getValue() & (~0ULL << ExtendBits)) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000741 return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000742 }
743 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Chris Lattnera179ab32005-10-11 17:56:34 +0000744 if (N0.getOpcode() == ISD::OR && N1C)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000745 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000746 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000747 return N1;
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000748 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
749 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
750 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
751 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
752
753 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
754 MVT::isInteger(LL.getValueType())) {
755 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
756 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
757 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
758 WorkList.push_back(ORNode.Val);
759 return DAG.getSetCC(VT, ORNode, LR, Op1);
760 }
761 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
762 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
763 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
764 WorkList.push_back(ANDNode.Val);
765 return DAG.getSetCC(VT, ANDNode, LR, Op1);
766 }
767 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
768 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
769 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
770 WorkList.push_back(ORNode.Val);
771 return DAG.getSetCC(VT, ORNode, LR, Op1);
772 }
773 }
774 // canonicalize equivalent to ll == rl
775 if (LL == RR && LR == RL) {
776 Op1 = ISD::getSetCCSwappedOperands(Op1);
777 std::swap(RL, RR);
778 }
779 if (LL == RL && LR == RR) {
780 bool isInteger = MVT::isInteger(LL.getValueType());
781 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
782 if (Result != ISD::SETCC_INVALID)
783 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
784 }
785 }
786 // fold (and (zext x), (zext y)) -> (zext (and x, y))
787 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
788 N1.getOpcode() == ISD::ZERO_EXTEND &&
789 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
790 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
791 N0.getOperand(0), N1.getOperand(0));
792 WorkList.push_back(ANDNode.Val);
793 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
794 }
Nate Begeman452d7be2005-09-16 00:54:12 +0000795 // fold (and (shl/srl x), (shl/srl y)) -> (shl/srl (and x, y))
796 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
797 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL)) &&
798 N0.getOperand(1) == N1.getOperand(1)) {
799 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
800 N0.getOperand(0), N1.getOperand(0));
801 WorkList.push_back(ANDNode.Val);
802 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
803 }
Chris Lattner85d63bb2005-10-15 22:18:08 +0000804 // fold (and (sra)) -> (and (srl)) when possible.
805 if (N0.getOpcode() == ISD::SRA && N0.Val->hasOneUse())
806 if (ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
807 // If the RHS of the AND has zeros where the sign bits of the SRA will
808 // land, turn the SRA into an SRL.
Chris Lattner750dbd52005-10-15 22:35:40 +0000809 if (MaskedValueIsZero(N1, (~0ULL << (OpSizeInBits-N01C->getValue())) &
Chris Lattner85d63bb2005-10-15 22:18:08 +0000810 (~0ULL>>(64-OpSizeInBits)), TLI)) {
811 WorkList.push_back(N);
812 CombineTo(N0.Val, DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
813 N0.getOperand(1)));
814 return SDOperand();
815 }
816 }
817
Nate Begemanded49632005-10-13 03:11:28 +0000818 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +0000819 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +0000820 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +0000821 // If we zero all the possible extended bits, then we can turn this into
822 // a zextload if we are running before legalize or the operation is legal.
Nate Begeman5054f162005-10-14 01:12:21 +0000823 if (MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT), TLI) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +0000824 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +0000825 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
826 N0.getOperand(1), N0.getOperand(2),
827 EVT);
Nate Begemanded49632005-10-13 03:11:28 +0000828 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +0000829 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +0000830 return SDOperand();
831 }
832 }
833 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Nate Begeman5054f162005-10-14 01:12:21 +0000834 if (N0.getOpcode() == ISD::SEXTLOAD && N0.Val->hasNUsesOfValue(1, 0)) {
Nate Begemanded49632005-10-13 03:11:28 +0000835 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +0000836 // If we zero all the possible extended bits, then we can turn this into
837 // a zextload if we are running before legalize or the operation is legal.
Nate Begeman5054f162005-10-14 01:12:21 +0000838 if (MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT), TLI) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +0000839 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +0000840 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
841 N0.getOperand(1), N0.getOperand(2),
842 EVT);
Nate Begemanded49632005-10-13 03:11:28 +0000843 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +0000844 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +0000845 return SDOperand();
846 }
847 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000848 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000849}
850
Nate Begeman83e75ec2005-09-06 04:43:02 +0000851SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000852 SDOperand N0 = N->getOperand(0);
853 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000854 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000855 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
856 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000857 MVT::ValueType VT = N1.getValueType();
858 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000859
860 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000861 if (N0C && N1C)
862 return DAG.getConstant(N0C->getValue() | N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000863 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000864 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000865 if (N0C && !N1C)
866 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000867 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000868 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000869 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000870 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000871 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000872 return N1;
873 // fold (or x, c) -> c iff (x & ~c) == 0
874 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
875 TLI))
876 return N1;
Nate Begeman223df222005-09-08 20:18:10 +0000877 // fold (or (or x, c1), c2) -> (or x, c1|c2)
878 if (N1C && N0.getOpcode() == ISD::OR) {
879 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
880 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
881 if (N00C)
882 return DAG.getNode(ISD::OR, VT, N0.getOperand(1),
883 DAG.getConstant(N1C->getValue()|N00C->getValue(), VT));
884 if (N01C)
885 return DAG.getNode(ISD::OR, VT, N0.getOperand(0),
886 DAG.getConstant(N1C->getValue()|N01C->getValue(), VT));
887 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000888 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
889 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
890 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
891 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
892
893 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
894 MVT::isInteger(LL.getValueType())) {
895 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
896 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
897 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
898 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
899 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
900 WorkList.push_back(ORNode.Val);
901 return DAG.getSetCC(VT, ORNode, LR, Op1);
902 }
903 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
904 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
905 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
906 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
907 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
908 WorkList.push_back(ANDNode.Val);
909 return DAG.getSetCC(VT, ANDNode, LR, Op1);
910 }
911 }
912 // canonicalize equivalent to ll == rl
913 if (LL == RR && LR == RL) {
914 Op1 = ISD::getSetCCSwappedOperands(Op1);
915 std::swap(RL, RR);
916 }
917 if (LL == RL && LR == RR) {
918 bool isInteger = MVT::isInteger(LL.getValueType());
919 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
920 if (Result != ISD::SETCC_INVALID)
921 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
922 }
923 }
924 // fold (or (zext x), (zext y)) -> (zext (or x, y))
925 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
926 N1.getOpcode() == ISD::ZERO_EXTEND &&
927 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
928 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
929 N0.getOperand(0), N1.getOperand(0));
930 WorkList.push_back(ORNode.Val);
931 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
932 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000933 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000934}
935
Nate Begeman83e75ec2005-09-06 04:43:02 +0000936SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000937 SDOperand N0 = N->getOperand(0);
938 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000939 SDOperand LHS, RHS, CC;
940 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
941 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000942 MVT::ValueType VT = N0.getValueType();
943
944 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000945 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000946 return DAG.getConstant(N0C->getValue() ^ N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000947 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000948 if (N0C && !N1C)
949 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000950 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000951 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000952 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000953 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +0000954 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
955 bool isInt = MVT::isInteger(LHS.getValueType());
956 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
957 isInt);
958 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000959 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000960 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000961 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000962 assert(0 && "Unhandled SetCC Equivalent!");
963 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000964 }
Nate Begeman99801192005-09-07 23:25:52 +0000965 // fold !(x or y) -> (!x and !y) iff x or y are setcc
966 if (N1C && N1C->getValue() == 1 &&
967 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000968 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000969 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
970 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000971 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
972 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000973 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
974 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000975 }
976 }
Nate Begeman99801192005-09-07 23:25:52 +0000977 // fold !(x or y) -> (!x and !y) iff x or y are constants
978 if (N1C && N1C->isAllOnesValue() &&
979 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000980 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000981 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
982 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000983 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
984 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000985 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
986 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000987 }
988 }
Nate Begeman223df222005-09-08 20:18:10 +0000989 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
990 if (N1C && N0.getOpcode() == ISD::XOR) {
991 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
992 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
993 if (N00C)
994 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
995 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
996 if (N01C)
997 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
998 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
999 }
1000 // fold (xor x, x) -> 0
1001 if (N0 == N1)
1002 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001003 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
1004 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1005 N1.getOpcode() == ISD::ZERO_EXTEND &&
1006 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1007 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1008 N0.getOperand(0), N1.getOperand(0));
1009 WorkList.push_back(XORNode.Val);
1010 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
1011 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001012 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001013}
1014
Nate Begeman83e75ec2005-09-06 04:43:02 +00001015SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001016 SDOperand N0 = N->getOperand(0);
1017 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001018 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1019 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001020 MVT::ValueType VT = N0.getValueType();
1021 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1022
1023 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001024 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001025 return DAG.getConstant(N0C->getValue() << N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001026 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001027 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001028 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001029 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001030 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001031 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001032 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001033 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001034 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001035 // if (shl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +00001036 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
1037 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001038 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001039 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001040 N0.getOperand(1).getOpcode() == ISD::Constant) {
1041 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001042 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001043 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001044 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001045 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001046 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001047 }
1048 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1049 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001050 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001051 N0.getOperand(1).getOpcode() == ISD::Constant) {
1052 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001053 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001054 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1055 DAG.getConstant(~0ULL << c1, VT));
1056 if (c2 > c1)
1057 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001058 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001059 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001060 return DAG.getNode(ISD::SRL, VT, Mask,
1061 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001062 }
1063 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001064 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001065 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001066 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1067 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001068}
1069
Nate Begeman83e75ec2005-09-06 04:43:02 +00001070SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001071 SDOperand N0 = N->getOperand(0);
1072 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001073 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1074 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001075 MVT::ValueType VT = N0.getValueType();
1076 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1077
1078 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001079 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001080 return DAG.getConstant(N0C->getSignExtended() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001081 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001082 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001083 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001084 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001085 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001086 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001087 // fold (sra x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001088 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001089 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001090 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001091 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001092 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001093 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begeman3df4d522005-10-12 20:40:40 +00001094 if (MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1)), TLI))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001095 return DAG.getNode(ISD::SRL, VT, N0, N1);
1096 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001097}
1098
Nate Begeman83e75ec2005-09-06 04:43:02 +00001099SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001100 SDOperand N0 = N->getOperand(0);
1101 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001102 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1103 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001104 MVT::ValueType VT = N0.getValueType();
1105 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1106
1107 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001108 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001109 return DAG.getConstant(N0C->getValue() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001110 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001111 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001112 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001113 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001114 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001115 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001116 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001117 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001118 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001119 // if (srl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +00001120 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
1121 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001122 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001123 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001124 N0.getOperand(1).getOpcode() == ISD::Constant) {
1125 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001126 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001127 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001128 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001129 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001130 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001131 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001132 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001133}
1134
Nate Begeman83e75ec2005-09-06 04:43:02 +00001135SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001136 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001137 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001138
1139 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001140 if (N0C)
1141 return DAG.getConstant(CountLeadingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001142 N0.getValueType());
1143 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001144}
1145
Nate Begeman83e75ec2005-09-06 04:43:02 +00001146SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001147 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001148 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001149
1150 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001151 if (N0C)
1152 return DAG.getConstant(CountTrailingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001153 N0.getValueType());
1154 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001155}
1156
Nate Begeman83e75ec2005-09-06 04:43:02 +00001157SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001158 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001159 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001160
1161 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001162 if (N0C)
1163 return DAG.getConstant(CountPopulation_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001164 N0.getValueType());
1165 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001166}
1167
Nate Begeman452d7be2005-09-16 00:54:12 +00001168SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1169 SDOperand N0 = N->getOperand(0);
1170 SDOperand N1 = N->getOperand(1);
1171 SDOperand N2 = N->getOperand(2);
1172 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1173 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1174 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1175 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001176
Nate Begeman452d7be2005-09-16 00:54:12 +00001177 // fold select C, X, X -> X
1178 if (N1 == N2)
1179 return N1;
1180 // fold select true, X, Y -> X
1181 if (N0C && !N0C->isNullValue())
1182 return N1;
1183 // fold select false, X, Y -> Y
1184 if (N0C && N0C->isNullValue())
1185 return N2;
1186 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001187 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001188 return DAG.getNode(ISD::OR, VT, N0, N2);
1189 // fold select C, 0, X -> ~C & X
1190 // FIXME: this should check for C type == X type, not i1?
1191 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1192 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1193 WorkList.push_back(XORNode.Val);
1194 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1195 }
1196 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001197 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001198 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1199 WorkList.push_back(XORNode.Val);
1200 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1201 }
1202 // fold select C, X, 0 -> C & X
1203 // FIXME: this should check for C type == X type, not i1?
1204 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1205 return DAG.getNode(ISD::AND, VT, N0, N1);
1206 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1207 if (MVT::i1 == VT && N0 == N1)
1208 return DAG.getNode(ISD::OR, VT, N0, N2);
1209 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1210 if (MVT::i1 == VT && N0 == N2)
1211 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman44728a72005-09-19 22:34:01 +00001212 // fold selects based on a setcc into other things, such as min/max/abs
1213 if (N0.getOpcode() == ISD::SETCC)
1214 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001215 return SDOperand();
1216}
1217
1218SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001219 SDOperand N0 = N->getOperand(0);
1220 SDOperand N1 = N->getOperand(1);
1221 SDOperand N2 = N->getOperand(2);
1222 SDOperand N3 = N->getOperand(3);
1223 SDOperand N4 = N->getOperand(4);
1224 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1225 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1226 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1227 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1228
1229 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001230 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001231 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1232
Nate Begeman44728a72005-09-19 22:34:01 +00001233 // fold select_cc lhs, rhs, x, x, cc -> x
1234 if (N2 == N3)
1235 return N2;
Nate Begeman44728a72005-09-19 22:34:01 +00001236 // fold select_cc into other things, such as min/max/abs
1237 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001238}
1239
1240SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1241 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1242 cast<CondCodeSDNode>(N->getOperand(2))->get());
1243}
1244
Nate Begeman5054f162005-10-14 01:12:21 +00001245SDOperand DAGCombiner::visitADD_PARTS(SDNode *N) {
1246 SDOperand LHSLo = N->getOperand(0);
1247 SDOperand RHSLo = N->getOperand(2);
1248 MVT::ValueType VT = LHSLo.getValueType();
1249
1250 // fold (a_Hi, 0) + (b_Hi, b_Lo) -> (b_Hi + a_Hi, b_Lo)
1251 if (MaskedValueIsZero(LHSLo, (1ULL << MVT::getSizeInBits(VT))-1, TLI)) {
1252 SDOperand Hi = DAG.getNode(ISD::ADD, VT, N->getOperand(1),
1253 N->getOperand(3));
1254 WorkList.push_back(Hi.Val);
1255 CombineTo(N, RHSLo, Hi);
1256 return SDOperand();
1257 }
1258 // fold (a_Hi, a_Lo) + (b_Hi, 0) -> (a_Hi + b_Hi, a_Lo)
1259 if (MaskedValueIsZero(RHSLo, (1ULL << MVT::getSizeInBits(VT))-1, TLI)) {
1260 SDOperand Hi = DAG.getNode(ISD::ADD, VT, N->getOperand(1),
1261 N->getOperand(3));
1262 WorkList.push_back(Hi.Val);
1263 CombineTo(N, LHSLo, Hi);
1264 return SDOperand();
1265 }
1266 return SDOperand();
1267}
1268
1269SDOperand DAGCombiner::visitSUB_PARTS(SDNode *N) {
1270 SDOperand LHSLo = N->getOperand(0);
1271 SDOperand RHSLo = N->getOperand(2);
1272 MVT::ValueType VT = LHSLo.getValueType();
1273
1274 // fold (a_Hi, a_Lo) - (b_Hi, 0) -> (a_Hi - b_Hi, a_Lo)
1275 if (MaskedValueIsZero(RHSLo, (1ULL << MVT::getSizeInBits(VT))-1, TLI)) {
1276 SDOperand Hi = DAG.getNode(ISD::SUB, VT, N->getOperand(1),
1277 N->getOperand(3));
1278 WorkList.push_back(Hi.Val);
1279 CombineTo(N, LHSLo, Hi);
1280 return SDOperand();
1281 }
1282 return SDOperand();
1283}
1284
Nate Begeman83e75ec2005-09-06 04:43:02 +00001285SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001286 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001287 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001288 MVT::ValueType VT = N->getValueType(0);
1289
Nate Begeman1d4d4142005-09-01 00:19:25 +00001290 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001291 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001292 return DAG.getConstant(N0C->getSignExtended(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001293 // fold (sext (sext x)) -> (sext x)
1294 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001295 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Nate Begeman765784a2005-10-12 23:18:53 +00001296 // fold (sext (sextload x)) -> (sextload x)
1297 if (N0.getOpcode() == ISD::SEXTLOAD && VT == N0.getValueType())
1298 return N0;
Nate Begeman3df4d522005-10-12 20:40:40 +00001299 // fold (sext (load x)) -> (sextload x)
1300 if (N0.getOpcode() == ISD::LOAD && N0.Val->hasNUsesOfValue(1, 0)) {
1301 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1302 N0.getOperand(1), N0.getOperand(2),
1303 N0.getValueType());
Nate Begeman765784a2005-10-12 23:18:53 +00001304 WorkList.push_back(N);
Chris Lattnerf9884052005-10-13 21:52:31 +00001305 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1306 ExtLoad.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001307 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001308 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001309 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001310}
1311
Nate Begeman83e75ec2005-09-06 04:43:02 +00001312SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001313 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001314 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001315 MVT::ValueType VT = N->getValueType(0);
1316
Nate Begeman1d4d4142005-09-01 00:19:25 +00001317 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001318 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001319 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001320 // fold (zext (zext x)) -> (zext x)
1321 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001322 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
1323 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001324}
1325
Nate Begeman83e75ec2005-09-06 04:43:02 +00001326SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001327 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001328 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001329 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001330 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001331 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001332 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001333
Nate Begeman1d4d4142005-09-01 00:19:25 +00001334 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001335 if (N0C) {
1336 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001337 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001338 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001339 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001340 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman216def82005-10-14 01:29:07 +00001341 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001342 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001343 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001344 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1345 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1346 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001347 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001348 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001349 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1350 if (N0.getOpcode() == ISD::AssertSext &&
1351 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001352 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001353 }
1354 // fold (sext_in_reg (sextload x)) -> (sextload x)
1355 if (N0.getOpcode() == ISD::SEXTLOAD &&
1356 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001357 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001358 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001359 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001360 if (N0.getOpcode() == ISD::SETCC &&
1361 TLI.getSetCCResultContents() ==
1362 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001363 return N0;
Nate Begeman07ed4172005-10-10 21:26:48 +00001364 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
1365 if (MaskedValueIsZero(N0, 1ULL << (EVTBits-1), TLI))
1366 return DAG.getNode(ISD::AND, N0.getValueType(), N0,
1367 DAG.getConstant(~0ULL >> (64-EVTBits), VT));
1368 // fold (sext_in_reg (srl x)) -> sra x
1369 if (N0.getOpcode() == ISD::SRL &&
1370 N0.getOperand(1).getOpcode() == ISD::Constant &&
1371 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) {
1372 return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0),
1373 N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001374 }
Nate Begemanded49632005-10-13 03:11:28 +00001375 // fold (sext_inreg (extload x)) -> (sextload x)
1376 if (N0.getOpcode() == ISD::EXTLOAD &&
1377 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001378 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001379 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1380 N0.getOperand(1), N0.getOperand(2),
1381 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001382 WorkList.push_back(N);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001383 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001384 return SDOperand();
1385 }
1386 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
1387 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.Val->hasNUsesOfValue(1, 0) &&
1388 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001389 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001390 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1391 N0.getOperand(1), N0.getOperand(2),
1392 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001393 WorkList.push_back(N);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001394 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001395 return SDOperand();
1396 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001397 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001398}
1399
Nate Begeman83e75ec2005-09-06 04:43:02 +00001400SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001401 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001402 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001403 MVT::ValueType VT = N->getValueType(0);
1404
1405 // noop truncate
1406 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001407 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001408 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001409 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001410 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001411 // fold (truncate (truncate x)) -> (truncate x)
1412 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001413 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001414 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1415 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1416 if (N0.getValueType() < VT)
1417 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001418 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001419 else if (N0.getValueType() > VT)
1420 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001421 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001422 else
1423 // if the source and dest are the same type, we can drop both the extend
1424 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001425 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001426 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001427 // fold (truncate (load x)) -> (smaller load x)
1428 if (N0.getOpcode() == ISD::LOAD && N0.Val->hasNUsesOfValue(1, 0)) {
1429 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1430 "Cannot truncate to larger type!");
1431 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001432 // For big endian targets, we need to add an offset to the pointer to load
1433 // the correct bytes. For little endian systems, we merely need to read
1434 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001435 uint64_t PtrOff =
1436 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001437 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1438 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1439 DAG.getConstant(PtrOff, PtrType));
1440 WorkList.push_back(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001441 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Nate Begeman765784a2005-10-12 23:18:53 +00001442 WorkList.push_back(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001443 CombineTo(N0.Val, Load, Load.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001444 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001445 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001446 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001447}
1448
Chris Lattner01b3d732005-09-28 22:28:18 +00001449SDOperand DAGCombiner::visitFADD(SDNode *N) {
1450 SDOperand N0 = N->getOperand(0);
1451 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001452 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1453 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001454 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001455
1456 // fold (fadd c1, c2) -> c1+c2
1457 if (N0CFP && N1CFP)
1458 return DAG.getConstantFP(N0CFP->getValue() + N1CFP->getValue(), VT);
1459 // canonicalize constant to RHS
1460 if (N0CFP && !N1CFP)
1461 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001462 // fold (A + (-B)) -> A-B
1463 if (N1.getOpcode() == ISD::FNEG)
1464 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001465 // fold ((-A) + B) -> B-A
1466 if (N0.getOpcode() == ISD::FNEG)
1467 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001468 return SDOperand();
1469}
1470
1471SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1472 SDOperand N0 = N->getOperand(0);
1473 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001474 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1475 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001476 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001477
1478 // fold (fsub c1, c2) -> c1-c2
1479 if (N0CFP && N1CFP)
1480 return DAG.getConstantFP(N0CFP->getValue() - N1CFP->getValue(), VT);
Chris Lattner01b3d732005-09-28 22:28:18 +00001481 // fold (A-(-B)) -> A+B
1482 if (N1.getOpcode() == ISD::FNEG)
1483 return DAG.getNode(ISD::FADD, N0.getValueType(), N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001484 return SDOperand();
1485}
1486
1487SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1488 SDOperand N0 = N->getOperand(0);
1489 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001490 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1491 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001492 MVT::ValueType VT = N->getValueType(0);
1493
Nate Begeman11af4ea2005-10-17 20:40:11 +00001494 // fold (fmul c1, c2) -> c1*c2
1495 if (N0CFP && N1CFP)
1496 return DAG.getConstantFP(N0CFP->getValue() * N1CFP->getValue(), VT);
1497 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001498 if (N0CFP && !N1CFP)
1499 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001500 // fold (fmul X, 2.0) -> (fadd X, X)
1501 if (N1CFP && N1CFP->isExactlyValue(+2.0))
1502 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001503 return SDOperand();
1504}
1505
1506SDOperand DAGCombiner::visitFDIV(SDNode *N) {
1507 SDOperand N0 = N->getOperand(0);
1508 SDOperand N1 = N->getOperand(1);
1509 MVT::ValueType VT = N->getValueType(0);
1510
1511 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1512 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1513 // fold floating point (fdiv c1, c2)
Nate Begeman11af4ea2005-10-17 20:40:11 +00001514 return DAG.getConstantFP(N0CFP->getValue() / N1CFP->getValue(), VT);
Chris Lattner01b3d732005-09-28 22:28:18 +00001515 }
1516 return SDOperand();
1517}
1518
1519SDOperand DAGCombiner::visitFREM(SDNode *N) {
1520 SDOperand N0 = N->getOperand(0);
1521 SDOperand N1 = N->getOperand(1);
1522 MVT::ValueType VT = N->getValueType(0);
1523
1524 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1525 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1526 // fold floating point (frem c1, c2) -> fmod(c1, c2)
Nate Begeman11af4ea2005-10-17 20:40:11 +00001527 return DAG.getConstantFP(fmod(N0CFP->getValue(),N1CFP->getValue()), VT);
Chris Lattner01b3d732005-09-28 22:28:18 +00001528 }
1529 return SDOperand();
1530}
1531
1532
Nate Begeman83e75ec2005-09-06 04:43:02 +00001533SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001534 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001535 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001536
1537 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001538 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001539 return DAG.getConstantFP(N0C->getSignExtended(), N->getValueType(0));
1540 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001541}
1542
Nate Begeman83e75ec2005-09-06 04:43:02 +00001543SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001544 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001545 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001546
1547 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001548 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001549 return DAG.getConstantFP(N0C->getValue(), N->getValueType(0));
1550 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001551}
1552
Nate Begeman83e75ec2005-09-06 04:43:02 +00001553SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001554 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001555
1556 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001557 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001558 return DAG.getConstant((int64_t)N0CFP->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_UINT(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_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001566 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001567 return DAG.getConstant((uint64_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_ROUND(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_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001575 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001576 return DAG.getConstantFP(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_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001581 SDOperand N0 = N->getOperand(0);
1582 MVT::ValueType VT = N->getValueType(0);
1583 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00001584 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001585
Nate Begeman1d4d4142005-09-01 00:19:25 +00001586 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001587 if (N0CFP) {
1588 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001589 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001590 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001591 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001592}
1593
Nate Begeman83e75ec2005-09-06 04:43:02 +00001594SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001595 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001596
1597 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001598 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001599 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1600 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001601}
1602
Nate Begeman83e75ec2005-09-06 04:43:02 +00001603SDOperand DAGCombiner::visitFNEG(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 // fold (neg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001606 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001607 return DAG.getConstantFP(-N0CFP->getValue(), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001608 // fold (neg (sub x, y)) -> (sub y, x)
1609 if (N->getOperand(0).getOpcode() == ISD::SUB)
1610 return DAG.getNode(ISD::SUB, N->getValueType(0), N->getOperand(1),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001611 N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001612 // fold (neg (neg x)) -> x
1613 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001614 return N->getOperand(0).getOperand(0);
1615 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001616}
1617
Nate Begeman83e75ec2005-09-06 04:43:02 +00001618SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001619 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001620 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001621 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001622 return DAG.getConstantFP(fabs(N0CFP->getValue()), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001623 // fold (fabs (fabs x)) -> (fabs x)
1624 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001625 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001626 // fold (fabs (fneg x)) -> (fabs x)
1627 if (N->getOperand(0).getOpcode() == ISD::FNEG)
1628 return DAG.getNode(ISD::FABS, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001629 N->getOperand(0).getOperand(0));
1630 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001631}
1632
Nate Begeman44728a72005-09-19 22:34:01 +00001633SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
1634 SDOperand Chain = N->getOperand(0);
1635 SDOperand N1 = N->getOperand(1);
1636 SDOperand N2 = N->getOperand(2);
1637 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1638
1639 // never taken branch, fold to chain
1640 if (N1C && N1C->isNullValue())
1641 return Chain;
1642 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00001643 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00001644 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1645 return SDOperand();
1646}
1647
1648SDOperand DAGCombiner::visitBRCONDTWOWAY(SDNode *N) {
1649 SDOperand Chain = N->getOperand(0);
1650 SDOperand N1 = N->getOperand(1);
1651 SDOperand N2 = N->getOperand(2);
1652 SDOperand N3 = N->getOperand(3);
1653 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1654
1655 // unconditional branch to true mbb
1656 if (N1C && N1C->getValue() == 1)
1657 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1658 // unconditional branch to false mbb
1659 if (N1C && N1C->isNullValue())
1660 return DAG.getNode(ISD::BR, MVT::Other, Chain, N3);
1661 return SDOperand();
1662}
1663
Chris Lattner3ea0b472005-10-05 06:47:48 +00001664// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
1665//
Nate Begeman44728a72005-09-19 22:34:01 +00001666SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00001667 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
1668 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
1669
1670 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00001671 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
1672 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
1673
1674 // fold br_cc true, dest -> br dest (unconditional branch)
1675 if (SCCC && SCCC->getValue())
1676 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
1677 N->getOperand(4));
1678 // fold br_cc false, dest -> unconditional fall through
1679 if (SCCC && SCCC->isNullValue())
1680 return N->getOperand(0);
1681 // fold to a simpler setcc
1682 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
1683 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
1684 Simp.getOperand(2), Simp.getOperand(0),
1685 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00001686 return SDOperand();
1687}
1688
1689SDOperand DAGCombiner::visitBRTWOWAY_CC(SDNode *N) {
Nate Begemane17daeb2005-10-05 21:43:42 +00001690 SDOperand Chain = N->getOperand(0);
1691 SDOperand CCN = N->getOperand(1);
1692 SDOperand LHS = N->getOperand(2);
1693 SDOperand RHS = N->getOperand(3);
1694 SDOperand N4 = N->getOperand(4);
1695 SDOperand N5 = N->getOperand(5);
1696
1697 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), LHS, RHS,
1698 cast<CondCodeSDNode>(CCN)->get(), false);
1699 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1700
1701 // fold select_cc lhs, rhs, x, x, cc -> x
1702 if (N4 == N5)
1703 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
1704 // fold select_cc true, x, y -> x
1705 if (SCCC && SCCC->getValue())
1706 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
1707 // fold select_cc false, x, y -> y
1708 if (SCCC && SCCC->isNullValue())
1709 return DAG.getNode(ISD::BR, MVT::Other, Chain, N5);
1710 // fold to a simpler setcc
1711 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
1712 return DAG.getBR2Way_CC(Chain, SCC.getOperand(2), SCC.getOperand(0),
1713 SCC.getOperand(1), N4, N5);
Nate Begeman44728a72005-09-19 22:34:01 +00001714 return SDOperand();
1715}
1716
Chris Lattner01a22022005-10-10 22:04:48 +00001717SDOperand DAGCombiner::visitLOAD(SDNode *N) {
1718 SDOperand Chain = N->getOperand(0);
1719 SDOperand Ptr = N->getOperand(1);
1720 SDOperand SrcValue = N->getOperand(2);
1721
1722 // If this load is directly stored, replace the load value with the stored
1723 // value.
1724 // TODO: Handle store large -> read small portion.
1725 // TODO: Handle TRUNCSTORE/EXTLOAD
1726 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
1727 Chain.getOperand(1).getValueType() == N->getValueType(0))
1728 return CombineTo(N, Chain.getOperand(1), Chain);
1729
1730 return SDOperand();
1731}
1732
Chris Lattner87514ca2005-10-10 22:31:19 +00001733SDOperand DAGCombiner::visitSTORE(SDNode *N) {
1734 SDOperand Chain = N->getOperand(0);
1735 SDOperand Value = N->getOperand(1);
1736 SDOperand Ptr = N->getOperand(2);
1737 SDOperand SrcValue = N->getOperand(3);
1738
1739 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00001740 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
1741 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */) {
Chris Lattner87514ca2005-10-10 22:31:19 +00001742 // Create a new store of Value that replaces both stores.
1743 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00001744 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
1745 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00001746 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
1747 PrevStore->getOperand(0), Value, Ptr,
1748 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00001749 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00001750 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00001751 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00001752 }
1753
1754 return SDOperand();
1755}
1756
Nate Begeman44728a72005-09-19 22:34:01 +00001757SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00001758 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
1759
1760 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
1761 cast<CondCodeSDNode>(N0.getOperand(2))->get());
1762 // If we got a simplified select_cc node back from SimplifySelectCC, then
1763 // break it down into a new SETCC node, and a new SELECT node, and then return
1764 // the SELECT node, since we were called with a SELECT node.
1765 if (SCC.Val) {
1766 // Check to see if we got a select_cc back (to turn into setcc/select).
1767 // Otherwise, just return whatever node we got back, like fabs.
1768 if (SCC.getOpcode() == ISD::SELECT_CC) {
1769 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
1770 SCC.getOperand(0), SCC.getOperand(1),
1771 SCC.getOperand(4));
1772 WorkList.push_back(SETCC.Val);
1773 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
1774 SCC.getOperand(3), SETCC);
1775 }
1776 return SCC;
1777 }
Nate Begeman44728a72005-09-19 22:34:01 +00001778 return SDOperand();
1779}
1780
1781SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
1782 SDOperand N2, SDOperand N3,
1783 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00001784
1785 MVT::ValueType VT = N2.getValueType();
1786 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1787 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1788 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1789 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1790
1791 // Determine if the condition we're dealing with is constant
1792 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
1793 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1794
1795 // fold select_cc true, x, y -> x
1796 if (SCCC && SCCC->getValue())
1797 return N2;
1798 // fold select_cc false, x, y -> y
1799 if (SCCC && SCCC->getValue() == 0)
1800 return N3;
1801
1802 // Check to see if we can simplify the select into an fabs node
1803 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1804 // Allow either -0.0 or 0.0
1805 if (CFP->getValue() == 0.0) {
1806 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
1807 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
1808 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
1809 N2 == N3.getOperand(0))
1810 return DAG.getNode(ISD::FABS, VT, N0);
1811
1812 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
1813 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
1814 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
1815 N2.getOperand(0) == N3)
1816 return DAG.getNode(ISD::FABS, VT, N3);
1817 }
1818 }
1819
1820 // Check to see if we can perform the "gzip trick", transforming
1821 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
1822 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
1823 MVT::isInteger(N0.getValueType()) &&
1824 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
1825 MVT::ValueType XType = N0.getValueType();
1826 MVT::ValueType AType = N2.getValueType();
1827 if (XType >= AType) {
1828 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00001829 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00001830 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
1831 unsigned ShCtV = Log2_64(N2C->getValue());
1832 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
1833 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
1834 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
1835 WorkList.push_back(Shift.Val);
1836 if (XType > AType) {
1837 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
1838 WorkList.push_back(Shift.Val);
1839 }
1840 return DAG.getNode(ISD::AND, AType, Shift, N2);
1841 }
1842 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
1843 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1844 TLI.getShiftAmountTy()));
1845 WorkList.push_back(Shift.Val);
1846 if (XType > AType) {
1847 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
1848 WorkList.push_back(Shift.Val);
1849 }
1850 return DAG.getNode(ISD::AND, AType, Shift, N2);
1851 }
1852 }
Nate Begeman07ed4172005-10-10 21:26:48 +00001853
1854 // fold select C, 16, 0 -> shl C, 4
1855 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
1856 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
1857 // Get a SetCC of the condition
1858 // FIXME: Should probably make sure that setcc is legal if we ever have a
1859 // target where it isn't.
1860 SDOperand Temp, SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
1861 WorkList.push_back(SCC.Val);
1862 // cast from setcc result type to select result type
1863 if (AfterLegalize)
1864 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
1865 else
1866 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
1867 WorkList.push_back(Temp.Val);
1868 // shl setcc result by log2 n2c
1869 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
1870 DAG.getConstant(Log2_64(N2C->getValue()),
1871 TLI.getShiftAmountTy()));
1872 }
1873
Nate Begemanf845b452005-10-08 00:29:44 +00001874 // Check to see if this is the equivalent of setcc
1875 // FIXME: Turn all of these into setcc if setcc if setcc is legal
1876 // otherwise, go ahead with the folds.
1877 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
1878 MVT::ValueType XType = N0.getValueType();
1879 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
1880 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
1881 if (Res.getValueType() != VT)
1882 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
1883 return Res;
1884 }
1885
1886 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
1887 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
1888 TLI.isOperationLegal(ISD::CTLZ, XType)) {
1889 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
1890 return DAG.getNode(ISD::SRL, XType, Ctlz,
1891 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
1892 TLI.getShiftAmountTy()));
1893 }
1894 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
1895 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
1896 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
1897 N0);
1898 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
1899 DAG.getConstant(~0ULL, XType));
1900 return DAG.getNode(ISD::SRL, XType,
1901 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
1902 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1903 TLI.getShiftAmountTy()));
1904 }
1905 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
1906 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
1907 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
1908 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1909 TLI.getShiftAmountTy()));
1910 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
1911 }
1912 }
1913
1914 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
1915 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
1916 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
1917 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
1918 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
1919 MVT::ValueType XType = N0.getValueType();
1920 if (SubC->isNullValue() && MVT::isInteger(XType)) {
1921 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
1922 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1923 TLI.getShiftAmountTy()));
1924 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
1925 WorkList.push_back(Shift.Val);
1926 WorkList.push_back(Add.Val);
1927 return DAG.getNode(ISD::XOR, XType, Add, Shift);
1928 }
1929 }
1930 }
1931
Nate Begeman44728a72005-09-19 22:34:01 +00001932 return SDOperand();
1933}
1934
Nate Begeman452d7be2005-09-16 00:54:12 +00001935SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00001936 SDOperand N1, ISD::CondCode Cond,
1937 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001938 // These setcc operations always fold.
1939 switch (Cond) {
1940 default: break;
1941 case ISD::SETFALSE:
1942 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1943 case ISD::SETTRUE:
1944 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
1945 }
1946
1947 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1948 uint64_t C1 = N1C->getValue();
1949 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
1950 uint64_t C0 = N0C->getValue();
1951
1952 // Sign extend the operands if required
1953 if (ISD::isSignedIntSetCC(Cond)) {
1954 C0 = N0C->getSignExtended();
1955 C1 = N1C->getSignExtended();
1956 }
1957
1958 switch (Cond) {
1959 default: assert(0 && "Unknown integer setcc!");
1960 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
1961 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
1962 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
1963 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
1964 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
1965 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
1966 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
1967 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
1968 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
1969 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
1970 }
1971 } else {
1972 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
1973 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
1974 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
1975
1976 // If the comparison constant has bits in the upper part, the
1977 // zero-extended value could never match.
1978 if (C1 & (~0ULL << InSize)) {
1979 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
1980 switch (Cond) {
1981 case ISD::SETUGT:
1982 case ISD::SETUGE:
1983 case ISD::SETEQ: return DAG.getConstant(0, VT);
1984 case ISD::SETULT:
1985 case ISD::SETULE:
1986 case ISD::SETNE: return DAG.getConstant(1, VT);
1987 case ISD::SETGT:
1988 case ISD::SETGE:
1989 // True if the sign bit of C1 is set.
1990 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
1991 case ISD::SETLT:
1992 case ISD::SETLE:
1993 // True if the sign bit of C1 isn't set.
1994 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
1995 default:
1996 break;
1997 }
1998 }
1999
2000 // Otherwise, we can perform the comparison with the low bits.
2001 switch (Cond) {
2002 case ISD::SETEQ:
2003 case ISD::SETNE:
2004 case ISD::SETUGT:
2005 case ISD::SETUGE:
2006 case ISD::SETULT:
2007 case ISD::SETULE:
2008 return DAG.getSetCC(VT, N0.getOperand(0),
2009 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
2010 Cond);
2011 default:
2012 break; // todo, be more careful with signed comparisons
2013 }
2014 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2015 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2016 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
2017 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
2018 MVT::ValueType ExtDstTy = N0.getValueType();
2019 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
2020
2021 // If the extended part has any inconsistent bits, it cannot ever
2022 // compare equal. In other words, they have to be all ones or all
2023 // zeros.
2024 uint64_t ExtBits =
2025 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
2026 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
2027 return DAG.getConstant(Cond == ISD::SETNE, VT);
2028
2029 SDOperand ZextOp;
2030 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
2031 if (Op0Ty == ExtSrcTy) {
2032 ZextOp = N0.getOperand(0);
2033 } else {
2034 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
2035 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
2036 DAG.getConstant(Imm, Op0Ty));
2037 }
2038 WorkList.push_back(ZextOp.Val);
2039 // Otherwise, make this a use of a zext.
2040 return DAG.getSetCC(VT, ZextOp,
2041 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
2042 ExtDstTy),
2043 Cond);
2044 }
Chris Lattner5c46f742005-10-05 06:11:08 +00002045
Nate Begeman452d7be2005-09-16 00:54:12 +00002046 uint64_t MinVal, MaxVal;
2047 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
2048 if (ISD::isSignedIntSetCC(Cond)) {
2049 MinVal = 1ULL << (OperandBitSize-1);
2050 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
2051 MaxVal = ~0ULL >> (65-OperandBitSize);
2052 else
2053 MaxVal = 0;
2054 } else {
2055 MinVal = 0;
2056 MaxVal = ~0ULL >> (64-OperandBitSize);
2057 }
2058
2059 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
2060 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
2061 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
2062 --C1; // X >= C0 --> X > (C0-1)
2063 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2064 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
2065 }
2066
2067 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
2068 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
2069 ++C1; // X <= C0 --> X < (C0+1)
2070 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2071 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
2072 }
2073
2074 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
2075 return DAG.getConstant(0, VT); // X < MIN --> false
2076
2077 // Canonicalize setgt X, Min --> setne X, Min
2078 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
2079 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
2080
2081 // If we have setult X, 1, turn it into seteq X, 0
2082 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
2083 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
2084 ISD::SETEQ);
2085 // If we have setugt X, Max-1, turn it into seteq X, Max
2086 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
2087 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
2088 ISD::SETEQ);
2089
2090 // If we have "setcc X, C0", check to see if we can shrink the immediate
2091 // by changing cc.
2092
2093 // SETUGT X, SINTMAX -> SETLT X, 0
2094 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
2095 C1 == (~0ULL >> (65-OperandBitSize)))
2096 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
2097 ISD::SETLT);
2098
2099 // FIXME: Implement the rest of these.
2100
2101 // Fold bit comparisons when we can.
2102 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2103 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
2104 if (ConstantSDNode *AndRHS =
2105 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2106 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
2107 // Perform the xform if the AND RHS is a single bit.
2108 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
2109 return DAG.getNode(ISD::SRL, VT, N0,
2110 DAG.getConstant(Log2_64(AndRHS->getValue()),
2111 TLI.getShiftAmountTy()));
2112 }
2113 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
2114 // (X & 8) == 8 --> (X & 8) >> 3
2115 // Perform the xform if C1 is a single bit.
2116 if ((C1 & (C1-1)) == 0) {
2117 return DAG.getNode(ISD::SRL, VT, N0,
2118 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
2119 }
2120 }
2121 }
2122 }
2123 } else if (isa<ConstantSDNode>(N0.Val)) {
2124 // Ensure that the constant occurs on the RHS.
2125 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2126 }
2127
2128 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
2129 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
2130 double C0 = N0C->getValue(), C1 = N1C->getValue();
2131
2132 switch (Cond) {
2133 default: break; // FIXME: Implement the rest of these!
2134 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2135 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2136 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
2137 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
2138 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
2139 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
2140 }
2141 } else {
2142 // Ensure that the constant occurs on the RHS.
2143 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2144 }
2145
2146 if (N0 == N1) {
2147 // We can always fold X == Y for integer setcc's.
2148 if (MVT::isInteger(N0.getValueType()))
2149 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2150 unsigned UOF = ISD::getUnorderedFlavor(Cond);
2151 if (UOF == 2) // FP operators that are undefined on NaNs.
2152 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2153 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2154 return DAG.getConstant(UOF, VT);
2155 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
2156 // if it is not already.
2157 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
2158 if (NewCond != Cond)
2159 return DAG.getSetCC(VT, N0, N1, NewCond);
2160 }
2161
2162 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2163 MVT::isInteger(N0.getValueType())) {
2164 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2165 N0.getOpcode() == ISD::XOR) {
2166 // Simplify (X+Y) == (X+Z) --> Y == Z
2167 if (N0.getOpcode() == N1.getOpcode()) {
2168 if (N0.getOperand(0) == N1.getOperand(0))
2169 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
2170 if (N0.getOperand(1) == N1.getOperand(1))
2171 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
2172 if (isCommutativeBinOp(N0.getOpcode())) {
2173 // If X op Y == Y op X, try other combinations.
2174 if (N0.getOperand(0) == N1.getOperand(1))
2175 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
2176 if (N0.getOperand(1) == N1.getOperand(0))
2177 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
2178 }
2179 }
2180
Chris Lattner5c46f742005-10-05 06:11:08 +00002181 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0. Common for condcodes.
2182 if (N0.getOpcode() == ISD::XOR)
2183 if (ConstantSDNode *XORC = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2184 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2185 // If we know that all of the inverted bits are zero, don't bother
2186 // performing the inversion.
2187 if (MaskedValueIsZero(N0.getOperand(0), ~XORC->getValue(), TLI))
2188 return DAG.getSetCC(VT, N0.getOperand(0),
2189 DAG.getConstant(XORC->getValue()^RHSC->getValue(),
2190 N0.getValueType()), Cond);
2191 }
2192
Nate Begeman452d7be2005-09-16 00:54:12 +00002193 // Simplify (X+Z) == X --> Z == 0
2194 if (N0.getOperand(0) == N1)
2195 return DAG.getSetCC(VT, N0.getOperand(1),
2196 DAG.getConstant(0, N0.getValueType()), Cond);
2197 if (N0.getOperand(1) == N1) {
2198 if (isCommutativeBinOp(N0.getOpcode()))
2199 return DAG.getSetCC(VT, N0.getOperand(0),
2200 DAG.getConstant(0, N0.getValueType()), Cond);
2201 else {
2202 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2203 // (Z-X) == X --> Z == X<<1
2204 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
2205 N1,
2206 DAG.getConstant(1,TLI.getShiftAmountTy()));
2207 WorkList.push_back(SH.Val);
2208 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
2209 }
2210 }
2211 }
2212
2213 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2214 N1.getOpcode() == ISD::XOR) {
2215 // Simplify X == (X+Z) --> Z == 0
2216 if (N1.getOperand(0) == N0) {
2217 return DAG.getSetCC(VT, N1.getOperand(1),
2218 DAG.getConstant(0, N1.getValueType()), Cond);
2219 } else if (N1.getOperand(1) == N0) {
2220 if (isCommutativeBinOp(N1.getOpcode())) {
2221 return DAG.getSetCC(VT, N1.getOperand(0),
2222 DAG.getConstant(0, N1.getValueType()), Cond);
2223 } else {
2224 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2225 // X == (Z-X) --> X<<1 == Z
2226 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
2227 DAG.getConstant(1,TLI.getShiftAmountTy()));
2228 WorkList.push_back(SH.Val);
2229 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
2230 }
2231 }
2232 }
2233 }
2234
2235 // Fold away ALL boolean setcc's.
2236 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00002237 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002238 switch (Cond) {
2239 default: assert(0 && "Unknown integer setcc!");
2240 case ISD::SETEQ: // X == Y -> (X^Y)^1
2241 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2242 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
2243 WorkList.push_back(Temp.Val);
2244 break;
2245 case ISD::SETNE: // X != Y --> (X^Y)
2246 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2247 break;
2248 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
2249 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
2250 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2251 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
2252 WorkList.push_back(Temp.Val);
2253 break;
2254 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
2255 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
2256 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2257 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
2258 WorkList.push_back(Temp.Val);
2259 break;
2260 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
2261 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
2262 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2263 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
2264 WorkList.push_back(Temp.Val);
2265 break;
2266 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
2267 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
2268 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2269 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
2270 break;
2271 }
2272 if (VT != MVT::i1) {
2273 WorkList.push_back(N0.Val);
2274 // FIXME: If running after legalize, we probably can't do this.
2275 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2276 }
2277 return N0;
2278 }
2279
2280 // Could not fold it.
2281 return SDOperand();
2282}
2283
Nate Begeman1d4d4142005-09-01 00:19:25 +00002284// SelectionDAG::Combine - This is the entry point for the file.
2285//
Nate Begeman4ebd8052005-09-01 23:24:04 +00002286void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002287 /// run - This is the main entry point to this class.
2288 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00002289 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002290}