blob: 579870e6d07b57920c074e5d759c4b9dd5af355d [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 &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000461 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 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)
Dan Gohman002e5d02008-03-13 22:13:53 +0000953 return DAG.getConstant(N0C->getAPIntValue() + N1C->getAPIntValue(), 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,
Dan Gohman002e5d02008-03-13 22:13:53 +0000964 DAG.getConstant(N1C->getAPIntValue()+
965 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000966 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000967 // reassociate add
968 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
969 if (RADD.Val != 0)
970 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000971 // fold ((0-A) + B) -> B-A
972 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
973 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000974 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000975 // fold (A + (0-B)) -> A-B
976 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
977 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000978 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000979 // fold (A+(B-A)) -> B
980 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000981 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000982
Evan Cheng860771d2006-03-01 01:09:54 +0000983 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000984 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000985
986 // fold (a+b) -> (a|b) iff a and b share no bits.
987 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
Dan Gohman948d8ea2008-02-20 16:33:30 +0000988 APInt LHSZero, LHSOne;
989 APInt RHSZero, RHSOne;
990 APInt Mask = APInt::getAllOnesValue(MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +0000991 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +0000992 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +0000993 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000994
995 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
996 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
997 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
998 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
999 return DAG.getNode(ISD::OR, VT, N0, N1);
1000 }
1001 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001002
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001003 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
1004 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
1005 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
1006 if (Result.Val) return Result;
1007 }
1008 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
1009 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
1010 if (Result.Val) return Result;
1011 }
1012
Evan Chengb13cdbd2007-06-21 07:39:16 +00001013 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
1014 if (N0.getOpcode() == ISD::SELECT && N0.Val->hasOneUse()) {
1015 SDOperand Result = combineSelectAndUse(N, N0, N1, DAG);
1016 if (Result.Val) return Result;
1017 }
1018 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1019 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1020 if (Result.Val) return Result;
1021 }
1022
Nate Begeman83e75ec2005-09-06 04:43:02 +00001023 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001024}
1025
Chris Lattner91153682007-03-04 20:03:15 +00001026SDOperand DAGCombiner::visitADDC(SDNode *N) {
1027 SDOperand N0 = N->getOperand(0);
1028 SDOperand N1 = N->getOperand(1);
1029 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1030 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1031 MVT::ValueType VT = N0.getValueType();
1032
1033 // If the flag result is dead, turn this into an ADD.
1034 if (N->hasNUsesOfValue(0, 1))
1035 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +00001036 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +00001037
1038 // canonicalize constant to RHS.
Chris Lattnerbcf24842007-03-04 20:08:45 +00001039 if (N0C && !N1C) {
1040 SDOperand Ops[] = { N1, N0 };
1041 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
1042 }
Chris Lattner91153682007-03-04 20:03:15 +00001043
Chris Lattnerb6541762007-03-04 20:40:38 +00001044 // fold (addc x, 0) -> x + no carry out
1045 if (N1C && N1C->isNullValue())
1046 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1047
1048 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001049 APInt LHSZero, LHSOne;
1050 APInt RHSZero, RHSOne;
1051 APInt Mask = APInt::getAllOnesValue(MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +00001052 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001053 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001054 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001055
1056 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1057 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1058 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1059 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1060 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1061 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1062 }
Chris Lattner91153682007-03-04 20:03:15 +00001063
1064 return SDOperand();
1065}
1066
1067SDOperand DAGCombiner::visitADDE(SDNode *N) {
1068 SDOperand N0 = N->getOperand(0);
1069 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +00001070 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001071 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1072 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Chris Lattnerbcf24842007-03-04 20:08:45 +00001073 //MVT::ValueType VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001074
1075 // canonicalize constant to RHS
Chris Lattnerbcf24842007-03-04 20:08:45 +00001076 if (N0C && !N1C) {
Chris Lattnerb6541762007-03-04 20:40:38 +00001077 SDOperand Ops[] = { N1, N0, CarryIn };
Chris Lattnerbcf24842007-03-04 20:08:45 +00001078 return DAG.getNode(ISD::ADDE, N->getVTList(), Ops, 3);
1079 }
Chris Lattner91153682007-03-04 20:03:15 +00001080
Chris Lattnerb6541762007-03-04 20:40:38 +00001081 // fold (adde x, y, false) -> (addc x, y)
1082 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) {
1083 SDOperand Ops[] = { N1, N0 };
1084 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
1085 }
Chris Lattner91153682007-03-04 20:03:15 +00001086
1087 return SDOperand();
1088}
1089
1090
1091
Nate Begeman83e75ec2005-09-06 04:43:02 +00001092SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001093 SDOperand N0 = N->getOperand(0);
1094 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001095 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1096 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001097 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001098
Dan Gohman7f321562007-06-25 16:23:39 +00001099 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001100 if (MVT::isVector(VT)) {
1101 SDOperand FoldedVOp = SimplifyVBinOp(N);
1102 if (FoldedVOp.Val) return FoldedVOp;
1103 }
Dan Gohman7f321562007-06-25 16:23:39 +00001104
Chris Lattner854077d2005-10-17 01:07:11 +00001105 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001106 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001107 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001108 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001109 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001110 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001111 // fold (sub x, c) -> (add x, -c)
1112 if (N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +00001113 return DAG.getNode(ISD::ADD, VT, N0,
1114 DAG.getConstant(-N1C->getAPIntValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001115 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001116 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001117 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001118 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001119 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001120 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001121 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1122 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1123 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1124 if (Result.Val) return Result;
1125 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001126 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001127 if (N0.getOpcode() == ISD::UNDEF)
1128 return N0;
1129 if (N1.getOpcode() == ISD::UNDEF)
1130 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001131
Nate Begeman83e75ec2005-09-06 04:43:02 +00001132 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001133}
1134
Nate Begeman83e75ec2005-09-06 04:43:02 +00001135SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001136 SDOperand N0 = N->getOperand(0);
1137 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001138 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1139 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +00001140 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001141
Dan Gohman7f321562007-06-25 16:23:39 +00001142 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001143 if (MVT::isVector(VT)) {
1144 SDOperand FoldedVOp = SimplifyVBinOp(N);
1145 if (FoldedVOp.Val) return FoldedVOp;
1146 }
Dan Gohman7f321562007-06-25 16:23:39 +00001147
Dan Gohman613e0d82007-07-03 14:03:57 +00001148 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001149 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001150 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001151 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001152 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001153 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001154 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001155 if (N0C && !N1C)
1156 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001157 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001158 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001159 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001160 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001161 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001162 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001163 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001164 if (N1C && N1C->getAPIntValue().isPowerOf2())
Chris Lattner3e6099b2005-10-30 06:41:49 +00001165 return DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001166 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001167 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001168 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1169 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1170 // FIXME: If the input is something that is easily negated (e.g. a
1171 // single-use add), we should put the negate there.
1172 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1173 DAG.getNode(ISD::SHL, VT, N0,
1174 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1175 TLI.getShiftAmountTy())));
1176 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001177
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001178 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1179 if (N1C && N0.getOpcode() == ISD::SHL &&
1180 isa<ConstantSDNode>(N0.getOperand(1))) {
1181 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001182 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001183 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1184 }
1185
1186 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1187 // use.
1188 {
1189 SDOperand Sh(0,0), Y(0,0);
1190 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1191 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1192 N0.Val->hasOneUse()) {
1193 Sh = N0; Y = N1;
1194 } else if (N1.getOpcode() == ISD::SHL &&
1195 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1196 Sh = N1; Y = N0;
1197 }
1198 if (Sh.Val) {
1199 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1200 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1201 }
1202 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001203 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1204 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1205 isa<ConstantSDNode>(N0.getOperand(1))) {
1206 return DAG.getNode(ISD::ADD, VT,
1207 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1208 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1209 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001210
Nate Begemancd4d58c2006-02-03 06:46:56 +00001211 // reassociate mul
1212 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1213 if (RMUL.Val != 0)
1214 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001215
Nate Begeman83e75ec2005-09-06 04:43:02 +00001216 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001217}
1218
Nate Begeman83e75ec2005-09-06 04:43:02 +00001219SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001220 SDOperand N0 = N->getOperand(0);
1221 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001222 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1223 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001224 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001225
Dan Gohman7f321562007-06-25 16:23:39 +00001226 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001227 if (MVT::isVector(VT)) {
1228 SDOperand FoldedVOp = SimplifyVBinOp(N);
1229 if (FoldedVOp.Val) return FoldedVOp;
1230 }
Dan Gohman7f321562007-06-25 16:23:39 +00001231
Nate Begeman1d4d4142005-09-01 00:19:25 +00001232 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001233 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001234 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001235 // fold (sdiv X, 1) -> X
1236 if (N1C && N1C->getSignExtended() == 1LL)
1237 return N0;
1238 // fold (sdiv X, -1) -> 0-X
1239 if (N1C && N1C->isAllOnesValue())
1240 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001241 // If we know the sign bits of both operands are zero, strength reduce to a
1242 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Chris Lattnerf32aac32008-01-27 23:32:17 +00001243 if (!MVT::isVector(VT)) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001244 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerf32aac32008-01-27 23:32:17 +00001245 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1246 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001247 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001248 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001249 (isPowerOf2_64(N1C->getSignExtended()) ||
1250 isPowerOf2_64(-N1C->getSignExtended()))) {
1251 // If dividing by powers of two is cheap, then don't perform the following
1252 // fold.
1253 if (TLI.isPow2DivCheap())
1254 return SDOperand();
1255 int64_t pow2 = N1C->getSignExtended();
1256 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001257 unsigned lg2 = Log2_64(abs2);
1258 // Splat the sign bit into the register
1259 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001260 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1261 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001262 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001263 // Add (N0 < 0) ? abs2 - 1 : 0;
1264 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1265 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001266 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001267 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001268 AddToWorkList(SRL.Val);
1269 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001270 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1271 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001272 // If we're dividing by a positive value, we're done. Otherwise, we must
1273 // negate the result.
1274 if (pow2 > 0)
1275 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001276 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001277 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1278 }
Nate Begeman69575232005-10-20 02:15:44 +00001279 // if integer divide is expensive and we satisfy the requirements, emit an
1280 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001281 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001282 !TLI.isIntDivCheap()) {
1283 SDOperand Op = BuildSDIV(N);
1284 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001285 }
Dan Gohman7f321562007-06-25 16:23:39 +00001286
Dan Gohman613e0d82007-07-03 14:03:57 +00001287 // undef / X -> 0
1288 if (N0.getOpcode() == ISD::UNDEF)
1289 return DAG.getConstant(0, VT);
1290 // X / undef -> undef
1291 if (N1.getOpcode() == ISD::UNDEF)
1292 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001293
Nate Begeman83e75ec2005-09-06 04:43:02 +00001294 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001295}
1296
Nate Begeman83e75ec2005-09-06 04:43:02 +00001297SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001298 SDOperand N0 = N->getOperand(0);
1299 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001300 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1301 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001302 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001303
Dan Gohman7f321562007-06-25 16:23:39 +00001304 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001305 if (MVT::isVector(VT)) {
1306 SDOperand FoldedVOp = SimplifyVBinOp(N);
1307 if (FoldedVOp.Val) return FoldedVOp;
1308 }
Dan Gohman7f321562007-06-25 16:23:39 +00001309
Nate Begeman1d4d4142005-09-01 00:19:25 +00001310 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001311 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001312 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001313 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001314 if (N1C && N1C->getAPIntValue().isPowerOf2())
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001315 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001316 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001317 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001318 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1319 if (N1.getOpcode() == ISD::SHL) {
1320 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001321 if (SHC->getAPIntValue().isPowerOf2()) {
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001322 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001323 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001324 DAG.getConstant(SHC->getAPIntValue()
1325 .logBase2(),
Nate Begemanc031e332006-02-05 07:36:48 +00001326 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001327 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001328 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001329 }
1330 }
1331 }
Nate Begeman69575232005-10-20 02:15:44 +00001332 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001333 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Chris Lattnere9936d12005-10-22 18:50:15 +00001334 SDOperand Op = BuildUDIV(N);
1335 if (Op.Val) return Op;
1336 }
Dan Gohman7f321562007-06-25 16:23:39 +00001337
Dan Gohman613e0d82007-07-03 14:03:57 +00001338 // undef / X -> 0
1339 if (N0.getOpcode() == ISD::UNDEF)
1340 return DAG.getConstant(0, VT);
1341 // X / undef -> undef
1342 if (N1.getOpcode() == ISD::UNDEF)
1343 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001344
Nate Begeman83e75ec2005-09-06 04:43:02 +00001345 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001346}
1347
Nate Begeman83e75ec2005-09-06 04:43:02 +00001348SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001349 SDOperand N0 = N->getOperand(0);
1350 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001351 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1352 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001353 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001354
1355 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001356 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001357 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001358 // If we know the sign bits of both operands are zero, strength reduce to a
1359 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Chris Lattneree339f42008-01-27 23:21:58 +00001360 if (!MVT::isVector(VT)) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001361 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001362 return DAG.getNode(ISD::UREM, VT, N0, N1);
1363 }
Chris Lattner26d29902006-10-12 20:58:32 +00001364
Dan Gohman77003042007-11-26 23:46:11 +00001365 // If X/C can be simplified by the division-by-constant logic, lower
1366 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001367 if (N1C && !N1C->isNullValue()) {
1368 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Chris Lattner5eee4272008-01-26 01:09:19 +00001369 AddToWorkList(Div.Val);
Dan Gohman77003042007-11-26 23:46:11 +00001370 SDOperand OptimizedDiv = combine(Div.Val);
1371 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1372 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1373 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1374 AddToWorkList(Mul.Val);
1375 return Sub;
1376 }
Chris Lattner26d29902006-10-12 20:58:32 +00001377 }
1378
Dan Gohman613e0d82007-07-03 14:03:57 +00001379 // undef % X -> 0
1380 if (N0.getOpcode() == ISD::UNDEF)
1381 return DAG.getConstant(0, VT);
1382 // X % undef -> undef
1383 if (N1.getOpcode() == ISD::UNDEF)
1384 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001385
Nate Begeman83e75ec2005-09-06 04:43:02 +00001386 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001387}
1388
Nate Begeman83e75ec2005-09-06 04:43:02 +00001389SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001390 SDOperand N0 = N->getOperand(0);
1391 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001392 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1393 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001394 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001395
1396 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001397 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001398 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001399 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001400 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
1401 return DAG.getNode(ISD::AND, VT, N0,
1402 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001403 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1404 if (N1.getOpcode() == ISD::SHL) {
1405 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001406 if (SHC->getAPIntValue().isPowerOf2()) {
1407 SDOperand Add =
1408 DAG.getNode(ISD::ADD, VT, N1,
1409 DAG.getConstant(APInt::getAllOnesValue(MVT::getSizeInBits(VT)),
1410 VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001411 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001412 return DAG.getNode(ISD::AND, VT, N0, Add);
1413 }
1414 }
1415 }
Chris Lattner26d29902006-10-12 20:58:32 +00001416
Dan Gohman77003042007-11-26 23:46:11 +00001417 // If X/C can be simplified by the division-by-constant logic, lower
1418 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001419 if (N1C && !N1C->isNullValue()) {
1420 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman77003042007-11-26 23:46:11 +00001421 SDOperand OptimizedDiv = combine(Div.Val);
1422 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1423 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1424 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1425 AddToWorkList(Mul.Val);
1426 return Sub;
1427 }
Chris Lattner26d29902006-10-12 20:58:32 +00001428 }
1429
Dan Gohman613e0d82007-07-03 14:03:57 +00001430 // undef % X -> 0
1431 if (N0.getOpcode() == ISD::UNDEF)
1432 return DAG.getConstant(0, VT);
1433 // X % undef -> undef
1434 if (N1.getOpcode() == ISD::UNDEF)
1435 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001436
Nate Begeman83e75ec2005-09-06 04:43:02 +00001437 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001438}
1439
Nate Begeman83e75ec2005-09-06 04:43:02 +00001440SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001441 SDOperand N0 = N->getOperand(0);
1442 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001443 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001444 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001445
1446 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001447 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001448 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001449 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001450 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001451 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1452 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001453 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001454 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001455 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001456 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001457
Nate Begeman83e75ec2005-09-06 04:43:02 +00001458 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001459}
1460
Nate Begeman83e75ec2005-09-06 04:43:02 +00001461SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001462 SDOperand N0 = N->getOperand(0);
1463 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001464 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001465 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001466
1467 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001468 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001469 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001470 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001471 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001472 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001473 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001474 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001475 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001476
Nate Begeman83e75ec2005-09-06 04:43:02 +00001477 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001478}
1479
Dan Gohman389079b2007-10-08 17:57:15 +00001480/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1481/// compute two values. LoOp and HiOp give the opcodes for the two computations
1482/// that are being performed. Return true if a simplification was made.
1483///
Chris Lattner5eee4272008-01-26 01:09:19 +00001484SDOperand DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1485 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001486 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001487 bool HiExists = N->hasAnyUseOfValue(1);
1488 if (!HiExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001489 (!AfterLegalize ||
1490 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001491 SDOperand Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
1492 N->getNumOperands());
1493 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001494 }
1495
1496 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001497 bool LoExists = N->hasAnyUseOfValue(0);
1498 if (!LoExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001499 (!AfterLegalize ||
1500 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001501 SDOperand Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
1502 N->getNumOperands());
1503 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001504 }
1505
Evan Cheng44711942007-11-08 09:25:29 +00001506 // If both halves are used, return as it is.
1507 if (LoExists && HiExists)
Chris Lattner5eee4272008-01-26 01:09:19 +00001508 return SDOperand();
Evan Cheng44711942007-11-08 09:25:29 +00001509
1510 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001511 if (LoExists) {
1512 SDOperand Lo = DAG.getNode(LoOp, N->getValueType(0),
1513 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001514 AddToWorkList(Lo.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001515 SDOperand LoOpt = combine(Lo.Val);
Chris Lattner5eee4272008-01-26 01:09:19 +00001516 if (LoOpt.Val && LoOpt.Val != Lo.Val &&
1517 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType()))
1518 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001519 }
1520
Evan Cheng44711942007-11-08 09:25:29 +00001521 if (HiExists) {
1522 SDOperand Hi = DAG.getNode(HiOp, N->getValueType(1),
1523 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001524 AddToWorkList(Hi.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001525 SDOperand HiOpt = combine(Hi.Val);
1526 if (HiOpt.Val && HiOpt != Hi &&
Chris Lattner5eee4272008-01-26 01:09:19 +00001527 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType()))
1528 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001529 }
Chris Lattner5eee4272008-01-26 01:09:19 +00001530 return SDOperand();
Dan Gohman389079b2007-10-08 17:57:15 +00001531}
1532
1533SDOperand DAGCombiner::visitSMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001534 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
1535 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001536
1537 return SDOperand();
1538}
1539
1540SDOperand DAGCombiner::visitUMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001541 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
1542 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001543
1544 return SDOperand();
1545}
1546
1547SDOperand DAGCombiner::visitSDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001548 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
1549 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001550
1551 return SDOperand();
1552}
1553
1554SDOperand DAGCombiner::visitUDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001555 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
1556 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001557
1558 return SDOperand();
1559}
1560
Chris Lattner35e5c142006-05-05 05:51:50 +00001561/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1562/// two operands of the same opcode, try to simplify it.
1563SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1564 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1565 MVT::ValueType VT = N0.getValueType();
1566 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1567
Chris Lattner540121f2006-05-05 06:31:05 +00001568 // For each of OP in AND/OR/XOR:
1569 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1570 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1571 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001572 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001573 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001574 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001575 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1576 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1577 N0.getOperand(0).getValueType(),
1578 N0.getOperand(0), N1.getOperand(0));
1579 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001580 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001581 }
1582
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001583 // For each of OP in SHL/SRL/SRA/AND...
1584 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1585 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1586 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001587 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001588 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001589 N0.getOperand(1) == N1.getOperand(1)) {
1590 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1591 N0.getOperand(0).getValueType(),
1592 N0.getOperand(0), N1.getOperand(0));
1593 AddToWorkList(ORNode.Val);
1594 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1595 }
1596
1597 return SDOperand();
1598}
1599
Nate Begeman83e75ec2005-09-06 04:43:02 +00001600SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001601 SDOperand N0 = N->getOperand(0);
1602 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001603 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001604 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1605 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001606 MVT::ValueType VT = N1.getValueType();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001607 unsigned BitWidth = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001608
Dan Gohman7f321562007-06-25 16:23:39 +00001609 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001610 if (MVT::isVector(VT)) {
1611 SDOperand FoldedVOp = SimplifyVBinOp(N);
1612 if (FoldedVOp.Val) return FoldedVOp;
1613 }
Dan Gohman7f321562007-06-25 16:23:39 +00001614
Dan Gohman613e0d82007-07-03 14:03:57 +00001615 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001616 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001617 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001618 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001619 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001620 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001621 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001622 if (N0C && !N1C)
1623 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001624 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001625 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001626 return N0;
1627 // if (and x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001628 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
1629 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001630 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001631 // reassociate and
1632 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1633 if (RAND.Val != 0)
1634 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001635 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001636 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001637 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001638 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001639 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001640 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1641 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001642 SDOperand N0Op0 = N0.getOperand(0);
1643 APInt Mask = ~N1C->getAPIntValue();
1644 Mask.trunc(N0Op0.getValueSizeInBits());
1645 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001646 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001647 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001648
1649 // Replace uses of the AND with uses of the Zero extend node.
1650 CombineTo(N, Zext);
1651
Chris Lattner3603cd62006-02-02 07:17:31 +00001652 // We actually want to replace all uses of the any_extend with the
1653 // zero_extend, to avoid duplicating things. This will later cause this
1654 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001655 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001656 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001657 }
1658 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001659 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1660 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1661 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1662 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1663
1664 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1665 MVT::isInteger(LL.getValueType())) {
1666 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001667 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001668 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001669 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001670 return DAG.getSetCC(VT, ORNode, LR, Op1);
1671 }
1672 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1673 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1674 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001675 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001676 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1677 }
1678 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1679 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1680 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001681 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001682 return DAG.getSetCC(VT, ORNode, LR, Op1);
1683 }
1684 }
1685 // canonicalize equivalent to ll == rl
1686 if (LL == RR && LR == RL) {
1687 Op1 = ISD::getSetCCSwappedOperands(Op1);
1688 std::swap(RL, RR);
1689 }
1690 if (LL == RL && LR == RR) {
1691 bool isInteger = MVT::isInteger(LL.getValueType());
1692 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1693 if (Result != ISD::SETCC_INVALID)
1694 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1695 }
1696 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001697
1698 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1699 if (N0.getOpcode() == N1.getOpcode()) {
1700 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1701 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001702 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001703
Nate Begemande996292006-02-03 22:24:05 +00001704 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1705 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001706 if (!MVT::isVector(VT) &&
1707 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001708 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001709 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001710 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001711 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001712 MVT::ValueType EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001713 // If we zero all the possible extended bits, then we can turn this into
1714 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001715 unsigned BitWidth = N1.getValueSizeInBits();
1716 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
1717 BitWidth - MVT::getSizeInBits(EVT))) &&
Evan Chengc5484282006-10-04 00:56:09 +00001718 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001719 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1720 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001721 LN0->getSrcValueOffset(), EVT,
1722 LN0->isVolatile(),
1723 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001724 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001725 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001726 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001727 }
1728 }
1729 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001730 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1731 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001732 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001733 MVT::ValueType EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001734 // If we zero all the possible extended bits, then we can turn this into
1735 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001736 unsigned BitWidth = N1.getValueSizeInBits();
1737 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
1738 BitWidth - MVT::getSizeInBits(EVT))) &&
Evan Chengc5484282006-10-04 00:56:09 +00001739 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001740 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1741 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001742 LN0->getSrcValueOffset(), EVT,
1743 LN0->isVolatile(),
1744 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001745 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001746 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001747 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001748 }
1749 }
Chris Lattner15045b62006-02-28 06:35:35 +00001750
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001751 // fold (and (load x), 255) -> (zextload x, i8)
1752 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001753 if (N1C && N0.getOpcode() == ISD::LOAD) {
1754 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1755 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Chris Lattnerddf89562008-01-17 19:59:44 +00001756 LN0->isUnindexed() && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001757 MVT::ValueType EVT, LoadedVT;
Dan Gohman002e5d02008-03-13 22:13:53 +00001758 if (N1C->getAPIntValue() == 255)
Evan Cheng466685d2006-10-09 20:57:25 +00001759 EVT = MVT::i8;
Dan Gohman002e5d02008-03-13 22:13:53 +00001760 else if (N1C->getAPIntValue() == 65535)
Evan Cheng466685d2006-10-09 20:57:25 +00001761 EVT = MVT::i16;
Dan Gohman002e5d02008-03-13 22:13:53 +00001762 else if (N1C->getAPIntValue() == ~0U)
Evan Cheng466685d2006-10-09 20:57:25 +00001763 EVT = MVT::i32;
1764 else
1765 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001766
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001767 LoadedVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001768 if (EVT != MVT::Other && LoadedVT > EVT &&
1769 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1770 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1771 // For big endian targets, we need to add an offset to the pointer to
1772 // load the correct bytes. For little endian systems, we merely need to
1773 // read fewer bytes from the same pointer.
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001774 unsigned LVTStoreBytes = MVT::getStoreSizeInBits(LoadedVT)/8;
1775 unsigned EVTStoreBytes = MVT::getStoreSizeInBits(EVT)/8;
1776 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001777 unsigned Alignment = LN0->getAlignment();
Evan Cheng466685d2006-10-09 20:57:25 +00001778 SDOperand NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001779 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001780 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1781 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001782 Alignment = MinAlign(Alignment, PtrOff);
1783 }
Evan Cheng466685d2006-10-09 20:57:25 +00001784 AddToWorkList(NewPtr.Val);
1785 SDOperand Load =
1786 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001787 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001788 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001789 AddToWorkList(N);
1790 CombineTo(N0.Val, Load, Load.getValue(1));
1791 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1792 }
Chris Lattner15045b62006-02-28 06:35:35 +00001793 }
1794 }
1795
Nate Begeman83e75ec2005-09-06 04:43:02 +00001796 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001797}
1798
Nate Begeman83e75ec2005-09-06 04:43:02 +00001799SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001800 SDOperand N0 = N->getOperand(0);
1801 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001802 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001803 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1804 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001805 MVT::ValueType VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001806
Dan Gohman7f321562007-06-25 16:23:39 +00001807 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001808 if (MVT::isVector(VT)) {
1809 SDOperand FoldedVOp = SimplifyVBinOp(N);
1810 if (FoldedVOp.Val) return FoldedVOp;
1811 }
Dan Gohman7f321562007-06-25 16:23:39 +00001812
Dan Gohman613e0d82007-07-03 14:03:57 +00001813 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001814 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001815 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001816 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001817 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001818 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001819 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001820 if (N0C && !N1C)
1821 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001822 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001823 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001824 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001825 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001826 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001827 return N1;
1828 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001829 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001830 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001831 // reassociate or
1832 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1833 if (ROR.Val != 0)
1834 return ROR;
1835 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1836 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001837 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001838 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1839 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1840 N1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001841 DAG.getConstant(N1C->getAPIntValue() |
1842 C1->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001843 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001844 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1845 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1846 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1847 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1848
1849 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1850 MVT::isInteger(LL.getValueType())) {
1851 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1852 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001853 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001854 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1855 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001856 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001857 return DAG.getSetCC(VT, ORNode, LR, Op1);
1858 }
1859 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1860 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1861 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1862 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1863 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001864 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001865 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1866 }
1867 }
1868 // canonicalize equivalent to ll == rl
1869 if (LL == RR && LR == RL) {
1870 Op1 = ISD::getSetCCSwappedOperands(Op1);
1871 std::swap(RL, RR);
1872 }
1873 if (LL == RL && LR == RR) {
1874 bool isInteger = MVT::isInteger(LL.getValueType());
1875 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1876 if (Result != ISD::SETCC_INVALID)
1877 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1878 }
1879 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001880
1881 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1882 if (N0.getOpcode() == N1.getOpcode()) {
1883 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1884 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001885 }
Chris Lattner516b9622006-09-14 20:50:57 +00001886
Chris Lattner1ec72732006-09-14 21:11:37 +00001887 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1888 if (N0.getOpcode() == ISD::AND &&
1889 N1.getOpcode() == ISD::AND &&
1890 N0.getOperand(1).getOpcode() == ISD::Constant &&
1891 N1.getOperand(1).getOpcode() == ISD::Constant &&
1892 // Don't increase # computations.
1893 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1894 // We can only do this xform if we know that bits from X that are set in C2
1895 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001896 const APInt &LHSMask =
1897 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1898 const APInt &RHSMask =
1899 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00001900
Dan Gohmanea859be2007-06-22 14:59:07 +00001901 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1902 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001903 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1904 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1905 }
1906 }
1907
1908
Chris Lattner516b9622006-09-14 20:50:57 +00001909 // See if this is some rotate idiom.
1910 if (SDNode *Rot = MatchRotate(N0, N1))
1911 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001912
Nate Begeman83e75ec2005-09-06 04:43:02 +00001913 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001914}
1915
Chris Lattner516b9622006-09-14 20:50:57 +00001916
1917/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1918static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1919 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001920 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001921 Mask = Op.getOperand(1);
1922 Op = Op.getOperand(0);
1923 } else {
1924 return false;
1925 }
1926 }
1927
1928 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1929 Shift = Op;
1930 return true;
1931 }
1932 return false;
1933}
1934
1935
1936// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1937// idioms for rotate, and if the target supports rotation instructions, generate
1938// a rot[lr].
1939SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1940 // Must be a legal type. Expanded an promoted things won't work with rotates.
1941 MVT::ValueType VT = LHS.getValueType();
1942 if (!TLI.isTypeLegal(VT)) return 0;
1943
1944 // The target must have at least one rotate flavor.
1945 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1946 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1947 if (!HasROTL && !HasROTR) return 0;
1948
1949 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1950 SDOperand LHSShift; // The shift.
1951 SDOperand LHSMask; // AND value if any.
1952 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1953 return 0; // Not part of a rotate.
1954
1955 SDOperand RHSShift; // The shift.
1956 SDOperand RHSMask; // AND value if any.
1957 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1958 return 0; // Not part of a rotate.
1959
1960 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1961 return 0; // Not shifting the same value.
1962
1963 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1964 return 0; // Shifts must disagree.
1965
1966 // Canonicalize shl to left side in a shl/srl pair.
1967 if (RHSShift.getOpcode() == ISD::SHL) {
1968 std::swap(LHS, RHS);
1969 std::swap(LHSShift, RHSShift);
1970 std::swap(LHSMask , RHSMask );
1971 }
1972
1973 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Scott Michelc9dc1142007-04-02 21:36:32 +00001974 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1975 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1976 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001977
1978 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1979 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001980 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1981 RHSShiftAmt.getOpcode() == ISD::Constant) {
1982 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
1983 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00001984 if ((LShVal + RShVal) != OpSizeInBits)
1985 return 0;
1986
1987 SDOperand Rot;
1988 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001989 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001990 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001991 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001992
1993 // If there is an AND of either shifted operand, apply it to the result.
1994 if (LHSMask.Val || RHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00001995 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Chris Lattner516b9622006-09-14 20:50:57 +00001996
1997 if (LHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00001998 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
1999 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002000 }
2001 if (RHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002002 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2003 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002004 }
2005
2006 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2007 }
2008
2009 return Rot.Val;
2010 }
2011
2012 // If there is a mask here, and we have a variable shift, we can't be sure
2013 // that we're masking out the right stuff.
2014 if (LHSMask.Val || RHSMask.Val)
2015 return 0;
2016
2017 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2018 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002019 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2020 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002021 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002022 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002023 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002024 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002025 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002026 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002027 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002028 }
Chris Lattner516b9622006-09-14 20:50:57 +00002029 }
2030 }
2031
2032 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2033 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002034 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2035 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002036 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002037 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002038 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002039 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002040 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002041 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002042 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002043 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002044 }
2045 }
2046
2047 // Look for sign/zext/any-extended cases:
2048 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2049 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2050 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
2051 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2052 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2053 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
2054 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
2055 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
2056 if (RExtOp0.getOpcode() == ISD::SUB &&
2057 RExtOp0.getOperand(1) == LExtOp0) {
2058 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2059 // (rotr x, y)
2060 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2061 // (rotl x, (sub 32, y))
2062 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002063 if (SUBC->getAPIntValue() == OpSizeInBits) {
Scott Michelc9dc1142007-04-02 21:36:32 +00002064 if (HasROTL)
2065 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2066 else
2067 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
2068 }
2069 }
2070 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2071 RExtOp0 == LExtOp0.getOperand(1)) {
2072 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2073 // (rotl x, y)
2074 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2075 // (rotr x, (sub 32, y))
2076 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002077 if (SUBC->getAPIntValue() == OpSizeInBits) {
Scott Michelc9dc1142007-04-02 21:36:32 +00002078 if (HasROTL)
2079 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
2080 else
2081 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2082 }
2083 }
Chris Lattner516b9622006-09-14 20:50:57 +00002084 }
2085 }
2086
2087 return 0;
2088}
2089
2090
Nate Begeman83e75ec2005-09-06 04:43:02 +00002091SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002092 SDOperand N0 = N->getOperand(0);
2093 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002094 SDOperand LHS, RHS, CC;
2095 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2096 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002097 MVT::ValueType VT = N0.getValueType();
2098
Dan Gohman7f321562007-06-25 16:23:39 +00002099 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00002100 if (MVT::isVector(VT)) {
2101 SDOperand FoldedVOp = SimplifyVBinOp(N);
2102 if (FoldedVOp.Val) return FoldedVOp;
2103 }
Dan Gohman7f321562007-06-25 16:23:39 +00002104
Dan Gohman613e0d82007-07-03 14:03:57 +00002105 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002106 if (N0.getOpcode() == ISD::UNDEF)
2107 return N0;
2108 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002109 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002110 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002111 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002112 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00002113 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002114 if (N0C && !N1C)
2115 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002116 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002117 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002118 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002119 // reassociate xor
2120 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
2121 if (RXOR.Val != 0)
2122 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002123 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002124 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Nate Begeman646d7e22005-09-02 21:18:40 +00002125 bool isInt = MVT::isInteger(LHS.getValueType());
2126 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2127 isInt);
2128 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002129 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002130 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002131 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002132 assert(0 && "Unhandled SetCC Equivalent!");
2133 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002134 }
Chris Lattner61c5ff42007-09-10 21:39:07 +00002135 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002136 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Chris Lattner61c5ff42007-09-10 21:39:07 +00002137 N0.Val->hasOneUse() && isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
2138 SDOperand V = N0.getOperand(0);
2139 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002140 DAG.getConstant(1, V.getValueType()));
Chris Lattner61c5ff42007-09-10 21:39:07 +00002141 AddToWorkList(V.Val);
2142 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2143 }
2144
Nate Begeman99801192005-09-07 23:25:52 +00002145 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Dan Gohman002e5d02008-03-13 22:13:53 +00002146 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002147 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002148 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002149 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2150 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002151 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2152 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002153 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002154 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002155 }
2156 }
Nate Begeman99801192005-09-07 23:25:52 +00002157 // fold !(x or y) -> (!x and !y) iff x or y are constants
2158 if (N1C && N1C->isAllOnesValue() &&
2159 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002160 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002161 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2162 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002163 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2164 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002165 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002166 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002167 }
2168 }
Nate Begeman223df222005-09-08 20:18:10 +00002169 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2170 if (N1C && N0.getOpcode() == ISD::XOR) {
2171 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2172 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2173 if (N00C)
2174 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00002175 DAG.getConstant(N1C->getAPIntValue()^
2176 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002177 if (N01C)
2178 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
Dan Gohman002e5d02008-03-13 22:13:53 +00002179 DAG.getConstant(N1C->getAPIntValue()^
2180 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002181 }
2182 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002183 if (N0 == N1) {
2184 if (!MVT::isVector(VT)) {
2185 return DAG.getConstant(0, VT);
2186 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2187 // Produce a vector of zeros.
Dan Gohman51eaa862007-06-14 22:58:02 +00002188 SDOperand El = DAG.getConstant(0, MVT::getVectorElementType(VT));
Chris Lattner4fbdd592006-03-28 19:11:05 +00002189 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002190 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002191 }
2192 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002193
2194 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2195 if (N0.getOpcode() == N1.getOpcode()) {
2196 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
2197 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002198 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002199
Chris Lattner3e104b12006-04-08 04:15:24 +00002200 // Simplify the expression using non-local knowledge.
2201 if (!MVT::isVector(VT) &&
2202 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002203 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002204
Nate Begeman83e75ec2005-09-06 04:43:02 +00002205 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002206}
2207
Chris Lattnere70da202007-12-06 07:33:36 +00002208/// visitShiftByConstant - Handle transforms common to the three shifts, when
2209/// the shift amount is a constant.
2210SDOperand DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
2211 SDNode *LHS = N->getOperand(0).Val;
2212 if (!LHS->hasOneUse()) return SDOperand();
2213
2214 // We want to pull some binops through shifts, so that we have (and (shift))
2215 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2216 // thing happens with address calculations, so it's important to canonicalize
2217 // it.
2218 bool HighBitSet = false; // Can we transform this if the high bit is set?
2219
2220 switch (LHS->getOpcode()) {
2221 default: return SDOperand();
2222 case ISD::OR:
2223 case ISD::XOR:
2224 HighBitSet = false; // We can only transform sra if the high bit is clear.
2225 break;
2226 case ISD::AND:
2227 HighBitSet = true; // We can only transform sra if the high bit is set.
2228 break;
2229 case ISD::ADD:
2230 if (N->getOpcode() != ISD::SHL)
2231 return SDOperand(); // only shl(add) not sr[al](add).
2232 HighBitSet = false; // We can only transform sra if the high bit is clear.
2233 break;
2234 }
2235
2236 // We require the RHS of the binop to be a constant as well.
2237 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
2238 if (!BinOpCst) return SDOperand();
2239
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002240
2241 // FIXME: disable this for unless the input to the binop is a shift by a
2242 // constant. If it is not a shift, it pessimizes some common cases like:
2243 //
2244 //void foo(int *X, int i) { X[i & 1235] = 1; }
2245 //int bar(int *X, int i) { return X[i & 255]; }
2246 SDNode *BinOpLHSVal = LHS->getOperand(0).Val;
2247 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2248 BinOpLHSVal->getOpcode() != ISD::SRA &&
2249 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2250 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
2251 return SDOperand();
2252
Chris Lattnere70da202007-12-06 07:33:36 +00002253 MVT::ValueType VT = N->getValueType(0);
2254
2255 // If this is a signed shift right, and the high bit is modified
2256 // by the logical operation, do not perform the transformation.
2257 // The highBitSet boolean indicates the value of the high bit of
2258 // the constant which would cause it to be modified for this
2259 // operation.
2260 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002261 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2262 if (BinOpRHSSignSet != HighBitSet)
Chris Lattnere70da202007-12-06 07:33:36 +00002263 return SDOperand();
2264 }
2265
2266 // Fold the constants, shifting the binop RHS by the shift amount.
2267 SDOperand NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
2268 LHS->getOperand(1), N->getOperand(1));
2269
2270 // Create the new shift.
2271 SDOperand NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
2272 N->getOperand(1));
2273
2274 // Create the new binop.
2275 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2276}
2277
2278
Nate Begeman83e75ec2005-09-06 04:43:02 +00002279SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002280 SDOperand N0 = N->getOperand(0);
2281 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002282 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2283 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002284 MVT::ValueType VT = N0.getValueType();
2285 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2286
2287 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002288 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002289 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002290 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002291 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002292 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002293 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002294 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002295 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002296 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002297 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002298 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002299 // if (shl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002300 if (DAG.MaskedValueIsZero(SDOperand(N, 0),
2301 APInt::getAllOnesValue(MVT::getSizeInBits(VT))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002302 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00002303 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002304 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002305 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002306 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002307 N0.getOperand(1).getOpcode() == ISD::Constant) {
2308 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002309 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002310 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002311 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002312 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002313 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002314 }
2315 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2316 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002317 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002318 N0.getOperand(1).getOpcode() == ISD::Constant) {
2319 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002320 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002321 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
2322 DAG.getConstant(~0ULL << c1, VT));
2323 if (c2 > c1)
2324 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002325 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002326 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002327 return DAG.getNode(ISD::SRL, VT, Mask,
2328 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002329 }
2330 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002331 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002332 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002333 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002334
2335 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002336}
2337
Nate Begeman83e75ec2005-09-06 04:43:02 +00002338SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002339 SDOperand N0 = N->getOperand(0);
2340 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002341 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2342 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002343 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002344
2345 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002346 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002347 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002348 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002349 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002350 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002351 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002352 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002353 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002354 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00002355 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002356 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002357 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002358 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002359 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002360 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2361 // sext_inreg.
2362 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
2363 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
2364 MVT::ValueType EVT;
2365 switch (LowBits) {
2366 default: EVT = MVT::Other; break;
2367 case 1: EVT = MVT::i1; break;
2368 case 8: EVT = MVT::i8; break;
2369 case 16: EVT = MVT::i16; break;
2370 case 32: EVT = MVT::i32; break;
2371 }
2372 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
2373 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2374 DAG.getValueType(EVT));
2375 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002376
2377 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2378 if (N1C && N0.getOpcode() == ISD::SRA) {
2379 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2380 unsigned Sum = N1C->getValue() + C1->getValue();
2381 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
2382 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2383 DAG.getConstant(Sum, N1C->getValueType(0)));
2384 }
2385 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002386
2387 // fold sra (shl X, m), result_size - n
2388 // -> (sign_extend (trunc (shl X, result_size - n - m))) for
Christopher Lambb9b04282008-03-20 04:31:39 +00002389 // result_size - n != m.
2390 // If truncate is free for the target sext(shl) is likely to result in better
2391 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002392 if (N0.getOpcode() == ISD::SHL) {
2393 // Get the two constanst of the shifts, CN0 = m, CN = n.
2394 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2395 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002396 // Determine what the truncate's result bitsize and type would be.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002397 unsigned VTValSize = MVT::getSizeInBits(VT);
2398 MVT::ValueType TruncVT = MVT::getIntegerType(VTValSize - N1C->getValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002399 // Determine the residual right-shift amount.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002400 unsigned ShiftAmt = N1C->getValue() - N01C->getValue();
Christopher Lambb9b04282008-03-20 04:31:39 +00002401
2402 // If the shift is not a no-op (in which case this should be just a sign
2403 // extend already), the truncated to type is legal, sign_extend is legal
2404 // on that type, and the the truncate to that type is both legal and free,
2405 // perform the transform.
2406 if (ShiftAmt &&
2407 TLI.isTypeLegal(TruncVT) &&
2408 TLI.isOperationLegal(ISD::SIGN_EXTEND, TruncVT) &&
2409 TLI.isOperationLegal(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002410 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002411
2412 SDOperand Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
2413 SDOperand Shift = DAG.getNode(ISD::SRL, VT, N0.getOperand(0), Amt);
2414 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, TruncVT, Shift);
2415 return DAG.getNode(ISD::SIGN_EXTEND, N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002416 }
2417 }
2418 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002419
Chris Lattnera8504462006-05-08 20:51:54 +00002420 // Simplify, based on bits shifted out of the LHS.
2421 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2422 return SDOperand(N, 0);
2423
2424
Nate Begeman1d4d4142005-09-01 00:19:25 +00002425 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002426 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002427 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002428
2429 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002430}
2431
Nate Begeman83e75ec2005-09-06 04:43:02 +00002432SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002433 SDOperand N0 = N->getOperand(0);
2434 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002435 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2436 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002437 MVT::ValueType VT = N0.getValueType();
2438 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2439
2440 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002441 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002442 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002443 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002444 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002445 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002446 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002447 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002448 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002449 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002450 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002451 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002452 // if (srl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002453 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
2454 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002455 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002456
Nate Begeman1d4d4142005-09-01 00:19:25 +00002457 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002458 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002459 N0.getOperand(1).getOpcode() == ISD::Constant) {
2460 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002461 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002462 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002463 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002464 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002465 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002466 }
Chris Lattner350bec02006-04-02 06:11:11 +00002467
Chris Lattner06afe072006-05-05 22:53:17 +00002468 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2469 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2470 // Shifting in all undef bits?
2471 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
2472 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
2473 return DAG.getNode(ISD::UNDEF, VT);
2474
2475 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
2476 AddToWorkList(SmallShift.Val);
2477 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2478 }
2479
Chris Lattner3657ffe2006-10-12 20:23:19 +00002480 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2481 // bit, which is unmodified by sra.
2482 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
2483 if (N0.getOpcode() == ISD::SRA)
2484 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2485 }
2486
Chris Lattner350bec02006-04-02 06:11:11 +00002487 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2488 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002489 N1C->getAPIntValue() == Log2_32(MVT::getSizeInBits(VT))) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002490 APInt KnownZero, KnownOne;
2491 APInt Mask = APInt::getAllOnesValue(MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +00002492 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002493
2494 // If any of the input bits are KnownOne, then the input couldn't be all
2495 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002496 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002497
2498 // If all of the bits input the to ctlz node are known to be zero, then
2499 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002500 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002501 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2502
2503 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2504 if ((UnknownBits & (UnknownBits-1)) == 0) {
2505 // Okay, we know that only that the single bit specified by UnknownBits
2506 // could be set on input to the CTLZ node. If this bit is set, the SRL
2507 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2508 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002509 unsigned ShAmt = UnknownBits.countTrailingZeros();
Chris Lattner350bec02006-04-02 06:11:11 +00002510 SDOperand Op = N0.getOperand(0);
2511 if (ShAmt) {
2512 Op = DAG.getNode(ISD::SRL, VT, Op,
2513 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2514 AddToWorkList(Op.Val);
2515 }
2516 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2517 }
2518 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002519
2520 // fold operands of srl based on knowledge that the low bits are not
2521 // demanded.
2522 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2523 return SDOperand(N, 0);
2524
Chris Lattnere70da202007-12-06 07:33:36 +00002525 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002526}
2527
Nate Begeman83e75ec2005-09-06 04:43:02 +00002528SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002529 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002530 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002531
2532 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002533 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002534 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002535 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002536}
2537
Nate Begeman83e75ec2005-09-06 04:43:02 +00002538SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002539 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002540 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002541
2542 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002543 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002544 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002545 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002546}
2547
Nate Begeman83e75ec2005-09-06 04:43:02 +00002548SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002549 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002550 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002551
2552 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002553 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002554 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002555 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002556}
2557
Nate Begeman452d7be2005-09-16 00:54:12 +00002558SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2559 SDOperand N0 = N->getOperand(0);
2560 SDOperand N1 = N->getOperand(1);
2561 SDOperand N2 = N->getOperand(2);
2562 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2563 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2564 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2565 MVT::ValueType VT = N->getValueType(0);
Evan Cheng571c4782007-08-18 05:57:05 +00002566 MVT::ValueType VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002567
Nate Begeman452d7be2005-09-16 00:54:12 +00002568 // fold select C, X, X -> X
2569 if (N1 == N2)
2570 return N1;
2571 // fold select true, X, Y -> X
2572 if (N0C && !N0C->isNullValue())
2573 return N1;
2574 // fold select false, X, Y -> Y
2575 if (N0C && N0C->isNullValue())
2576 return N2;
2577 // fold select C, 1, X -> C | X
Dan Gohman002e5d02008-03-13 22:13:53 +00002578 if (MVT::i1 == VT && N1C && N1C->getAPIntValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002579 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002580 // fold select C, 0, 1 -> ~C
2581 if (MVT::isInteger(VT) && MVT::isInteger(VT0) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002582 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Evan Cheng571c4782007-08-18 05:57:05 +00002583 SDOperand XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
2584 if (VT == VT0)
2585 return XORNode;
2586 AddToWorkList(XORNode.Val);
2587 if (MVT::getSizeInBits(VT) > MVT::getSizeInBits(VT0))
2588 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2589 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2590 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002591 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002592 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
2593 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002594 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002595 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2596 }
2597 // fold select C, X, 1 -> ~C | X
Dan Gohman002e5d02008-03-13 22:13:53 +00002598 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002599 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002600 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002601 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2602 }
2603 // fold select C, X, 0 -> C & X
2604 // FIXME: this should check for C type == X type, not i1?
2605 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2606 return DAG.getNode(ISD::AND, VT, N0, N1);
2607 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2608 if (MVT::i1 == VT && N0 == N1)
2609 return DAG.getNode(ISD::OR, VT, N0, N2);
2610 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2611 if (MVT::i1 == VT && N0 == N2)
2612 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002613
Chris Lattner40c62d52005-10-18 06:04:22 +00002614 // If we can fold this based on the true/false value, do so.
2615 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002616 return SDOperand(N, 0); // Don't revisit N.
2617
Nate Begeman44728a72005-09-19 22:34:01 +00002618 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002619 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002620 // FIXME:
2621 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2622 // having to say they don't support SELECT_CC on every type the DAG knows
2623 // about, since there is no way to mark an opcode illegal at all value types
2624 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2625 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2626 N1, N2, N0.getOperand(2));
2627 else
2628 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002629 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002630 return SDOperand();
2631}
2632
2633SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002634 SDOperand N0 = N->getOperand(0);
2635 SDOperand N1 = N->getOperand(1);
2636 SDOperand N2 = N->getOperand(2);
2637 SDOperand N3 = N->getOperand(3);
2638 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002639 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2640
Nate Begeman44728a72005-09-19 22:34:01 +00002641 // fold select_cc lhs, rhs, x, x, cc -> x
2642 if (N2 == N3)
2643 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002644
Chris Lattner5f42a242006-09-20 06:19:26 +00002645 // Determine if the condition we're dealing with is constant
Scott Michel5b8f82e2008-03-10 15:42:14 +00002646 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002647 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002648
2649 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002650 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00002651 return N2; // cond always true -> true val
2652 else
2653 return N3; // cond always false -> false val
2654 }
2655
2656 // Fold to a simpler select_cc
2657 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2658 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2659 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2660 SCC.getOperand(2));
2661
Chris Lattner40c62d52005-10-18 06:04:22 +00002662 // If we can fold this based on the true/false value, do so.
2663 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002664 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002665
Nate Begeman44728a72005-09-19 22:34:01 +00002666 // fold select_cc into other things, such as min/max/abs
2667 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002668}
2669
2670SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2671 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2672 cast<CondCodeSDNode>(N->getOperand(2))->get());
2673}
2674
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002675// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2676// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2677// transformation. Returns true if extension are possible and the above
2678// mentioned transformation is profitable.
2679static bool ExtendUsesToFormExtLoad(SDNode *N, SDOperand N0,
2680 unsigned ExtOpc,
2681 SmallVector<SDNode*, 4> &ExtendNodes,
2682 TargetLowering &TLI) {
2683 bool HasCopyToRegUses = false;
2684 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
2685 for (SDNode::use_iterator UI = N0.Val->use_begin(), UE = N0.Val->use_end();
2686 UI != UE; ++UI) {
2687 SDNode *User = *UI;
2688 if (User == N)
2689 continue;
2690 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2691 if (User->getOpcode() == ISD::SETCC) {
2692 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2693 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2694 // Sign bits will be lost after a zext.
2695 return false;
2696 bool Add = false;
2697 for (unsigned i = 0; i != 2; ++i) {
2698 SDOperand UseOp = User->getOperand(i);
2699 if (UseOp == N0)
2700 continue;
2701 if (!isa<ConstantSDNode>(UseOp))
2702 return false;
2703 Add = true;
2704 }
2705 if (Add)
2706 ExtendNodes.push_back(User);
2707 } else {
2708 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2709 SDOperand UseOp = User->getOperand(i);
2710 if (UseOp == N0) {
2711 // If truncate from extended type to original load type is free
2712 // on this target, then it's ok to extend a CopyToReg.
2713 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2714 HasCopyToRegUses = true;
2715 else
2716 return false;
2717 }
2718 }
2719 }
2720 }
2721
2722 if (HasCopyToRegUses) {
2723 bool BothLiveOut = false;
2724 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2725 UI != UE; ++UI) {
2726 SDNode *User = *UI;
2727 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2728 SDOperand UseOp = User->getOperand(i);
2729 if (UseOp.Val == N && UseOp.ResNo == 0) {
2730 BothLiveOut = true;
2731 break;
2732 }
2733 }
2734 }
2735 if (BothLiveOut)
2736 // Both unextended and extended values are live out. There had better be
2737 // good a reason for the transformation.
2738 return ExtendNodes.size();
2739 }
2740 return true;
2741}
2742
Nate Begeman83e75ec2005-09-06 04:43:02 +00002743SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002744 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002745 MVT::ValueType VT = N->getValueType(0);
2746
Nate Begeman1d4d4142005-09-01 00:19:25 +00002747 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002748 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002749 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002750
Nate Begeman1d4d4142005-09-01 00:19:25 +00002751 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002752 // fold (sext (aext x)) -> (sext x)
2753 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002754 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002755
Evan Chengc88138f2007-03-22 01:54:19 +00002756 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2757 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Chris Lattner22558872007-02-26 03:13:59 +00002758 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002759 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002760 if (NarrowLoad.Val) {
2761 if (NarrowLoad.Val != N0.Val)
2762 CombineTo(N0.Val, NarrowLoad);
2763 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2764 }
Evan Chengc88138f2007-03-22 01:54:19 +00002765 }
2766
2767 // See if the value being truncated is already sign extended. If so, just
2768 // eliminate the trunc/sext pair.
2769 if (N0.getOpcode() == ISD::TRUNCATE) {
Chris Lattner6007b842006-09-21 06:00:20 +00002770 SDOperand Op = N0.getOperand(0);
Chris Lattner22558872007-02-26 03:13:59 +00002771 unsigned OpBits = MVT::getSizeInBits(Op.getValueType());
2772 unsigned MidBits = MVT::getSizeInBits(N0.getValueType());
2773 unsigned DestBits = MVT::getSizeInBits(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002774 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002775
2776 if (OpBits == DestBits) {
2777 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2778 // bits, it is already ready.
2779 if (NumSignBits > DestBits-MidBits)
2780 return Op;
2781 } else if (OpBits < DestBits) {
2782 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2783 // bits, just sext from i32.
2784 if (NumSignBits > OpBits-MidBits)
2785 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2786 } else {
2787 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2788 // bits, just truncate to i32.
2789 if (NumSignBits > OpBits-MidBits)
2790 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002791 }
Chris Lattner22558872007-02-26 03:13:59 +00002792
2793 // fold (sext (truncate x)) -> (sextinreg x).
2794 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2795 N0.getValueType())) {
2796 if (Op.getValueType() < VT)
2797 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2798 else if (Op.getValueType() > VT)
2799 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2800 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2801 DAG.getValueType(N0.getValueType()));
2802 }
Chris Lattner6007b842006-09-21 06:00:20 +00002803 }
Chris Lattner310b5782006-05-06 23:06:26 +00002804
Evan Cheng110dec22005-12-14 02:19:23 +00002805 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002806 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002807 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002808 bool DoXform = true;
2809 SmallVector<SDNode*, 4> SetCCs;
2810 if (!N0.hasOneUse())
2811 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2812 if (DoXform) {
2813 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2814 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2815 LN0->getBasePtr(), LN0->getSrcValue(),
2816 LN0->getSrcValueOffset(),
2817 N0.getValueType(),
2818 LN0->isVolatile(),
2819 LN0->getAlignment());
2820 CombineTo(N, ExtLoad);
2821 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2822 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2823 // Extend SetCC uses if necessary.
2824 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2825 SDNode *SetCC = SetCCs[i];
2826 SmallVector<SDOperand, 4> Ops;
2827 for (unsigned j = 0; j != 2; ++j) {
2828 SDOperand SOp = SetCC->getOperand(j);
2829 if (SOp == Trunc)
2830 Ops.push_back(ExtLoad);
2831 else
2832 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2833 }
2834 Ops.push_back(SetCC->getOperand(2));
2835 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2836 &Ops[0], Ops.size()));
2837 }
2838 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2839 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002840 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002841
2842 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2843 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002844 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2845 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002846 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002847 MVT::ValueType EVT = LN0->getMemoryVT();
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002848 if (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
2849 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2850 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002851 LN0->getSrcValueOffset(), EVT,
2852 LN0->isVolatile(),
2853 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002854 CombineTo(N, ExtLoad);
2855 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2856 ExtLoad.getValue(1));
2857 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2858 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002859 }
2860
Chris Lattner20a35c32007-04-11 05:32:27 +00002861 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2862 if (N0.getOpcode() == ISD::SETCC) {
2863 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002864 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2865 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2866 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2867 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002868 }
2869
Nate Begeman83e75ec2005-09-06 04:43:02 +00002870 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002871}
2872
Nate Begeman83e75ec2005-09-06 04:43:02 +00002873SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002874 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002875 MVT::ValueType VT = N->getValueType(0);
2876
Nate Begeman1d4d4142005-09-01 00:19:25 +00002877 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002878 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002879 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002880 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002881 // fold (zext (aext x)) -> (zext x)
2882 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002883 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002884
Evan Chengc88138f2007-03-22 01:54:19 +00002885 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2886 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002887 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002888 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002889 if (NarrowLoad.Val) {
2890 if (NarrowLoad.Val != N0.Val)
2891 CombineTo(N0.Val, NarrowLoad);
2892 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2893 }
Evan Chengc88138f2007-03-22 01:54:19 +00002894 }
2895
Chris Lattner6007b842006-09-21 06:00:20 +00002896 // fold (zext (truncate x)) -> (and x, mask)
2897 if (N0.getOpcode() == ISD::TRUNCATE &&
2898 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2899 SDOperand Op = N0.getOperand(0);
2900 if (Op.getValueType() < VT) {
2901 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2902 } else if (Op.getValueType() > VT) {
2903 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2904 }
2905 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2906 }
2907
Chris Lattner111c2282006-09-21 06:14:31 +00002908 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2909 if (N0.getOpcode() == ISD::AND &&
2910 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2911 N0.getOperand(1).getOpcode() == ISD::Constant) {
2912 SDOperand X = N0.getOperand(0).getOperand(0);
2913 if (X.getValueType() < VT) {
2914 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2915 } else if (X.getValueType() > VT) {
2916 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2917 }
Dan Gohman220a8232008-03-03 23:51:38 +00002918 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2919 Mask.zext(MVT::getSizeInBits(VT));
Chris Lattner111c2282006-09-21 06:14:31 +00002920 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2921 }
2922
Evan Cheng110dec22005-12-14 02:19:23 +00002923 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002924 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002925 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002926 bool DoXform = true;
2927 SmallVector<SDNode*, 4> SetCCs;
2928 if (!N0.hasOneUse())
2929 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
2930 if (DoXform) {
2931 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2932 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2933 LN0->getBasePtr(), LN0->getSrcValue(),
2934 LN0->getSrcValueOffset(),
2935 N0.getValueType(),
2936 LN0->isVolatile(),
2937 LN0->getAlignment());
2938 CombineTo(N, ExtLoad);
2939 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2940 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2941 // Extend SetCC uses if necessary.
2942 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2943 SDNode *SetCC = SetCCs[i];
2944 SmallVector<SDOperand, 4> Ops;
2945 for (unsigned j = 0; j != 2; ++j) {
2946 SDOperand SOp = SetCC->getOperand(j);
2947 if (SOp == Trunc)
2948 Ops.push_back(ExtLoad);
2949 else
Evan Chengde1631b2007-10-30 20:11:21 +00002950 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002951 }
2952 Ops.push_back(SetCC->getOperand(2));
2953 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2954 &Ops[0], Ops.size()));
2955 }
2956 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2957 }
Evan Cheng110dec22005-12-14 02:19:23 +00002958 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002959
2960 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2961 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002962 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2963 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002964 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002965 MVT::ValueType EVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002966 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2967 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002968 LN0->getSrcValueOffset(), EVT,
2969 LN0->isVolatile(),
2970 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002971 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002972 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2973 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002974 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002975 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002976
2977 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2978 if (N0.getOpcode() == ISD::SETCC) {
2979 SDOperand SCC =
2980 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2981 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00002982 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2983 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002984 }
2985
Nate Begeman83e75ec2005-09-06 04:43:02 +00002986 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002987}
2988
Chris Lattner5ffc0662006-05-05 05:58:59 +00002989SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2990 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002991 MVT::ValueType VT = N->getValueType(0);
2992
2993 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002994 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002995 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2996 // fold (aext (aext x)) -> (aext x)
2997 // fold (aext (zext x)) -> (zext x)
2998 // fold (aext (sext x)) -> (sext x)
2999 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3000 N0.getOpcode() == ISD::ZERO_EXTEND ||
3001 N0.getOpcode() == ISD::SIGN_EXTEND)
3002 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
3003
Evan Chengc88138f2007-03-22 01:54:19 +00003004 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3005 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3006 if (N0.getOpcode() == ISD::TRUNCATE) {
3007 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00003008 if (NarrowLoad.Val) {
3009 if (NarrowLoad.Val != N0.Val)
3010 CombineTo(N0.Val, NarrowLoad);
3011 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
3012 }
Evan Chengc88138f2007-03-22 01:54:19 +00003013 }
3014
Chris Lattner84750582006-09-20 06:29:17 +00003015 // fold (aext (truncate x))
3016 if (N0.getOpcode() == ISD::TRUNCATE) {
3017 SDOperand TruncOp = N0.getOperand(0);
3018 if (TruncOp.getValueType() == VT)
3019 return TruncOp; // x iff x size == zext size.
3020 if (TruncOp.getValueType() > VT)
3021 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
3022 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
3023 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00003024
3025 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3026 if (N0.getOpcode() == ISD::AND &&
3027 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3028 N0.getOperand(1).getOpcode() == ISD::Constant) {
3029 SDOperand X = N0.getOperand(0).getOperand(0);
3030 if (X.getValueType() < VT) {
3031 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
3032 } else if (X.getValueType() > VT) {
3033 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3034 }
Dan Gohman220a8232008-03-03 23:51:38 +00003035 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
3036 Mask.zext(MVT::getSizeInBits(VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00003037 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3038 }
3039
Chris Lattner5ffc0662006-05-05 05:58:59 +00003040 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00003041 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003042 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003043 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3044 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3045 LN0->getBasePtr(), LN0->getSrcValue(),
3046 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003047 N0.getValueType(),
3048 LN0->isVolatile(),
3049 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003050 CombineTo(N, ExtLoad);
3051 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3052 ExtLoad.getValue(1));
3053 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3054 }
3055
3056 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3057 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3058 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003059 if (N0.getOpcode() == ISD::LOAD &&
3060 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003061 N0.hasOneUse()) {
3062 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003063 MVT::ValueType EVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00003064 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
3065 LN0->getChain(), LN0->getBasePtr(),
3066 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003067 LN0->getSrcValueOffset(), EVT,
3068 LN0->isVolatile(),
3069 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003070 CombineTo(N, ExtLoad);
3071 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3072 ExtLoad.getValue(1));
3073 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3074 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003075
3076 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3077 if (N0.getOpcode() == ISD::SETCC) {
3078 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003079 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3080 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003081 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00003082 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003083 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003084 }
3085
Chris Lattner5ffc0662006-05-05 05:58:59 +00003086 return SDOperand();
3087}
3088
Chris Lattner2b4c2792007-10-13 06:35:54 +00003089/// GetDemandedBits - See if the specified operand can be simplified with the
3090/// knowledge that only the bits specified by Mask are used. If so, return the
3091/// simpler operand, otherwise return a null SDOperand.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003092SDOperand DAGCombiner::GetDemandedBits(SDOperand V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003093 switch (V.getOpcode()) {
3094 default: break;
3095 case ISD::OR:
3096 case ISD::XOR:
3097 // If the LHS or RHS don't contribute bits to the or, drop them.
3098 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3099 return V.getOperand(1);
3100 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3101 return V.getOperand(0);
3102 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003103 case ISD::SRL:
3104 // Only look at single-use SRLs.
3105 if (!V.Val->hasOneUse())
3106 break;
3107 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3108 // See if we can recursively simplify the LHS.
3109 unsigned Amt = RHSC->getValue();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003110 APInt NewMask = Mask << Amt;
3111 SDOperand SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Chris Lattnere33544c2007-10-13 06:58:48 +00003112 if (SimplifyLHS.Val) {
3113 return DAG.getNode(ISD::SRL, V.getValueType(),
3114 SimplifyLHS, V.getOperand(1));
3115 }
3116 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003117 }
3118 return SDOperand();
3119}
3120
Evan Chengc88138f2007-03-22 01:54:19 +00003121/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3122/// bits and then truncated to a narrower type and where N is a multiple
3123/// of number of bits of the narrower type, transform it to a narrower load
3124/// from address + N / num of bits of new type. If the result is to be
3125/// extended, also fold the extension to form a extending load.
3126SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
3127 unsigned Opc = N->getOpcode();
3128 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
3129 SDOperand N0 = N->getOperand(0);
3130 MVT::ValueType VT = N->getValueType(0);
3131 MVT::ValueType EVT = N->getValueType(0);
3132
Evan Chenge177e302007-03-23 22:13:36 +00003133 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3134 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003135 if (Opc == ISD::SIGN_EXTEND_INREG) {
3136 ExtType = ISD::SEXTLOAD;
3137 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00003138 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
3139 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00003140 }
3141
3142 unsigned EVTBits = MVT::getSizeInBits(EVT);
3143 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003144 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003145 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3146 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3147 ShAmt = N01->getValue();
3148 // Is the shift amount a multiple of size of VT?
3149 if ((ShAmt & (EVTBits-1)) == 0) {
3150 N0 = N0.getOperand(0);
3151 if (MVT::getSizeInBits(N0.getValueType()) <= EVTBits)
3152 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00003153 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003154 }
3155 }
3156 }
3157
3158 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
3159 // Do not allow folding to i1 here. i1 is implicitly stored in memory in
3160 // zero extended form: by shrinking the load, we lose track of the fact
3161 // that it is already zero extended.
3162 // FIXME: This should be reevaluated.
3163 VT != MVT::i1) {
3164 assert(MVT::getSizeInBits(N0.getValueType()) > EVTBits &&
3165 "Cannot truncate to larger type!");
3166 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3167 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003168 // For big endian targets, we need to adjust the offset to the pointer to
3169 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003170 if (TLI.isBigEndian()) {
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003171 unsigned LVTStoreBits = MVT::getStoreSizeInBits(N0.getValueType());
3172 unsigned EVTStoreBits = MVT::getStoreSizeInBits(EVT);
3173 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3174 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003175 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003176 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Evan Chengc88138f2007-03-22 01:54:19 +00003177 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
3178 DAG.getConstant(PtrOff, PtrType));
3179 AddToWorkList(NewPtr.Val);
3180 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
3181 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003182 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Duncan Sandsdc846502007-10-28 12:59:45 +00003183 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003184 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003185 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00003186 LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003187 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003188 if (CombineSRL) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003189 WorkListRemover DeadNodes(*this);
3190 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3191 &DeadNodes);
Evan Chengb37b80c2007-03-23 20:55:21 +00003192 CombineTo(N->getOperand(0).Val, Load);
3193 } else
3194 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003195 if (ShAmt) {
3196 if (Opc == ISD::SIGN_EXTEND_INREG)
3197 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3198 else
3199 return DAG.getNode(Opc, VT, Load);
3200 }
Evan Chengc88138f2007-03-22 01:54:19 +00003201 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3202 }
3203
3204 return SDOperand();
3205}
3206
Chris Lattner5ffc0662006-05-05 05:58:59 +00003207
Nate Begeman83e75ec2005-09-06 04:43:02 +00003208SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003209 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003210 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003211 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003212 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003213 unsigned VTBits = MVT::getSizeInBits(VT);
Nate Begeman07ed4172005-10-10 21:26:48 +00003214 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003215
Nate Begeman1d4d4142005-09-01 00:19:25 +00003216 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003217 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003218 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003219
Chris Lattner541a24f2006-05-06 22:43:44 +00003220 // If the input is already sign extended, just drop the extension.
Dan Gohmanea859be2007-06-22 14:59:07 +00003221 if (DAG.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003222 return N0;
3223
Nate Begeman646d7e22005-09-02 21:18:40 +00003224 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3225 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3226 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003227 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003228 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003229
Chris Lattner95a5e052007-04-17 19:03:21 +00003230 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003231 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003232 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003233
Chris Lattner95a5e052007-04-17 19:03:21 +00003234 // fold operands of sext_in_reg based on knowledge that the top bits are not
3235 // demanded.
3236 if (SimplifyDemandedBits(SDOperand(N, 0)))
3237 return SDOperand(N, 0);
3238
Evan Chengc88138f2007-03-22 01:54:19 +00003239 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3240 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
3241 SDOperand NarrowLoad = ReduceLoadWidth(N);
3242 if (NarrowLoad.Val)
3243 return NarrowLoad;
3244
Chris Lattner4b37e872006-05-08 21:18:59 +00003245 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3246 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3247 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3248 if (N0.getOpcode() == ISD::SRL) {
3249 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
3250 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
3251 // We can turn this into an SRA iff the input to the SRL is already sign
3252 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003253 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Chris Lattner4b37e872006-05-08 21:18:59 +00003254 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
3255 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3256 }
3257 }
Evan Chengc88138f2007-03-22 01:54:19 +00003258
Nate Begemanded49632005-10-13 03:11:28 +00003259 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00003260 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003261 ISD::isUNINDEXEDLoad(N0.Val) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003262 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003263 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003264 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3265 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3266 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003267 LN0->getSrcValueOffset(), EVT,
3268 LN0->isVolatile(),
3269 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003270 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003271 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003272 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003273 }
3274 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00003275 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
3276 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003277 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003278 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003279 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3280 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3281 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003282 LN0->getSrcValueOffset(), EVT,
3283 LN0->isVolatile(),
3284 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003285 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003286 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003287 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003288 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003289 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003290}
3291
Nate Begeman83e75ec2005-09-06 04:43:02 +00003292SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003293 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003294 MVT::ValueType VT = N->getValueType(0);
3295
3296 // noop truncate
3297 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003298 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003299 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003300 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003301 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003302 // fold (truncate (truncate x)) -> (truncate x)
3303 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003304 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003305 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003306 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3307 N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003308 if (N0.getOperand(0).getValueType() < VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003309 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003310 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003311 else if (N0.getOperand(0).getValueType() > VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003312 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003313 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003314 else
3315 // if the source and dest are the same type, we can drop both the extend
3316 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003317 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003318 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003319
Chris Lattner2b4c2792007-10-13 06:35:54 +00003320 // See if we can simplify the input to this truncate through knowledge that
3321 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3322 // -> trunc y
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003323 SDOperand Shorter =
3324 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
3325 MVT::getSizeInBits(VT)));
Chris Lattner2b4c2792007-10-13 06:35:54 +00003326 if (Shorter.Val)
3327 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3328
Nate Begeman3df4d522005-10-12 20:40:40 +00003329 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003330 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003331 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003332}
3333
Chris Lattner94683772005-12-23 05:30:37 +00003334SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3335 SDOperand N0 = N->getOperand(0);
3336 MVT::ValueType VT = N->getValueType(0);
3337
Dan Gohman7f321562007-06-25 16:23:39 +00003338 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3339 // Only do this before legalize, since afterward the target may be depending
3340 // on the bitconvert.
3341 // First check to see if this is all constant.
3342 if (!AfterLegalize &&
3343 N0.getOpcode() == ISD::BUILD_VECTOR && N0.Val->hasOneUse() &&
3344 MVT::isVector(VT)) {
3345 bool isSimple = true;
3346 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3347 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3348 N0.getOperand(i).getOpcode() != ISD::Constant &&
3349 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3350 isSimple = false;
3351 break;
3352 }
3353
3354 MVT::ValueType DestEltVT = MVT::getVectorElementType(N->getValueType(0));
3355 assert(!MVT::isVector(DestEltVT) &&
3356 "Element type of vector ValueType must not be vector!");
3357 if (isSimple) {
3358 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.Val, DestEltVT);
3359 }
3360 }
3361
Chris Lattner94683772005-12-23 05:30:37 +00003362 // If the input is a constant, let getNode() fold it.
3363 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
3364 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3365 if (Res.Val != N) return Res;
3366 }
3367
Chris Lattnerc8547d82005-12-23 05:37:50 +00003368 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3369 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003370
Chris Lattner57104102005-12-23 05:44:41 +00003371 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003372 // If the resultant load doesn't need a higher alignment than the original!
3373 if (ISD::isNormalLoad(N0.Val) && N0.hasOneUse() &&
Evan Cheng59d5b682007-05-07 21:27:48 +00003374 TLI.isOperationLegal(ISD::LOAD, VT)) {
Evan Cheng466685d2006-10-09 20:57:25 +00003375 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00003376 unsigned Align = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00003377 getABITypeAlignment(MVT::getTypeForValueType(VT));
Evan Cheng59d5b682007-05-07 21:27:48 +00003378 unsigned OrigAlign = LN0->getAlignment();
3379 if (Align <= OrigAlign) {
3380 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
3381 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003382 LN0->isVolatile(), Align);
Evan Cheng59d5b682007-05-07 21:27:48 +00003383 AddToWorkList(N);
3384 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
3385 Load.getValue(1));
3386 return Load;
3387 }
Chris Lattner57104102005-12-23 05:44:41 +00003388 }
3389
Chris Lattner3bd39d42008-01-27 17:42:27 +00003390 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3391 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3392 // This often reduces constant pool loads.
3393 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
3394 N0.Val->hasOneUse() && MVT::isInteger(VT) && !MVT::isVector(VT)) {
3395 SDOperand NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3396 AddToWorkList(NewConv.Val);
3397
Dan Gohman220a8232008-03-03 23:51:38 +00003398 APInt SignBit = APInt::getSignBit(MVT::getSizeInBits(VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00003399 if (N0.getOpcode() == ISD::FNEG)
3400 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3401 assert(N0.getOpcode() == ISD::FABS);
3402 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3403 }
3404
3405 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3406 // Note that we don't handle copysign(x,cst) because this can always be folded
3407 // to an fneg or fabs.
3408 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003409 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
3410 MVT::isInteger(VT) && !MVT::isVector(VT)) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003411 unsigned OrigXWidth = MVT::getSizeInBits(N0.getOperand(1).getValueType());
3412 SDOperand X = DAG.getNode(ISD::BIT_CONVERT, MVT::getIntegerType(OrigXWidth),
3413 N0.getOperand(1));
3414 AddToWorkList(X.Val);
3415
3416 // If X has a different width than the result/lhs, sext it or truncate it.
3417 unsigned VTWidth = MVT::getSizeInBits(VT);
3418 if (OrigXWidth < VTWidth) {
3419 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
3420 AddToWorkList(X.Val);
3421 } else if (OrigXWidth > VTWidth) {
3422 // To get the sign bit in the right place, we have to shift it right
3423 // before truncating.
3424 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3425 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3426 AddToWorkList(X.Val);
3427 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3428 AddToWorkList(X.Val);
3429 }
3430
Dan Gohman220a8232008-03-03 23:51:38 +00003431 APInt SignBit = APInt::getSignBit(MVT::getSizeInBits(VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00003432 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
3433 AddToWorkList(X.Val);
3434
3435 SDOperand Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3436 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
3437 AddToWorkList(Cst.Val);
3438
3439 return DAG.getNode(ISD::OR, VT, X, Cst);
3440 }
3441
Chris Lattner94683772005-12-23 05:30:37 +00003442 return SDOperand();
3443}
3444
Dan Gohman7f321562007-06-25 16:23:39 +00003445/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003446/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3447/// destination element value type.
3448SDOperand DAGCombiner::
Dan Gohman7f321562007-06-25 16:23:39 +00003449ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003450 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
3451
3452 // If this is already the right type, we're done.
3453 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
3454
3455 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
3456 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
3457
3458 // If this is a conversion of N elements of one type to N elements of another
3459 // type, convert each element. This handles FP<->INT cases.
3460 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003461 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003462 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003463 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00003464 AddToWorkList(Ops.back().Val);
3465 }
Dan Gohman7f321562007-06-25 16:23:39 +00003466 MVT::ValueType VT =
3467 MVT::getVectorType(DstEltVT,
3468 MVT::getVectorNumElements(BV->getValueType(0)));
3469 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003470 }
3471
3472 // Otherwise, we're growing or shrinking the elements. To avoid having to
3473 // handle annoying details of growing/shrinking FP values, we convert them to
3474 // int first.
3475 if (MVT::isFloatingPoint(SrcEltVT)) {
3476 // Convert the input float vector to a int vector where the elements are the
3477 // same sizes.
3478 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
3479 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003480 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003481 SrcEltVT = IntVT;
3482 }
3483
3484 // Now we know the input is an integer vector. If the output is a FP type,
3485 // convert to integer first, then to FP of the right size.
3486 if (MVT::isFloatingPoint(DstEltVT)) {
3487 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
3488 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003489 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003490
3491 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003492 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003493 }
3494
3495 // Okay, we know the src/dst types are both integers of differing types.
3496 // Handling growing first.
3497 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
3498 if (SrcBitSize < DstBitSize) {
3499 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3500
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003501 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003502 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003503 i += NumInputsPerOutput) {
3504 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003505 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003506 bool EltIsUndef = true;
3507 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3508 // Shift the previously computed bits over.
3509 NewBits <<= SrcBitSize;
3510 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
3511 if (Op.getOpcode() == ISD::UNDEF) continue;
3512 EltIsUndef = false;
3513
Dan Gohman220a8232008-03-03 23:51:38 +00003514 NewBits |=
3515 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003516 }
3517
3518 if (EltIsUndef)
3519 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3520 else
3521 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3522 }
3523
Evan Chengefec7512008-02-18 23:04:32 +00003524 MVT::ValueType VT = MVT::getVectorType(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003525 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003526 }
3527
3528 // Finally, this must be the case where we are shrinking elements: each input
3529 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003530 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003531 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Evan Chengefec7512008-02-18 23:04:32 +00003532 MVT::ValueType VT = MVT::getVectorType(DstEltVT,
3533 NumOutputsPerInput * BV->getNumOperands());
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003534 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003535 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003536 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3537 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3538 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3539 continue;
3540 }
Dan Gohman220a8232008-03-03 23:51:38 +00003541 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003542 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003543 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003544 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003545 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003546 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3547 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003548 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003549 }
3550
3551 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003552 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003553 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3554 }
Dan Gohman7f321562007-06-25 16:23:39 +00003555 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003556}
3557
3558
3559
Chris Lattner01b3d732005-09-28 22:28:18 +00003560SDOperand DAGCombiner::visitFADD(SDNode *N) {
3561 SDOperand N0 = N->getOperand(0);
3562 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003563 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3564 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003565 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003566
Dan Gohman7f321562007-06-25 16:23:39 +00003567 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003568 if (MVT::isVector(VT)) {
3569 SDOperand FoldedVOp = SimplifyVBinOp(N);
3570 if (FoldedVOp.Val) return FoldedVOp;
3571 }
Dan Gohman7f321562007-06-25 16:23:39 +00003572
Nate Begemana0e221d2005-10-18 00:28:13 +00003573 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003574 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003575 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003576 // canonicalize constant to RHS
3577 if (N0CFP && !N1CFP)
3578 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003579 // fold (A + (-B)) -> A-B
Chris Lattner0254e702008-02-26 07:04:54 +00003580 if (isNegatibleForFree(N1, AfterLegalize) == 2)
3581 return DAG.getNode(ISD::FSUB, VT, N0,
3582 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner01b3d732005-09-28 22:28:18 +00003583 // fold ((-A) + B) -> B-A
Chris Lattner0254e702008-02-26 07:04:54 +00003584 if (isNegatibleForFree(N0, AfterLegalize) == 2)
3585 return DAG.getNode(ISD::FSUB, VT, N1,
3586 GetNegatedExpression(N0, DAG, AfterLegalize));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003587
3588 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3589 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
3590 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3591 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3592 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3593
Chris Lattner01b3d732005-09-28 22:28:18 +00003594 return SDOperand();
3595}
3596
3597SDOperand DAGCombiner::visitFSUB(SDNode *N) {
3598 SDOperand N0 = N->getOperand(0);
3599 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003600 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3601 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003602 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003603
Dan Gohman7f321562007-06-25 16:23:39 +00003604 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003605 if (MVT::isVector(VT)) {
3606 SDOperand FoldedVOp = SimplifyVBinOp(N);
3607 if (FoldedVOp.Val) return FoldedVOp;
3608 }
Dan Gohman7f321562007-06-25 16:23:39 +00003609
Nate Begemana0e221d2005-10-18 00:28:13 +00003610 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003611 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003612 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003613 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003614 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Chris Lattner0254e702008-02-26 07:04:54 +00003615 if (isNegatibleForFree(N1, AfterLegalize))
3616 return GetNegatedExpression(N1, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003617 return DAG.getNode(ISD::FNEG, VT, N1);
3618 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003619 // fold (A-(-B)) -> A+B
Chris Lattner0254e702008-02-26 07:04:54 +00003620 if (isNegatibleForFree(N1, AfterLegalize))
3621 return DAG.getNode(ISD::FADD, VT, N0,
3622 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003623
Chris Lattner01b3d732005-09-28 22:28:18 +00003624 return SDOperand();
3625}
3626
3627SDOperand DAGCombiner::visitFMUL(SDNode *N) {
3628 SDOperand N0 = N->getOperand(0);
3629 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003630 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3631 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003632 MVT::ValueType VT = N->getValueType(0);
3633
Dan Gohman7f321562007-06-25 16:23:39 +00003634 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003635 if (MVT::isVector(VT)) {
3636 SDOperand FoldedVOp = SimplifyVBinOp(N);
3637 if (FoldedVOp.Val) return FoldedVOp;
3638 }
Dan Gohman7f321562007-06-25 16:23:39 +00003639
Nate Begeman11af4ea2005-10-17 20:40:11 +00003640 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003641 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003642 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003643 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003644 if (N0CFP && !N1CFP)
3645 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003646 // fold (fmul X, 2.0) -> (fadd X, X)
3647 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3648 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003649 // fold (fmul X, -1.0) -> (fneg X)
3650 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3651 return DAG.getNode(ISD::FNEG, VT, N0);
3652
3653 // -X * -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003654 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3655 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003656 // Both can be negated for free, check to see if at least one is cheaper
3657 // negated.
3658 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003659 return DAG.getNode(ISD::FMUL, VT,
3660 GetNegatedExpression(N0, DAG, AfterLegalize),
3661 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003662 }
3663 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003664
3665 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3666 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
3667 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3668 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3669 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3670
Chris Lattner01b3d732005-09-28 22:28:18 +00003671 return SDOperand();
3672}
3673
3674SDOperand DAGCombiner::visitFDIV(SDNode *N) {
3675 SDOperand N0 = N->getOperand(0);
3676 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003677 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3678 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003679 MVT::ValueType VT = N->getValueType(0);
3680
Dan Gohman7f321562007-06-25 16:23:39 +00003681 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003682 if (MVT::isVector(VT)) {
3683 SDOperand FoldedVOp = SimplifyVBinOp(N);
3684 if (FoldedVOp.Val) return FoldedVOp;
3685 }
Dan Gohman7f321562007-06-25 16:23:39 +00003686
Nate Begemana148d982006-01-18 22:35:16 +00003687 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003688 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003689 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003690
3691
3692 // -X / -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003693 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3694 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003695 // Both can be negated for free, check to see if at least one is cheaper
3696 // negated.
3697 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003698 return DAG.getNode(ISD::FDIV, VT,
3699 GetNegatedExpression(N0, DAG, AfterLegalize),
3700 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003701 }
3702 }
3703
Chris Lattner01b3d732005-09-28 22:28:18 +00003704 return SDOperand();
3705}
3706
3707SDOperand DAGCombiner::visitFREM(SDNode *N) {
3708 SDOperand N0 = N->getOperand(0);
3709 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003710 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3711 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003712 MVT::ValueType VT = N->getValueType(0);
3713
Nate Begemana148d982006-01-18 22:35:16 +00003714 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003715 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003716 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003717
Chris Lattner01b3d732005-09-28 22:28:18 +00003718 return SDOperand();
3719}
3720
Chris Lattner12d83032006-03-05 05:30:57 +00003721SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3722 SDOperand N0 = N->getOperand(0);
3723 SDOperand N1 = N->getOperand(1);
3724 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3725 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3726 MVT::ValueType VT = N->getValueType(0);
3727
Dale Johannesendb44bf82007-10-16 23:38:29 +00003728 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003729 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3730
3731 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003732 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003733 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3734 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003735 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003736 return DAG.getNode(ISD::FABS, VT, N0);
3737 else
3738 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3739 }
3740
3741 // copysign(fabs(x), y) -> copysign(x, y)
3742 // copysign(fneg(x), y) -> copysign(x, y)
3743 // copysign(copysign(x,z), y) -> copysign(x, y)
3744 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3745 N0.getOpcode() == ISD::FCOPYSIGN)
3746 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3747
3748 // copysign(x, abs(y)) -> abs(x)
3749 if (N1.getOpcode() == ISD::FABS)
3750 return DAG.getNode(ISD::FABS, VT, N0);
3751
3752 // copysign(x, copysign(y,z)) -> copysign(x, z)
3753 if (N1.getOpcode() == ISD::FCOPYSIGN)
3754 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3755
3756 // copysign(x, fp_extend(y)) -> copysign(x, y)
3757 // copysign(x, fp_round(y)) -> copysign(x, y)
3758 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3759 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3760
3761 return SDOperand();
3762}
3763
3764
Chris Lattner01b3d732005-09-28 22:28:18 +00003765
Nate Begeman83e75ec2005-09-06 04:43:02 +00003766SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003767 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003768 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003769 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003770
3771 // fold (sint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003772 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003773 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003774 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003775}
3776
Nate Begeman83e75ec2005-09-06 04:43:02 +00003777SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003778 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003779 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003780 MVT::ValueType VT = N->getValueType(0);
3781
Nate Begeman1d4d4142005-09-01 00:19:25 +00003782 // fold (uint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003783 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003784 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003785 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003786}
3787
Nate Begeman83e75ec2005-09-06 04:43:02 +00003788SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003789 SDOperand N0 = N->getOperand(0);
3790 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3791 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003792
3793 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003794 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003795 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003796 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003797}
3798
Nate Begeman83e75ec2005-09-06 04:43:02 +00003799SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003800 SDOperand N0 = N->getOperand(0);
3801 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3802 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003803
3804 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00003805 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003806 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003807 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003808}
3809
Nate Begeman83e75ec2005-09-06 04:43:02 +00003810SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003811 SDOperand N0 = N->getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003812 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003813 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3814 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003815
3816 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003817 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00003818 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003819
3820 // fold (fp_round (fp_extend x)) -> x
3821 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3822 return N0.getOperand(0);
3823
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00003824 // fold (fp_round (fp_round x)) -> (fp_round x)
3825 if (N0.getOpcode() == ISD::FP_ROUND) {
3826 // This is a value preserving truncation if both round's are.
3827 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
3828 N0.Val->getConstantOperandVal(1) == 1;
3829 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
3830 DAG.getIntPtrConstant(IsTrunc));
3831 }
3832
Chris Lattner79dbea52006-03-13 06:26:26 +00003833 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3834 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003835 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003836 AddToWorkList(Tmp.Val);
3837 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3838 }
3839
Nate Begeman83e75ec2005-09-06 04:43:02 +00003840 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003841}
3842
Nate Begeman83e75ec2005-09-06 04:43:02 +00003843SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003844 SDOperand N0 = N->getOperand(0);
3845 MVT::ValueType VT = N->getValueType(0);
3846 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003847 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003848
Nate Begeman1d4d4142005-09-01 00:19:25 +00003849 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003850 if (N0CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003851 SDOperand Round = DAG.getConstantFP(N0CFP->getValueAPF(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003852 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003853 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003854 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003855}
3856
Nate Begeman83e75ec2005-09-06 04:43:02 +00003857SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003858 SDOperand N0 = N->getOperand(0);
3859 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3860 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003861
Chris Lattner5938bef2007-12-29 06:55:23 +00003862 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
3863 if (N->hasOneUse() && (*N->use_begin())->getOpcode() == ISD::FP_ROUND)
3864 return SDOperand();
Chris Lattner0bd48932008-01-17 07:00:52 +00003865
Nate Begeman1d4d4142005-09-01 00:19:25 +00003866 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003867 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003868 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003869
3870 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
3871 // value of X.
3872 if (N0.getOpcode() == ISD::FP_ROUND && N0.Val->getConstantOperandVal(1) == 1){
3873 SDOperand In = N0.getOperand(0);
3874 if (In.getValueType() == VT) return In;
3875 if (VT < In.getValueType())
3876 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
3877 return DAG.getNode(ISD::FP_EXTEND, VT, In);
3878 }
3879
3880 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Dale Johannesenb6210fc2007-10-19 20:29:00 +00003881 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003882 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003883 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3884 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3885 LN0->getBasePtr(), LN0->getSrcValue(),
3886 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003887 N0.getValueType(),
3888 LN0->isVolatile(),
3889 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003890 CombineTo(N, ExtLoad);
Chris Lattner0bd48932008-01-17 07:00:52 +00003891 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad,
3892 DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00003893 ExtLoad.getValue(1));
3894 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3895 }
3896
3897
Nate Begeman83e75ec2005-09-06 04:43:02 +00003898 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003899}
3900
Nate Begeman83e75ec2005-09-06 04:43:02 +00003901SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003902 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003903
Chris Lattner0254e702008-02-26 07:04:54 +00003904 if (isNegatibleForFree(N0, AfterLegalize))
3905 return GetNegatedExpression(N0, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003906
Chris Lattner3bd39d42008-01-27 17:42:27 +00003907 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
3908 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00003909 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
3910 MVT::isInteger(N0.getOperand(0).getValueType()) &&
3911 !MVT::isVector(N0.getOperand(0).getValueType())) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003912 SDOperand Int = N0.getOperand(0);
3913 MVT::ValueType IntVT = Int.getValueType();
3914 if (MVT::isInteger(IntVT) && !MVT::isVector(IntVT)) {
3915 Int = DAG.getNode(ISD::XOR, IntVT, Int,
3916 DAG.getConstant(MVT::getIntVTSignBit(IntVT), IntVT));
3917 AddToWorkList(Int.Val);
3918 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
3919 }
3920 }
3921
Nate Begeman83e75ec2005-09-06 04:43:02 +00003922 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003923}
3924
Nate Begeman83e75ec2005-09-06 04:43:02 +00003925SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003926 SDOperand N0 = N->getOperand(0);
3927 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3928 MVT::ValueType VT = N->getValueType(0);
3929
Nate Begeman1d4d4142005-09-01 00:19:25 +00003930 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003931 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003932 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003933 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003934 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003935 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003936 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003937 // fold (fabs (fcopysign x, y)) -> (fabs x)
3938 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
3939 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
3940
Chris Lattner3bd39d42008-01-27 17:42:27 +00003941 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
3942 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00003943 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
3944 MVT::isInteger(N0.getOperand(0).getValueType()) &&
3945 !MVT::isVector(N0.getOperand(0).getValueType())) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003946 SDOperand Int = N0.getOperand(0);
3947 MVT::ValueType IntVT = Int.getValueType();
3948 if (MVT::isInteger(IntVT) && !MVT::isVector(IntVT)) {
3949 Int = DAG.getNode(ISD::AND, IntVT, Int,
3950 DAG.getConstant(~MVT::getIntVTSignBit(IntVT), IntVT));
3951 AddToWorkList(Int.Val);
3952 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
3953 }
3954 }
3955
Nate Begeman83e75ec2005-09-06 04:43:02 +00003956 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003957}
3958
Nate Begeman44728a72005-09-19 22:34:01 +00003959SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
3960 SDOperand Chain = N->getOperand(0);
3961 SDOperand N1 = N->getOperand(1);
3962 SDOperand N2 = N->getOperand(2);
3963 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3964
3965 // never taken branch, fold to chain
3966 if (N1C && N1C->isNullValue())
3967 return Chain;
3968 // unconditional branch
Dan Gohman002e5d02008-03-13 22:13:53 +00003969 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00003970 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003971 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
3972 // on the target.
3973 if (N1.getOpcode() == ISD::SETCC &&
3974 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
3975 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
3976 N1.getOperand(0), N1.getOperand(1), N2);
3977 }
Nate Begeman44728a72005-09-19 22:34:01 +00003978 return SDOperand();
3979}
3980
Chris Lattner3ea0b472005-10-05 06:47:48 +00003981// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
3982//
Nate Begeman44728a72005-09-19 22:34:01 +00003983SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00003984 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
3985 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
3986
3987 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00003988 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003989 if (Simp.Val) AddToWorkList(Simp.Val);
3990
Nate Begemane17daeb2005-10-05 21:43:42 +00003991 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
3992
3993 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman002e5d02008-03-13 22:13:53 +00003994 if (SCCC && !SCCC->isNullValue())
Nate Begemane17daeb2005-10-05 21:43:42 +00003995 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
3996 N->getOperand(4));
3997 // fold br_cc false, dest -> unconditional fall through
3998 if (SCCC && SCCC->isNullValue())
3999 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00004000
Nate Begemane17daeb2005-10-05 21:43:42 +00004001 // fold to a simpler setcc
4002 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
4003 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4004 Simp.getOperand(2), Simp.getOperand(0),
4005 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00004006 return SDOperand();
4007}
4008
Chris Lattner448f2192006-11-11 00:39:41 +00004009
4010/// CombineToPreIndexedLoadStore - Try turning a load / store and a
4011/// pre-indexed load / store when the base pointer is a add or subtract
4012/// and it has other uses besides the load / store. After the
4013/// transformation, the new indexed load / store has effectively folded
4014/// the add / subtract in and all of its other uses are redirected to the
4015/// new load / store.
4016bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
4017 if (!AfterLegalize)
4018 return false;
4019
4020 bool isLoad = true;
4021 SDOperand Ptr;
4022 MVT::ValueType VT;
4023 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004024 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004025 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004026 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00004027 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00004028 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4029 return false;
4030 Ptr = LD->getBasePtr();
4031 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004032 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004033 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004034 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004035 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4036 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4037 return false;
4038 Ptr = ST->getBasePtr();
4039 isLoad = false;
4040 } else
4041 return false;
4042
Chris Lattner9f1794e2006-11-11 00:56:29 +00004043 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4044 // out. There is no reason to make this a preinc/predec.
4045 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
4046 Ptr.Val->hasOneUse())
4047 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004048
Chris Lattner9f1794e2006-11-11 00:56:29 +00004049 // Ask the target to do addressing mode selection.
4050 SDOperand BasePtr;
4051 SDOperand Offset;
4052 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4053 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4054 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004055 // Don't create a indexed load / store with zero offset.
4056 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004057 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004058 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004059
Chris Lattner41e53fd2006-11-11 01:00:15 +00004060 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004061 // 1) The new base ptr is a frame index.
4062 // 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 +00004063 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004064 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004065 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004066 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004067
Chris Lattner41e53fd2006-11-11 01:00:15 +00004068 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4069 // (plus the implicit offset) to a register to preinc anyway.
4070 if (isa<FrameIndexSDNode>(BasePtr))
4071 return false;
4072
4073 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004074 if (!isLoad) {
4075 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Cheng917be682008-03-04 00:41:45 +00004076 if (Val == BasePtr || BasePtr.Val->isPredecessorOf(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004077 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004078 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004079
Evan Chengc843abe2007-05-24 02:35:39 +00004080 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004081 bool RealUse = false;
4082 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4083 E = Ptr.Val->use_end(); I != E; ++I) {
4084 SDNode *Use = *I;
4085 if (Use == N)
4086 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004087 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004088 return false;
4089
4090 if (!((Use->getOpcode() == ISD::LOAD &&
4091 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004092 (Use->getOpcode() == ISD::STORE &&
4093 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004094 RealUse = true;
4095 }
4096 if (!RealUse)
4097 return false;
4098
4099 SDOperand Result;
4100 if (isLoad)
4101 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
4102 else
4103 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4104 ++PreIndexedNodes;
4105 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004106 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004107 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4108 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004109 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004110 if (isLoad) {
4111 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004112 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004113 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004114 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004115 } else {
4116 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004117 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004118 }
4119
Chris Lattner9f1794e2006-11-11 00:56:29 +00004120 // Finally, since the node is now dead, remove it from the graph.
4121 DAG.DeleteNode(N);
4122
4123 // Replace the uses of Ptr with uses of the updated base value.
4124 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004125 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004126 removeFromWorkList(Ptr.Val);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004127 DAG.DeleteNode(Ptr.Val);
4128
4129 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004130}
4131
4132/// CombineToPostIndexedLoadStore - Try combine a load / store with a
4133/// add / sub of the base pointer node into a post-indexed load / store.
4134/// The transformation folded the add / subtract into the new indexed
4135/// load / store effectively and all of its uses are redirected to the
4136/// new load / store.
4137bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
4138 if (!AfterLegalize)
4139 return false;
4140
4141 bool isLoad = true;
4142 SDOperand Ptr;
4143 MVT::ValueType VT;
4144 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004145 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004146 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004147 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004148 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4149 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4150 return false;
4151 Ptr = LD->getBasePtr();
4152 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004153 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004154 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004155 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004156 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4157 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4158 return false;
4159 Ptr = ST->getBasePtr();
4160 isLoad = false;
4161 } else
4162 return false;
4163
Evan Chengcc470212006-11-16 00:08:20 +00004164 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004165 return false;
4166
4167 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4168 E = Ptr.Val->use_end(); I != E; ++I) {
4169 SDNode *Op = *I;
4170 if (Op == N ||
4171 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4172 continue;
4173
4174 SDOperand BasePtr;
4175 SDOperand Offset;
4176 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4177 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4178 if (Ptr == Offset)
4179 std::swap(BasePtr, Offset);
4180 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004181 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004182 // Don't create a indexed load / store with zero offset.
4183 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004184 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004185 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004186
Chris Lattner9f1794e2006-11-11 00:56:29 +00004187 // Try turning it into a post-indexed load / store except when
4188 // 1) All uses are load / store ops that use it as base ptr.
4189 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4190 // nor a successor of N. Otherwise, if Op is folded that would
4191 // create a cycle.
4192
4193 // Check for #1.
4194 bool TryNext = false;
4195 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
4196 EE = BasePtr.Val->use_end(); II != EE; ++II) {
4197 SDNode *Use = *II;
4198 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00004199 continue;
4200
Chris Lattner9f1794e2006-11-11 00:56:29 +00004201 // If all the uses are load / store addresses, then don't do the
4202 // transformation.
4203 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4204 bool RealUse = false;
4205 for (SDNode::use_iterator III = Use->use_begin(),
4206 EEE = Use->use_end(); III != EEE; ++III) {
4207 SDNode *UseUse = *III;
4208 if (!((UseUse->getOpcode() == ISD::LOAD &&
4209 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004210 (UseUse->getOpcode() == ISD::STORE &&
4211 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004212 RealUse = true;
4213 }
Chris Lattner448f2192006-11-11 00:39:41 +00004214
Chris Lattner9f1794e2006-11-11 00:56:29 +00004215 if (!RealUse) {
4216 TryNext = true;
4217 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004218 }
4219 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004220 }
4221 if (TryNext)
4222 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004223
Chris Lattner9f1794e2006-11-11 00:56:29 +00004224 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004225 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Chris Lattner9f1794e2006-11-11 00:56:29 +00004226 SDOperand Result = isLoad
4227 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
4228 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4229 ++PostIndexedNodes;
4230 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004231 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004232 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4233 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004234 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004235 if (isLoad) {
4236 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004237 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004238 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004239 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004240 } else {
4241 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004242 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004243 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004244
Chris Lattner9f1794e2006-11-11 00:56:29 +00004245 // Finally, since the node is now dead, remove it from the graph.
4246 DAG.DeleteNode(N);
4247
4248 // Replace the uses of Use with uses of the updated base value.
4249 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
4250 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004251 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004252 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004253 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004254 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004255 }
4256 }
4257 }
4258 return false;
4259}
4260
Chris Lattner00161a62008-01-25 07:20:16 +00004261/// InferAlignment - If we can infer some alignment information from this
4262/// pointer, return it.
4263static unsigned InferAlignment(SDOperand Ptr, SelectionDAG &DAG) {
4264 // If this is a direct reference to a stack slot, use information about the
4265 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004266 int FrameIdx = 1 << 31;
4267 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004268 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004269 FrameIdx = FI->getIndex();
4270 } else if (Ptr.getOpcode() == ISD::ADD &&
4271 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4272 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4273 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4274 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004275 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004276
4277 if (FrameIdx != (1 << 31)) {
4278 // FIXME: Handle FI+CST.
4279 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4280 if (MFI.isFixedObjectIndex(FrameIdx)) {
4281 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx);
4282
4283 // The alignment of the frame index can be determined from its offset from
4284 // the incoming frame position. If the frame object is at offset 32 and
4285 // the stack is guaranteed to be 16-byte aligned, then we know that the
4286 // object is 16-byte aligned.
4287 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4288 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4289
4290 // Finally, the frame object itself may have a known alignment. Factor
4291 // the alignment + offset into a new alignment. For example, if we know
4292 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4293 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4294 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4295 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4296 FrameOffset);
4297 return std::max(Align, FIInfoAlign);
4298 }
4299 }
Chris Lattner00161a62008-01-25 07:20:16 +00004300
4301 return 0;
4302}
Chris Lattner448f2192006-11-11 00:39:41 +00004303
Chris Lattner01a22022005-10-10 22:04:48 +00004304SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004305 LoadSDNode *LD = cast<LoadSDNode>(N);
4306 SDOperand Chain = LD->getChain();
4307 SDOperand Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004308
4309 // Try to infer better alignment information than the load already has.
4310 if (LD->isUnindexed()) {
4311 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4312 if (Align > LD->getAlignment())
4313 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4314 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004315 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004316 LD->isVolatile(), Align);
4317 }
4318 }
4319
Evan Cheng45a7ca92007-05-01 00:38:21 +00004320
4321 // If load is not volatile and there are no uses of the loaded value (and
4322 // the updated indexed value in case of indexed loads), change uses of the
4323 // chain value into uses of the chain input (i.e. delete the dead load).
4324 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004325 if (N->getValueType(1) == MVT::Other) {
4326 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004327 if (N->hasNUsesOfValue(0, 0)) {
4328 // It's not safe to use the two value CombineTo variant here. e.g.
4329 // v1, chain2 = load chain1, loc
4330 // v2, chain3 = load chain2, loc
4331 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004332 // Now we replace use of chain2 with chain1. This makes the second load
4333 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004334 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004335 DOUT << "\nWith chain: "; DEBUG(Chain.Val->dump(&DAG));
4336 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004337 WorkListRemover DeadNodes(*this);
4338 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004339 if (N->use_empty()) {
4340 removeFromWorkList(N);
4341 DAG.DeleteNode(N);
4342 }
Evan Cheng02c42852008-01-16 23:11:54 +00004343 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
4344 }
Evan Cheng498f5592007-05-01 08:53:39 +00004345 } else {
4346 // Indexed loads.
4347 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4348 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Evan Cheng02c42852008-01-16 23:11:54 +00004349 SDOperand Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
4350 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
4351 DOUT << "\nWith: "; DEBUG(Undef.Val->dump(&DAG));
4352 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004353 WorkListRemover DeadNodes(*this);
4354 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Undef, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004355 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004356 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004357 &DeadNodes);
4358 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004359 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004360 DAG.DeleteNode(N);
4361 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004362 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004363 }
4364 }
Chris Lattner01a22022005-10-10 22:04:48 +00004365
4366 // If this load is directly stored, replace the load value with the stored
4367 // value.
4368 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004369 // TODO: Handle TRUNCSTORE/LOADEXT
4370 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004371 if (ISD::isNON_TRUNCStore(Chain.Val)) {
4372 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4373 if (PrevST->getBasePtr() == Ptr &&
4374 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004375 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004376 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004377 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004378
Jim Laskey7ca56af2006-10-11 13:47:09 +00004379 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004380 // Walk up chain skipping non-aliasing memory nodes.
4381 SDOperand BetterChain = FindBetterChain(N, Chain);
4382
Jim Laskey6ff23e52006-10-04 16:53:27 +00004383 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004384 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004385 SDOperand ReplLoad;
4386
Jim Laskey279f0532006-09-25 16:29:54 +00004387 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004388 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4389 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004390 LD->getSrcValue(), LD->getSrcValueOffset(),
4391 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004392 } else {
4393 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4394 LD->getValueType(0),
4395 BetterChain, Ptr, LD->getSrcValue(),
4396 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004397 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004398 LD->isVolatile(),
4399 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004400 }
Jim Laskey279f0532006-09-25 16:29:54 +00004401
Jim Laskey6ff23e52006-10-04 16:53:27 +00004402 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00004403 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
4404 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004405
Jim Laskey274062c2006-10-13 23:32:28 +00004406 // Replace uses with load result and token factor. Don't add users
4407 // to work list.
4408 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004409 }
4410 }
4411
Evan Cheng7fc033a2006-11-03 03:06:21 +00004412 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004413 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00004414 return SDOperand(N, 0);
4415
Chris Lattner01a22022005-10-10 22:04:48 +00004416 return SDOperand();
4417}
4418
Chris Lattner07649d92008-01-08 23:08:06 +00004419
Chris Lattner87514ca2005-10-10 22:31:19 +00004420SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004421 StoreSDNode *ST = cast<StoreSDNode>(N);
4422 SDOperand Chain = ST->getChain();
4423 SDOperand Value = ST->getValue();
4424 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004425
Chris Lattner00161a62008-01-25 07:20:16 +00004426 // Try to infer better alignment information than the store already has.
4427 if (ST->isUnindexed()) {
4428 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4429 if (Align > ST->getAlignment())
4430 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004431 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004432 ST->isVolatile(), Align);
4433 }
4434 }
4435
Evan Cheng59d5b682007-05-07 21:27:48 +00004436 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004437 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004438 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004439 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004440 unsigned Align = ST->getAlignment();
4441 MVT::ValueType SVT = Value.getOperand(0).getValueType();
4442 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00004443 getABITypeAlignment(MVT::getTypeForValueType(SVT));
Evan Chengc2cd2b22007-05-07 21:36:06 +00004444 if (Align <= OrigAlign && TLI.isOperationLegal(ISD::STORE, SVT))
Evan Cheng59d5b682007-05-07 21:27:48 +00004445 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004446 ST->getSrcValueOffset(), ST->isVolatile(), Align);
Jim Laskey279f0532006-09-25 16:29:54 +00004447 }
4448
Nate Begeman2cbba892006-12-11 02:23:46 +00004449 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004450 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Evan Cheng25ece662006-12-11 17:25:19 +00004451 if (Value.getOpcode() != ISD::TargetConstantFP) {
4452 SDOperand Tmp;
Chris Lattner62be1a72006-12-12 04:16:14 +00004453 switch (CFP->getValueType(0)) {
4454 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004455 case MVT::f80: // We don't do this for these yet.
4456 case MVT::f128:
4457 case MVT::ppcf128:
4458 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004459 case MVT::f32:
4460 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004461 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
4462 convertToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004463 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004464 ST->getSrcValueOffset(), ST->isVolatile(),
4465 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004466 }
4467 break;
4468 case MVT::f64:
4469 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004470 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
4471 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004472 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004473 ST->getSrcValueOffset(), ST->isVolatile(),
4474 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004475 } else if (TLI.isTypeLegal(MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004476 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004477 // argument passing. Since this is so common, custom legalize the
4478 // 64-bit integer store into two 32-bit stores.
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004479 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
Chris Lattner62be1a72006-12-12 04:16:14 +00004480 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4481 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004482 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004483
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004484 int SVOffset = ST->getSrcValueOffset();
4485 unsigned Alignment = ST->getAlignment();
4486 bool isVolatile = ST->isVolatile();
4487
Chris Lattner62be1a72006-12-12 04:16:14 +00004488 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004489 ST->getSrcValueOffset(),
4490 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004491 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4492 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004493 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004494 Alignment = MinAlign(Alignment, 4U);
Chris Lattner62be1a72006-12-12 04:16:14 +00004495 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004496 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004497 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4498 }
4499 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004500 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004501 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004502 }
4503
Jim Laskey279f0532006-09-25 16:29:54 +00004504 if (CombinerAA) {
4505 // Walk up chain skipping non-aliasing memory nodes.
4506 SDOperand BetterChain = FindBetterChain(N, Chain);
4507
Jim Laskey6ff23e52006-10-04 16:53:27 +00004508 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004509 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004510 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004511 SDOperand ReplStore;
4512 if (ST->isTruncatingStore()) {
4513 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004514 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004515 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004516 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004517 } else {
4518 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004519 ST->getSrcValue(), ST->getSrcValueOffset(),
4520 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004521 }
4522
Jim Laskey279f0532006-09-25 16:29:54 +00004523 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00004524 SDOperand Token =
4525 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4526
4527 // Don't add users to work list.
4528 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004529 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004530 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004531
Evan Cheng33dbedc2006-11-05 09:31:14 +00004532 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004533 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00004534 return SDOperand(N, 0);
4535
Chris Lattner3c872852007-12-29 06:26:16 +00004536 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004537 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Chris Lattner2b4c2792007-10-13 06:35:54 +00004538 MVT::isInteger(Value.getValueType())) {
4539 // See if we can simplify the input to this truncstore with knowledge that
4540 // only the low bits are being used. For example:
4541 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
4542 SDOperand Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004543 GetDemandedBits(Value,
4544 APInt::getLowBitsSet(Value.getValueSizeInBits(),
4545 MVT::getSizeInBits(ST->getMemoryVT())));
Chris Lattner2b4c2792007-10-13 06:35:54 +00004546 AddToWorkList(Value.Val);
4547 if (Shorter.Val)
4548 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004549 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004550 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004551
4552 // Otherwise, see if we can simplify the operation with
4553 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004554 if (SimplifyDemandedBits(Value,
4555 APInt::getLowBitsSet(
4556 Value.getValueSizeInBits(),
4557 MVT::getSizeInBits(ST->getMemoryVT()))))
Chris Lattnere33544c2007-10-13 06:58:48 +00004558 return SDOperand(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004559 }
4560
Chris Lattner3c872852007-12-29 06:26:16 +00004561 // If this is a load followed by a store to the same location, then the store
4562 // is dead/noop.
4563 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004564 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004565 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004566 // There can't be any side effects between the load and store, such as
4567 // a call or store.
Chris Lattner572dee72008-01-16 05:49:24 +00004568 Chain.reachesChainWithoutSideEffects(SDOperand(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004569 // The store is dead, remove it.
4570 return Chain;
4571 }
4572 }
4573
Chris Lattnerddf89562008-01-17 19:59:44 +00004574 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4575 // truncating store. We can do this even if this is already a truncstore.
4576 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
4577 && TLI.isTypeLegal(Value.getOperand(0).getValueType()) &&
4578 Value.Val->hasOneUse() && ST->isUnindexed() &&
4579 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004580 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004581 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004582 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004583 ST->isVolatile(), ST->getAlignment());
4584 }
4585
Chris Lattner87514ca2005-10-10 22:31:19 +00004586 return SDOperand();
4587}
4588
Chris Lattnerca242442006-03-19 01:27:56 +00004589SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4590 SDOperand InVec = N->getOperand(0);
4591 SDOperand InVal = N->getOperand(1);
4592 SDOperand EltNo = N->getOperand(2);
4593
4594 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4595 // vector with the inserted element.
4596 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
4597 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004598 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004599 if (Elt < Ops.size())
4600 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004601 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4602 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004603 }
4604
4605 return SDOperand();
4606}
4607
Evan Cheng513da432007-10-06 08:19:55 +00004608SDOperand DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
4609 SDOperand InVec = N->getOperand(0);
4610 SDOperand EltNo = N->getOperand(1);
4611
4612 // (vextract (v4f32 s2v (f32 load $addr)), 0) -> (f32 load $addr)
4613 // (vextract (v4i32 bc (v4f32 s2v (f32 load $addr))), 0) -> (i32 load $addr)
4614 if (isa<ConstantSDNode>(EltNo)) {
4615 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
4616 bool NewLoad = false;
4617 if (Elt == 0) {
4618 MVT::ValueType VT = InVec.getValueType();
4619 MVT::ValueType EVT = MVT::getVectorElementType(VT);
4620 MVT::ValueType LVT = EVT;
4621 unsigned NumElts = MVT::getVectorNumElements(VT);
4622 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
4623 MVT::ValueType BCVT = InVec.getOperand(0).getValueType();
Dan Gohman090b38a2007-10-29 20:44:42 +00004624 if (!MVT::isVector(BCVT) ||
4625 NumElts != MVT::getVectorNumElements(BCVT))
Evan Cheng513da432007-10-06 08:19:55 +00004626 return SDOperand();
4627 InVec = InVec.getOperand(0);
4628 EVT = MVT::getVectorElementType(BCVT);
4629 NewLoad = true;
4630 }
4631 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4632 InVec.getOperand(0).getValueType() == EVT &&
4633 ISD::isNormalLoad(InVec.getOperand(0).Val) &&
4634 InVec.getOperand(0).hasOneUse()) {
4635 LoadSDNode *LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4636 unsigned Align = LN0->getAlignment();
4637 if (NewLoad) {
4638 // Check the resultant load doesn't need a higher alignment than the
4639 // original load.
4640 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
4641 getABITypeAlignment(MVT::getTypeForValueType(LVT));
4642 if (!TLI.isOperationLegal(ISD::LOAD, LVT) || NewAlign > Align)
4643 return SDOperand();
4644 Align = NewAlign;
4645 }
4646
4647 return DAG.getLoad(LVT, LN0->getChain(), LN0->getBasePtr(),
4648 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4649 LN0->isVolatile(), Align);
4650 }
4651 }
4652 }
4653 return SDOperand();
4654}
4655
4656
Dan Gohman7f321562007-06-25 16:23:39 +00004657SDOperand DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
4658 unsigned NumInScalars = N->getNumOperands();
4659 MVT::ValueType VT = N->getValueType(0);
4660 unsigned NumElts = MVT::getVectorNumElements(VT);
4661 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattnerca242442006-03-19 01:27:56 +00004662
Dan Gohman7f321562007-06-25 16:23:39 +00004663 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4664 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4665 // at most two distinct vectors, turn this into a shuffle node.
Chris Lattnerd7648c82006-03-28 20:28:38 +00004666 SDOperand VecIn1, VecIn2;
4667 for (unsigned i = 0; i != NumInScalars; ++i) {
4668 // Ignore undef inputs.
4669 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4670
Dan Gohman7f321562007-06-25 16:23:39 +00004671 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004672 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004673 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004674 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
4675 VecIn1 = VecIn2 = SDOperand(0, 0);
4676 break;
4677 }
4678
Dan Gohman7f321562007-06-25 16:23:39 +00004679 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004680 // we can't make a shuffle.
4681 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004682 if (ExtractedFromVec.getValueType() != VT) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004683 VecIn1 = VecIn2 = SDOperand(0, 0);
4684 break;
4685 }
4686
4687 // Otherwise, remember this. We allow up to two distinct input vectors.
4688 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4689 continue;
4690
4691 if (VecIn1.Val == 0) {
4692 VecIn1 = ExtractedFromVec;
4693 } else if (VecIn2.Val == 0) {
4694 VecIn2 = ExtractedFromVec;
4695 } else {
4696 // Too many inputs.
4697 VecIn1 = VecIn2 = SDOperand(0, 0);
4698 break;
4699 }
4700 }
4701
4702 // If everything is good, we can make a shuffle operation.
4703 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004704 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004705 for (unsigned i = 0; i != NumInScalars; ++i) {
4706 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004707 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004708 continue;
4709 }
4710
4711 SDOperand Extract = N->getOperand(i);
4712
4713 // If extracting from the first vector, just use the index directly.
4714 if (Extract.getOperand(0) == VecIn1) {
4715 BuildVecIndices.push_back(Extract.getOperand(1));
4716 continue;
4717 }
4718
4719 // Otherwise, use InIdx + VecSize
4720 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004721 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004722 }
4723
4724 // Add count and size info.
Chris Lattner0bd48932008-01-17 07:00:52 +00004725 MVT::ValueType BuildVecVT = MVT::getVectorType(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004726
Dan Gohman7f321562007-06-25 16:23:39 +00004727 // Return the new VECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004728 SDOperand Ops[5];
4729 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00004730 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004731 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004732 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004733 // Use an undef build_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00004734 std::vector<SDOperand> UnOps(NumInScalars,
4735 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004736 EltType));
4737 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004738 &UnOps[0], UnOps.size());
4739 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00004740 }
Dan Gohman7f321562007-06-25 16:23:39 +00004741 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004742 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004743 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004744 }
4745
4746 return SDOperand();
4747}
4748
Dan Gohman7f321562007-06-25 16:23:39 +00004749SDOperand DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
4750 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
4751 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
4752 // inputs come from at most two distinct vectors, turn this into a shuffle
4753 // node.
4754
4755 // If we only have one input vector, we don't need to do any concatenation.
4756 if (N->getNumOperands() == 1) {
4757 return N->getOperand(0);
4758 }
4759
4760 return SDOperand();
4761}
4762
Chris Lattner66445d32006-03-28 22:11:53 +00004763SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004764 SDOperand ShufMask = N->getOperand(2);
4765 unsigned NumElts = ShufMask.getNumOperands();
4766
4767 // If the shuffle mask is an identity operation on the LHS, return the LHS.
4768 bool isIdentity = true;
4769 for (unsigned i = 0; i != NumElts; ++i) {
4770 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4771 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
4772 isIdentity = false;
4773 break;
4774 }
4775 }
4776 if (isIdentity) return N->getOperand(0);
4777
4778 // If the shuffle mask is an identity operation on the RHS, return the RHS.
4779 isIdentity = true;
4780 for (unsigned i = 0; i != NumElts; ++i) {
4781 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4782 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
4783 isIdentity = false;
4784 break;
4785 }
4786 }
4787 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004788
4789 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
4790 // needed at all.
4791 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00004792 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004793 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00004794 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004795 for (unsigned i = 0; i != NumElts; ++i)
4796 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
4797 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
4798 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00004799 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004800 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004801 BaseIdx = Idx;
4802 } else {
4803 if (BaseIdx != Idx)
4804 isSplat = false;
4805 if (VecNum != V) {
4806 isUnary = false;
4807 break;
4808 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004809 }
4810 }
4811
4812 SDOperand N0 = N->getOperand(0);
4813 SDOperand N1 = N->getOperand(1);
4814 // Normalize unary shuffle so the RHS is undef.
4815 if (isUnary && VecNum == 1)
4816 std::swap(N0, N1);
4817
Evan Cheng917ec982006-07-21 08:25:53 +00004818 // If it is a splat, check if the argument vector is a build_vector with
4819 // all scalar elements the same.
4820 if (isSplat) {
4821 SDNode *V = N0.Val;
Evan Cheng917ec982006-07-21 08:25:53 +00004822
Dan Gohman7f321562007-06-25 16:23:39 +00004823 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004824 // not the number of vector elements, look through it. Be careful not to
4825 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004826 if (V->getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng59569222006-10-16 22:49:37 +00004827 SDOperand ConvInput = V->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004828 if (MVT::getVectorNumElements(ConvInput.getValueType()) == NumElts)
Evan Cheng59569222006-10-16 22:49:37 +00004829 V = ConvInput.Val;
4830 }
4831
Dan Gohman7f321562007-06-25 16:23:39 +00004832 if (V->getOpcode() == ISD::BUILD_VECTOR) {
4833 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00004834 if (NumElems > BaseIdx) {
4835 SDOperand Base;
4836 bool AllSame = true;
4837 for (unsigned i = 0; i != NumElems; ++i) {
4838 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4839 Base = V->getOperand(i);
4840 break;
4841 }
4842 }
4843 // Splat of <u, u, u, u>, return <u, u, u, u>
4844 if (!Base.Val)
4845 return N0;
4846 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00004847 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00004848 AllSame = false;
4849 break;
4850 }
4851 }
4852 // Splat of <x, x, x, x>, return <x, x, x, x>
4853 if (AllSame)
4854 return N0;
4855 }
4856 }
4857 }
4858
Evan Chenge7bec0d2006-07-20 22:44:41 +00004859 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4860 // into an undef.
4861 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004862 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4863 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004864 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004865 for (unsigned i = 0; i != NumElts; ++i) {
4866 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4867 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4868 MappedOps.push_back(ShufMask.getOperand(i));
4869 } else {
4870 unsigned NewIdx =
4871 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
4872 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
4873 }
4874 }
Dan Gohman7f321562007-06-25 16:23:39 +00004875 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004876 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004877 AddToWorkList(ShufMask.Val);
Dan Gohman7f321562007-06-25 16:23:39 +00004878 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
4879 N0,
4880 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
4881 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00004882 }
Dan Gohman7f321562007-06-25 16:23:39 +00004883
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004884 return SDOperand();
4885}
4886
Evan Cheng44f1f092006-04-20 08:56:16 +00004887/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00004888/// an AND to a vector_shuffle with the destination vector and a zero vector.
4889/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00004890/// vector_shuffle V, Zero, <0, 4, 2, 4>
4891SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
4892 SDOperand LHS = N->getOperand(0);
4893 SDOperand RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00004894 if (N->getOpcode() == ISD::AND) {
4895 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00004896 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004897 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Evan Cheng44f1f092006-04-20 08:56:16 +00004898 std::vector<SDOperand> IdxOps;
4899 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00004900 unsigned NumElts = NumOps;
4901 MVT::ValueType EVT = MVT::getVectorElementType(RHS.getValueType());
Evan Cheng44f1f092006-04-20 08:56:16 +00004902 for (unsigned i = 0; i != NumElts; ++i) {
4903 SDOperand Elt = RHS.getOperand(i);
4904 if (!isa<ConstantSDNode>(Elt))
4905 return SDOperand();
4906 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
4907 IdxOps.push_back(DAG.getConstant(i, EVT));
4908 else if (cast<ConstantSDNode>(Elt)->isNullValue())
4909 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
4910 else
4911 return SDOperand();
4912 }
4913
4914 // Let's see if the target supports this vector_shuffle.
4915 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
4916 return SDOperand();
4917
Dan Gohman7f321562007-06-25 16:23:39 +00004918 // Return the new VECTOR_SHUFFLE node.
4919 MVT::ValueType VT = MVT::getVectorType(EVT, NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00004920 std::vector<SDOperand> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004921 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00004922 Ops.push_back(LHS);
4923 AddToWorkList(LHS.Val);
4924 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00004925 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004926 &ZeroOps[0], ZeroOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004927 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004928 &IdxOps[0], IdxOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004929 SDOperand Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004930 &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004931 if (VT != LHS.getValueType()) {
4932 Result = DAG.getNode(ISD::BIT_CONVERT, LHS.getValueType(), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00004933 }
4934 return Result;
4935 }
4936 }
4937 return SDOperand();
4938}
4939
Dan Gohman7f321562007-06-25 16:23:39 +00004940/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
4941SDOperand DAGCombiner::SimplifyVBinOp(SDNode *N) {
4942 // After legalize, the target may be depending on adds and other
4943 // binary ops to provide legal ways to construct constants or other
4944 // things. Simplifying them may result in a loss of legality.
4945 if (AfterLegalize) return SDOperand();
4946
4947 MVT::ValueType VT = N->getValueType(0);
Dan Gohman05d92fe2007-07-13 20:03:40 +00004948 assert(MVT::isVector(VT) && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00004949
4950 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattneredab1b92006-04-02 03:25:57 +00004951 SDOperand LHS = N->getOperand(0);
4952 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00004953 SDOperand Shuffle = XformToShuffleWithZero(N);
4954 if (Shuffle.Val) return Shuffle;
4955
Dan Gohman7f321562007-06-25 16:23:39 +00004956 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00004957 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00004958 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
4959 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004960 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004961 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Chris Lattneredab1b92006-04-02 03:25:57 +00004962 SDOperand LHSOp = LHS.getOperand(i);
4963 SDOperand RHSOp = RHS.getOperand(i);
4964 // If these two elements can't be folded, bail out.
4965 if ((LHSOp.getOpcode() != ISD::UNDEF &&
4966 LHSOp.getOpcode() != ISD::Constant &&
4967 LHSOp.getOpcode() != ISD::ConstantFP) ||
4968 (RHSOp.getOpcode() != ISD::UNDEF &&
4969 RHSOp.getOpcode() != ISD::Constant &&
4970 RHSOp.getOpcode() != ISD::ConstantFP))
4971 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00004972 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00004973 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
4974 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00004975 if ((RHSOp.getOpcode() == ISD::Constant &&
4976 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
4977 (RHSOp.getOpcode() == ISD::ConstantFP &&
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00004978 cast<ConstantFPSDNode>(RHSOp.Val)->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00004979 break;
4980 }
Dan Gohman7f321562007-06-25 16:23:39 +00004981 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00004982 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00004983 assert((Ops.back().getOpcode() == ISD::UNDEF ||
4984 Ops.back().getOpcode() == ISD::Constant ||
4985 Ops.back().getOpcode() == ISD::ConstantFP) &&
4986 "Scalar binop didn't fold!");
4987 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004988
Dan Gohman7f321562007-06-25 16:23:39 +00004989 if (Ops.size() == LHS.getNumOperands()) {
4990 MVT::ValueType VT = LHS.getValueType();
4991 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004992 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004993 }
4994
4995 return SDOperand();
4996}
4997
Nate Begeman44728a72005-09-19 22:34:01 +00004998SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00004999 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5000
5001 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
5002 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5003 // If we got a simplified select_cc node back from SimplifySelectCC, then
5004 // break it down into a new SETCC node, and a new SELECT node, and then return
5005 // the SELECT node, since we were called with a SELECT node.
5006 if (SCC.Val) {
5007 // Check to see if we got a select_cc back (to turn into setcc/select).
5008 // Otherwise, just return whatever node we got back, like fabs.
5009 if (SCC.getOpcode() == ISD::SELECT_CC) {
5010 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
5011 SCC.getOperand(0), SCC.getOperand(1),
5012 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00005013 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005014 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5015 SCC.getOperand(3), SETCC);
5016 }
5017 return SCC;
5018 }
Nate Begeman44728a72005-09-19 22:34:01 +00005019 return SDOperand();
5020}
5021
Chris Lattner40c62d52005-10-18 06:04:22 +00005022/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5023/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00005024/// select. Callers of this should assume that TheSelect is deleted if this
5025/// returns true. As such, they should return the appropriate thing (e.g. the
5026/// node) back to the top-level of the DAG combiner loop to avoid it being
5027/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00005028///
5029bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
5030 SDOperand RHS) {
5031
5032 // If this is a select from two identical things, try to pull the operation
5033 // through the select.
5034 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00005035 // If this is a load and the token chain is identical, replace the select
5036 // of two loads with a load through a select of the address to load from.
5037 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5038 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005039 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005040 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005041 LHS.getOperand(0) == RHS.getOperand(0)) {
5042 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5043 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5044
5045 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005046 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005047 // FIXME: this conflates two src values, discarding one. This is not
5048 // the right thing to do, but nothing uses srcvalues now. When they do,
5049 // turn SrcValue into a list of locations.
5050 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005051 if (TheSelect->getOpcode() == ISD::SELECT) {
5052 // Check that the condition doesn't reach either load. If so, folding
5053 // this will induce a cycle into the DAG.
Evan Cheng917be682008-03-04 00:41:45 +00005054 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5055 !RLD->isPredecessorOf(TheSelect->getOperand(0).Val)) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005056 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5057 TheSelect->getOperand(0), LLD->getBasePtr(),
5058 RLD->getBasePtr());
5059 }
5060 } else {
5061 // Check that the condition doesn't reach either load. If so, folding
5062 // this will induce a cycle into the DAG.
Evan Cheng917be682008-03-04 00:41:45 +00005063 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5064 !RLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5065 !LLD->isPredecessorOf(TheSelect->getOperand(1).Val) &&
5066 !RLD->isPredecessorOf(TheSelect->getOperand(1).Val)) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005067 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005068 TheSelect->getOperand(0),
5069 TheSelect->getOperand(1),
5070 LLD->getBasePtr(), RLD->getBasePtr(),
5071 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005072 }
Evan Cheng466685d2006-10-09 20:57:25 +00005073 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005074
5075 if (Addr.Val) {
5076 SDOperand Load;
5077 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5078 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5079 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005080 LLD->getSrcValueOffset(),
5081 LLD->isVolatile(),
5082 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005083 else {
5084 Load = DAG.getExtLoad(LLD->getExtensionType(),
5085 TheSelect->getValueType(0),
5086 LLD->getChain(), Addr, LLD->getSrcValue(),
5087 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005088 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005089 LLD->isVolatile(),
5090 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005091 }
5092 // Users of the select now use the result of the load.
5093 CombineTo(TheSelect, Load);
5094
5095 // Users of the old loads now use the new load's chain. We know the
5096 // old-load value is dead now.
5097 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
5098 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
5099 return true;
5100 }
Evan Chengc5484282006-10-04 00:56:09 +00005101 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005102 }
5103 }
5104
5105 return false;
5106}
5107
Nate Begeman44728a72005-09-19 22:34:01 +00005108SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
5109 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00005110 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005111
5112 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00005113 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
5114 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
5115 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
5116
5117 // Determine if the condition we're dealing with is constant
Scott Michel5b8f82e2008-03-10 15:42:14 +00005118 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00005119 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005120 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
5121
5122 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00005123 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005124 return N2;
5125 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00005126 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005127 return N3;
5128
5129 // Check to see if we can simplify the select into an fabs node
5130 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5131 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005132 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005133 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5134 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5135 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5136 N2 == N3.getOperand(0))
5137 return DAG.getNode(ISD::FABS, VT, N0);
5138
5139 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5140 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5141 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5142 N2.getOperand(0) == N3)
5143 return DAG.getNode(ISD::FABS, VT, N3);
5144 }
5145 }
5146
5147 // Check to see if we can perform the "gzip trick", transforming
5148 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005149 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00005150 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00005151 MVT::isInteger(N2.getValueType()) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005152 (N1C->isNullValue() || // (a < 0) ? b : 0
5153 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00005154 MVT::ValueType XType = N0.getValueType();
5155 MVT::ValueType AType = N2.getValueType();
5156 if (XType >= AType) {
5157 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005158 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00005159 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5160 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Nate Begemanf845b452005-10-08 00:29:44 +00005161 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
5162 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5163 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00005164 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005165 if (XType > AType) {
5166 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005167 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005168 }
5169 return DAG.getNode(ISD::AND, AType, Shift, N2);
5170 }
5171 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5172 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5173 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00005174 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005175 if (XType > AType) {
5176 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005177 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005178 }
5179 return DAG.getNode(ISD::AND, AType, Shift, N2);
5180 }
5181 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005182
5183 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00005184 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Nate Begeman07ed4172005-10-10 21:26:48 +00005185 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005186
5187 // If the caller doesn't want us to simplify this into a zext of a compare,
5188 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00005189 if (NotExtCompare && N2C->getAPIntValue() == 1)
Chris Lattner1eba01e2007-04-11 06:50:51 +00005190 return SDOperand();
5191
Nate Begeman07ed4172005-10-10 21:26:48 +00005192 // Get a SetCC of the condition
5193 // FIXME: Should probably make sure that setcc is legal if we ever have a
5194 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00005195 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005196 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00005197 if (AfterLegalize) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005198 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Chris Lattner555d8d62006-12-07 22:36:47 +00005199 if (N2.getValueType() < SCC.getValueType())
5200 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5201 else
5202 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005203 } else {
5204 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005205 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005206 }
Chris Lattner5750df92006-03-01 04:03:14 +00005207 AddToWorkList(SCC.Val);
5208 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005209
Dan Gohman002e5d02008-03-13 22:13:53 +00005210 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005211 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005212 // shl setcc result by log2 n2c
5213 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00005214 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Nate Begeman07ed4172005-10-10 21:26:48 +00005215 TLI.getShiftAmountTy()));
5216 }
5217
Nate Begemanf845b452005-10-08 00:29:44 +00005218 // Check to see if this is the equivalent of setcc
5219 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5220 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00005221 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005222 MVT::ValueType XType = N0.getValueType();
Scott Michel5b8f82e2008-03-10 15:42:14 +00005223 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(N0))) {
5224 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00005225 if (Res.getValueType() != VT)
5226 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5227 return Res;
5228 }
5229
5230 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5231 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
5232 TLI.isOperationLegal(ISD::CTLZ, XType)) {
5233 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
5234 return DAG.getNode(ISD::SRL, XType, Ctlz,
5235 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
5236 TLI.getShiftAmountTy()));
5237 }
5238 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5239 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
5240 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
5241 N0);
5242 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
5243 DAG.getConstant(~0ULL, XType));
5244 return DAG.getNode(ISD::SRL, XType,
5245 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
5246 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5247 TLI.getShiftAmountTy()));
5248 }
5249 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5250 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
5251 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
5252 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5253 TLI.getShiftAmountTy()));
5254 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5255 }
5256 }
5257
5258 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5259 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5260 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005261 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
5262 N2.getOperand(0) == N1 && MVT::isInteger(N0.getValueType())) {
5263 MVT::ValueType XType = N0.getValueType();
5264 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5265 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5266 TLI.getShiftAmountTy()));
5267 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
5268 AddToWorkList(Shift.Val);
5269 AddToWorkList(Add.Val);
5270 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5271 }
5272 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5273 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5274 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5275 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5276 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Nate Begemanf845b452005-10-08 00:29:44 +00005277 MVT::ValueType XType = N0.getValueType();
5278 if (SubC->isNullValue() && MVT::isInteger(XType)) {
5279 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5280 DAG.getConstant(MVT::getSizeInBits(XType)-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005281 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00005282 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005283 AddToWorkList(Shift.Val);
5284 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005285 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5286 }
5287 }
5288 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005289
Nate Begeman44728a72005-09-19 22:34:01 +00005290 return SDOperand();
5291}
5292
Evan Chengfa1eb272007-02-08 22:13:59 +00005293/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Nate Begeman452d7be2005-09-16 00:54:12 +00005294SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00005295 SDOperand N1, ISD::CondCode Cond,
5296 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005297 TargetLowering::DAGCombinerInfo
5298 DagCombineInfo(DAG, !AfterLegalize, false, this);
5299 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005300}
5301
Nate Begeman69575232005-10-20 02:15:44 +00005302/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5303/// return a DAG expression to select that will generate the same value by
5304/// multiplying by a magic number. See:
5305/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5306SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005307 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005308 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
5309
Andrew Lenharth232c9102006-06-12 16:07:18 +00005310 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005311 ii != ee; ++ii)
5312 AddToWorkList(*ii);
5313 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005314}
5315
5316/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5317/// return a DAG expression to select that will generate the same value by
5318/// multiplying by a magic number. See:
5319/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5320SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005321 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005322 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005323
Andrew Lenharth232c9102006-06-12 16:07:18 +00005324 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005325 ii != ee; ++ii)
5326 AddToWorkList(*ii);
5327 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005328}
5329
Jim Laskey71382342006-10-07 23:37:56 +00005330/// FindBaseOffset - Return true if base is known not to alias with anything
5331/// but itself. Provides base object and offset as results.
5332static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
5333 // Assume it is a primitive operation.
5334 Base = Ptr; Offset = 0;
5335
5336 // If it's an adding a simple constant then integrate the offset.
5337 if (Base.getOpcode() == ISD::ADD) {
5338 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5339 Base = Base.getOperand(0);
5340 Offset += C->getValue();
5341 }
5342 }
5343
5344 // If it's any of the following then it can't alias with anything but itself.
5345 return isa<FrameIndexSDNode>(Base) ||
5346 isa<ConstantPoolSDNode>(Base) ||
5347 isa<GlobalAddressSDNode>(Base);
5348}
5349
5350/// isAlias - Return true if there is any possibility that the two addresses
5351/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00005352bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
5353 const Value *SrcValue1, int SrcValueOffset1,
5354 SDOperand Ptr2, int64_t Size2,
5355 const Value *SrcValue2, int SrcValueOffset2)
5356{
Jim Laskey71382342006-10-07 23:37:56 +00005357 // If they are the same then they must be aliases.
5358 if (Ptr1 == Ptr2) return true;
5359
5360 // Gather base node and offset information.
5361 SDOperand Base1, Base2;
5362 int64_t Offset1, Offset2;
5363 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5364 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5365
5366 // If they have a same base address then...
5367 if (Base1 == Base2) {
5368 // Check to see if the addresses overlap.
5369 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5370 }
5371
Jim Laskey096c22e2006-10-18 12:29:57 +00005372 // If we know both bases then they can't alias.
5373 if (KnownBase1 && KnownBase2) return false;
5374
Jim Laskey07a27092006-10-18 19:08:31 +00005375 if (CombinerGlobalAA) {
5376 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005377 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5378 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5379 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005380 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005381 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005382 if (AAResult == AliasAnalysis::NoAlias)
5383 return false;
5384 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005385
5386 // Otherwise we have to assume they alias.
5387 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005388}
5389
5390/// FindAliasInfo - Extracts the relevant alias information from the memory
5391/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005392bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00005393 SDOperand &Ptr, int64_t &Size,
5394 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005395 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5396 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005397 Size = MVT::getSizeInBits(LD->getMemoryVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005398 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005399 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005400 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005401 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005402 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005403 Size = MVT::getSizeInBits(ST->getMemoryVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005404 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005405 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005406 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005407 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005408 }
5409
5410 return false;
5411}
5412
Jim Laskey6ff23e52006-10-04 16:53:27 +00005413/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5414/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00005415void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005416 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005417 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005418 std::set<SDNode *> Visited; // Visited node set.
5419
Jim Laskey279f0532006-09-25 16:29:54 +00005420 // Get alias information for node.
5421 SDOperand Ptr;
5422 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005423 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005424 int SrcValueOffset;
5425 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005426
Jim Laskey6ff23e52006-10-04 16:53:27 +00005427 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005428 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005429
Jim Laskeybc588b82006-10-05 15:07:25 +00005430 // Look at each chain and determine if it is an alias. If so, add it to the
5431 // aliases list. If not, then continue up the chain looking for the next
5432 // candidate.
5433 while (!Chains.empty()) {
5434 SDOperand Chain = Chains.back();
5435 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005436
Jim Laskeybc588b82006-10-05 15:07:25 +00005437 // Don't bother if we've been before.
5438 if (Visited.find(Chain.Val) != Visited.end()) continue;
5439 Visited.insert(Chain.Val);
5440
5441 switch (Chain.getOpcode()) {
5442 case ISD::EntryToken:
5443 // Entry token is ideal chain operand, but handled in FindBetterChain.
5444 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005445
Jim Laskeybc588b82006-10-05 15:07:25 +00005446 case ISD::LOAD:
5447 case ISD::STORE: {
5448 // Get alias information for Chain.
5449 SDOperand OpPtr;
5450 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005451 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005452 int OpSrcValueOffset;
5453 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
5454 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005455
5456 // If chain is alias then stop here.
5457 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005458 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5459 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005460 Aliases.push_back(Chain);
5461 } else {
5462 // Look further up the chain.
5463 Chains.push_back(Chain.getOperand(0));
5464 // Clean up old chain.
5465 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00005466 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005467 break;
5468 }
5469
5470 case ISD::TokenFactor:
5471 // We have to check each of the operands of the token factor, so we queue
5472 // then up. Adding the operands to the queue (stack) in reverse order
5473 // maintains the original order and increases the likelihood that getNode
5474 // will find a matching token factor (CSE.)
5475 for (unsigned n = Chain.getNumOperands(); n;)
5476 Chains.push_back(Chain.getOperand(--n));
5477 // Eliminate the token factor if we can.
5478 AddToWorkList(Chain.Val);
5479 break;
5480
5481 default:
5482 // For all other instructions we will just have to take what we can get.
5483 Aliases.push_back(Chain);
5484 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005485 }
5486 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005487}
5488
5489/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5490/// for a better chain (aliasing node.)
5491SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
5492 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005493
Jim Laskey6ff23e52006-10-04 16:53:27 +00005494 // Accumulate all the aliases to this node.
5495 GatherAllAliases(N, OldChain, Aliases);
5496
5497 if (Aliases.size() == 0) {
5498 // If no operands then chain to entry token.
5499 return DAG.getEntryNode();
5500 } else if (Aliases.size() == 1) {
5501 // If a single operand then chain to it. We don't need to revisit it.
5502 return Aliases[0];
5503 }
5504
5505 // Construct a custom tailored token factor.
5506 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5507 &Aliases[0], Aliases.size());
5508
5509 // Make sure the old chain gets cleaned up.
5510 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
5511
5512 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005513}
5514
Nate Begeman1d4d4142005-09-01 00:19:25 +00005515// SelectionDAG::Combine - This is the entry point for the file.
5516//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005517void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00005518 if (!RunningAfterLegalize && ViewDAGCombine1)
5519 viewGraph();
5520 if (RunningAfterLegalize && ViewDAGCombine2)
5521 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005522 /// run - This is the main entry point to this class.
5523 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005524 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005525}