blob: e037f2e13c3ee54dda0f19ef49d52c2333f14dce [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
12//
13// FIXME: Missing folds
14// sdiv, udiv, srem, urem (X, const) where X is an integer can be expanded into
15// a sequence of multiplies, shifts, and adds. This should be controlled by
16// some kind of hint from the target that int div is expensive.
17// various folds of mulh[s,u] by constants such as -1, powers of 2, etc.
18//
19// FIXME: Should add a corresponding version of fold AND with
20// ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
21// we don't have yet.
22//
Nate Begeman44728a72005-09-19 22:34:01 +000023// FIXME: select C, 16, 0 -> shr C, 4
24// FIXME: select C, pow2, pow2 -> something smart
25// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
26// FIXME: (select C, load A, load B) -> load (select C, A, B)
27// FIXME: store -> load -> forward substitute
28// FIXME: Dead stores -> nuke
29// FIXME: shr X, (and Y,31) -> shr X, Y
30// FIXME: TRUNC (LOAD) -> EXT_LOAD/LOAD(smaller)
Nate Begeman1d4d4142005-09-01 00:19:25 +000031// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000032// FIXME: undef values
Nate Begeman1d4d4142005-09-01 00:19:25 +000033// FIXME: zero extend when top bits are 0 -> drop it ?
Nate Begeman4ebd8052005-09-01 23:24:04 +000034// FIXME: make truncate see through SIGN_EXTEND and AND
35// FIXME: sext_in_reg(setcc) on targets that return zero or one, and where
36// EVT != MVT::i1 can drop the sext.
Nate Begeman4ebd8052005-09-01 23:24:04 +000037// FIXME: (sra (sra x, c1), c2) -> (sra x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +000038// FIXME: verify that getNode can't return extends with an operand whose type
39// is >= to that of the extend.
40// FIXME: divide by zero is currently left unfolded. do we want to turn this
41// into an undef?
Nate Begeman1d4d4142005-09-01 00:19:25 +000042//
43//===----------------------------------------------------------------------===//
44
45#define DEBUG_TYPE "dagcombine"
46#include "llvm/ADT/Statistic.h"
47#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000048#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000049#include "llvm/Support/MathExtras.h"
50#include "llvm/Target/TargetLowering.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000051#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000052#include <cmath>
53using namespace llvm;
54
55namespace {
56 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
57
58 class DAGCombiner {
59 SelectionDAG &DAG;
60 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000061 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000062
63 // Worklist of all of the nodes that need to be simplified.
64 std::vector<SDNode*> WorkList;
65
66 /// AddUsersToWorkList - When an instruction is simplified, add all users of
67 /// the instruction to the work lists because they might get more simplified
68 /// now.
69 ///
70 void AddUsersToWorkList(SDNode *N) {
71 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000072 UI != UE; ++UI)
73 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000074 }
75
76 /// removeFromWorkList - remove all instances of N from the worklist.
77 void removeFromWorkList(SDNode *N) {
78 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
79 WorkList.end());
80 }
81
82 /// visit - call the node-specific routine that knows how to fold each
83 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +000084 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +000085
86 // Visitation implementation - Implement dag node combining for different
87 // node types. The semantics are as follows:
88 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +000089 // SDOperand.Val == 0 - No change was made
90 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +000091 //
Nate Begeman83e75ec2005-09-06 04:43:02 +000092 SDOperand visitTokenFactor(SDNode *N);
93 SDOperand visitADD(SDNode *N);
94 SDOperand visitSUB(SDNode *N);
95 SDOperand visitMUL(SDNode *N);
96 SDOperand visitSDIV(SDNode *N);
97 SDOperand visitUDIV(SDNode *N);
98 SDOperand visitSREM(SDNode *N);
99 SDOperand visitUREM(SDNode *N);
100 SDOperand visitMULHU(SDNode *N);
101 SDOperand visitMULHS(SDNode *N);
102 SDOperand visitAND(SDNode *N);
103 SDOperand visitOR(SDNode *N);
104 SDOperand visitXOR(SDNode *N);
105 SDOperand visitSHL(SDNode *N);
106 SDOperand visitSRA(SDNode *N);
107 SDOperand visitSRL(SDNode *N);
108 SDOperand visitCTLZ(SDNode *N);
109 SDOperand visitCTTZ(SDNode *N);
110 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000111 SDOperand visitSELECT(SDNode *N);
112 SDOperand visitSELECT_CC(SDNode *N);
113 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000114 SDOperand visitSIGN_EXTEND(SDNode *N);
115 SDOperand visitZERO_EXTEND(SDNode *N);
116 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
117 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000118
119 SDOperand visitFADD(SDNode *N);
120 SDOperand visitFSUB(SDNode *N);
121 SDOperand visitFMUL(SDNode *N);
122 SDOperand visitFDIV(SDNode *N);
123 SDOperand visitFREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000124 SDOperand visitSINT_TO_FP(SDNode *N);
125 SDOperand visitUINT_TO_FP(SDNode *N);
126 SDOperand visitFP_TO_SINT(SDNode *N);
127 SDOperand visitFP_TO_UINT(SDNode *N);
128 SDOperand visitFP_ROUND(SDNode *N);
129 SDOperand visitFP_ROUND_INREG(SDNode *N);
130 SDOperand visitFP_EXTEND(SDNode *N);
131 SDOperand visitFNEG(SDNode *N);
132 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000133 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000134 SDOperand visitBRCONDTWOWAY(SDNode *N);
135 SDOperand visitBR_CC(SDNode *N);
136 SDOperand visitBRTWOWAY_CC(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000137
Nate Begeman44728a72005-09-19 22:34:01 +0000138 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
139 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
140 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000141 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000142 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000143public:
144 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000145 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000146
147 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000148 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000149 };
150}
151
152/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
153/// this predicate to simplify operations downstream. V and Mask are known to
154/// be the same type.
155static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
156 const TargetLowering &TLI) {
157 unsigned SrcBits;
158 if (Mask == 0) return true;
159
160 // If we know the result of a setcc has the top bits zero, use this info.
161 switch (Op.getOpcode()) {
Nate Begeman4ebd8052005-09-01 23:24:04 +0000162 case ISD::Constant:
163 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
164 case ISD::SETCC:
Nate Begeman646d7e22005-09-02 21:18:40 +0000165 // FIXME: teach this about non ZeroOrOne values, such as 0 or -1
Nate Begeman4ebd8052005-09-01 23:24:04 +0000166 return ((Mask & 1) == 0) &&
167 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
168 case ISD::ZEXTLOAD:
169 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
170 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
171 case ISD::ZERO_EXTEND:
172 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
173 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
174 case ISD::AssertZext:
175 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
176 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
177 case ISD::AND:
178 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
179 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
180 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
181 // FALL THROUGH
182 case ISD::OR:
183 case ISD::XOR:
184 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
185 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
186 case ISD::SELECT:
187 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
188 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
189 case ISD::SELECT_CC:
190 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
191 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
192 case ISD::SRL:
193 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
194 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
195 uint64_t NewVal = Mask << ShAmt->getValue();
196 SrcBits = MVT::getSizeInBits(Op.getValueType());
197 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
198 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
199 }
200 return false;
201 case ISD::SHL:
202 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
203 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
204 uint64_t NewVal = Mask >> ShAmt->getValue();
205 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
206 }
207 return false;
Chris Lattnerc4ced262005-10-07 15:30:32 +0000208 case ISD::SUB:
209 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
210 // We know that the top bits of C-X are clear if X contains less bits
211 // than C (i.e. no wrap-around can happen). For example, 20-X is
212 // positive if we can prove that X is >= 0 and < 16.
213 unsigned Bits = MVT::getSizeInBits(CLHS->getValueType(0));
214 if ((CLHS->getValue() & (1 << (Bits-1))) == 0) { // sign bit clear
215 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
216 uint64_t MaskV = (1ULL << (63-NLZ))-1;
217 if (MaskedValueIsZero(Op.getOperand(1), ~MaskV, TLI)) {
218 // High bits are clear this value is known to be >= C.
219 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
220 if ((Mask & ((1ULL << (64-NLZ2))-1)) == 0)
221 return true;
222 }
223 }
224 }
225 break;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000226 case ISD::CTTZ:
227 case ISD::CTLZ:
228 case ISD::CTPOP:
229 // Bit counting instructions can not set the high bits of the result
230 // register. The max number of bits sets depends on the input.
231 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
232
233 // TODO we could handle some SRA cases here.
234 default: break;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000235 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000236 return false;
237}
238
Nate Begeman4ebd8052005-09-01 23:24:04 +0000239// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
240// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000241// Also, set the incoming LHS, RHS, and CC references to the appropriate
242// nodes based on the type of node we are checking. This simplifies life a
243// bit for the callers.
244static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
245 SDOperand &CC) {
246 if (N.getOpcode() == ISD::SETCC) {
247 LHS = N.getOperand(0);
248 RHS = N.getOperand(1);
249 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000250 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000251 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000252 if (N.getOpcode() == ISD::SELECT_CC &&
253 N.getOperand(2).getOpcode() == ISD::Constant &&
254 N.getOperand(3).getOpcode() == ISD::Constant &&
255 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000256 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
257 LHS = N.getOperand(0);
258 RHS = N.getOperand(1);
259 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000260 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000261 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000262 return false;
263}
264
Nate Begeman99801192005-09-07 23:25:52 +0000265// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
266// one use. If this is true, it allows the users to invert the operation for
267// free when it is profitable to do so.
268static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000269 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000270 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000271 return true;
272 return false;
273}
274
Nate Begeman452d7be2005-09-16 00:54:12 +0000275// FIXME: This should probably go in the ISD class rather than being duplicated
276// in several files.
277static bool isCommutativeBinOp(unsigned Opcode) {
278 switch (Opcode) {
279 case ISD::ADD:
280 case ISD::MUL:
281 case ISD::AND:
282 case ISD::OR:
283 case ISD::XOR: return true;
284 default: return false; // FIXME: Need commutative info for user ops!
285 }
286}
287
Nate Begeman4ebd8052005-09-01 23:24:04 +0000288void DAGCombiner::Run(bool RunningAfterLegalize) {
289 // set the instance variable, so that the various visit routines may use it.
290 AfterLegalize = RunningAfterLegalize;
291
Nate Begeman646d7e22005-09-02 21:18:40 +0000292 // Add all the dag nodes to the worklist.
293 WorkList.insert(WorkList.end(), DAG.allnodes_begin(), DAG.allnodes_end());
Nate Begeman83e75ec2005-09-06 04:43:02 +0000294
Chris Lattner95038592005-10-05 06:35:28 +0000295 // Create a dummy node (which is not added to allnodes), that adds a reference
296 // to the root node, preventing it from being deleted, and tracking any
297 // changes of the root.
298 HandleSDNode Dummy(DAG.getRoot());
299
Nate Begeman1d4d4142005-09-01 00:19:25 +0000300 // while the worklist isn't empty, inspect the node on the end of it and
301 // try and combine it.
302 while (!WorkList.empty()) {
303 SDNode *N = WorkList.back();
304 WorkList.pop_back();
305
306 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000307 // N is deleted from the DAG, since they too may now be dead or may have a
308 // reduced number of uses, allowing other xforms.
309 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000310 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
311 WorkList.push_back(N->getOperand(i).Val);
312
Nate Begeman1d4d4142005-09-01 00:19:25 +0000313 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000314 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000315 continue;
316 }
317
Nate Begeman83e75ec2005-09-06 04:43:02 +0000318 SDOperand RV = visit(N);
319 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000320 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000321 // If we get back the same node we passed in, rather than a new node or
322 // zero, we know that the node must have defined multiple values and
323 // CombineTo was used. Since CombineTo takes care of the worklist
324 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000325 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000326 DEBUG(std::cerr << "\nReplacing "; N->dump();
327 std::cerr << "\nWith: "; RV.Val->dump();
328 std::cerr << '\n');
Nate Begeman99801192005-09-07 23:25:52 +0000329 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV));
Nate Begeman646d7e22005-09-02 21:18:40 +0000330
331 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000332 WorkList.push_back(RV.Val);
333 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000334
335 // Nodes can end up on the worklist more than once. Make sure we do
336 // not process a node that has been replaced.
337 removeFromWorkList(N);
Chris Lattner5c46f742005-10-05 06:11:08 +0000338
339 // Finally, since the node is now dead, remove it from the graph.
340 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000341 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000342 }
343 }
Chris Lattner95038592005-10-05 06:35:28 +0000344
345 // If the root changed (e.g. it was a dead load, update the root).
346 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000347}
348
Nate Begeman83e75ec2005-09-06 04:43:02 +0000349SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000350 switch(N->getOpcode()) {
351 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000352 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000353 case ISD::ADD: return visitADD(N);
354 case ISD::SUB: return visitSUB(N);
355 case ISD::MUL: return visitMUL(N);
356 case ISD::SDIV: return visitSDIV(N);
357 case ISD::UDIV: return visitUDIV(N);
358 case ISD::SREM: return visitSREM(N);
359 case ISD::UREM: return visitUREM(N);
360 case ISD::MULHU: return visitMULHU(N);
361 case ISD::MULHS: return visitMULHS(N);
362 case ISD::AND: return visitAND(N);
363 case ISD::OR: return visitOR(N);
364 case ISD::XOR: return visitXOR(N);
365 case ISD::SHL: return visitSHL(N);
366 case ISD::SRA: return visitSRA(N);
367 case ISD::SRL: return visitSRL(N);
368 case ISD::CTLZ: return visitCTLZ(N);
369 case ISD::CTTZ: return visitCTTZ(N);
370 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000371 case ISD::SELECT: return visitSELECT(N);
372 case ISD::SELECT_CC: return visitSELECT_CC(N);
373 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000374 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
375 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
376 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
377 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000378 case ISD::FADD: return visitFADD(N);
379 case ISD::FSUB: return visitFSUB(N);
380 case ISD::FMUL: return visitFMUL(N);
381 case ISD::FDIV: return visitFDIV(N);
382 case ISD::FREM: return visitFREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000383 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
384 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
385 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
386 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
387 case ISD::FP_ROUND: return visitFP_ROUND(N);
388 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
389 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
390 case ISD::FNEG: return visitFNEG(N);
391 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000392 case ISD::BRCOND: return visitBRCOND(N);
393 case ISD::BRCONDTWOWAY: return visitBRCONDTWOWAY(N);
394 case ISD::BR_CC: return visitBR_CC(N);
395 case ISD::BRTWOWAY_CC: return visitBRTWOWAY_CC(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000396 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000397 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000398}
399
Nate Begeman83e75ec2005-09-06 04:43:02 +0000400SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000401 // If the token factor has two operands and one is the entry token, replace
402 // the token factor with the other operand.
403 if (N->getNumOperands() == 2) {
404 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000405 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000406 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000407 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000408 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000409 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000410}
411
Nate Begeman83e75ec2005-09-06 04:43:02 +0000412SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000413 SDOperand N0 = N->getOperand(0);
414 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000415 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
416 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000417 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000418
419 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000420 if (N0C && N1C)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000421 return DAG.getConstant(N0C->getValue() + N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000422 // canonicalize constant to RHS
423 if (N0C && !N1C) {
424 std::swap(N0, N1);
425 std::swap(N0C, N1C);
426 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000427 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000428 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000429 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000430 // fold (add (add x, c1), c2) -> (add x, c1+c2)
431 if (N1C && N0.getOpcode() == ISD::ADD) {
432 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
433 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
434 if (N00C)
435 return DAG.getNode(ISD::ADD, VT, N0.getOperand(1),
436 DAG.getConstant(N1C->getValue()+N00C->getValue(), VT));
437 if (N01C)
438 return DAG.getNode(ISD::ADD, VT, N0.getOperand(0),
439 DAG.getConstant(N1C->getValue()+N01C->getValue(), VT));
440 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000441 // fold ((0-A) + B) -> B-A
442 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
443 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000444 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000445 // fold (A + (0-B)) -> A-B
446 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
447 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000448 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000449 // fold (A+(B-A)) -> B
450 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000451 return N1.getOperand(0);
452 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000453}
454
Nate Begeman83e75ec2005-09-06 04:43:02 +0000455SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000456 SDOperand N0 = N->getOperand(0);
457 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000458 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
459 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000460
461 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000462 if (N0C && N1C)
463 return DAG.getConstant(N0C->getValue() - N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000464 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000465 // fold (sub x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000466 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000467 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000468 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000469 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000470 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000471 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000472 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000473 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000474 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000475}
476
Nate Begeman83e75ec2005-09-06 04:43:02 +0000477SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000478 SDOperand N0 = N->getOperand(0);
479 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000480 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
481 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000482 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000483
484 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000485 if (N0C && N1C)
486 return DAG.getConstant(N0C->getValue() * N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000487 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000488 // canonicalize constant to RHS
489 if (N0C && !N1C) {
490 std::swap(N0, N1);
491 std::swap(N0C, N1C);
492 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000493 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000494 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000495 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000496 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000497 if (N1C && N1C->isAllOnesValue())
Nate Begeman1d4d4142005-09-01 00:19:25 +0000498 return DAG.getNode(ISD::SUB, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000499 DAG.getConstant(0, N->getValueType(0)), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000500 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000501 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000502 return DAG.getNode(ISD::SHL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000503 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000504 TLI.getShiftAmountTy()));
Nate Begeman223df222005-09-08 20:18:10 +0000505 // fold (mul (mul x, c1), c2) -> (mul x, c1*c2)
506 if (N1C && N0.getOpcode() == ISD::MUL) {
507 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
508 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
509 if (N00C)
510 return DAG.getNode(ISD::MUL, VT, N0.getOperand(1),
511 DAG.getConstant(N1C->getValue()*N00C->getValue(), VT));
512 if (N01C)
513 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0),
514 DAG.getConstant(N1C->getValue()*N01C->getValue(), VT));
515 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000516 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000517}
518
Nate Begeman83e75ec2005-09-06 04:43:02 +0000519SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000520 SDOperand N0 = N->getOperand(0);
521 SDOperand N1 = N->getOperand(1);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000522 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000523 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
524 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000525
526 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000527 if (N0C && N1C && !N1C->isNullValue())
528 return DAG.getConstant(N0C->getSignExtended() / N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000529 N->getValueType(0));
Chris Lattner094c8fc2005-10-07 06:10:46 +0000530
531 // If we know the sign bits of both operands are zero, strength reduce to a
532 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
533 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
534 if (MaskedValueIsZero(N1, SignBit, TLI) &&
535 MaskedValueIsZero(N0, SignBit, TLI))
536 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
537
538
Nate Begeman83e75ec2005-09-06 04:43:02 +0000539 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000540}
541
Nate Begeman83e75ec2005-09-06 04:43:02 +0000542SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000543 SDOperand N0 = N->getOperand(0);
544 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000545 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
546 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000547
548 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000549 if (N0C && N1C && !N1C->isNullValue())
550 return DAG.getConstant(N0C->getValue() / N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000551 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000552 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000553 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000554 return DAG.getNode(ISD::SRL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000555 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000556 TLI.getShiftAmountTy()));
557 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000558}
559
Nate Begeman83e75ec2005-09-06 04:43:02 +0000560SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000561 SDOperand N0 = N->getOperand(0);
562 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000563 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
564 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000565
566 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000567 if (N0C && N1C && !N1C->isNullValue())
568 return DAG.getConstant(N0C->getSignExtended() % N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000569 N->getValueType(0));
Nate Begeman83e75ec2005-09-06 04:43:02 +0000570 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000571}
572
Nate Begeman83e75ec2005-09-06 04:43:02 +0000573SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000574 SDOperand N0 = N->getOperand(0);
575 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000576 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
577 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000578
579 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000580 if (N0C && N1C && !N1C->isNullValue())
581 return DAG.getConstant(N0C->getValue() % N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000582 N->getValueType(0));
Nate Begeman646d7e22005-09-02 21:18:40 +0000583 // FIXME: c2 power of 2 -> mask?
Nate Begeman83e75ec2005-09-06 04:43:02 +0000584 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000585}
586
Nate Begeman83e75ec2005-09-06 04:43:02 +0000587SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000588 SDOperand N0 = N->getOperand(0);
589 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000590 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000591
592 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000593 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000594 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000595 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000596 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000597 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
598 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000599 TLI.getShiftAmountTy()));
600 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000601}
602
Nate Begeman83e75ec2005-09-06 04:43:02 +0000603SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000604 SDOperand N0 = N->getOperand(0);
605 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000606 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000607
608 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000609 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000610 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000611 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000612 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000613 return DAG.getConstant(0, N0.getValueType());
614 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000615}
616
Nate Begeman83e75ec2005-09-06 04:43:02 +0000617SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000618 SDOperand N0 = N->getOperand(0);
619 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000620 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000621 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
622 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000623 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000624 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000625
626 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000627 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000628 return DAG.getConstant(N0C->getValue() & N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000629 // canonicalize constant to RHS
630 if (N0C && !N1C) {
631 std::swap(N0, N1);
632 std::swap(N0C, N1C);
633 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000634 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000635 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000636 return N0;
637 // if (and x, c) is known to be zero, return 0
638 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
639 return DAG.getConstant(0, VT);
640 // fold (and x, c) -> x iff (x & ~c) == 0
641 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
642 TLI))
643 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000644 // fold (and (and x, c1), c2) -> (and x, c1^c2)
645 if (N1C && N0.getOpcode() == ISD::AND) {
646 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
647 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
648 if (N00C)
649 return DAG.getNode(ISD::AND, VT, N0.getOperand(1),
650 DAG.getConstant(N1C->getValue()&N00C->getValue(), VT));
651 if (N01C)
652 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
653 DAG.getConstant(N1C->getValue()&N01C->getValue(), VT));
654 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000655 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
656 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG) {
657 unsigned ExtendBits =
658 MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT());
Nate Begeman646d7e22005-09-02 21:18:40 +0000659 if ((N1C->getValue() & (~0ULL << ExtendBits)) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000660 return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000661 }
662 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
663 if (N0.getOpcode() == ISD::OR)
664 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000665 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000666 return N1;
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000667 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
668 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
669 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
670 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
671
672 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
673 MVT::isInteger(LL.getValueType())) {
674 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
675 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
676 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
677 WorkList.push_back(ORNode.Val);
678 return DAG.getSetCC(VT, ORNode, LR, Op1);
679 }
680 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
681 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
682 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
683 WorkList.push_back(ANDNode.Val);
684 return DAG.getSetCC(VT, ANDNode, LR, Op1);
685 }
686 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
687 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
688 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
689 WorkList.push_back(ORNode.Val);
690 return DAG.getSetCC(VT, ORNode, LR, Op1);
691 }
692 }
693 // canonicalize equivalent to ll == rl
694 if (LL == RR && LR == RL) {
695 Op1 = ISD::getSetCCSwappedOperands(Op1);
696 std::swap(RL, RR);
697 }
698 if (LL == RL && LR == RR) {
699 bool isInteger = MVT::isInteger(LL.getValueType());
700 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
701 if (Result != ISD::SETCC_INVALID)
702 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
703 }
704 }
705 // fold (and (zext x), (zext y)) -> (zext (and x, y))
706 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
707 N1.getOpcode() == ISD::ZERO_EXTEND &&
708 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
709 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
710 N0.getOperand(0), N1.getOperand(0));
711 WorkList.push_back(ANDNode.Val);
712 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
713 }
Nate Begeman452d7be2005-09-16 00:54:12 +0000714 // fold (and (shl/srl x), (shl/srl y)) -> (shl/srl (and x, y))
715 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
716 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL)) &&
717 N0.getOperand(1) == N1.getOperand(1)) {
718 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
719 N0.getOperand(0), N1.getOperand(0));
720 WorkList.push_back(ANDNode.Val);
721 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
722 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000723 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000724}
725
Nate Begeman83e75ec2005-09-06 04:43:02 +0000726SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000727 SDOperand N0 = N->getOperand(0);
728 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000729 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000730 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
731 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000732 MVT::ValueType VT = N1.getValueType();
733 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000734
735 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000736 if (N0C && N1C)
737 return DAG.getConstant(N0C->getValue() | N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000738 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000739 // canonicalize constant to RHS
740 if (N0C && !N1C) {
741 std::swap(N0, N1);
742 std::swap(N0C, N1C);
743 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000744 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000745 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000746 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000747 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000748 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000749 return N1;
750 // fold (or x, c) -> c iff (x & ~c) == 0
751 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
752 TLI))
753 return N1;
Nate Begeman223df222005-09-08 20:18:10 +0000754 // fold (or (or x, c1), c2) -> (or x, c1|c2)
755 if (N1C && N0.getOpcode() == ISD::OR) {
756 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
757 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
758 if (N00C)
759 return DAG.getNode(ISD::OR, VT, N0.getOperand(1),
760 DAG.getConstant(N1C->getValue()|N00C->getValue(), VT));
761 if (N01C)
762 return DAG.getNode(ISD::OR, VT, N0.getOperand(0),
763 DAG.getConstant(N1C->getValue()|N01C->getValue(), VT));
764 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000765 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
766 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
767 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
768 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
769
770 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
771 MVT::isInteger(LL.getValueType())) {
772 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
773 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
774 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
775 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
776 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
777 WorkList.push_back(ORNode.Val);
778 return DAG.getSetCC(VT, ORNode, LR, Op1);
779 }
780 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
781 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
782 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
783 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
784 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
785 WorkList.push_back(ANDNode.Val);
786 return DAG.getSetCC(VT, ANDNode, LR, Op1);
787 }
788 }
789 // canonicalize equivalent to ll == rl
790 if (LL == RR && LR == RL) {
791 Op1 = ISD::getSetCCSwappedOperands(Op1);
792 std::swap(RL, RR);
793 }
794 if (LL == RL && LR == RR) {
795 bool isInteger = MVT::isInteger(LL.getValueType());
796 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
797 if (Result != ISD::SETCC_INVALID)
798 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
799 }
800 }
801 // fold (or (zext x), (zext y)) -> (zext (or x, y))
802 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
803 N1.getOpcode() == ISD::ZERO_EXTEND &&
804 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
805 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
806 N0.getOperand(0), N1.getOperand(0));
807 WorkList.push_back(ORNode.Val);
808 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
809 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000810 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000811}
812
Nate Begeman83e75ec2005-09-06 04:43:02 +0000813SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000814 SDOperand N0 = N->getOperand(0);
815 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000816 SDOperand LHS, RHS, CC;
817 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
818 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000819 MVT::ValueType VT = N0.getValueType();
820
821 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000822 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000823 return DAG.getConstant(N0C->getValue() ^ N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000824 // canonicalize constant to RHS
825 if (N0C && !N1C) {
826 std::swap(N0, N1);
827 std::swap(N0C, N1C);
828 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000829 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000830 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000831 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000832 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +0000833 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
834 bool isInt = MVT::isInteger(LHS.getValueType());
835 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
836 isInt);
837 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000838 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000839 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000840 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000841 assert(0 && "Unhandled SetCC Equivalent!");
842 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000843 }
Nate Begeman99801192005-09-07 23:25:52 +0000844 // fold !(x or y) -> (!x and !y) iff x or y are setcc
845 if (N1C && N1C->getValue() == 1 &&
846 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000847 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000848 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
849 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000850 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
851 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000852 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
853 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000854 }
855 }
Nate Begeman99801192005-09-07 23:25:52 +0000856 // fold !(x or y) -> (!x and !y) iff x or y are constants
857 if (N1C && N1C->isAllOnesValue() &&
858 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000859 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000860 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
861 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000862 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
863 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000864 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
865 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000866 }
867 }
Nate Begeman223df222005-09-08 20:18:10 +0000868 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
869 if (N1C && N0.getOpcode() == ISD::XOR) {
870 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
871 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
872 if (N00C)
873 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
874 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
875 if (N01C)
876 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
877 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
878 }
879 // fold (xor x, x) -> 0
880 if (N0 == N1)
881 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000882 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
883 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
884 N1.getOpcode() == ISD::ZERO_EXTEND &&
885 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
886 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
887 N0.getOperand(0), N1.getOperand(0));
888 WorkList.push_back(XORNode.Val);
889 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
890 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000891 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000892}
893
Nate Begeman83e75ec2005-09-06 04:43:02 +0000894SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000895 SDOperand N0 = N->getOperand(0);
896 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000897 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
898 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000899 MVT::ValueType VT = N0.getValueType();
900 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
901
902 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000903 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000904 return DAG.getConstant(N0C->getValue() << N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000905 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000906 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000907 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000908 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000909 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000910 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000911 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000912 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000913 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000914 // if (shl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +0000915 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
916 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000917 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000918 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000919 N0.getOperand(1).getOpcode() == ISD::Constant) {
920 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000921 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000922 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000923 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000924 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000925 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000926 }
927 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
928 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000929 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000930 N0.getOperand(1).getOpcode() == ISD::Constant) {
931 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000932 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000933 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
934 DAG.getConstant(~0ULL << c1, VT));
935 if (c2 > c1)
936 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000937 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000938 else
Nate Begeman83e75ec2005-09-06 04:43:02 +0000939 return DAG.getNode(ISD::SRL, VT, Mask,
940 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000941 }
942 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000943 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +0000944 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000945 DAG.getConstant(~0ULL << N1C->getValue(), VT));
946 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000947}
948
Nate Begeman83e75ec2005-09-06 04:43:02 +0000949SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000950 SDOperand N0 = N->getOperand(0);
951 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000952 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
953 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000954 MVT::ValueType VT = N0.getValueType();
955 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
956
957 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000958 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000959 return DAG.getConstant(N0C->getSignExtended() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000960 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000961 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000962 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000963 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000964 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000965 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000966 // fold (sra x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000967 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000968 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000969 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000970 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000971 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000972 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begeman646d7e22005-09-02 21:18:40 +0000973 if (N1C && MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1)), TLI))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000974 return DAG.getNode(ISD::SRL, VT, N0, N1);
975 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000976}
977
Nate Begeman83e75ec2005-09-06 04:43:02 +0000978SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000979 SDOperand N0 = N->getOperand(0);
980 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000981 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
982 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000983 MVT::ValueType VT = N0.getValueType();
984 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
985
986 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000987 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000988 return DAG.getConstant(N0C->getValue() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000989 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000990 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000991 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000992 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000993 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000994 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000995 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000996 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000997 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000998 // if (srl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +0000999 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
1000 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001001 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001002 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001003 N0.getOperand(1).getOpcode() == ISD::Constant) {
1004 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001005 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001006 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001007 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001008 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001009 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001010 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001011 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001012}
1013
Nate Begeman83e75ec2005-09-06 04:43:02 +00001014SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001015 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001016 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001017
1018 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001019 if (N0C)
1020 return DAG.getConstant(CountLeadingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001021 N0.getValueType());
1022 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001023}
1024
Nate Begeman83e75ec2005-09-06 04:43:02 +00001025SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001026 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001027 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001028
1029 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001030 if (N0C)
1031 return DAG.getConstant(CountTrailingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001032 N0.getValueType());
1033 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001034}
1035
Nate Begeman83e75ec2005-09-06 04:43:02 +00001036SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001037 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001038 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001039
1040 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001041 if (N0C)
1042 return DAG.getConstant(CountPopulation_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001043 N0.getValueType());
1044 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001045}
1046
Nate Begeman452d7be2005-09-16 00:54:12 +00001047SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1048 SDOperand N0 = N->getOperand(0);
1049 SDOperand N1 = N->getOperand(1);
1050 SDOperand N2 = N->getOperand(2);
1051 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1052 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1053 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1054 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001055
Nate Begeman452d7be2005-09-16 00:54:12 +00001056 // fold select C, X, X -> X
1057 if (N1 == N2)
1058 return N1;
1059 // fold select true, X, Y -> X
1060 if (N0C && !N0C->isNullValue())
1061 return N1;
1062 // fold select false, X, Y -> Y
1063 if (N0C && N0C->isNullValue())
1064 return N2;
1065 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001066 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001067 return DAG.getNode(ISD::OR, VT, N0, N2);
1068 // fold select C, 0, X -> ~C & X
1069 // FIXME: this should check for C type == X type, not i1?
1070 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1071 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1072 WorkList.push_back(XORNode.Val);
1073 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1074 }
1075 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001076 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001077 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1078 WorkList.push_back(XORNode.Val);
1079 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1080 }
1081 // fold select C, X, 0 -> C & X
1082 // FIXME: this should check for C type == X type, not i1?
1083 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1084 return DAG.getNode(ISD::AND, VT, N0, N1);
1085 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1086 if (MVT::i1 == VT && N0 == N1)
1087 return DAG.getNode(ISD::OR, VT, N0, N2);
1088 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1089 if (MVT::i1 == VT && N0 == N2)
1090 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman44728a72005-09-19 22:34:01 +00001091 // fold selects based on a setcc into other things, such as min/max/abs
1092 if (N0.getOpcode() == ISD::SETCC)
1093 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001094 return SDOperand();
1095}
1096
1097SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001098 SDOperand N0 = N->getOperand(0);
1099 SDOperand N1 = N->getOperand(1);
1100 SDOperand N2 = N->getOperand(2);
1101 SDOperand N3 = N->getOperand(3);
1102 SDOperand N4 = N->getOperand(4);
1103 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1104 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1105 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1106 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1107
1108 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001109 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001110 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1111
Nate Begeman44728a72005-09-19 22:34:01 +00001112 // fold select_cc lhs, rhs, x, x, cc -> x
1113 if (N2 == N3)
1114 return N2;
1115 // fold select_cc true, x, y -> x
Chris Lattner91559022005-10-05 04:45:43 +00001116 if (SCCC && SCCC->getValue())
Nate Begeman44728a72005-09-19 22:34:01 +00001117 return N2;
1118 // fold select_cc false, x, y -> y
Chris Lattner91559022005-10-05 04:45:43 +00001119 if (SCCC && SCCC->getValue() == 0)
Nate Begeman44728a72005-09-19 22:34:01 +00001120 return N3;
1121 // fold select_cc into other things, such as min/max/abs
1122 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001123}
1124
1125SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1126 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1127 cast<CondCodeSDNode>(N->getOperand(2))->get());
1128}
1129
Nate Begeman83e75ec2005-09-06 04:43:02 +00001130SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001131 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001132 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001133 MVT::ValueType VT = N->getValueType(0);
1134
Nate Begeman1d4d4142005-09-01 00:19:25 +00001135 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001136 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001137 return DAG.getConstant(N0C->getSignExtended(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001138 // fold (sext (sext x)) -> (sext x)
1139 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001140 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
1141 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001142}
1143
Nate Begeman83e75ec2005-09-06 04:43:02 +00001144SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001145 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001146 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001147 MVT::ValueType VT = N->getValueType(0);
1148
Nate Begeman1d4d4142005-09-01 00:19:25 +00001149 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001150 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001151 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001152 // fold (zext (zext x)) -> (zext x)
1153 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001154 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
1155 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001156}
1157
Nate Begeman83e75ec2005-09-06 04:43:02 +00001158SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001159 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001160 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001161 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001162 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001163 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001164
Nate Begeman1d4d4142005-09-01 00:19:25 +00001165 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001166 if (N0C) {
1167 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001168 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001169 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001170 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001171 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman646d7e22005-09-02 21:18:40 +00001172 cast<VTSDNode>(N0.getOperand(1))->getVT() < EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001173 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001174 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001175 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1176 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1177 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001178 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001179 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001180 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1181 if (N0.getOpcode() == ISD::AssertSext &&
1182 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001183 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001184 }
1185 // fold (sext_in_reg (sextload x)) -> (sextload x)
1186 if (N0.getOpcode() == ISD::SEXTLOAD &&
1187 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001188 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001189 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001190 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001191 // FIXME: teach isSetCCEquivalent about 0, -1 and then use it here
Nate Begeman1d4d4142005-09-01 00:19:25 +00001192 if (N0.getOpcode() == ISD::SETCC &&
1193 TLI.getSetCCResultContents() ==
1194 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001195 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001196 // FIXME: this code is currently just ported over from SelectionDAG.cpp
1197 // we probably actually want to handle this in two pieces. Rather than
1198 // checking all the top bits for zero, just check the sign bit here and turn
1199 // it into a zero extend inreg (AND with constant).
1200 // then, let the code for AND figure out if the mask is superfluous rather
1201 // than doing so here.
1202 if (N0.getOpcode() == ISD::AND &&
1203 N0.getOperand(1).getOpcode() == ISD::Constant) {
1204 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1205 unsigned NumBits = MVT::getSizeInBits(EVT);
1206 if ((Mask & (~0ULL << (NumBits-1))) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001207 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001208 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001209 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001210}
1211
Nate Begeman83e75ec2005-09-06 04:43:02 +00001212SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001213 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001214 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001215 MVT::ValueType VT = N->getValueType(0);
1216
1217 // noop truncate
1218 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001219 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001220 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001221 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001222 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001223 // fold (truncate (truncate x)) -> (truncate x)
1224 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001225 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001226 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1227 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1228 if (N0.getValueType() < VT)
1229 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001230 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001231 else if (N0.getValueType() > VT)
1232 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001233 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001234 else
1235 // if the source and dest are the same type, we can drop both the extend
1236 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001237 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001238 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001239 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001240}
1241
Chris Lattner01b3d732005-09-28 22:28:18 +00001242SDOperand DAGCombiner::visitFADD(SDNode *N) {
1243 SDOperand N0 = N->getOperand(0);
1244 SDOperand N1 = N->getOperand(1);
1245 MVT::ValueType VT = N->getValueType(0);
1246
1247 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1248 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1249 // fold floating point (fadd c1, c2)
1250 return DAG.getConstantFP(N0CFP->getValue() + N1CFP->getValue(),
1251 N->getValueType(0));
1252 }
1253 // fold (A + (-B)) -> A-B
1254 if (N1.getOpcode() == ISD::FNEG)
1255 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
1256
1257 // fold ((-A) + B) -> B-A
1258 if (N0.getOpcode() == ISD::FNEG)
1259 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
1260
1261 return SDOperand();
1262}
1263
1264SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1265 SDOperand N0 = N->getOperand(0);
1266 SDOperand N1 = N->getOperand(1);
1267 MVT::ValueType VT = N->getValueType(0);
1268
1269 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1270 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1271 // fold floating point (fsub c1, c2)
1272 return DAG.getConstantFP(N0CFP->getValue() - N1CFP->getValue(),
1273 N->getValueType(0));
1274 }
1275 // fold (A-(-B)) -> A+B
1276 if (N1.getOpcode() == ISD::FNEG)
1277 return DAG.getNode(ISD::FADD, N0.getValueType(), N0, N1.getOperand(0));
1278
1279 return SDOperand();
1280}
1281
1282SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1283 SDOperand N0 = N->getOperand(0);
1284 SDOperand N1 = N->getOperand(1);
1285 MVT::ValueType VT = N->getValueType(0);
1286
1287 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1288 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1289 // fold floating point (fmul c1, c2)
1290 return DAG.getConstantFP(N0CFP->getValue() * N1CFP->getValue(),
1291 N->getValueType(0));
1292 }
1293 return SDOperand();
1294}
1295
1296SDOperand DAGCombiner::visitFDIV(SDNode *N) {
1297 SDOperand N0 = N->getOperand(0);
1298 SDOperand N1 = N->getOperand(1);
1299 MVT::ValueType VT = N->getValueType(0);
1300
1301 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1302 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1303 // fold floating point (fdiv c1, c2)
1304 return DAG.getConstantFP(N0CFP->getValue() / N1CFP->getValue(),
1305 N->getValueType(0));
1306 }
1307 return SDOperand();
1308}
1309
1310SDOperand DAGCombiner::visitFREM(SDNode *N) {
1311 SDOperand N0 = N->getOperand(0);
1312 SDOperand N1 = N->getOperand(1);
1313 MVT::ValueType VT = N->getValueType(0);
1314
1315 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1316 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1317 // fold floating point (frem c1, c2) -> fmod(c1, c2)
1318 return DAG.getConstantFP(fmod(N0CFP->getValue(),N1CFP->getValue()),
1319 N->getValueType(0));
1320 }
1321 return SDOperand();
1322}
1323
1324
Nate Begeman83e75ec2005-09-06 04:43:02 +00001325SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001326 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001327 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001328
1329 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001330 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001331 return DAG.getConstantFP(N0C->getSignExtended(), N->getValueType(0));
1332 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001333}
1334
Nate Begeman83e75ec2005-09-06 04:43:02 +00001335SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001336 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001337 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001338
1339 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001340 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001341 return DAG.getConstantFP(N0C->getValue(), N->getValueType(0));
1342 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001343}
1344
Nate Begeman83e75ec2005-09-06 04:43:02 +00001345SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001346 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001347
1348 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001349 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001350 return DAG.getConstant((int64_t)N0CFP->getValue(), N->getValueType(0));
1351 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001352}
1353
Nate Begeman83e75ec2005-09-06 04:43:02 +00001354SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001355 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001356
1357 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001358 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001359 return DAG.getConstant((uint64_t)N0CFP->getValue(), N->getValueType(0));
1360 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001361}
1362
Nate Begeman83e75ec2005-09-06 04:43:02 +00001363SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001364 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001365
1366 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001367 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001368 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1369 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001370}
1371
Nate Begeman83e75ec2005-09-06 04:43:02 +00001372SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001373 SDOperand N0 = N->getOperand(0);
1374 MVT::ValueType VT = N->getValueType(0);
1375 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00001376 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001377
Nate Begeman1d4d4142005-09-01 00:19:25 +00001378 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001379 if (N0CFP) {
1380 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001381 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001382 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001383 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001384}
1385
Nate Begeman83e75ec2005-09-06 04:43:02 +00001386SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001387 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001388
1389 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001390 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001391 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1392 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001393}
1394
Nate Begeman83e75ec2005-09-06 04:43:02 +00001395SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001396 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001397 // fold (neg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001398 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001399 return DAG.getConstantFP(-N0CFP->getValue(), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001400 // fold (neg (sub x, y)) -> (sub y, x)
1401 if (N->getOperand(0).getOpcode() == ISD::SUB)
1402 return DAG.getNode(ISD::SUB, N->getValueType(0), N->getOperand(1),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001403 N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001404 // fold (neg (neg x)) -> x
1405 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001406 return N->getOperand(0).getOperand(0);
1407 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001408}
1409
Nate Begeman83e75ec2005-09-06 04:43:02 +00001410SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001411 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001412 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001413 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001414 return DAG.getConstantFP(fabs(N0CFP->getValue()), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001415 // fold (fabs (fabs x)) -> (fabs x)
1416 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001417 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001418 // fold (fabs (fneg x)) -> (fabs x)
1419 if (N->getOperand(0).getOpcode() == ISD::FNEG)
1420 return DAG.getNode(ISD::FABS, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001421 N->getOperand(0).getOperand(0));
1422 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001423}
1424
Nate Begeman44728a72005-09-19 22:34:01 +00001425SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
1426 SDOperand Chain = N->getOperand(0);
1427 SDOperand N1 = N->getOperand(1);
1428 SDOperand N2 = N->getOperand(2);
1429 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1430
1431 // never taken branch, fold to chain
1432 if (N1C && N1C->isNullValue())
1433 return Chain;
1434 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00001435 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00001436 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1437 return SDOperand();
1438}
1439
1440SDOperand DAGCombiner::visitBRCONDTWOWAY(SDNode *N) {
1441 SDOperand Chain = N->getOperand(0);
1442 SDOperand N1 = N->getOperand(1);
1443 SDOperand N2 = N->getOperand(2);
1444 SDOperand N3 = N->getOperand(3);
1445 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1446
1447 // unconditional branch to true mbb
1448 if (N1C && N1C->getValue() == 1)
1449 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1450 // unconditional branch to false mbb
1451 if (N1C && N1C->isNullValue())
1452 return DAG.getNode(ISD::BR, MVT::Other, Chain, N3);
1453 return SDOperand();
1454}
1455
Chris Lattner3ea0b472005-10-05 06:47:48 +00001456// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
1457//
Nate Begeman44728a72005-09-19 22:34:01 +00001458SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00001459 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
1460 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
1461
1462 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00001463 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
1464 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
1465
1466 // fold br_cc true, dest -> br dest (unconditional branch)
1467 if (SCCC && SCCC->getValue())
1468 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
1469 N->getOperand(4));
1470 // fold br_cc false, dest -> unconditional fall through
1471 if (SCCC && SCCC->isNullValue())
1472 return N->getOperand(0);
1473 // fold to a simpler setcc
1474 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
1475 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
1476 Simp.getOperand(2), Simp.getOperand(0),
1477 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00001478 return SDOperand();
1479}
1480
1481SDOperand DAGCombiner::visitBRTWOWAY_CC(SDNode *N) {
Nate Begemane17daeb2005-10-05 21:43:42 +00001482 SDOperand Chain = N->getOperand(0);
1483 SDOperand CCN = N->getOperand(1);
1484 SDOperand LHS = N->getOperand(2);
1485 SDOperand RHS = N->getOperand(3);
1486 SDOperand N4 = N->getOperand(4);
1487 SDOperand N5 = N->getOperand(5);
1488
1489 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), LHS, RHS,
1490 cast<CondCodeSDNode>(CCN)->get(), false);
1491 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1492
1493 // fold select_cc lhs, rhs, x, x, cc -> x
1494 if (N4 == N5)
1495 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
1496 // fold select_cc true, x, y -> x
1497 if (SCCC && SCCC->getValue())
1498 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
1499 // fold select_cc false, x, y -> y
1500 if (SCCC && SCCC->isNullValue())
1501 return DAG.getNode(ISD::BR, MVT::Other, Chain, N5);
1502 // fold to a simpler setcc
1503 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
1504 return DAG.getBR2Way_CC(Chain, SCC.getOperand(2), SCC.getOperand(0),
1505 SCC.getOperand(1), N4, N5);
Nate Begeman44728a72005-09-19 22:34:01 +00001506 return SDOperand();
1507}
1508
1509SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
1510 return SDOperand();
1511}
1512
1513SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
1514 SDOperand N2, SDOperand N3,
1515 ISD::CondCode CC) {
1516 return SDOperand();
1517}
1518
Nate Begeman452d7be2005-09-16 00:54:12 +00001519SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00001520 SDOperand N1, ISD::CondCode Cond,
1521 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001522 // These setcc operations always fold.
1523 switch (Cond) {
1524 default: break;
1525 case ISD::SETFALSE:
1526 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1527 case ISD::SETTRUE:
1528 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
1529 }
1530
1531 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1532 uint64_t C1 = N1C->getValue();
1533 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
1534 uint64_t C0 = N0C->getValue();
1535
1536 // Sign extend the operands if required
1537 if (ISD::isSignedIntSetCC(Cond)) {
1538 C0 = N0C->getSignExtended();
1539 C1 = N1C->getSignExtended();
1540 }
1541
1542 switch (Cond) {
1543 default: assert(0 && "Unknown integer setcc!");
1544 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
1545 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
1546 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
1547 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
1548 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
1549 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
1550 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
1551 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
1552 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
1553 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
1554 }
1555 } else {
1556 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
1557 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
1558 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
1559
1560 // If the comparison constant has bits in the upper part, the
1561 // zero-extended value could never match.
1562 if (C1 & (~0ULL << InSize)) {
1563 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
1564 switch (Cond) {
1565 case ISD::SETUGT:
1566 case ISD::SETUGE:
1567 case ISD::SETEQ: return DAG.getConstant(0, VT);
1568 case ISD::SETULT:
1569 case ISD::SETULE:
1570 case ISD::SETNE: return DAG.getConstant(1, VT);
1571 case ISD::SETGT:
1572 case ISD::SETGE:
1573 // True if the sign bit of C1 is set.
1574 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
1575 case ISD::SETLT:
1576 case ISD::SETLE:
1577 // True if the sign bit of C1 isn't set.
1578 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
1579 default:
1580 break;
1581 }
1582 }
1583
1584 // Otherwise, we can perform the comparison with the low bits.
1585 switch (Cond) {
1586 case ISD::SETEQ:
1587 case ISD::SETNE:
1588 case ISD::SETUGT:
1589 case ISD::SETUGE:
1590 case ISD::SETULT:
1591 case ISD::SETULE:
1592 return DAG.getSetCC(VT, N0.getOperand(0),
1593 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
1594 Cond);
1595 default:
1596 break; // todo, be more careful with signed comparisons
1597 }
1598 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1599 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1600 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
1601 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
1602 MVT::ValueType ExtDstTy = N0.getValueType();
1603 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
1604
1605 // If the extended part has any inconsistent bits, it cannot ever
1606 // compare equal. In other words, they have to be all ones or all
1607 // zeros.
1608 uint64_t ExtBits =
1609 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
1610 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
1611 return DAG.getConstant(Cond == ISD::SETNE, VT);
1612
1613 SDOperand ZextOp;
1614 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
1615 if (Op0Ty == ExtSrcTy) {
1616 ZextOp = N0.getOperand(0);
1617 } else {
1618 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
1619 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
1620 DAG.getConstant(Imm, Op0Ty));
1621 }
1622 WorkList.push_back(ZextOp.Val);
1623 // Otherwise, make this a use of a zext.
1624 return DAG.getSetCC(VT, ZextOp,
1625 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
1626 ExtDstTy),
1627 Cond);
1628 }
Chris Lattner5c46f742005-10-05 06:11:08 +00001629
Nate Begeman452d7be2005-09-16 00:54:12 +00001630 uint64_t MinVal, MaxVal;
1631 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
1632 if (ISD::isSignedIntSetCC(Cond)) {
1633 MinVal = 1ULL << (OperandBitSize-1);
1634 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
1635 MaxVal = ~0ULL >> (65-OperandBitSize);
1636 else
1637 MaxVal = 0;
1638 } else {
1639 MinVal = 0;
1640 MaxVal = ~0ULL >> (64-OperandBitSize);
1641 }
1642
1643 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
1644 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
1645 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
1646 --C1; // X >= C0 --> X > (C0-1)
1647 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
1648 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
1649 }
1650
1651 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
1652 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
1653 ++C1; // X <= C0 --> X < (C0+1)
1654 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
1655 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
1656 }
1657
1658 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
1659 return DAG.getConstant(0, VT); // X < MIN --> false
1660
1661 // Canonicalize setgt X, Min --> setne X, Min
1662 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
1663 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
1664
1665 // If we have setult X, 1, turn it into seteq X, 0
1666 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
1667 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
1668 ISD::SETEQ);
1669 // If we have setugt X, Max-1, turn it into seteq X, Max
1670 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
1671 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
1672 ISD::SETEQ);
1673
1674 // If we have "setcc X, C0", check to see if we can shrink the immediate
1675 // by changing cc.
1676
1677 // SETUGT X, SINTMAX -> SETLT X, 0
1678 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
1679 C1 == (~0ULL >> (65-OperandBitSize)))
1680 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
1681 ISD::SETLT);
1682
1683 // FIXME: Implement the rest of these.
1684
1685 // Fold bit comparisons when we can.
1686 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1687 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
1688 if (ConstantSDNode *AndRHS =
1689 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1690 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1691 // Perform the xform if the AND RHS is a single bit.
1692 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
1693 return DAG.getNode(ISD::SRL, VT, N0,
1694 DAG.getConstant(Log2_64(AndRHS->getValue()),
1695 TLI.getShiftAmountTy()));
1696 }
1697 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
1698 // (X & 8) == 8 --> (X & 8) >> 3
1699 // Perform the xform if C1 is a single bit.
1700 if ((C1 & (C1-1)) == 0) {
1701 return DAG.getNode(ISD::SRL, VT, N0,
1702 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
1703 }
1704 }
1705 }
1706 }
1707 } else if (isa<ConstantSDNode>(N0.Val)) {
1708 // Ensure that the constant occurs on the RHS.
1709 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1710 }
1711
1712 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
1713 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
1714 double C0 = N0C->getValue(), C1 = N1C->getValue();
1715
1716 switch (Cond) {
1717 default: break; // FIXME: Implement the rest of these!
1718 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
1719 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
1720 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
1721 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
1722 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
1723 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
1724 }
1725 } else {
1726 // Ensure that the constant occurs on the RHS.
1727 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1728 }
1729
1730 if (N0 == N1) {
1731 // We can always fold X == Y for integer setcc's.
1732 if (MVT::isInteger(N0.getValueType()))
1733 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1734 unsigned UOF = ISD::getUnorderedFlavor(Cond);
1735 if (UOF == 2) // FP operators that are undefined on NaNs.
1736 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1737 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
1738 return DAG.getConstant(UOF, VT);
1739 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
1740 // if it is not already.
1741 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
1742 if (NewCond != Cond)
1743 return DAG.getSetCC(VT, N0, N1, NewCond);
1744 }
1745
1746 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1747 MVT::isInteger(N0.getValueType())) {
1748 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
1749 N0.getOpcode() == ISD::XOR) {
1750 // Simplify (X+Y) == (X+Z) --> Y == Z
1751 if (N0.getOpcode() == N1.getOpcode()) {
1752 if (N0.getOperand(0) == N1.getOperand(0))
1753 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
1754 if (N0.getOperand(1) == N1.getOperand(1))
1755 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
1756 if (isCommutativeBinOp(N0.getOpcode())) {
1757 // If X op Y == Y op X, try other combinations.
1758 if (N0.getOperand(0) == N1.getOperand(1))
1759 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
1760 if (N0.getOperand(1) == N1.getOperand(0))
1761 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
1762 }
1763 }
1764
Chris Lattner5c46f742005-10-05 06:11:08 +00001765 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0. Common for condcodes.
1766 if (N0.getOpcode() == ISD::XOR)
1767 if (ConstantSDNode *XORC = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
1768 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
1769 // If we know that all of the inverted bits are zero, don't bother
1770 // performing the inversion.
1771 if (MaskedValueIsZero(N0.getOperand(0), ~XORC->getValue(), TLI))
1772 return DAG.getSetCC(VT, N0.getOperand(0),
1773 DAG.getConstant(XORC->getValue()^RHSC->getValue(),
1774 N0.getValueType()), Cond);
1775 }
1776
Nate Begeman452d7be2005-09-16 00:54:12 +00001777 // Simplify (X+Z) == X --> Z == 0
1778 if (N0.getOperand(0) == N1)
1779 return DAG.getSetCC(VT, N0.getOperand(1),
1780 DAG.getConstant(0, N0.getValueType()), Cond);
1781 if (N0.getOperand(1) == N1) {
1782 if (isCommutativeBinOp(N0.getOpcode()))
1783 return DAG.getSetCC(VT, N0.getOperand(0),
1784 DAG.getConstant(0, N0.getValueType()), Cond);
1785 else {
1786 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
1787 // (Z-X) == X --> Z == X<<1
1788 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
1789 N1,
1790 DAG.getConstant(1,TLI.getShiftAmountTy()));
1791 WorkList.push_back(SH.Val);
1792 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
1793 }
1794 }
1795 }
1796
1797 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
1798 N1.getOpcode() == ISD::XOR) {
1799 // Simplify X == (X+Z) --> Z == 0
1800 if (N1.getOperand(0) == N0) {
1801 return DAG.getSetCC(VT, N1.getOperand(1),
1802 DAG.getConstant(0, N1.getValueType()), Cond);
1803 } else if (N1.getOperand(1) == N0) {
1804 if (isCommutativeBinOp(N1.getOpcode())) {
1805 return DAG.getSetCC(VT, N1.getOperand(0),
1806 DAG.getConstant(0, N1.getValueType()), Cond);
1807 } else {
1808 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
1809 // X == (Z-X) --> X<<1 == Z
1810 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
1811 DAG.getConstant(1,TLI.getShiftAmountTy()));
1812 WorkList.push_back(SH.Val);
1813 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
1814 }
1815 }
1816 }
1817 }
1818
1819 // Fold away ALL boolean setcc's.
1820 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00001821 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001822 switch (Cond) {
1823 default: assert(0 && "Unknown integer setcc!");
1824 case ISD::SETEQ: // X == Y -> (X^Y)^1
1825 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1826 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
1827 WorkList.push_back(Temp.Val);
1828 break;
1829 case ISD::SETNE: // X != Y --> (X^Y)
1830 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1831 break;
1832 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
1833 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
1834 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
1835 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
1836 WorkList.push_back(Temp.Val);
1837 break;
1838 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
1839 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
1840 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
1841 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
1842 WorkList.push_back(Temp.Val);
1843 break;
1844 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
1845 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
1846 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
1847 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
1848 WorkList.push_back(Temp.Val);
1849 break;
1850 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
1851 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
1852 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
1853 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
1854 break;
1855 }
1856 if (VT != MVT::i1) {
1857 WorkList.push_back(N0.Val);
1858 // FIXME: If running after legalize, we probably can't do this.
1859 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
1860 }
1861 return N0;
1862 }
1863
1864 // Could not fold it.
1865 return SDOperand();
1866}
1867
Nate Begeman1d4d4142005-09-01 00:19:25 +00001868// SelectionDAG::Combine - This is the entry point for the file.
1869//
Nate Begeman4ebd8052005-09-01 23:24:04 +00001870void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001871 /// run - This is the main entry point to this class.
1872 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00001873 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001874}