blob: 99cbb7f523eae3b832d91d6eeea06a4d121e8d45 [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
1105 if (N0 == N1)
1106 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001107 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001108 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001109 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001110 // fold (sub x, c) -> (add x, -c)
1111 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001112 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001113 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001114 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001115 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001116 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001117 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001118 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001119 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1120 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1121 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1122 if (Result.Val) return Result;
1123 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001124 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001125 if (N0.getOpcode() == ISD::UNDEF)
1126 return N0;
1127 if (N1.getOpcode() == ISD::UNDEF)
1128 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001129
Nate Begeman83e75ec2005-09-06 04:43:02 +00001130 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001131}
1132
Nate Begeman83e75ec2005-09-06 04:43:02 +00001133SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001134 SDOperand N0 = N->getOperand(0);
1135 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001136 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1137 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +00001138 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001139
Dan Gohman7f321562007-06-25 16:23:39 +00001140 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001141 if (MVT::isVector(VT)) {
1142 SDOperand FoldedVOp = SimplifyVBinOp(N);
1143 if (FoldedVOp.Val) return FoldedVOp;
1144 }
Dan Gohman7f321562007-06-25 16:23:39 +00001145
Dan Gohman613e0d82007-07-03 14:03:57 +00001146 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001147 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001148 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001149 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001150 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001151 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001152 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001153 if (N0C && !N1C)
1154 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001155 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001156 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001157 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001158 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001159 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001160 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001161 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +00001162 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +00001163 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001164 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001165 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001166 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1167 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1168 // FIXME: If the input is something that is easily negated (e.g. a
1169 // single-use add), we should put the negate there.
1170 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1171 DAG.getNode(ISD::SHL, VT, N0,
1172 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1173 TLI.getShiftAmountTy())));
1174 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001175
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001176 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1177 if (N1C && N0.getOpcode() == ISD::SHL &&
1178 isa<ConstantSDNode>(N0.getOperand(1))) {
1179 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001180 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001181 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1182 }
1183
1184 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1185 // use.
1186 {
1187 SDOperand Sh(0,0), Y(0,0);
1188 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1189 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1190 N0.Val->hasOneUse()) {
1191 Sh = N0; Y = N1;
1192 } else if (N1.getOpcode() == ISD::SHL &&
1193 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1194 Sh = N1; Y = N0;
1195 }
1196 if (Sh.Val) {
1197 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1198 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1199 }
1200 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001201 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1202 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1203 isa<ConstantSDNode>(N0.getOperand(1))) {
1204 return DAG.getNode(ISD::ADD, VT,
1205 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1206 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1207 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001208
Nate Begemancd4d58c2006-02-03 06:46:56 +00001209 // reassociate mul
1210 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1211 if (RMUL.Val != 0)
1212 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001213
Nate Begeman83e75ec2005-09-06 04:43:02 +00001214 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001215}
1216
Nate Begeman83e75ec2005-09-06 04:43:02 +00001217SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001218 SDOperand N0 = N->getOperand(0);
1219 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001220 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1221 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001222 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001223
Dan Gohman7f321562007-06-25 16:23:39 +00001224 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001225 if (MVT::isVector(VT)) {
1226 SDOperand FoldedVOp = SimplifyVBinOp(N);
1227 if (FoldedVOp.Val) return FoldedVOp;
1228 }
Dan Gohman7f321562007-06-25 16:23:39 +00001229
Nate Begeman1d4d4142005-09-01 00:19:25 +00001230 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001231 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001232 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001233 // fold (sdiv X, 1) -> X
1234 if (N1C && N1C->getSignExtended() == 1LL)
1235 return N0;
1236 // fold (sdiv X, -1) -> 0-X
1237 if (N1C && N1C->isAllOnesValue())
1238 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001239 // If we know the sign bits of both operands are zero, strength reduce to a
1240 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Chris Lattnerf32aac32008-01-27 23:32:17 +00001241 if (!MVT::isVector(VT)) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001242 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerf32aac32008-01-27 23:32:17 +00001243 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1244 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001245 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +00001246 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001247 (isPowerOf2_64(N1C->getSignExtended()) ||
1248 isPowerOf2_64(-N1C->getSignExtended()))) {
1249 // If dividing by powers of two is cheap, then don't perform the following
1250 // fold.
1251 if (TLI.isPow2DivCheap())
1252 return SDOperand();
1253 int64_t pow2 = N1C->getSignExtended();
1254 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001255 unsigned lg2 = Log2_64(abs2);
1256 // Splat the sign bit into the register
1257 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001258 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1259 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001260 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001261 // Add (N0 < 0) ? abs2 - 1 : 0;
1262 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1263 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001264 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001265 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001266 AddToWorkList(SRL.Val);
1267 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001268 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1269 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001270 // If we're dividing by a positive value, we're done. Otherwise, we must
1271 // negate the result.
1272 if (pow2 > 0)
1273 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001274 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001275 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1276 }
Nate Begeman69575232005-10-20 02:15:44 +00001277 // if integer divide is expensive and we satisfy the requirements, emit an
1278 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001279 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001280 !TLI.isIntDivCheap()) {
1281 SDOperand Op = BuildSDIV(N);
1282 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001283 }
Dan Gohman7f321562007-06-25 16:23:39 +00001284
Dan Gohman613e0d82007-07-03 14:03:57 +00001285 // undef / X -> 0
1286 if (N0.getOpcode() == ISD::UNDEF)
1287 return DAG.getConstant(0, VT);
1288 // X / undef -> undef
1289 if (N1.getOpcode() == ISD::UNDEF)
1290 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001291
Nate Begeman83e75ec2005-09-06 04:43:02 +00001292 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001293}
1294
Nate Begeman83e75ec2005-09-06 04:43:02 +00001295SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001296 SDOperand N0 = N->getOperand(0);
1297 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001298 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1299 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001300 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001301
Dan Gohman7f321562007-06-25 16:23:39 +00001302 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001303 if (MVT::isVector(VT)) {
1304 SDOperand FoldedVOp = SimplifyVBinOp(N);
1305 if (FoldedVOp.Val) return FoldedVOp;
1306 }
Dan Gohman7f321562007-06-25 16:23:39 +00001307
Nate Begeman1d4d4142005-09-01 00:19:25 +00001308 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001309 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001310 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001311 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +00001312 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001313 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001314 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001315 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001316 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1317 if (N1.getOpcode() == ISD::SHL) {
1318 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1319 if (isPowerOf2_64(SHC->getValue())) {
1320 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001321 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
1322 DAG.getConstant(Log2_64(SHC->getValue()),
1323 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001324 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001325 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001326 }
1327 }
1328 }
Nate Begeman69575232005-10-20 02:15:44 +00001329 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +00001330 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
1331 SDOperand Op = BuildUDIV(N);
1332 if (Op.Val) return Op;
1333 }
Dan Gohman7f321562007-06-25 16:23:39 +00001334
Dan Gohman613e0d82007-07-03 14:03:57 +00001335 // undef / X -> 0
1336 if (N0.getOpcode() == ISD::UNDEF)
1337 return DAG.getConstant(0, VT);
1338 // X / undef -> undef
1339 if (N1.getOpcode() == ISD::UNDEF)
1340 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001341
Nate Begeman83e75ec2005-09-06 04:43:02 +00001342 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001343}
1344
Nate Begeman83e75ec2005-09-06 04:43:02 +00001345SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001346 SDOperand N0 = N->getOperand(0);
1347 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001348 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1349 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001350 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001351
1352 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001353 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001354 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001355 // If we know the sign bits of both operands are zero, strength reduce to a
1356 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Chris Lattneree339f42008-01-27 23:21:58 +00001357 if (!MVT::isVector(VT)) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001358 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001359 return DAG.getNode(ISD::UREM, VT, N0, N1);
1360 }
Chris Lattner26d29902006-10-12 20:58:32 +00001361
Dan Gohman77003042007-11-26 23:46:11 +00001362 // If X/C can be simplified by the division-by-constant logic, lower
1363 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001364 if (N1C && !N1C->isNullValue()) {
1365 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Chris Lattner5eee4272008-01-26 01:09:19 +00001366 AddToWorkList(Div.Val);
Dan Gohman77003042007-11-26 23:46:11 +00001367 SDOperand OptimizedDiv = combine(Div.Val);
1368 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1369 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1370 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1371 AddToWorkList(Mul.Val);
1372 return Sub;
1373 }
Chris Lattner26d29902006-10-12 20:58:32 +00001374 }
1375
Dan Gohman613e0d82007-07-03 14:03:57 +00001376 // undef % X -> 0
1377 if (N0.getOpcode() == ISD::UNDEF)
1378 return DAG.getConstant(0, VT);
1379 // X % undef -> undef
1380 if (N1.getOpcode() == ISD::UNDEF)
1381 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001382
Nate Begeman83e75ec2005-09-06 04:43:02 +00001383 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001384}
1385
Nate Begeman83e75ec2005-09-06 04:43:02 +00001386SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001387 SDOperand N0 = N->getOperand(0);
1388 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001389 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1390 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001391 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001392
1393 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001394 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001395 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001396 // fold (urem x, pow2) -> (and x, pow2-1)
1397 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +00001398 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001399 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1400 if (N1.getOpcode() == ISD::SHL) {
1401 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1402 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +00001403 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001404 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001405 return DAG.getNode(ISD::AND, VT, N0, Add);
1406 }
1407 }
1408 }
Chris Lattner26d29902006-10-12 20:58:32 +00001409
Dan Gohman77003042007-11-26 23:46:11 +00001410 // If X/C can be simplified by the division-by-constant logic, lower
1411 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001412 if (N1C && !N1C->isNullValue()) {
1413 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman77003042007-11-26 23:46:11 +00001414 SDOperand OptimizedDiv = combine(Div.Val);
1415 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1416 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1417 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1418 AddToWorkList(Mul.Val);
1419 return Sub;
1420 }
Chris Lattner26d29902006-10-12 20:58:32 +00001421 }
1422
Dan Gohman613e0d82007-07-03 14:03:57 +00001423 // undef % X -> 0
1424 if (N0.getOpcode() == ISD::UNDEF)
1425 return DAG.getConstant(0, VT);
1426 // X % undef -> undef
1427 if (N1.getOpcode() == ISD::UNDEF)
1428 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001429
Nate Begeman83e75ec2005-09-06 04:43:02 +00001430 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001431}
1432
Nate Begeman83e75ec2005-09-06 04:43:02 +00001433SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001434 SDOperand N0 = N->getOperand(0);
1435 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001436 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001437 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001438
1439 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001440 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001441 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001442 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001443 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001444 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1445 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001446 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001447 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001448 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001449 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001450
Nate Begeman83e75ec2005-09-06 04:43:02 +00001451 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001452}
1453
Nate Begeman83e75ec2005-09-06 04:43:02 +00001454SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001455 SDOperand N0 = N->getOperand(0);
1456 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001457 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001458 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001459
1460 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001461 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001462 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001463 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001464 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001465 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001466 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001467 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001468 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001469
Nate Begeman83e75ec2005-09-06 04:43:02 +00001470 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001471}
1472
Dan Gohman389079b2007-10-08 17:57:15 +00001473/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1474/// compute two values. LoOp and HiOp give the opcodes for the two computations
1475/// that are being performed. Return true if a simplification was made.
1476///
Chris Lattner5eee4272008-01-26 01:09:19 +00001477SDOperand DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1478 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001479 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001480 bool HiExists = N->hasAnyUseOfValue(1);
1481 if (!HiExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001482 (!AfterLegalize ||
1483 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001484 SDOperand Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
1485 N->getNumOperands());
1486 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001487 }
1488
1489 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001490 bool LoExists = N->hasAnyUseOfValue(0);
1491 if (!LoExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001492 (!AfterLegalize ||
1493 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001494 SDOperand Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
1495 N->getNumOperands());
1496 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001497 }
1498
Evan Cheng44711942007-11-08 09:25:29 +00001499 // If both halves are used, return as it is.
1500 if (LoExists && HiExists)
Chris Lattner5eee4272008-01-26 01:09:19 +00001501 return SDOperand();
Evan Cheng44711942007-11-08 09:25:29 +00001502
1503 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001504 if (LoExists) {
1505 SDOperand Lo = DAG.getNode(LoOp, N->getValueType(0),
1506 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001507 AddToWorkList(Lo.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001508 SDOperand LoOpt = combine(Lo.Val);
Chris Lattner5eee4272008-01-26 01:09:19 +00001509 if (LoOpt.Val && LoOpt.Val != Lo.Val &&
1510 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType()))
1511 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001512 }
1513
Evan Cheng44711942007-11-08 09:25:29 +00001514 if (HiExists) {
1515 SDOperand Hi = DAG.getNode(HiOp, N->getValueType(1),
1516 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001517 AddToWorkList(Hi.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001518 SDOperand HiOpt = combine(Hi.Val);
1519 if (HiOpt.Val && HiOpt != Hi &&
Chris Lattner5eee4272008-01-26 01:09:19 +00001520 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType()))
1521 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001522 }
Chris Lattner5eee4272008-01-26 01:09:19 +00001523 return SDOperand();
Dan Gohman389079b2007-10-08 17:57:15 +00001524}
1525
1526SDOperand DAGCombiner::visitSMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001527 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
1528 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001529
1530 return SDOperand();
1531}
1532
1533SDOperand DAGCombiner::visitUMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001534 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
1535 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001536
1537 return SDOperand();
1538}
1539
1540SDOperand DAGCombiner::visitSDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001541 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
1542 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001543
1544 return SDOperand();
1545}
1546
1547SDOperand DAGCombiner::visitUDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001548 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
1549 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001550
1551 return SDOperand();
1552}
1553
Chris Lattner35e5c142006-05-05 05:51:50 +00001554/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1555/// two operands of the same opcode, try to simplify it.
1556SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1557 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1558 MVT::ValueType VT = N0.getValueType();
1559 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1560
Chris Lattner540121f2006-05-05 06:31:05 +00001561 // For each of OP in AND/OR/XOR:
1562 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1563 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1564 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001565 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001566 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001567 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001568 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1569 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1570 N0.getOperand(0).getValueType(),
1571 N0.getOperand(0), N1.getOperand(0));
1572 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001573 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001574 }
1575
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001576 // For each of OP in SHL/SRL/SRA/AND...
1577 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1578 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1579 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001580 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001581 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001582 N0.getOperand(1) == N1.getOperand(1)) {
1583 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1584 N0.getOperand(0).getValueType(),
1585 N0.getOperand(0), N1.getOperand(0));
1586 AddToWorkList(ORNode.Val);
1587 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1588 }
1589
1590 return SDOperand();
1591}
1592
Nate Begeman83e75ec2005-09-06 04:43:02 +00001593SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001594 SDOperand N0 = N->getOperand(0);
1595 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001596 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001597 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1598 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001599 MVT::ValueType VT = N1.getValueType();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001600 unsigned BitWidth = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001601
Dan Gohman7f321562007-06-25 16:23:39 +00001602 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001603 if (MVT::isVector(VT)) {
1604 SDOperand FoldedVOp = SimplifyVBinOp(N);
1605 if (FoldedVOp.Val) return FoldedVOp;
1606 }
Dan Gohman7f321562007-06-25 16:23:39 +00001607
Dan Gohman613e0d82007-07-03 14:03:57 +00001608 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001609 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001610 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001611 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001612 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001613 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001614 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001615 if (N0C && !N1C)
1616 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001617 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001618 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001619 return N0;
1620 // if (and x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001621 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
1622 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001623 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001624 // reassociate and
1625 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1626 if (RAND.Val != 0)
1627 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001628 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001629 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001630 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001631 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001632 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001633 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1634 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001635 SDOperand N0Op0 = N0.getOperand(0);
1636 APInt Mask = ~N1C->getAPIntValue();
1637 Mask.trunc(N0Op0.getValueSizeInBits());
1638 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001639 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001640 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001641
1642 // Replace uses of the AND with uses of the Zero extend node.
1643 CombineTo(N, Zext);
1644
Chris Lattner3603cd62006-02-02 07:17:31 +00001645 // We actually want to replace all uses of the any_extend with the
1646 // zero_extend, to avoid duplicating things. This will later cause this
1647 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001648 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001649 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001650 }
1651 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001652 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1653 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1654 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1655 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1656
1657 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1658 MVT::isInteger(LL.getValueType())) {
1659 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1660 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1661 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001662 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001663 return DAG.getSetCC(VT, ORNode, LR, Op1);
1664 }
1665 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1666 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1667 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001668 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001669 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1670 }
1671 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1672 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1673 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001674 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001675 return DAG.getSetCC(VT, ORNode, LR, Op1);
1676 }
1677 }
1678 // canonicalize equivalent to ll == rl
1679 if (LL == RR && LR == RL) {
1680 Op1 = ISD::getSetCCSwappedOperands(Op1);
1681 std::swap(RL, RR);
1682 }
1683 if (LL == RL && LR == RR) {
1684 bool isInteger = MVT::isInteger(LL.getValueType());
1685 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1686 if (Result != ISD::SETCC_INVALID)
1687 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1688 }
1689 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001690
1691 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1692 if (N0.getOpcode() == N1.getOpcode()) {
1693 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1694 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001695 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001696
Nate Begemande996292006-02-03 22:24:05 +00001697 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1698 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001699 if (!MVT::isVector(VT) &&
1700 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001701 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001702 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001703 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001704 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001705 MVT::ValueType EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001706 // If we zero all the possible extended bits, then we can turn this into
1707 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001708 unsigned BitWidth = N1.getValueSizeInBits();
1709 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
1710 BitWidth - MVT::getSizeInBits(EVT))) &&
Evan Chengc5484282006-10-04 00:56:09 +00001711 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001712 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1713 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001714 LN0->getSrcValueOffset(), EVT,
1715 LN0->isVolatile(),
1716 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001717 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001718 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001719 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001720 }
1721 }
1722 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001723 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1724 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001725 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001726 MVT::ValueType EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001727 // If we zero all the possible extended bits, then we can turn this into
1728 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001729 unsigned BitWidth = N1.getValueSizeInBits();
1730 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
1731 BitWidth - MVT::getSizeInBits(EVT))) &&
Evan Chengc5484282006-10-04 00:56:09 +00001732 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001733 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1734 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001735 LN0->getSrcValueOffset(), EVT,
1736 LN0->isVolatile(),
1737 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001738 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001739 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001740 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001741 }
1742 }
Chris Lattner15045b62006-02-28 06:35:35 +00001743
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001744 // fold (and (load x), 255) -> (zextload x, i8)
1745 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001746 if (N1C && N0.getOpcode() == ISD::LOAD) {
1747 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1748 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Chris Lattnerddf89562008-01-17 19:59:44 +00001749 LN0->isUnindexed() && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001750 MVT::ValueType EVT, LoadedVT;
1751 if (N1C->getValue() == 255)
1752 EVT = MVT::i8;
1753 else if (N1C->getValue() == 65535)
1754 EVT = MVT::i16;
1755 else if (N1C->getValue() == ~0U)
1756 EVT = MVT::i32;
1757 else
1758 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001759
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001760 LoadedVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001761 if (EVT != MVT::Other && LoadedVT > EVT &&
1762 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1763 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1764 // For big endian targets, we need to add an offset to the pointer to
1765 // load the correct bytes. For little endian systems, we merely need to
1766 // read fewer bytes from the same pointer.
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001767 unsigned LVTStoreBytes = MVT::getStoreSizeInBits(LoadedVT)/8;
1768 unsigned EVTStoreBytes = MVT::getStoreSizeInBits(EVT)/8;
1769 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001770 unsigned Alignment = LN0->getAlignment();
Evan Cheng466685d2006-10-09 20:57:25 +00001771 SDOperand NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001772 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001773 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1774 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001775 Alignment = MinAlign(Alignment, PtrOff);
1776 }
Evan Cheng466685d2006-10-09 20:57:25 +00001777 AddToWorkList(NewPtr.Val);
1778 SDOperand Load =
1779 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001780 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001781 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001782 AddToWorkList(N);
1783 CombineTo(N0.Val, Load, Load.getValue(1));
1784 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1785 }
Chris Lattner15045b62006-02-28 06:35:35 +00001786 }
1787 }
1788
Nate Begeman83e75ec2005-09-06 04:43:02 +00001789 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001790}
1791
Nate Begeman83e75ec2005-09-06 04:43:02 +00001792SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001793 SDOperand N0 = N->getOperand(0);
1794 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001795 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001796 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1797 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001798 MVT::ValueType VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001799
Dan Gohman7f321562007-06-25 16:23:39 +00001800 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001801 if (MVT::isVector(VT)) {
1802 SDOperand FoldedVOp = SimplifyVBinOp(N);
1803 if (FoldedVOp.Val) return FoldedVOp;
1804 }
Dan Gohman7f321562007-06-25 16:23:39 +00001805
Dan Gohman613e0d82007-07-03 14:03:57 +00001806 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001807 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001808 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001809 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001810 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001811 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001812 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001813 if (N0C && !N1C)
1814 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001815 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001816 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001817 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001818 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001819 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001820 return N1;
1821 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001822 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001823 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001824 // reassociate or
1825 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1826 if (ROR.Val != 0)
1827 return ROR;
1828 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1829 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001830 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001831 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1832 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1833 N1),
1834 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001835 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001836 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1837 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1838 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1839 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1840
1841 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1842 MVT::isInteger(LL.getValueType())) {
1843 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1844 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1845 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1846 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1847 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001848 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001849 return DAG.getSetCC(VT, ORNode, LR, Op1);
1850 }
1851 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1852 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1853 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1854 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1855 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001856 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001857 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1858 }
1859 }
1860 // canonicalize equivalent to ll == rl
1861 if (LL == RR && LR == RL) {
1862 Op1 = ISD::getSetCCSwappedOperands(Op1);
1863 std::swap(RL, RR);
1864 }
1865 if (LL == RL && LR == RR) {
1866 bool isInteger = MVT::isInteger(LL.getValueType());
1867 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1868 if (Result != ISD::SETCC_INVALID)
1869 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1870 }
1871 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001872
1873 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1874 if (N0.getOpcode() == N1.getOpcode()) {
1875 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1876 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001877 }
Chris Lattner516b9622006-09-14 20:50:57 +00001878
Chris Lattner1ec72732006-09-14 21:11:37 +00001879 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1880 if (N0.getOpcode() == ISD::AND &&
1881 N1.getOpcode() == ISD::AND &&
1882 N0.getOperand(1).getOpcode() == ISD::Constant &&
1883 N1.getOperand(1).getOpcode() == ISD::Constant &&
1884 // Don't increase # computations.
1885 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1886 // We can only do this xform if we know that bits from X that are set in C2
1887 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001888 const APInt &LHSMask =
1889 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1890 const APInt &RHSMask =
1891 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00001892
Dan Gohmanea859be2007-06-22 14:59:07 +00001893 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1894 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001895 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1896 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1897 }
1898 }
1899
1900
Chris Lattner516b9622006-09-14 20:50:57 +00001901 // See if this is some rotate idiom.
1902 if (SDNode *Rot = MatchRotate(N0, N1))
1903 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001904
Nate Begeman83e75ec2005-09-06 04:43:02 +00001905 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001906}
1907
Chris Lattner516b9622006-09-14 20:50:57 +00001908
1909/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1910static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1911 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001912 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001913 Mask = Op.getOperand(1);
1914 Op = Op.getOperand(0);
1915 } else {
1916 return false;
1917 }
1918 }
1919
1920 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1921 Shift = Op;
1922 return true;
1923 }
1924 return false;
1925}
1926
1927
1928// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1929// idioms for rotate, and if the target supports rotation instructions, generate
1930// a rot[lr].
1931SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1932 // Must be a legal type. Expanded an promoted things won't work with rotates.
1933 MVT::ValueType VT = LHS.getValueType();
1934 if (!TLI.isTypeLegal(VT)) return 0;
1935
1936 // The target must have at least one rotate flavor.
1937 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1938 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1939 if (!HasROTL && !HasROTR) return 0;
1940
1941 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1942 SDOperand LHSShift; // The shift.
1943 SDOperand LHSMask; // AND value if any.
1944 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1945 return 0; // Not part of a rotate.
1946
1947 SDOperand RHSShift; // The shift.
1948 SDOperand RHSMask; // AND value if any.
1949 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1950 return 0; // Not part of a rotate.
1951
1952 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1953 return 0; // Not shifting the same value.
1954
1955 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1956 return 0; // Shifts must disagree.
1957
1958 // Canonicalize shl to left side in a shl/srl pair.
1959 if (RHSShift.getOpcode() == ISD::SHL) {
1960 std::swap(LHS, RHS);
1961 std::swap(LHSShift, RHSShift);
1962 std::swap(LHSMask , RHSMask );
1963 }
1964
1965 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Scott Michelc9dc1142007-04-02 21:36:32 +00001966 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1967 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1968 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001969
1970 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1971 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001972 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1973 RHSShiftAmt.getOpcode() == ISD::Constant) {
1974 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
1975 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00001976 if ((LShVal + RShVal) != OpSizeInBits)
1977 return 0;
1978
1979 SDOperand Rot;
1980 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001981 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001982 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001983 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001984
1985 // If there is an AND of either shifted operand, apply it to the result.
1986 if (LHSMask.Val || RHSMask.Val) {
1987 uint64_t Mask = MVT::getIntVTBitMask(VT);
1988
1989 if (LHSMask.Val) {
1990 uint64_t RHSBits = (1ULL << LShVal)-1;
1991 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1992 }
1993 if (RHSMask.Val) {
1994 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1995 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1996 }
1997
1998 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1999 }
2000
2001 return Rot.Val;
2002 }
2003
2004 // If there is a mask here, and we have a variable shift, we can't be sure
2005 // that we're masking out the right stuff.
2006 if (LHSMask.Val || RHSMask.Val)
2007 return 0;
2008
2009 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2010 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002011 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2012 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002013 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002014 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002015 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002016 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002017 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002018 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002019 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002020 }
Chris Lattner516b9622006-09-14 20:50:57 +00002021 }
2022 }
2023
2024 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2025 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002026 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2027 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002028 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002029 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002030 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002031 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002032 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002033 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002034 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002035 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002036 }
2037 }
2038
2039 // Look for sign/zext/any-extended cases:
2040 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2041 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2042 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
2043 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2044 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2045 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
2046 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
2047 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
2048 if (RExtOp0.getOpcode() == ISD::SUB &&
2049 RExtOp0.getOperand(1) == LExtOp0) {
2050 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2051 // (rotr x, y)
2052 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2053 // (rotl x, (sub 32, y))
2054 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
2055 if (SUBC->getValue() == OpSizeInBits) {
2056 if (HasROTL)
2057 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2058 else
2059 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
2060 }
2061 }
2062 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2063 RExtOp0 == LExtOp0.getOperand(1)) {
2064 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2065 // (rotl x, y)
2066 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2067 // (rotr x, (sub 32, y))
2068 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
2069 if (SUBC->getValue() == OpSizeInBits) {
2070 if (HasROTL)
2071 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
2072 else
2073 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2074 }
2075 }
Chris Lattner516b9622006-09-14 20:50:57 +00002076 }
2077 }
2078
2079 return 0;
2080}
2081
2082
Nate Begeman83e75ec2005-09-06 04:43:02 +00002083SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002084 SDOperand N0 = N->getOperand(0);
2085 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002086 SDOperand LHS, RHS, CC;
2087 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2088 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002089 MVT::ValueType VT = N0.getValueType();
2090
Dan Gohman7f321562007-06-25 16:23:39 +00002091 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00002092 if (MVT::isVector(VT)) {
2093 SDOperand FoldedVOp = SimplifyVBinOp(N);
2094 if (FoldedVOp.Val) return FoldedVOp;
2095 }
Dan Gohman7f321562007-06-25 16:23:39 +00002096
Dan Gohman613e0d82007-07-03 14:03:57 +00002097 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002098 if (N0.getOpcode() == ISD::UNDEF)
2099 return N0;
2100 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002101 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002102 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002103 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002104 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00002105 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002106 if (N0C && !N1C)
2107 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002108 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002109 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002110 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002111 // reassociate xor
2112 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
2113 if (RXOR.Val != 0)
2114 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002115 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00002116 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
2117 bool isInt = MVT::isInteger(LHS.getValueType());
2118 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2119 isInt);
2120 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002121 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002122 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002123 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002124 assert(0 && "Unhandled SetCC Equivalent!");
2125 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002126 }
Chris Lattner61c5ff42007-09-10 21:39:07 +00002127 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
2128 if (N1C && N1C->getValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
2129 N0.Val->hasOneUse() && isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
2130 SDOperand V = N0.getOperand(0);
2131 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002132 DAG.getConstant(1, V.getValueType()));
Chris Lattner61c5ff42007-09-10 21:39:07 +00002133 AddToWorkList(V.Val);
2134 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2135 }
2136
Nate Begeman99801192005-09-07 23:25:52 +00002137 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Chris Lattner734c91d2006-11-10 21:37:15 +00002138 if (N1C && N1C->getValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002139 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002140 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002141 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2142 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002143 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2144 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002145 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002146 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002147 }
2148 }
Nate Begeman99801192005-09-07 23:25:52 +00002149 // fold !(x or y) -> (!x and !y) iff x or y are constants
2150 if (N1C && N1C->isAllOnesValue() &&
2151 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002152 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002153 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2154 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002155 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2156 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002157 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002158 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002159 }
2160 }
Nate Begeman223df222005-09-08 20:18:10 +00002161 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2162 if (N1C && N0.getOpcode() == ISD::XOR) {
2163 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2164 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2165 if (N00C)
2166 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
2167 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
2168 if (N01C)
2169 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
2170 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
2171 }
2172 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002173 if (N0 == N1) {
2174 if (!MVT::isVector(VT)) {
2175 return DAG.getConstant(0, VT);
2176 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2177 // Produce a vector of zeros.
Dan Gohman51eaa862007-06-14 22:58:02 +00002178 SDOperand El = DAG.getConstant(0, MVT::getVectorElementType(VT));
Chris Lattner4fbdd592006-03-28 19:11:05 +00002179 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002180 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002181 }
2182 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002183
2184 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2185 if (N0.getOpcode() == N1.getOpcode()) {
2186 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
2187 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002188 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002189
Chris Lattner3e104b12006-04-08 04:15:24 +00002190 // Simplify the expression using non-local knowledge.
2191 if (!MVT::isVector(VT) &&
2192 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002193 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002194
Nate Begeman83e75ec2005-09-06 04:43:02 +00002195 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002196}
2197
Chris Lattnere70da202007-12-06 07:33:36 +00002198/// visitShiftByConstant - Handle transforms common to the three shifts, when
2199/// the shift amount is a constant.
2200SDOperand DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
2201 SDNode *LHS = N->getOperand(0).Val;
2202 if (!LHS->hasOneUse()) return SDOperand();
2203
2204 // We want to pull some binops through shifts, so that we have (and (shift))
2205 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2206 // thing happens with address calculations, so it's important to canonicalize
2207 // it.
2208 bool HighBitSet = false; // Can we transform this if the high bit is set?
2209
2210 switch (LHS->getOpcode()) {
2211 default: return SDOperand();
2212 case ISD::OR:
2213 case ISD::XOR:
2214 HighBitSet = false; // We can only transform sra if the high bit is clear.
2215 break;
2216 case ISD::AND:
2217 HighBitSet = true; // We can only transform sra if the high bit is set.
2218 break;
2219 case ISD::ADD:
2220 if (N->getOpcode() != ISD::SHL)
2221 return SDOperand(); // only shl(add) not sr[al](add).
2222 HighBitSet = false; // We can only transform sra if the high bit is clear.
2223 break;
2224 }
2225
2226 // We require the RHS of the binop to be a constant as well.
2227 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
2228 if (!BinOpCst) return SDOperand();
2229
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002230
2231 // FIXME: disable this for unless the input to the binop is a shift by a
2232 // constant. If it is not a shift, it pessimizes some common cases like:
2233 //
2234 //void foo(int *X, int i) { X[i & 1235] = 1; }
2235 //int bar(int *X, int i) { return X[i & 255]; }
2236 SDNode *BinOpLHSVal = LHS->getOperand(0).Val;
2237 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2238 BinOpLHSVal->getOpcode() != ISD::SRA &&
2239 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2240 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
2241 return SDOperand();
2242
Chris Lattnere70da202007-12-06 07:33:36 +00002243 MVT::ValueType VT = N->getValueType(0);
2244
2245 // If this is a signed shift right, and the high bit is modified
2246 // by the logical operation, do not perform the transformation.
2247 // The highBitSet boolean indicates the value of the high bit of
2248 // the constant which would cause it to be modified for this
2249 // operation.
2250 if (N->getOpcode() == ISD::SRA) {
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002251 uint64_t BinOpRHSSign = BinOpCst->getValue() >> (MVT::getSizeInBits(VT)-1);
Chris Lattnere70da202007-12-06 07:33:36 +00002252 if ((bool)BinOpRHSSign != HighBitSet)
2253 return SDOperand();
2254 }
2255
2256 // Fold the constants, shifting the binop RHS by the shift amount.
2257 SDOperand NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
2258 LHS->getOperand(1), N->getOperand(1));
2259
2260 // Create the new shift.
2261 SDOperand NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
2262 N->getOperand(1));
2263
2264 // Create the new binop.
2265 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2266}
2267
2268
Nate Begeman83e75ec2005-09-06 04:43:02 +00002269SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002270 SDOperand N0 = N->getOperand(0);
2271 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002272 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2273 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002274 MVT::ValueType VT = N0.getValueType();
2275 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2276
2277 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002278 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002279 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002280 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002281 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002282 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002283 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002284 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002285 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002286 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002287 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002288 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002289 // if (shl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002290 if (DAG.MaskedValueIsZero(SDOperand(N, 0),
2291 APInt::getAllOnesValue(MVT::getSizeInBits(VT))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002292 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00002293 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002294 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002295 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002296 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002297 N0.getOperand(1).getOpcode() == ISD::Constant) {
2298 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002299 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002300 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002301 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002302 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002303 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002304 }
2305 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2306 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002307 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002308 N0.getOperand(1).getOpcode() == ISD::Constant) {
2309 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002310 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002311 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
2312 DAG.getConstant(~0ULL << c1, VT));
2313 if (c2 > c1)
2314 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002315 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002316 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002317 return DAG.getNode(ISD::SRL, VT, Mask,
2318 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002319 }
2320 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002321 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002322 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002323 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002324
2325 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002326}
2327
Nate Begeman83e75ec2005-09-06 04:43:02 +00002328SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002329 SDOperand N0 = N->getOperand(0);
2330 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002331 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2332 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002333 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002334
2335 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002336 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002337 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002338 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002339 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002340 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002341 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002342 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002343 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002344 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00002345 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002346 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002347 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002348 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002349 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002350 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2351 // sext_inreg.
2352 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
2353 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
2354 MVT::ValueType EVT;
2355 switch (LowBits) {
2356 default: EVT = MVT::Other; break;
2357 case 1: EVT = MVT::i1; break;
2358 case 8: EVT = MVT::i8; break;
2359 case 16: EVT = MVT::i16; break;
2360 case 32: EVT = MVT::i32; break;
2361 }
2362 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
2363 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2364 DAG.getValueType(EVT));
2365 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002366
2367 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2368 if (N1C && N0.getOpcode() == ISD::SRA) {
2369 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2370 unsigned Sum = N1C->getValue() + C1->getValue();
2371 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
2372 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2373 DAG.getConstant(Sum, N1C->getValueType(0)));
2374 }
2375 }
2376
Chris Lattnera8504462006-05-08 20:51:54 +00002377 // Simplify, based on bits shifted out of the LHS.
2378 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2379 return SDOperand(N, 0);
2380
2381
Nate Begeman1d4d4142005-09-01 00:19:25 +00002382 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002383 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002384 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002385
2386 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002387}
2388
Nate Begeman83e75ec2005-09-06 04:43:02 +00002389SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002390 SDOperand N0 = N->getOperand(0);
2391 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002392 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2393 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002394 MVT::ValueType VT = N0.getValueType();
2395 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2396
2397 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002398 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002399 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002400 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002401 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002402 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002403 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002404 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002405 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002406 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002407 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002408 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002409 // if (srl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002410 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
2411 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002412 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002413
Nate Begeman1d4d4142005-09-01 00:19:25 +00002414 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002415 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002416 N0.getOperand(1).getOpcode() == ISD::Constant) {
2417 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002418 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002419 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002420 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002421 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002422 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002423 }
Chris Lattner350bec02006-04-02 06:11:11 +00002424
Chris Lattner06afe072006-05-05 22:53:17 +00002425 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2426 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2427 // Shifting in all undef bits?
2428 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
2429 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
2430 return DAG.getNode(ISD::UNDEF, VT);
2431
2432 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
2433 AddToWorkList(SmallShift.Val);
2434 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2435 }
2436
Chris Lattner3657ffe2006-10-12 20:23:19 +00002437 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2438 // bit, which is unmodified by sra.
2439 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
2440 if (N0.getOpcode() == ISD::SRA)
2441 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2442 }
2443
Chris Lattner350bec02006-04-02 06:11:11 +00002444 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2445 if (N1C && N0.getOpcode() == ISD::CTLZ &&
2446 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002447 APInt KnownZero, KnownOne;
2448 APInt Mask = APInt::getAllOnesValue(MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +00002449 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002450
2451 // If any of the input bits are KnownOne, then the input couldn't be all
2452 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002453 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002454
2455 // If all of the bits input the to ctlz node are known to be zero, then
2456 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002457 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002458 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2459
2460 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2461 if ((UnknownBits & (UnknownBits-1)) == 0) {
2462 // Okay, we know that only that the single bit specified by UnknownBits
2463 // could be set on input to the CTLZ node. If this bit is set, the SRL
2464 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2465 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002466 unsigned ShAmt = UnknownBits.countTrailingZeros();
Chris Lattner350bec02006-04-02 06:11:11 +00002467 SDOperand Op = N0.getOperand(0);
2468 if (ShAmt) {
2469 Op = DAG.getNode(ISD::SRL, VT, Op,
2470 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2471 AddToWorkList(Op.Val);
2472 }
2473 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2474 }
2475 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002476
2477 // fold operands of srl based on knowledge that the low bits are not
2478 // demanded.
2479 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2480 return SDOperand(N, 0);
2481
Chris Lattnere70da202007-12-06 07:33:36 +00002482 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002483}
2484
Nate Begeman83e75ec2005-09-06 04:43:02 +00002485SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002486 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002487 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002488
2489 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002490 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002491 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002492 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002493}
2494
Nate Begeman83e75ec2005-09-06 04:43:02 +00002495SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002496 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002497 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002498
2499 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002500 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002501 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002502 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002503}
2504
Nate Begeman83e75ec2005-09-06 04:43:02 +00002505SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002506 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002507 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002508
2509 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002510 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002511 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002512 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002513}
2514
Nate Begeman452d7be2005-09-16 00:54:12 +00002515SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2516 SDOperand N0 = N->getOperand(0);
2517 SDOperand N1 = N->getOperand(1);
2518 SDOperand N2 = N->getOperand(2);
2519 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2520 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2521 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2522 MVT::ValueType VT = N->getValueType(0);
Evan Cheng571c4782007-08-18 05:57:05 +00002523 MVT::ValueType VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002524
Nate Begeman452d7be2005-09-16 00:54:12 +00002525 // fold select C, X, X -> X
2526 if (N1 == N2)
2527 return N1;
2528 // fold select true, X, Y -> X
2529 if (N0C && !N0C->isNullValue())
2530 return N1;
2531 // fold select false, X, Y -> Y
2532 if (N0C && N0C->isNullValue())
2533 return N2;
2534 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002535 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002536 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002537 // fold select C, 0, 1 -> ~C
2538 if (MVT::isInteger(VT) && MVT::isInteger(VT0) &&
2539 N1C && N2C && N1C->isNullValue() && N2C->getValue() == 1) {
2540 SDOperand XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
2541 if (VT == VT0)
2542 return XORNode;
2543 AddToWorkList(XORNode.Val);
2544 if (MVT::getSizeInBits(VT) > MVT::getSizeInBits(VT0))
2545 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2546 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2547 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002548 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002549 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
2550 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002551 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002552 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2553 }
2554 // fold select C, X, 1 -> ~C | X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002555 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getValue() == 1) {
2556 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002557 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002558 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2559 }
2560 // fold select C, X, 0 -> C & X
2561 // FIXME: this should check for C type == X type, not i1?
2562 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2563 return DAG.getNode(ISD::AND, VT, N0, N1);
2564 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2565 if (MVT::i1 == VT && N0 == N1)
2566 return DAG.getNode(ISD::OR, VT, N0, N2);
2567 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2568 if (MVT::i1 == VT && N0 == N2)
2569 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002570
Chris Lattner40c62d52005-10-18 06:04:22 +00002571 // If we can fold this based on the true/false value, do so.
2572 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002573 return SDOperand(N, 0); // Don't revisit N.
2574
Nate Begeman44728a72005-09-19 22:34:01 +00002575 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002576 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002577 // FIXME:
2578 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2579 // having to say they don't support SELECT_CC on every type the DAG knows
2580 // about, since there is no way to mark an opcode illegal at all value types
2581 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2582 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2583 N1, N2, N0.getOperand(2));
2584 else
2585 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002586 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002587 return SDOperand();
2588}
2589
2590SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002591 SDOperand N0 = N->getOperand(0);
2592 SDOperand N1 = N->getOperand(1);
2593 SDOperand N2 = N->getOperand(2);
2594 SDOperand N3 = N->getOperand(3);
2595 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002596 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2597
Nate Begeman44728a72005-09-19 22:34:01 +00002598 // fold select_cc lhs, rhs, x, x, cc -> x
2599 if (N2 == N3)
2600 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002601
Chris Lattner5f42a242006-09-20 06:19:26 +00002602 // Determine if the condition we're dealing with is constant
2603 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002604 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002605
2606 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
2607 if (SCCC->getValue())
2608 return N2; // cond always true -> true val
2609 else
2610 return N3; // cond always false -> false val
2611 }
2612
2613 // Fold to a simpler select_cc
2614 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2615 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2616 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2617 SCC.getOperand(2));
2618
Chris Lattner40c62d52005-10-18 06:04:22 +00002619 // If we can fold this based on the true/false value, do so.
2620 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002621 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002622
Nate Begeman44728a72005-09-19 22:34:01 +00002623 // fold select_cc into other things, such as min/max/abs
2624 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002625}
2626
2627SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2628 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2629 cast<CondCodeSDNode>(N->getOperand(2))->get());
2630}
2631
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002632// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2633// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2634// transformation. Returns true if extension are possible and the above
2635// mentioned transformation is profitable.
2636static bool ExtendUsesToFormExtLoad(SDNode *N, SDOperand N0,
2637 unsigned ExtOpc,
2638 SmallVector<SDNode*, 4> &ExtendNodes,
2639 TargetLowering &TLI) {
2640 bool HasCopyToRegUses = false;
2641 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
2642 for (SDNode::use_iterator UI = N0.Val->use_begin(), UE = N0.Val->use_end();
2643 UI != UE; ++UI) {
2644 SDNode *User = *UI;
2645 if (User == N)
2646 continue;
2647 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2648 if (User->getOpcode() == ISD::SETCC) {
2649 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2650 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2651 // Sign bits will be lost after a zext.
2652 return false;
2653 bool Add = false;
2654 for (unsigned i = 0; i != 2; ++i) {
2655 SDOperand UseOp = User->getOperand(i);
2656 if (UseOp == N0)
2657 continue;
2658 if (!isa<ConstantSDNode>(UseOp))
2659 return false;
2660 Add = true;
2661 }
2662 if (Add)
2663 ExtendNodes.push_back(User);
2664 } else {
2665 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2666 SDOperand UseOp = User->getOperand(i);
2667 if (UseOp == N0) {
2668 // If truncate from extended type to original load type is free
2669 // on this target, then it's ok to extend a CopyToReg.
2670 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2671 HasCopyToRegUses = true;
2672 else
2673 return false;
2674 }
2675 }
2676 }
2677 }
2678
2679 if (HasCopyToRegUses) {
2680 bool BothLiveOut = false;
2681 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2682 UI != UE; ++UI) {
2683 SDNode *User = *UI;
2684 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2685 SDOperand UseOp = User->getOperand(i);
2686 if (UseOp.Val == N && UseOp.ResNo == 0) {
2687 BothLiveOut = true;
2688 break;
2689 }
2690 }
2691 }
2692 if (BothLiveOut)
2693 // Both unextended and extended values are live out. There had better be
2694 // good a reason for the transformation.
2695 return ExtendNodes.size();
2696 }
2697 return true;
2698}
2699
Nate Begeman83e75ec2005-09-06 04:43:02 +00002700SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002701 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002702 MVT::ValueType VT = N->getValueType(0);
2703
Nate Begeman1d4d4142005-09-01 00:19:25 +00002704 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002705 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002706 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002707
Nate Begeman1d4d4142005-09-01 00:19:25 +00002708 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002709 // fold (sext (aext x)) -> (sext x)
2710 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002711 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002712
Evan Chengc88138f2007-03-22 01:54:19 +00002713 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2714 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Chris Lattner22558872007-02-26 03:13:59 +00002715 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002716 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002717 if (NarrowLoad.Val) {
2718 if (NarrowLoad.Val != N0.Val)
2719 CombineTo(N0.Val, NarrowLoad);
2720 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2721 }
Evan Chengc88138f2007-03-22 01:54:19 +00002722 }
2723
2724 // See if the value being truncated is already sign extended. If so, just
2725 // eliminate the trunc/sext pair.
2726 if (N0.getOpcode() == ISD::TRUNCATE) {
Chris Lattner6007b842006-09-21 06:00:20 +00002727 SDOperand Op = N0.getOperand(0);
Chris Lattner22558872007-02-26 03:13:59 +00002728 unsigned OpBits = MVT::getSizeInBits(Op.getValueType());
2729 unsigned MidBits = MVT::getSizeInBits(N0.getValueType());
2730 unsigned DestBits = MVT::getSizeInBits(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002731 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002732
2733 if (OpBits == DestBits) {
2734 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2735 // bits, it is already ready.
2736 if (NumSignBits > DestBits-MidBits)
2737 return Op;
2738 } else if (OpBits < DestBits) {
2739 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2740 // bits, just sext from i32.
2741 if (NumSignBits > OpBits-MidBits)
2742 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2743 } else {
2744 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2745 // bits, just truncate to i32.
2746 if (NumSignBits > OpBits-MidBits)
2747 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002748 }
Chris Lattner22558872007-02-26 03:13:59 +00002749
2750 // fold (sext (truncate x)) -> (sextinreg x).
2751 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2752 N0.getValueType())) {
2753 if (Op.getValueType() < VT)
2754 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2755 else if (Op.getValueType() > VT)
2756 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2757 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2758 DAG.getValueType(N0.getValueType()));
2759 }
Chris Lattner6007b842006-09-21 06:00:20 +00002760 }
Chris Lattner310b5782006-05-06 23:06:26 +00002761
Evan Cheng110dec22005-12-14 02:19:23 +00002762 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002763 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002764 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002765 bool DoXform = true;
2766 SmallVector<SDNode*, 4> SetCCs;
2767 if (!N0.hasOneUse())
2768 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2769 if (DoXform) {
2770 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2771 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2772 LN0->getBasePtr(), LN0->getSrcValue(),
2773 LN0->getSrcValueOffset(),
2774 N0.getValueType(),
2775 LN0->isVolatile(),
2776 LN0->getAlignment());
2777 CombineTo(N, ExtLoad);
2778 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2779 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2780 // Extend SetCC uses if necessary.
2781 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2782 SDNode *SetCC = SetCCs[i];
2783 SmallVector<SDOperand, 4> Ops;
2784 for (unsigned j = 0; j != 2; ++j) {
2785 SDOperand SOp = SetCC->getOperand(j);
2786 if (SOp == Trunc)
2787 Ops.push_back(ExtLoad);
2788 else
2789 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2790 }
2791 Ops.push_back(SetCC->getOperand(2));
2792 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2793 &Ops[0], Ops.size()));
2794 }
2795 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2796 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002797 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002798
2799 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2800 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002801 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2802 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002803 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002804 MVT::ValueType EVT = LN0->getMemoryVT();
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002805 if (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
2806 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2807 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002808 LN0->getSrcValueOffset(), EVT,
2809 LN0->isVolatile(),
2810 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002811 CombineTo(N, ExtLoad);
2812 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2813 ExtLoad.getValue(1));
2814 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2815 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002816 }
2817
Chris Lattner20a35c32007-04-11 05:32:27 +00002818 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2819 if (N0.getOpcode() == ISD::SETCC) {
2820 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002821 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2822 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2823 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2824 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002825 }
2826
Nate Begeman83e75ec2005-09-06 04:43:02 +00002827 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002828}
2829
Nate Begeman83e75ec2005-09-06 04:43:02 +00002830SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002831 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002832 MVT::ValueType VT = N->getValueType(0);
2833
Nate Begeman1d4d4142005-09-01 00:19:25 +00002834 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002835 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002836 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002837 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002838 // fold (zext (aext x)) -> (zext x)
2839 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002840 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002841
Evan Chengc88138f2007-03-22 01:54:19 +00002842 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2843 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002844 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002845 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002846 if (NarrowLoad.Val) {
2847 if (NarrowLoad.Val != N0.Val)
2848 CombineTo(N0.Val, NarrowLoad);
2849 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2850 }
Evan Chengc88138f2007-03-22 01:54:19 +00002851 }
2852
Chris Lattner6007b842006-09-21 06:00:20 +00002853 // fold (zext (truncate x)) -> (and x, mask)
2854 if (N0.getOpcode() == ISD::TRUNCATE &&
2855 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2856 SDOperand Op = N0.getOperand(0);
2857 if (Op.getValueType() < VT) {
2858 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2859 } else if (Op.getValueType() > VT) {
2860 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2861 }
2862 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2863 }
2864
Chris Lattner111c2282006-09-21 06:14:31 +00002865 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2866 if (N0.getOpcode() == ISD::AND &&
2867 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2868 N0.getOperand(1).getOpcode() == ISD::Constant) {
2869 SDOperand X = N0.getOperand(0).getOperand(0);
2870 if (X.getValueType() < VT) {
2871 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2872 } else if (X.getValueType() > VT) {
2873 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2874 }
2875 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2876 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2877 }
2878
Evan Cheng110dec22005-12-14 02:19:23 +00002879 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002880 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002881 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002882 bool DoXform = true;
2883 SmallVector<SDNode*, 4> SetCCs;
2884 if (!N0.hasOneUse())
2885 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
2886 if (DoXform) {
2887 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2888 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2889 LN0->getBasePtr(), LN0->getSrcValue(),
2890 LN0->getSrcValueOffset(),
2891 N0.getValueType(),
2892 LN0->isVolatile(),
2893 LN0->getAlignment());
2894 CombineTo(N, ExtLoad);
2895 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2896 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2897 // Extend SetCC uses if necessary.
2898 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2899 SDNode *SetCC = SetCCs[i];
2900 SmallVector<SDOperand, 4> Ops;
2901 for (unsigned j = 0; j != 2; ++j) {
2902 SDOperand SOp = SetCC->getOperand(j);
2903 if (SOp == Trunc)
2904 Ops.push_back(ExtLoad);
2905 else
Evan Chengde1631b2007-10-30 20:11:21 +00002906 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002907 }
2908 Ops.push_back(SetCC->getOperand(2));
2909 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2910 &Ops[0], Ops.size()));
2911 }
2912 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2913 }
Evan Cheng110dec22005-12-14 02:19:23 +00002914 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002915
2916 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2917 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002918 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2919 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002920 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002921 MVT::ValueType EVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002922 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2923 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002924 LN0->getSrcValueOffset(), EVT,
2925 LN0->isVolatile(),
2926 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002927 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002928 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2929 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002930 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002931 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002932
2933 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2934 if (N0.getOpcode() == ISD::SETCC) {
2935 SDOperand SCC =
2936 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2937 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00002938 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2939 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002940 }
2941
Nate Begeman83e75ec2005-09-06 04:43:02 +00002942 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002943}
2944
Chris Lattner5ffc0662006-05-05 05:58:59 +00002945SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2946 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002947 MVT::ValueType VT = N->getValueType(0);
2948
2949 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002950 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002951 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2952 // fold (aext (aext x)) -> (aext x)
2953 // fold (aext (zext x)) -> (zext x)
2954 // fold (aext (sext x)) -> (sext x)
2955 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2956 N0.getOpcode() == ISD::ZERO_EXTEND ||
2957 N0.getOpcode() == ISD::SIGN_EXTEND)
2958 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2959
Evan Chengc88138f2007-03-22 01:54:19 +00002960 // fold (aext (truncate (load x))) -> (aext (smaller load x))
2961 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
2962 if (N0.getOpcode() == ISD::TRUNCATE) {
2963 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002964 if (NarrowLoad.Val) {
2965 if (NarrowLoad.Val != N0.Val)
2966 CombineTo(N0.Val, NarrowLoad);
2967 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
2968 }
Evan Chengc88138f2007-03-22 01:54:19 +00002969 }
2970
Chris Lattner84750582006-09-20 06:29:17 +00002971 // fold (aext (truncate x))
2972 if (N0.getOpcode() == ISD::TRUNCATE) {
2973 SDOperand TruncOp = N0.getOperand(0);
2974 if (TruncOp.getValueType() == VT)
2975 return TruncOp; // x iff x size == zext size.
2976 if (TruncOp.getValueType() > VT)
2977 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2978 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2979 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002980
2981 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2982 if (N0.getOpcode() == ISD::AND &&
2983 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2984 N0.getOperand(1).getOpcode() == ISD::Constant) {
2985 SDOperand X = N0.getOperand(0).getOperand(0);
2986 if (X.getValueType() < VT) {
2987 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2988 } else if (X.getValueType() > VT) {
2989 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2990 }
2991 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2992 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2993 }
2994
Chris Lattner5ffc0662006-05-05 05:58:59 +00002995 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002996 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002997 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002998 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2999 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3000 LN0->getBasePtr(), LN0->getSrcValue(),
3001 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003002 N0.getValueType(),
3003 LN0->isVolatile(),
3004 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003005 CombineTo(N, ExtLoad);
3006 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3007 ExtLoad.getValue(1));
3008 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3009 }
3010
3011 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3012 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3013 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003014 if (N0.getOpcode() == ISD::LOAD &&
3015 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003016 N0.hasOneUse()) {
3017 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003018 MVT::ValueType EVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00003019 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
3020 LN0->getChain(), LN0->getBasePtr(),
3021 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003022 LN0->getSrcValueOffset(), EVT,
3023 LN0->isVolatile(),
3024 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003025 CombineTo(N, ExtLoad);
3026 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3027 ExtLoad.getValue(1));
3028 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3029 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003030
3031 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3032 if (N0.getOpcode() == ISD::SETCC) {
3033 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003034 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3035 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003036 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00003037 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003038 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003039 }
3040
Chris Lattner5ffc0662006-05-05 05:58:59 +00003041 return SDOperand();
3042}
3043
Chris Lattner2b4c2792007-10-13 06:35:54 +00003044/// GetDemandedBits - See if the specified operand can be simplified with the
3045/// knowledge that only the bits specified by Mask are used. If so, return the
3046/// simpler operand, otherwise return a null SDOperand.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003047SDOperand DAGCombiner::GetDemandedBits(SDOperand V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003048 switch (V.getOpcode()) {
3049 default: break;
3050 case ISD::OR:
3051 case ISD::XOR:
3052 // If the LHS or RHS don't contribute bits to the or, drop them.
3053 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3054 return V.getOperand(1);
3055 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3056 return V.getOperand(0);
3057 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003058 case ISD::SRL:
3059 // Only look at single-use SRLs.
3060 if (!V.Val->hasOneUse())
3061 break;
3062 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3063 // See if we can recursively simplify the LHS.
3064 unsigned Amt = RHSC->getValue();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003065 APInt NewMask = Mask << Amt;
3066 SDOperand SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Chris Lattnere33544c2007-10-13 06:58:48 +00003067 if (SimplifyLHS.Val) {
3068 return DAG.getNode(ISD::SRL, V.getValueType(),
3069 SimplifyLHS, V.getOperand(1));
3070 }
3071 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003072 }
3073 return SDOperand();
3074}
3075
Evan Chengc88138f2007-03-22 01:54:19 +00003076/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3077/// bits and then truncated to a narrower type and where N is a multiple
3078/// of number of bits of the narrower type, transform it to a narrower load
3079/// from address + N / num of bits of new type. If the result is to be
3080/// extended, also fold the extension to form a extending load.
3081SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
3082 unsigned Opc = N->getOpcode();
3083 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
3084 SDOperand N0 = N->getOperand(0);
3085 MVT::ValueType VT = N->getValueType(0);
3086 MVT::ValueType EVT = N->getValueType(0);
3087
Evan Chenge177e302007-03-23 22:13:36 +00003088 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3089 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003090 if (Opc == ISD::SIGN_EXTEND_INREG) {
3091 ExtType = ISD::SEXTLOAD;
3092 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00003093 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
3094 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00003095 }
3096
3097 unsigned EVTBits = MVT::getSizeInBits(EVT);
3098 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003099 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003100 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3101 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3102 ShAmt = N01->getValue();
3103 // Is the shift amount a multiple of size of VT?
3104 if ((ShAmt & (EVTBits-1)) == 0) {
3105 N0 = N0.getOperand(0);
3106 if (MVT::getSizeInBits(N0.getValueType()) <= EVTBits)
3107 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00003108 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003109 }
3110 }
3111 }
3112
3113 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
3114 // Do not allow folding to i1 here. i1 is implicitly stored in memory in
3115 // zero extended form: by shrinking the load, we lose track of the fact
3116 // that it is already zero extended.
3117 // FIXME: This should be reevaluated.
3118 VT != MVT::i1) {
3119 assert(MVT::getSizeInBits(N0.getValueType()) > EVTBits &&
3120 "Cannot truncate to larger type!");
3121 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3122 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003123 // For big endian targets, we need to adjust the offset to the pointer to
3124 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003125 if (TLI.isBigEndian()) {
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003126 unsigned LVTStoreBits = MVT::getStoreSizeInBits(N0.getValueType());
3127 unsigned EVTStoreBits = MVT::getStoreSizeInBits(EVT);
3128 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3129 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003130 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003131 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Evan Chengc88138f2007-03-22 01:54:19 +00003132 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
3133 DAG.getConstant(PtrOff, PtrType));
3134 AddToWorkList(NewPtr.Val);
3135 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
3136 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003137 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Duncan Sandsdc846502007-10-28 12:59:45 +00003138 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003139 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003140 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00003141 LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003142 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003143 if (CombineSRL) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003144 WorkListRemover DeadNodes(*this);
3145 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3146 &DeadNodes);
Evan Chengb37b80c2007-03-23 20:55:21 +00003147 CombineTo(N->getOperand(0).Val, Load);
3148 } else
3149 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003150 if (ShAmt) {
3151 if (Opc == ISD::SIGN_EXTEND_INREG)
3152 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3153 else
3154 return DAG.getNode(Opc, VT, Load);
3155 }
Evan Chengc88138f2007-03-22 01:54:19 +00003156 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3157 }
3158
3159 return SDOperand();
3160}
3161
Chris Lattner5ffc0662006-05-05 05:58:59 +00003162
Nate Begeman83e75ec2005-09-06 04:43:02 +00003163SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003164 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003165 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003166 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003167 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003168 unsigned VTBits = MVT::getSizeInBits(VT);
Nate Begeman07ed4172005-10-10 21:26:48 +00003169 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003170
Nate Begeman1d4d4142005-09-01 00:19:25 +00003171 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003172 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003173 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003174
Chris Lattner541a24f2006-05-06 22:43:44 +00003175 // If the input is already sign extended, just drop the extension.
Dan Gohmanea859be2007-06-22 14:59:07 +00003176 if (DAG.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003177 return N0;
3178
Nate Begeman646d7e22005-09-02 21:18:40 +00003179 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3180 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3181 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003182 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003183 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003184
Chris Lattner95a5e052007-04-17 19:03:21 +00003185 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003186 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003187 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003188
Chris Lattner95a5e052007-04-17 19:03:21 +00003189 // fold operands of sext_in_reg based on knowledge that the top bits are not
3190 // demanded.
3191 if (SimplifyDemandedBits(SDOperand(N, 0)))
3192 return SDOperand(N, 0);
3193
Evan Chengc88138f2007-03-22 01:54:19 +00003194 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3195 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
3196 SDOperand NarrowLoad = ReduceLoadWidth(N);
3197 if (NarrowLoad.Val)
3198 return NarrowLoad;
3199
Chris Lattner4b37e872006-05-08 21:18:59 +00003200 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3201 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3202 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3203 if (N0.getOpcode() == ISD::SRL) {
3204 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
3205 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
3206 // We can turn this into an SRA iff the input to the SRL is already sign
3207 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003208 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Chris Lattner4b37e872006-05-08 21:18:59 +00003209 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
3210 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3211 }
3212 }
Evan Chengc88138f2007-03-22 01:54:19 +00003213
Nate Begemanded49632005-10-13 03:11:28 +00003214 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00003215 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003216 ISD::isUNINDEXEDLoad(N0.Val) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003217 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003218 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003219 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3220 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3221 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003222 LN0->getSrcValueOffset(), EVT,
3223 LN0->isVolatile(),
3224 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003225 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003226 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003227 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003228 }
3229 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00003230 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
3231 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003232 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003233 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003234 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3235 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3236 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003237 LN0->getSrcValueOffset(), EVT,
3238 LN0->isVolatile(),
3239 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003240 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003241 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003242 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003243 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003244 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003245}
3246
Nate Begeman83e75ec2005-09-06 04:43:02 +00003247SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003248 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003249 MVT::ValueType VT = N->getValueType(0);
3250
3251 // noop truncate
3252 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003253 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003254 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003255 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003256 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003257 // fold (truncate (truncate x)) -> (truncate x)
3258 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003259 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003260 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003261 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3262 N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003263 if (N0.getOperand(0).getValueType() < VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003264 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003265 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003266 else if (N0.getOperand(0).getValueType() > VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003267 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003268 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003269 else
3270 // if the source and dest are the same type, we can drop both the extend
3271 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003272 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003273 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003274
Chris Lattner2b4c2792007-10-13 06:35:54 +00003275 // See if we can simplify the input to this truncate through knowledge that
3276 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3277 // -> trunc y
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003278 SDOperand Shorter =
3279 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
3280 MVT::getSizeInBits(VT)));
Chris Lattner2b4c2792007-10-13 06:35:54 +00003281 if (Shorter.Val)
3282 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3283
Nate Begeman3df4d522005-10-12 20:40:40 +00003284 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003285 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003286 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003287}
3288
Chris Lattner94683772005-12-23 05:30:37 +00003289SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3290 SDOperand N0 = N->getOperand(0);
3291 MVT::ValueType VT = N->getValueType(0);
3292
Dan Gohman7f321562007-06-25 16:23:39 +00003293 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3294 // Only do this before legalize, since afterward the target may be depending
3295 // on the bitconvert.
3296 // First check to see if this is all constant.
3297 if (!AfterLegalize &&
3298 N0.getOpcode() == ISD::BUILD_VECTOR && N0.Val->hasOneUse() &&
3299 MVT::isVector(VT)) {
3300 bool isSimple = true;
3301 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3302 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3303 N0.getOperand(i).getOpcode() != ISD::Constant &&
3304 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3305 isSimple = false;
3306 break;
3307 }
3308
3309 MVT::ValueType DestEltVT = MVT::getVectorElementType(N->getValueType(0));
3310 assert(!MVT::isVector(DestEltVT) &&
3311 "Element type of vector ValueType must not be vector!");
3312 if (isSimple) {
3313 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.Val, DestEltVT);
3314 }
3315 }
3316
Chris Lattner94683772005-12-23 05:30:37 +00003317 // If the input is a constant, let getNode() fold it.
3318 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
3319 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3320 if (Res.Val != N) return Res;
3321 }
3322
Chris Lattnerc8547d82005-12-23 05:37:50 +00003323 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3324 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003325
Chris Lattner57104102005-12-23 05:44:41 +00003326 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003327 // If the resultant load doesn't need a higher alignment than the original!
3328 if (ISD::isNormalLoad(N0.Val) && N0.hasOneUse() &&
Evan Cheng59d5b682007-05-07 21:27:48 +00003329 TLI.isOperationLegal(ISD::LOAD, VT)) {
Evan Cheng466685d2006-10-09 20:57:25 +00003330 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00003331 unsigned Align = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00003332 getABITypeAlignment(MVT::getTypeForValueType(VT));
Evan Cheng59d5b682007-05-07 21:27:48 +00003333 unsigned OrigAlign = LN0->getAlignment();
3334 if (Align <= OrigAlign) {
3335 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
3336 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003337 LN0->isVolatile(), Align);
Evan Cheng59d5b682007-05-07 21:27:48 +00003338 AddToWorkList(N);
3339 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
3340 Load.getValue(1));
3341 return Load;
3342 }
Chris Lattner57104102005-12-23 05:44:41 +00003343 }
3344
Chris Lattner3bd39d42008-01-27 17:42:27 +00003345 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3346 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3347 // This often reduces constant pool loads.
3348 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
3349 N0.Val->hasOneUse() && MVT::isInteger(VT) && !MVT::isVector(VT)) {
3350 SDOperand NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3351 AddToWorkList(NewConv.Val);
3352
3353 uint64_t SignBit = MVT::getIntVTSignBit(VT);
3354 if (N0.getOpcode() == ISD::FNEG)
3355 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3356 assert(N0.getOpcode() == ISD::FABS);
3357 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3358 }
3359
3360 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3361 // Note that we don't handle copysign(x,cst) because this can always be folded
3362 // to an fneg or fabs.
3363 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003364 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
3365 MVT::isInteger(VT) && !MVT::isVector(VT)) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003366 unsigned OrigXWidth = MVT::getSizeInBits(N0.getOperand(1).getValueType());
3367 SDOperand X = DAG.getNode(ISD::BIT_CONVERT, MVT::getIntegerType(OrigXWidth),
3368 N0.getOperand(1));
3369 AddToWorkList(X.Val);
3370
3371 // If X has a different width than the result/lhs, sext it or truncate it.
3372 unsigned VTWidth = MVT::getSizeInBits(VT);
3373 if (OrigXWidth < VTWidth) {
3374 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
3375 AddToWorkList(X.Val);
3376 } else if (OrigXWidth > VTWidth) {
3377 // To get the sign bit in the right place, we have to shift it right
3378 // before truncating.
3379 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3380 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3381 AddToWorkList(X.Val);
3382 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3383 AddToWorkList(X.Val);
3384 }
3385
3386 uint64_t SignBit = MVT::getIntVTSignBit(VT);
3387 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
3388 AddToWorkList(X.Val);
3389
3390 SDOperand Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3391 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
3392 AddToWorkList(Cst.Val);
3393
3394 return DAG.getNode(ISD::OR, VT, X, Cst);
3395 }
3396
Chris Lattner94683772005-12-23 05:30:37 +00003397 return SDOperand();
3398}
3399
Dan Gohman7f321562007-06-25 16:23:39 +00003400/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003401/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3402/// destination element value type.
3403SDOperand DAGCombiner::
Dan Gohman7f321562007-06-25 16:23:39 +00003404ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003405 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
3406
3407 // If this is already the right type, we're done.
3408 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
3409
3410 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
3411 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
3412
3413 // If this is a conversion of N elements of one type to N elements of another
3414 // type, convert each element. This handles FP<->INT cases.
3415 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003416 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003417 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003418 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00003419 AddToWorkList(Ops.back().Val);
3420 }
Dan Gohman7f321562007-06-25 16:23:39 +00003421 MVT::ValueType VT =
3422 MVT::getVectorType(DstEltVT,
3423 MVT::getVectorNumElements(BV->getValueType(0)));
3424 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003425 }
3426
3427 // Otherwise, we're growing or shrinking the elements. To avoid having to
3428 // handle annoying details of growing/shrinking FP values, we convert them to
3429 // int first.
3430 if (MVT::isFloatingPoint(SrcEltVT)) {
3431 // Convert the input float vector to a int vector where the elements are the
3432 // same sizes.
3433 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
3434 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003435 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003436 SrcEltVT = IntVT;
3437 }
3438
3439 // Now we know the input is an integer vector. If the output is a FP type,
3440 // convert to integer first, then to FP of the right size.
3441 if (MVT::isFloatingPoint(DstEltVT)) {
3442 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
3443 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003444 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003445
3446 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003447 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003448 }
3449
3450 // Okay, we know the src/dst types are both integers of differing types.
3451 // Handling growing first.
3452 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
3453 if (SrcBitSize < DstBitSize) {
3454 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3455
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003456 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003457 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003458 i += NumInputsPerOutput) {
3459 bool isLE = TLI.isLittleEndian();
3460 uint64_t NewBits = 0;
3461 bool EltIsUndef = true;
3462 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3463 // Shift the previously computed bits over.
3464 NewBits <<= SrcBitSize;
3465 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
3466 if (Op.getOpcode() == ISD::UNDEF) continue;
3467 EltIsUndef = false;
3468
3469 NewBits |= cast<ConstantSDNode>(Op)->getValue();
3470 }
3471
3472 if (EltIsUndef)
3473 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3474 else
3475 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3476 }
3477
Evan Chengefec7512008-02-18 23:04:32 +00003478 MVT::ValueType VT = MVT::getVectorType(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003479 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003480 }
3481
3482 // Finally, this must be the case where we are shrinking elements: each input
3483 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003484 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003485 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Evan Chengefec7512008-02-18 23:04:32 +00003486 MVT::ValueType VT = MVT::getVectorType(DstEltVT,
3487 NumOutputsPerInput * BV->getNumOperands());
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003488 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003489 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003490 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3491 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3492 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3493 continue;
3494 }
3495 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003496 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
3497 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
Chris Lattner6258fb22006-04-02 02:53:43 +00003498 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Evan Chengefec7512008-02-18 23:04:32 +00003499 if (isS2V && i == 0 && j == 0 && ThisVal == OpVal)
3500 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3501 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
3502 OpVal >>= DstBitSize;
Chris Lattner6258fb22006-04-02 02:53:43 +00003503 }
3504
3505 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003506 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003507 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3508 }
Dan Gohman7f321562007-06-25 16:23:39 +00003509 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003510}
3511
3512
3513
Chris Lattner01b3d732005-09-28 22:28:18 +00003514SDOperand DAGCombiner::visitFADD(SDNode *N) {
3515 SDOperand N0 = N->getOperand(0);
3516 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003517 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3518 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003519 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003520
Dan Gohman7f321562007-06-25 16:23:39 +00003521 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003522 if (MVT::isVector(VT)) {
3523 SDOperand FoldedVOp = SimplifyVBinOp(N);
3524 if (FoldedVOp.Val) return FoldedVOp;
3525 }
Dan Gohman7f321562007-06-25 16:23:39 +00003526
Nate Begemana0e221d2005-10-18 00:28:13 +00003527 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003528 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003529 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003530 // canonicalize constant to RHS
3531 if (N0CFP && !N1CFP)
3532 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003533 // fold (A + (-B)) -> A-B
Chris Lattner0254e702008-02-26 07:04:54 +00003534 if (isNegatibleForFree(N1, AfterLegalize) == 2)
3535 return DAG.getNode(ISD::FSUB, VT, N0,
3536 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner01b3d732005-09-28 22:28:18 +00003537 // fold ((-A) + B) -> B-A
Chris Lattner0254e702008-02-26 07:04:54 +00003538 if (isNegatibleForFree(N0, AfterLegalize) == 2)
3539 return DAG.getNode(ISD::FSUB, VT, N1,
3540 GetNegatedExpression(N0, DAG, AfterLegalize));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003541
3542 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3543 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
3544 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3545 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3546 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3547
Chris Lattner01b3d732005-09-28 22:28:18 +00003548 return SDOperand();
3549}
3550
3551SDOperand DAGCombiner::visitFSUB(SDNode *N) {
3552 SDOperand N0 = N->getOperand(0);
3553 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003554 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3555 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003556 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003557
Dan Gohman7f321562007-06-25 16:23:39 +00003558 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003559 if (MVT::isVector(VT)) {
3560 SDOperand FoldedVOp = SimplifyVBinOp(N);
3561 if (FoldedVOp.Val) return FoldedVOp;
3562 }
Dan Gohman7f321562007-06-25 16:23:39 +00003563
Nate Begemana0e221d2005-10-18 00:28:13 +00003564 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003565 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003566 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003567 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003568 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Chris Lattner0254e702008-02-26 07:04:54 +00003569 if (isNegatibleForFree(N1, AfterLegalize))
3570 return GetNegatedExpression(N1, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003571 return DAG.getNode(ISD::FNEG, VT, N1);
3572 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003573 // fold (A-(-B)) -> A+B
Chris Lattner0254e702008-02-26 07:04:54 +00003574 if (isNegatibleForFree(N1, AfterLegalize))
3575 return DAG.getNode(ISD::FADD, VT, N0,
3576 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003577
Chris Lattner01b3d732005-09-28 22:28:18 +00003578 return SDOperand();
3579}
3580
3581SDOperand DAGCombiner::visitFMUL(SDNode *N) {
3582 SDOperand N0 = N->getOperand(0);
3583 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003584 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3585 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003586 MVT::ValueType VT = N->getValueType(0);
3587
Dan Gohman7f321562007-06-25 16:23:39 +00003588 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003589 if (MVT::isVector(VT)) {
3590 SDOperand FoldedVOp = SimplifyVBinOp(N);
3591 if (FoldedVOp.Val) return FoldedVOp;
3592 }
Dan Gohman7f321562007-06-25 16:23:39 +00003593
Nate Begeman11af4ea2005-10-17 20:40:11 +00003594 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003595 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003596 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003597 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003598 if (N0CFP && !N1CFP)
3599 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003600 // fold (fmul X, 2.0) -> (fadd X, X)
3601 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3602 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003603 // fold (fmul X, -1.0) -> (fneg X)
3604 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3605 return DAG.getNode(ISD::FNEG, VT, N0);
3606
3607 // -X * -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003608 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3609 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003610 // Both can be negated for free, check to see if at least one is cheaper
3611 // negated.
3612 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003613 return DAG.getNode(ISD::FMUL, VT,
3614 GetNegatedExpression(N0, DAG, AfterLegalize),
3615 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003616 }
3617 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003618
3619 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3620 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
3621 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3622 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3623 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3624
Chris Lattner01b3d732005-09-28 22:28:18 +00003625 return SDOperand();
3626}
3627
3628SDOperand DAGCombiner::visitFDIV(SDNode *N) {
3629 SDOperand N0 = N->getOperand(0);
3630 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003631 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3632 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003633 MVT::ValueType VT = N->getValueType(0);
3634
Dan Gohman7f321562007-06-25 16:23:39 +00003635 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003636 if (MVT::isVector(VT)) {
3637 SDOperand FoldedVOp = SimplifyVBinOp(N);
3638 if (FoldedVOp.Val) return FoldedVOp;
3639 }
Dan Gohman7f321562007-06-25 16:23:39 +00003640
Nate Begemana148d982006-01-18 22:35:16 +00003641 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003642 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003643 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003644
3645
3646 // -X / -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003647 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3648 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003649 // Both can be negated for free, check to see if at least one is cheaper
3650 // negated.
3651 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003652 return DAG.getNode(ISD::FDIV, VT,
3653 GetNegatedExpression(N0, DAG, AfterLegalize),
3654 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003655 }
3656 }
3657
Chris Lattner01b3d732005-09-28 22:28:18 +00003658 return SDOperand();
3659}
3660
3661SDOperand DAGCombiner::visitFREM(SDNode *N) {
3662 SDOperand N0 = N->getOperand(0);
3663 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003664 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3665 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003666 MVT::ValueType VT = N->getValueType(0);
3667
Nate Begemana148d982006-01-18 22:35:16 +00003668 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003669 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003670 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003671
Chris Lattner01b3d732005-09-28 22:28:18 +00003672 return SDOperand();
3673}
3674
Chris Lattner12d83032006-03-05 05:30:57 +00003675SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3676 SDOperand N0 = N->getOperand(0);
3677 SDOperand N1 = N->getOperand(1);
3678 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3679 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3680 MVT::ValueType VT = N->getValueType(0);
3681
Dale Johannesendb44bf82007-10-16 23:38:29 +00003682 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003683 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3684
3685 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003686 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003687 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3688 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003689 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003690 return DAG.getNode(ISD::FABS, VT, N0);
3691 else
3692 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3693 }
3694
3695 // copysign(fabs(x), y) -> copysign(x, y)
3696 // copysign(fneg(x), y) -> copysign(x, y)
3697 // copysign(copysign(x,z), y) -> copysign(x, y)
3698 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3699 N0.getOpcode() == ISD::FCOPYSIGN)
3700 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3701
3702 // copysign(x, abs(y)) -> abs(x)
3703 if (N1.getOpcode() == ISD::FABS)
3704 return DAG.getNode(ISD::FABS, VT, N0);
3705
3706 // copysign(x, copysign(y,z)) -> copysign(x, z)
3707 if (N1.getOpcode() == ISD::FCOPYSIGN)
3708 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3709
3710 // copysign(x, fp_extend(y)) -> copysign(x, y)
3711 // copysign(x, fp_round(y)) -> copysign(x, y)
3712 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3713 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3714
3715 return SDOperand();
3716}
3717
3718
Chris Lattner01b3d732005-09-28 22:28:18 +00003719
Nate Begeman83e75ec2005-09-06 04:43:02 +00003720SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003721 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003722 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003723 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003724
3725 // fold (sint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003726 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003727 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003728 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003729}
3730
Nate Begeman83e75ec2005-09-06 04:43:02 +00003731SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003732 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003733 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003734 MVT::ValueType VT = N->getValueType(0);
3735
Nate Begeman1d4d4142005-09-01 00:19:25 +00003736 // fold (uint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003737 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003738 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003739 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003740}
3741
Nate Begeman83e75ec2005-09-06 04:43:02 +00003742SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003743 SDOperand N0 = N->getOperand(0);
3744 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3745 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003746
3747 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003748 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003749 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003750 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003751}
3752
Nate Begeman83e75ec2005-09-06 04:43:02 +00003753SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003754 SDOperand N0 = N->getOperand(0);
3755 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3756 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003757
3758 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00003759 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003760 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003761 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003762}
3763
Nate Begeman83e75ec2005-09-06 04:43:02 +00003764SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003765 SDOperand N0 = N->getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003766 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003767 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3768 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003769
3770 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003771 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00003772 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003773
3774 // fold (fp_round (fp_extend x)) -> x
3775 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3776 return N0.getOperand(0);
3777
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00003778 // fold (fp_round (fp_round x)) -> (fp_round x)
3779 if (N0.getOpcode() == ISD::FP_ROUND) {
3780 // This is a value preserving truncation if both round's are.
3781 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
3782 N0.Val->getConstantOperandVal(1) == 1;
3783 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
3784 DAG.getIntPtrConstant(IsTrunc));
3785 }
3786
Chris Lattner79dbea52006-03-13 06:26:26 +00003787 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3788 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003789 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003790 AddToWorkList(Tmp.Val);
3791 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3792 }
3793
Nate Begeman83e75ec2005-09-06 04:43:02 +00003794 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003795}
3796
Nate Begeman83e75ec2005-09-06 04:43:02 +00003797SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003798 SDOperand N0 = N->getOperand(0);
3799 MVT::ValueType VT = N->getValueType(0);
3800 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003801 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003802
Nate Begeman1d4d4142005-09-01 00:19:25 +00003803 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003804 if (N0CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003805 SDOperand Round = DAG.getConstantFP(N0CFP->getValueAPF(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003806 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003807 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003808 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003809}
3810
Nate Begeman83e75ec2005-09-06 04:43:02 +00003811SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003812 SDOperand N0 = N->getOperand(0);
3813 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3814 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003815
Chris Lattner5938bef2007-12-29 06:55:23 +00003816 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
3817 if (N->hasOneUse() && (*N->use_begin())->getOpcode() == ISD::FP_ROUND)
3818 return SDOperand();
Chris Lattner0bd48932008-01-17 07:00:52 +00003819
Nate Begeman1d4d4142005-09-01 00:19:25 +00003820 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003821 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003822 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003823
3824 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
3825 // value of X.
3826 if (N0.getOpcode() == ISD::FP_ROUND && N0.Val->getConstantOperandVal(1) == 1){
3827 SDOperand In = N0.getOperand(0);
3828 if (In.getValueType() == VT) return In;
3829 if (VT < In.getValueType())
3830 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
3831 return DAG.getNode(ISD::FP_EXTEND, VT, In);
3832 }
3833
3834 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Dale Johannesenb6210fc2007-10-19 20:29:00 +00003835 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003836 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003837 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3838 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3839 LN0->getBasePtr(), LN0->getSrcValue(),
3840 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003841 N0.getValueType(),
3842 LN0->isVolatile(),
3843 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003844 CombineTo(N, ExtLoad);
Chris Lattner0bd48932008-01-17 07:00:52 +00003845 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad,
3846 DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00003847 ExtLoad.getValue(1));
3848 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3849 }
3850
3851
Nate Begeman83e75ec2005-09-06 04:43:02 +00003852 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003853}
3854
Nate Begeman83e75ec2005-09-06 04:43:02 +00003855SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003856 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003857
Chris Lattner0254e702008-02-26 07:04:54 +00003858 if (isNegatibleForFree(N0, AfterLegalize))
3859 return GetNegatedExpression(N0, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003860
Chris Lattner3bd39d42008-01-27 17:42:27 +00003861 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
3862 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00003863 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
3864 MVT::isInteger(N0.getOperand(0).getValueType()) &&
3865 !MVT::isVector(N0.getOperand(0).getValueType())) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003866 SDOperand Int = N0.getOperand(0);
3867 MVT::ValueType IntVT = Int.getValueType();
3868 if (MVT::isInteger(IntVT) && !MVT::isVector(IntVT)) {
3869 Int = DAG.getNode(ISD::XOR, IntVT, Int,
3870 DAG.getConstant(MVT::getIntVTSignBit(IntVT), IntVT));
3871 AddToWorkList(Int.Val);
3872 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
3873 }
3874 }
3875
Nate Begeman83e75ec2005-09-06 04:43:02 +00003876 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003877}
3878
Nate Begeman83e75ec2005-09-06 04:43:02 +00003879SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003880 SDOperand N0 = N->getOperand(0);
3881 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3882 MVT::ValueType VT = N->getValueType(0);
3883
Nate Begeman1d4d4142005-09-01 00:19:25 +00003884 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003885 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003886 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003887 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003888 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003889 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003890 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003891 // fold (fabs (fcopysign x, y)) -> (fabs x)
3892 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
3893 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
3894
Chris Lattner3bd39d42008-01-27 17:42:27 +00003895 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
3896 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00003897 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
3898 MVT::isInteger(N0.getOperand(0).getValueType()) &&
3899 !MVT::isVector(N0.getOperand(0).getValueType())) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003900 SDOperand Int = N0.getOperand(0);
3901 MVT::ValueType IntVT = Int.getValueType();
3902 if (MVT::isInteger(IntVT) && !MVT::isVector(IntVT)) {
3903 Int = DAG.getNode(ISD::AND, IntVT, Int,
3904 DAG.getConstant(~MVT::getIntVTSignBit(IntVT), IntVT));
3905 AddToWorkList(Int.Val);
3906 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
3907 }
3908 }
3909
Nate Begeman83e75ec2005-09-06 04:43:02 +00003910 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003911}
3912
Nate Begeman44728a72005-09-19 22:34:01 +00003913SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
3914 SDOperand Chain = N->getOperand(0);
3915 SDOperand N1 = N->getOperand(1);
3916 SDOperand N2 = N->getOperand(2);
3917 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3918
3919 // never taken branch, fold to chain
3920 if (N1C && N1C->isNullValue())
3921 return Chain;
3922 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00003923 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00003924 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003925 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
3926 // on the target.
3927 if (N1.getOpcode() == ISD::SETCC &&
3928 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
3929 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
3930 N1.getOperand(0), N1.getOperand(1), N2);
3931 }
Nate Begeman44728a72005-09-19 22:34:01 +00003932 return SDOperand();
3933}
3934
Chris Lattner3ea0b472005-10-05 06:47:48 +00003935// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
3936//
Nate Begeman44728a72005-09-19 22:34:01 +00003937SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00003938 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
3939 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
3940
3941 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00003942 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003943 if (Simp.Val) AddToWorkList(Simp.Val);
3944
Nate Begemane17daeb2005-10-05 21:43:42 +00003945 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
3946
3947 // fold br_cc true, dest -> br dest (unconditional branch)
3948 if (SCCC && SCCC->getValue())
3949 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
3950 N->getOperand(4));
3951 // fold br_cc false, dest -> unconditional fall through
3952 if (SCCC && SCCC->isNullValue())
3953 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00003954
Nate Begemane17daeb2005-10-05 21:43:42 +00003955 // fold to a simpler setcc
3956 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
3957 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
3958 Simp.getOperand(2), Simp.getOperand(0),
3959 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00003960 return SDOperand();
3961}
3962
Chris Lattner448f2192006-11-11 00:39:41 +00003963
3964/// CombineToPreIndexedLoadStore - Try turning a load / store and a
3965/// pre-indexed load / store when the base pointer is a add or subtract
3966/// and it has other uses besides the load / store. After the
3967/// transformation, the new indexed load / store has effectively folded
3968/// the add / subtract in and all of its other uses are redirected to the
3969/// new load / store.
3970bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
3971 if (!AfterLegalize)
3972 return false;
3973
3974 bool isLoad = true;
3975 SDOperand Ptr;
3976 MVT::ValueType VT;
3977 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00003978 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00003979 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003980 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00003981 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00003982 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
3983 return false;
3984 Ptr = LD->getBasePtr();
3985 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00003986 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00003987 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003988 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00003989 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
3990 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
3991 return false;
3992 Ptr = ST->getBasePtr();
3993 isLoad = false;
3994 } else
3995 return false;
3996
Chris Lattner9f1794e2006-11-11 00:56:29 +00003997 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
3998 // out. There is no reason to make this a preinc/predec.
3999 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
4000 Ptr.Val->hasOneUse())
4001 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004002
Chris Lattner9f1794e2006-11-11 00:56:29 +00004003 // Ask the target to do addressing mode selection.
4004 SDOperand BasePtr;
4005 SDOperand Offset;
4006 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4007 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4008 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004009 // Don't create a indexed load / store with zero offset.
4010 if (isa<ConstantSDNode>(Offset) &&
4011 cast<ConstantSDNode>(Offset)->getValue() == 0)
4012 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004013
Chris Lattner41e53fd2006-11-11 01:00:15 +00004014 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004015 // 1) The new base ptr is a frame index.
4016 // 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 +00004017 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004018 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004019 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004020 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004021
Chris Lattner41e53fd2006-11-11 01:00:15 +00004022 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4023 // (plus the implicit offset) to a register to preinc anyway.
4024 if (isa<FrameIndexSDNode>(BasePtr))
4025 return false;
4026
4027 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004028 if (!isLoad) {
4029 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Chengc843abe2007-05-24 02:35:39 +00004030 if (Val == BasePtr || BasePtr.Val->isPredecessor(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004031 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004032 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004033
Evan Chengc843abe2007-05-24 02:35:39 +00004034 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004035 bool RealUse = false;
4036 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4037 E = Ptr.Val->use_end(); I != E; ++I) {
4038 SDNode *Use = *I;
4039 if (Use == N)
4040 continue;
4041 if (Use->isPredecessor(N))
4042 return false;
4043
4044 if (!((Use->getOpcode() == ISD::LOAD &&
4045 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004046 (Use->getOpcode() == ISD::STORE &&
4047 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004048 RealUse = true;
4049 }
4050 if (!RealUse)
4051 return false;
4052
4053 SDOperand Result;
4054 if (isLoad)
4055 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
4056 else
4057 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4058 ++PreIndexedNodes;
4059 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004060 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004061 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4062 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004063 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004064 if (isLoad) {
4065 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004066 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004067 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004068 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004069 } else {
4070 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004071 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004072 }
4073
Chris Lattner9f1794e2006-11-11 00:56:29 +00004074 // Finally, since the node is now dead, remove it from the graph.
4075 DAG.DeleteNode(N);
4076
4077 // Replace the uses of Ptr with uses of the updated base value.
4078 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004079 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004080 removeFromWorkList(Ptr.Val);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004081 DAG.DeleteNode(Ptr.Val);
4082
4083 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004084}
4085
4086/// CombineToPostIndexedLoadStore - Try combine a load / store with a
4087/// add / sub of the base pointer node into a post-indexed load / store.
4088/// The transformation folded the add / subtract into the new indexed
4089/// load / store effectively and all of its uses are redirected to the
4090/// new load / store.
4091bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
4092 if (!AfterLegalize)
4093 return false;
4094
4095 bool isLoad = true;
4096 SDOperand Ptr;
4097 MVT::ValueType VT;
4098 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004099 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004100 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004101 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004102 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4103 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4104 return false;
4105 Ptr = LD->getBasePtr();
4106 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004107 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004108 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004109 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004110 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4111 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4112 return false;
4113 Ptr = ST->getBasePtr();
4114 isLoad = false;
4115 } else
4116 return false;
4117
Evan Chengcc470212006-11-16 00:08:20 +00004118 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004119 return false;
4120
4121 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4122 E = Ptr.Val->use_end(); I != E; ++I) {
4123 SDNode *Op = *I;
4124 if (Op == N ||
4125 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4126 continue;
4127
4128 SDOperand BasePtr;
4129 SDOperand Offset;
4130 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4131 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4132 if (Ptr == Offset)
4133 std::swap(BasePtr, Offset);
4134 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004135 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004136 // Don't create a indexed load / store with zero offset.
4137 if (isa<ConstantSDNode>(Offset) &&
4138 cast<ConstantSDNode>(Offset)->getValue() == 0)
4139 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004140
Chris Lattner9f1794e2006-11-11 00:56:29 +00004141 // Try turning it into a post-indexed load / store except when
4142 // 1) All uses are load / store ops that use it as base ptr.
4143 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4144 // nor a successor of N. Otherwise, if Op is folded that would
4145 // create a cycle.
4146
4147 // Check for #1.
4148 bool TryNext = false;
4149 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
4150 EE = BasePtr.Val->use_end(); II != EE; ++II) {
4151 SDNode *Use = *II;
4152 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00004153 continue;
4154
Chris Lattner9f1794e2006-11-11 00:56:29 +00004155 // If all the uses are load / store addresses, then don't do the
4156 // transformation.
4157 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4158 bool RealUse = false;
4159 for (SDNode::use_iterator III = Use->use_begin(),
4160 EEE = Use->use_end(); III != EEE; ++III) {
4161 SDNode *UseUse = *III;
4162 if (!((UseUse->getOpcode() == ISD::LOAD &&
4163 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004164 (UseUse->getOpcode() == ISD::STORE &&
4165 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004166 RealUse = true;
4167 }
Chris Lattner448f2192006-11-11 00:39:41 +00004168
Chris Lattner9f1794e2006-11-11 00:56:29 +00004169 if (!RealUse) {
4170 TryNext = true;
4171 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004172 }
4173 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004174 }
4175 if (TryNext)
4176 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004177
Chris Lattner9f1794e2006-11-11 00:56:29 +00004178 // Check for #2
4179 if (!Op->isPredecessor(N) && !N->isPredecessor(Op)) {
4180 SDOperand Result = isLoad
4181 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
4182 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4183 ++PostIndexedNodes;
4184 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004185 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004186 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4187 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004188 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004189 if (isLoad) {
4190 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004191 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004192 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004193 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004194 } else {
4195 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004196 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004197 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004198
Chris Lattner9f1794e2006-11-11 00:56:29 +00004199 // Finally, since the node is now dead, remove it from the graph.
4200 DAG.DeleteNode(N);
4201
4202 // Replace the uses of Use with uses of the updated base value.
4203 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
4204 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004205 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004206 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004207 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004208 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004209 }
4210 }
4211 }
4212 return false;
4213}
4214
Chris Lattner00161a62008-01-25 07:20:16 +00004215/// InferAlignment - If we can infer some alignment information from this
4216/// pointer, return it.
4217static unsigned InferAlignment(SDOperand Ptr, SelectionDAG &DAG) {
4218 // If this is a direct reference to a stack slot, use information about the
4219 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004220 int FrameIdx = 1 << 31;
4221 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004222 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004223 FrameIdx = FI->getIndex();
4224 } else if (Ptr.getOpcode() == ISD::ADD &&
4225 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4226 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4227 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4228 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004229 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004230
4231 if (FrameIdx != (1 << 31)) {
4232 // FIXME: Handle FI+CST.
4233 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4234 if (MFI.isFixedObjectIndex(FrameIdx)) {
4235 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx);
4236
4237 // The alignment of the frame index can be determined from its offset from
4238 // the incoming frame position. If the frame object is at offset 32 and
4239 // the stack is guaranteed to be 16-byte aligned, then we know that the
4240 // object is 16-byte aligned.
4241 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4242 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4243
4244 // Finally, the frame object itself may have a known alignment. Factor
4245 // the alignment + offset into a new alignment. For example, if we know
4246 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4247 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4248 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4249 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4250 FrameOffset);
4251 return std::max(Align, FIInfoAlign);
4252 }
4253 }
Chris Lattner00161a62008-01-25 07:20:16 +00004254
4255 return 0;
4256}
Chris Lattner448f2192006-11-11 00:39:41 +00004257
Chris Lattner01a22022005-10-10 22:04:48 +00004258SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004259 LoadSDNode *LD = cast<LoadSDNode>(N);
4260 SDOperand Chain = LD->getChain();
4261 SDOperand Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004262
4263 // Try to infer better alignment information than the load already has.
4264 if (LD->isUnindexed()) {
4265 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4266 if (Align > LD->getAlignment())
4267 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4268 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004269 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004270 LD->isVolatile(), Align);
4271 }
4272 }
4273
Evan Cheng45a7ca92007-05-01 00:38:21 +00004274
4275 // If load is not volatile and there are no uses of the loaded value (and
4276 // the updated indexed value in case of indexed loads), change uses of the
4277 // chain value into uses of the chain input (i.e. delete the dead load).
4278 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004279 if (N->getValueType(1) == MVT::Other) {
4280 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004281 if (N->hasNUsesOfValue(0, 0)) {
4282 // It's not safe to use the two value CombineTo variant here. e.g.
4283 // v1, chain2 = load chain1, loc
4284 // v2, chain3 = load chain2, loc
4285 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004286 // Now we replace use of chain2 with chain1. This makes the second load
4287 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004288 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004289 DOUT << "\nWith chain: "; DEBUG(Chain.Val->dump(&DAG));
4290 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004291 WorkListRemover DeadNodes(*this);
4292 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004293 if (N->use_empty()) {
4294 removeFromWorkList(N);
4295 DAG.DeleteNode(N);
4296 }
Evan Cheng02c42852008-01-16 23:11:54 +00004297 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
4298 }
Evan Cheng498f5592007-05-01 08:53:39 +00004299 } else {
4300 // Indexed loads.
4301 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4302 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Evan Cheng02c42852008-01-16 23:11:54 +00004303 SDOperand Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
4304 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
4305 DOUT << "\nWith: "; DEBUG(Undef.Val->dump(&DAG));
4306 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004307 WorkListRemover DeadNodes(*this);
4308 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Undef, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004309 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004310 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004311 &DeadNodes);
4312 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004313 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004314 DAG.DeleteNode(N);
4315 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004316 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004317 }
4318 }
Chris Lattner01a22022005-10-10 22:04:48 +00004319
4320 // If this load is directly stored, replace the load value with the stored
4321 // value.
4322 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004323 // TODO: Handle TRUNCSTORE/LOADEXT
4324 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004325 if (ISD::isNON_TRUNCStore(Chain.Val)) {
4326 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4327 if (PrevST->getBasePtr() == Ptr &&
4328 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004329 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004330 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004331 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004332
Jim Laskey7ca56af2006-10-11 13:47:09 +00004333 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004334 // Walk up chain skipping non-aliasing memory nodes.
4335 SDOperand BetterChain = FindBetterChain(N, Chain);
4336
Jim Laskey6ff23e52006-10-04 16:53:27 +00004337 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004338 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004339 SDOperand ReplLoad;
4340
Jim Laskey279f0532006-09-25 16:29:54 +00004341 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004342 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4343 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004344 LD->getSrcValue(), LD->getSrcValueOffset(),
4345 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004346 } else {
4347 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4348 LD->getValueType(0),
4349 BetterChain, Ptr, LD->getSrcValue(),
4350 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004351 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004352 LD->isVolatile(),
4353 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004354 }
Jim Laskey279f0532006-09-25 16:29:54 +00004355
Jim Laskey6ff23e52006-10-04 16:53:27 +00004356 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00004357 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
4358 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004359
Jim Laskey274062c2006-10-13 23:32:28 +00004360 // Replace uses with load result and token factor. Don't add users
4361 // to work list.
4362 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004363 }
4364 }
4365
Evan Cheng7fc033a2006-11-03 03:06:21 +00004366 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004367 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00004368 return SDOperand(N, 0);
4369
Chris Lattner01a22022005-10-10 22:04:48 +00004370 return SDOperand();
4371}
4372
Chris Lattner07649d92008-01-08 23:08:06 +00004373
Chris Lattner87514ca2005-10-10 22:31:19 +00004374SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004375 StoreSDNode *ST = cast<StoreSDNode>(N);
4376 SDOperand Chain = ST->getChain();
4377 SDOperand Value = ST->getValue();
4378 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004379
Chris Lattner00161a62008-01-25 07:20:16 +00004380 // Try to infer better alignment information than the store already has.
4381 if (ST->isUnindexed()) {
4382 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4383 if (Align > ST->getAlignment())
4384 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004385 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004386 ST->isVolatile(), Align);
4387 }
4388 }
4389
Evan Cheng59d5b682007-05-07 21:27:48 +00004390 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004391 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004392 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004393 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004394 unsigned Align = ST->getAlignment();
4395 MVT::ValueType SVT = Value.getOperand(0).getValueType();
4396 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00004397 getABITypeAlignment(MVT::getTypeForValueType(SVT));
Evan Chengc2cd2b22007-05-07 21:36:06 +00004398 if (Align <= OrigAlign && TLI.isOperationLegal(ISD::STORE, SVT))
Evan Cheng59d5b682007-05-07 21:27:48 +00004399 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004400 ST->getSrcValueOffset(), ST->isVolatile(), Align);
Jim Laskey279f0532006-09-25 16:29:54 +00004401 }
4402
Nate Begeman2cbba892006-12-11 02:23:46 +00004403 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004404 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Evan Cheng25ece662006-12-11 17:25:19 +00004405 if (Value.getOpcode() != ISD::TargetConstantFP) {
4406 SDOperand Tmp;
Chris Lattner62be1a72006-12-12 04:16:14 +00004407 switch (CFP->getValueType(0)) {
4408 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004409 case MVT::f80: // We don't do this for these yet.
4410 case MVT::f128:
4411 case MVT::ppcf128:
4412 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004413 case MVT::f32:
4414 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004415 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
4416 convertToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004417 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004418 ST->getSrcValueOffset(), ST->isVolatile(),
4419 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004420 }
4421 break;
4422 case MVT::f64:
4423 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004424 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
4425 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004426 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004427 ST->getSrcValueOffset(), ST->isVolatile(),
4428 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004429 } else if (TLI.isTypeLegal(MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004430 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004431 // argument passing. Since this is so common, custom legalize the
4432 // 64-bit integer store into two 32-bit stores.
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004433 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
Chris Lattner62be1a72006-12-12 04:16:14 +00004434 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4435 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004436 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004437
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004438 int SVOffset = ST->getSrcValueOffset();
4439 unsigned Alignment = ST->getAlignment();
4440 bool isVolatile = ST->isVolatile();
4441
Chris Lattner62be1a72006-12-12 04:16:14 +00004442 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004443 ST->getSrcValueOffset(),
4444 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004445 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4446 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004447 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004448 Alignment = MinAlign(Alignment, 4U);
Chris Lattner62be1a72006-12-12 04:16:14 +00004449 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004450 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004451 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4452 }
4453 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004454 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004455 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004456 }
4457
Jim Laskey279f0532006-09-25 16:29:54 +00004458 if (CombinerAA) {
4459 // Walk up chain skipping non-aliasing memory nodes.
4460 SDOperand BetterChain = FindBetterChain(N, Chain);
4461
Jim Laskey6ff23e52006-10-04 16:53:27 +00004462 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004463 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004464 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004465 SDOperand ReplStore;
4466 if (ST->isTruncatingStore()) {
4467 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004468 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004469 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004470 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004471 } else {
4472 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004473 ST->getSrcValue(), ST->getSrcValueOffset(),
4474 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004475 }
4476
Jim Laskey279f0532006-09-25 16:29:54 +00004477 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00004478 SDOperand Token =
4479 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4480
4481 // Don't add users to work list.
4482 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004483 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004484 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004485
Evan Cheng33dbedc2006-11-05 09:31:14 +00004486 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004487 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00004488 return SDOperand(N, 0);
4489
Chris Lattner3c872852007-12-29 06:26:16 +00004490 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004491 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Chris Lattner2b4c2792007-10-13 06:35:54 +00004492 MVT::isInteger(Value.getValueType())) {
4493 // See if we can simplify the input to this truncstore with knowledge that
4494 // only the low bits are being used. For example:
4495 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
4496 SDOperand Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004497 GetDemandedBits(Value,
4498 APInt::getLowBitsSet(Value.getValueSizeInBits(),
4499 MVT::getSizeInBits(ST->getMemoryVT())));
Chris Lattner2b4c2792007-10-13 06:35:54 +00004500 AddToWorkList(Value.Val);
4501 if (Shorter.Val)
4502 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004503 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004504 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004505
4506 // Otherwise, see if we can simplify the operation with
4507 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004508 if (SimplifyDemandedBits(Value,
4509 APInt::getLowBitsSet(
4510 Value.getValueSizeInBits(),
4511 MVT::getSizeInBits(ST->getMemoryVT()))))
Chris Lattnere33544c2007-10-13 06:58:48 +00004512 return SDOperand(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004513 }
4514
Chris Lattner3c872852007-12-29 06:26:16 +00004515 // If this is a load followed by a store to the same location, then the store
4516 // is dead/noop.
4517 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004518 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004519 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004520 // There can't be any side effects between the load and store, such as
4521 // a call or store.
Chris Lattner572dee72008-01-16 05:49:24 +00004522 Chain.reachesChainWithoutSideEffects(SDOperand(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004523 // The store is dead, remove it.
4524 return Chain;
4525 }
4526 }
4527
Chris Lattnerddf89562008-01-17 19:59:44 +00004528 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4529 // truncating store. We can do this even if this is already a truncstore.
4530 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
4531 && TLI.isTypeLegal(Value.getOperand(0).getValueType()) &&
4532 Value.Val->hasOneUse() && ST->isUnindexed() &&
4533 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004534 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004535 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004536 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004537 ST->isVolatile(), ST->getAlignment());
4538 }
4539
Chris Lattner87514ca2005-10-10 22:31:19 +00004540 return SDOperand();
4541}
4542
Chris Lattnerca242442006-03-19 01:27:56 +00004543SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4544 SDOperand InVec = N->getOperand(0);
4545 SDOperand InVal = N->getOperand(1);
4546 SDOperand EltNo = N->getOperand(2);
4547
4548 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4549 // vector with the inserted element.
4550 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
4551 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004552 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004553 if (Elt < Ops.size())
4554 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004555 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4556 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004557 }
4558
4559 return SDOperand();
4560}
4561
Evan Cheng513da432007-10-06 08:19:55 +00004562SDOperand DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
4563 SDOperand InVec = N->getOperand(0);
4564 SDOperand EltNo = N->getOperand(1);
4565
4566 // (vextract (v4f32 s2v (f32 load $addr)), 0) -> (f32 load $addr)
4567 // (vextract (v4i32 bc (v4f32 s2v (f32 load $addr))), 0) -> (i32 load $addr)
4568 if (isa<ConstantSDNode>(EltNo)) {
4569 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
4570 bool NewLoad = false;
4571 if (Elt == 0) {
4572 MVT::ValueType VT = InVec.getValueType();
4573 MVT::ValueType EVT = MVT::getVectorElementType(VT);
4574 MVT::ValueType LVT = EVT;
4575 unsigned NumElts = MVT::getVectorNumElements(VT);
4576 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
4577 MVT::ValueType BCVT = InVec.getOperand(0).getValueType();
Dan Gohman090b38a2007-10-29 20:44:42 +00004578 if (!MVT::isVector(BCVT) ||
4579 NumElts != MVT::getVectorNumElements(BCVT))
Evan Cheng513da432007-10-06 08:19:55 +00004580 return SDOperand();
4581 InVec = InVec.getOperand(0);
4582 EVT = MVT::getVectorElementType(BCVT);
4583 NewLoad = true;
4584 }
4585 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4586 InVec.getOperand(0).getValueType() == EVT &&
4587 ISD::isNormalLoad(InVec.getOperand(0).Val) &&
4588 InVec.getOperand(0).hasOneUse()) {
4589 LoadSDNode *LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4590 unsigned Align = LN0->getAlignment();
4591 if (NewLoad) {
4592 // Check the resultant load doesn't need a higher alignment than the
4593 // original load.
4594 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
4595 getABITypeAlignment(MVT::getTypeForValueType(LVT));
4596 if (!TLI.isOperationLegal(ISD::LOAD, LVT) || NewAlign > Align)
4597 return SDOperand();
4598 Align = NewAlign;
4599 }
4600
4601 return DAG.getLoad(LVT, LN0->getChain(), LN0->getBasePtr(),
4602 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4603 LN0->isVolatile(), Align);
4604 }
4605 }
4606 }
4607 return SDOperand();
4608}
4609
4610
Dan Gohman7f321562007-06-25 16:23:39 +00004611SDOperand DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
4612 unsigned NumInScalars = N->getNumOperands();
4613 MVT::ValueType VT = N->getValueType(0);
4614 unsigned NumElts = MVT::getVectorNumElements(VT);
4615 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattnerca242442006-03-19 01:27:56 +00004616
Dan Gohman7f321562007-06-25 16:23:39 +00004617 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4618 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4619 // at most two distinct vectors, turn this into a shuffle node.
Chris Lattnerd7648c82006-03-28 20:28:38 +00004620 SDOperand VecIn1, VecIn2;
4621 for (unsigned i = 0; i != NumInScalars; ++i) {
4622 // Ignore undef inputs.
4623 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4624
Dan Gohman7f321562007-06-25 16:23:39 +00004625 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004626 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004627 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004628 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
4629 VecIn1 = VecIn2 = SDOperand(0, 0);
4630 break;
4631 }
4632
Dan Gohman7f321562007-06-25 16:23:39 +00004633 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004634 // we can't make a shuffle.
4635 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004636 if (ExtractedFromVec.getValueType() != VT) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004637 VecIn1 = VecIn2 = SDOperand(0, 0);
4638 break;
4639 }
4640
4641 // Otherwise, remember this. We allow up to two distinct input vectors.
4642 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4643 continue;
4644
4645 if (VecIn1.Val == 0) {
4646 VecIn1 = ExtractedFromVec;
4647 } else if (VecIn2.Val == 0) {
4648 VecIn2 = ExtractedFromVec;
4649 } else {
4650 // Too many inputs.
4651 VecIn1 = VecIn2 = SDOperand(0, 0);
4652 break;
4653 }
4654 }
4655
4656 // If everything is good, we can make a shuffle operation.
4657 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004658 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004659 for (unsigned i = 0; i != NumInScalars; ++i) {
4660 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004661 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004662 continue;
4663 }
4664
4665 SDOperand Extract = N->getOperand(i);
4666
4667 // If extracting from the first vector, just use the index directly.
4668 if (Extract.getOperand(0) == VecIn1) {
4669 BuildVecIndices.push_back(Extract.getOperand(1));
4670 continue;
4671 }
4672
4673 // Otherwise, use InIdx + VecSize
4674 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004675 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004676 }
4677
4678 // Add count and size info.
Chris Lattner0bd48932008-01-17 07:00:52 +00004679 MVT::ValueType BuildVecVT = MVT::getVectorType(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004680
Dan Gohman7f321562007-06-25 16:23:39 +00004681 // Return the new VECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004682 SDOperand Ops[5];
4683 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00004684 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004685 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004686 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004687 // Use an undef build_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00004688 std::vector<SDOperand> UnOps(NumInScalars,
4689 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004690 EltType));
4691 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004692 &UnOps[0], UnOps.size());
4693 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00004694 }
Dan Gohman7f321562007-06-25 16:23:39 +00004695 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004696 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004697 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004698 }
4699
4700 return SDOperand();
4701}
4702
Dan Gohman7f321562007-06-25 16:23:39 +00004703SDOperand DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
4704 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
4705 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
4706 // inputs come from at most two distinct vectors, turn this into a shuffle
4707 // node.
4708
4709 // If we only have one input vector, we don't need to do any concatenation.
4710 if (N->getNumOperands() == 1) {
4711 return N->getOperand(0);
4712 }
4713
4714 return SDOperand();
4715}
4716
Chris Lattner66445d32006-03-28 22:11:53 +00004717SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004718 SDOperand ShufMask = N->getOperand(2);
4719 unsigned NumElts = ShufMask.getNumOperands();
4720
4721 // If the shuffle mask is an identity operation on the LHS, return the LHS.
4722 bool isIdentity = true;
4723 for (unsigned i = 0; i != NumElts; ++i) {
4724 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4725 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
4726 isIdentity = false;
4727 break;
4728 }
4729 }
4730 if (isIdentity) return N->getOperand(0);
4731
4732 // If the shuffle mask is an identity operation on the RHS, return the RHS.
4733 isIdentity = true;
4734 for (unsigned i = 0; i != NumElts; ++i) {
4735 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4736 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
4737 isIdentity = false;
4738 break;
4739 }
4740 }
4741 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004742
4743 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
4744 // needed at all.
4745 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00004746 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004747 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00004748 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004749 for (unsigned i = 0; i != NumElts; ++i)
4750 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
4751 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
4752 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00004753 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004754 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004755 BaseIdx = Idx;
4756 } else {
4757 if (BaseIdx != Idx)
4758 isSplat = false;
4759 if (VecNum != V) {
4760 isUnary = false;
4761 break;
4762 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004763 }
4764 }
4765
4766 SDOperand N0 = N->getOperand(0);
4767 SDOperand N1 = N->getOperand(1);
4768 // Normalize unary shuffle so the RHS is undef.
4769 if (isUnary && VecNum == 1)
4770 std::swap(N0, N1);
4771
Evan Cheng917ec982006-07-21 08:25:53 +00004772 // If it is a splat, check if the argument vector is a build_vector with
4773 // all scalar elements the same.
4774 if (isSplat) {
4775 SDNode *V = N0.Val;
Evan Cheng917ec982006-07-21 08:25:53 +00004776
Dan Gohman7f321562007-06-25 16:23:39 +00004777 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004778 // not the number of vector elements, look through it. Be careful not to
4779 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004780 if (V->getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng59569222006-10-16 22:49:37 +00004781 SDOperand ConvInput = V->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004782 if (MVT::getVectorNumElements(ConvInput.getValueType()) == NumElts)
Evan Cheng59569222006-10-16 22:49:37 +00004783 V = ConvInput.Val;
4784 }
4785
Dan Gohman7f321562007-06-25 16:23:39 +00004786 if (V->getOpcode() == ISD::BUILD_VECTOR) {
4787 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00004788 if (NumElems > BaseIdx) {
4789 SDOperand Base;
4790 bool AllSame = true;
4791 for (unsigned i = 0; i != NumElems; ++i) {
4792 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4793 Base = V->getOperand(i);
4794 break;
4795 }
4796 }
4797 // Splat of <u, u, u, u>, return <u, u, u, u>
4798 if (!Base.Val)
4799 return N0;
4800 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00004801 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00004802 AllSame = false;
4803 break;
4804 }
4805 }
4806 // Splat of <x, x, x, x>, return <x, x, x, x>
4807 if (AllSame)
4808 return N0;
4809 }
4810 }
4811 }
4812
Evan Chenge7bec0d2006-07-20 22:44:41 +00004813 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4814 // into an undef.
4815 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004816 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4817 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004818 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004819 for (unsigned i = 0; i != NumElts; ++i) {
4820 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4821 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4822 MappedOps.push_back(ShufMask.getOperand(i));
4823 } else {
4824 unsigned NewIdx =
4825 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
4826 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
4827 }
4828 }
Dan Gohman7f321562007-06-25 16:23:39 +00004829 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004830 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004831 AddToWorkList(ShufMask.Val);
Dan Gohman7f321562007-06-25 16:23:39 +00004832 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
4833 N0,
4834 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
4835 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00004836 }
Dan Gohman7f321562007-06-25 16:23:39 +00004837
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004838 return SDOperand();
4839}
4840
Evan Cheng44f1f092006-04-20 08:56:16 +00004841/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00004842/// an AND to a vector_shuffle with the destination vector and a zero vector.
4843/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00004844/// vector_shuffle V, Zero, <0, 4, 2, 4>
4845SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
4846 SDOperand LHS = N->getOperand(0);
4847 SDOperand RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00004848 if (N->getOpcode() == ISD::AND) {
4849 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00004850 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004851 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Evan Cheng44f1f092006-04-20 08:56:16 +00004852 std::vector<SDOperand> IdxOps;
4853 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00004854 unsigned NumElts = NumOps;
4855 MVT::ValueType EVT = MVT::getVectorElementType(RHS.getValueType());
Evan Cheng44f1f092006-04-20 08:56:16 +00004856 for (unsigned i = 0; i != NumElts; ++i) {
4857 SDOperand Elt = RHS.getOperand(i);
4858 if (!isa<ConstantSDNode>(Elt))
4859 return SDOperand();
4860 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
4861 IdxOps.push_back(DAG.getConstant(i, EVT));
4862 else if (cast<ConstantSDNode>(Elt)->isNullValue())
4863 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
4864 else
4865 return SDOperand();
4866 }
4867
4868 // Let's see if the target supports this vector_shuffle.
4869 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
4870 return SDOperand();
4871
Dan Gohman7f321562007-06-25 16:23:39 +00004872 // Return the new VECTOR_SHUFFLE node.
4873 MVT::ValueType VT = MVT::getVectorType(EVT, NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00004874 std::vector<SDOperand> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004875 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00004876 Ops.push_back(LHS);
4877 AddToWorkList(LHS.Val);
4878 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00004879 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004880 &ZeroOps[0], ZeroOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004881 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004882 &IdxOps[0], IdxOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004883 SDOperand Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004884 &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004885 if (VT != LHS.getValueType()) {
4886 Result = DAG.getNode(ISD::BIT_CONVERT, LHS.getValueType(), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00004887 }
4888 return Result;
4889 }
4890 }
4891 return SDOperand();
4892}
4893
Dan Gohman7f321562007-06-25 16:23:39 +00004894/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
4895SDOperand DAGCombiner::SimplifyVBinOp(SDNode *N) {
4896 // After legalize, the target may be depending on adds and other
4897 // binary ops to provide legal ways to construct constants or other
4898 // things. Simplifying them may result in a loss of legality.
4899 if (AfterLegalize) return SDOperand();
4900
4901 MVT::ValueType VT = N->getValueType(0);
Dan Gohman05d92fe2007-07-13 20:03:40 +00004902 assert(MVT::isVector(VT) && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00004903
4904 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattneredab1b92006-04-02 03:25:57 +00004905 SDOperand LHS = N->getOperand(0);
4906 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00004907 SDOperand Shuffle = XformToShuffleWithZero(N);
4908 if (Shuffle.Val) return Shuffle;
4909
Dan Gohman7f321562007-06-25 16:23:39 +00004910 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00004911 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00004912 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
4913 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004914 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004915 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Chris Lattneredab1b92006-04-02 03:25:57 +00004916 SDOperand LHSOp = LHS.getOperand(i);
4917 SDOperand RHSOp = RHS.getOperand(i);
4918 // If these two elements can't be folded, bail out.
4919 if ((LHSOp.getOpcode() != ISD::UNDEF &&
4920 LHSOp.getOpcode() != ISD::Constant &&
4921 LHSOp.getOpcode() != ISD::ConstantFP) ||
4922 (RHSOp.getOpcode() != ISD::UNDEF &&
4923 RHSOp.getOpcode() != ISD::Constant &&
4924 RHSOp.getOpcode() != ISD::ConstantFP))
4925 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00004926 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00004927 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
4928 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00004929 if ((RHSOp.getOpcode() == ISD::Constant &&
4930 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
4931 (RHSOp.getOpcode() == ISD::ConstantFP &&
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00004932 cast<ConstantFPSDNode>(RHSOp.Val)->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00004933 break;
4934 }
Dan Gohman7f321562007-06-25 16:23:39 +00004935 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00004936 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00004937 assert((Ops.back().getOpcode() == ISD::UNDEF ||
4938 Ops.back().getOpcode() == ISD::Constant ||
4939 Ops.back().getOpcode() == ISD::ConstantFP) &&
4940 "Scalar binop didn't fold!");
4941 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004942
Dan Gohman7f321562007-06-25 16:23:39 +00004943 if (Ops.size() == LHS.getNumOperands()) {
4944 MVT::ValueType VT = LHS.getValueType();
4945 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004946 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004947 }
4948
4949 return SDOperand();
4950}
4951
Nate Begeman44728a72005-09-19 22:34:01 +00004952SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00004953 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
4954
4955 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
4956 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4957 // If we got a simplified select_cc node back from SimplifySelectCC, then
4958 // break it down into a new SETCC node, and a new SELECT node, and then return
4959 // the SELECT node, since we were called with a SELECT node.
4960 if (SCC.Val) {
4961 // Check to see if we got a select_cc back (to turn into setcc/select).
4962 // Otherwise, just return whatever node we got back, like fabs.
4963 if (SCC.getOpcode() == ISD::SELECT_CC) {
4964 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
4965 SCC.getOperand(0), SCC.getOperand(1),
4966 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00004967 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004968 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
4969 SCC.getOperand(3), SETCC);
4970 }
4971 return SCC;
4972 }
Nate Begeman44728a72005-09-19 22:34:01 +00004973 return SDOperand();
4974}
4975
Chris Lattner40c62d52005-10-18 06:04:22 +00004976/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
4977/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00004978/// select. Callers of this should assume that TheSelect is deleted if this
4979/// returns true. As such, they should return the appropriate thing (e.g. the
4980/// node) back to the top-level of the DAG combiner loop to avoid it being
4981/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00004982///
4983bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
4984 SDOperand RHS) {
4985
4986 // If this is a select from two identical things, try to pull the operation
4987 // through the select.
4988 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00004989 // If this is a load and the token chain is identical, replace the select
4990 // of two loads with a load through a select of the address to load from.
4991 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
4992 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00004993 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00004994 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00004995 LHS.getOperand(0) == RHS.getOperand(0)) {
4996 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
4997 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
4998
4999 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005000 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005001 // FIXME: this conflates two src values, discarding one. This is not
5002 // the right thing to do, but nothing uses srcvalues now. When they do,
5003 // turn SrcValue into a list of locations.
5004 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005005 if (TheSelect->getOpcode() == ISD::SELECT) {
5006 // Check that the condition doesn't reach either load. If so, folding
5007 // this will induce a cycle into the DAG.
5008 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
5009 !RLD->isPredecessor(TheSelect->getOperand(0).Val)) {
5010 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5011 TheSelect->getOperand(0), LLD->getBasePtr(),
5012 RLD->getBasePtr());
5013 }
5014 } else {
5015 // Check that the condition doesn't reach either load. If so, folding
5016 // this will induce a cycle into the DAG.
5017 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
5018 !RLD->isPredecessor(TheSelect->getOperand(0).Val) &&
5019 !LLD->isPredecessor(TheSelect->getOperand(1).Val) &&
5020 !RLD->isPredecessor(TheSelect->getOperand(1).Val)) {
5021 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005022 TheSelect->getOperand(0),
5023 TheSelect->getOperand(1),
5024 LLD->getBasePtr(), RLD->getBasePtr(),
5025 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005026 }
Evan Cheng466685d2006-10-09 20:57:25 +00005027 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005028
5029 if (Addr.Val) {
5030 SDOperand Load;
5031 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5032 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5033 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005034 LLD->getSrcValueOffset(),
5035 LLD->isVolatile(),
5036 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005037 else {
5038 Load = DAG.getExtLoad(LLD->getExtensionType(),
5039 TheSelect->getValueType(0),
5040 LLD->getChain(), Addr, LLD->getSrcValue(),
5041 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005042 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005043 LLD->isVolatile(),
5044 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005045 }
5046 // Users of the select now use the result of the load.
5047 CombineTo(TheSelect, Load);
5048
5049 // Users of the old loads now use the new load's chain. We know the
5050 // old-load value is dead now.
5051 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
5052 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
5053 return true;
5054 }
Evan Chengc5484282006-10-04 00:56:09 +00005055 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005056 }
5057 }
5058
5059 return false;
5060}
5061
Nate Begeman44728a72005-09-19 22:34:01 +00005062SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
5063 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00005064 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005065
5066 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00005067 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
5068 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
5069 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
5070
5071 // Determine if the condition we're dealing with is constant
5072 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00005073 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005074 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
5075
5076 // fold select_cc true, x, y -> x
5077 if (SCCC && SCCC->getValue())
5078 return N2;
5079 // fold select_cc false, x, y -> y
5080 if (SCCC && SCCC->getValue() == 0)
5081 return N3;
5082
5083 // Check to see if we can simplify the select into an fabs node
5084 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5085 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005086 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005087 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5088 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5089 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5090 N2 == N3.getOperand(0))
5091 return DAG.getNode(ISD::FABS, VT, N0);
5092
5093 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5094 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5095 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5096 N2.getOperand(0) == N3)
5097 return DAG.getNode(ISD::FABS, VT, N3);
5098 }
5099 }
5100
5101 // Check to see if we can perform the "gzip trick", transforming
5102 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005103 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00005104 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00005105 MVT::isInteger(N2.getValueType()) &&
5106 (N1C->isNullValue() || // (a < 0) ? b : 0
5107 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00005108 MVT::ValueType XType = N0.getValueType();
5109 MVT::ValueType AType = N2.getValueType();
5110 if (XType >= AType) {
5111 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005112 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00005113 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
5114 unsigned ShCtV = Log2_64(N2C->getValue());
5115 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
5116 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5117 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00005118 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005119 if (XType > AType) {
5120 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005121 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005122 }
5123 return DAG.getNode(ISD::AND, AType, Shift, N2);
5124 }
5125 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5126 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5127 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00005128 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005129 if (XType > AType) {
5130 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005131 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005132 }
5133 return DAG.getNode(ISD::AND, AType, Shift, N2);
5134 }
5135 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005136
5137 // fold select C, 16, 0 -> shl C, 4
5138 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
5139 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005140
5141 // If the caller doesn't want us to simplify this into a zext of a compare,
5142 // don't do it.
5143 if (NotExtCompare && N2C->getValue() == 1)
5144 return SDOperand();
5145
Nate Begeman07ed4172005-10-10 21:26:48 +00005146 // Get a SetCC of the condition
5147 // FIXME: Should probably make sure that setcc is legal if we ever have a
5148 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00005149 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005150 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00005151 if (AfterLegalize) {
5152 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Chris Lattner555d8d62006-12-07 22:36:47 +00005153 if (N2.getValueType() < SCC.getValueType())
5154 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5155 else
5156 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005157 } else {
5158 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005159 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005160 }
Chris Lattner5750df92006-03-01 04:03:14 +00005161 AddToWorkList(SCC.Val);
5162 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005163
5164 if (N2C->getValue() == 1)
5165 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005166 // shl setcc result by log2 n2c
5167 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
5168 DAG.getConstant(Log2_64(N2C->getValue()),
5169 TLI.getShiftAmountTy()));
5170 }
5171
Nate Begemanf845b452005-10-08 00:29:44 +00005172 // Check to see if this is the equivalent of setcc
5173 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5174 // otherwise, go ahead with the folds.
5175 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
5176 MVT::ValueType XType = N0.getValueType();
5177 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
5178 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
5179 if (Res.getValueType() != VT)
5180 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5181 return Res;
5182 }
5183
5184 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5185 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
5186 TLI.isOperationLegal(ISD::CTLZ, XType)) {
5187 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
5188 return DAG.getNode(ISD::SRL, XType, Ctlz,
5189 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
5190 TLI.getShiftAmountTy()));
5191 }
5192 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5193 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
5194 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
5195 N0);
5196 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
5197 DAG.getConstant(~0ULL, XType));
5198 return DAG.getNode(ISD::SRL, XType,
5199 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
5200 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5201 TLI.getShiftAmountTy()));
5202 }
5203 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5204 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
5205 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
5206 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5207 TLI.getShiftAmountTy()));
5208 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5209 }
5210 }
5211
5212 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5213 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5214 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005215 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
5216 N2.getOperand(0) == N1 && MVT::isInteger(N0.getValueType())) {
5217 MVT::ValueType XType = N0.getValueType();
5218 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5219 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5220 TLI.getShiftAmountTy()));
5221 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
5222 AddToWorkList(Shift.Val);
5223 AddToWorkList(Add.Val);
5224 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5225 }
5226 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5227 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5228 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5229 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5230 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Nate Begemanf845b452005-10-08 00:29:44 +00005231 MVT::ValueType XType = N0.getValueType();
5232 if (SubC->isNullValue() && MVT::isInteger(XType)) {
5233 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5234 DAG.getConstant(MVT::getSizeInBits(XType)-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005235 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00005236 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005237 AddToWorkList(Shift.Val);
5238 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005239 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5240 }
5241 }
5242 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005243
Nate Begeman44728a72005-09-19 22:34:01 +00005244 return SDOperand();
5245}
5246
Evan Chengfa1eb272007-02-08 22:13:59 +00005247/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Nate Begeman452d7be2005-09-16 00:54:12 +00005248SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00005249 SDOperand N1, ISD::CondCode Cond,
5250 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005251 TargetLowering::DAGCombinerInfo
5252 DagCombineInfo(DAG, !AfterLegalize, false, this);
5253 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005254}
5255
Nate Begeman69575232005-10-20 02:15:44 +00005256/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5257/// return a DAG expression to select that will generate the same value by
5258/// multiplying by a magic number. See:
5259/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5260SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005261 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005262 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
5263
Andrew Lenharth232c9102006-06-12 16:07:18 +00005264 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005265 ii != ee; ++ii)
5266 AddToWorkList(*ii);
5267 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005268}
5269
5270/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5271/// return a DAG expression to select that will generate the same value by
5272/// multiplying by a magic number. See:
5273/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5274SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005275 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005276 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005277
Andrew Lenharth232c9102006-06-12 16:07:18 +00005278 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005279 ii != ee; ++ii)
5280 AddToWorkList(*ii);
5281 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005282}
5283
Jim Laskey71382342006-10-07 23:37:56 +00005284/// FindBaseOffset - Return true if base is known not to alias with anything
5285/// but itself. Provides base object and offset as results.
5286static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
5287 // Assume it is a primitive operation.
5288 Base = Ptr; Offset = 0;
5289
5290 // If it's an adding a simple constant then integrate the offset.
5291 if (Base.getOpcode() == ISD::ADD) {
5292 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5293 Base = Base.getOperand(0);
5294 Offset += C->getValue();
5295 }
5296 }
5297
5298 // If it's any of the following then it can't alias with anything but itself.
5299 return isa<FrameIndexSDNode>(Base) ||
5300 isa<ConstantPoolSDNode>(Base) ||
5301 isa<GlobalAddressSDNode>(Base);
5302}
5303
5304/// isAlias - Return true if there is any possibility that the two addresses
5305/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00005306bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
5307 const Value *SrcValue1, int SrcValueOffset1,
5308 SDOperand Ptr2, int64_t Size2,
5309 const Value *SrcValue2, int SrcValueOffset2)
5310{
Jim Laskey71382342006-10-07 23:37:56 +00005311 // If they are the same then they must be aliases.
5312 if (Ptr1 == Ptr2) return true;
5313
5314 // Gather base node and offset information.
5315 SDOperand Base1, Base2;
5316 int64_t Offset1, Offset2;
5317 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5318 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5319
5320 // If they have a same base address then...
5321 if (Base1 == Base2) {
5322 // Check to see if the addresses overlap.
5323 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5324 }
5325
Jim Laskey096c22e2006-10-18 12:29:57 +00005326 // If we know both bases then they can't alias.
5327 if (KnownBase1 && KnownBase2) return false;
5328
Jim Laskey07a27092006-10-18 19:08:31 +00005329 if (CombinerGlobalAA) {
5330 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005331 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5332 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5333 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005334 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005335 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005336 if (AAResult == AliasAnalysis::NoAlias)
5337 return false;
5338 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005339
5340 // Otherwise we have to assume they alias.
5341 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005342}
5343
5344/// FindAliasInfo - Extracts the relevant alias information from the memory
5345/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005346bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00005347 SDOperand &Ptr, int64_t &Size,
5348 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005349 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5350 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005351 Size = MVT::getSizeInBits(LD->getMemoryVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005352 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005353 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005354 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005355 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005356 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005357 Size = MVT::getSizeInBits(ST->getMemoryVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005358 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005359 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005360 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005361 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005362 }
5363
5364 return false;
5365}
5366
Jim Laskey6ff23e52006-10-04 16:53:27 +00005367/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5368/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00005369void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005370 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005371 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005372 std::set<SDNode *> Visited; // Visited node set.
5373
Jim Laskey279f0532006-09-25 16:29:54 +00005374 // Get alias information for node.
5375 SDOperand Ptr;
5376 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005377 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005378 int SrcValueOffset;
5379 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005380
Jim Laskey6ff23e52006-10-04 16:53:27 +00005381 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005382 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005383
Jim Laskeybc588b82006-10-05 15:07:25 +00005384 // Look at each chain and determine if it is an alias. If so, add it to the
5385 // aliases list. If not, then continue up the chain looking for the next
5386 // candidate.
5387 while (!Chains.empty()) {
5388 SDOperand Chain = Chains.back();
5389 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005390
Jim Laskeybc588b82006-10-05 15:07:25 +00005391 // Don't bother if we've been before.
5392 if (Visited.find(Chain.Val) != Visited.end()) continue;
5393 Visited.insert(Chain.Val);
5394
5395 switch (Chain.getOpcode()) {
5396 case ISD::EntryToken:
5397 // Entry token is ideal chain operand, but handled in FindBetterChain.
5398 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005399
Jim Laskeybc588b82006-10-05 15:07:25 +00005400 case ISD::LOAD:
5401 case ISD::STORE: {
5402 // Get alias information for Chain.
5403 SDOperand OpPtr;
5404 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005405 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005406 int OpSrcValueOffset;
5407 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
5408 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005409
5410 // If chain is alias then stop here.
5411 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005412 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5413 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005414 Aliases.push_back(Chain);
5415 } else {
5416 // Look further up the chain.
5417 Chains.push_back(Chain.getOperand(0));
5418 // Clean up old chain.
5419 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00005420 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005421 break;
5422 }
5423
5424 case ISD::TokenFactor:
5425 // We have to check each of the operands of the token factor, so we queue
5426 // then up. Adding the operands to the queue (stack) in reverse order
5427 // maintains the original order and increases the likelihood that getNode
5428 // will find a matching token factor (CSE.)
5429 for (unsigned n = Chain.getNumOperands(); n;)
5430 Chains.push_back(Chain.getOperand(--n));
5431 // Eliminate the token factor if we can.
5432 AddToWorkList(Chain.Val);
5433 break;
5434
5435 default:
5436 // For all other instructions we will just have to take what we can get.
5437 Aliases.push_back(Chain);
5438 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005439 }
5440 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005441}
5442
5443/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5444/// for a better chain (aliasing node.)
5445SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
5446 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005447
Jim Laskey6ff23e52006-10-04 16:53:27 +00005448 // Accumulate all the aliases to this node.
5449 GatherAllAliases(N, OldChain, Aliases);
5450
5451 if (Aliases.size() == 0) {
5452 // If no operands then chain to entry token.
5453 return DAG.getEntryNode();
5454 } else if (Aliases.size() == 1) {
5455 // If a single operand then chain to it. We don't need to revisit it.
5456 return Aliases[0];
5457 }
5458
5459 // Construct a custom tailored token factor.
5460 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5461 &Aliases[0], Aliases.size());
5462
5463 // Make sure the old chain gets cleaned up.
5464 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
5465
5466 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005467}
5468
Nate Begeman1d4d4142005-09-01 00:19:25 +00005469// SelectionDAG::Combine - This is the entry point for the file.
5470//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005471void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00005472 if (!RunningAfterLegalize && ViewDAGCombine1)
5473 viewGraph();
5474 if (RunningAfterLegalize && ViewDAGCombine2)
5475 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005476 /// run - This is the main entry point to this class.
5477 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005478 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005479}