blob: ed2af3c590b5275b7927805a3c7786b35af85b0c [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, 16, 0 -> shr C, 4
24// FIXME: select C, pow2, pow2 -> something smart
25// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
26// FIXME: (select C, load A, load B) -> load (select C, A, B)
27// FIXME: store -> load -> forward substitute
28// FIXME: Dead stores -> nuke
29// FIXME: shr X, (and Y,31) -> shr X, Y
30// FIXME: TRUNC (LOAD) -> EXT_LOAD/LOAD(smaller)
Nate Begeman1d4d4142005-09-01 00:19:25 +000031// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000032// FIXME: undef values
Nate Begeman1d4d4142005-09-01 00:19:25 +000033// FIXME: zero extend when top bits are 0 -> drop it ?
Nate Begeman4ebd8052005-09-01 23:24:04 +000034// FIXME: make truncate see through SIGN_EXTEND and AND
35// FIXME: sext_in_reg(setcc) on targets that return zero or one, and where
36// EVT != MVT::i1 can drop the sext.
Nate Begeman4ebd8052005-09-01 23:24:04 +000037// FIXME: (sra (sra x, c1), c2) -> (sra x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +000038// FIXME: verify that getNode can't return extends with an operand whose type
39// is >= to that of the extend.
40// FIXME: divide by zero is currently left unfolded. do we want to turn this
41// into an undef?
Nate Begeman1d4d4142005-09-01 00:19:25 +000042//
43//===----------------------------------------------------------------------===//
44
45#define DEBUG_TYPE "dagcombine"
46#include "llvm/ADT/Statistic.h"
47#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000048#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000049#include "llvm/Support/MathExtras.h"
50#include "llvm/Target/TargetLowering.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000051#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000052#include <cmath>
53using namespace llvm;
54
55namespace {
56 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
57
58 class DAGCombiner {
59 SelectionDAG &DAG;
60 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000061 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000062
63 // Worklist of all of the nodes that need to be simplified.
64 std::vector<SDNode*> WorkList;
65
66 /// AddUsersToWorkList - When an instruction is simplified, add all users of
67 /// the instruction to the work lists because they might get more simplified
68 /// now.
69 ///
70 void AddUsersToWorkList(SDNode *N) {
71 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000072 UI != UE; ++UI)
73 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000074 }
75
76 /// removeFromWorkList - remove all instances of N from the worklist.
77 void removeFromWorkList(SDNode *N) {
78 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
79 WorkList.end());
80 }
81
82 /// visit - call the node-specific routine that knows how to fold each
83 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +000084 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +000085
86 // Visitation implementation - Implement dag node combining for different
87 // node types. The semantics are as follows:
88 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +000089 // SDOperand.Val == 0 - No change was made
90 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +000091 //
Nate Begeman83e75ec2005-09-06 04:43:02 +000092 SDOperand visitTokenFactor(SDNode *N);
93 SDOperand visitADD(SDNode *N);
94 SDOperand visitSUB(SDNode *N);
95 SDOperand visitMUL(SDNode *N);
96 SDOperand visitSDIV(SDNode *N);
97 SDOperand visitUDIV(SDNode *N);
98 SDOperand visitSREM(SDNode *N);
99 SDOperand visitUREM(SDNode *N);
100 SDOperand visitMULHU(SDNode *N);
101 SDOperand visitMULHS(SDNode *N);
102 SDOperand visitAND(SDNode *N);
103 SDOperand visitOR(SDNode *N);
104 SDOperand visitXOR(SDNode *N);
105 SDOperand visitSHL(SDNode *N);
106 SDOperand visitSRA(SDNode *N);
107 SDOperand visitSRL(SDNode *N);
108 SDOperand visitCTLZ(SDNode *N);
109 SDOperand visitCTTZ(SDNode *N);
110 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000111 SDOperand visitSELECT(SDNode *N);
112 SDOperand visitSELECT_CC(SDNode *N);
113 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000114 SDOperand visitSIGN_EXTEND(SDNode *N);
115 SDOperand visitZERO_EXTEND(SDNode *N);
116 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
117 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000118
119 SDOperand visitFADD(SDNode *N);
120 SDOperand visitFSUB(SDNode *N);
121 SDOperand visitFMUL(SDNode *N);
122 SDOperand visitFDIV(SDNode *N);
123 SDOperand visitFREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000124 SDOperand visitSINT_TO_FP(SDNode *N);
125 SDOperand visitUINT_TO_FP(SDNode *N);
126 SDOperand visitFP_TO_SINT(SDNode *N);
127 SDOperand visitFP_TO_UINT(SDNode *N);
128 SDOperand visitFP_ROUND(SDNode *N);
129 SDOperand visitFP_ROUND_INREG(SDNode *N);
130 SDOperand visitFP_EXTEND(SDNode *N);
131 SDOperand visitFNEG(SDNode *N);
132 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000133 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000134 SDOperand visitBRCONDTWOWAY(SDNode *N);
135 SDOperand visitBR_CC(SDNode *N);
136 SDOperand visitBRTWOWAY_CC(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000137
Nate Begeman44728a72005-09-19 22:34:01 +0000138 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
139 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
140 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000141 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
142 ISD::CondCode Cond);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000143public:
144 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000145 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000146
147 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000148 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000149 };
150}
151
152/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
153/// this predicate to simplify operations downstream. V and Mask are known to
154/// be the same type.
155static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
156 const TargetLowering &TLI) {
157 unsigned SrcBits;
158 if (Mask == 0) return true;
159
160 // If we know the result of a setcc has the top bits zero, use this info.
161 switch (Op.getOpcode()) {
Nate Begeman4ebd8052005-09-01 23:24:04 +0000162 case ISD::Constant:
163 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
164 case ISD::SETCC:
Nate Begeman646d7e22005-09-02 21:18:40 +0000165 // FIXME: teach this about non ZeroOrOne values, such as 0 or -1
Nate Begeman4ebd8052005-09-01 23:24:04 +0000166 return ((Mask & 1) == 0) &&
167 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
168 case ISD::ZEXTLOAD:
169 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
170 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
171 case ISD::ZERO_EXTEND:
172 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
173 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
174 case ISD::AssertZext:
175 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
176 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
177 case ISD::AND:
178 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
179 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
180 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
181 // FALL THROUGH
182 case ISD::OR:
183 case ISD::XOR:
184 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
185 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
186 case ISD::SELECT:
187 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
188 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
189 case ISD::SELECT_CC:
190 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
191 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
192 case ISD::SRL:
193 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
194 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
195 uint64_t NewVal = Mask << ShAmt->getValue();
196 SrcBits = MVT::getSizeInBits(Op.getValueType());
197 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
198 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
199 }
200 return false;
201 case ISD::SHL:
202 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
203 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
204 uint64_t NewVal = Mask >> ShAmt->getValue();
205 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
206 }
207 return false;
208 case ISD::CTTZ:
209 case ISD::CTLZ:
210 case ISD::CTPOP:
211 // Bit counting instructions can not set the high bits of the result
212 // register. The max number of bits sets depends on the input.
213 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
214
215 // TODO we could handle some SRA cases here.
216 default: break;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000217 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000218 return false;
219}
220
Nate Begeman4ebd8052005-09-01 23:24:04 +0000221// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
222// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000223// Also, set the incoming LHS, RHS, and CC references to the appropriate
224// nodes based on the type of node we are checking. This simplifies life a
225// bit for the callers.
226static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
227 SDOperand &CC) {
228 if (N.getOpcode() == ISD::SETCC) {
229 LHS = N.getOperand(0);
230 RHS = N.getOperand(1);
231 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000232 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000233 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000234 if (N.getOpcode() == ISD::SELECT_CC &&
235 N.getOperand(2).getOpcode() == ISD::Constant &&
236 N.getOperand(3).getOpcode() == ISD::Constant &&
237 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000238 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
239 LHS = N.getOperand(0);
240 RHS = N.getOperand(1);
241 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000242 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000243 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000244 return false;
245}
246
Nate Begeman99801192005-09-07 23:25:52 +0000247// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
248// one use. If this is true, it allows the users to invert the operation for
249// free when it is profitable to do so.
250static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000251 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000252 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000253 return true;
254 return false;
255}
256
Nate Begeman452d7be2005-09-16 00:54:12 +0000257// FIXME: This should probably go in the ISD class rather than being duplicated
258// in several files.
259static bool isCommutativeBinOp(unsigned Opcode) {
260 switch (Opcode) {
261 case ISD::ADD:
262 case ISD::MUL:
263 case ISD::AND:
264 case ISD::OR:
265 case ISD::XOR: return true;
266 default: return false; // FIXME: Need commutative info for user ops!
267 }
268}
269
Nate Begeman4ebd8052005-09-01 23:24:04 +0000270void DAGCombiner::Run(bool RunningAfterLegalize) {
271 // set the instance variable, so that the various visit routines may use it.
272 AfterLegalize = RunningAfterLegalize;
273
Nate Begeman646d7e22005-09-02 21:18:40 +0000274 // Add all the dag nodes to the worklist.
275 WorkList.insert(WorkList.end(), DAG.allnodes_begin(), DAG.allnodes_end());
Nate Begeman83e75ec2005-09-06 04:43:02 +0000276
Nate Begeman1d4d4142005-09-01 00:19:25 +0000277 // while the worklist isn't empty, inspect the node on the end of it and
278 // try and combine it.
279 while (!WorkList.empty()) {
280 SDNode *N = WorkList.back();
281 WorkList.pop_back();
282
283 // If N has no uses, it is dead. Make sure to revisit all N's operands once
284 // N is deleted from the DAG, since they too may now be dead.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000285 // FIXME: is there a better way to keep from deleting the dag root because
286 // we think it has no uses? This works for now...
287 if (N->use_empty() && N != DAG.getRoot().Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000288 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
289 WorkList.push_back(N->getOperand(i).Val);
290
291 DAG.DeleteNode(N);
292 removeFromWorkList(N);
293 continue;
294 }
295
Nate Begeman83e75ec2005-09-06 04:43:02 +0000296 SDOperand RV = visit(N);
297 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000298 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000299 // If we get back the same node we passed in, rather than a new node or
300 // zero, we know that the node must have defined multiple values and
301 // CombineTo was used. Since CombineTo takes care of the worklist
302 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000303 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000304 DEBUG(std::cerr << "\nReplacing "; N->dump();
305 std::cerr << "\nWith: "; RV.Val->dump();
306 std::cerr << '\n');
Nate Begeman99801192005-09-07 23:25:52 +0000307 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV));
Nate Begeman646d7e22005-09-02 21:18:40 +0000308
309 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000310 WorkList.push_back(RV.Val);
311 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000312
313 // Nodes can end up on the worklist more than once. Make sure we do
314 // not process a node that has been replaced.
315 removeFromWorkList(N);
Chris Lattner5c46f742005-10-05 06:11:08 +0000316
317 // Finally, since the node is now dead, remove it from the graph.
318 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000319 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000320 }
321 }
322}
323
Nate Begeman83e75ec2005-09-06 04:43:02 +0000324SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000325 switch(N->getOpcode()) {
326 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000327 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000328 case ISD::ADD: return visitADD(N);
329 case ISD::SUB: return visitSUB(N);
330 case ISD::MUL: return visitMUL(N);
331 case ISD::SDIV: return visitSDIV(N);
332 case ISD::UDIV: return visitUDIV(N);
333 case ISD::SREM: return visitSREM(N);
334 case ISD::UREM: return visitUREM(N);
335 case ISD::MULHU: return visitMULHU(N);
336 case ISD::MULHS: return visitMULHS(N);
337 case ISD::AND: return visitAND(N);
338 case ISD::OR: return visitOR(N);
339 case ISD::XOR: return visitXOR(N);
340 case ISD::SHL: return visitSHL(N);
341 case ISD::SRA: return visitSRA(N);
342 case ISD::SRL: return visitSRL(N);
343 case ISD::CTLZ: return visitCTLZ(N);
344 case ISD::CTTZ: return visitCTTZ(N);
345 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000346 case ISD::SELECT: return visitSELECT(N);
347 case ISD::SELECT_CC: return visitSELECT_CC(N);
348 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000349 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
350 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
351 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
352 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000353 case ISD::FADD: return visitFADD(N);
354 case ISD::FSUB: return visitFSUB(N);
355 case ISD::FMUL: return visitFMUL(N);
356 case ISD::FDIV: return visitFDIV(N);
357 case ISD::FREM: return visitFREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000358 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
359 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
360 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
361 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
362 case ISD::FP_ROUND: return visitFP_ROUND(N);
363 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
364 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
365 case ISD::FNEG: return visitFNEG(N);
366 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000367 case ISD::BRCOND: return visitBRCOND(N);
368 case ISD::BRCONDTWOWAY: return visitBRCONDTWOWAY(N);
369 case ISD::BR_CC: return visitBR_CC(N);
370 case ISD::BRTWOWAY_CC: return visitBRTWOWAY_CC(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000371 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000372 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000373}
374
Nate Begeman83e75ec2005-09-06 04:43:02 +0000375SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000376 // If the token factor has two operands and one is the entry token, replace
377 // the token factor with the other operand.
378 if (N->getNumOperands() == 2) {
379 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000380 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000381 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000382 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000383 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000384 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000385}
386
Nate Begeman83e75ec2005-09-06 04:43:02 +0000387SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000388 SDOperand N0 = N->getOperand(0);
389 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000390 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
391 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000392 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000393
394 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000395 if (N0C && N1C)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000396 return DAG.getConstant(N0C->getValue() + N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000397 // canonicalize constant to RHS
398 if (N0C && !N1C) {
399 std::swap(N0, N1);
400 std::swap(N0C, N1C);
401 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000402 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000403 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000404 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000405 // fold (add (add x, c1), c2) -> (add x, c1+c2)
406 if (N1C && N0.getOpcode() == ISD::ADD) {
407 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
408 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
409 if (N00C)
410 return DAG.getNode(ISD::ADD, VT, N0.getOperand(1),
411 DAG.getConstant(N1C->getValue()+N00C->getValue(), VT));
412 if (N01C)
413 return DAG.getNode(ISD::ADD, VT, N0.getOperand(0),
414 DAG.getConstant(N1C->getValue()+N01C->getValue(), VT));
415 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000416 // fold ((0-A) + B) -> B-A
417 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
418 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000419 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000420 // fold (A + (0-B)) -> A-B
421 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
422 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000423 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000424 // fold (A+(B-A)) -> B
425 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000426 return N1.getOperand(0);
427 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000428}
429
Nate Begeman83e75ec2005-09-06 04:43:02 +0000430SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000431 SDOperand N0 = N->getOperand(0);
432 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000433 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
434 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000435
436 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000437 if (N0C && N1C)
438 return DAG.getConstant(N0C->getValue() - N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000439 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000440 // fold (sub x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000441 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000442 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000443 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000444 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000445 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000446 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000447 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000448 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000449 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000450}
451
Nate Begeman83e75ec2005-09-06 04:43:02 +0000452SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000453 SDOperand N0 = N->getOperand(0);
454 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000455 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
456 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000457 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000458
459 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000460 if (N0C && N1C)
461 return DAG.getConstant(N0C->getValue() * N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000462 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000463 // canonicalize constant to RHS
464 if (N0C && !N1C) {
465 std::swap(N0, N1);
466 std::swap(N0C, N1C);
467 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000468 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000469 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000470 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000471 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000472 if (N1C && N1C->isAllOnesValue())
Nate Begeman1d4d4142005-09-01 00:19:25 +0000473 return DAG.getNode(ISD::SUB, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000474 DAG.getConstant(0, N->getValueType(0)), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000475 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000476 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000477 return DAG.getNode(ISD::SHL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000478 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000479 TLI.getShiftAmountTy()));
Nate Begeman223df222005-09-08 20:18:10 +0000480 // fold (mul (mul x, c1), c2) -> (mul x, c1*c2)
481 if (N1C && N0.getOpcode() == ISD::MUL) {
482 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
483 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
484 if (N00C)
485 return DAG.getNode(ISD::MUL, VT, N0.getOperand(1),
486 DAG.getConstant(N1C->getValue()*N00C->getValue(), VT));
487 if (N01C)
488 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0),
489 DAG.getConstant(N1C->getValue()*N01C->getValue(), VT));
490 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000491 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000492}
493
Nate Begeman83e75ec2005-09-06 04:43:02 +0000494SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000495 SDOperand N0 = N->getOperand(0);
496 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000497 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
498 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000499
500 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000501 if (N0C && N1C && !N1C->isNullValue())
502 return DAG.getConstant(N0C->getSignExtended() / N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000503 N->getValueType(0));
Nate Begeman83e75ec2005-09-06 04:43:02 +0000504 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000505}
506
Nate Begeman83e75ec2005-09-06 04:43:02 +0000507SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000508 SDOperand N0 = N->getOperand(0);
509 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000510 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
511 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000512
513 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000514 if (N0C && N1C && !N1C->isNullValue())
515 return DAG.getConstant(N0C->getValue() / N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000516 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000517 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000518 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000519 return DAG.getNode(ISD::SRL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000520 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000521 TLI.getShiftAmountTy()));
522 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000523}
524
Nate Begeman83e75ec2005-09-06 04:43:02 +0000525SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000526 SDOperand N0 = N->getOperand(0);
527 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000528 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
529 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000530
531 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000532 if (N0C && N1C && !N1C->isNullValue())
533 return DAG.getConstant(N0C->getSignExtended() % N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000534 N->getValueType(0));
Nate Begeman83e75ec2005-09-06 04:43:02 +0000535 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000536}
537
Nate Begeman83e75ec2005-09-06 04:43:02 +0000538SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000539 SDOperand N0 = N->getOperand(0);
540 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000541 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
542 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000543
544 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000545 if (N0C && N1C && !N1C->isNullValue())
546 return DAG.getConstant(N0C->getValue() % N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000547 N->getValueType(0));
Nate Begeman646d7e22005-09-02 21:18:40 +0000548 // FIXME: c2 power of 2 -> mask?
Nate Begeman83e75ec2005-09-06 04:43:02 +0000549 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000550}
551
Nate Begeman83e75ec2005-09-06 04:43:02 +0000552SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000553 SDOperand N0 = N->getOperand(0);
554 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000555 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000556
557 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000558 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000559 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000560 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000561 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000562 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
563 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000564 TLI.getShiftAmountTy()));
565 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000566}
567
Nate Begeman83e75ec2005-09-06 04:43:02 +0000568SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000569 SDOperand N0 = N->getOperand(0);
570 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000571 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000572
573 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000574 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000575 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000576 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000577 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000578 return DAG.getConstant(0, N0.getValueType());
579 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000580}
581
Nate Begeman83e75ec2005-09-06 04:43:02 +0000582SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000583 SDOperand N0 = N->getOperand(0);
584 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000585 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000586 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
587 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000588 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000589 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000590
591 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000592 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000593 return DAG.getConstant(N0C->getValue() & N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000594 // canonicalize constant to RHS
595 if (N0C && !N1C) {
596 std::swap(N0, N1);
597 std::swap(N0C, N1C);
598 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000599 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000600 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000601 return N0;
602 // if (and x, c) is known to be zero, return 0
603 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
604 return DAG.getConstant(0, VT);
605 // fold (and x, c) -> x iff (x & ~c) == 0
606 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
607 TLI))
608 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000609 // fold (and (and x, c1), c2) -> (and x, c1^c2)
610 if (N1C && N0.getOpcode() == ISD::AND) {
611 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
612 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
613 if (N00C)
614 return DAG.getNode(ISD::AND, VT, N0.getOperand(1),
615 DAG.getConstant(N1C->getValue()&N00C->getValue(), VT));
616 if (N01C)
617 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
618 DAG.getConstant(N1C->getValue()&N01C->getValue(), VT));
619 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000620 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
621 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG) {
622 unsigned ExtendBits =
623 MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT());
Nate Begeman646d7e22005-09-02 21:18:40 +0000624 if ((N1C->getValue() & (~0ULL << ExtendBits)) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000625 return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000626 }
627 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
628 if (N0.getOpcode() == ISD::OR)
629 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000630 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000631 return N1;
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000632 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
633 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
634 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
635 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
636
637 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
638 MVT::isInteger(LL.getValueType())) {
639 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
640 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
641 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
642 WorkList.push_back(ORNode.Val);
643 return DAG.getSetCC(VT, ORNode, LR, Op1);
644 }
645 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
646 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
647 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
648 WorkList.push_back(ANDNode.Val);
649 return DAG.getSetCC(VT, ANDNode, LR, Op1);
650 }
651 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
652 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
653 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
654 WorkList.push_back(ORNode.Val);
655 return DAG.getSetCC(VT, ORNode, LR, Op1);
656 }
657 }
658 // canonicalize equivalent to ll == rl
659 if (LL == RR && LR == RL) {
660 Op1 = ISD::getSetCCSwappedOperands(Op1);
661 std::swap(RL, RR);
662 }
663 if (LL == RL && LR == RR) {
664 bool isInteger = MVT::isInteger(LL.getValueType());
665 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
666 if (Result != ISD::SETCC_INVALID)
667 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
668 }
669 }
670 // fold (and (zext x), (zext y)) -> (zext (and x, y))
671 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
672 N1.getOpcode() == ISD::ZERO_EXTEND &&
673 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
674 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
675 N0.getOperand(0), N1.getOperand(0));
676 WorkList.push_back(ANDNode.Val);
677 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
678 }
Nate Begeman452d7be2005-09-16 00:54:12 +0000679 // fold (and (shl/srl x), (shl/srl y)) -> (shl/srl (and x, y))
680 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
681 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL)) &&
682 N0.getOperand(1) == N1.getOperand(1)) {
683 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
684 N0.getOperand(0), N1.getOperand(0));
685 WorkList.push_back(ANDNode.Val);
686 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
687 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000688 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000689}
690
Nate Begeman83e75ec2005-09-06 04:43:02 +0000691SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000692 SDOperand N0 = N->getOperand(0);
693 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000694 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000695 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
696 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000697 MVT::ValueType VT = N1.getValueType();
698 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000699
700 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000701 if (N0C && N1C)
702 return DAG.getConstant(N0C->getValue() | N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000703 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000704 // canonicalize constant to RHS
705 if (N0C && !N1C) {
706 std::swap(N0, N1);
707 std::swap(N0C, N1C);
708 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000709 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000710 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000711 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000712 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000713 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000714 return N1;
715 // fold (or x, c) -> c iff (x & ~c) == 0
716 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
717 TLI))
718 return N1;
Nate Begeman223df222005-09-08 20:18:10 +0000719 // fold (or (or x, c1), c2) -> (or x, c1|c2)
720 if (N1C && N0.getOpcode() == ISD::OR) {
721 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
722 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
723 if (N00C)
724 return DAG.getNode(ISD::OR, VT, N0.getOperand(1),
725 DAG.getConstant(N1C->getValue()|N00C->getValue(), VT));
726 if (N01C)
727 return DAG.getNode(ISD::OR, VT, N0.getOperand(0),
728 DAG.getConstant(N1C->getValue()|N01C->getValue(), VT));
729 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000730 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
731 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
732 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
733 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
734
735 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
736 MVT::isInteger(LL.getValueType())) {
737 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
738 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
739 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
740 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
741 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
742 WorkList.push_back(ORNode.Val);
743 return DAG.getSetCC(VT, ORNode, LR, Op1);
744 }
745 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
746 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
747 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
748 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
749 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
750 WorkList.push_back(ANDNode.Val);
751 return DAG.getSetCC(VT, ANDNode, LR, Op1);
752 }
753 }
754 // canonicalize equivalent to ll == rl
755 if (LL == RR && LR == RL) {
756 Op1 = ISD::getSetCCSwappedOperands(Op1);
757 std::swap(RL, RR);
758 }
759 if (LL == RL && LR == RR) {
760 bool isInteger = MVT::isInteger(LL.getValueType());
761 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
762 if (Result != ISD::SETCC_INVALID)
763 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
764 }
765 }
766 // fold (or (zext x), (zext y)) -> (zext (or x, y))
767 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
768 N1.getOpcode() == ISD::ZERO_EXTEND &&
769 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
770 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
771 N0.getOperand(0), N1.getOperand(0));
772 WorkList.push_back(ORNode.Val);
773 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
774 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000775 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000776}
777
Nate Begeman83e75ec2005-09-06 04:43:02 +0000778SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000779 SDOperand N0 = N->getOperand(0);
780 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000781 SDOperand LHS, RHS, CC;
782 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
783 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000784 MVT::ValueType VT = N0.getValueType();
785
786 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000787 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000788 return DAG.getConstant(N0C->getValue() ^ N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000789 // canonicalize constant to RHS
790 if (N0C && !N1C) {
791 std::swap(N0, N1);
792 std::swap(N0C, N1C);
793 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000794 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000795 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000796 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000797 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +0000798 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
799 bool isInt = MVT::isInteger(LHS.getValueType());
800 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
801 isInt);
802 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000803 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000804 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000805 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000806 assert(0 && "Unhandled SetCC Equivalent!");
807 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000808 }
Nate Begeman99801192005-09-07 23:25:52 +0000809 // fold !(x or y) -> (!x and !y) iff x or y are setcc
810 if (N1C && N1C->getValue() == 1 &&
811 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000812 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000813 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
814 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000815 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
816 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000817 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
818 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000819 }
820 }
Nate Begeman99801192005-09-07 23:25:52 +0000821 // fold !(x or y) -> (!x and !y) iff x or y are constants
822 if (N1C && N1C->isAllOnesValue() &&
823 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000824 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000825 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
826 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000827 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
828 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000829 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
830 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000831 }
832 }
Nate Begeman223df222005-09-08 20:18:10 +0000833 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
834 if (N1C && N0.getOpcode() == ISD::XOR) {
835 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
836 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
837 if (N00C)
838 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
839 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
840 if (N01C)
841 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
842 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
843 }
844 // fold (xor x, x) -> 0
845 if (N0 == N1)
846 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000847 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
848 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
849 N1.getOpcode() == ISD::ZERO_EXTEND &&
850 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
851 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
852 N0.getOperand(0), N1.getOperand(0));
853 WorkList.push_back(XORNode.Val);
854 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
855 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000856 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000857}
858
Nate Begeman83e75ec2005-09-06 04:43:02 +0000859SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000860 SDOperand N0 = N->getOperand(0);
861 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000862 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
863 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000864 MVT::ValueType VT = N0.getValueType();
865 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
866
867 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000868 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000869 return DAG.getConstant(N0C->getValue() << N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000870 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000871 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000872 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000873 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000874 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000875 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000876 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000877 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000878 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000879 // if (shl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +0000880 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
881 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000882 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000883 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000884 N0.getOperand(1).getOpcode() == ISD::Constant) {
885 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000886 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000887 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000888 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000889 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000890 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000891 }
892 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
893 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000894 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000895 N0.getOperand(1).getOpcode() == ISD::Constant) {
896 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000897 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000898 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
899 DAG.getConstant(~0ULL << c1, VT));
900 if (c2 > c1)
901 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000902 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000903 else
Nate Begeman83e75ec2005-09-06 04:43:02 +0000904 return DAG.getNode(ISD::SRL, VT, Mask,
905 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000906 }
907 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000908 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +0000909 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000910 DAG.getConstant(~0ULL << N1C->getValue(), VT));
911 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000912}
913
Nate Begeman83e75ec2005-09-06 04:43:02 +0000914SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000915 SDOperand N0 = N->getOperand(0);
916 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000917 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
918 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000919 MVT::ValueType VT = N0.getValueType();
920 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
921
922 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000923 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000924 return DAG.getConstant(N0C->getSignExtended() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000925 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000926 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000927 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000928 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000929 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000930 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000931 // fold (sra x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000932 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000933 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000934 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000935 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000936 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000937 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begeman646d7e22005-09-02 21:18:40 +0000938 if (N1C && MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1)), TLI))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000939 return DAG.getNode(ISD::SRL, VT, N0, N1);
940 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000941}
942
Nate Begeman83e75ec2005-09-06 04:43:02 +0000943SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000944 SDOperand N0 = N->getOperand(0);
945 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000946 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
947 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000948 MVT::ValueType VT = N0.getValueType();
949 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
950
951 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000952 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000953 return DAG.getConstant(N0C->getValue() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000954 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000955 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000956 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000957 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000958 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000959 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000960 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000961 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000962 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000963 // if (srl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +0000964 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
965 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000966 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000967 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000968 N0.getOperand(1).getOpcode() == ISD::Constant) {
969 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000970 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000971 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000972 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000973 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000974 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000975 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000976 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000977}
978
Nate Begeman83e75ec2005-09-06 04:43:02 +0000979SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000980 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000981 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000982
983 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000984 if (N0C)
985 return DAG.getConstant(CountLeadingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000986 N0.getValueType());
987 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000988}
989
Nate Begeman83e75ec2005-09-06 04:43:02 +0000990SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000991 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000992 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000993
994 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000995 if (N0C)
996 return DAG.getConstant(CountTrailingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000997 N0.getValueType());
998 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000999}
1000
Nate Begeman83e75ec2005-09-06 04:43:02 +00001001SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001002 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001003 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001004
1005 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001006 if (N0C)
1007 return DAG.getConstant(CountPopulation_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001008 N0.getValueType());
1009 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001010}
1011
Nate Begeman452d7be2005-09-16 00:54:12 +00001012SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1013 SDOperand N0 = N->getOperand(0);
1014 SDOperand N1 = N->getOperand(1);
1015 SDOperand N2 = N->getOperand(2);
1016 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1017 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1018 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1019 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001020
Nate Begeman452d7be2005-09-16 00:54:12 +00001021 // fold select C, X, X -> X
1022 if (N1 == N2)
1023 return N1;
1024 // fold select true, X, Y -> X
1025 if (N0C && !N0C->isNullValue())
1026 return N1;
1027 // fold select false, X, Y -> Y
1028 if (N0C && N0C->isNullValue())
1029 return N2;
1030 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001031 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001032 return DAG.getNode(ISD::OR, VT, N0, N2);
1033 // fold select C, 0, X -> ~C & X
1034 // FIXME: this should check for C type == X type, not i1?
1035 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1036 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1037 WorkList.push_back(XORNode.Val);
1038 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1039 }
1040 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001041 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001042 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1043 WorkList.push_back(XORNode.Val);
1044 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1045 }
1046 // fold select C, X, 0 -> C & X
1047 // FIXME: this should check for C type == X type, not i1?
1048 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1049 return DAG.getNode(ISD::AND, VT, N0, N1);
1050 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1051 if (MVT::i1 == VT && N0 == N1)
1052 return DAG.getNode(ISD::OR, VT, N0, N2);
1053 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1054 if (MVT::i1 == VT && N0 == N2)
1055 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman44728a72005-09-19 22:34:01 +00001056 // fold selects based on a setcc into other things, such as min/max/abs
1057 if (N0.getOpcode() == ISD::SETCC)
1058 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001059 return SDOperand();
1060}
1061
1062SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001063 SDOperand N0 = N->getOperand(0);
1064 SDOperand N1 = N->getOperand(1);
1065 SDOperand N2 = N->getOperand(2);
1066 SDOperand N3 = N->getOperand(3);
1067 SDOperand N4 = N->getOperand(4);
1068 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1069 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1070 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1071 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1072
1073 // Determine if the condition we're dealing with is constant
1074 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Chris Lattner91559022005-10-05 04:45:43 +00001075 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1076
Nate Begeman44728a72005-09-19 22:34:01 +00001077 // fold select_cc lhs, rhs, x, x, cc -> x
1078 if (N2 == N3)
1079 return N2;
1080 // fold select_cc true, x, y -> x
Chris Lattner91559022005-10-05 04:45:43 +00001081 if (SCCC && SCCC->getValue())
Nate Begeman44728a72005-09-19 22:34:01 +00001082 return N2;
1083 // fold select_cc false, x, y -> y
Chris Lattner91559022005-10-05 04:45:43 +00001084 if (SCCC && SCCC->getValue() == 0)
Nate Begeman44728a72005-09-19 22:34:01 +00001085 return N3;
1086 // fold select_cc into other things, such as min/max/abs
1087 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001088}
1089
1090SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1091 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1092 cast<CondCodeSDNode>(N->getOperand(2))->get());
1093}
1094
Nate Begeman83e75ec2005-09-06 04:43:02 +00001095SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001096 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001097 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001098 MVT::ValueType VT = N->getValueType(0);
1099
Nate Begeman1d4d4142005-09-01 00:19:25 +00001100 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001101 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001102 return DAG.getConstant(N0C->getSignExtended(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001103 // fold (sext (sext x)) -> (sext x)
1104 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001105 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
1106 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001107}
1108
Nate Begeman83e75ec2005-09-06 04:43:02 +00001109SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001110 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001111 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001112 MVT::ValueType VT = N->getValueType(0);
1113
Nate Begeman1d4d4142005-09-01 00:19:25 +00001114 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001115 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001116 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001117 // fold (zext (zext x)) -> (zext x)
1118 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001119 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
1120 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001121}
1122
Nate Begeman83e75ec2005-09-06 04:43:02 +00001123SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001124 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001125 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001126 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001127 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001128 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001129
Nate Begeman1d4d4142005-09-01 00:19:25 +00001130 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001131 if (N0C) {
1132 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001133 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001134 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001135 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001136 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman646d7e22005-09-02 21:18:40 +00001137 cast<VTSDNode>(N0.getOperand(1))->getVT() < EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001138 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001139 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001140 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1141 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1142 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001143 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001144 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001145 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1146 if (N0.getOpcode() == ISD::AssertSext &&
1147 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001148 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001149 }
1150 // fold (sext_in_reg (sextload x)) -> (sextload x)
1151 if (N0.getOpcode() == ISD::SEXTLOAD &&
1152 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001153 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001154 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001155 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001156 // FIXME: teach isSetCCEquivalent about 0, -1 and then use it here
Nate Begeman1d4d4142005-09-01 00:19:25 +00001157 if (N0.getOpcode() == ISD::SETCC &&
1158 TLI.getSetCCResultContents() ==
1159 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001160 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001161 // FIXME: this code is currently just ported over from SelectionDAG.cpp
1162 // we probably actually want to handle this in two pieces. Rather than
1163 // checking all the top bits for zero, just check the sign bit here and turn
1164 // it into a zero extend inreg (AND with constant).
1165 // then, let the code for AND figure out if the mask is superfluous rather
1166 // than doing so here.
1167 if (N0.getOpcode() == ISD::AND &&
1168 N0.getOperand(1).getOpcode() == ISD::Constant) {
1169 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1170 unsigned NumBits = MVT::getSizeInBits(EVT);
1171 if ((Mask & (~0ULL << (NumBits-1))) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001172 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001173 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001174 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001175}
1176
Nate Begeman83e75ec2005-09-06 04:43:02 +00001177SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001178 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001179 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001180 MVT::ValueType VT = N->getValueType(0);
1181
1182 // noop truncate
1183 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001184 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001185 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001186 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001187 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001188 // fold (truncate (truncate x)) -> (truncate x)
1189 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001190 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001191 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1192 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1193 if (N0.getValueType() < VT)
1194 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001195 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001196 else if (N0.getValueType() > VT)
1197 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001198 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001199 else
1200 // if the source and dest are the same type, we can drop both the extend
1201 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001202 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001203 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001204 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001205}
1206
Chris Lattner01b3d732005-09-28 22:28:18 +00001207SDOperand DAGCombiner::visitFADD(SDNode *N) {
1208 SDOperand N0 = N->getOperand(0);
1209 SDOperand N1 = N->getOperand(1);
1210 MVT::ValueType VT = N->getValueType(0);
1211
1212 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1213 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1214 // fold floating point (fadd c1, c2)
1215 return DAG.getConstantFP(N0CFP->getValue() + N1CFP->getValue(),
1216 N->getValueType(0));
1217 }
1218 // fold (A + (-B)) -> A-B
1219 if (N1.getOpcode() == ISD::FNEG)
1220 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
1221
1222 // fold ((-A) + B) -> B-A
1223 if (N0.getOpcode() == ISD::FNEG)
1224 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
1225
1226 return SDOperand();
1227}
1228
1229SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1230 SDOperand N0 = N->getOperand(0);
1231 SDOperand N1 = N->getOperand(1);
1232 MVT::ValueType VT = N->getValueType(0);
1233
1234 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1235 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1236 // fold floating point (fsub c1, c2)
1237 return DAG.getConstantFP(N0CFP->getValue() - N1CFP->getValue(),
1238 N->getValueType(0));
1239 }
1240 // fold (A-(-B)) -> A+B
1241 if (N1.getOpcode() == ISD::FNEG)
1242 return DAG.getNode(ISD::FADD, N0.getValueType(), N0, N1.getOperand(0));
1243
1244 return SDOperand();
1245}
1246
1247SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1248 SDOperand N0 = N->getOperand(0);
1249 SDOperand N1 = N->getOperand(1);
1250 MVT::ValueType VT = N->getValueType(0);
1251
1252 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1253 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1254 // fold floating point (fmul c1, c2)
1255 return DAG.getConstantFP(N0CFP->getValue() * N1CFP->getValue(),
1256 N->getValueType(0));
1257 }
1258 return SDOperand();
1259}
1260
1261SDOperand DAGCombiner::visitFDIV(SDNode *N) {
1262 SDOperand N0 = N->getOperand(0);
1263 SDOperand N1 = N->getOperand(1);
1264 MVT::ValueType VT = N->getValueType(0);
1265
1266 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1267 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1268 // fold floating point (fdiv c1, c2)
1269 return DAG.getConstantFP(N0CFP->getValue() / N1CFP->getValue(),
1270 N->getValueType(0));
1271 }
1272 return SDOperand();
1273}
1274
1275SDOperand DAGCombiner::visitFREM(SDNode *N) {
1276 SDOperand N0 = N->getOperand(0);
1277 SDOperand N1 = N->getOperand(1);
1278 MVT::ValueType VT = N->getValueType(0);
1279
1280 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1281 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1282 // fold floating point (frem c1, c2) -> fmod(c1, c2)
1283 return DAG.getConstantFP(fmod(N0CFP->getValue(),N1CFP->getValue()),
1284 N->getValueType(0));
1285 }
1286 return SDOperand();
1287}
1288
1289
Nate Begeman83e75ec2005-09-06 04:43:02 +00001290SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001291 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001292 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001293
1294 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001295 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001296 return DAG.getConstantFP(N0C->getSignExtended(), N->getValueType(0));
1297 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001298}
1299
Nate Begeman83e75ec2005-09-06 04:43:02 +00001300SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001301 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001302 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001303
1304 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001305 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001306 return DAG.getConstantFP(N0C->getValue(), N->getValueType(0));
1307 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001308}
1309
Nate Begeman83e75ec2005-09-06 04:43:02 +00001310SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001311 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001312
1313 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001314 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001315 return DAG.getConstant((int64_t)N0CFP->getValue(), N->getValueType(0));
1316 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001317}
1318
Nate Begeman83e75ec2005-09-06 04:43:02 +00001319SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001320 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001321
1322 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001323 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001324 return DAG.getConstant((uint64_t)N0CFP->getValue(), N->getValueType(0));
1325 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001326}
1327
Nate Begeman83e75ec2005-09-06 04:43:02 +00001328SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001329 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001330
1331 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001332 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001333 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1334 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001335}
1336
Nate Begeman83e75ec2005-09-06 04:43:02 +00001337SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001338 SDOperand N0 = N->getOperand(0);
1339 MVT::ValueType VT = N->getValueType(0);
1340 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00001341 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001342
Nate Begeman1d4d4142005-09-01 00:19:25 +00001343 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001344 if (N0CFP) {
1345 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001346 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001347 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001348 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001349}
1350
Nate Begeman83e75ec2005-09-06 04:43:02 +00001351SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001352 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001353
1354 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001355 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001356 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1357 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001358}
1359
Nate Begeman83e75ec2005-09-06 04:43:02 +00001360SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001361 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001362 // fold (neg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001363 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001364 return DAG.getConstantFP(-N0CFP->getValue(), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001365 // fold (neg (sub x, y)) -> (sub y, x)
1366 if (N->getOperand(0).getOpcode() == ISD::SUB)
1367 return DAG.getNode(ISD::SUB, N->getValueType(0), N->getOperand(1),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001368 N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001369 // fold (neg (neg x)) -> x
1370 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001371 return N->getOperand(0).getOperand(0);
1372 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001373}
1374
Nate Begeman83e75ec2005-09-06 04:43:02 +00001375SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001376 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001377 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001378 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001379 return DAG.getConstantFP(fabs(N0CFP->getValue()), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001380 // fold (fabs (fabs x)) -> (fabs x)
1381 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001382 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001383 // fold (fabs (fneg x)) -> (fabs x)
1384 if (N->getOperand(0).getOpcode() == ISD::FNEG)
1385 return DAG.getNode(ISD::FABS, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001386 N->getOperand(0).getOperand(0));
1387 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001388}
1389
Nate Begeman44728a72005-09-19 22:34:01 +00001390SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
1391 SDOperand Chain = N->getOperand(0);
1392 SDOperand N1 = N->getOperand(1);
1393 SDOperand N2 = N->getOperand(2);
1394 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1395
1396 // never taken branch, fold to chain
1397 if (N1C && N1C->isNullValue())
1398 return Chain;
1399 // unconditional branch
1400 if (N1C && !N1C->isNullValue())
1401 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1402 return SDOperand();
1403}
1404
1405SDOperand DAGCombiner::visitBRCONDTWOWAY(SDNode *N) {
1406 SDOperand Chain = N->getOperand(0);
1407 SDOperand N1 = N->getOperand(1);
1408 SDOperand N2 = N->getOperand(2);
1409 SDOperand N3 = N->getOperand(3);
1410 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1411
1412 // unconditional branch to true mbb
1413 if (N1C && N1C->getValue() == 1)
1414 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1415 // unconditional branch to false mbb
1416 if (N1C && N1C->isNullValue())
1417 return DAG.getNode(ISD::BR, MVT::Other, Chain, N3);
1418 return SDOperand();
1419}
1420
1421SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
1422 // FIXME: come up with a common way between br_cc, brtwoway_cc, and select_cc
1423 // to canonicalize the condition without calling getnode a bazillion times.
1424 return SDOperand();
1425}
1426
1427SDOperand DAGCombiner::visitBRTWOWAY_CC(SDNode *N) {
1428 // FIXME: come up with a common way between br_cc, brtwoway_cc, and select_cc
1429 // to canonicalize the condition without calling getnode a bazillion times.
1430 return SDOperand();
1431}
1432
1433SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
1434 return SDOperand();
1435}
1436
1437SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
1438 SDOperand N2, SDOperand N3,
1439 ISD::CondCode CC) {
1440 return SDOperand();
1441}
1442
Nate Begeman452d7be2005-09-16 00:54:12 +00001443SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
1444 SDOperand N1, ISD::CondCode Cond) {
1445 // These setcc operations always fold.
1446 switch (Cond) {
1447 default: break;
1448 case ISD::SETFALSE:
1449 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1450 case ISD::SETTRUE:
1451 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
1452 }
1453
1454 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1455 uint64_t C1 = N1C->getValue();
1456 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
1457 uint64_t C0 = N0C->getValue();
1458
1459 // Sign extend the operands if required
1460 if (ISD::isSignedIntSetCC(Cond)) {
1461 C0 = N0C->getSignExtended();
1462 C1 = N1C->getSignExtended();
1463 }
1464
1465 switch (Cond) {
1466 default: assert(0 && "Unknown integer setcc!");
1467 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
1468 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
1469 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
1470 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
1471 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
1472 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
1473 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
1474 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
1475 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
1476 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
1477 }
1478 } else {
1479 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
1480 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
1481 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
1482
1483 // If the comparison constant has bits in the upper part, the
1484 // zero-extended value could never match.
1485 if (C1 & (~0ULL << InSize)) {
1486 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
1487 switch (Cond) {
1488 case ISD::SETUGT:
1489 case ISD::SETUGE:
1490 case ISD::SETEQ: return DAG.getConstant(0, VT);
1491 case ISD::SETULT:
1492 case ISD::SETULE:
1493 case ISD::SETNE: return DAG.getConstant(1, VT);
1494 case ISD::SETGT:
1495 case ISD::SETGE:
1496 // True if the sign bit of C1 is set.
1497 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
1498 case ISD::SETLT:
1499 case ISD::SETLE:
1500 // True if the sign bit of C1 isn't set.
1501 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
1502 default:
1503 break;
1504 }
1505 }
1506
1507 // Otherwise, we can perform the comparison with the low bits.
1508 switch (Cond) {
1509 case ISD::SETEQ:
1510 case ISD::SETNE:
1511 case ISD::SETUGT:
1512 case ISD::SETUGE:
1513 case ISD::SETULT:
1514 case ISD::SETULE:
1515 return DAG.getSetCC(VT, N0.getOperand(0),
1516 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
1517 Cond);
1518 default:
1519 break; // todo, be more careful with signed comparisons
1520 }
1521 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1522 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1523 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
1524 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
1525 MVT::ValueType ExtDstTy = N0.getValueType();
1526 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
1527
1528 // If the extended part has any inconsistent bits, it cannot ever
1529 // compare equal. In other words, they have to be all ones or all
1530 // zeros.
1531 uint64_t ExtBits =
1532 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
1533 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
1534 return DAG.getConstant(Cond == ISD::SETNE, VT);
1535
1536 SDOperand ZextOp;
1537 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
1538 if (Op0Ty == ExtSrcTy) {
1539 ZextOp = N0.getOperand(0);
1540 } else {
1541 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
1542 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
1543 DAG.getConstant(Imm, Op0Ty));
1544 }
1545 WorkList.push_back(ZextOp.Val);
1546 // Otherwise, make this a use of a zext.
1547 return DAG.getSetCC(VT, ZextOp,
1548 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
1549 ExtDstTy),
1550 Cond);
1551 }
Chris Lattner5c46f742005-10-05 06:11:08 +00001552
Nate Begeman452d7be2005-09-16 00:54:12 +00001553 uint64_t MinVal, MaxVal;
1554 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
1555 if (ISD::isSignedIntSetCC(Cond)) {
1556 MinVal = 1ULL << (OperandBitSize-1);
1557 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
1558 MaxVal = ~0ULL >> (65-OperandBitSize);
1559 else
1560 MaxVal = 0;
1561 } else {
1562 MinVal = 0;
1563 MaxVal = ~0ULL >> (64-OperandBitSize);
1564 }
1565
1566 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
1567 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
1568 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
1569 --C1; // X >= C0 --> X > (C0-1)
1570 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
1571 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
1572 }
1573
1574 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
1575 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
1576 ++C1; // X <= C0 --> X < (C0+1)
1577 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
1578 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
1579 }
1580
1581 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
1582 return DAG.getConstant(0, VT); // X < MIN --> false
1583
1584 // Canonicalize setgt X, Min --> setne X, Min
1585 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
1586 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
1587
1588 // If we have setult X, 1, turn it into seteq X, 0
1589 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
1590 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
1591 ISD::SETEQ);
1592 // If we have setugt X, Max-1, turn it into seteq X, Max
1593 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
1594 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
1595 ISD::SETEQ);
1596
1597 // If we have "setcc X, C0", check to see if we can shrink the immediate
1598 // by changing cc.
1599
1600 // SETUGT X, SINTMAX -> SETLT X, 0
1601 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
1602 C1 == (~0ULL >> (65-OperandBitSize)))
1603 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
1604 ISD::SETLT);
1605
1606 // FIXME: Implement the rest of these.
1607
1608 // Fold bit comparisons when we can.
1609 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1610 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
1611 if (ConstantSDNode *AndRHS =
1612 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1613 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1614 // Perform the xform if the AND RHS is a single bit.
1615 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
1616 return DAG.getNode(ISD::SRL, VT, N0,
1617 DAG.getConstant(Log2_64(AndRHS->getValue()),
1618 TLI.getShiftAmountTy()));
1619 }
1620 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
1621 // (X & 8) == 8 --> (X & 8) >> 3
1622 // Perform the xform if C1 is a single bit.
1623 if ((C1 & (C1-1)) == 0) {
1624 return DAG.getNode(ISD::SRL, VT, N0,
1625 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
1626 }
1627 }
1628 }
1629 }
1630 } else if (isa<ConstantSDNode>(N0.Val)) {
1631 // Ensure that the constant occurs on the RHS.
1632 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1633 }
1634
1635 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
1636 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
1637 double C0 = N0C->getValue(), C1 = N1C->getValue();
1638
1639 switch (Cond) {
1640 default: break; // FIXME: Implement the rest of these!
1641 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
1642 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
1643 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
1644 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
1645 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
1646 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
1647 }
1648 } else {
1649 // Ensure that the constant occurs on the RHS.
1650 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1651 }
1652
1653 if (N0 == N1) {
1654 // We can always fold X == Y for integer setcc's.
1655 if (MVT::isInteger(N0.getValueType()))
1656 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1657 unsigned UOF = ISD::getUnorderedFlavor(Cond);
1658 if (UOF == 2) // FP operators that are undefined on NaNs.
1659 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1660 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
1661 return DAG.getConstant(UOF, VT);
1662 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
1663 // if it is not already.
1664 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
1665 if (NewCond != Cond)
1666 return DAG.getSetCC(VT, N0, N1, NewCond);
1667 }
1668
1669 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1670 MVT::isInteger(N0.getValueType())) {
1671 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
1672 N0.getOpcode() == ISD::XOR) {
1673 // Simplify (X+Y) == (X+Z) --> Y == Z
1674 if (N0.getOpcode() == N1.getOpcode()) {
1675 if (N0.getOperand(0) == N1.getOperand(0))
1676 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
1677 if (N0.getOperand(1) == N1.getOperand(1))
1678 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
1679 if (isCommutativeBinOp(N0.getOpcode())) {
1680 // If X op Y == Y op X, try other combinations.
1681 if (N0.getOperand(0) == N1.getOperand(1))
1682 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
1683 if (N0.getOperand(1) == N1.getOperand(0))
1684 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
1685 }
1686 }
1687
Chris Lattner5c46f742005-10-05 06:11:08 +00001688 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0. Common for condcodes.
1689 if (N0.getOpcode() == ISD::XOR)
1690 if (ConstantSDNode *XORC = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
1691 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
1692 // If we know that all of the inverted bits are zero, don't bother
1693 // performing the inversion.
1694 if (MaskedValueIsZero(N0.getOperand(0), ~XORC->getValue(), TLI))
1695 return DAG.getSetCC(VT, N0.getOperand(0),
1696 DAG.getConstant(XORC->getValue()^RHSC->getValue(),
1697 N0.getValueType()), Cond);
1698 }
1699
Nate Begeman452d7be2005-09-16 00:54:12 +00001700 // Simplify (X+Z) == X --> Z == 0
1701 if (N0.getOperand(0) == N1)
1702 return DAG.getSetCC(VT, N0.getOperand(1),
1703 DAG.getConstant(0, N0.getValueType()), Cond);
1704 if (N0.getOperand(1) == N1) {
1705 if (isCommutativeBinOp(N0.getOpcode()))
1706 return DAG.getSetCC(VT, N0.getOperand(0),
1707 DAG.getConstant(0, N0.getValueType()), Cond);
1708 else {
1709 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
1710 // (Z-X) == X --> Z == X<<1
1711 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
1712 N1,
1713 DAG.getConstant(1,TLI.getShiftAmountTy()));
1714 WorkList.push_back(SH.Val);
1715 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
1716 }
1717 }
1718 }
1719
1720 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
1721 N1.getOpcode() == ISD::XOR) {
1722 // Simplify X == (X+Z) --> Z == 0
1723 if (N1.getOperand(0) == N0) {
1724 return DAG.getSetCC(VT, N1.getOperand(1),
1725 DAG.getConstant(0, N1.getValueType()), Cond);
1726 } else if (N1.getOperand(1) == N0) {
1727 if (isCommutativeBinOp(N1.getOpcode())) {
1728 return DAG.getSetCC(VT, N1.getOperand(0),
1729 DAG.getConstant(0, N1.getValueType()), Cond);
1730 } else {
1731 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
1732 // X == (Z-X) --> X<<1 == Z
1733 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
1734 DAG.getConstant(1,TLI.getShiftAmountTy()));
1735 WorkList.push_back(SH.Val);
1736 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
1737 }
1738 }
1739 }
1740 }
1741
1742 // Fold away ALL boolean setcc's.
1743 SDOperand Temp;
1744 if (N0.getValueType() == MVT::i1) {
1745 switch (Cond) {
1746 default: assert(0 && "Unknown integer setcc!");
1747 case ISD::SETEQ: // X == Y -> (X^Y)^1
1748 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1749 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
1750 WorkList.push_back(Temp.Val);
1751 break;
1752 case ISD::SETNE: // X != Y --> (X^Y)
1753 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1754 break;
1755 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
1756 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
1757 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
1758 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
1759 WorkList.push_back(Temp.Val);
1760 break;
1761 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
1762 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
1763 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
1764 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
1765 WorkList.push_back(Temp.Val);
1766 break;
1767 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
1768 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
1769 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
1770 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
1771 WorkList.push_back(Temp.Val);
1772 break;
1773 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
1774 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
1775 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
1776 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
1777 break;
1778 }
1779 if (VT != MVT::i1) {
1780 WorkList.push_back(N0.Val);
1781 // FIXME: If running after legalize, we probably can't do this.
1782 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
1783 }
1784 return N0;
1785 }
1786
1787 // Could not fold it.
1788 return SDOperand();
1789}
1790
Nate Begeman1d4d4142005-09-01 00:19:25 +00001791// SelectionDAG::Combine - This is the entry point for the file.
1792//
Nate Begeman4ebd8052005-09-01 23:24:04 +00001793void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001794 /// run - This is the main entry point to this class.
1795 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00001796 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001797}