blob: 3a8c46815c8d99cf60834047411dde5fb5acf622 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begeman1d4d4142005-09-01 00:19:25 +00007//
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.
Nate Begeman1d4d4142005-09-01 00:19:25 +000012//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "dagcombine"
Nate Begeman1d4d4142005-09-01 00:19:25 +000016#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner00161a62008-01-25 07:20:16 +000017#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000019#include "llvm/Analysis/AliasAnalysis.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000020#include "llvm/Target/TargetData.h"
Chris Lattner1329cb82008-01-26 19:45:50 +000021#include "llvm/Target/TargetFrameInfo.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000022#include "llvm/Target/TargetLowering.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000024#include "llvm/Target/TargetOptions.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000025#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/Statistic.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000028#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000031#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000032using namespace llvm;
33
Chris Lattnercd3245a2006-12-19 22:41:21 +000034STATISTIC(NodesCombined , "Number of dag nodes combined");
35STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
36STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
37
Nate Begeman1d4d4142005-09-01 00:19:25 +000038namespace {
Chris Lattner938ab022007-01-16 04:55:25 +000039#ifndef NDEBUG
40 static cl::opt<bool>
41 ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden,
42 cl::desc("Pop up a window to show dags before the first "
43 "dag combine pass"));
44 static cl::opt<bool>
45 ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden,
46 cl::desc("Pop up a window to show dags before the second "
47 "dag combine pass"));
48#else
49 static const bool ViewDAGCombine1 = false;
50 static const bool ViewDAGCombine2 = false;
51#endif
52
Jim Laskey71382342006-10-07 23:37:56 +000053 static cl::opt<bool>
54 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000055 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000056
Jim Laskey07a27092006-10-18 19:08:31 +000057 static cl::opt<bool>
58 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
59 cl::desc("Include global information in alias analysis"));
60
Jim Laskeybc588b82006-10-05 15:07:25 +000061//------------------------------ DAGCombiner ---------------------------------//
62
Jim Laskey71382342006-10-07 23:37:56 +000063 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000064 SelectionDAG &DAG;
65 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000066 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000067
68 // Worklist of all of the nodes that need to be simplified.
69 std::vector<SDNode*> WorkList;
70
Jim Laskeyc7c3f112006-10-16 20:52:31 +000071 // AA - Used for DAG load/store alias analysis.
72 AliasAnalysis &AA;
73
Nate Begeman1d4d4142005-09-01 00:19:25 +000074 /// AddUsersToWorkList - When an instruction is simplified, add all users of
75 /// the instruction to the work lists because they might get more simplified
76 /// now.
77 ///
78 void AddUsersToWorkList(SDNode *N) {
79 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000080 UI != UE; ++UI)
Jim Laskey6ff23e52006-10-04 16:53:27 +000081 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000082 }
83
Dan Gohman389079b2007-10-08 17:57:15 +000084 /// visit - call the node-specific routine that knows how to fold each
85 /// particular type of node.
86 SDOperand visit(SDNode *N);
87
Chris Lattner24664722006-03-01 04:53:38 +000088 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000089 /// AddToWorkList - Add to the work list making sure it's instance is at the
90 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000091 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000092 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000093 WorkList.push_back(N);
94 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000095
Chris Lattnerf8dc0612008-02-03 06:49:24 +000096 /// removeFromWorkList - remove all instances of N from the worklist.
97 ///
98 void removeFromWorkList(SDNode *N) {
99 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
100 WorkList.end());
Chris Lattner01a22022005-10-10 22:04:48 +0000101 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000102
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000103 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
104 bool AddTo = true);
105
Jim Laskey274062c2006-10-13 23:32:28 +0000106 SDOperand CombineTo(SDNode *N, SDOperand Res, bool AddTo = true) {
107 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000108 }
109
Jim Laskey274062c2006-10-13 23:32:28 +0000110 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1,
111 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000112 SDOperand To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000113 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000114 }
Chris Lattner0bd48932008-01-17 07:00:52 +0000115
Chris Lattner24664722006-03-01 04:53:38 +0000116 private:
117
Chris Lattner012f2412006-02-17 21:58:01 +0000118 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000119 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000120 /// propagation. If so, return true.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000121 bool SimplifyDemandedBits(SDOperand Op) {
122 APInt Demanded = APInt::getAllOnesValue(Op.getValueSizeInBits());
123 return SimplifyDemandedBits(Op, Demanded);
124 }
125
126 bool SimplifyDemandedBits(SDOperand Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000127
Chris Lattner448f2192006-11-11 00:39:41 +0000128 bool CombineToPreIndexedLoadStore(SDNode *N);
129 bool CombineToPostIndexedLoadStore(SDNode *N);
130
131
Dan Gohman389079b2007-10-08 17:57:15 +0000132 /// combine - call the node-specific routine that knows how to fold each
133 /// particular type of node. If that doesn't do anything, try the
134 /// target-specific DAG combines.
135 SDOperand combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000136
137 // Visitation implementation - Implement dag node combining for different
138 // node types. The semantics are as follows:
139 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000140 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000141 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000142 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000143 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000144 SDOperand visitTokenFactor(SDNode *N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000145 SDOperand visitMERGE_VALUES(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000146 SDOperand visitADD(SDNode *N);
147 SDOperand visitSUB(SDNode *N);
Chris Lattner91153682007-03-04 20:03:15 +0000148 SDOperand visitADDC(SDNode *N);
149 SDOperand visitADDE(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000150 SDOperand visitMUL(SDNode *N);
151 SDOperand visitSDIV(SDNode *N);
152 SDOperand visitUDIV(SDNode *N);
153 SDOperand visitSREM(SDNode *N);
154 SDOperand visitUREM(SDNode *N);
155 SDOperand visitMULHU(SDNode *N);
156 SDOperand visitMULHS(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +0000157 SDOperand visitSMUL_LOHI(SDNode *N);
158 SDOperand visitUMUL_LOHI(SDNode *N);
159 SDOperand visitSDIVREM(SDNode *N);
160 SDOperand visitUDIVREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000161 SDOperand visitAND(SDNode *N);
162 SDOperand visitOR(SDNode *N);
163 SDOperand visitXOR(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000164 SDOperand SimplifyVBinOp(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000165 SDOperand visitSHL(SDNode *N);
166 SDOperand visitSRA(SDNode *N);
167 SDOperand visitSRL(SDNode *N);
168 SDOperand visitCTLZ(SDNode *N);
169 SDOperand visitCTTZ(SDNode *N);
170 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000171 SDOperand visitSELECT(SDNode *N);
172 SDOperand visitSELECT_CC(SDNode *N);
173 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000174 SDOperand visitSIGN_EXTEND(SDNode *N);
175 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000176 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000177 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
178 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000179 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000180 SDOperand visitFADD(SDNode *N);
181 SDOperand visitFSUB(SDNode *N);
182 SDOperand visitFMUL(SDNode *N);
183 SDOperand visitFDIV(SDNode *N);
184 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000185 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000186 SDOperand visitSINT_TO_FP(SDNode *N);
187 SDOperand visitUINT_TO_FP(SDNode *N);
188 SDOperand visitFP_TO_SINT(SDNode *N);
189 SDOperand visitFP_TO_UINT(SDNode *N);
190 SDOperand visitFP_ROUND(SDNode *N);
191 SDOperand visitFP_ROUND_INREG(SDNode *N);
192 SDOperand visitFP_EXTEND(SDNode *N);
193 SDOperand visitFNEG(SDNode *N);
194 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000195 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000196 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000197 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000198 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000199 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
Evan Cheng513da432007-10-06 08:19:55 +0000200 SDOperand visitEXTRACT_VECTOR_ELT(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000201 SDOperand visitBUILD_VECTOR(SDNode *N);
202 SDOperand visitCONCAT_VECTORS(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000203 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000204
Evan Cheng44f1f092006-04-20 08:56:16 +0000205 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000206 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
207
Chris Lattnere70da202007-12-06 07:33:36 +0000208 SDOperand visitShiftByConstant(SDNode *N, unsigned Amt);
209
Chris Lattner40c62d52005-10-18 06:04:22 +0000210 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000211 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000212 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
213 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000214 SDOperand N3, ISD::CondCode CC,
215 bool NotExtCompare = false);
Nate Begeman452d7be2005-09-16 00:54:12 +0000216 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000217 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner5eee4272008-01-26 01:09:19 +0000218 SDOperand SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
219 unsigned HiOp);
Dan Gohman7f321562007-06-25 16:23:39 +0000220 SDOperand ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000221 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000222 SDOperand BuildUDIV(SDNode *N);
223 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Evan Chengc88138f2007-03-22 01:54:19 +0000224 SDOperand ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000225
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000226 SDOperand GetDemandedBits(SDOperand V, const APInt &Mask);
Chris Lattner2b4c2792007-10-13 06:35:54 +0000227
Jim Laskey6ff23e52006-10-04 16:53:27 +0000228 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
229 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000230 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000231 SmallVector<SDOperand, 8> &Aliases);
232
Jim Laskey096c22e2006-10-18 12:29:57 +0000233 /// isAlias - Return true if there is any possibility that the two addresses
234 /// overlap.
235 bool isAlias(SDOperand Ptr1, int64_t Size1,
236 const Value *SrcValue1, int SrcValueOffset1,
237 SDOperand Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000238 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000239
Jim Laskey7ca56af2006-10-11 13:47:09 +0000240 /// FindAliasInfo - Extracts the relevant alias information from the memory
241 /// node. Returns true if the operand was a load.
242 bool FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +0000243 SDOperand &Ptr, int64_t &Size,
244 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000245
Jim Laskey279f0532006-09-25 16:29:54 +0000246 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000247 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000248 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
249
Nate Begeman1d4d4142005-09-01 00:19:25 +0000250public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000251 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
252 : DAG(D),
253 TLI(D.getTargetLoweringInfo()),
254 AfterLegalize(false),
255 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000256
257 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000258 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000259 };
260}
261
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000262
263namespace {
264/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
265/// nodes from the worklist.
266class VISIBILITY_HIDDEN WorkListRemover :
267 public SelectionDAG::DAGUpdateListener {
268 DAGCombiner &DC;
269public:
Dan Gohmanb5660dc2008-02-20 16:44:09 +0000270 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000271
272 virtual void NodeDeleted(SDNode *N) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000273 DC.removeFromWorkList(N);
274 }
275
276 virtual void NodeUpdated(SDNode *N) {
277 // Ignore updates.
278 }
279};
280}
281
Chris Lattner24664722006-03-01 04:53:38 +0000282//===----------------------------------------------------------------------===//
283// TargetLowering::DAGCombinerInfo implementation
284//===----------------------------------------------------------------------===//
285
286void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
287 ((DAGCombiner*)DC)->AddToWorkList(N);
288}
289
290SDOperand TargetLowering::DAGCombinerInfo::
291CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000292 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000293}
294
295SDOperand TargetLowering::DAGCombinerInfo::
296CombineTo(SDNode *N, SDOperand Res) {
297 return ((DAGCombiner*)DC)->CombineTo(N, Res);
298}
299
300
301SDOperand TargetLowering::DAGCombinerInfo::
302CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
303 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
304}
305
306
Chris Lattner24664722006-03-01 04:53:38 +0000307//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000308// Helper Functions
309//===----------------------------------------------------------------------===//
310
311/// isNegatibleForFree - Return 1 if we can compute the negated form of the
312/// specified expression for the same cost as the expression itself, or 2 if we
313/// can compute the negated form more cheaply than the expression itself.
Chris Lattner0254e702008-02-26 07:04:54 +0000314static char isNegatibleForFree(SDOperand Op, bool AfterLegalize,
315 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000316 // No compile time optimizations on this type.
317 if (Op.getValueType() == MVT::ppcf128)
318 return 0;
319
Chris Lattner29446522007-05-14 22:04:50 +0000320 // fneg is removable even if it has multiple uses.
321 if (Op.getOpcode() == ISD::FNEG) return 2;
322
323 // Don't allow anything with multiple uses.
324 if (!Op.hasOneUse()) return 0;
325
Chris Lattner3adf9512007-05-25 02:19:06 +0000326 // Don't recurse exponentially.
327 if (Depth > 6) return 0;
328
Chris Lattner29446522007-05-14 22:04:50 +0000329 switch (Op.getOpcode()) {
330 default: return false;
331 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000332 // Don't invert constant FP values after legalize. The negated constant
333 // isn't necessarily legal.
334 return AfterLegalize ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000335 case ISD::FADD:
336 // FIXME: determine better conditions for this xform.
337 if (!UnsafeFPMath) return 0;
338
339 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000340 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000341 return V;
342 // -(A+B) -> -B - A
Chris Lattner0254e702008-02-26 07:04:54 +0000343 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000344 case ISD::FSUB:
345 // We can't turn -(A-B) into B-A when we honor signed zeros.
346 if (!UnsafeFPMath) return 0;
347
348 // -(A-B) -> B-A
349 return 1;
350
351 case ISD::FMUL:
352 case ISD::FDIV:
353 if (HonorSignDependentRoundingFPMath()) return 0;
354
355 // -(X*Y) -> (-X * Y) or (X*-Y)
Chris Lattner0254e702008-02-26 07:04:54 +0000356 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000357 return V;
358
Chris Lattner0254e702008-02-26 07:04:54 +0000359 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000360
361 case ISD::FP_EXTEND:
362 case ISD::FP_ROUND:
363 case ISD::FSIN:
Chris Lattner0254e702008-02-26 07:04:54 +0000364 return isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000365 }
366}
367
368/// GetNegatedExpression - If isNegatibleForFree returns true, this function
369/// returns the newly negated expression.
Chris Lattner3adf9512007-05-25 02:19:06 +0000370static SDOperand GetNegatedExpression(SDOperand Op, SelectionDAG &DAG,
Chris Lattner0254e702008-02-26 07:04:54 +0000371 bool AfterLegalize, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000372 // fneg is removable even if it has multiple uses.
373 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
374
375 // Don't allow anything with multiple uses.
376 assert(Op.hasOneUse() && "Unknown reuse!");
377
Chris Lattner3adf9512007-05-25 02:19:06 +0000378 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000379 switch (Op.getOpcode()) {
380 default: assert(0 && "Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000381 case ISD::ConstantFP: {
382 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
383 V.changeSign();
384 return DAG.getConstantFP(V, Op.getValueType());
385 }
Chris Lattner29446522007-05-14 22:04:50 +0000386 case ISD::FADD:
387 // FIXME: determine better conditions for this xform.
388 assert(UnsafeFPMath);
389
390 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000391 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000392 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000393 GetNegatedExpression(Op.getOperand(0), DAG,
394 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000395 Op.getOperand(1));
396 // -(A+B) -> -B - A
397 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000398 GetNegatedExpression(Op.getOperand(1), DAG,
399 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000400 Op.getOperand(0));
401 case ISD::FSUB:
402 // We can't turn -(A-B) into B-A when we honor signed zeros.
403 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000404
405 // -(0-B) -> B
406 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000407 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000408 return Op.getOperand(1);
Chris Lattner29446522007-05-14 22:04:50 +0000409
410 // -(A-B) -> B-A
411 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
412 Op.getOperand(0));
413
414 case ISD::FMUL:
415 case ISD::FDIV:
416 assert(!HonorSignDependentRoundingFPMath());
417
418 // -(X*Y) -> -X * Y
Chris Lattneraeecb6c2008-02-26 17:09:59 +0000419 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000420 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000421 GetNegatedExpression(Op.getOperand(0), DAG,
422 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000423 Op.getOperand(1));
424
425 // -(X*Y) -> X * -Y
426 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
427 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000428 GetNegatedExpression(Op.getOperand(1), DAG,
429 AfterLegalize, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000430
431 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000432 case ISD::FSIN:
433 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000434 GetNegatedExpression(Op.getOperand(0), DAG,
435 AfterLegalize, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000436 case ISD::FP_ROUND:
437 return DAG.getNode(ISD::FP_ROUND, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000438 GetNegatedExpression(Op.getOperand(0), DAG,
439 AfterLegalize, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000440 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000441 }
442}
Chris Lattner24664722006-03-01 04:53:38 +0000443
444
Nate Begeman4ebd8052005-09-01 23:24:04 +0000445// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
446// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000447// Also, set the incoming LHS, RHS, and CC references to the appropriate
448// nodes based on the type of node we are checking. This simplifies life a
449// bit for the callers.
450static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
451 SDOperand &CC) {
452 if (N.getOpcode() == ISD::SETCC) {
453 LHS = N.getOperand(0);
454 RHS = N.getOperand(1);
455 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000456 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000457 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000458 if (N.getOpcode() == ISD::SELECT_CC &&
459 N.getOperand(2).getOpcode() == ISD::Constant &&
460 N.getOperand(3).getOpcode() == ISD::Constant &&
461 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000462 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
463 LHS = N.getOperand(0);
464 RHS = N.getOperand(1);
465 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000466 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000467 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000468 return false;
469}
470
Nate Begeman99801192005-09-07 23:25:52 +0000471// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
472// one use. If this is true, it allows the users to invert the operation for
473// free when it is profitable to do so.
474static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000475 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000476 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000477 return true;
478 return false;
479}
480
Nate Begemancd4d58c2006-02-03 06:46:56 +0000481SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
482 MVT::ValueType VT = N0.getValueType();
483 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
484 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
485 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
486 if (isa<ConstantSDNode>(N1)) {
487 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000488 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000489 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
490 } else if (N0.hasOneUse()) {
491 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000492 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000493 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
494 }
495 }
496 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
497 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
498 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
499 if (isa<ConstantSDNode>(N0)) {
500 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000501 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000502 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
503 } else if (N1.hasOneUse()) {
504 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000505 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000506 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
507 }
508 }
509 return SDOperand();
510}
511
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000512SDOperand DAGCombiner::CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
513 bool AddTo) {
514 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
515 ++NodesCombined;
516 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
517 DOUT << "\nWith: "; DEBUG(To[0].Val->dump(&DAG));
518 DOUT << " and " << NumTo-1 << " other values\n";
519 WorkListRemover DeadNodes(*this);
520 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
521
522 if (AddTo) {
523 // Push the new nodes and any users onto the worklist
524 for (unsigned i = 0, e = NumTo; i != e; ++i) {
525 AddToWorkList(To[i].Val);
526 AddUsersToWorkList(To[i].Val);
527 }
528 }
529
530 // Nodes can be reintroduced into the worklist. Make sure we do not
531 // process a node that has been replaced.
532 removeFromWorkList(N);
533
534 // Finally, since the node is now dead, remove it from the graph.
535 DAG.DeleteNode(N);
536 return SDOperand(N, 0);
537}
538
539/// SimplifyDemandedBits - Check the specified integer node value to see if
540/// it can be simplified or if things it uses can be simplified by bit
541/// propagation. If so, return true.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000542bool DAGCombiner::SimplifyDemandedBits(SDOperand Op, const APInt &Demanded) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000543 TargetLowering::TargetLoweringOpt TLO(DAG, AfterLegalize);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000544 APInt KnownZero, KnownOne;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000545 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
546 return false;
547
548 // Revisit the node.
549 AddToWorkList(Op.Val);
550
551 // Replace the old value with the new one.
552 ++NodesCombined;
553 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.Val->dump(&DAG));
554 DOUT << "\nWith: "; DEBUG(TLO.New.Val->dump(&DAG));
555 DOUT << '\n';
556
557 // Replace all uses. If any nodes become isomorphic to other nodes and
558 // are deleted, make sure to remove them from our worklist.
559 WorkListRemover DeadNodes(*this);
560 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
561
562 // Push the new node and any (possibly new) users onto the worklist.
563 AddToWorkList(TLO.New.Val);
564 AddUsersToWorkList(TLO.New.Val);
565
566 // Finally, if the node is now dead, remove it from the graph. The node
567 // may not be dead if the replacement process recursively simplified to
568 // something else needing this node.
569 if (TLO.Old.Val->use_empty()) {
570 removeFromWorkList(TLO.Old.Val);
571
572 // If the operands of this node are only used by the node, they will now
573 // be dead. Make sure to visit them first to delete dead nodes early.
574 for (unsigned i = 0, e = TLO.Old.Val->getNumOperands(); i != e; ++i)
575 if (TLO.Old.Val->getOperand(i).Val->hasOneUse())
576 AddToWorkList(TLO.Old.Val->getOperand(i).Val);
577
578 DAG.DeleteNode(TLO.Old.Val);
579 }
580 return true;
581}
582
Chris Lattner29446522007-05-14 22:04:50 +0000583//===----------------------------------------------------------------------===//
584// Main DAG Combiner implementation
585//===----------------------------------------------------------------------===//
586
Nate Begeman4ebd8052005-09-01 23:24:04 +0000587void DAGCombiner::Run(bool RunningAfterLegalize) {
588 // set the instance variable, so that the various visit routines may use it.
589 AfterLegalize = RunningAfterLegalize;
590
Nate Begeman646d7e22005-09-02 21:18:40 +0000591 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000592 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
593 E = DAG.allnodes_end(); I != E; ++I)
594 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000595
Chris Lattner95038592005-10-05 06:35:28 +0000596 // Create a dummy node (which is not added to allnodes), that adds a reference
597 // to the root node, preventing it from being deleted, and tracking any
598 // changes of the root.
599 HandleSDNode Dummy(DAG.getRoot());
600
Jim Laskey26f7fa72006-10-17 19:33:52 +0000601 // The root of the dag may dangle to deleted nodes until the dag combiner is
602 // done. Set it to null to avoid confusion.
603 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000604
Nate Begeman1d4d4142005-09-01 00:19:25 +0000605 // while the worklist isn't empty, inspect the node on the end of it and
606 // try and combine it.
607 while (!WorkList.empty()) {
608 SDNode *N = WorkList.back();
609 WorkList.pop_back();
610
611 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000612 // N is deleted from the DAG, since they too may now be dead or may have a
613 // reduced number of uses, allowing other xforms.
614 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000615 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000616 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000617
Chris Lattner95038592005-10-05 06:35:28 +0000618 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000619 continue;
620 }
621
Dan Gohman389079b2007-10-08 17:57:15 +0000622 SDOperand RV = combine(N);
Chris Lattner24664722006-03-01 04:53:38 +0000623
Chris Lattner50d8e492008-01-25 23:34:24 +0000624 if (RV.Val == 0)
625 continue;
626
627 ++NodesCombined;
Chris Lattner5eee4272008-01-26 01:09:19 +0000628
Chris Lattner50d8e492008-01-25 23:34:24 +0000629 // If we get back the same node we passed in, rather than a new node or
630 // zero, we know that the node must have defined multiple values and
631 // CombineTo was used. Since CombineTo takes care of the worklist
632 // mechanics for us, we have no work to do in this case.
633 if (RV.Val == N)
634 continue;
635
636 assert(N->getOpcode() != ISD::DELETED_NODE &&
637 RV.Val->getOpcode() != ISD::DELETED_NODE &&
638 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000639
Chris Lattner50d8e492008-01-25 23:34:24 +0000640 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
641 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
642 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000643 WorkListRemover DeadNodes(*this);
Chris Lattner50d8e492008-01-25 23:34:24 +0000644 if (N->getNumValues() == RV.Val->getNumValues())
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000645 DAG.ReplaceAllUsesWith(N, RV.Val, &DeadNodes);
Chris Lattner50d8e492008-01-25 23:34:24 +0000646 else {
Chris Lattner5eee4272008-01-26 01:09:19 +0000647 assert(N->getValueType(0) == RV.getValueType() &&
648 N->getNumValues() == 1 && "Type mismatch");
Chris Lattner50d8e492008-01-25 23:34:24 +0000649 SDOperand OpV = RV;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000650 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000651 }
Chris Lattner50d8e492008-01-25 23:34:24 +0000652
653 // Push the new node and any users onto the worklist
654 AddToWorkList(RV.Val);
655 AddUsersToWorkList(RV.Val);
656
657 // Add any uses of the old node to the worklist in case this node is the
658 // last one that uses them. They may become dead after this node is
659 // deleted.
660 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
661 AddToWorkList(N->getOperand(i).Val);
662
663 // Nodes can be reintroduced into the worklist. Make sure we do not
664 // process a node that has been replaced.
665 removeFromWorkList(N);
Chris Lattner50d8e492008-01-25 23:34:24 +0000666
667 // Finally, since the node is now dead, remove it from the graph.
668 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000669 }
Chris Lattner95038592005-10-05 06:35:28 +0000670
671 // If the root changed (e.g. it was a dead load, update the root).
672 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000673}
674
Nate Begeman83e75ec2005-09-06 04:43:02 +0000675SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000676 switch(N->getOpcode()) {
677 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000678 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000679 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000680 case ISD::ADD: return visitADD(N);
681 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000682 case ISD::ADDC: return visitADDC(N);
683 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000684 case ISD::MUL: return visitMUL(N);
685 case ISD::SDIV: return visitSDIV(N);
686 case ISD::UDIV: return visitUDIV(N);
687 case ISD::SREM: return visitSREM(N);
688 case ISD::UREM: return visitUREM(N);
689 case ISD::MULHU: return visitMULHU(N);
690 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000691 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
692 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
693 case ISD::SDIVREM: return visitSDIVREM(N);
694 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000695 case ISD::AND: return visitAND(N);
696 case ISD::OR: return visitOR(N);
697 case ISD::XOR: return visitXOR(N);
698 case ISD::SHL: return visitSHL(N);
699 case ISD::SRA: return visitSRA(N);
700 case ISD::SRL: return visitSRL(N);
701 case ISD::CTLZ: return visitCTLZ(N);
702 case ISD::CTTZ: return visitCTTZ(N);
703 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000704 case ISD::SELECT: return visitSELECT(N);
705 case ISD::SELECT_CC: return visitSELECT_CC(N);
706 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000707 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
708 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000709 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000710 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
711 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000712 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000713 case ISD::FADD: return visitFADD(N);
714 case ISD::FSUB: return visitFSUB(N);
715 case ISD::FMUL: return visitFMUL(N);
716 case ISD::FDIV: return visitFDIV(N);
717 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000718 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000719 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
720 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
721 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
722 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
723 case ISD::FP_ROUND: return visitFP_ROUND(N);
724 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
725 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
726 case ISD::FNEG: return visitFNEG(N);
727 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000728 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000729 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000730 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000731 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000732 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000733 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000734 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
735 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000736 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000737 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000738 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000739}
740
Dan Gohman389079b2007-10-08 17:57:15 +0000741SDOperand DAGCombiner::combine(SDNode *N) {
742
743 SDOperand RV = visit(N);
744
745 // If nothing happened, try a target-specific DAG combine.
746 if (RV.Val == 0) {
747 assert(N->getOpcode() != ISD::DELETED_NODE &&
748 "Node was deleted but visit returned NULL!");
749
750 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
751 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
752
753 // Expose the DAG combiner to the target combiner impls.
754 TargetLowering::DAGCombinerInfo
755 DagCombineInfo(DAG, !AfterLegalize, false, this);
756
757 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
758 }
759 }
760
761 return RV;
762}
763
Chris Lattner6270f682006-10-08 22:57:01 +0000764/// getInputChainForNode - Given a node, return its input chain if it has one,
765/// otherwise return a null sd operand.
766static SDOperand getInputChainForNode(SDNode *N) {
767 if (unsigned NumOps = N->getNumOperands()) {
768 if (N->getOperand(0).getValueType() == MVT::Other)
769 return N->getOperand(0);
770 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
771 return N->getOperand(NumOps-1);
772 for (unsigned i = 1; i < NumOps-1; ++i)
773 if (N->getOperand(i).getValueType() == MVT::Other)
774 return N->getOperand(i);
775 }
776 return SDOperand(0, 0);
777}
778
Nate Begeman83e75ec2005-09-06 04:43:02 +0000779SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000780 // If N has two operands, where one has an input chain equal to the other,
781 // the 'other' chain is redundant.
782 if (N->getNumOperands() == 2) {
783 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
784 return N->getOperand(0);
785 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
786 return N->getOperand(1);
787 }
788
Chris Lattnerc76d4412007-05-16 06:37:59 +0000789 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
790 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
791 SmallPtrSet<SDNode*, 16> SeenOps;
792 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000793
794 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000795 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000796
Jim Laskey71382342006-10-07 23:37:56 +0000797 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000798 // encountered.
799 for (unsigned i = 0; i < TFs.size(); ++i) {
800 SDNode *TF = TFs[i];
801
Jim Laskey6ff23e52006-10-04 16:53:27 +0000802 // Check each of the operands.
803 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
804 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000805
Jim Laskey6ff23e52006-10-04 16:53:27 +0000806 switch (Op.getOpcode()) {
807 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000808 // Entry tokens don't need to be added to the list. They are
809 // rededundant.
810 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000811 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000812
Jim Laskey6ff23e52006-10-04 16:53:27 +0000813 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000814 if ((CombinerAA || Op.hasOneUse()) &&
815 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000816 // Queue up for processing.
817 TFs.push_back(Op.Val);
818 // Clean up in case the token factor is removed.
819 AddToWorkList(Op.Val);
820 Changed = true;
821 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000822 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000823 // Fall thru
824
825 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000826 // Only add if it isn't already in the list.
827 if (SeenOps.insert(Op.Val))
Jim Laskeybc588b82006-10-05 15:07:25 +0000828 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000829 else
830 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000831 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000832 }
833 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000834 }
835
836 SDOperand Result;
837
838 // If we've change things around then replace token factor.
839 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000840 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000841 // The entry token is the only possible outcome.
842 Result = DAG.getEntryNode();
843 } else {
844 // New and improved token factor.
845 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000846 }
Jim Laskey274062c2006-10-13 23:32:28 +0000847
848 // Don't add users to work list.
849 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000850 }
Jim Laskey279f0532006-09-25 16:29:54 +0000851
Jim Laskey6ff23e52006-10-04 16:53:27 +0000852 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000853}
854
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000855/// MERGE_VALUES can always be eliminated.
856SDOperand DAGCombiner::visitMERGE_VALUES(SDNode *N) {
857 WorkListRemover DeadNodes(*this);
858 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
859 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, i), N->getOperand(i),
860 &DeadNodes);
861 removeFromWorkList(N);
862 DAG.DeleteNode(N);
863 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
864}
865
866
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000867static
868SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
869 MVT::ValueType VT = N0.getValueType();
870 SDOperand N00 = N0.getOperand(0);
871 SDOperand N01 = N0.getOperand(1);
872 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
873 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
874 isa<ConstantSDNode>(N00.getOperand(1))) {
875 N0 = DAG.getNode(ISD::ADD, VT,
876 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
877 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
878 return DAG.getNode(ISD::ADD, VT, N0, N1);
879 }
880 return SDOperand();
881}
882
Evan Chengb13cdbd2007-06-21 07:39:16 +0000883static
884SDOperand combineSelectAndUse(SDNode *N, SDOperand Slct, SDOperand OtherOp,
885 SelectionDAG &DAG) {
886 MVT::ValueType VT = N->getValueType(0);
887 unsigned Opc = N->getOpcode();
888 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
889 SDOperand LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
890 SDOperand RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
891 ISD::CondCode CC = ISD::SETCC_INVALID;
892 if (isSlctCC)
893 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
894 else {
895 SDOperand CCOp = Slct.getOperand(0);
896 if (CCOp.getOpcode() == ISD::SETCC)
897 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
898 }
899
900 bool DoXform = false;
901 bool InvCC = false;
902 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
903 "Bad input!");
904 if (LHS.getOpcode() == ISD::Constant &&
905 cast<ConstantSDNode>(LHS)->isNullValue())
906 DoXform = true;
907 else if (CC != ISD::SETCC_INVALID &&
908 RHS.getOpcode() == ISD::Constant &&
909 cast<ConstantSDNode>(RHS)->isNullValue()) {
910 std::swap(LHS, RHS);
Chris Lattner4626b252008-01-17 07:20:38 +0000911 SDOperand Op0 = Slct.getOperand(0);
912 bool isInt = MVT::isInteger(isSlctCC ? Op0.getValueType()
913 : Op0.getOperand(0).getValueType());
Evan Chengb13cdbd2007-06-21 07:39:16 +0000914 CC = ISD::getSetCCInverse(CC, isInt);
915 DoXform = true;
916 InvCC = true;
917 }
918
919 if (DoXform) {
920 SDOperand Result = DAG.getNode(Opc, VT, OtherOp, RHS);
921 if (isSlctCC)
922 return DAG.getSelectCC(OtherOp, Result,
923 Slct.getOperand(0), Slct.getOperand(1), CC);
924 SDOperand CCOp = Slct.getOperand(0);
925 if (InvCC)
926 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
927 CCOp.getOperand(1), CC);
928 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
929 }
930 return SDOperand();
931}
932
Nate Begeman83e75ec2005-09-06 04:43:02 +0000933SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000934 SDOperand N0 = N->getOperand(0);
935 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000936 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
937 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000938 MVT::ValueType VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000939
940 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +0000941 if (MVT::isVector(VT)) {
942 SDOperand FoldedVOp = SimplifyVBinOp(N);
943 if (FoldedVOp.Val) return FoldedVOp;
944 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000945
Dan Gohman613e0d82007-07-03 14:03:57 +0000946 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000947 if (N0.getOpcode() == ISD::UNDEF)
948 return N0;
949 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000950 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000951 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000952 if (N0C && N1C)
Bill Wendling91b9ad12008-02-10 08:10:24 +0000953 return DAG.getConstant(N0C->getValue() + N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000954 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000955 if (N0C && !N1C)
956 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000957 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000958 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000959 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000960 // fold ((c1-A)+c2) -> (c1+c2)-A
961 if (N1C && N0.getOpcode() == ISD::SUB)
962 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
963 return DAG.getNode(ISD::SUB, VT,
964 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
965 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000966 // reassociate add
967 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
968 if (RADD.Val != 0)
969 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000970 // fold ((0-A) + B) -> B-A
971 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
972 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000973 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000974 // fold (A + (0-B)) -> A-B
975 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
976 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000977 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000978 // fold (A+(B-A)) -> B
979 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000980 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000981
Evan Cheng860771d2006-03-01 01:09:54 +0000982 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000983 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000984
985 // fold (a+b) -> (a|b) iff a and b share no bits.
986 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
Dan Gohman948d8ea2008-02-20 16:33:30 +0000987 APInt LHSZero, LHSOne;
988 APInt RHSZero, RHSOne;
989 APInt Mask = APInt::getAllOnesValue(MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +0000990 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +0000991 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +0000992 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000993
994 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
995 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
996 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
997 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
998 return DAG.getNode(ISD::OR, VT, N0, N1);
999 }
1000 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001001
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001002 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
1003 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
1004 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
1005 if (Result.Val) return Result;
1006 }
1007 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
1008 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
1009 if (Result.Val) return Result;
1010 }
1011
Evan Chengb13cdbd2007-06-21 07:39:16 +00001012 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
1013 if (N0.getOpcode() == ISD::SELECT && N0.Val->hasOneUse()) {
1014 SDOperand Result = combineSelectAndUse(N, N0, N1, DAG);
1015 if (Result.Val) return Result;
1016 }
1017 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1018 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1019 if (Result.Val) return Result;
1020 }
1021
Nate Begeman83e75ec2005-09-06 04:43:02 +00001022 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001023}
1024
Chris Lattner91153682007-03-04 20:03:15 +00001025SDOperand DAGCombiner::visitADDC(SDNode *N) {
1026 SDOperand N0 = N->getOperand(0);
1027 SDOperand N1 = N->getOperand(1);
1028 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1029 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1030 MVT::ValueType VT = N0.getValueType();
1031
1032 // If the flag result is dead, turn this into an ADD.
1033 if (N->hasNUsesOfValue(0, 1))
1034 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +00001035 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +00001036
1037 // canonicalize constant to RHS.
Chris Lattnerbcf24842007-03-04 20:08:45 +00001038 if (N0C && !N1C) {
1039 SDOperand Ops[] = { N1, N0 };
1040 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
1041 }
Chris Lattner91153682007-03-04 20:03:15 +00001042
Chris Lattnerb6541762007-03-04 20:40:38 +00001043 // fold (addc x, 0) -> x + no carry out
1044 if (N1C && N1C->isNullValue())
1045 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1046
1047 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001048 APInt LHSZero, LHSOne;
1049 APInt RHSZero, RHSOne;
1050 APInt Mask = APInt::getAllOnesValue(MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +00001051 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001052 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001053 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001054
1055 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1056 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1057 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1058 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1059 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1060 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1061 }
Chris Lattner91153682007-03-04 20:03:15 +00001062
1063 return SDOperand();
1064}
1065
1066SDOperand DAGCombiner::visitADDE(SDNode *N) {
1067 SDOperand N0 = N->getOperand(0);
1068 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +00001069 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001070 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1071 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Chris Lattnerbcf24842007-03-04 20:08:45 +00001072 //MVT::ValueType VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001073
1074 // canonicalize constant to RHS
Chris Lattnerbcf24842007-03-04 20:08:45 +00001075 if (N0C && !N1C) {
Chris Lattnerb6541762007-03-04 20:40:38 +00001076 SDOperand Ops[] = { N1, N0, CarryIn };
Chris Lattnerbcf24842007-03-04 20:08:45 +00001077 return DAG.getNode(ISD::ADDE, N->getVTList(), Ops, 3);
1078 }
Chris Lattner91153682007-03-04 20:03:15 +00001079
Chris Lattnerb6541762007-03-04 20:40:38 +00001080 // fold (adde x, y, false) -> (addc x, y)
1081 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) {
1082 SDOperand Ops[] = { N1, N0 };
1083 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
1084 }
Chris Lattner91153682007-03-04 20:03:15 +00001085
1086 return SDOperand();
1087}
1088
1089
1090
Nate Begeman83e75ec2005-09-06 04:43:02 +00001091SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001092 SDOperand N0 = N->getOperand(0);
1093 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001094 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1095 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001096 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001097
Dan Gohman7f321562007-06-25 16:23:39 +00001098 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001099 if (MVT::isVector(VT)) {
1100 SDOperand FoldedVOp = SimplifyVBinOp(N);
1101 if (FoldedVOp.Val) return FoldedVOp;
1102 }
Dan Gohman7f321562007-06-25 16:23:39 +00001103
Chris Lattner854077d2005-10-17 01:07:11 +00001104 // fold (sub x, x) -> 0
Evan Chengff247d22008-03-10 07:59:01 +00001105 if (N0 == N1) {
Evan Chengf79e6062008-03-10 07:19:13 +00001106 if (ISD::isBuildVectorAllZeros(N0.Val))
1107 // Zero vectors might be normalized to a particular vector type to ensure
1108 // they are CSE'd. Return it as it is.
1109 return N0;
Chris Lattner854077d2005-10-17 01:07:11 +00001110 return DAG.getConstant(0, N->getValueType(0));
Evan Chengff247d22008-03-10 07:59:01 +00001111 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001112 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001113 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001114 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001115 // fold (sub x, c) -> (add x, -c)
1116 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001117 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001118 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001119 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001120 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001121 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001122 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001123 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001124 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1125 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1126 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1127 if (Result.Val) return Result;
1128 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001129 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001130 if (N0.getOpcode() == ISD::UNDEF)
1131 return N0;
1132 if (N1.getOpcode() == ISD::UNDEF)
1133 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001134
Nate Begeman83e75ec2005-09-06 04:43:02 +00001135 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001136}
1137
Nate Begeman83e75ec2005-09-06 04:43:02 +00001138SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001139 SDOperand N0 = N->getOperand(0);
1140 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001141 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1142 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +00001143 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001144
Dan Gohman7f321562007-06-25 16:23:39 +00001145 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001146 if (MVT::isVector(VT)) {
1147 SDOperand FoldedVOp = SimplifyVBinOp(N);
1148 if (FoldedVOp.Val) return FoldedVOp;
1149 }
Dan Gohman7f321562007-06-25 16:23:39 +00001150
Dan Gohman613e0d82007-07-03 14:03:57 +00001151 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001152 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001153 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001154 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001155 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001156 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001157 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001158 if (N0C && !N1C)
1159 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001160 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001161 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001162 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001163 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001164 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001165 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001166 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +00001167 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +00001168 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001169 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001170 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001171 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1172 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1173 // FIXME: If the input is something that is easily negated (e.g. a
1174 // single-use add), we should put the negate there.
1175 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1176 DAG.getNode(ISD::SHL, VT, N0,
1177 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1178 TLI.getShiftAmountTy())));
1179 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001180
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001181 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1182 if (N1C && N0.getOpcode() == ISD::SHL &&
1183 isa<ConstantSDNode>(N0.getOperand(1))) {
1184 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001185 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001186 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1187 }
1188
1189 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1190 // use.
1191 {
1192 SDOperand Sh(0,0), Y(0,0);
1193 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1194 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1195 N0.Val->hasOneUse()) {
1196 Sh = N0; Y = N1;
1197 } else if (N1.getOpcode() == ISD::SHL &&
1198 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1199 Sh = N1; Y = N0;
1200 }
1201 if (Sh.Val) {
1202 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1203 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1204 }
1205 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001206 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1207 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1208 isa<ConstantSDNode>(N0.getOperand(1))) {
1209 return DAG.getNode(ISD::ADD, VT,
1210 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1211 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1212 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001213
Nate Begemancd4d58c2006-02-03 06:46:56 +00001214 // reassociate mul
1215 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1216 if (RMUL.Val != 0)
1217 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001218
Nate Begeman83e75ec2005-09-06 04:43:02 +00001219 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001220}
1221
Nate Begeman83e75ec2005-09-06 04:43:02 +00001222SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001223 SDOperand N0 = N->getOperand(0);
1224 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001225 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1226 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001227 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001228
Dan Gohman7f321562007-06-25 16:23:39 +00001229 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001230 if (MVT::isVector(VT)) {
1231 SDOperand FoldedVOp = SimplifyVBinOp(N);
1232 if (FoldedVOp.Val) return FoldedVOp;
1233 }
Dan Gohman7f321562007-06-25 16:23:39 +00001234
Nate Begeman1d4d4142005-09-01 00:19:25 +00001235 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001236 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001237 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001238 // fold (sdiv X, 1) -> X
1239 if (N1C && N1C->getSignExtended() == 1LL)
1240 return N0;
1241 // fold (sdiv X, -1) -> 0-X
1242 if (N1C && N1C->isAllOnesValue())
1243 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001244 // If we know the sign bits of both operands are zero, strength reduce to a
1245 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Chris Lattnerf32aac32008-01-27 23:32:17 +00001246 if (!MVT::isVector(VT)) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001247 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerf32aac32008-01-27 23:32:17 +00001248 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1249 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001250 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +00001251 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001252 (isPowerOf2_64(N1C->getSignExtended()) ||
1253 isPowerOf2_64(-N1C->getSignExtended()))) {
1254 // If dividing by powers of two is cheap, then don't perform the following
1255 // fold.
1256 if (TLI.isPow2DivCheap())
1257 return SDOperand();
1258 int64_t pow2 = N1C->getSignExtended();
1259 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001260 unsigned lg2 = Log2_64(abs2);
1261 // Splat the sign bit into the register
1262 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001263 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1264 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001265 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001266 // Add (N0 < 0) ? abs2 - 1 : 0;
1267 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1268 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001269 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001270 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001271 AddToWorkList(SRL.Val);
1272 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001273 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1274 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001275 // If we're dividing by a positive value, we're done. Otherwise, we must
1276 // negate the result.
1277 if (pow2 > 0)
1278 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001279 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001280 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1281 }
Nate Begeman69575232005-10-20 02:15:44 +00001282 // if integer divide is expensive and we satisfy the requirements, emit an
1283 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001284 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001285 !TLI.isIntDivCheap()) {
1286 SDOperand Op = BuildSDIV(N);
1287 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001288 }
Dan Gohman7f321562007-06-25 16:23:39 +00001289
Dan Gohman613e0d82007-07-03 14:03:57 +00001290 // undef / X -> 0
1291 if (N0.getOpcode() == ISD::UNDEF)
1292 return DAG.getConstant(0, VT);
1293 // X / undef -> undef
1294 if (N1.getOpcode() == ISD::UNDEF)
1295 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001296
Nate Begeman83e75ec2005-09-06 04:43:02 +00001297 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001298}
1299
Nate Begeman83e75ec2005-09-06 04:43:02 +00001300SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001301 SDOperand N0 = N->getOperand(0);
1302 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001303 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1304 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001305 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001306
Dan Gohman7f321562007-06-25 16:23:39 +00001307 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001308 if (MVT::isVector(VT)) {
1309 SDOperand FoldedVOp = SimplifyVBinOp(N);
1310 if (FoldedVOp.Val) return FoldedVOp;
1311 }
Dan Gohman7f321562007-06-25 16:23:39 +00001312
Nate Begeman1d4d4142005-09-01 00:19:25 +00001313 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001314 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001315 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001316 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +00001317 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001318 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001319 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001320 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001321 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1322 if (N1.getOpcode() == ISD::SHL) {
1323 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1324 if (isPowerOf2_64(SHC->getValue())) {
1325 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001326 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
1327 DAG.getConstant(Log2_64(SHC->getValue()),
1328 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001329 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001330 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001331 }
1332 }
1333 }
Nate Begeman69575232005-10-20 02:15:44 +00001334 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +00001335 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
1336 SDOperand Op = BuildUDIV(N);
1337 if (Op.Val) return Op;
1338 }
Dan Gohman7f321562007-06-25 16:23:39 +00001339
Dan Gohman613e0d82007-07-03 14:03:57 +00001340 // undef / X -> 0
1341 if (N0.getOpcode() == ISD::UNDEF)
1342 return DAG.getConstant(0, VT);
1343 // X / undef -> undef
1344 if (N1.getOpcode() == ISD::UNDEF)
1345 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001346
Nate Begeman83e75ec2005-09-06 04:43:02 +00001347 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001348}
1349
Nate Begeman83e75ec2005-09-06 04:43:02 +00001350SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001351 SDOperand N0 = N->getOperand(0);
1352 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001353 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1354 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001355 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001356
1357 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001358 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001359 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001360 // If we know the sign bits of both operands are zero, strength reduce to a
1361 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Chris Lattneree339f42008-01-27 23:21:58 +00001362 if (!MVT::isVector(VT)) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001363 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001364 return DAG.getNode(ISD::UREM, VT, N0, N1);
1365 }
Chris Lattner26d29902006-10-12 20:58:32 +00001366
Dan Gohman77003042007-11-26 23:46:11 +00001367 // If X/C can be simplified by the division-by-constant logic, lower
1368 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001369 if (N1C && !N1C->isNullValue()) {
1370 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Chris Lattner5eee4272008-01-26 01:09:19 +00001371 AddToWorkList(Div.Val);
Dan Gohman77003042007-11-26 23:46:11 +00001372 SDOperand OptimizedDiv = combine(Div.Val);
1373 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1374 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1375 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1376 AddToWorkList(Mul.Val);
1377 return Sub;
1378 }
Chris Lattner26d29902006-10-12 20:58:32 +00001379 }
1380
Dan Gohman613e0d82007-07-03 14:03:57 +00001381 // undef % X -> 0
1382 if (N0.getOpcode() == ISD::UNDEF)
1383 return DAG.getConstant(0, VT);
1384 // X % undef -> undef
1385 if (N1.getOpcode() == ISD::UNDEF)
1386 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001387
Nate Begeman83e75ec2005-09-06 04:43:02 +00001388 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001389}
1390
Nate Begeman83e75ec2005-09-06 04:43:02 +00001391SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001392 SDOperand N0 = N->getOperand(0);
1393 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001394 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1395 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001396 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001397
1398 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001399 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001400 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001401 // fold (urem x, pow2) -> (and x, pow2-1)
1402 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +00001403 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001404 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1405 if (N1.getOpcode() == ISD::SHL) {
1406 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1407 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +00001408 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001409 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001410 return DAG.getNode(ISD::AND, VT, N0, Add);
1411 }
1412 }
1413 }
Chris Lattner26d29902006-10-12 20:58:32 +00001414
Dan Gohman77003042007-11-26 23:46:11 +00001415 // If X/C can be simplified by the division-by-constant logic, lower
1416 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001417 if (N1C && !N1C->isNullValue()) {
1418 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman77003042007-11-26 23:46:11 +00001419 SDOperand OptimizedDiv = combine(Div.Val);
1420 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1421 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1422 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1423 AddToWorkList(Mul.Val);
1424 return Sub;
1425 }
Chris Lattner26d29902006-10-12 20:58:32 +00001426 }
1427
Dan Gohman613e0d82007-07-03 14:03:57 +00001428 // undef % X -> 0
1429 if (N0.getOpcode() == ISD::UNDEF)
1430 return DAG.getConstant(0, VT);
1431 // X % undef -> undef
1432 if (N1.getOpcode() == ISD::UNDEF)
1433 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001434
Nate Begeman83e75ec2005-09-06 04:43:02 +00001435 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001436}
1437
Nate Begeman83e75ec2005-09-06 04:43:02 +00001438SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001439 SDOperand N0 = N->getOperand(0);
1440 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001441 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001442 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001443
1444 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001445 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001446 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001447 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001448 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001449 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1450 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001451 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001452 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001453 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001454 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001455
Nate Begeman83e75ec2005-09-06 04:43:02 +00001456 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001457}
1458
Nate Begeman83e75ec2005-09-06 04:43:02 +00001459SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001460 SDOperand N0 = N->getOperand(0);
1461 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001462 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001463 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001464
1465 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001466 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001467 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001468 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001469 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001470 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001471 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001472 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001473 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001474
Nate Begeman83e75ec2005-09-06 04:43:02 +00001475 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001476}
1477
Dan Gohman389079b2007-10-08 17:57:15 +00001478/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1479/// compute two values. LoOp and HiOp give the opcodes for the two computations
1480/// that are being performed. Return true if a simplification was made.
1481///
Chris Lattner5eee4272008-01-26 01:09:19 +00001482SDOperand DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1483 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001484 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001485 bool HiExists = N->hasAnyUseOfValue(1);
1486 if (!HiExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001487 (!AfterLegalize ||
1488 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001489 SDOperand Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
1490 N->getNumOperands());
1491 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001492 }
1493
1494 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001495 bool LoExists = N->hasAnyUseOfValue(0);
1496 if (!LoExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001497 (!AfterLegalize ||
1498 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001499 SDOperand Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
1500 N->getNumOperands());
1501 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001502 }
1503
Evan Cheng44711942007-11-08 09:25:29 +00001504 // If both halves are used, return as it is.
1505 if (LoExists && HiExists)
Chris Lattner5eee4272008-01-26 01:09:19 +00001506 return SDOperand();
Evan Cheng44711942007-11-08 09:25:29 +00001507
1508 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001509 if (LoExists) {
1510 SDOperand Lo = DAG.getNode(LoOp, N->getValueType(0),
1511 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001512 AddToWorkList(Lo.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001513 SDOperand LoOpt = combine(Lo.Val);
Chris Lattner5eee4272008-01-26 01:09:19 +00001514 if (LoOpt.Val && LoOpt.Val != Lo.Val &&
1515 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType()))
1516 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001517 }
1518
Evan Cheng44711942007-11-08 09:25:29 +00001519 if (HiExists) {
1520 SDOperand Hi = DAG.getNode(HiOp, N->getValueType(1),
1521 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001522 AddToWorkList(Hi.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001523 SDOperand HiOpt = combine(Hi.Val);
1524 if (HiOpt.Val && HiOpt != Hi &&
Chris Lattner5eee4272008-01-26 01:09:19 +00001525 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType()))
1526 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001527 }
Chris Lattner5eee4272008-01-26 01:09:19 +00001528 return SDOperand();
Dan Gohman389079b2007-10-08 17:57:15 +00001529}
1530
1531SDOperand DAGCombiner::visitSMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001532 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
1533 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001534
1535 return SDOperand();
1536}
1537
1538SDOperand DAGCombiner::visitUMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001539 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
1540 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001541
1542 return SDOperand();
1543}
1544
1545SDOperand DAGCombiner::visitSDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001546 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
1547 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001548
1549 return SDOperand();
1550}
1551
1552SDOperand DAGCombiner::visitUDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001553 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
1554 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001555
1556 return SDOperand();
1557}
1558
Chris Lattner35e5c142006-05-05 05:51:50 +00001559/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1560/// two operands of the same opcode, try to simplify it.
1561SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1562 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1563 MVT::ValueType VT = N0.getValueType();
1564 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1565
Chris Lattner540121f2006-05-05 06:31:05 +00001566 // For each of OP in AND/OR/XOR:
1567 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1568 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1569 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001570 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001571 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001572 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001573 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1574 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1575 N0.getOperand(0).getValueType(),
1576 N0.getOperand(0), N1.getOperand(0));
1577 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001578 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001579 }
1580
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001581 // For each of OP in SHL/SRL/SRA/AND...
1582 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1583 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1584 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001585 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001586 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001587 N0.getOperand(1) == N1.getOperand(1)) {
1588 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1589 N0.getOperand(0).getValueType(),
1590 N0.getOperand(0), N1.getOperand(0));
1591 AddToWorkList(ORNode.Val);
1592 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1593 }
1594
1595 return SDOperand();
1596}
1597
Nate Begeman83e75ec2005-09-06 04:43:02 +00001598SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001599 SDOperand N0 = N->getOperand(0);
1600 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001601 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001602 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1603 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001604 MVT::ValueType VT = N1.getValueType();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001605 unsigned BitWidth = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001606
Dan Gohman7f321562007-06-25 16:23:39 +00001607 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001608 if (MVT::isVector(VT)) {
1609 SDOperand FoldedVOp = SimplifyVBinOp(N);
1610 if (FoldedVOp.Val) return FoldedVOp;
1611 }
Dan Gohman7f321562007-06-25 16:23:39 +00001612
Dan Gohman613e0d82007-07-03 14:03:57 +00001613 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001614 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001615 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001616 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001617 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001618 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001619 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001620 if (N0C && !N1C)
1621 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001622 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001623 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001624 return N0;
1625 // if (and x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001626 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
1627 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001628 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001629 // reassociate and
1630 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1631 if (RAND.Val != 0)
1632 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001633 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001634 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001635 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001636 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001637 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001638 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1639 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001640 SDOperand N0Op0 = N0.getOperand(0);
1641 APInt Mask = ~N1C->getAPIntValue();
1642 Mask.trunc(N0Op0.getValueSizeInBits());
1643 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001644 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001645 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001646
1647 // Replace uses of the AND with uses of the Zero extend node.
1648 CombineTo(N, Zext);
1649
Chris Lattner3603cd62006-02-02 07:17:31 +00001650 // We actually want to replace all uses of the any_extend with the
1651 // zero_extend, to avoid duplicating things. This will later cause this
1652 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001653 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001654 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001655 }
1656 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001657 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1658 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1659 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1660 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1661
1662 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1663 MVT::isInteger(LL.getValueType())) {
1664 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1665 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1666 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001667 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001668 return DAG.getSetCC(VT, ORNode, LR, Op1);
1669 }
1670 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1671 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1672 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001673 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001674 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1675 }
1676 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1677 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1678 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001679 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001680 return DAG.getSetCC(VT, ORNode, LR, Op1);
1681 }
1682 }
1683 // canonicalize equivalent to ll == rl
1684 if (LL == RR && LR == RL) {
1685 Op1 = ISD::getSetCCSwappedOperands(Op1);
1686 std::swap(RL, RR);
1687 }
1688 if (LL == RL && LR == RR) {
1689 bool isInteger = MVT::isInteger(LL.getValueType());
1690 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1691 if (Result != ISD::SETCC_INVALID)
1692 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1693 }
1694 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001695
1696 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1697 if (N0.getOpcode() == N1.getOpcode()) {
1698 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1699 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001700 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001701
Nate Begemande996292006-02-03 22:24:05 +00001702 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1703 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001704 if (!MVT::isVector(VT) &&
1705 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001706 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001707 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001708 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001709 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001710 MVT::ValueType EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001711 // If we zero all the possible extended bits, then we can turn this into
1712 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001713 unsigned BitWidth = N1.getValueSizeInBits();
1714 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
1715 BitWidth - MVT::getSizeInBits(EVT))) &&
Evan Chengc5484282006-10-04 00:56:09 +00001716 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001717 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1718 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001719 LN0->getSrcValueOffset(), EVT,
1720 LN0->isVolatile(),
1721 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001722 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001723 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001724 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001725 }
1726 }
1727 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001728 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1729 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001730 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001731 MVT::ValueType EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001732 // If we zero all the possible extended bits, then we can turn this into
1733 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001734 unsigned BitWidth = N1.getValueSizeInBits();
1735 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
1736 BitWidth - MVT::getSizeInBits(EVT))) &&
Evan Chengc5484282006-10-04 00:56:09 +00001737 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001738 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1739 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001740 LN0->getSrcValueOffset(), EVT,
1741 LN0->isVolatile(),
1742 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001743 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001744 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001745 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001746 }
1747 }
Chris Lattner15045b62006-02-28 06:35:35 +00001748
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001749 // fold (and (load x), 255) -> (zextload x, i8)
1750 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001751 if (N1C && N0.getOpcode() == ISD::LOAD) {
1752 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1753 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Chris Lattnerddf89562008-01-17 19:59:44 +00001754 LN0->isUnindexed() && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001755 MVT::ValueType EVT, LoadedVT;
1756 if (N1C->getValue() == 255)
1757 EVT = MVT::i8;
1758 else if (N1C->getValue() == 65535)
1759 EVT = MVT::i16;
1760 else if (N1C->getValue() == ~0U)
1761 EVT = MVT::i32;
1762 else
1763 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001764
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001765 LoadedVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001766 if (EVT != MVT::Other && LoadedVT > EVT &&
1767 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1768 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1769 // For big endian targets, we need to add an offset to the pointer to
1770 // load the correct bytes. For little endian systems, we merely need to
1771 // read fewer bytes from the same pointer.
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001772 unsigned LVTStoreBytes = MVT::getStoreSizeInBits(LoadedVT)/8;
1773 unsigned EVTStoreBytes = MVT::getStoreSizeInBits(EVT)/8;
1774 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001775 unsigned Alignment = LN0->getAlignment();
Evan Cheng466685d2006-10-09 20:57:25 +00001776 SDOperand NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001777 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001778 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1779 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001780 Alignment = MinAlign(Alignment, PtrOff);
1781 }
Evan Cheng466685d2006-10-09 20:57:25 +00001782 AddToWorkList(NewPtr.Val);
1783 SDOperand Load =
1784 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001785 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001786 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001787 AddToWorkList(N);
1788 CombineTo(N0.Val, Load, Load.getValue(1));
1789 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1790 }
Chris Lattner15045b62006-02-28 06:35:35 +00001791 }
1792 }
1793
Nate Begeman83e75ec2005-09-06 04:43:02 +00001794 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001795}
1796
Nate Begeman83e75ec2005-09-06 04:43:02 +00001797SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001798 SDOperand N0 = N->getOperand(0);
1799 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001800 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001801 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1802 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001803 MVT::ValueType VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001804
Dan Gohman7f321562007-06-25 16:23:39 +00001805 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001806 if (MVT::isVector(VT)) {
1807 SDOperand FoldedVOp = SimplifyVBinOp(N);
1808 if (FoldedVOp.Val) return FoldedVOp;
1809 }
Dan Gohman7f321562007-06-25 16:23:39 +00001810
Dan Gohman613e0d82007-07-03 14:03:57 +00001811 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001812 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001813 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001814 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001815 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001816 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001817 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001818 if (N0C && !N1C)
1819 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001820 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001821 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001822 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001823 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001824 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001825 return N1;
1826 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001827 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001828 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001829 // reassociate or
1830 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1831 if (ROR.Val != 0)
1832 return ROR;
1833 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1834 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001835 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001836 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1837 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1838 N1),
1839 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001840 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001841 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1842 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1843 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1844 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1845
1846 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1847 MVT::isInteger(LL.getValueType())) {
1848 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1849 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1850 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1851 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1852 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001853 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001854 return DAG.getSetCC(VT, ORNode, LR, Op1);
1855 }
1856 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1857 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1858 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1859 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1860 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001861 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001862 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1863 }
1864 }
1865 // canonicalize equivalent to ll == rl
1866 if (LL == RR && LR == RL) {
1867 Op1 = ISD::getSetCCSwappedOperands(Op1);
1868 std::swap(RL, RR);
1869 }
1870 if (LL == RL && LR == RR) {
1871 bool isInteger = MVT::isInteger(LL.getValueType());
1872 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1873 if (Result != ISD::SETCC_INVALID)
1874 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1875 }
1876 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001877
1878 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1879 if (N0.getOpcode() == N1.getOpcode()) {
1880 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1881 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001882 }
Chris Lattner516b9622006-09-14 20:50:57 +00001883
Chris Lattner1ec72732006-09-14 21:11:37 +00001884 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1885 if (N0.getOpcode() == ISD::AND &&
1886 N1.getOpcode() == ISD::AND &&
1887 N0.getOperand(1).getOpcode() == ISD::Constant &&
1888 N1.getOperand(1).getOpcode() == ISD::Constant &&
1889 // Don't increase # computations.
1890 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1891 // We can only do this xform if we know that bits from X that are set in C2
1892 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001893 const APInt &LHSMask =
1894 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1895 const APInt &RHSMask =
1896 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00001897
Dan Gohmanea859be2007-06-22 14:59:07 +00001898 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1899 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001900 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1901 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1902 }
1903 }
1904
1905
Chris Lattner516b9622006-09-14 20:50:57 +00001906 // See if this is some rotate idiom.
1907 if (SDNode *Rot = MatchRotate(N0, N1))
1908 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001909
Nate Begeman83e75ec2005-09-06 04:43:02 +00001910 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001911}
1912
Chris Lattner516b9622006-09-14 20:50:57 +00001913
1914/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1915static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1916 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001917 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001918 Mask = Op.getOperand(1);
1919 Op = Op.getOperand(0);
1920 } else {
1921 return false;
1922 }
1923 }
1924
1925 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1926 Shift = Op;
1927 return true;
1928 }
1929 return false;
1930}
1931
1932
1933// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1934// idioms for rotate, and if the target supports rotation instructions, generate
1935// a rot[lr].
1936SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1937 // Must be a legal type. Expanded an promoted things won't work with rotates.
1938 MVT::ValueType VT = LHS.getValueType();
1939 if (!TLI.isTypeLegal(VT)) return 0;
1940
1941 // The target must have at least one rotate flavor.
1942 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1943 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1944 if (!HasROTL && !HasROTR) return 0;
1945
1946 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1947 SDOperand LHSShift; // The shift.
1948 SDOperand LHSMask; // AND value if any.
1949 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1950 return 0; // Not part of a rotate.
1951
1952 SDOperand RHSShift; // The shift.
1953 SDOperand RHSMask; // AND value if any.
1954 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1955 return 0; // Not part of a rotate.
1956
1957 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1958 return 0; // Not shifting the same value.
1959
1960 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1961 return 0; // Shifts must disagree.
1962
1963 // Canonicalize shl to left side in a shl/srl pair.
1964 if (RHSShift.getOpcode() == ISD::SHL) {
1965 std::swap(LHS, RHS);
1966 std::swap(LHSShift, RHSShift);
1967 std::swap(LHSMask , RHSMask );
1968 }
1969
1970 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Scott Michelc9dc1142007-04-02 21:36:32 +00001971 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1972 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1973 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001974
1975 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1976 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001977 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1978 RHSShiftAmt.getOpcode() == ISD::Constant) {
1979 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
1980 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00001981 if ((LShVal + RShVal) != OpSizeInBits)
1982 return 0;
1983
1984 SDOperand Rot;
1985 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001986 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001987 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001988 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001989
1990 // If there is an AND of either shifted operand, apply it to the result.
1991 if (LHSMask.Val || RHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00001992 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Chris Lattner516b9622006-09-14 20:50:57 +00001993
1994 if (LHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00001995 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
1996 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00001997 }
1998 if (RHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00001999 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2000 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002001 }
2002
2003 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2004 }
2005
2006 return Rot.Val;
2007 }
2008
2009 // If there is a mask here, and we have a variable shift, we can't be sure
2010 // that we're masking out the right stuff.
2011 if (LHSMask.Val || RHSMask.Val)
2012 return 0;
2013
2014 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2015 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002016 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2017 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002018 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002019 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002020 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002021 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002022 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002023 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002024 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002025 }
Chris Lattner516b9622006-09-14 20:50:57 +00002026 }
2027 }
2028
2029 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2030 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002031 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2032 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002033 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002034 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002035 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002036 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002037 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002038 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002039 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002040 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002041 }
2042 }
2043
2044 // Look for sign/zext/any-extended cases:
2045 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2046 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2047 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
2048 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2049 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2050 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
2051 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
2052 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
2053 if (RExtOp0.getOpcode() == ISD::SUB &&
2054 RExtOp0.getOperand(1) == LExtOp0) {
2055 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2056 // (rotr x, y)
2057 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2058 // (rotl x, (sub 32, y))
2059 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
2060 if (SUBC->getValue() == OpSizeInBits) {
2061 if (HasROTL)
2062 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2063 else
2064 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
2065 }
2066 }
2067 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2068 RExtOp0 == LExtOp0.getOperand(1)) {
2069 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2070 // (rotl x, y)
2071 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2072 // (rotr x, (sub 32, y))
2073 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
2074 if (SUBC->getValue() == OpSizeInBits) {
2075 if (HasROTL)
2076 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
2077 else
2078 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2079 }
2080 }
Chris Lattner516b9622006-09-14 20:50:57 +00002081 }
2082 }
2083
2084 return 0;
2085}
2086
2087
Nate Begeman83e75ec2005-09-06 04:43:02 +00002088SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002089 SDOperand N0 = N->getOperand(0);
2090 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002091 SDOperand LHS, RHS, CC;
2092 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2093 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002094 MVT::ValueType VT = N0.getValueType();
2095
Dan Gohman7f321562007-06-25 16:23:39 +00002096 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00002097 if (MVT::isVector(VT)) {
2098 SDOperand FoldedVOp = SimplifyVBinOp(N);
2099 if (FoldedVOp.Val) return FoldedVOp;
2100 }
Dan Gohman7f321562007-06-25 16:23:39 +00002101
Dan Gohman613e0d82007-07-03 14:03:57 +00002102 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002103 if (N0.getOpcode() == ISD::UNDEF)
2104 return N0;
2105 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002106 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002107 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002108 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002109 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00002110 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002111 if (N0C && !N1C)
2112 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002113 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002114 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002115 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002116 // reassociate xor
2117 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
2118 if (RXOR.Val != 0)
2119 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002120 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00002121 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
2122 bool isInt = MVT::isInteger(LHS.getValueType());
2123 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2124 isInt);
2125 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002126 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002127 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002128 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002129 assert(0 && "Unhandled SetCC Equivalent!");
2130 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002131 }
Chris Lattner61c5ff42007-09-10 21:39:07 +00002132 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
2133 if (N1C && N1C->getValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
2134 N0.Val->hasOneUse() && isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
2135 SDOperand V = N0.getOperand(0);
2136 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002137 DAG.getConstant(1, V.getValueType()));
Chris Lattner61c5ff42007-09-10 21:39:07 +00002138 AddToWorkList(V.Val);
2139 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2140 }
2141
Nate Begeman99801192005-09-07 23:25:52 +00002142 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Chris Lattner734c91d2006-11-10 21:37:15 +00002143 if (N1C && N1C->getValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002144 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002145 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002146 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2147 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002148 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2149 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002150 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002151 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002152 }
2153 }
Nate Begeman99801192005-09-07 23:25:52 +00002154 // fold !(x or y) -> (!x and !y) iff x or y are constants
2155 if (N1C && N1C->isAllOnesValue() &&
2156 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002157 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002158 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2159 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002160 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2161 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002162 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002163 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002164 }
2165 }
Nate Begeman223df222005-09-08 20:18:10 +00002166 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2167 if (N1C && N0.getOpcode() == ISD::XOR) {
2168 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2169 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2170 if (N00C)
2171 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
2172 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
2173 if (N01C)
2174 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
2175 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
2176 }
2177 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002178 if (N0 == N1) {
2179 if (!MVT::isVector(VT)) {
2180 return DAG.getConstant(0, VT);
2181 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2182 // Produce a vector of zeros.
Dan Gohman51eaa862007-06-14 22:58:02 +00002183 SDOperand El = DAG.getConstant(0, MVT::getVectorElementType(VT));
Chris Lattner4fbdd592006-03-28 19:11:05 +00002184 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002185 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002186 }
2187 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002188
2189 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2190 if (N0.getOpcode() == N1.getOpcode()) {
2191 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
2192 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002193 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002194
Chris Lattner3e104b12006-04-08 04:15:24 +00002195 // Simplify the expression using non-local knowledge.
2196 if (!MVT::isVector(VT) &&
2197 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002198 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002199
Nate Begeman83e75ec2005-09-06 04:43:02 +00002200 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002201}
2202
Chris Lattnere70da202007-12-06 07:33:36 +00002203/// visitShiftByConstant - Handle transforms common to the three shifts, when
2204/// the shift amount is a constant.
2205SDOperand DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
2206 SDNode *LHS = N->getOperand(0).Val;
2207 if (!LHS->hasOneUse()) return SDOperand();
2208
2209 // We want to pull some binops through shifts, so that we have (and (shift))
2210 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2211 // thing happens with address calculations, so it's important to canonicalize
2212 // it.
2213 bool HighBitSet = false; // Can we transform this if the high bit is set?
2214
2215 switch (LHS->getOpcode()) {
2216 default: return SDOperand();
2217 case ISD::OR:
2218 case ISD::XOR:
2219 HighBitSet = false; // We can only transform sra if the high bit is clear.
2220 break;
2221 case ISD::AND:
2222 HighBitSet = true; // We can only transform sra if the high bit is set.
2223 break;
2224 case ISD::ADD:
2225 if (N->getOpcode() != ISD::SHL)
2226 return SDOperand(); // only shl(add) not sr[al](add).
2227 HighBitSet = false; // We can only transform sra if the high bit is clear.
2228 break;
2229 }
2230
2231 // We require the RHS of the binop to be a constant as well.
2232 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
2233 if (!BinOpCst) return SDOperand();
2234
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002235
2236 // FIXME: disable this for unless the input to the binop is a shift by a
2237 // constant. If it is not a shift, it pessimizes some common cases like:
2238 //
2239 //void foo(int *X, int i) { X[i & 1235] = 1; }
2240 //int bar(int *X, int i) { return X[i & 255]; }
2241 SDNode *BinOpLHSVal = LHS->getOperand(0).Val;
2242 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2243 BinOpLHSVal->getOpcode() != ISD::SRA &&
2244 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2245 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
2246 return SDOperand();
2247
Chris Lattnere70da202007-12-06 07:33:36 +00002248 MVT::ValueType VT = N->getValueType(0);
2249
2250 // If this is a signed shift right, and the high bit is modified
2251 // by the logical operation, do not perform the transformation.
2252 // The highBitSet boolean indicates the value of the high bit of
2253 // the constant which would cause it to be modified for this
2254 // operation.
2255 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002256 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2257 if (BinOpRHSSignSet != HighBitSet)
Chris Lattnere70da202007-12-06 07:33:36 +00002258 return SDOperand();
2259 }
2260
2261 // Fold the constants, shifting the binop RHS by the shift amount.
2262 SDOperand NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
2263 LHS->getOperand(1), N->getOperand(1));
2264
2265 // Create the new shift.
2266 SDOperand NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
2267 N->getOperand(1));
2268
2269 // Create the new binop.
2270 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2271}
2272
2273
Nate Begeman83e75ec2005-09-06 04:43:02 +00002274SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002275 SDOperand N0 = N->getOperand(0);
2276 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002277 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2278 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002279 MVT::ValueType VT = N0.getValueType();
2280 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2281
2282 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002283 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002284 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002285 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002286 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002287 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002288 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002289 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002290 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002291 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002292 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002293 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002294 // if (shl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002295 if (DAG.MaskedValueIsZero(SDOperand(N, 0),
2296 APInt::getAllOnesValue(MVT::getSizeInBits(VT))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002297 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00002298 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002299 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002300 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002301 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002302 N0.getOperand(1).getOpcode() == ISD::Constant) {
2303 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002304 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002305 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002306 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002307 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002308 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002309 }
2310 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2311 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002312 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002313 N0.getOperand(1).getOpcode() == ISD::Constant) {
2314 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002315 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002316 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
2317 DAG.getConstant(~0ULL << c1, VT));
2318 if (c2 > c1)
2319 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002320 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002321 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002322 return DAG.getNode(ISD::SRL, VT, Mask,
2323 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002324 }
2325 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002326 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002327 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002328 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002329
2330 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002331}
2332
Nate Begeman83e75ec2005-09-06 04:43:02 +00002333SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002334 SDOperand N0 = N->getOperand(0);
2335 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002336 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2337 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002338 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002339
2340 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002341 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002342 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002343 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002344 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002345 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002346 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002347 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002348 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002349 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00002350 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002351 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002352 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002353 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002354 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002355 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2356 // sext_inreg.
2357 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
2358 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
2359 MVT::ValueType EVT;
2360 switch (LowBits) {
2361 default: EVT = MVT::Other; break;
2362 case 1: EVT = MVT::i1; break;
2363 case 8: EVT = MVT::i8; break;
2364 case 16: EVT = MVT::i16; break;
2365 case 32: EVT = MVT::i32; break;
2366 }
2367 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
2368 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2369 DAG.getValueType(EVT));
2370 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002371
2372 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2373 if (N1C && N0.getOpcode() == ISD::SRA) {
2374 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2375 unsigned Sum = N1C->getValue() + C1->getValue();
2376 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
2377 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2378 DAG.getConstant(Sum, N1C->getValueType(0)));
2379 }
2380 }
2381
Chris Lattnera8504462006-05-08 20:51:54 +00002382 // Simplify, based on bits shifted out of the LHS.
2383 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2384 return SDOperand(N, 0);
2385
2386
Nate Begeman1d4d4142005-09-01 00:19:25 +00002387 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002388 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002389 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002390
2391 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002392}
2393
Nate Begeman83e75ec2005-09-06 04:43:02 +00002394SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002395 SDOperand N0 = N->getOperand(0);
2396 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002397 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2398 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002399 MVT::ValueType VT = N0.getValueType();
2400 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2401
2402 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002403 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002404 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002405 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002406 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002407 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002408 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002409 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002410 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002411 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002412 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002413 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002414 // if (srl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002415 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
2416 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002417 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002418
Nate Begeman1d4d4142005-09-01 00:19:25 +00002419 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002420 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002421 N0.getOperand(1).getOpcode() == ISD::Constant) {
2422 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002423 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002424 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002425 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002426 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002427 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002428 }
Chris Lattner350bec02006-04-02 06:11:11 +00002429
Chris Lattner06afe072006-05-05 22:53:17 +00002430 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2431 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2432 // Shifting in all undef bits?
2433 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
2434 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
2435 return DAG.getNode(ISD::UNDEF, VT);
2436
2437 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
2438 AddToWorkList(SmallShift.Val);
2439 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2440 }
2441
Chris Lattner3657ffe2006-10-12 20:23:19 +00002442 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2443 // bit, which is unmodified by sra.
2444 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
2445 if (N0.getOpcode() == ISD::SRA)
2446 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2447 }
2448
Chris Lattner350bec02006-04-02 06:11:11 +00002449 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2450 if (N1C && N0.getOpcode() == ISD::CTLZ &&
2451 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002452 APInt KnownZero, KnownOne;
2453 APInt Mask = APInt::getAllOnesValue(MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +00002454 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002455
2456 // If any of the input bits are KnownOne, then the input couldn't be all
2457 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002458 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002459
2460 // If all of the bits input the to ctlz node are known to be zero, then
2461 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002462 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002463 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2464
2465 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2466 if ((UnknownBits & (UnknownBits-1)) == 0) {
2467 // Okay, we know that only that the single bit specified by UnknownBits
2468 // could be set on input to the CTLZ node. If this bit is set, the SRL
2469 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2470 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002471 unsigned ShAmt = UnknownBits.countTrailingZeros();
Chris Lattner350bec02006-04-02 06:11:11 +00002472 SDOperand Op = N0.getOperand(0);
2473 if (ShAmt) {
2474 Op = DAG.getNode(ISD::SRL, VT, Op,
2475 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2476 AddToWorkList(Op.Val);
2477 }
2478 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2479 }
2480 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002481
2482 // fold operands of srl based on knowledge that the low bits are not
2483 // demanded.
2484 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2485 return SDOperand(N, 0);
2486
Chris Lattnere70da202007-12-06 07:33:36 +00002487 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002488}
2489
Nate Begeman83e75ec2005-09-06 04:43:02 +00002490SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002491 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002492 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002493
2494 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002495 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002496 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002497 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002498}
2499
Nate Begeman83e75ec2005-09-06 04:43:02 +00002500SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002501 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002502 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002503
2504 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002505 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002506 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002507 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002508}
2509
Nate Begeman83e75ec2005-09-06 04:43:02 +00002510SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002511 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002512 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002513
2514 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002515 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002516 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002517 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002518}
2519
Nate Begeman452d7be2005-09-16 00:54:12 +00002520SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2521 SDOperand N0 = N->getOperand(0);
2522 SDOperand N1 = N->getOperand(1);
2523 SDOperand N2 = N->getOperand(2);
2524 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2525 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2526 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2527 MVT::ValueType VT = N->getValueType(0);
Evan Cheng571c4782007-08-18 05:57:05 +00002528 MVT::ValueType VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002529
Nate Begeman452d7be2005-09-16 00:54:12 +00002530 // fold select C, X, X -> X
2531 if (N1 == N2)
2532 return N1;
2533 // fold select true, X, Y -> X
2534 if (N0C && !N0C->isNullValue())
2535 return N1;
2536 // fold select false, X, Y -> Y
2537 if (N0C && N0C->isNullValue())
2538 return N2;
2539 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002540 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002541 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002542 // fold select C, 0, 1 -> ~C
2543 if (MVT::isInteger(VT) && MVT::isInteger(VT0) &&
2544 N1C && N2C && N1C->isNullValue() && N2C->getValue() == 1) {
2545 SDOperand XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
2546 if (VT == VT0)
2547 return XORNode;
2548 AddToWorkList(XORNode.Val);
2549 if (MVT::getSizeInBits(VT) > MVT::getSizeInBits(VT0))
2550 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2551 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2552 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002553 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002554 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
2555 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002556 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002557 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2558 }
2559 // fold select C, X, 1 -> ~C | X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002560 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getValue() == 1) {
2561 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002562 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002563 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2564 }
2565 // fold select C, X, 0 -> C & X
2566 // FIXME: this should check for C type == X type, not i1?
2567 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2568 return DAG.getNode(ISD::AND, VT, N0, N1);
2569 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2570 if (MVT::i1 == VT && N0 == N1)
2571 return DAG.getNode(ISD::OR, VT, N0, N2);
2572 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2573 if (MVT::i1 == VT && N0 == N2)
2574 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002575
Chris Lattner40c62d52005-10-18 06:04:22 +00002576 // If we can fold this based on the true/false value, do so.
2577 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002578 return SDOperand(N, 0); // Don't revisit N.
2579
Nate Begeman44728a72005-09-19 22:34:01 +00002580 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002581 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002582 // FIXME:
2583 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2584 // having to say they don't support SELECT_CC on every type the DAG knows
2585 // about, since there is no way to mark an opcode illegal at all value types
2586 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2587 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2588 N1, N2, N0.getOperand(2));
2589 else
2590 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002591 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002592 return SDOperand();
2593}
2594
2595SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002596 SDOperand N0 = N->getOperand(0);
2597 SDOperand N1 = N->getOperand(1);
2598 SDOperand N2 = N->getOperand(2);
2599 SDOperand N3 = N->getOperand(3);
2600 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002601 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2602
Nate Begeman44728a72005-09-19 22:34:01 +00002603 // fold select_cc lhs, rhs, x, x, cc -> x
2604 if (N2 == N3)
2605 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002606
Chris Lattner5f42a242006-09-20 06:19:26 +00002607 // Determine if the condition we're dealing with is constant
2608 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002609 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002610
2611 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
2612 if (SCCC->getValue())
2613 return N2; // cond always true -> true val
2614 else
2615 return N3; // cond always false -> false val
2616 }
2617
2618 // Fold to a simpler select_cc
2619 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2620 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2621 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2622 SCC.getOperand(2));
2623
Chris Lattner40c62d52005-10-18 06:04:22 +00002624 // If we can fold this based on the true/false value, do so.
2625 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002626 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002627
Nate Begeman44728a72005-09-19 22:34:01 +00002628 // fold select_cc into other things, such as min/max/abs
2629 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002630}
2631
2632SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2633 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2634 cast<CondCodeSDNode>(N->getOperand(2))->get());
2635}
2636
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002637// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2638// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2639// transformation. Returns true if extension are possible and the above
2640// mentioned transformation is profitable.
2641static bool ExtendUsesToFormExtLoad(SDNode *N, SDOperand N0,
2642 unsigned ExtOpc,
2643 SmallVector<SDNode*, 4> &ExtendNodes,
2644 TargetLowering &TLI) {
2645 bool HasCopyToRegUses = false;
2646 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
2647 for (SDNode::use_iterator UI = N0.Val->use_begin(), UE = N0.Val->use_end();
2648 UI != UE; ++UI) {
2649 SDNode *User = *UI;
2650 if (User == N)
2651 continue;
2652 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2653 if (User->getOpcode() == ISD::SETCC) {
2654 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2655 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2656 // Sign bits will be lost after a zext.
2657 return false;
2658 bool Add = false;
2659 for (unsigned i = 0; i != 2; ++i) {
2660 SDOperand UseOp = User->getOperand(i);
2661 if (UseOp == N0)
2662 continue;
2663 if (!isa<ConstantSDNode>(UseOp))
2664 return false;
2665 Add = true;
2666 }
2667 if (Add)
2668 ExtendNodes.push_back(User);
2669 } else {
2670 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2671 SDOperand UseOp = User->getOperand(i);
2672 if (UseOp == N0) {
2673 // If truncate from extended type to original load type is free
2674 // on this target, then it's ok to extend a CopyToReg.
2675 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2676 HasCopyToRegUses = true;
2677 else
2678 return false;
2679 }
2680 }
2681 }
2682 }
2683
2684 if (HasCopyToRegUses) {
2685 bool BothLiveOut = false;
2686 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2687 UI != UE; ++UI) {
2688 SDNode *User = *UI;
2689 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2690 SDOperand UseOp = User->getOperand(i);
2691 if (UseOp.Val == N && UseOp.ResNo == 0) {
2692 BothLiveOut = true;
2693 break;
2694 }
2695 }
2696 }
2697 if (BothLiveOut)
2698 // Both unextended and extended values are live out. There had better be
2699 // good a reason for the transformation.
2700 return ExtendNodes.size();
2701 }
2702 return true;
2703}
2704
Nate Begeman83e75ec2005-09-06 04:43:02 +00002705SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002706 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002707 MVT::ValueType VT = N->getValueType(0);
2708
Nate Begeman1d4d4142005-09-01 00:19:25 +00002709 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002710 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002711 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002712
Nate Begeman1d4d4142005-09-01 00:19:25 +00002713 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002714 // fold (sext (aext x)) -> (sext x)
2715 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002716 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002717
Evan Chengc88138f2007-03-22 01:54:19 +00002718 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2719 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Chris Lattner22558872007-02-26 03:13:59 +00002720 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002721 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002722 if (NarrowLoad.Val) {
2723 if (NarrowLoad.Val != N0.Val)
2724 CombineTo(N0.Val, NarrowLoad);
2725 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2726 }
Evan Chengc88138f2007-03-22 01:54:19 +00002727 }
2728
2729 // See if the value being truncated is already sign extended. If so, just
2730 // eliminate the trunc/sext pair.
2731 if (N0.getOpcode() == ISD::TRUNCATE) {
Chris Lattner6007b842006-09-21 06:00:20 +00002732 SDOperand Op = N0.getOperand(0);
Chris Lattner22558872007-02-26 03:13:59 +00002733 unsigned OpBits = MVT::getSizeInBits(Op.getValueType());
2734 unsigned MidBits = MVT::getSizeInBits(N0.getValueType());
2735 unsigned DestBits = MVT::getSizeInBits(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002736 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002737
2738 if (OpBits == DestBits) {
2739 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2740 // bits, it is already ready.
2741 if (NumSignBits > DestBits-MidBits)
2742 return Op;
2743 } else if (OpBits < DestBits) {
2744 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2745 // bits, just sext from i32.
2746 if (NumSignBits > OpBits-MidBits)
2747 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2748 } else {
2749 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2750 // bits, just truncate to i32.
2751 if (NumSignBits > OpBits-MidBits)
2752 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002753 }
Chris Lattner22558872007-02-26 03:13:59 +00002754
2755 // fold (sext (truncate x)) -> (sextinreg x).
2756 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2757 N0.getValueType())) {
2758 if (Op.getValueType() < VT)
2759 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2760 else if (Op.getValueType() > VT)
2761 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2762 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2763 DAG.getValueType(N0.getValueType()));
2764 }
Chris Lattner6007b842006-09-21 06:00:20 +00002765 }
Chris Lattner310b5782006-05-06 23:06:26 +00002766
Evan Cheng110dec22005-12-14 02:19:23 +00002767 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002768 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002769 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002770 bool DoXform = true;
2771 SmallVector<SDNode*, 4> SetCCs;
2772 if (!N0.hasOneUse())
2773 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2774 if (DoXform) {
2775 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2776 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2777 LN0->getBasePtr(), LN0->getSrcValue(),
2778 LN0->getSrcValueOffset(),
2779 N0.getValueType(),
2780 LN0->isVolatile(),
2781 LN0->getAlignment());
2782 CombineTo(N, ExtLoad);
2783 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2784 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2785 // Extend SetCC uses if necessary.
2786 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2787 SDNode *SetCC = SetCCs[i];
2788 SmallVector<SDOperand, 4> Ops;
2789 for (unsigned j = 0; j != 2; ++j) {
2790 SDOperand SOp = SetCC->getOperand(j);
2791 if (SOp == Trunc)
2792 Ops.push_back(ExtLoad);
2793 else
2794 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2795 }
2796 Ops.push_back(SetCC->getOperand(2));
2797 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2798 &Ops[0], Ops.size()));
2799 }
2800 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2801 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002802 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002803
2804 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2805 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002806 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2807 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002808 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002809 MVT::ValueType EVT = LN0->getMemoryVT();
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002810 if (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
2811 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2812 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002813 LN0->getSrcValueOffset(), EVT,
2814 LN0->isVolatile(),
2815 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002816 CombineTo(N, ExtLoad);
2817 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2818 ExtLoad.getValue(1));
2819 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2820 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002821 }
2822
Chris Lattner20a35c32007-04-11 05:32:27 +00002823 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2824 if (N0.getOpcode() == ISD::SETCC) {
2825 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002826 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2827 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2828 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2829 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002830 }
2831
Nate Begeman83e75ec2005-09-06 04:43:02 +00002832 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002833}
2834
Nate Begeman83e75ec2005-09-06 04:43:02 +00002835SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002836 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002837 MVT::ValueType VT = N->getValueType(0);
2838
Nate Begeman1d4d4142005-09-01 00:19:25 +00002839 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002840 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002841 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002842 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002843 // fold (zext (aext x)) -> (zext x)
2844 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002845 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002846
Evan Chengc88138f2007-03-22 01:54:19 +00002847 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2848 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002849 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002850 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002851 if (NarrowLoad.Val) {
2852 if (NarrowLoad.Val != N0.Val)
2853 CombineTo(N0.Val, NarrowLoad);
2854 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2855 }
Evan Chengc88138f2007-03-22 01:54:19 +00002856 }
2857
Chris Lattner6007b842006-09-21 06:00:20 +00002858 // fold (zext (truncate x)) -> (and x, mask)
2859 if (N0.getOpcode() == ISD::TRUNCATE &&
2860 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2861 SDOperand Op = N0.getOperand(0);
2862 if (Op.getValueType() < VT) {
2863 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2864 } else if (Op.getValueType() > VT) {
2865 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2866 }
2867 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2868 }
2869
Chris Lattner111c2282006-09-21 06:14:31 +00002870 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2871 if (N0.getOpcode() == ISD::AND &&
2872 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2873 N0.getOperand(1).getOpcode() == ISD::Constant) {
2874 SDOperand X = N0.getOperand(0).getOperand(0);
2875 if (X.getValueType() < VT) {
2876 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2877 } else if (X.getValueType() > VT) {
2878 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2879 }
Dan Gohman220a8232008-03-03 23:51:38 +00002880 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2881 Mask.zext(MVT::getSizeInBits(VT));
Chris Lattner111c2282006-09-21 06:14:31 +00002882 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2883 }
2884
Evan Cheng110dec22005-12-14 02:19:23 +00002885 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002886 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002887 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002888 bool DoXform = true;
2889 SmallVector<SDNode*, 4> SetCCs;
2890 if (!N0.hasOneUse())
2891 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
2892 if (DoXform) {
2893 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2894 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2895 LN0->getBasePtr(), LN0->getSrcValue(),
2896 LN0->getSrcValueOffset(),
2897 N0.getValueType(),
2898 LN0->isVolatile(),
2899 LN0->getAlignment());
2900 CombineTo(N, ExtLoad);
2901 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2902 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2903 // Extend SetCC uses if necessary.
2904 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2905 SDNode *SetCC = SetCCs[i];
2906 SmallVector<SDOperand, 4> Ops;
2907 for (unsigned j = 0; j != 2; ++j) {
2908 SDOperand SOp = SetCC->getOperand(j);
2909 if (SOp == Trunc)
2910 Ops.push_back(ExtLoad);
2911 else
Evan Chengde1631b2007-10-30 20:11:21 +00002912 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002913 }
2914 Ops.push_back(SetCC->getOperand(2));
2915 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2916 &Ops[0], Ops.size()));
2917 }
2918 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2919 }
Evan Cheng110dec22005-12-14 02:19:23 +00002920 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002921
2922 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2923 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002924 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2925 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002926 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002927 MVT::ValueType EVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002928 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2929 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002930 LN0->getSrcValueOffset(), EVT,
2931 LN0->isVolatile(),
2932 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002933 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002934 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2935 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002936 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002937 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002938
2939 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2940 if (N0.getOpcode() == ISD::SETCC) {
2941 SDOperand SCC =
2942 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2943 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00002944 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2945 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002946 }
2947
Nate Begeman83e75ec2005-09-06 04:43:02 +00002948 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002949}
2950
Chris Lattner5ffc0662006-05-05 05:58:59 +00002951SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2952 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002953 MVT::ValueType VT = N->getValueType(0);
2954
2955 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002956 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002957 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2958 // fold (aext (aext x)) -> (aext x)
2959 // fold (aext (zext x)) -> (zext x)
2960 // fold (aext (sext x)) -> (sext x)
2961 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2962 N0.getOpcode() == ISD::ZERO_EXTEND ||
2963 N0.getOpcode() == ISD::SIGN_EXTEND)
2964 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2965
Evan Chengc88138f2007-03-22 01:54:19 +00002966 // fold (aext (truncate (load x))) -> (aext (smaller load x))
2967 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
2968 if (N0.getOpcode() == ISD::TRUNCATE) {
2969 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002970 if (NarrowLoad.Val) {
2971 if (NarrowLoad.Val != N0.Val)
2972 CombineTo(N0.Val, NarrowLoad);
2973 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
2974 }
Evan Chengc88138f2007-03-22 01:54:19 +00002975 }
2976
Chris Lattner84750582006-09-20 06:29:17 +00002977 // fold (aext (truncate x))
2978 if (N0.getOpcode() == ISD::TRUNCATE) {
2979 SDOperand TruncOp = N0.getOperand(0);
2980 if (TruncOp.getValueType() == VT)
2981 return TruncOp; // x iff x size == zext size.
2982 if (TruncOp.getValueType() > VT)
2983 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2984 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2985 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002986
2987 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2988 if (N0.getOpcode() == ISD::AND &&
2989 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2990 N0.getOperand(1).getOpcode() == ISD::Constant) {
2991 SDOperand X = N0.getOperand(0).getOperand(0);
2992 if (X.getValueType() < VT) {
2993 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2994 } else if (X.getValueType() > VT) {
2995 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2996 }
Dan Gohman220a8232008-03-03 23:51:38 +00002997 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2998 Mask.zext(MVT::getSizeInBits(VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00002999 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3000 }
3001
Chris Lattner5ffc0662006-05-05 05:58:59 +00003002 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00003003 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003004 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003005 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3006 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3007 LN0->getBasePtr(), LN0->getSrcValue(),
3008 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003009 N0.getValueType(),
3010 LN0->isVolatile(),
3011 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003012 CombineTo(N, ExtLoad);
3013 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3014 ExtLoad.getValue(1));
3015 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3016 }
3017
3018 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3019 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3020 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003021 if (N0.getOpcode() == ISD::LOAD &&
3022 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003023 N0.hasOneUse()) {
3024 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003025 MVT::ValueType EVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00003026 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
3027 LN0->getChain(), LN0->getBasePtr(),
3028 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003029 LN0->getSrcValueOffset(), EVT,
3030 LN0->isVolatile(),
3031 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003032 CombineTo(N, ExtLoad);
3033 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3034 ExtLoad.getValue(1));
3035 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3036 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003037
3038 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3039 if (N0.getOpcode() == ISD::SETCC) {
3040 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003041 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3042 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003043 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00003044 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003045 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003046 }
3047
Chris Lattner5ffc0662006-05-05 05:58:59 +00003048 return SDOperand();
3049}
3050
Chris Lattner2b4c2792007-10-13 06:35:54 +00003051/// GetDemandedBits - See if the specified operand can be simplified with the
3052/// knowledge that only the bits specified by Mask are used. If so, return the
3053/// simpler operand, otherwise return a null SDOperand.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003054SDOperand DAGCombiner::GetDemandedBits(SDOperand V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003055 switch (V.getOpcode()) {
3056 default: break;
3057 case ISD::OR:
3058 case ISD::XOR:
3059 // If the LHS or RHS don't contribute bits to the or, drop them.
3060 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3061 return V.getOperand(1);
3062 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3063 return V.getOperand(0);
3064 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003065 case ISD::SRL:
3066 // Only look at single-use SRLs.
3067 if (!V.Val->hasOneUse())
3068 break;
3069 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3070 // See if we can recursively simplify the LHS.
3071 unsigned Amt = RHSC->getValue();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003072 APInt NewMask = Mask << Amt;
3073 SDOperand SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Chris Lattnere33544c2007-10-13 06:58:48 +00003074 if (SimplifyLHS.Val) {
3075 return DAG.getNode(ISD::SRL, V.getValueType(),
3076 SimplifyLHS, V.getOperand(1));
3077 }
3078 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003079 }
3080 return SDOperand();
3081}
3082
Evan Chengc88138f2007-03-22 01:54:19 +00003083/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3084/// bits and then truncated to a narrower type and where N is a multiple
3085/// of number of bits of the narrower type, transform it to a narrower load
3086/// from address + N / num of bits of new type. If the result is to be
3087/// extended, also fold the extension to form a extending load.
3088SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
3089 unsigned Opc = N->getOpcode();
3090 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
3091 SDOperand N0 = N->getOperand(0);
3092 MVT::ValueType VT = N->getValueType(0);
3093 MVT::ValueType EVT = N->getValueType(0);
3094
Evan Chenge177e302007-03-23 22:13:36 +00003095 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3096 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003097 if (Opc == ISD::SIGN_EXTEND_INREG) {
3098 ExtType = ISD::SEXTLOAD;
3099 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00003100 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
3101 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00003102 }
3103
3104 unsigned EVTBits = MVT::getSizeInBits(EVT);
3105 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003106 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003107 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3108 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3109 ShAmt = N01->getValue();
3110 // Is the shift amount a multiple of size of VT?
3111 if ((ShAmt & (EVTBits-1)) == 0) {
3112 N0 = N0.getOperand(0);
3113 if (MVT::getSizeInBits(N0.getValueType()) <= EVTBits)
3114 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00003115 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003116 }
3117 }
3118 }
3119
3120 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
3121 // Do not allow folding to i1 here. i1 is implicitly stored in memory in
3122 // zero extended form: by shrinking the load, we lose track of the fact
3123 // that it is already zero extended.
3124 // FIXME: This should be reevaluated.
3125 VT != MVT::i1) {
3126 assert(MVT::getSizeInBits(N0.getValueType()) > EVTBits &&
3127 "Cannot truncate to larger type!");
3128 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3129 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003130 // For big endian targets, we need to adjust the offset to the pointer to
3131 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003132 if (TLI.isBigEndian()) {
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003133 unsigned LVTStoreBits = MVT::getStoreSizeInBits(N0.getValueType());
3134 unsigned EVTStoreBits = MVT::getStoreSizeInBits(EVT);
3135 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3136 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003137 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003138 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Evan Chengc88138f2007-03-22 01:54:19 +00003139 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
3140 DAG.getConstant(PtrOff, PtrType));
3141 AddToWorkList(NewPtr.Val);
3142 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
3143 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003144 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Duncan Sandsdc846502007-10-28 12:59:45 +00003145 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003146 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003147 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00003148 LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003149 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003150 if (CombineSRL) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003151 WorkListRemover DeadNodes(*this);
3152 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3153 &DeadNodes);
Evan Chengb37b80c2007-03-23 20:55:21 +00003154 CombineTo(N->getOperand(0).Val, Load);
3155 } else
3156 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003157 if (ShAmt) {
3158 if (Opc == ISD::SIGN_EXTEND_INREG)
3159 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3160 else
3161 return DAG.getNode(Opc, VT, Load);
3162 }
Evan Chengc88138f2007-03-22 01:54:19 +00003163 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3164 }
3165
3166 return SDOperand();
3167}
3168
Chris Lattner5ffc0662006-05-05 05:58:59 +00003169
Nate Begeman83e75ec2005-09-06 04:43:02 +00003170SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003171 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003172 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003173 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003174 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003175 unsigned VTBits = MVT::getSizeInBits(VT);
Nate Begeman07ed4172005-10-10 21:26:48 +00003176 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003177
Nate Begeman1d4d4142005-09-01 00:19:25 +00003178 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003179 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003180 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003181
Chris Lattner541a24f2006-05-06 22:43:44 +00003182 // If the input is already sign extended, just drop the extension.
Dan Gohmanea859be2007-06-22 14:59:07 +00003183 if (DAG.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003184 return N0;
3185
Nate Begeman646d7e22005-09-02 21:18:40 +00003186 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3187 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3188 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003189 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003190 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003191
Chris Lattner95a5e052007-04-17 19:03:21 +00003192 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003193 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003194 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003195
Chris Lattner95a5e052007-04-17 19:03:21 +00003196 // fold operands of sext_in_reg based on knowledge that the top bits are not
3197 // demanded.
3198 if (SimplifyDemandedBits(SDOperand(N, 0)))
3199 return SDOperand(N, 0);
3200
Evan Chengc88138f2007-03-22 01:54:19 +00003201 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3202 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
3203 SDOperand NarrowLoad = ReduceLoadWidth(N);
3204 if (NarrowLoad.Val)
3205 return NarrowLoad;
3206
Chris Lattner4b37e872006-05-08 21:18:59 +00003207 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3208 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3209 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3210 if (N0.getOpcode() == ISD::SRL) {
3211 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
3212 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
3213 // We can turn this into an SRA iff the input to the SRL is already sign
3214 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003215 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Chris Lattner4b37e872006-05-08 21:18:59 +00003216 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
3217 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3218 }
3219 }
Evan Chengc88138f2007-03-22 01:54:19 +00003220
Nate Begemanded49632005-10-13 03:11:28 +00003221 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00003222 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003223 ISD::isUNINDEXEDLoad(N0.Val) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003224 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003225 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003226 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3227 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3228 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003229 LN0->getSrcValueOffset(), EVT,
3230 LN0->isVolatile(),
3231 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003232 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003233 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003234 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003235 }
3236 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00003237 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
3238 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003239 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003240 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003241 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3242 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3243 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003244 LN0->getSrcValueOffset(), EVT,
3245 LN0->isVolatile(),
3246 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003247 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003248 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003249 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003250 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003251 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003252}
3253
Nate Begeman83e75ec2005-09-06 04:43:02 +00003254SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003255 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003256 MVT::ValueType VT = N->getValueType(0);
3257
3258 // noop truncate
3259 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003260 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003261 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003262 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003263 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003264 // fold (truncate (truncate x)) -> (truncate x)
3265 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003266 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003267 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003268 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3269 N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003270 if (N0.getOperand(0).getValueType() < VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003271 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003272 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003273 else if (N0.getOperand(0).getValueType() > VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003274 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003275 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003276 else
3277 // if the source and dest are the same type, we can drop both the extend
3278 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003279 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003280 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003281
Chris Lattner2b4c2792007-10-13 06:35:54 +00003282 // See if we can simplify the input to this truncate through knowledge that
3283 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3284 // -> trunc y
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003285 SDOperand Shorter =
3286 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
3287 MVT::getSizeInBits(VT)));
Chris Lattner2b4c2792007-10-13 06:35:54 +00003288 if (Shorter.Val)
3289 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3290
Nate Begeman3df4d522005-10-12 20:40:40 +00003291 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003292 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003293 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003294}
3295
Chris Lattner94683772005-12-23 05:30:37 +00003296SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3297 SDOperand N0 = N->getOperand(0);
3298 MVT::ValueType VT = N->getValueType(0);
3299
Dan Gohman7f321562007-06-25 16:23:39 +00003300 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3301 // Only do this before legalize, since afterward the target may be depending
3302 // on the bitconvert.
3303 // First check to see if this is all constant.
3304 if (!AfterLegalize &&
3305 N0.getOpcode() == ISD::BUILD_VECTOR && N0.Val->hasOneUse() &&
3306 MVT::isVector(VT)) {
3307 bool isSimple = true;
3308 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3309 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3310 N0.getOperand(i).getOpcode() != ISD::Constant &&
3311 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3312 isSimple = false;
3313 break;
3314 }
3315
3316 MVT::ValueType DestEltVT = MVT::getVectorElementType(N->getValueType(0));
3317 assert(!MVT::isVector(DestEltVT) &&
3318 "Element type of vector ValueType must not be vector!");
3319 if (isSimple) {
3320 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.Val, DestEltVT);
3321 }
3322 }
3323
Chris Lattner94683772005-12-23 05:30:37 +00003324 // If the input is a constant, let getNode() fold it.
3325 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
3326 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3327 if (Res.Val != N) return Res;
3328 }
3329
Chris Lattnerc8547d82005-12-23 05:37:50 +00003330 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3331 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003332
Chris Lattner57104102005-12-23 05:44:41 +00003333 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003334 // If the resultant load doesn't need a higher alignment than the original!
3335 if (ISD::isNormalLoad(N0.Val) && N0.hasOneUse() &&
Evan Cheng59d5b682007-05-07 21:27:48 +00003336 TLI.isOperationLegal(ISD::LOAD, VT)) {
Evan Cheng466685d2006-10-09 20:57:25 +00003337 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00003338 unsigned Align = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00003339 getABITypeAlignment(MVT::getTypeForValueType(VT));
Evan Cheng59d5b682007-05-07 21:27:48 +00003340 unsigned OrigAlign = LN0->getAlignment();
3341 if (Align <= OrigAlign) {
3342 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
3343 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003344 LN0->isVolatile(), Align);
Evan Cheng59d5b682007-05-07 21:27:48 +00003345 AddToWorkList(N);
3346 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
3347 Load.getValue(1));
3348 return Load;
3349 }
Chris Lattner57104102005-12-23 05:44:41 +00003350 }
3351
Chris Lattner3bd39d42008-01-27 17:42:27 +00003352 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3353 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3354 // This often reduces constant pool loads.
3355 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
3356 N0.Val->hasOneUse() && MVT::isInteger(VT) && !MVT::isVector(VT)) {
3357 SDOperand NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3358 AddToWorkList(NewConv.Val);
3359
Dan Gohman220a8232008-03-03 23:51:38 +00003360 APInt SignBit = APInt::getSignBit(MVT::getSizeInBits(VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00003361 if (N0.getOpcode() == ISD::FNEG)
3362 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3363 assert(N0.getOpcode() == ISD::FABS);
3364 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3365 }
3366
3367 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3368 // Note that we don't handle copysign(x,cst) because this can always be folded
3369 // to an fneg or fabs.
3370 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003371 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
3372 MVT::isInteger(VT) && !MVT::isVector(VT)) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003373 unsigned OrigXWidth = MVT::getSizeInBits(N0.getOperand(1).getValueType());
3374 SDOperand X = DAG.getNode(ISD::BIT_CONVERT, MVT::getIntegerType(OrigXWidth),
3375 N0.getOperand(1));
3376 AddToWorkList(X.Val);
3377
3378 // If X has a different width than the result/lhs, sext it or truncate it.
3379 unsigned VTWidth = MVT::getSizeInBits(VT);
3380 if (OrigXWidth < VTWidth) {
3381 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
3382 AddToWorkList(X.Val);
3383 } else if (OrigXWidth > VTWidth) {
3384 // To get the sign bit in the right place, we have to shift it right
3385 // before truncating.
3386 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3387 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3388 AddToWorkList(X.Val);
3389 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3390 AddToWorkList(X.Val);
3391 }
3392
Dan Gohman220a8232008-03-03 23:51:38 +00003393 APInt SignBit = APInt::getSignBit(MVT::getSizeInBits(VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00003394 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
3395 AddToWorkList(X.Val);
3396
3397 SDOperand Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3398 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
3399 AddToWorkList(Cst.Val);
3400
3401 return DAG.getNode(ISD::OR, VT, X, Cst);
3402 }
3403
Chris Lattner94683772005-12-23 05:30:37 +00003404 return SDOperand();
3405}
3406
Dan Gohman7f321562007-06-25 16:23:39 +00003407/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003408/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3409/// destination element value type.
3410SDOperand DAGCombiner::
Dan Gohman7f321562007-06-25 16:23:39 +00003411ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003412 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
3413
3414 // If this is already the right type, we're done.
3415 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
3416
3417 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
3418 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
3419
3420 // If this is a conversion of N elements of one type to N elements of another
3421 // type, convert each element. This handles FP<->INT cases.
3422 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003423 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003424 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003425 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00003426 AddToWorkList(Ops.back().Val);
3427 }
Dan Gohman7f321562007-06-25 16:23:39 +00003428 MVT::ValueType VT =
3429 MVT::getVectorType(DstEltVT,
3430 MVT::getVectorNumElements(BV->getValueType(0)));
3431 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003432 }
3433
3434 // Otherwise, we're growing or shrinking the elements. To avoid having to
3435 // handle annoying details of growing/shrinking FP values, we convert them to
3436 // int first.
3437 if (MVT::isFloatingPoint(SrcEltVT)) {
3438 // Convert the input float vector to a int vector where the elements are the
3439 // same sizes.
3440 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
3441 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003442 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003443 SrcEltVT = IntVT;
3444 }
3445
3446 // Now we know the input is an integer vector. If the output is a FP type,
3447 // convert to integer first, then to FP of the right size.
3448 if (MVT::isFloatingPoint(DstEltVT)) {
3449 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
3450 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003451 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003452
3453 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003454 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003455 }
3456
3457 // Okay, we know the src/dst types are both integers of differing types.
3458 // Handling growing first.
3459 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
3460 if (SrcBitSize < DstBitSize) {
3461 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3462
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003463 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003464 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003465 i += NumInputsPerOutput) {
3466 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003467 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003468 bool EltIsUndef = true;
3469 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3470 // Shift the previously computed bits over.
3471 NewBits <<= SrcBitSize;
3472 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
3473 if (Op.getOpcode() == ISD::UNDEF) continue;
3474 EltIsUndef = false;
3475
Dan Gohman220a8232008-03-03 23:51:38 +00003476 NewBits |=
3477 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003478 }
3479
3480 if (EltIsUndef)
3481 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3482 else
3483 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3484 }
3485
Evan Chengefec7512008-02-18 23:04:32 +00003486 MVT::ValueType VT = MVT::getVectorType(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003487 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003488 }
3489
3490 // Finally, this must be the case where we are shrinking elements: each input
3491 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003492 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003493 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Evan Chengefec7512008-02-18 23:04:32 +00003494 MVT::ValueType VT = MVT::getVectorType(DstEltVT,
3495 NumOutputsPerInput * BV->getNumOperands());
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003496 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003497 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003498 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3499 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3500 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3501 continue;
3502 }
Dan Gohman220a8232008-03-03 23:51:38 +00003503 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003504 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003505 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003506 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003507 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003508 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3509 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003510 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003511 }
3512
3513 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003514 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003515 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3516 }
Dan Gohman7f321562007-06-25 16:23:39 +00003517 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003518}
3519
3520
3521
Chris Lattner01b3d732005-09-28 22:28:18 +00003522SDOperand DAGCombiner::visitFADD(SDNode *N) {
3523 SDOperand N0 = N->getOperand(0);
3524 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003525 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3526 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003527 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003528
Dan Gohman7f321562007-06-25 16:23:39 +00003529 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003530 if (MVT::isVector(VT)) {
3531 SDOperand FoldedVOp = SimplifyVBinOp(N);
3532 if (FoldedVOp.Val) return FoldedVOp;
3533 }
Dan Gohman7f321562007-06-25 16:23:39 +00003534
Nate Begemana0e221d2005-10-18 00:28:13 +00003535 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003536 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003537 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003538 // canonicalize constant to RHS
3539 if (N0CFP && !N1CFP)
3540 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003541 // fold (A + (-B)) -> A-B
Chris Lattner0254e702008-02-26 07:04:54 +00003542 if (isNegatibleForFree(N1, AfterLegalize) == 2)
3543 return DAG.getNode(ISD::FSUB, VT, N0,
3544 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner01b3d732005-09-28 22:28:18 +00003545 // fold ((-A) + B) -> B-A
Chris Lattner0254e702008-02-26 07:04:54 +00003546 if (isNegatibleForFree(N0, AfterLegalize) == 2)
3547 return DAG.getNode(ISD::FSUB, VT, N1,
3548 GetNegatedExpression(N0, DAG, AfterLegalize));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003549
3550 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3551 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
3552 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3553 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3554 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3555
Chris Lattner01b3d732005-09-28 22:28:18 +00003556 return SDOperand();
3557}
3558
3559SDOperand DAGCombiner::visitFSUB(SDNode *N) {
3560 SDOperand N0 = N->getOperand(0);
3561 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003562 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3563 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003564 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003565
Dan Gohman7f321562007-06-25 16:23:39 +00003566 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003567 if (MVT::isVector(VT)) {
3568 SDOperand FoldedVOp = SimplifyVBinOp(N);
3569 if (FoldedVOp.Val) return FoldedVOp;
3570 }
Dan Gohman7f321562007-06-25 16:23:39 +00003571
Nate Begemana0e221d2005-10-18 00:28:13 +00003572 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003573 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003574 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003575 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003576 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Chris Lattner0254e702008-02-26 07:04:54 +00003577 if (isNegatibleForFree(N1, AfterLegalize))
3578 return GetNegatedExpression(N1, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003579 return DAG.getNode(ISD::FNEG, VT, N1);
3580 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003581 // fold (A-(-B)) -> A+B
Chris Lattner0254e702008-02-26 07:04:54 +00003582 if (isNegatibleForFree(N1, AfterLegalize))
3583 return DAG.getNode(ISD::FADD, VT, N0,
3584 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003585
Chris Lattner01b3d732005-09-28 22:28:18 +00003586 return SDOperand();
3587}
3588
3589SDOperand DAGCombiner::visitFMUL(SDNode *N) {
3590 SDOperand N0 = N->getOperand(0);
3591 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003592 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3593 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003594 MVT::ValueType VT = N->getValueType(0);
3595
Dan Gohman7f321562007-06-25 16:23:39 +00003596 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003597 if (MVT::isVector(VT)) {
3598 SDOperand FoldedVOp = SimplifyVBinOp(N);
3599 if (FoldedVOp.Val) return FoldedVOp;
3600 }
Dan Gohman7f321562007-06-25 16:23:39 +00003601
Nate Begeman11af4ea2005-10-17 20:40:11 +00003602 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003603 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003604 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003605 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003606 if (N0CFP && !N1CFP)
3607 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003608 // fold (fmul X, 2.0) -> (fadd X, X)
3609 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3610 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003611 // fold (fmul X, -1.0) -> (fneg X)
3612 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3613 return DAG.getNode(ISD::FNEG, VT, N0);
3614
3615 // -X * -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003616 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3617 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003618 // Both can be negated for free, check to see if at least one is cheaper
3619 // negated.
3620 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003621 return DAG.getNode(ISD::FMUL, VT,
3622 GetNegatedExpression(N0, DAG, AfterLegalize),
3623 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003624 }
3625 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003626
3627 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3628 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
3629 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3630 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3631 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3632
Chris Lattner01b3d732005-09-28 22:28:18 +00003633 return SDOperand();
3634}
3635
3636SDOperand DAGCombiner::visitFDIV(SDNode *N) {
3637 SDOperand N0 = N->getOperand(0);
3638 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003639 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3640 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003641 MVT::ValueType VT = N->getValueType(0);
3642
Dan Gohman7f321562007-06-25 16:23:39 +00003643 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003644 if (MVT::isVector(VT)) {
3645 SDOperand FoldedVOp = SimplifyVBinOp(N);
3646 if (FoldedVOp.Val) return FoldedVOp;
3647 }
Dan Gohman7f321562007-06-25 16:23:39 +00003648
Nate Begemana148d982006-01-18 22:35:16 +00003649 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003650 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003651 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003652
3653
3654 // -X / -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003655 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3656 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003657 // Both can be negated for free, check to see if at least one is cheaper
3658 // negated.
3659 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003660 return DAG.getNode(ISD::FDIV, VT,
3661 GetNegatedExpression(N0, DAG, AfterLegalize),
3662 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003663 }
3664 }
3665
Chris Lattner01b3d732005-09-28 22:28:18 +00003666 return SDOperand();
3667}
3668
3669SDOperand DAGCombiner::visitFREM(SDNode *N) {
3670 SDOperand N0 = N->getOperand(0);
3671 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003672 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3673 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003674 MVT::ValueType VT = N->getValueType(0);
3675
Nate Begemana148d982006-01-18 22:35:16 +00003676 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003677 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003678 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003679
Chris Lattner01b3d732005-09-28 22:28:18 +00003680 return SDOperand();
3681}
3682
Chris Lattner12d83032006-03-05 05:30:57 +00003683SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3684 SDOperand N0 = N->getOperand(0);
3685 SDOperand N1 = N->getOperand(1);
3686 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3687 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3688 MVT::ValueType VT = N->getValueType(0);
3689
Dale Johannesendb44bf82007-10-16 23:38:29 +00003690 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003691 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3692
3693 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003694 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003695 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3696 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003697 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003698 return DAG.getNode(ISD::FABS, VT, N0);
3699 else
3700 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3701 }
3702
3703 // copysign(fabs(x), y) -> copysign(x, y)
3704 // copysign(fneg(x), y) -> copysign(x, y)
3705 // copysign(copysign(x,z), y) -> copysign(x, y)
3706 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3707 N0.getOpcode() == ISD::FCOPYSIGN)
3708 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3709
3710 // copysign(x, abs(y)) -> abs(x)
3711 if (N1.getOpcode() == ISD::FABS)
3712 return DAG.getNode(ISD::FABS, VT, N0);
3713
3714 // copysign(x, copysign(y,z)) -> copysign(x, z)
3715 if (N1.getOpcode() == ISD::FCOPYSIGN)
3716 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3717
3718 // copysign(x, fp_extend(y)) -> copysign(x, y)
3719 // copysign(x, fp_round(y)) -> copysign(x, y)
3720 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3721 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3722
3723 return SDOperand();
3724}
3725
3726
Chris Lattner01b3d732005-09-28 22:28:18 +00003727
Nate Begeman83e75ec2005-09-06 04:43:02 +00003728SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003729 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003730 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003731 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003732
3733 // fold (sint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003734 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003735 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003736 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003737}
3738
Nate Begeman83e75ec2005-09-06 04:43:02 +00003739SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003740 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003741 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003742 MVT::ValueType VT = N->getValueType(0);
3743
Nate Begeman1d4d4142005-09-01 00:19:25 +00003744 // fold (uint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003745 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003746 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003747 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003748}
3749
Nate Begeman83e75ec2005-09-06 04:43:02 +00003750SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003751 SDOperand N0 = N->getOperand(0);
3752 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3753 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003754
3755 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003756 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003757 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003758 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003759}
3760
Nate Begeman83e75ec2005-09-06 04:43:02 +00003761SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003762 SDOperand N0 = N->getOperand(0);
3763 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3764 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003765
3766 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00003767 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003768 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003769 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003770}
3771
Nate Begeman83e75ec2005-09-06 04:43:02 +00003772SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003773 SDOperand N0 = N->getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003774 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003775 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3776 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003777
3778 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003779 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00003780 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003781
3782 // fold (fp_round (fp_extend x)) -> x
3783 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3784 return N0.getOperand(0);
3785
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00003786 // fold (fp_round (fp_round x)) -> (fp_round x)
3787 if (N0.getOpcode() == ISD::FP_ROUND) {
3788 // This is a value preserving truncation if both round's are.
3789 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
3790 N0.Val->getConstantOperandVal(1) == 1;
3791 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
3792 DAG.getIntPtrConstant(IsTrunc));
3793 }
3794
Chris Lattner79dbea52006-03-13 06:26:26 +00003795 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3796 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003797 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003798 AddToWorkList(Tmp.Val);
3799 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3800 }
3801
Nate Begeman83e75ec2005-09-06 04:43:02 +00003802 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003803}
3804
Nate Begeman83e75ec2005-09-06 04:43:02 +00003805SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003806 SDOperand N0 = N->getOperand(0);
3807 MVT::ValueType VT = N->getValueType(0);
3808 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003809 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003810
Nate Begeman1d4d4142005-09-01 00:19:25 +00003811 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003812 if (N0CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003813 SDOperand Round = DAG.getConstantFP(N0CFP->getValueAPF(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003814 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003815 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003816 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003817}
3818
Nate Begeman83e75ec2005-09-06 04:43:02 +00003819SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003820 SDOperand N0 = N->getOperand(0);
3821 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3822 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003823
Chris Lattner5938bef2007-12-29 06:55:23 +00003824 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
3825 if (N->hasOneUse() && (*N->use_begin())->getOpcode() == ISD::FP_ROUND)
3826 return SDOperand();
Chris Lattner0bd48932008-01-17 07:00:52 +00003827
Nate Begeman1d4d4142005-09-01 00:19:25 +00003828 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003829 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003830 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003831
3832 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
3833 // value of X.
3834 if (N0.getOpcode() == ISD::FP_ROUND && N0.Val->getConstantOperandVal(1) == 1){
3835 SDOperand In = N0.getOperand(0);
3836 if (In.getValueType() == VT) return In;
3837 if (VT < In.getValueType())
3838 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
3839 return DAG.getNode(ISD::FP_EXTEND, VT, In);
3840 }
3841
3842 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Dale Johannesenb6210fc2007-10-19 20:29:00 +00003843 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003844 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003845 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3846 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3847 LN0->getBasePtr(), LN0->getSrcValue(),
3848 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003849 N0.getValueType(),
3850 LN0->isVolatile(),
3851 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003852 CombineTo(N, ExtLoad);
Chris Lattner0bd48932008-01-17 07:00:52 +00003853 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad,
3854 DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00003855 ExtLoad.getValue(1));
3856 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3857 }
3858
3859
Nate Begeman83e75ec2005-09-06 04:43:02 +00003860 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003861}
3862
Nate Begeman83e75ec2005-09-06 04:43:02 +00003863SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003864 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003865
Chris Lattner0254e702008-02-26 07:04:54 +00003866 if (isNegatibleForFree(N0, AfterLegalize))
3867 return GetNegatedExpression(N0, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003868
Chris Lattner3bd39d42008-01-27 17:42:27 +00003869 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
3870 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00003871 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
3872 MVT::isInteger(N0.getOperand(0).getValueType()) &&
3873 !MVT::isVector(N0.getOperand(0).getValueType())) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003874 SDOperand Int = N0.getOperand(0);
3875 MVT::ValueType IntVT = Int.getValueType();
3876 if (MVT::isInteger(IntVT) && !MVT::isVector(IntVT)) {
3877 Int = DAG.getNode(ISD::XOR, IntVT, Int,
3878 DAG.getConstant(MVT::getIntVTSignBit(IntVT), IntVT));
3879 AddToWorkList(Int.Val);
3880 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
3881 }
3882 }
3883
Nate Begeman83e75ec2005-09-06 04:43:02 +00003884 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003885}
3886
Nate Begeman83e75ec2005-09-06 04:43:02 +00003887SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003888 SDOperand N0 = N->getOperand(0);
3889 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3890 MVT::ValueType VT = N->getValueType(0);
3891
Nate Begeman1d4d4142005-09-01 00:19:25 +00003892 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003893 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003894 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003895 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003896 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003897 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003898 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003899 // fold (fabs (fcopysign x, y)) -> (fabs x)
3900 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
3901 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
3902
Chris Lattner3bd39d42008-01-27 17:42:27 +00003903 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
3904 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00003905 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
3906 MVT::isInteger(N0.getOperand(0).getValueType()) &&
3907 !MVT::isVector(N0.getOperand(0).getValueType())) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003908 SDOperand Int = N0.getOperand(0);
3909 MVT::ValueType IntVT = Int.getValueType();
3910 if (MVT::isInteger(IntVT) && !MVT::isVector(IntVT)) {
3911 Int = DAG.getNode(ISD::AND, IntVT, Int,
3912 DAG.getConstant(~MVT::getIntVTSignBit(IntVT), IntVT));
3913 AddToWorkList(Int.Val);
3914 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
3915 }
3916 }
3917
Nate Begeman83e75ec2005-09-06 04:43:02 +00003918 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003919}
3920
Nate Begeman44728a72005-09-19 22:34:01 +00003921SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
3922 SDOperand Chain = N->getOperand(0);
3923 SDOperand N1 = N->getOperand(1);
3924 SDOperand N2 = N->getOperand(2);
3925 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3926
3927 // never taken branch, fold to chain
3928 if (N1C && N1C->isNullValue())
3929 return Chain;
3930 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00003931 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00003932 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003933 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
3934 // on the target.
3935 if (N1.getOpcode() == ISD::SETCC &&
3936 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
3937 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
3938 N1.getOperand(0), N1.getOperand(1), N2);
3939 }
Nate Begeman44728a72005-09-19 22:34:01 +00003940 return SDOperand();
3941}
3942
Chris Lattner3ea0b472005-10-05 06:47:48 +00003943// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
3944//
Nate Begeman44728a72005-09-19 22:34:01 +00003945SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00003946 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
3947 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
3948
3949 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00003950 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003951 if (Simp.Val) AddToWorkList(Simp.Val);
3952
Nate Begemane17daeb2005-10-05 21:43:42 +00003953 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
3954
3955 // fold br_cc true, dest -> br dest (unconditional branch)
3956 if (SCCC && SCCC->getValue())
3957 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
3958 N->getOperand(4));
3959 // fold br_cc false, dest -> unconditional fall through
3960 if (SCCC && SCCC->isNullValue())
3961 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00003962
Nate Begemane17daeb2005-10-05 21:43:42 +00003963 // fold to a simpler setcc
3964 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
3965 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
3966 Simp.getOperand(2), Simp.getOperand(0),
3967 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00003968 return SDOperand();
3969}
3970
Chris Lattner448f2192006-11-11 00:39:41 +00003971
3972/// CombineToPreIndexedLoadStore - Try turning a load / store and a
3973/// pre-indexed load / store when the base pointer is a add or subtract
3974/// and it has other uses besides the load / store. After the
3975/// transformation, the new indexed load / store has effectively folded
3976/// the add / subtract in and all of its other uses are redirected to the
3977/// new load / store.
3978bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
3979 if (!AfterLegalize)
3980 return false;
3981
3982 bool isLoad = true;
3983 SDOperand Ptr;
3984 MVT::ValueType VT;
3985 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00003986 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00003987 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003988 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00003989 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00003990 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
3991 return false;
3992 Ptr = LD->getBasePtr();
3993 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00003994 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00003995 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003996 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00003997 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
3998 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
3999 return false;
4000 Ptr = ST->getBasePtr();
4001 isLoad = false;
4002 } else
4003 return false;
4004
Chris Lattner9f1794e2006-11-11 00:56:29 +00004005 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4006 // out. There is no reason to make this a preinc/predec.
4007 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
4008 Ptr.Val->hasOneUse())
4009 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004010
Chris Lattner9f1794e2006-11-11 00:56:29 +00004011 // Ask the target to do addressing mode selection.
4012 SDOperand BasePtr;
4013 SDOperand Offset;
4014 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4015 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4016 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004017 // Don't create a indexed load / store with zero offset.
4018 if (isa<ConstantSDNode>(Offset) &&
4019 cast<ConstantSDNode>(Offset)->getValue() == 0)
4020 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004021
Chris Lattner41e53fd2006-11-11 01:00:15 +00004022 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004023 // 1) The new base ptr is a frame index.
4024 // 2) If N is a store and the new base ptr is either the same as or is a
Chris Lattner9f1794e2006-11-11 00:56:29 +00004025 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004026 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004027 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004028 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004029
Chris Lattner41e53fd2006-11-11 01:00:15 +00004030 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4031 // (plus the implicit offset) to a register to preinc anyway.
4032 if (isa<FrameIndexSDNode>(BasePtr))
4033 return false;
4034
4035 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004036 if (!isLoad) {
4037 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Cheng917be682008-03-04 00:41:45 +00004038 if (Val == BasePtr || BasePtr.Val->isPredecessorOf(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004039 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004040 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004041
Evan Chengc843abe2007-05-24 02:35:39 +00004042 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004043 bool RealUse = false;
4044 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4045 E = Ptr.Val->use_end(); I != E; ++I) {
4046 SDNode *Use = *I;
4047 if (Use == N)
4048 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004049 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004050 return false;
4051
4052 if (!((Use->getOpcode() == ISD::LOAD &&
4053 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004054 (Use->getOpcode() == ISD::STORE &&
4055 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004056 RealUse = true;
4057 }
4058 if (!RealUse)
4059 return false;
4060
4061 SDOperand Result;
4062 if (isLoad)
4063 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
4064 else
4065 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4066 ++PreIndexedNodes;
4067 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004068 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004069 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4070 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004071 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004072 if (isLoad) {
4073 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004074 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004075 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004076 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004077 } else {
4078 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004079 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004080 }
4081
Chris Lattner9f1794e2006-11-11 00:56:29 +00004082 // Finally, since the node is now dead, remove it from the graph.
4083 DAG.DeleteNode(N);
4084
4085 // Replace the uses of Ptr with uses of the updated base value.
4086 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004087 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004088 removeFromWorkList(Ptr.Val);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004089 DAG.DeleteNode(Ptr.Val);
4090
4091 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004092}
4093
4094/// CombineToPostIndexedLoadStore - Try combine a load / store with a
4095/// add / sub of the base pointer node into a post-indexed load / store.
4096/// The transformation folded the add / subtract into the new indexed
4097/// load / store effectively and all of its uses are redirected to the
4098/// new load / store.
4099bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
4100 if (!AfterLegalize)
4101 return false;
4102
4103 bool isLoad = true;
4104 SDOperand Ptr;
4105 MVT::ValueType VT;
4106 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004107 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004108 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004109 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004110 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4111 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4112 return false;
4113 Ptr = LD->getBasePtr();
4114 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004115 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004116 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004117 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004118 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4119 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4120 return false;
4121 Ptr = ST->getBasePtr();
4122 isLoad = false;
4123 } else
4124 return false;
4125
Evan Chengcc470212006-11-16 00:08:20 +00004126 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004127 return false;
4128
4129 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4130 E = Ptr.Val->use_end(); I != E; ++I) {
4131 SDNode *Op = *I;
4132 if (Op == N ||
4133 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4134 continue;
4135
4136 SDOperand BasePtr;
4137 SDOperand Offset;
4138 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4139 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4140 if (Ptr == Offset)
4141 std::swap(BasePtr, Offset);
4142 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004143 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004144 // Don't create a indexed load / store with zero offset.
4145 if (isa<ConstantSDNode>(Offset) &&
4146 cast<ConstantSDNode>(Offset)->getValue() == 0)
4147 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004148
Chris Lattner9f1794e2006-11-11 00:56:29 +00004149 // Try turning it into a post-indexed load / store except when
4150 // 1) All uses are load / store ops that use it as base ptr.
4151 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4152 // nor a successor of N. Otherwise, if Op is folded that would
4153 // create a cycle.
4154
4155 // Check for #1.
4156 bool TryNext = false;
4157 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
4158 EE = BasePtr.Val->use_end(); II != EE; ++II) {
4159 SDNode *Use = *II;
4160 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00004161 continue;
4162
Chris Lattner9f1794e2006-11-11 00:56:29 +00004163 // If all the uses are load / store addresses, then don't do the
4164 // transformation.
4165 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4166 bool RealUse = false;
4167 for (SDNode::use_iterator III = Use->use_begin(),
4168 EEE = Use->use_end(); III != EEE; ++III) {
4169 SDNode *UseUse = *III;
4170 if (!((UseUse->getOpcode() == ISD::LOAD &&
4171 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004172 (UseUse->getOpcode() == ISD::STORE &&
4173 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004174 RealUse = true;
4175 }
Chris Lattner448f2192006-11-11 00:39:41 +00004176
Chris Lattner9f1794e2006-11-11 00:56:29 +00004177 if (!RealUse) {
4178 TryNext = true;
4179 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004180 }
4181 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004182 }
4183 if (TryNext)
4184 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004185
Chris Lattner9f1794e2006-11-11 00:56:29 +00004186 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004187 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Chris Lattner9f1794e2006-11-11 00:56:29 +00004188 SDOperand Result = isLoad
4189 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
4190 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4191 ++PostIndexedNodes;
4192 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004193 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004194 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4195 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004196 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004197 if (isLoad) {
4198 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004199 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004200 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004201 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004202 } else {
4203 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004204 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004205 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004206
Chris Lattner9f1794e2006-11-11 00:56:29 +00004207 // Finally, since the node is now dead, remove it from the graph.
4208 DAG.DeleteNode(N);
4209
4210 // Replace the uses of Use with uses of the updated base value.
4211 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
4212 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004213 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004214 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004215 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004216 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004217 }
4218 }
4219 }
4220 return false;
4221}
4222
Chris Lattner00161a62008-01-25 07:20:16 +00004223/// InferAlignment - If we can infer some alignment information from this
4224/// pointer, return it.
4225static unsigned InferAlignment(SDOperand Ptr, SelectionDAG &DAG) {
4226 // If this is a direct reference to a stack slot, use information about the
4227 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004228 int FrameIdx = 1 << 31;
4229 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004230 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004231 FrameIdx = FI->getIndex();
4232 } else if (Ptr.getOpcode() == ISD::ADD &&
4233 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4234 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4235 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4236 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004237 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004238
4239 if (FrameIdx != (1 << 31)) {
4240 // FIXME: Handle FI+CST.
4241 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4242 if (MFI.isFixedObjectIndex(FrameIdx)) {
4243 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx);
4244
4245 // The alignment of the frame index can be determined from its offset from
4246 // the incoming frame position. If the frame object is at offset 32 and
4247 // the stack is guaranteed to be 16-byte aligned, then we know that the
4248 // object is 16-byte aligned.
4249 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4250 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4251
4252 // Finally, the frame object itself may have a known alignment. Factor
4253 // the alignment + offset into a new alignment. For example, if we know
4254 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4255 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4256 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4257 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4258 FrameOffset);
4259 return std::max(Align, FIInfoAlign);
4260 }
4261 }
Chris Lattner00161a62008-01-25 07:20:16 +00004262
4263 return 0;
4264}
Chris Lattner448f2192006-11-11 00:39:41 +00004265
Chris Lattner01a22022005-10-10 22:04:48 +00004266SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004267 LoadSDNode *LD = cast<LoadSDNode>(N);
4268 SDOperand Chain = LD->getChain();
4269 SDOperand Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004270
4271 // Try to infer better alignment information than the load already has.
4272 if (LD->isUnindexed()) {
4273 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4274 if (Align > LD->getAlignment())
4275 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4276 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004277 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004278 LD->isVolatile(), Align);
4279 }
4280 }
4281
Evan Cheng45a7ca92007-05-01 00:38:21 +00004282
4283 // If load is not volatile and there are no uses of the loaded value (and
4284 // the updated indexed value in case of indexed loads), change uses of the
4285 // chain value into uses of the chain input (i.e. delete the dead load).
4286 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004287 if (N->getValueType(1) == MVT::Other) {
4288 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004289 if (N->hasNUsesOfValue(0, 0)) {
4290 // It's not safe to use the two value CombineTo variant here. e.g.
4291 // v1, chain2 = load chain1, loc
4292 // v2, chain3 = load chain2, loc
4293 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004294 // Now we replace use of chain2 with chain1. This makes the second load
4295 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004296 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004297 DOUT << "\nWith chain: "; DEBUG(Chain.Val->dump(&DAG));
4298 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004299 WorkListRemover DeadNodes(*this);
4300 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004301 if (N->use_empty()) {
4302 removeFromWorkList(N);
4303 DAG.DeleteNode(N);
4304 }
Evan Cheng02c42852008-01-16 23:11:54 +00004305 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
4306 }
Evan Cheng498f5592007-05-01 08:53:39 +00004307 } else {
4308 // Indexed loads.
4309 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4310 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Evan Cheng02c42852008-01-16 23:11:54 +00004311 SDOperand Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
4312 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
4313 DOUT << "\nWith: "; DEBUG(Undef.Val->dump(&DAG));
4314 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004315 WorkListRemover DeadNodes(*this);
4316 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Undef, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004317 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004318 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004319 &DeadNodes);
4320 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004321 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004322 DAG.DeleteNode(N);
4323 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004324 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004325 }
4326 }
Chris Lattner01a22022005-10-10 22:04:48 +00004327
4328 // If this load is directly stored, replace the load value with the stored
4329 // value.
4330 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004331 // TODO: Handle TRUNCSTORE/LOADEXT
4332 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004333 if (ISD::isNON_TRUNCStore(Chain.Val)) {
4334 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4335 if (PrevST->getBasePtr() == Ptr &&
4336 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004337 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004338 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004339 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004340
Jim Laskey7ca56af2006-10-11 13:47:09 +00004341 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004342 // Walk up chain skipping non-aliasing memory nodes.
4343 SDOperand BetterChain = FindBetterChain(N, Chain);
4344
Jim Laskey6ff23e52006-10-04 16:53:27 +00004345 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004346 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004347 SDOperand ReplLoad;
4348
Jim Laskey279f0532006-09-25 16:29:54 +00004349 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004350 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4351 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004352 LD->getSrcValue(), LD->getSrcValueOffset(),
4353 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004354 } else {
4355 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4356 LD->getValueType(0),
4357 BetterChain, Ptr, LD->getSrcValue(),
4358 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004359 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004360 LD->isVolatile(),
4361 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004362 }
Jim Laskey279f0532006-09-25 16:29:54 +00004363
Jim Laskey6ff23e52006-10-04 16:53:27 +00004364 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00004365 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
4366 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004367
Jim Laskey274062c2006-10-13 23:32:28 +00004368 // Replace uses with load result and token factor. Don't add users
4369 // to work list.
4370 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004371 }
4372 }
4373
Evan Cheng7fc033a2006-11-03 03:06:21 +00004374 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004375 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00004376 return SDOperand(N, 0);
4377
Chris Lattner01a22022005-10-10 22:04:48 +00004378 return SDOperand();
4379}
4380
Chris Lattner07649d92008-01-08 23:08:06 +00004381
Chris Lattner87514ca2005-10-10 22:31:19 +00004382SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004383 StoreSDNode *ST = cast<StoreSDNode>(N);
4384 SDOperand Chain = ST->getChain();
4385 SDOperand Value = ST->getValue();
4386 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004387
Chris Lattner00161a62008-01-25 07:20:16 +00004388 // Try to infer better alignment information than the store already has.
4389 if (ST->isUnindexed()) {
4390 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4391 if (Align > ST->getAlignment())
4392 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004393 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004394 ST->isVolatile(), Align);
4395 }
4396 }
4397
Evan Cheng59d5b682007-05-07 21:27:48 +00004398 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004399 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004400 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004401 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004402 unsigned Align = ST->getAlignment();
4403 MVT::ValueType SVT = Value.getOperand(0).getValueType();
4404 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00004405 getABITypeAlignment(MVT::getTypeForValueType(SVT));
Evan Chengc2cd2b22007-05-07 21:36:06 +00004406 if (Align <= OrigAlign && TLI.isOperationLegal(ISD::STORE, SVT))
Evan Cheng59d5b682007-05-07 21:27:48 +00004407 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004408 ST->getSrcValueOffset(), ST->isVolatile(), Align);
Jim Laskey279f0532006-09-25 16:29:54 +00004409 }
4410
Nate Begeman2cbba892006-12-11 02:23:46 +00004411 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004412 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Evan Cheng25ece662006-12-11 17:25:19 +00004413 if (Value.getOpcode() != ISD::TargetConstantFP) {
4414 SDOperand Tmp;
Chris Lattner62be1a72006-12-12 04:16:14 +00004415 switch (CFP->getValueType(0)) {
4416 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004417 case MVT::f80: // We don't do this for these yet.
4418 case MVT::f128:
4419 case MVT::ppcf128:
4420 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004421 case MVT::f32:
4422 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004423 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
4424 convertToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004425 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004426 ST->getSrcValueOffset(), ST->isVolatile(),
4427 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004428 }
4429 break;
4430 case MVT::f64:
4431 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004432 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
4433 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004434 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004435 ST->getSrcValueOffset(), ST->isVolatile(),
4436 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004437 } else if (TLI.isTypeLegal(MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004438 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004439 // argument passing. Since this is so common, custom legalize the
4440 // 64-bit integer store into two 32-bit stores.
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004441 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
Chris Lattner62be1a72006-12-12 04:16:14 +00004442 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4443 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004444 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004445
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004446 int SVOffset = ST->getSrcValueOffset();
4447 unsigned Alignment = ST->getAlignment();
4448 bool isVolatile = ST->isVolatile();
4449
Chris Lattner62be1a72006-12-12 04:16:14 +00004450 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004451 ST->getSrcValueOffset(),
4452 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004453 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4454 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004455 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004456 Alignment = MinAlign(Alignment, 4U);
Chris Lattner62be1a72006-12-12 04:16:14 +00004457 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004458 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004459 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4460 }
4461 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004462 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004463 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004464 }
4465
Jim Laskey279f0532006-09-25 16:29:54 +00004466 if (CombinerAA) {
4467 // Walk up chain skipping non-aliasing memory nodes.
4468 SDOperand BetterChain = FindBetterChain(N, Chain);
4469
Jim Laskey6ff23e52006-10-04 16:53:27 +00004470 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004471 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004472 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004473 SDOperand ReplStore;
4474 if (ST->isTruncatingStore()) {
4475 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004476 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004477 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004478 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004479 } else {
4480 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004481 ST->getSrcValue(), ST->getSrcValueOffset(),
4482 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004483 }
4484
Jim Laskey279f0532006-09-25 16:29:54 +00004485 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00004486 SDOperand Token =
4487 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4488
4489 // Don't add users to work list.
4490 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004491 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004492 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004493
Evan Cheng33dbedc2006-11-05 09:31:14 +00004494 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004495 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00004496 return SDOperand(N, 0);
4497
Chris Lattner3c872852007-12-29 06:26:16 +00004498 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004499 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Chris Lattner2b4c2792007-10-13 06:35:54 +00004500 MVT::isInteger(Value.getValueType())) {
4501 // See if we can simplify the input to this truncstore with knowledge that
4502 // only the low bits are being used. For example:
4503 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
4504 SDOperand Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004505 GetDemandedBits(Value,
4506 APInt::getLowBitsSet(Value.getValueSizeInBits(),
4507 MVT::getSizeInBits(ST->getMemoryVT())));
Chris Lattner2b4c2792007-10-13 06:35:54 +00004508 AddToWorkList(Value.Val);
4509 if (Shorter.Val)
4510 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004511 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004512 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004513
4514 // Otherwise, see if we can simplify the operation with
4515 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004516 if (SimplifyDemandedBits(Value,
4517 APInt::getLowBitsSet(
4518 Value.getValueSizeInBits(),
4519 MVT::getSizeInBits(ST->getMemoryVT()))))
Chris Lattnere33544c2007-10-13 06:58:48 +00004520 return SDOperand(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004521 }
4522
Chris Lattner3c872852007-12-29 06:26:16 +00004523 // If this is a load followed by a store to the same location, then the store
4524 // is dead/noop.
4525 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004526 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004527 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004528 // There can't be any side effects between the load and store, such as
4529 // a call or store.
Chris Lattner572dee72008-01-16 05:49:24 +00004530 Chain.reachesChainWithoutSideEffects(SDOperand(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004531 // The store is dead, remove it.
4532 return Chain;
4533 }
4534 }
4535
Chris Lattnerddf89562008-01-17 19:59:44 +00004536 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4537 // truncating store. We can do this even if this is already a truncstore.
4538 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
4539 && TLI.isTypeLegal(Value.getOperand(0).getValueType()) &&
4540 Value.Val->hasOneUse() && ST->isUnindexed() &&
4541 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004542 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004543 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004544 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004545 ST->isVolatile(), ST->getAlignment());
4546 }
4547
Chris Lattner87514ca2005-10-10 22:31:19 +00004548 return SDOperand();
4549}
4550
Chris Lattnerca242442006-03-19 01:27:56 +00004551SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4552 SDOperand InVec = N->getOperand(0);
4553 SDOperand InVal = N->getOperand(1);
4554 SDOperand EltNo = N->getOperand(2);
4555
4556 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4557 // vector with the inserted element.
4558 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
4559 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004560 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004561 if (Elt < Ops.size())
4562 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004563 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4564 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004565 }
4566
4567 return SDOperand();
4568}
4569
Evan Cheng513da432007-10-06 08:19:55 +00004570SDOperand DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
4571 SDOperand InVec = N->getOperand(0);
4572 SDOperand EltNo = N->getOperand(1);
4573
4574 // (vextract (v4f32 s2v (f32 load $addr)), 0) -> (f32 load $addr)
4575 // (vextract (v4i32 bc (v4f32 s2v (f32 load $addr))), 0) -> (i32 load $addr)
4576 if (isa<ConstantSDNode>(EltNo)) {
4577 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
4578 bool NewLoad = false;
4579 if (Elt == 0) {
4580 MVT::ValueType VT = InVec.getValueType();
4581 MVT::ValueType EVT = MVT::getVectorElementType(VT);
4582 MVT::ValueType LVT = EVT;
4583 unsigned NumElts = MVT::getVectorNumElements(VT);
4584 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
4585 MVT::ValueType BCVT = InVec.getOperand(0).getValueType();
Dan Gohman090b38a2007-10-29 20:44:42 +00004586 if (!MVT::isVector(BCVT) ||
4587 NumElts != MVT::getVectorNumElements(BCVT))
Evan Cheng513da432007-10-06 08:19:55 +00004588 return SDOperand();
4589 InVec = InVec.getOperand(0);
4590 EVT = MVT::getVectorElementType(BCVT);
4591 NewLoad = true;
4592 }
4593 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4594 InVec.getOperand(0).getValueType() == EVT &&
4595 ISD::isNormalLoad(InVec.getOperand(0).Val) &&
4596 InVec.getOperand(0).hasOneUse()) {
4597 LoadSDNode *LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4598 unsigned Align = LN0->getAlignment();
4599 if (NewLoad) {
4600 // Check the resultant load doesn't need a higher alignment than the
4601 // original load.
4602 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
4603 getABITypeAlignment(MVT::getTypeForValueType(LVT));
4604 if (!TLI.isOperationLegal(ISD::LOAD, LVT) || NewAlign > Align)
4605 return SDOperand();
4606 Align = NewAlign;
4607 }
4608
4609 return DAG.getLoad(LVT, LN0->getChain(), LN0->getBasePtr(),
4610 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4611 LN0->isVolatile(), Align);
4612 }
4613 }
4614 }
4615 return SDOperand();
4616}
4617
4618
Dan Gohman7f321562007-06-25 16:23:39 +00004619SDOperand DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
4620 unsigned NumInScalars = N->getNumOperands();
4621 MVT::ValueType VT = N->getValueType(0);
4622 unsigned NumElts = MVT::getVectorNumElements(VT);
4623 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattnerca242442006-03-19 01:27:56 +00004624
Dan Gohman7f321562007-06-25 16:23:39 +00004625 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4626 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4627 // at most two distinct vectors, turn this into a shuffle node.
Chris Lattnerd7648c82006-03-28 20:28:38 +00004628 SDOperand VecIn1, VecIn2;
4629 for (unsigned i = 0; i != NumInScalars; ++i) {
4630 // Ignore undef inputs.
4631 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4632
Dan Gohman7f321562007-06-25 16:23:39 +00004633 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004634 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004635 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004636 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
4637 VecIn1 = VecIn2 = SDOperand(0, 0);
4638 break;
4639 }
4640
Dan Gohman7f321562007-06-25 16:23:39 +00004641 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004642 // we can't make a shuffle.
4643 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004644 if (ExtractedFromVec.getValueType() != VT) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004645 VecIn1 = VecIn2 = SDOperand(0, 0);
4646 break;
4647 }
4648
4649 // Otherwise, remember this. We allow up to two distinct input vectors.
4650 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4651 continue;
4652
4653 if (VecIn1.Val == 0) {
4654 VecIn1 = ExtractedFromVec;
4655 } else if (VecIn2.Val == 0) {
4656 VecIn2 = ExtractedFromVec;
4657 } else {
4658 // Too many inputs.
4659 VecIn1 = VecIn2 = SDOperand(0, 0);
4660 break;
4661 }
4662 }
4663
4664 // If everything is good, we can make a shuffle operation.
4665 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004666 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004667 for (unsigned i = 0; i != NumInScalars; ++i) {
4668 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004669 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004670 continue;
4671 }
4672
4673 SDOperand Extract = N->getOperand(i);
4674
4675 // If extracting from the first vector, just use the index directly.
4676 if (Extract.getOperand(0) == VecIn1) {
4677 BuildVecIndices.push_back(Extract.getOperand(1));
4678 continue;
4679 }
4680
4681 // Otherwise, use InIdx + VecSize
4682 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004683 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004684 }
4685
4686 // Add count and size info.
Chris Lattner0bd48932008-01-17 07:00:52 +00004687 MVT::ValueType BuildVecVT = MVT::getVectorType(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004688
Dan Gohman7f321562007-06-25 16:23:39 +00004689 // Return the new VECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004690 SDOperand Ops[5];
4691 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00004692 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004693 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004694 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004695 // Use an undef build_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00004696 std::vector<SDOperand> UnOps(NumInScalars,
4697 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004698 EltType));
4699 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004700 &UnOps[0], UnOps.size());
4701 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00004702 }
Dan Gohman7f321562007-06-25 16:23:39 +00004703 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004704 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004705 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004706 }
4707
4708 return SDOperand();
4709}
4710
Dan Gohman7f321562007-06-25 16:23:39 +00004711SDOperand DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
4712 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
4713 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
4714 // inputs come from at most two distinct vectors, turn this into a shuffle
4715 // node.
4716
4717 // If we only have one input vector, we don't need to do any concatenation.
4718 if (N->getNumOperands() == 1) {
4719 return N->getOperand(0);
4720 }
4721
4722 return SDOperand();
4723}
4724
Chris Lattner66445d32006-03-28 22:11:53 +00004725SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004726 SDOperand ShufMask = N->getOperand(2);
4727 unsigned NumElts = ShufMask.getNumOperands();
4728
4729 // If the shuffle mask is an identity operation on the LHS, return the LHS.
4730 bool isIdentity = true;
4731 for (unsigned i = 0; i != NumElts; ++i) {
4732 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4733 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
4734 isIdentity = false;
4735 break;
4736 }
4737 }
4738 if (isIdentity) return N->getOperand(0);
4739
4740 // If the shuffle mask is an identity operation on the RHS, return the RHS.
4741 isIdentity = true;
4742 for (unsigned i = 0; i != NumElts; ++i) {
4743 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4744 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
4745 isIdentity = false;
4746 break;
4747 }
4748 }
4749 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004750
4751 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
4752 // needed at all.
4753 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00004754 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004755 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00004756 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004757 for (unsigned i = 0; i != NumElts; ++i)
4758 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
4759 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
4760 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00004761 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004762 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004763 BaseIdx = Idx;
4764 } else {
4765 if (BaseIdx != Idx)
4766 isSplat = false;
4767 if (VecNum != V) {
4768 isUnary = false;
4769 break;
4770 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004771 }
4772 }
4773
4774 SDOperand N0 = N->getOperand(0);
4775 SDOperand N1 = N->getOperand(1);
4776 // Normalize unary shuffle so the RHS is undef.
4777 if (isUnary && VecNum == 1)
4778 std::swap(N0, N1);
4779
Evan Cheng917ec982006-07-21 08:25:53 +00004780 // If it is a splat, check if the argument vector is a build_vector with
4781 // all scalar elements the same.
4782 if (isSplat) {
4783 SDNode *V = N0.Val;
Evan Cheng917ec982006-07-21 08:25:53 +00004784
Dan Gohman7f321562007-06-25 16:23:39 +00004785 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004786 // not the number of vector elements, look through it. Be careful not to
4787 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004788 if (V->getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng59569222006-10-16 22:49:37 +00004789 SDOperand ConvInput = V->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004790 if (MVT::getVectorNumElements(ConvInput.getValueType()) == NumElts)
Evan Cheng59569222006-10-16 22:49:37 +00004791 V = ConvInput.Val;
4792 }
4793
Dan Gohman7f321562007-06-25 16:23:39 +00004794 if (V->getOpcode() == ISD::BUILD_VECTOR) {
4795 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00004796 if (NumElems > BaseIdx) {
4797 SDOperand Base;
4798 bool AllSame = true;
4799 for (unsigned i = 0; i != NumElems; ++i) {
4800 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4801 Base = V->getOperand(i);
4802 break;
4803 }
4804 }
4805 // Splat of <u, u, u, u>, return <u, u, u, u>
4806 if (!Base.Val)
4807 return N0;
4808 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00004809 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00004810 AllSame = false;
4811 break;
4812 }
4813 }
4814 // Splat of <x, x, x, x>, return <x, x, x, x>
4815 if (AllSame)
4816 return N0;
4817 }
4818 }
4819 }
4820
Evan Chenge7bec0d2006-07-20 22:44:41 +00004821 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4822 // into an undef.
4823 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004824 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4825 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004826 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004827 for (unsigned i = 0; i != NumElts; ++i) {
4828 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4829 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4830 MappedOps.push_back(ShufMask.getOperand(i));
4831 } else {
4832 unsigned NewIdx =
4833 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
4834 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
4835 }
4836 }
Dan Gohman7f321562007-06-25 16:23:39 +00004837 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004838 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004839 AddToWorkList(ShufMask.Val);
Dan Gohman7f321562007-06-25 16:23:39 +00004840 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
4841 N0,
4842 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
4843 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00004844 }
Dan Gohman7f321562007-06-25 16:23:39 +00004845
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004846 return SDOperand();
4847}
4848
Evan Cheng44f1f092006-04-20 08:56:16 +00004849/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00004850/// an AND to a vector_shuffle with the destination vector and a zero vector.
4851/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00004852/// vector_shuffle V, Zero, <0, 4, 2, 4>
4853SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
4854 SDOperand LHS = N->getOperand(0);
4855 SDOperand RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00004856 if (N->getOpcode() == ISD::AND) {
4857 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00004858 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004859 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Evan Cheng44f1f092006-04-20 08:56:16 +00004860 std::vector<SDOperand> IdxOps;
4861 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00004862 unsigned NumElts = NumOps;
4863 MVT::ValueType EVT = MVT::getVectorElementType(RHS.getValueType());
Evan Cheng44f1f092006-04-20 08:56:16 +00004864 for (unsigned i = 0; i != NumElts; ++i) {
4865 SDOperand Elt = RHS.getOperand(i);
4866 if (!isa<ConstantSDNode>(Elt))
4867 return SDOperand();
4868 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
4869 IdxOps.push_back(DAG.getConstant(i, EVT));
4870 else if (cast<ConstantSDNode>(Elt)->isNullValue())
4871 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
4872 else
4873 return SDOperand();
4874 }
4875
4876 // Let's see if the target supports this vector_shuffle.
4877 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
4878 return SDOperand();
4879
Dan Gohman7f321562007-06-25 16:23:39 +00004880 // Return the new VECTOR_SHUFFLE node.
4881 MVT::ValueType VT = MVT::getVectorType(EVT, NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00004882 std::vector<SDOperand> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004883 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00004884 Ops.push_back(LHS);
4885 AddToWorkList(LHS.Val);
4886 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00004887 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004888 &ZeroOps[0], ZeroOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004889 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004890 &IdxOps[0], IdxOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004891 SDOperand Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004892 &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004893 if (VT != LHS.getValueType()) {
4894 Result = DAG.getNode(ISD::BIT_CONVERT, LHS.getValueType(), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00004895 }
4896 return Result;
4897 }
4898 }
4899 return SDOperand();
4900}
4901
Dan Gohman7f321562007-06-25 16:23:39 +00004902/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
4903SDOperand DAGCombiner::SimplifyVBinOp(SDNode *N) {
4904 // After legalize, the target may be depending on adds and other
4905 // binary ops to provide legal ways to construct constants or other
4906 // things. Simplifying them may result in a loss of legality.
4907 if (AfterLegalize) return SDOperand();
4908
4909 MVT::ValueType VT = N->getValueType(0);
Dan Gohman05d92fe2007-07-13 20:03:40 +00004910 assert(MVT::isVector(VT) && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00004911
4912 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattneredab1b92006-04-02 03:25:57 +00004913 SDOperand LHS = N->getOperand(0);
4914 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00004915 SDOperand Shuffle = XformToShuffleWithZero(N);
4916 if (Shuffle.Val) return Shuffle;
4917
Dan Gohman7f321562007-06-25 16:23:39 +00004918 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00004919 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00004920 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
4921 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004922 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004923 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Chris Lattneredab1b92006-04-02 03:25:57 +00004924 SDOperand LHSOp = LHS.getOperand(i);
4925 SDOperand RHSOp = RHS.getOperand(i);
4926 // If these two elements can't be folded, bail out.
4927 if ((LHSOp.getOpcode() != ISD::UNDEF &&
4928 LHSOp.getOpcode() != ISD::Constant &&
4929 LHSOp.getOpcode() != ISD::ConstantFP) ||
4930 (RHSOp.getOpcode() != ISD::UNDEF &&
4931 RHSOp.getOpcode() != ISD::Constant &&
4932 RHSOp.getOpcode() != ISD::ConstantFP))
4933 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00004934 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00004935 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
4936 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00004937 if ((RHSOp.getOpcode() == ISD::Constant &&
4938 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
4939 (RHSOp.getOpcode() == ISD::ConstantFP &&
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00004940 cast<ConstantFPSDNode>(RHSOp.Val)->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00004941 break;
4942 }
Dan Gohman7f321562007-06-25 16:23:39 +00004943 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00004944 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00004945 assert((Ops.back().getOpcode() == ISD::UNDEF ||
4946 Ops.back().getOpcode() == ISD::Constant ||
4947 Ops.back().getOpcode() == ISD::ConstantFP) &&
4948 "Scalar binop didn't fold!");
4949 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004950
Dan Gohman7f321562007-06-25 16:23:39 +00004951 if (Ops.size() == LHS.getNumOperands()) {
4952 MVT::ValueType VT = LHS.getValueType();
4953 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004954 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004955 }
4956
4957 return SDOperand();
4958}
4959
Nate Begeman44728a72005-09-19 22:34:01 +00004960SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00004961 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
4962
4963 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
4964 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4965 // If we got a simplified select_cc node back from SimplifySelectCC, then
4966 // break it down into a new SETCC node, and a new SELECT node, and then return
4967 // the SELECT node, since we were called with a SELECT node.
4968 if (SCC.Val) {
4969 // Check to see if we got a select_cc back (to turn into setcc/select).
4970 // Otherwise, just return whatever node we got back, like fabs.
4971 if (SCC.getOpcode() == ISD::SELECT_CC) {
4972 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
4973 SCC.getOperand(0), SCC.getOperand(1),
4974 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00004975 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004976 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
4977 SCC.getOperand(3), SETCC);
4978 }
4979 return SCC;
4980 }
Nate Begeman44728a72005-09-19 22:34:01 +00004981 return SDOperand();
4982}
4983
Chris Lattner40c62d52005-10-18 06:04:22 +00004984/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
4985/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00004986/// select. Callers of this should assume that TheSelect is deleted if this
4987/// returns true. As such, they should return the appropriate thing (e.g. the
4988/// node) back to the top-level of the DAG combiner loop to avoid it being
4989/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00004990///
4991bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
4992 SDOperand RHS) {
4993
4994 // If this is a select from two identical things, try to pull the operation
4995 // through the select.
4996 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00004997 // If this is a load and the token chain is identical, replace the select
4998 // of two loads with a load through a select of the address to load from.
4999 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5000 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005001 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005002 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005003 LHS.getOperand(0) == RHS.getOperand(0)) {
5004 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5005 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5006
5007 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005008 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005009 // FIXME: this conflates two src values, discarding one. This is not
5010 // the right thing to do, but nothing uses srcvalues now. When they do,
5011 // turn SrcValue into a list of locations.
5012 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005013 if (TheSelect->getOpcode() == ISD::SELECT) {
5014 // Check that the condition doesn't reach either load. If so, folding
5015 // this will induce a cycle into the DAG.
Evan Cheng917be682008-03-04 00:41:45 +00005016 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5017 !RLD->isPredecessorOf(TheSelect->getOperand(0).Val)) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005018 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5019 TheSelect->getOperand(0), LLD->getBasePtr(),
5020 RLD->getBasePtr());
5021 }
5022 } else {
5023 // Check that the condition doesn't reach either load. If so, folding
5024 // this will induce a cycle into the DAG.
Evan Cheng917be682008-03-04 00:41:45 +00005025 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5026 !RLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5027 !LLD->isPredecessorOf(TheSelect->getOperand(1).Val) &&
5028 !RLD->isPredecessorOf(TheSelect->getOperand(1).Val)) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005029 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005030 TheSelect->getOperand(0),
5031 TheSelect->getOperand(1),
5032 LLD->getBasePtr(), RLD->getBasePtr(),
5033 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005034 }
Evan Cheng466685d2006-10-09 20:57:25 +00005035 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005036
5037 if (Addr.Val) {
5038 SDOperand Load;
5039 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5040 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5041 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005042 LLD->getSrcValueOffset(),
5043 LLD->isVolatile(),
5044 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005045 else {
5046 Load = DAG.getExtLoad(LLD->getExtensionType(),
5047 TheSelect->getValueType(0),
5048 LLD->getChain(), Addr, LLD->getSrcValue(),
5049 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005050 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005051 LLD->isVolatile(),
5052 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005053 }
5054 // Users of the select now use the result of the load.
5055 CombineTo(TheSelect, Load);
5056
5057 // Users of the old loads now use the new load's chain. We know the
5058 // old-load value is dead now.
5059 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
5060 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
5061 return true;
5062 }
Evan Chengc5484282006-10-04 00:56:09 +00005063 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005064 }
5065 }
5066
5067 return false;
5068}
5069
Nate Begeman44728a72005-09-19 22:34:01 +00005070SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
5071 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00005072 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005073
5074 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00005075 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
5076 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
5077 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
5078
5079 // Determine if the condition we're dealing with is constant
5080 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00005081 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005082 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
5083
5084 // fold select_cc true, x, y -> x
5085 if (SCCC && SCCC->getValue())
5086 return N2;
5087 // fold select_cc false, x, y -> y
5088 if (SCCC && SCCC->getValue() == 0)
5089 return N3;
5090
5091 // Check to see if we can simplify the select into an fabs node
5092 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5093 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005094 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005095 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5096 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5097 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5098 N2 == N3.getOperand(0))
5099 return DAG.getNode(ISD::FABS, VT, N0);
5100
5101 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5102 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5103 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5104 N2.getOperand(0) == N3)
5105 return DAG.getNode(ISD::FABS, VT, N3);
5106 }
5107 }
5108
5109 // Check to see if we can perform the "gzip trick", transforming
5110 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005111 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00005112 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00005113 MVT::isInteger(N2.getValueType()) &&
5114 (N1C->isNullValue() || // (a < 0) ? b : 0
5115 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00005116 MVT::ValueType XType = N0.getValueType();
5117 MVT::ValueType AType = N2.getValueType();
5118 if (XType >= AType) {
5119 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005120 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00005121 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
5122 unsigned ShCtV = Log2_64(N2C->getValue());
5123 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
5124 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5125 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00005126 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005127 if (XType > AType) {
5128 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005129 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005130 }
5131 return DAG.getNode(ISD::AND, AType, Shift, N2);
5132 }
5133 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5134 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5135 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00005136 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005137 if (XType > AType) {
5138 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005139 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005140 }
5141 return DAG.getNode(ISD::AND, AType, Shift, N2);
5142 }
5143 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005144
5145 // fold select C, 16, 0 -> shl C, 4
5146 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
5147 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005148
5149 // If the caller doesn't want us to simplify this into a zext of a compare,
5150 // don't do it.
5151 if (NotExtCompare && N2C->getValue() == 1)
5152 return SDOperand();
5153
Nate Begeman07ed4172005-10-10 21:26:48 +00005154 // Get a SetCC of the condition
5155 // FIXME: Should probably make sure that setcc is legal if we ever have a
5156 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00005157 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005158 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00005159 if (AfterLegalize) {
5160 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Chris Lattner555d8d62006-12-07 22:36:47 +00005161 if (N2.getValueType() < SCC.getValueType())
5162 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5163 else
5164 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005165 } else {
5166 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005167 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005168 }
Chris Lattner5750df92006-03-01 04:03:14 +00005169 AddToWorkList(SCC.Val);
5170 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005171
5172 if (N2C->getValue() == 1)
5173 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005174 // shl setcc result by log2 n2c
5175 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
5176 DAG.getConstant(Log2_64(N2C->getValue()),
5177 TLI.getShiftAmountTy()));
5178 }
5179
Nate Begemanf845b452005-10-08 00:29:44 +00005180 // Check to see if this is the equivalent of setcc
5181 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5182 // otherwise, go ahead with the folds.
5183 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
5184 MVT::ValueType XType = N0.getValueType();
5185 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
5186 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
5187 if (Res.getValueType() != VT)
5188 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5189 return Res;
5190 }
5191
5192 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5193 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
5194 TLI.isOperationLegal(ISD::CTLZ, XType)) {
5195 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
5196 return DAG.getNode(ISD::SRL, XType, Ctlz,
5197 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
5198 TLI.getShiftAmountTy()));
5199 }
5200 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5201 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
5202 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
5203 N0);
5204 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
5205 DAG.getConstant(~0ULL, XType));
5206 return DAG.getNode(ISD::SRL, XType,
5207 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
5208 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5209 TLI.getShiftAmountTy()));
5210 }
5211 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5212 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
5213 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
5214 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5215 TLI.getShiftAmountTy()));
5216 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5217 }
5218 }
5219
5220 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5221 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5222 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005223 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
5224 N2.getOperand(0) == N1 && MVT::isInteger(N0.getValueType())) {
5225 MVT::ValueType XType = N0.getValueType();
5226 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5227 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5228 TLI.getShiftAmountTy()));
5229 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
5230 AddToWorkList(Shift.Val);
5231 AddToWorkList(Add.Val);
5232 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5233 }
5234 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5235 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5236 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5237 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5238 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Nate Begemanf845b452005-10-08 00:29:44 +00005239 MVT::ValueType XType = N0.getValueType();
5240 if (SubC->isNullValue() && MVT::isInteger(XType)) {
5241 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5242 DAG.getConstant(MVT::getSizeInBits(XType)-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005243 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00005244 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005245 AddToWorkList(Shift.Val);
5246 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005247 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5248 }
5249 }
5250 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005251
Nate Begeman44728a72005-09-19 22:34:01 +00005252 return SDOperand();
5253}
5254
Evan Chengfa1eb272007-02-08 22:13:59 +00005255/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Nate Begeman452d7be2005-09-16 00:54:12 +00005256SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00005257 SDOperand N1, ISD::CondCode Cond,
5258 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005259 TargetLowering::DAGCombinerInfo
5260 DagCombineInfo(DAG, !AfterLegalize, false, this);
5261 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005262}
5263
Nate Begeman69575232005-10-20 02:15:44 +00005264/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5265/// return a DAG expression to select that will generate the same value by
5266/// multiplying by a magic number. See:
5267/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5268SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005269 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005270 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
5271
Andrew Lenharth232c9102006-06-12 16:07:18 +00005272 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005273 ii != ee; ++ii)
5274 AddToWorkList(*ii);
5275 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005276}
5277
5278/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5279/// return a DAG expression to select that will generate the same value by
5280/// multiplying by a magic number. See:
5281/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5282SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005283 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005284 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005285
Andrew Lenharth232c9102006-06-12 16:07:18 +00005286 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005287 ii != ee; ++ii)
5288 AddToWorkList(*ii);
5289 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005290}
5291
Jim Laskey71382342006-10-07 23:37:56 +00005292/// FindBaseOffset - Return true if base is known not to alias with anything
5293/// but itself. Provides base object and offset as results.
5294static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
5295 // Assume it is a primitive operation.
5296 Base = Ptr; Offset = 0;
5297
5298 // If it's an adding a simple constant then integrate the offset.
5299 if (Base.getOpcode() == ISD::ADD) {
5300 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5301 Base = Base.getOperand(0);
5302 Offset += C->getValue();
5303 }
5304 }
5305
5306 // If it's any of the following then it can't alias with anything but itself.
5307 return isa<FrameIndexSDNode>(Base) ||
5308 isa<ConstantPoolSDNode>(Base) ||
5309 isa<GlobalAddressSDNode>(Base);
5310}
5311
5312/// isAlias - Return true if there is any possibility that the two addresses
5313/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00005314bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
5315 const Value *SrcValue1, int SrcValueOffset1,
5316 SDOperand Ptr2, int64_t Size2,
5317 const Value *SrcValue2, int SrcValueOffset2)
5318{
Jim Laskey71382342006-10-07 23:37:56 +00005319 // If they are the same then they must be aliases.
5320 if (Ptr1 == Ptr2) return true;
5321
5322 // Gather base node and offset information.
5323 SDOperand Base1, Base2;
5324 int64_t Offset1, Offset2;
5325 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5326 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5327
5328 // If they have a same base address then...
5329 if (Base1 == Base2) {
5330 // Check to see if the addresses overlap.
5331 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5332 }
5333
Jim Laskey096c22e2006-10-18 12:29:57 +00005334 // If we know both bases then they can't alias.
5335 if (KnownBase1 && KnownBase2) return false;
5336
Jim Laskey07a27092006-10-18 19:08:31 +00005337 if (CombinerGlobalAA) {
5338 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005339 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5340 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5341 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005342 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005343 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005344 if (AAResult == AliasAnalysis::NoAlias)
5345 return false;
5346 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005347
5348 // Otherwise we have to assume they alias.
5349 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005350}
5351
5352/// FindAliasInfo - Extracts the relevant alias information from the memory
5353/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005354bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00005355 SDOperand &Ptr, int64_t &Size,
5356 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005357 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5358 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005359 Size = MVT::getSizeInBits(LD->getMemoryVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005360 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005361 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005362 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005363 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005364 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005365 Size = MVT::getSizeInBits(ST->getMemoryVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005366 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005367 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005368 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005369 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005370 }
5371
5372 return false;
5373}
5374
Jim Laskey6ff23e52006-10-04 16:53:27 +00005375/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5376/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00005377void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005378 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005379 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005380 std::set<SDNode *> Visited; // Visited node set.
5381
Jim Laskey279f0532006-09-25 16:29:54 +00005382 // Get alias information for node.
5383 SDOperand Ptr;
5384 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005385 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005386 int SrcValueOffset;
5387 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005388
Jim Laskey6ff23e52006-10-04 16:53:27 +00005389 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005390 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005391
Jim Laskeybc588b82006-10-05 15:07:25 +00005392 // Look at each chain and determine if it is an alias. If so, add it to the
5393 // aliases list. If not, then continue up the chain looking for the next
5394 // candidate.
5395 while (!Chains.empty()) {
5396 SDOperand Chain = Chains.back();
5397 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005398
Jim Laskeybc588b82006-10-05 15:07:25 +00005399 // Don't bother if we've been before.
5400 if (Visited.find(Chain.Val) != Visited.end()) continue;
5401 Visited.insert(Chain.Val);
5402
5403 switch (Chain.getOpcode()) {
5404 case ISD::EntryToken:
5405 // Entry token is ideal chain operand, but handled in FindBetterChain.
5406 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005407
Jim Laskeybc588b82006-10-05 15:07:25 +00005408 case ISD::LOAD:
5409 case ISD::STORE: {
5410 // Get alias information for Chain.
5411 SDOperand OpPtr;
5412 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005413 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005414 int OpSrcValueOffset;
5415 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
5416 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005417
5418 // If chain is alias then stop here.
5419 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005420 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5421 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005422 Aliases.push_back(Chain);
5423 } else {
5424 // Look further up the chain.
5425 Chains.push_back(Chain.getOperand(0));
5426 // Clean up old chain.
5427 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00005428 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005429 break;
5430 }
5431
5432 case ISD::TokenFactor:
5433 // We have to check each of the operands of the token factor, so we queue
5434 // then up. Adding the operands to the queue (stack) in reverse order
5435 // maintains the original order and increases the likelihood that getNode
5436 // will find a matching token factor (CSE.)
5437 for (unsigned n = Chain.getNumOperands(); n;)
5438 Chains.push_back(Chain.getOperand(--n));
5439 // Eliminate the token factor if we can.
5440 AddToWorkList(Chain.Val);
5441 break;
5442
5443 default:
5444 // For all other instructions we will just have to take what we can get.
5445 Aliases.push_back(Chain);
5446 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005447 }
5448 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005449}
5450
5451/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5452/// for a better chain (aliasing node.)
5453SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
5454 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005455
Jim Laskey6ff23e52006-10-04 16:53:27 +00005456 // Accumulate all the aliases to this node.
5457 GatherAllAliases(N, OldChain, Aliases);
5458
5459 if (Aliases.size() == 0) {
5460 // If no operands then chain to entry token.
5461 return DAG.getEntryNode();
5462 } else if (Aliases.size() == 1) {
5463 // If a single operand then chain to it. We don't need to revisit it.
5464 return Aliases[0];
5465 }
5466
5467 // Construct a custom tailored token factor.
5468 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5469 &Aliases[0], Aliases.size());
5470
5471 // Make sure the old chain gets cleaned up.
5472 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
5473
5474 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005475}
5476
Nate Begeman1d4d4142005-09-01 00:19:25 +00005477// SelectionDAG::Combine - This is the entry point for the file.
5478//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005479void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00005480 if (!RunningAfterLegalize && ViewDAGCombine1)
5481 viewGraph();
5482 if (RunningAfterLegalize && ViewDAGCombine2)
5483 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005484 /// run - This is the main entry point to this class.
5485 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005486 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005487}