blob: 899543dbbe943a5eec3982e8c145df9237bf2f12 [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);
118 SDOperand visitSINT_TO_FP(SDNode *N);
119 SDOperand visitUINT_TO_FP(SDNode *N);
120 SDOperand visitFP_TO_SINT(SDNode *N);
121 SDOperand visitFP_TO_UINT(SDNode *N);
122 SDOperand visitFP_ROUND(SDNode *N);
123 SDOperand visitFP_ROUND_INREG(SDNode *N);
124 SDOperand visitFP_EXTEND(SDNode *N);
125 SDOperand visitFNEG(SDNode *N);
126 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000127 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000128 SDOperand visitBRCONDTWOWAY(SDNode *N);
129 SDOperand visitBR_CC(SDNode *N);
130 SDOperand visitBRTWOWAY_CC(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000131
Nate Begeman44728a72005-09-19 22:34:01 +0000132 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
133 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
134 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000135 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
136 ISD::CondCode Cond);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000137public:
138 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000139 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000140
141 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000142 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000143 };
144}
145
146/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
147/// this predicate to simplify operations downstream. V and Mask are known to
148/// be the same type.
149static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
150 const TargetLowering &TLI) {
151 unsigned SrcBits;
152 if (Mask == 0) return true;
153
154 // If we know the result of a setcc has the top bits zero, use this info.
155 switch (Op.getOpcode()) {
Nate Begeman4ebd8052005-09-01 23:24:04 +0000156 case ISD::Constant:
157 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
158 case ISD::SETCC:
Nate Begeman646d7e22005-09-02 21:18:40 +0000159 // FIXME: teach this about non ZeroOrOne values, such as 0 or -1
Nate Begeman4ebd8052005-09-01 23:24:04 +0000160 return ((Mask & 1) == 0) &&
161 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
162 case ISD::ZEXTLOAD:
163 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
164 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
165 case ISD::ZERO_EXTEND:
166 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
167 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
168 case ISD::AssertZext:
169 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
170 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
171 case ISD::AND:
172 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
173 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
174 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
175 // FALL THROUGH
176 case ISD::OR:
177 case ISD::XOR:
178 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
179 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
180 case ISD::SELECT:
181 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
182 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
183 case ISD::SELECT_CC:
184 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
185 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
186 case ISD::SRL:
187 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
188 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
189 uint64_t NewVal = Mask << ShAmt->getValue();
190 SrcBits = MVT::getSizeInBits(Op.getValueType());
191 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
192 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
193 }
194 return false;
195 case ISD::SHL:
196 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
197 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
198 uint64_t NewVal = Mask >> ShAmt->getValue();
199 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
200 }
201 return false;
202 case ISD::CTTZ:
203 case ISD::CTLZ:
204 case ISD::CTPOP:
205 // Bit counting instructions can not set the high bits of the result
206 // register. The max number of bits sets depends on the input.
207 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
208
209 // TODO we could handle some SRA cases here.
210 default: break;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000211 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000212 return false;
213}
214
Nate Begeman4ebd8052005-09-01 23:24:04 +0000215// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
216// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000217// Also, set the incoming LHS, RHS, and CC references to the appropriate
218// nodes based on the type of node we are checking. This simplifies life a
219// bit for the callers.
220static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
221 SDOperand &CC) {
222 if (N.getOpcode() == ISD::SETCC) {
223 LHS = N.getOperand(0);
224 RHS = N.getOperand(1);
225 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000226 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000227 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000228 if (N.getOpcode() == ISD::SELECT_CC &&
229 N.getOperand(2).getOpcode() == ISD::Constant &&
230 N.getOperand(3).getOpcode() == ISD::Constant &&
231 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000232 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
233 LHS = N.getOperand(0);
234 RHS = N.getOperand(1);
235 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000236 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000237 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000238 return false;
239}
240
Nate Begeman99801192005-09-07 23:25:52 +0000241// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
242// one use. If this is true, it allows the users to invert the operation for
243// free when it is profitable to do so.
244static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000245 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000246 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000247 return true;
248 return false;
249}
250
Nate Begeman452d7be2005-09-16 00:54:12 +0000251// FIXME: This should probably go in the ISD class rather than being duplicated
252// in several files.
253static bool isCommutativeBinOp(unsigned Opcode) {
254 switch (Opcode) {
255 case ISD::ADD:
256 case ISD::MUL:
257 case ISD::AND:
258 case ISD::OR:
259 case ISD::XOR: return true;
260 default: return false; // FIXME: Need commutative info for user ops!
261 }
262}
263
Nate Begeman4ebd8052005-09-01 23:24:04 +0000264void DAGCombiner::Run(bool RunningAfterLegalize) {
265 // set the instance variable, so that the various visit routines may use it.
266 AfterLegalize = RunningAfterLegalize;
267
Nate Begeman646d7e22005-09-02 21:18:40 +0000268 // Add all the dag nodes to the worklist.
269 WorkList.insert(WorkList.end(), DAG.allnodes_begin(), DAG.allnodes_end());
Nate Begeman83e75ec2005-09-06 04:43:02 +0000270
Nate Begeman1d4d4142005-09-01 00:19:25 +0000271 // while the worklist isn't empty, inspect the node on the end of it and
272 // try and combine it.
273 while (!WorkList.empty()) {
274 SDNode *N = WorkList.back();
275 WorkList.pop_back();
276
277 // If N has no uses, it is dead. Make sure to revisit all N's operands once
278 // N is deleted from the DAG, since they too may now be dead.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000279 // FIXME: is there a better way to keep from deleting the dag root because
280 // we think it has no uses? This works for now...
281 if (N->use_empty() && N != DAG.getRoot().Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000282 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
283 WorkList.push_back(N->getOperand(i).Val);
284
285 DAG.DeleteNode(N);
286 removeFromWorkList(N);
287 continue;
288 }
289
Nate Begeman83e75ec2005-09-06 04:43:02 +0000290 SDOperand RV = visit(N);
291 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000292 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000293 // If we get back the same node we passed in, rather than a new node or
294 // zero, we know that the node must have defined multiple values and
295 // CombineTo was used. Since CombineTo takes care of the worklist
296 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000297 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000298 DEBUG(std::cerr << "\nReplacing "; N->dump();
299 std::cerr << "\nWith: "; RV.Val->dump();
300 std::cerr << '\n');
Nate Begeman99801192005-09-07 23:25:52 +0000301 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV));
Nate Begeman646d7e22005-09-02 21:18:40 +0000302
303 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000304 WorkList.push_back(RV.Val);
305 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000306
307 // Nodes can end up on the worklist more than once. Make sure we do
308 // not process a node that has been replaced.
309 removeFromWorkList(N);
310 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000311 }
312 }
313}
314
Nate Begeman83e75ec2005-09-06 04:43:02 +0000315SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000316 switch(N->getOpcode()) {
317 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000318 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000319 case ISD::ADD: return visitADD(N);
320 case ISD::SUB: return visitSUB(N);
321 case ISD::MUL: return visitMUL(N);
322 case ISD::SDIV: return visitSDIV(N);
323 case ISD::UDIV: return visitUDIV(N);
324 case ISD::SREM: return visitSREM(N);
325 case ISD::UREM: return visitUREM(N);
326 case ISD::MULHU: return visitMULHU(N);
327 case ISD::MULHS: return visitMULHS(N);
328 case ISD::AND: return visitAND(N);
329 case ISD::OR: return visitOR(N);
330 case ISD::XOR: return visitXOR(N);
331 case ISD::SHL: return visitSHL(N);
332 case ISD::SRA: return visitSRA(N);
333 case ISD::SRL: return visitSRL(N);
334 case ISD::CTLZ: return visitCTLZ(N);
335 case ISD::CTTZ: return visitCTTZ(N);
336 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000337 case ISD::SELECT: return visitSELECT(N);
338 case ISD::SELECT_CC: return visitSELECT_CC(N);
339 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000340 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
341 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
342 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
343 case ISD::TRUNCATE: return visitTRUNCATE(N);
344 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
345 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
346 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
347 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
348 case ISD::FP_ROUND: return visitFP_ROUND(N);
349 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
350 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
351 case ISD::FNEG: return visitFNEG(N);
352 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000353 case ISD::BRCOND: return visitBRCOND(N);
354 case ISD::BRCONDTWOWAY: return visitBRCONDTWOWAY(N);
355 case ISD::BR_CC: return visitBR_CC(N);
356 case ISD::BRTWOWAY_CC: return visitBRTWOWAY_CC(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000357 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000358 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000359}
360
Nate Begeman83e75ec2005-09-06 04:43:02 +0000361SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000362 // If the token factor has two operands and one is the entry token, replace
363 // the token factor with the other operand.
364 if (N->getNumOperands() == 2) {
365 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000366 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000367 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000368 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000369 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000370 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000371}
372
Nate Begeman83e75ec2005-09-06 04:43:02 +0000373SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000374 SDOperand N0 = N->getOperand(0);
375 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000376 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
377 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
378 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
379 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000380 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000381
382 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000383 if (N0C && N1C)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000384 return DAG.getConstant(N0C->getValue() + N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000385 // canonicalize constant to RHS
386 if (N0C && !N1C) {
387 std::swap(N0, N1);
388 std::swap(N0C, N1C);
389 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000390 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000391 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000392 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000393 // fold floating point (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000394 if (N0CFP && N1CFP)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000395 return DAG.getConstantFP(N0CFP->getValue() + N1CFP->getValue(), VT);
Nate Begeman223df222005-09-08 20:18:10 +0000396 // fold (add (add x, c1), c2) -> (add x, c1+c2)
397 if (N1C && N0.getOpcode() == ISD::ADD) {
398 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
399 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
400 if (N00C)
401 return DAG.getNode(ISD::ADD, VT, N0.getOperand(1),
402 DAG.getConstant(N1C->getValue()+N00C->getValue(), VT));
403 if (N01C)
404 return DAG.getNode(ISD::ADD, VT, N0.getOperand(0),
405 DAG.getConstant(N1C->getValue()+N01C->getValue(), VT));
406 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000407 // fold (A + (-B)) -> A-B
408 if (N1.getOpcode() == ISD::FNEG)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000409 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000410 // fold ((-A) + B) -> B-A
411 if (N0.getOpcode() == ISD::FNEG)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000412 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000413 // fold ((0-A) + B) -> B-A
414 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
415 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000416 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000417 // fold (A + (0-B)) -> A-B
418 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
419 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000420 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000421 // fold (A+(B-A)) -> B for non-fp types
422 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1) &&
423 !MVT::isFloatingPoint(N1.getValueType()))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000424 return N1.getOperand(0);
425 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000426}
427
Nate Begeman83e75ec2005-09-06 04:43:02 +0000428SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000429 SDOperand N0 = N->getOperand(0);
430 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000431 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
432 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
433 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0.Val);
434 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(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 floating point (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000444 if (N0CFP && N1CFP)
445 return DAG.getConstantFP(N0CFP->getValue() - N1CFP->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000446 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000447 // fold (A+B)-A -> B
448 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
449 !MVT::isFloatingPoint(N1.getValueType()))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000450 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000451 // fold (A+B)-B -> A
452 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1 &&
453 !MVT::isFloatingPoint(N1.getValueType()))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000454 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000455 // fold (A-(-B)) -> A+B
456 if (N1.getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000457 return DAG.getNode(ISD::ADD, N0.getValueType(), N0, N1.getOperand(0));
458 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000459}
460
Nate Begeman83e75ec2005-09-06 04:43:02 +0000461SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000462 SDOperand N0 = N->getOperand(0);
463 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000464 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
465 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
466 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
467 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000468 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000469
470 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000471 if (N0C && N1C)
472 return DAG.getConstant(N0C->getValue() * N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000473 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000474 // canonicalize constant to RHS
475 if (N0C && !N1C) {
476 std::swap(N0, N1);
477 std::swap(N0C, N1C);
478 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000479 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000480 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000481 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000482 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000483 if (N1C && N1C->isAllOnesValue())
Nate Begeman1d4d4142005-09-01 00:19:25 +0000484 return DAG.getNode(ISD::SUB, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000485 DAG.getConstant(0, N->getValueType(0)), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000486 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000487 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000488 return DAG.getNode(ISD::SHL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000489 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000490 TLI.getShiftAmountTy()));
Nate Begeman223df222005-09-08 20:18:10 +0000491 // fold (mul (mul x, c1), c2) -> (mul x, c1*c2)
492 if (N1C && N0.getOpcode() == ISD::MUL) {
493 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
494 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
495 if (N00C)
496 return DAG.getNode(ISD::MUL, VT, N0.getOperand(1),
497 DAG.getConstant(N1C->getValue()*N00C->getValue(), VT));
498 if (N01C)
499 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0),
500 DAG.getConstant(N1C->getValue()*N01C->getValue(), VT));
501 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000502 // fold floating point (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000503 if (N0CFP && N1CFP)
504 return DAG.getConstantFP(N0CFP->getValue() * N1CFP->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000505 N->getValueType(0));
506 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000507}
508
Nate Begeman83e75ec2005-09-06 04:43:02 +0000509SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000510 SDOperand N0 = N->getOperand(0);
511 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000512 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
513 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
514 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0.Val);
515 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000516
517 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000518 if (N0C && N1C && !N1C->isNullValue())
519 return DAG.getConstant(N0C->getSignExtended() / N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000520 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000521 // fold floating point (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000522 if (N0CFP && N1CFP)
523 return DAG.getConstantFP(N0CFP->getValue() / N1CFP->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000524 N->getValueType(0));
525 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000526}
527
Nate Begeman83e75ec2005-09-06 04:43:02 +0000528SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000529 SDOperand N0 = N->getOperand(0);
530 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000531 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
532 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000533
534 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000535 if (N0C && N1C && !N1C->isNullValue())
536 return DAG.getConstant(N0C->getValue() / N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000537 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000538 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000539 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000540 return DAG.getNode(ISD::SRL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000541 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000542 TLI.getShiftAmountTy()));
543 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000544}
545
Nate Begeman83e75ec2005-09-06 04:43:02 +0000546SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000547 SDOperand N0 = N->getOperand(0);
548 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000549 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
550 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
551 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
552 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000553
554 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000555 if (N0C && N1C && !N1C->isNullValue())
556 return DAG.getConstant(N0C->getSignExtended() % N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000557 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000558 // fold floating point (srem c1, c2) -> fmod(c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000559 if (N0CFP && N1CFP)
560 return DAG.getConstantFP(fmod(N0CFP->getValue(),N1CFP->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000561 N->getValueType(0));
562 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000563}
564
Nate Begeman83e75ec2005-09-06 04:43:02 +0000565SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000566 SDOperand N0 = N->getOperand(0);
567 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000568 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
569 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000570
571 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000572 if (N0C && N1C && !N1C->isNullValue())
573 return DAG.getConstant(N0C->getValue() % N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000574 N->getValueType(0));
Nate Begeman646d7e22005-09-02 21:18:40 +0000575 // FIXME: c2 power of 2 -> mask?
Nate Begeman83e75ec2005-09-06 04:43:02 +0000576 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000577}
578
Nate Begeman83e75ec2005-09-06 04:43:02 +0000579SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000580 SDOperand N0 = N->getOperand(0);
581 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000582 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000583
584 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000585 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000586 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000587 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000588 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000589 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
590 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000591 TLI.getShiftAmountTy()));
592 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000593}
594
Nate Begeman83e75ec2005-09-06 04:43:02 +0000595SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000596 SDOperand N0 = N->getOperand(0);
597 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000598 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000599
600 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000601 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000602 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000603 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000604 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000605 return DAG.getConstant(0, N0.getValueType());
606 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000607}
608
Nate Begeman83e75ec2005-09-06 04:43:02 +0000609SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000610 SDOperand N0 = N->getOperand(0);
611 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000612 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000613 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
614 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000615 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000616 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000617
618 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000619 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000620 return DAG.getConstant(N0C->getValue() & N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000621 // canonicalize constant to RHS
622 if (N0C && !N1C) {
623 std::swap(N0, N1);
624 std::swap(N0C, N1C);
625 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000626 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000627 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000628 return N0;
629 // if (and x, c) is known to be zero, return 0
630 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
631 return DAG.getConstant(0, VT);
632 // fold (and x, c) -> x iff (x & ~c) == 0
633 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
634 TLI))
635 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000636 // fold (and (and x, c1), c2) -> (and x, c1^c2)
637 if (N1C && N0.getOpcode() == ISD::AND) {
638 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
639 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
640 if (N00C)
641 return DAG.getNode(ISD::AND, VT, N0.getOperand(1),
642 DAG.getConstant(N1C->getValue()&N00C->getValue(), VT));
643 if (N01C)
644 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
645 DAG.getConstant(N1C->getValue()&N01C->getValue(), VT));
646 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000647 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
648 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG) {
649 unsigned ExtendBits =
650 MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT());
Nate Begeman646d7e22005-09-02 21:18:40 +0000651 if ((N1C->getValue() & (~0ULL << ExtendBits)) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000652 return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000653 }
654 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
655 if (N0.getOpcode() == ISD::OR)
656 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000657 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000658 return N1;
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000659 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
660 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
661 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
662 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
663
664 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
665 MVT::isInteger(LL.getValueType())) {
666 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
667 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
668 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
669 WorkList.push_back(ORNode.Val);
670 return DAG.getSetCC(VT, ORNode, LR, Op1);
671 }
672 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
673 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
674 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
675 WorkList.push_back(ANDNode.Val);
676 return DAG.getSetCC(VT, ANDNode, LR, Op1);
677 }
678 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
679 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
680 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
681 WorkList.push_back(ORNode.Val);
682 return DAG.getSetCC(VT, ORNode, LR, Op1);
683 }
684 }
685 // canonicalize equivalent to ll == rl
686 if (LL == RR && LR == RL) {
687 Op1 = ISD::getSetCCSwappedOperands(Op1);
688 std::swap(RL, RR);
689 }
690 if (LL == RL && LR == RR) {
691 bool isInteger = MVT::isInteger(LL.getValueType());
692 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
693 if (Result != ISD::SETCC_INVALID)
694 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
695 }
696 }
697 // fold (and (zext x), (zext y)) -> (zext (and x, y))
698 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
699 N1.getOpcode() == ISD::ZERO_EXTEND &&
700 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
701 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
702 N0.getOperand(0), N1.getOperand(0));
703 WorkList.push_back(ANDNode.Val);
704 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
705 }
Nate Begeman452d7be2005-09-16 00:54:12 +0000706 // fold (and (shl/srl x), (shl/srl y)) -> (shl/srl (and x, y))
707 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
708 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL)) &&
709 N0.getOperand(1) == N1.getOperand(1)) {
710 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
711 N0.getOperand(0), N1.getOperand(0));
712 WorkList.push_back(ANDNode.Val);
713 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
714 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000715 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000716}
717
Nate Begeman83e75ec2005-09-06 04:43:02 +0000718SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000719 SDOperand N0 = N->getOperand(0);
720 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000721 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000722 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
723 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000724 MVT::ValueType VT = N1.getValueType();
725 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000726
727 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000728 if (N0C && N1C)
729 return DAG.getConstant(N0C->getValue() | N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000730 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000731 // canonicalize constant to RHS
732 if (N0C && !N1C) {
733 std::swap(N0, N1);
734 std::swap(N0C, N1C);
735 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000736 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000737 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000738 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000739 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000740 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000741 return N1;
742 // fold (or x, c) -> c iff (x & ~c) == 0
743 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
744 TLI))
745 return N1;
Nate Begeman223df222005-09-08 20:18:10 +0000746 // fold (or (or x, c1), c2) -> (or x, c1|c2)
747 if (N1C && N0.getOpcode() == ISD::OR) {
748 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
749 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
750 if (N00C)
751 return DAG.getNode(ISD::OR, VT, N0.getOperand(1),
752 DAG.getConstant(N1C->getValue()|N00C->getValue(), VT));
753 if (N01C)
754 return DAG.getNode(ISD::OR, VT, N0.getOperand(0),
755 DAG.getConstant(N1C->getValue()|N01C->getValue(), VT));
756 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000757 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
758 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
759 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
760 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
761
762 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
763 MVT::isInteger(LL.getValueType())) {
764 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
765 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
766 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
767 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
768 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
769 WorkList.push_back(ORNode.Val);
770 return DAG.getSetCC(VT, ORNode, LR, Op1);
771 }
772 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
773 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
774 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
775 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
776 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
777 WorkList.push_back(ANDNode.Val);
778 return DAG.getSetCC(VT, ANDNode, LR, Op1);
779 }
780 }
781 // canonicalize equivalent to ll == rl
782 if (LL == RR && LR == RL) {
783 Op1 = ISD::getSetCCSwappedOperands(Op1);
784 std::swap(RL, RR);
785 }
786 if (LL == RL && LR == RR) {
787 bool isInteger = MVT::isInteger(LL.getValueType());
788 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
789 if (Result != ISD::SETCC_INVALID)
790 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
791 }
792 }
793 // fold (or (zext x), (zext y)) -> (zext (or x, y))
794 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
795 N1.getOpcode() == ISD::ZERO_EXTEND &&
796 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
797 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
798 N0.getOperand(0), N1.getOperand(0));
799 WorkList.push_back(ORNode.Val);
800 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
801 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000802 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000803}
804
Nate Begeman83e75ec2005-09-06 04:43:02 +0000805SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000806 SDOperand N0 = N->getOperand(0);
807 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000808 SDOperand LHS, RHS, CC;
809 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
810 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000811 MVT::ValueType VT = N0.getValueType();
812
813 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000814 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000815 return DAG.getConstant(N0C->getValue() ^ N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000816 // canonicalize constant to RHS
817 if (N0C && !N1C) {
818 std::swap(N0, N1);
819 std::swap(N0C, N1C);
820 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000821 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000822 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000823 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000824 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +0000825 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
826 bool isInt = MVT::isInteger(LHS.getValueType());
827 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
828 isInt);
829 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000830 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000831 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000832 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000833 assert(0 && "Unhandled SetCC Equivalent!");
834 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000835 }
Nate Begeman99801192005-09-07 23:25:52 +0000836 // fold !(x or y) -> (!x and !y) iff x or y are setcc
837 if (N1C && N1C->getValue() == 1 &&
838 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000839 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000840 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
841 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000842 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
843 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000844 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
845 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000846 }
847 }
Nate Begeman99801192005-09-07 23:25:52 +0000848 // fold !(x or y) -> (!x and !y) iff x or y are constants
849 if (N1C && N1C->isAllOnesValue() &&
850 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000851 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000852 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
853 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000854 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
855 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000856 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
857 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000858 }
859 }
Nate Begeman223df222005-09-08 20:18:10 +0000860 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
861 if (N1C && N0.getOpcode() == ISD::XOR) {
862 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
863 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
864 if (N00C)
865 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
866 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
867 if (N01C)
868 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
869 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
870 }
871 // fold (xor x, x) -> 0
872 if (N0 == N1)
873 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000874 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
875 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
876 N1.getOpcode() == ISD::ZERO_EXTEND &&
877 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
878 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
879 N0.getOperand(0), N1.getOperand(0));
880 WorkList.push_back(XORNode.Val);
881 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
882 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000883 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000884}
885
Nate Begeman83e75ec2005-09-06 04:43:02 +0000886SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000887 SDOperand N0 = N->getOperand(0);
888 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000889 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
890 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000891 MVT::ValueType VT = N0.getValueType();
892 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
893
894 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000895 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000896 return DAG.getConstant(N0C->getValue() << N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000897 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000898 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000899 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000900 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000901 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000902 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000903 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000904 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000905 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000906 // if (shl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +0000907 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
908 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000909 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000910 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000911 N0.getOperand(1).getOpcode() == ISD::Constant) {
912 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000913 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000914 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000915 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000916 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000917 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000918 }
919 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
920 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000921 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000922 N0.getOperand(1).getOpcode() == ISD::Constant) {
923 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000924 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000925 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
926 DAG.getConstant(~0ULL << c1, VT));
927 if (c2 > c1)
928 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000929 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000930 else
Nate Begeman83e75ec2005-09-06 04:43:02 +0000931 return DAG.getNode(ISD::SRL, VT, Mask,
932 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000933 }
934 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000935 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +0000936 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000937 DAG.getConstant(~0ULL << N1C->getValue(), VT));
938 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000939}
940
Nate Begeman83e75ec2005-09-06 04:43:02 +0000941SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000942 SDOperand N0 = N->getOperand(0);
943 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000944 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
945 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000946 MVT::ValueType VT = N0.getValueType();
947 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
948
949 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000950 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000951 return DAG.getConstant(N0C->getSignExtended() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000952 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000953 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000954 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000955 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000956 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000957 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000958 // fold (sra x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000959 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000960 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000961 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000962 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000963 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000964 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begeman646d7e22005-09-02 21:18:40 +0000965 if (N1C && MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1)), TLI))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000966 return DAG.getNode(ISD::SRL, VT, N0, N1);
967 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000968}
969
Nate Begeman83e75ec2005-09-06 04:43:02 +0000970SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000971 SDOperand N0 = N->getOperand(0);
972 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000973 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
974 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000975 MVT::ValueType VT = N0.getValueType();
976 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
977
978 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000979 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000980 return DAG.getConstant(N0C->getValue() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000981 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000982 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000983 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000984 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000985 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000986 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000987 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000988 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000989 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000990 // if (srl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +0000991 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
992 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000993 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000994 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000995 N0.getOperand(1).getOpcode() == ISD::Constant) {
996 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000997 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000998 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000999 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001000 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001001 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001002 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001003 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001004}
1005
Nate Begeman83e75ec2005-09-06 04:43:02 +00001006SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001007 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001008 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001009
1010 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001011 if (N0C)
1012 return DAG.getConstant(CountLeadingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001013 N0.getValueType());
1014 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001015}
1016
Nate Begeman83e75ec2005-09-06 04:43:02 +00001017SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001018 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001019 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001020
1021 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001022 if (N0C)
1023 return DAG.getConstant(CountTrailingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001024 N0.getValueType());
1025 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001026}
1027
Nate Begeman83e75ec2005-09-06 04:43:02 +00001028SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001029 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001030 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001031
1032 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001033 if (N0C)
1034 return DAG.getConstant(CountPopulation_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001035 N0.getValueType());
1036 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001037}
1038
Nate Begeman452d7be2005-09-16 00:54:12 +00001039SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1040 SDOperand N0 = N->getOperand(0);
1041 SDOperand N1 = N->getOperand(1);
1042 SDOperand N2 = N->getOperand(2);
1043 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1044 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1045 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1046 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001047
Nate Begeman452d7be2005-09-16 00:54:12 +00001048 // fold select C, X, X -> X
1049 if (N1 == N2)
1050 return N1;
1051 // fold select true, X, Y -> X
1052 if (N0C && !N0C->isNullValue())
1053 return N1;
1054 // fold select false, X, Y -> Y
1055 if (N0C && N0C->isNullValue())
1056 return N2;
1057 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001058 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001059 return DAG.getNode(ISD::OR, VT, N0, N2);
1060 // fold select C, 0, X -> ~C & X
1061 // FIXME: this should check for C type == X type, not i1?
1062 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1063 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1064 WorkList.push_back(XORNode.Val);
1065 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1066 }
1067 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001068 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001069 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1070 WorkList.push_back(XORNode.Val);
1071 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1072 }
1073 // fold select C, X, 0 -> C & X
1074 // FIXME: this should check for C type == X type, not i1?
1075 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1076 return DAG.getNode(ISD::AND, VT, N0, N1);
1077 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1078 if (MVT::i1 == VT && N0 == N1)
1079 return DAG.getNode(ISD::OR, VT, N0, N2);
1080 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1081 if (MVT::i1 == VT && N0 == N2)
1082 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman44728a72005-09-19 22:34:01 +00001083 // fold selects based on a setcc into other things, such as min/max/abs
1084 if (N0.getOpcode() == ISD::SETCC)
1085 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001086 return SDOperand();
1087}
1088
1089SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001090 SDOperand N0 = N->getOperand(0);
1091 SDOperand N1 = N->getOperand(1);
1092 SDOperand N2 = N->getOperand(2);
1093 SDOperand N3 = N->getOperand(3);
1094 SDOperand N4 = N->getOperand(4);
1095 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1096 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1097 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1098 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1099
1100 // Determine if the condition we're dealing with is constant
1101 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC);
1102 ConstantSDNode *SCCC = dyn_cast<ConstantSDNode>(SCC);
1103 bool constTrue = SCCC && SCCC->getValue() == 1;
1104 bool constFalse = SCCC && SCCC->isNullValue();
1105
1106 // fold select_cc lhs, rhs, x, x, cc -> x
1107 if (N2 == N3)
1108 return N2;
1109 // fold select_cc true, x, y -> x
1110 if (constTrue)
1111 return N2;
1112 // fold select_cc false, x, y -> y
1113 if (constFalse)
1114 return N3;
1115 // fold select_cc into other things, such as min/max/abs
1116 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001117}
1118
1119SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1120 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1121 cast<CondCodeSDNode>(N->getOperand(2))->get());
1122}
1123
Nate Begeman83e75ec2005-09-06 04:43:02 +00001124SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001125 SDOperand N0 = N->getOperand(0);
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);
1128
Nate Begeman1d4d4142005-09-01 00:19:25 +00001129 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001130 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001131 return DAG.getConstant(N0C->getSignExtended(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001132 // fold (sext (sext x)) -> (sext x)
1133 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001134 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
1135 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001136}
1137
Nate Begeman83e75ec2005-09-06 04:43:02 +00001138SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001139 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001140 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001141 MVT::ValueType VT = N->getValueType(0);
1142
Nate Begeman1d4d4142005-09-01 00:19:25 +00001143 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001144 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001145 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001146 // fold (zext (zext x)) -> (zext x)
1147 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001148 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
1149 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001150}
1151
Nate Begeman83e75ec2005-09-06 04:43:02 +00001152SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001153 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001154 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001155 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001156 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001157 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001158
Nate Begeman1d4d4142005-09-01 00:19:25 +00001159 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001160 if (N0C) {
1161 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001162 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001163 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001164 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001165 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman646d7e22005-09-02 21:18:40 +00001166 cast<VTSDNode>(N0.getOperand(1))->getVT() < EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001167 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001168 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001169 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1170 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1171 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001172 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001173 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001174 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1175 if (N0.getOpcode() == ISD::AssertSext &&
1176 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001177 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001178 }
1179 // fold (sext_in_reg (sextload x)) -> (sextload x)
1180 if (N0.getOpcode() == ISD::SEXTLOAD &&
1181 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001182 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001183 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001184 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001185 // FIXME: teach isSetCCEquivalent about 0, -1 and then use it here
Nate Begeman1d4d4142005-09-01 00:19:25 +00001186 if (N0.getOpcode() == ISD::SETCC &&
1187 TLI.getSetCCResultContents() ==
1188 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001189 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001190 // FIXME: this code is currently just ported over from SelectionDAG.cpp
1191 // we probably actually want to handle this in two pieces. Rather than
1192 // checking all the top bits for zero, just check the sign bit here and turn
1193 // it into a zero extend inreg (AND with constant).
1194 // then, let the code for AND figure out if the mask is superfluous rather
1195 // than doing so here.
1196 if (N0.getOpcode() == ISD::AND &&
1197 N0.getOperand(1).getOpcode() == ISD::Constant) {
1198 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1199 unsigned NumBits = MVT::getSizeInBits(EVT);
1200 if ((Mask & (~0ULL << (NumBits-1))) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001201 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001202 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001203 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001204}
1205
Nate Begeman83e75ec2005-09-06 04:43:02 +00001206SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001207 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001208 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001209 MVT::ValueType VT = N->getValueType(0);
1210
1211 // noop truncate
1212 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001213 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001214 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001215 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001216 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001217 // fold (truncate (truncate x)) -> (truncate x)
1218 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001219 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001220 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1221 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1222 if (N0.getValueType() < VT)
1223 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001224 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001225 else if (N0.getValueType() > VT)
1226 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001227 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001228 else
1229 // if the source and dest are the same type, we can drop both the extend
1230 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001231 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001232 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001233 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001234}
1235
Nate Begeman83e75ec2005-09-06 04:43:02 +00001236SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001237 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001238 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001239
1240 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001241 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001242 return DAG.getConstantFP(N0C->getSignExtended(), N->getValueType(0));
1243 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001244}
1245
Nate Begeman83e75ec2005-09-06 04:43:02 +00001246SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001247 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001248 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001249
1250 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001251 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001252 return DAG.getConstantFP(N0C->getValue(), N->getValueType(0));
1253 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001254}
1255
Nate Begeman83e75ec2005-09-06 04:43:02 +00001256SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001257 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001258
1259 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001260 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001261 return DAG.getConstant((int64_t)N0CFP->getValue(), N->getValueType(0));
1262 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001263}
1264
Nate Begeman83e75ec2005-09-06 04:43:02 +00001265SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001266 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001267
1268 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001269 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001270 return DAG.getConstant((uint64_t)N0CFP->getValue(), N->getValueType(0));
1271 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001272}
1273
Nate Begeman83e75ec2005-09-06 04:43:02 +00001274SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001275 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001276
1277 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001278 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001279 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1280 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001281}
1282
Nate Begeman83e75ec2005-09-06 04:43:02 +00001283SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001284 SDOperand N0 = N->getOperand(0);
1285 MVT::ValueType VT = N->getValueType(0);
1286 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00001287 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001288
Nate Begeman1d4d4142005-09-01 00:19:25 +00001289 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001290 if (N0CFP) {
1291 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001292 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001293 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001294 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001295}
1296
Nate Begeman83e75ec2005-09-06 04:43:02 +00001297SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001298 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001299
1300 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001301 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001302 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1303 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001304}
1305
Nate Begeman83e75ec2005-09-06 04:43:02 +00001306SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001307 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001308 // fold (neg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001309 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001310 return DAG.getConstantFP(-N0CFP->getValue(), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001311 // fold (neg (sub x, y)) -> (sub y, x)
1312 if (N->getOperand(0).getOpcode() == ISD::SUB)
1313 return DAG.getNode(ISD::SUB, N->getValueType(0), N->getOperand(1),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001314 N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001315 // fold (neg (neg x)) -> x
1316 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001317 return N->getOperand(0).getOperand(0);
1318 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001319}
1320
Nate Begeman83e75ec2005-09-06 04:43:02 +00001321SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001322 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001323 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001324 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001325 return DAG.getConstantFP(fabs(N0CFP->getValue()), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001326 // fold (fabs (fabs x)) -> (fabs x)
1327 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001328 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001329 // fold (fabs (fneg x)) -> (fabs x)
1330 if (N->getOperand(0).getOpcode() == ISD::FNEG)
1331 return DAG.getNode(ISD::FABS, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001332 N->getOperand(0).getOperand(0));
1333 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001334}
1335
Nate Begeman44728a72005-09-19 22:34:01 +00001336SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
1337 SDOperand Chain = N->getOperand(0);
1338 SDOperand N1 = N->getOperand(1);
1339 SDOperand N2 = N->getOperand(2);
1340 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1341
1342 // never taken branch, fold to chain
1343 if (N1C && N1C->isNullValue())
1344 return Chain;
1345 // unconditional branch
1346 if (N1C && !N1C->isNullValue())
1347 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1348 return SDOperand();
1349}
1350
1351SDOperand DAGCombiner::visitBRCONDTWOWAY(SDNode *N) {
1352 SDOperand Chain = N->getOperand(0);
1353 SDOperand N1 = N->getOperand(1);
1354 SDOperand N2 = N->getOperand(2);
1355 SDOperand N3 = N->getOperand(3);
1356 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1357
1358 // unconditional branch to true mbb
1359 if (N1C && N1C->getValue() == 1)
1360 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1361 // unconditional branch to false mbb
1362 if (N1C && N1C->isNullValue())
1363 return DAG.getNode(ISD::BR, MVT::Other, Chain, N3);
1364 return SDOperand();
1365}
1366
1367SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
1368 // FIXME: come up with a common way between br_cc, brtwoway_cc, and select_cc
1369 // to canonicalize the condition without calling getnode a bazillion times.
1370 return SDOperand();
1371}
1372
1373SDOperand DAGCombiner::visitBRTWOWAY_CC(SDNode *N) {
1374 // FIXME: come up with a common way between br_cc, brtwoway_cc, and select_cc
1375 // to canonicalize the condition without calling getnode a bazillion times.
1376 return SDOperand();
1377}
1378
1379SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
1380 return SDOperand();
1381}
1382
1383SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
1384 SDOperand N2, SDOperand N3,
1385 ISD::CondCode CC) {
1386 return SDOperand();
1387}
1388
Nate Begeman452d7be2005-09-16 00:54:12 +00001389SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
1390 SDOperand N1, ISD::CondCode Cond) {
1391 // These setcc operations always fold.
1392 switch (Cond) {
1393 default: break;
1394 case ISD::SETFALSE:
1395 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1396 case ISD::SETTRUE:
1397 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
1398 }
1399
1400 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1401 uint64_t C1 = N1C->getValue();
1402 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
1403 uint64_t C0 = N0C->getValue();
1404
1405 // Sign extend the operands if required
1406 if (ISD::isSignedIntSetCC(Cond)) {
1407 C0 = N0C->getSignExtended();
1408 C1 = N1C->getSignExtended();
1409 }
1410
1411 switch (Cond) {
1412 default: assert(0 && "Unknown integer setcc!");
1413 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
1414 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
1415 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
1416 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
1417 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
1418 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
1419 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
1420 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
1421 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
1422 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
1423 }
1424 } else {
1425 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
1426 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
1427 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
1428
1429 // If the comparison constant has bits in the upper part, the
1430 // zero-extended value could never match.
1431 if (C1 & (~0ULL << InSize)) {
1432 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
1433 switch (Cond) {
1434 case ISD::SETUGT:
1435 case ISD::SETUGE:
1436 case ISD::SETEQ: return DAG.getConstant(0, VT);
1437 case ISD::SETULT:
1438 case ISD::SETULE:
1439 case ISD::SETNE: return DAG.getConstant(1, VT);
1440 case ISD::SETGT:
1441 case ISD::SETGE:
1442 // True if the sign bit of C1 is set.
1443 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
1444 case ISD::SETLT:
1445 case ISD::SETLE:
1446 // True if the sign bit of C1 isn't set.
1447 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
1448 default:
1449 break;
1450 }
1451 }
1452
1453 // Otherwise, we can perform the comparison with the low bits.
1454 switch (Cond) {
1455 case ISD::SETEQ:
1456 case ISD::SETNE:
1457 case ISD::SETUGT:
1458 case ISD::SETUGE:
1459 case ISD::SETULT:
1460 case ISD::SETULE:
1461 return DAG.getSetCC(VT, N0.getOperand(0),
1462 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
1463 Cond);
1464 default:
1465 break; // todo, be more careful with signed comparisons
1466 }
1467 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1468 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1469 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
1470 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
1471 MVT::ValueType ExtDstTy = N0.getValueType();
1472 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
1473
1474 // If the extended part has any inconsistent bits, it cannot ever
1475 // compare equal. In other words, they have to be all ones or all
1476 // zeros.
1477 uint64_t ExtBits =
1478 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
1479 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
1480 return DAG.getConstant(Cond == ISD::SETNE, VT);
1481
1482 SDOperand ZextOp;
1483 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
1484 if (Op0Ty == ExtSrcTy) {
1485 ZextOp = N0.getOperand(0);
1486 } else {
1487 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
1488 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
1489 DAG.getConstant(Imm, Op0Ty));
1490 }
1491 WorkList.push_back(ZextOp.Val);
1492 // Otherwise, make this a use of a zext.
1493 return DAG.getSetCC(VT, ZextOp,
1494 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
1495 ExtDstTy),
1496 Cond);
1497 }
1498
1499 uint64_t MinVal, MaxVal;
1500 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
1501 if (ISD::isSignedIntSetCC(Cond)) {
1502 MinVal = 1ULL << (OperandBitSize-1);
1503 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
1504 MaxVal = ~0ULL >> (65-OperandBitSize);
1505 else
1506 MaxVal = 0;
1507 } else {
1508 MinVal = 0;
1509 MaxVal = ~0ULL >> (64-OperandBitSize);
1510 }
1511
1512 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
1513 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
1514 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
1515 --C1; // X >= C0 --> X > (C0-1)
1516 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
1517 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
1518 }
1519
1520 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
1521 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
1522 ++C1; // X <= C0 --> X < (C0+1)
1523 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
1524 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
1525 }
1526
1527 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
1528 return DAG.getConstant(0, VT); // X < MIN --> false
1529
1530 // Canonicalize setgt X, Min --> setne X, Min
1531 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
1532 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
1533
1534 // If we have setult X, 1, turn it into seteq X, 0
1535 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
1536 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
1537 ISD::SETEQ);
1538 // If we have setugt X, Max-1, turn it into seteq X, Max
1539 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
1540 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
1541 ISD::SETEQ);
1542
1543 // If we have "setcc X, C0", check to see if we can shrink the immediate
1544 // by changing cc.
1545
1546 // SETUGT X, SINTMAX -> SETLT X, 0
1547 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
1548 C1 == (~0ULL >> (65-OperandBitSize)))
1549 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
1550 ISD::SETLT);
1551
1552 // FIXME: Implement the rest of these.
1553
1554 // Fold bit comparisons when we can.
1555 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1556 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
1557 if (ConstantSDNode *AndRHS =
1558 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1559 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1560 // Perform the xform if the AND RHS is a single bit.
1561 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
1562 return DAG.getNode(ISD::SRL, VT, N0,
1563 DAG.getConstant(Log2_64(AndRHS->getValue()),
1564 TLI.getShiftAmountTy()));
1565 }
1566 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
1567 // (X & 8) == 8 --> (X & 8) >> 3
1568 // Perform the xform if C1 is a single bit.
1569 if ((C1 & (C1-1)) == 0) {
1570 return DAG.getNode(ISD::SRL, VT, N0,
1571 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
1572 }
1573 }
1574 }
1575 }
1576 } else if (isa<ConstantSDNode>(N0.Val)) {
1577 // Ensure that the constant occurs on the RHS.
1578 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1579 }
1580
1581 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
1582 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
1583 double C0 = N0C->getValue(), C1 = N1C->getValue();
1584
1585 switch (Cond) {
1586 default: break; // FIXME: Implement the rest of these!
1587 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
1588 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
1589 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
1590 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
1591 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
1592 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
1593 }
1594 } else {
1595 // Ensure that the constant occurs on the RHS.
1596 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1597 }
1598
1599 if (N0 == N1) {
1600 // We can always fold X == Y for integer setcc's.
1601 if (MVT::isInteger(N0.getValueType()))
1602 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1603 unsigned UOF = ISD::getUnorderedFlavor(Cond);
1604 if (UOF == 2) // FP operators that are undefined on NaNs.
1605 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1606 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
1607 return DAG.getConstant(UOF, VT);
1608 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
1609 // if it is not already.
1610 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
1611 if (NewCond != Cond)
1612 return DAG.getSetCC(VT, N0, N1, NewCond);
1613 }
1614
1615 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1616 MVT::isInteger(N0.getValueType())) {
1617 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
1618 N0.getOpcode() == ISD::XOR) {
1619 // Simplify (X+Y) == (X+Z) --> Y == Z
1620 if (N0.getOpcode() == N1.getOpcode()) {
1621 if (N0.getOperand(0) == N1.getOperand(0))
1622 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
1623 if (N0.getOperand(1) == N1.getOperand(1))
1624 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
1625 if (isCommutativeBinOp(N0.getOpcode())) {
1626 // If X op Y == Y op X, try other combinations.
1627 if (N0.getOperand(0) == N1.getOperand(1))
1628 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
1629 if (N0.getOperand(1) == N1.getOperand(0))
1630 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
1631 }
1632 }
1633
1634 // Simplify (X+Z) == X --> Z == 0
1635 if (N0.getOperand(0) == N1)
1636 return DAG.getSetCC(VT, N0.getOperand(1),
1637 DAG.getConstant(0, N0.getValueType()), Cond);
1638 if (N0.getOperand(1) == N1) {
1639 if (isCommutativeBinOp(N0.getOpcode()))
1640 return DAG.getSetCC(VT, N0.getOperand(0),
1641 DAG.getConstant(0, N0.getValueType()), Cond);
1642 else {
1643 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
1644 // (Z-X) == X --> Z == X<<1
1645 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
1646 N1,
1647 DAG.getConstant(1,TLI.getShiftAmountTy()));
1648 WorkList.push_back(SH.Val);
1649 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
1650 }
1651 }
1652 }
1653
1654 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
1655 N1.getOpcode() == ISD::XOR) {
1656 // Simplify X == (X+Z) --> Z == 0
1657 if (N1.getOperand(0) == N0) {
1658 return DAG.getSetCC(VT, N1.getOperand(1),
1659 DAG.getConstant(0, N1.getValueType()), Cond);
1660 } else if (N1.getOperand(1) == N0) {
1661 if (isCommutativeBinOp(N1.getOpcode())) {
1662 return DAG.getSetCC(VT, N1.getOperand(0),
1663 DAG.getConstant(0, N1.getValueType()), Cond);
1664 } else {
1665 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
1666 // X == (Z-X) --> X<<1 == Z
1667 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
1668 DAG.getConstant(1,TLI.getShiftAmountTy()));
1669 WorkList.push_back(SH.Val);
1670 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
1671 }
1672 }
1673 }
1674 }
1675
1676 // Fold away ALL boolean setcc's.
1677 SDOperand Temp;
1678 if (N0.getValueType() == MVT::i1) {
1679 switch (Cond) {
1680 default: assert(0 && "Unknown integer setcc!");
1681 case ISD::SETEQ: // X == Y -> (X^Y)^1
1682 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1683 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
1684 WorkList.push_back(Temp.Val);
1685 break;
1686 case ISD::SETNE: // X != Y --> (X^Y)
1687 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1688 break;
1689 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
1690 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
1691 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
1692 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
1693 WorkList.push_back(Temp.Val);
1694 break;
1695 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
1696 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
1697 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
1698 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
1699 WorkList.push_back(Temp.Val);
1700 break;
1701 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
1702 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
1703 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
1704 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
1705 WorkList.push_back(Temp.Val);
1706 break;
1707 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
1708 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
1709 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
1710 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
1711 break;
1712 }
1713 if (VT != MVT::i1) {
1714 WorkList.push_back(N0.Val);
1715 // FIXME: If running after legalize, we probably can't do this.
1716 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
1717 }
1718 return N0;
1719 }
1720
1721 // Could not fold it.
1722 return SDOperand();
1723}
1724
Nate Begeman1d4d4142005-09-01 00:19:25 +00001725// SelectionDAG::Combine - This is the entry point for the file.
1726//
Nate Begeman4ebd8052005-09-01 23:24:04 +00001727void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001728 /// run - This is the main entry point to this class.
1729 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00001730 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001731}