blob: dc59006fcfbe54ffd32315add3901b955b83d834 [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)
Roman Levensteindc1adac2008-04-07 10:06:32 +000081 AddToWorkList(UI->getUser());
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
Evan Cheng08b11732008-03-22 01:55:50 +0000761 // If N is a commutative binary node, try commuting it to enable more
762 // sdisel CSE.
763 if (RV.Val == 0 &&
764 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
765 N->getNumValues() == 1) {
766 SDOperand N0 = N->getOperand(0);
767 SDOperand N1 = N->getOperand(1);
768 // Constant operands are canonicalized to RHS.
769 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
770 SDOperand Ops[] = { N1, N0 };
771 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
772 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +0000773 if (CSENode)
Evan Cheng08b11732008-03-22 01:55:50 +0000774 return SDOperand(CSENode, 0);
775 }
776 }
777
Dan Gohman389079b2007-10-08 17:57:15 +0000778 return RV;
779}
780
Chris Lattner6270f682006-10-08 22:57:01 +0000781/// getInputChainForNode - Given a node, return its input chain if it has one,
782/// otherwise return a null sd operand.
783static SDOperand getInputChainForNode(SDNode *N) {
784 if (unsigned NumOps = N->getNumOperands()) {
785 if (N->getOperand(0).getValueType() == MVT::Other)
786 return N->getOperand(0);
787 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
788 return N->getOperand(NumOps-1);
789 for (unsigned i = 1; i < NumOps-1; ++i)
790 if (N->getOperand(i).getValueType() == MVT::Other)
791 return N->getOperand(i);
792 }
793 return SDOperand(0, 0);
794}
795
Nate Begeman83e75ec2005-09-06 04:43:02 +0000796SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000797 // If N has two operands, where one has an input chain equal to the other,
798 // the 'other' chain is redundant.
799 if (N->getNumOperands() == 2) {
800 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
801 return N->getOperand(0);
802 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
803 return N->getOperand(1);
804 }
805
Chris Lattnerc76d4412007-05-16 06:37:59 +0000806 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
807 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
808 SmallPtrSet<SDNode*, 16> SeenOps;
809 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000810
811 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000812 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000813
Jim Laskey71382342006-10-07 23:37:56 +0000814 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000815 // encountered.
816 for (unsigned i = 0; i < TFs.size(); ++i) {
817 SDNode *TF = TFs[i];
818
Jim Laskey6ff23e52006-10-04 16:53:27 +0000819 // Check each of the operands.
820 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
821 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000822
Jim Laskey6ff23e52006-10-04 16:53:27 +0000823 switch (Op.getOpcode()) {
824 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000825 // Entry tokens don't need to be added to the list. They are
826 // rededundant.
827 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000828 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000829
Jim Laskey6ff23e52006-10-04 16:53:27 +0000830 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000831 if ((CombinerAA || Op.hasOneUse()) &&
832 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000833 // Queue up for processing.
834 TFs.push_back(Op.Val);
835 // Clean up in case the token factor is removed.
836 AddToWorkList(Op.Val);
837 Changed = true;
838 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000839 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000840 // Fall thru
841
842 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000843 // Only add if it isn't already in the list.
844 if (SeenOps.insert(Op.Val))
Jim Laskeybc588b82006-10-05 15:07:25 +0000845 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000846 else
847 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000848 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000849 }
850 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000851 }
852
853 SDOperand Result;
854
855 // If we've change things around then replace token factor.
856 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000857 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000858 // The entry token is the only possible outcome.
859 Result = DAG.getEntryNode();
860 } else {
861 // New and improved token factor.
862 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000863 }
Jim Laskey274062c2006-10-13 23:32:28 +0000864
865 // Don't add users to work list.
866 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000867 }
Jim Laskey279f0532006-09-25 16:29:54 +0000868
Jim Laskey6ff23e52006-10-04 16:53:27 +0000869 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000870}
871
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000872/// MERGE_VALUES can always be eliminated.
873SDOperand DAGCombiner::visitMERGE_VALUES(SDNode *N) {
874 WorkListRemover DeadNodes(*this);
875 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
876 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, i), N->getOperand(i),
877 &DeadNodes);
878 removeFromWorkList(N);
879 DAG.DeleteNode(N);
880 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
881}
882
883
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000884static
885SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
886 MVT::ValueType VT = N0.getValueType();
887 SDOperand N00 = N0.getOperand(0);
888 SDOperand N01 = N0.getOperand(1);
889 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
890 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
891 isa<ConstantSDNode>(N00.getOperand(1))) {
892 N0 = DAG.getNode(ISD::ADD, VT,
893 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
894 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
895 return DAG.getNode(ISD::ADD, VT, N0, N1);
896 }
897 return SDOperand();
898}
899
Evan Chengb13cdbd2007-06-21 07:39:16 +0000900static
901SDOperand combineSelectAndUse(SDNode *N, SDOperand Slct, SDOperand OtherOp,
902 SelectionDAG &DAG) {
903 MVT::ValueType VT = N->getValueType(0);
904 unsigned Opc = N->getOpcode();
905 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
906 SDOperand LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
907 SDOperand RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
908 ISD::CondCode CC = ISD::SETCC_INVALID;
909 if (isSlctCC)
910 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
911 else {
912 SDOperand CCOp = Slct.getOperand(0);
913 if (CCOp.getOpcode() == ISD::SETCC)
914 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
915 }
916
917 bool DoXform = false;
918 bool InvCC = false;
919 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
920 "Bad input!");
921 if (LHS.getOpcode() == ISD::Constant &&
922 cast<ConstantSDNode>(LHS)->isNullValue())
923 DoXform = true;
924 else if (CC != ISD::SETCC_INVALID &&
925 RHS.getOpcode() == ISD::Constant &&
926 cast<ConstantSDNode>(RHS)->isNullValue()) {
927 std::swap(LHS, RHS);
Chris Lattner4626b252008-01-17 07:20:38 +0000928 SDOperand Op0 = Slct.getOperand(0);
929 bool isInt = MVT::isInteger(isSlctCC ? Op0.getValueType()
930 : Op0.getOperand(0).getValueType());
Evan Chengb13cdbd2007-06-21 07:39:16 +0000931 CC = ISD::getSetCCInverse(CC, isInt);
932 DoXform = true;
933 InvCC = true;
934 }
935
936 if (DoXform) {
937 SDOperand Result = DAG.getNode(Opc, VT, OtherOp, RHS);
938 if (isSlctCC)
939 return DAG.getSelectCC(OtherOp, Result,
940 Slct.getOperand(0), Slct.getOperand(1), CC);
941 SDOperand CCOp = Slct.getOperand(0);
942 if (InvCC)
943 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
944 CCOp.getOperand(1), CC);
945 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
946 }
947 return SDOperand();
948}
949
Nate Begeman83e75ec2005-09-06 04:43:02 +0000950SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000951 SDOperand N0 = N->getOperand(0);
952 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000953 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
954 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000955 MVT::ValueType VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000956
957 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +0000958 if (MVT::isVector(VT)) {
959 SDOperand FoldedVOp = SimplifyVBinOp(N);
960 if (FoldedVOp.Val) return FoldedVOp;
961 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000962
Dan Gohman613e0d82007-07-03 14:03:57 +0000963 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000964 if (N0.getOpcode() == ISD::UNDEF)
965 return N0;
966 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000967 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000968 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000969 if (N0C && N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +0000970 return DAG.getConstant(N0C->getAPIntValue() + N1C->getAPIntValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000971 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000972 if (N0C && !N1C)
973 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000974 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000975 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000976 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000977 // fold ((c1-A)+c2) -> (c1+c2)-A
978 if (N1C && N0.getOpcode() == ISD::SUB)
979 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
980 return DAG.getNode(ISD::SUB, VT,
Dan Gohman002e5d02008-03-13 22:13:53 +0000981 DAG.getConstant(N1C->getAPIntValue()+
982 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000983 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000984 // reassociate add
985 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
986 if (RADD.Val != 0)
987 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000988 // fold ((0-A) + B) -> B-A
989 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
990 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000991 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000992 // fold (A + (0-B)) -> A-B
993 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
994 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000995 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000996 // fold (A+(B-A)) -> B
997 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000998 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000999
Evan Cheng860771d2006-03-01 01:09:54 +00001000 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001001 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +00001002
1003 // fold (a+b) -> (a|b) iff a and b share no bits.
1004 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001005 APInt LHSZero, LHSOne;
1006 APInt RHSZero, RHSOne;
1007 APInt Mask = APInt::getAllOnesValue(MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +00001008 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001009 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001010 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +00001011
1012 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1013 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1014 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1015 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1016 return DAG.getNode(ISD::OR, VT, N0, N1);
1017 }
1018 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001019
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001020 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
1021 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
1022 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
1023 if (Result.Val) return Result;
1024 }
1025 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
1026 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
1027 if (Result.Val) return Result;
1028 }
1029
Evan Chengb13cdbd2007-06-21 07:39:16 +00001030 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
1031 if (N0.getOpcode() == ISD::SELECT && N0.Val->hasOneUse()) {
1032 SDOperand Result = combineSelectAndUse(N, N0, N1, DAG);
1033 if (Result.Val) return Result;
1034 }
1035 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1036 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1037 if (Result.Val) return Result;
1038 }
1039
Nate Begeman83e75ec2005-09-06 04:43:02 +00001040 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001041}
1042
Chris Lattner91153682007-03-04 20:03:15 +00001043SDOperand DAGCombiner::visitADDC(SDNode *N) {
1044 SDOperand N0 = N->getOperand(0);
1045 SDOperand N1 = N->getOperand(1);
1046 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1047 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1048 MVT::ValueType VT = N0.getValueType();
1049
1050 // If the flag result is dead, turn this into an ADD.
1051 if (N->hasNUsesOfValue(0, 1))
1052 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +00001053 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +00001054
1055 // canonicalize constant to RHS.
Chris Lattnerbcf24842007-03-04 20:08:45 +00001056 if (N0C && !N1C) {
1057 SDOperand Ops[] = { N1, N0 };
1058 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
1059 }
Chris Lattner91153682007-03-04 20:03:15 +00001060
Chris Lattnerb6541762007-03-04 20:40:38 +00001061 // fold (addc x, 0) -> x + no carry out
1062 if (N1C && N1C->isNullValue())
1063 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1064
1065 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001066 APInt LHSZero, LHSOne;
1067 APInt RHSZero, RHSOne;
1068 APInt Mask = APInt::getAllOnesValue(MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +00001069 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001070 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001071 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001072
1073 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1074 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1075 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1076 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1077 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1078 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1079 }
Chris Lattner91153682007-03-04 20:03:15 +00001080
1081 return SDOperand();
1082}
1083
1084SDOperand DAGCombiner::visitADDE(SDNode *N) {
1085 SDOperand N0 = N->getOperand(0);
1086 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +00001087 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001088 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1089 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Chris Lattnerbcf24842007-03-04 20:08:45 +00001090 //MVT::ValueType VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001091
1092 // canonicalize constant to RHS
Chris Lattnerbcf24842007-03-04 20:08:45 +00001093 if (N0C && !N1C) {
Chris Lattnerb6541762007-03-04 20:40:38 +00001094 SDOperand Ops[] = { N1, N0, CarryIn };
Chris Lattnerbcf24842007-03-04 20:08:45 +00001095 return DAG.getNode(ISD::ADDE, N->getVTList(), Ops, 3);
1096 }
Chris Lattner91153682007-03-04 20:03:15 +00001097
Chris Lattnerb6541762007-03-04 20:40:38 +00001098 // fold (adde x, y, false) -> (addc x, y)
1099 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) {
1100 SDOperand Ops[] = { N1, N0 };
1101 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
1102 }
Chris Lattner91153682007-03-04 20:03:15 +00001103
1104 return SDOperand();
1105}
1106
1107
1108
Nate Begeman83e75ec2005-09-06 04:43:02 +00001109SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001110 SDOperand N0 = N->getOperand(0);
1111 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001112 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1113 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001114 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001115
Dan Gohman7f321562007-06-25 16:23:39 +00001116 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001117 if (MVT::isVector(VT)) {
1118 SDOperand FoldedVOp = SimplifyVBinOp(N);
1119 if (FoldedVOp.Val) return FoldedVOp;
1120 }
Dan Gohman7f321562007-06-25 16:23:39 +00001121
Chris Lattner854077d2005-10-17 01:07:11 +00001122 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001123 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001124 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001125 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001126 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001127 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001128 // fold (sub x, c) -> (add x, -c)
1129 if (N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +00001130 return DAG.getNode(ISD::ADD, VT, N0,
1131 DAG.getConstant(-N1C->getAPIntValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001132 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001133 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001134 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001135 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001136 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001137 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001138 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1139 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1140 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1141 if (Result.Val) return Result;
1142 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001143 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001144 if (N0.getOpcode() == ISD::UNDEF)
1145 return N0;
1146 if (N1.getOpcode() == ISD::UNDEF)
1147 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001148
Nate Begeman83e75ec2005-09-06 04:43:02 +00001149 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001150}
1151
Nate Begeman83e75ec2005-09-06 04:43:02 +00001152SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001153 SDOperand N0 = N->getOperand(0);
1154 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001155 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1156 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +00001157 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001158
Dan Gohman7f321562007-06-25 16:23:39 +00001159 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001160 if (MVT::isVector(VT)) {
1161 SDOperand FoldedVOp = SimplifyVBinOp(N);
1162 if (FoldedVOp.Val) return FoldedVOp;
1163 }
Dan Gohman7f321562007-06-25 16:23:39 +00001164
Dan Gohman613e0d82007-07-03 14:03:57 +00001165 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001166 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001167 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001168 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001169 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001170 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001171 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001172 if (N0C && !N1C)
1173 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001174 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001175 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001176 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001177 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001178 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001179 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001180 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001181 if (N1C && N1C->getAPIntValue().isPowerOf2())
Chris Lattner3e6099b2005-10-30 06:41:49 +00001182 return DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001183 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001184 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001185 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1186 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1187 // FIXME: If the input is something that is easily negated (e.g. a
1188 // single-use add), we should put the negate there.
1189 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1190 DAG.getNode(ISD::SHL, VT, N0,
1191 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1192 TLI.getShiftAmountTy())));
1193 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001194
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001195 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1196 if (N1C && N0.getOpcode() == ISD::SHL &&
1197 isa<ConstantSDNode>(N0.getOperand(1))) {
1198 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001199 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001200 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1201 }
1202
1203 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1204 // use.
1205 {
1206 SDOperand Sh(0,0), Y(0,0);
1207 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1208 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1209 N0.Val->hasOneUse()) {
1210 Sh = N0; Y = N1;
1211 } else if (N1.getOpcode() == ISD::SHL &&
1212 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1213 Sh = N1; Y = N0;
1214 }
1215 if (Sh.Val) {
1216 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1217 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1218 }
1219 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001220 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1221 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1222 isa<ConstantSDNode>(N0.getOperand(1))) {
1223 return DAG.getNode(ISD::ADD, VT,
1224 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1225 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1226 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001227
Nate Begemancd4d58c2006-02-03 06:46:56 +00001228 // reassociate mul
1229 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1230 if (RMUL.Val != 0)
1231 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001232
Nate Begeman83e75ec2005-09-06 04:43:02 +00001233 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001234}
1235
Nate Begeman83e75ec2005-09-06 04:43:02 +00001236SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001237 SDOperand N0 = N->getOperand(0);
1238 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001239 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1240 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001241 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001242
Dan Gohman7f321562007-06-25 16:23:39 +00001243 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001244 if (MVT::isVector(VT)) {
1245 SDOperand FoldedVOp = SimplifyVBinOp(N);
1246 if (FoldedVOp.Val) return FoldedVOp;
1247 }
Dan Gohman7f321562007-06-25 16:23:39 +00001248
Nate Begeman1d4d4142005-09-01 00:19:25 +00001249 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001250 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001251 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001252 // fold (sdiv X, 1) -> X
1253 if (N1C && N1C->getSignExtended() == 1LL)
1254 return N0;
1255 // fold (sdiv X, -1) -> 0-X
1256 if (N1C && N1C->isAllOnesValue())
1257 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001258 // If we know the sign bits of both operands are zero, strength reduce to a
1259 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Chris Lattnerf32aac32008-01-27 23:32:17 +00001260 if (!MVT::isVector(VT)) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001261 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerf32aac32008-01-27 23:32:17 +00001262 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1263 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001264 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001265 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001266 (isPowerOf2_64(N1C->getSignExtended()) ||
1267 isPowerOf2_64(-N1C->getSignExtended()))) {
1268 // If dividing by powers of two is cheap, then don't perform the following
1269 // fold.
1270 if (TLI.isPow2DivCheap())
1271 return SDOperand();
1272 int64_t pow2 = N1C->getSignExtended();
1273 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001274 unsigned lg2 = Log2_64(abs2);
1275 // Splat the sign bit into the register
1276 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001277 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1278 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001279 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001280 // Add (N0 < 0) ? abs2 - 1 : 0;
1281 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1282 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001283 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001284 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001285 AddToWorkList(SRL.Val);
1286 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001287 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1288 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001289 // If we're dividing by a positive value, we're done. Otherwise, we must
1290 // negate the result.
1291 if (pow2 > 0)
1292 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001293 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001294 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1295 }
Nate Begeman69575232005-10-20 02:15:44 +00001296 // if integer divide is expensive and we satisfy the requirements, emit an
1297 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001298 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001299 !TLI.isIntDivCheap()) {
1300 SDOperand Op = BuildSDIV(N);
1301 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001302 }
Dan Gohman7f321562007-06-25 16:23:39 +00001303
Dan Gohman613e0d82007-07-03 14:03:57 +00001304 // undef / X -> 0
1305 if (N0.getOpcode() == ISD::UNDEF)
1306 return DAG.getConstant(0, VT);
1307 // X / undef -> undef
1308 if (N1.getOpcode() == ISD::UNDEF)
1309 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001310
Nate Begeman83e75ec2005-09-06 04:43:02 +00001311 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001312}
1313
Nate Begeman83e75ec2005-09-06 04:43:02 +00001314SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001315 SDOperand N0 = N->getOperand(0);
1316 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001317 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1318 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001319 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001320
Dan Gohman7f321562007-06-25 16:23:39 +00001321 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001322 if (MVT::isVector(VT)) {
1323 SDOperand FoldedVOp = SimplifyVBinOp(N);
1324 if (FoldedVOp.Val) return FoldedVOp;
1325 }
Dan Gohman7f321562007-06-25 16:23:39 +00001326
Nate Begeman1d4d4142005-09-01 00:19:25 +00001327 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001328 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001329 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001330 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001331 if (N1C && N1C->getAPIntValue().isPowerOf2())
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001332 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001333 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001334 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001335 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1336 if (N1.getOpcode() == ISD::SHL) {
1337 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001338 if (SHC->getAPIntValue().isPowerOf2()) {
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001339 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001340 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001341 DAG.getConstant(SHC->getAPIntValue()
1342 .logBase2(),
Nate Begemanc031e332006-02-05 07:36:48 +00001343 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001344 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001345 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001346 }
1347 }
1348 }
Nate Begeman69575232005-10-20 02:15:44 +00001349 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001350 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Chris Lattnere9936d12005-10-22 18:50:15 +00001351 SDOperand Op = BuildUDIV(N);
1352 if (Op.Val) return Op;
1353 }
Dan Gohman7f321562007-06-25 16:23:39 +00001354
Dan Gohman613e0d82007-07-03 14:03:57 +00001355 // undef / X -> 0
1356 if (N0.getOpcode() == ISD::UNDEF)
1357 return DAG.getConstant(0, VT);
1358 // X / undef -> undef
1359 if (N1.getOpcode() == ISD::UNDEF)
1360 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001361
Nate Begeman83e75ec2005-09-06 04:43:02 +00001362 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001363}
1364
Nate Begeman83e75ec2005-09-06 04:43:02 +00001365SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001366 SDOperand N0 = N->getOperand(0);
1367 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001368 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1369 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001370 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001371
1372 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001373 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001374 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001375 // If we know the sign bits of both operands are zero, strength reduce to a
1376 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Chris Lattneree339f42008-01-27 23:21:58 +00001377 if (!MVT::isVector(VT)) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001378 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001379 return DAG.getNode(ISD::UREM, VT, N0, N1);
1380 }
Chris Lattner26d29902006-10-12 20:58:32 +00001381
Dan Gohman77003042007-11-26 23:46:11 +00001382 // If X/C can be simplified by the division-by-constant logic, lower
1383 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001384 if (N1C && !N1C->isNullValue()) {
1385 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Chris Lattner5eee4272008-01-26 01:09:19 +00001386 AddToWorkList(Div.Val);
Dan Gohman77003042007-11-26 23:46:11 +00001387 SDOperand OptimizedDiv = combine(Div.Val);
1388 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1389 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1390 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1391 AddToWorkList(Mul.Val);
1392 return Sub;
1393 }
Chris Lattner26d29902006-10-12 20:58:32 +00001394 }
1395
Dan Gohman613e0d82007-07-03 14:03:57 +00001396 // undef % X -> 0
1397 if (N0.getOpcode() == ISD::UNDEF)
1398 return DAG.getConstant(0, VT);
1399 // X % undef -> undef
1400 if (N1.getOpcode() == ISD::UNDEF)
1401 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001402
Nate Begeman83e75ec2005-09-06 04:43:02 +00001403 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001404}
1405
Nate Begeman83e75ec2005-09-06 04:43:02 +00001406SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001407 SDOperand N0 = N->getOperand(0);
1408 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001409 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1410 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001411 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001412
1413 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001414 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001415 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001416 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001417 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
1418 return DAG.getNode(ISD::AND, VT, N0,
1419 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001420 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1421 if (N1.getOpcode() == ISD::SHL) {
1422 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001423 if (SHC->getAPIntValue().isPowerOf2()) {
1424 SDOperand Add =
1425 DAG.getNode(ISD::ADD, VT, N1,
1426 DAG.getConstant(APInt::getAllOnesValue(MVT::getSizeInBits(VT)),
1427 VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001428 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001429 return DAG.getNode(ISD::AND, VT, N0, Add);
1430 }
1431 }
1432 }
Chris Lattner26d29902006-10-12 20:58:32 +00001433
Dan Gohman77003042007-11-26 23:46:11 +00001434 // If X/C can be simplified by the division-by-constant logic, lower
1435 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001436 if (N1C && !N1C->isNullValue()) {
1437 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman77003042007-11-26 23:46:11 +00001438 SDOperand OptimizedDiv = combine(Div.Val);
1439 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1440 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1441 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1442 AddToWorkList(Mul.Val);
1443 return Sub;
1444 }
Chris Lattner26d29902006-10-12 20:58:32 +00001445 }
1446
Dan Gohman613e0d82007-07-03 14:03:57 +00001447 // undef % X -> 0
1448 if (N0.getOpcode() == ISD::UNDEF)
1449 return DAG.getConstant(0, VT);
1450 // X % undef -> undef
1451 if (N1.getOpcode() == ISD::UNDEF)
1452 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001453
Nate Begeman83e75ec2005-09-06 04:43:02 +00001454 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001455}
1456
Nate Begeman83e75ec2005-09-06 04:43:02 +00001457SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001458 SDOperand N0 = N->getOperand(0);
1459 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001460 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001461 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001462
1463 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001464 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001465 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001466 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001467 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001468 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1469 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001470 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001471 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001472 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001473 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001474
Nate Begeman83e75ec2005-09-06 04:43:02 +00001475 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001476}
1477
Nate Begeman83e75ec2005-09-06 04:43:02 +00001478SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001479 SDOperand N0 = N->getOperand(0);
1480 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001481 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001482 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001483
1484 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001485 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001486 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001487 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001488 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001489 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001490 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001491 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001492 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001493
Nate Begeman83e75ec2005-09-06 04:43:02 +00001494 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001495}
1496
Dan Gohman389079b2007-10-08 17:57:15 +00001497/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1498/// compute two values. LoOp and HiOp give the opcodes for the two computations
1499/// that are being performed. Return true if a simplification was made.
1500///
Chris Lattner5eee4272008-01-26 01:09:19 +00001501SDOperand DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1502 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001503 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001504 bool HiExists = N->hasAnyUseOfValue(1);
1505 if (!HiExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001506 (!AfterLegalize ||
1507 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001508 SDOperand Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
1509 N->getNumOperands());
1510 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001511 }
1512
1513 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001514 bool LoExists = N->hasAnyUseOfValue(0);
1515 if (!LoExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001516 (!AfterLegalize ||
1517 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001518 SDOperand Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
1519 N->getNumOperands());
1520 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001521 }
1522
Evan Cheng44711942007-11-08 09:25:29 +00001523 // If both halves are used, return as it is.
1524 if (LoExists && HiExists)
Chris Lattner5eee4272008-01-26 01:09:19 +00001525 return SDOperand();
Evan Cheng44711942007-11-08 09:25:29 +00001526
1527 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001528 if (LoExists) {
1529 SDOperand Lo = DAG.getNode(LoOp, N->getValueType(0),
1530 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001531 AddToWorkList(Lo.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001532 SDOperand LoOpt = combine(Lo.Val);
Chris Lattner5eee4272008-01-26 01:09:19 +00001533 if (LoOpt.Val && LoOpt.Val != Lo.Val &&
1534 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType()))
1535 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001536 }
1537
Evan Cheng44711942007-11-08 09:25:29 +00001538 if (HiExists) {
1539 SDOperand Hi = DAG.getNode(HiOp, N->getValueType(1),
1540 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001541 AddToWorkList(Hi.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001542 SDOperand HiOpt = combine(Hi.Val);
1543 if (HiOpt.Val && HiOpt != Hi &&
Chris Lattner5eee4272008-01-26 01:09:19 +00001544 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType()))
1545 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001546 }
Chris Lattner5eee4272008-01-26 01:09:19 +00001547 return SDOperand();
Dan Gohman389079b2007-10-08 17:57:15 +00001548}
1549
1550SDOperand DAGCombiner::visitSMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001551 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
1552 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001553
1554 return SDOperand();
1555}
1556
1557SDOperand DAGCombiner::visitUMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001558 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
1559 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001560
1561 return SDOperand();
1562}
1563
1564SDOperand DAGCombiner::visitSDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001565 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
1566 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001567
1568 return SDOperand();
1569}
1570
1571SDOperand DAGCombiner::visitUDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001572 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
1573 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001574
1575 return SDOperand();
1576}
1577
Chris Lattner35e5c142006-05-05 05:51:50 +00001578/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1579/// two operands of the same opcode, try to simplify it.
1580SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1581 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1582 MVT::ValueType VT = N0.getValueType();
1583 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1584
Chris Lattner540121f2006-05-05 06:31:05 +00001585 // For each of OP in AND/OR/XOR:
1586 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1587 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1588 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001589 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001590 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001591 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001592 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1593 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1594 N0.getOperand(0).getValueType(),
1595 N0.getOperand(0), N1.getOperand(0));
1596 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001597 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001598 }
1599
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001600 // For each of OP in SHL/SRL/SRA/AND...
1601 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1602 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1603 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001604 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001605 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001606 N0.getOperand(1) == N1.getOperand(1)) {
1607 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1608 N0.getOperand(0).getValueType(),
1609 N0.getOperand(0), N1.getOperand(0));
1610 AddToWorkList(ORNode.Val);
1611 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1612 }
1613
1614 return SDOperand();
1615}
1616
Nate Begeman83e75ec2005-09-06 04:43:02 +00001617SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001618 SDOperand N0 = N->getOperand(0);
1619 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001620 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001621 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1622 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001623 MVT::ValueType VT = N1.getValueType();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001624 unsigned BitWidth = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001625
Dan Gohman7f321562007-06-25 16:23:39 +00001626 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001627 if (MVT::isVector(VT)) {
1628 SDOperand FoldedVOp = SimplifyVBinOp(N);
1629 if (FoldedVOp.Val) return FoldedVOp;
1630 }
Dan Gohman7f321562007-06-25 16:23:39 +00001631
Dan Gohman613e0d82007-07-03 14:03:57 +00001632 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001633 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001634 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001635 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001636 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001637 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001638 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001639 if (N0C && !N1C)
1640 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001641 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001642 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001643 return N0;
1644 // if (and x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001645 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
1646 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001647 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001648 // reassociate and
1649 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1650 if (RAND.Val != 0)
1651 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001652 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001653 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001654 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001655 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001656 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001657 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1658 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001659 SDOperand N0Op0 = N0.getOperand(0);
1660 APInt Mask = ~N1C->getAPIntValue();
1661 Mask.trunc(N0Op0.getValueSizeInBits());
1662 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001663 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001664 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001665
1666 // Replace uses of the AND with uses of the Zero extend node.
1667 CombineTo(N, Zext);
1668
Chris Lattner3603cd62006-02-02 07:17:31 +00001669 // We actually want to replace all uses of the any_extend with the
1670 // zero_extend, to avoid duplicating things. This will later cause this
1671 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001672 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001673 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001674 }
1675 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001676 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1677 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1678 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1679 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1680
1681 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1682 MVT::isInteger(LL.getValueType())) {
1683 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001684 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001685 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001686 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001687 return DAG.getSetCC(VT, ORNode, LR, Op1);
1688 }
1689 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1690 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1691 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001692 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001693 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1694 }
1695 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1696 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1697 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001698 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001699 return DAG.getSetCC(VT, ORNode, LR, Op1);
1700 }
1701 }
1702 // canonicalize equivalent to ll == rl
1703 if (LL == RR && LR == RL) {
1704 Op1 = ISD::getSetCCSwappedOperands(Op1);
1705 std::swap(RL, RR);
1706 }
1707 if (LL == RL && LR == RR) {
1708 bool isInteger = MVT::isInteger(LL.getValueType());
1709 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1710 if (Result != ISD::SETCC_INVALID)
1711 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1712 }
1713 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001714
1715 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1716 if (N0.getOpcode() == N1.getOpcode()) {
1717 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1718 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001719 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001720
Nate Begemande996292006-02-03 22:24:05 +00001721 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1722 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001723 if (!MVT::isVector(VT) &&
1724 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001725 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001726 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001727 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001728 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001729 MVT::ValueType EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001730 // If we zero all the possible extended bits, then we can turn this into
1731 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001732 unsigned BitWidth = N1.getValueSizeInBits();
1733 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
1734 BitWidth - MVT::getSizeInBits(EVT))) &&
Evan Chengc5484282006-10-04 00:56:09 +00001735 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001736 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1737 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001738 LN0->getSrcValueOffset(), EVT,
1739 LN0->isVolatile(),
1740 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001741 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001742 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001743 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001744 }
1745 }
1746 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001747 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1748 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001749 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001750 MVT::ValueType EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001751 // If we zero all the possible extended bits, then we can turn this into
1752 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001753 unsigned BitWidth = N1.getValueSizeInBits();
1754 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
1755 BitWidth - MVT::getSizeInBits(EVT))) &&
Evan Chengc5484282006-10-04 00:56:09 +00001756 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001757 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1758 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001759 LN0->getSrcValueOffset(), EVT,
1760 LN0->isVolatile(),
1761 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001762 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001763 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001764 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001765 }
1766 }
Chris Lattner15045b62006-02-28 06:35:35 +00001767
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001768 // fold (and (load x), 255) -> (zextload x, i8)
1769 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001770 if (N1C && N0.getOpcode() == ISD::LOAD) {
1771 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1772 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Chris Lattnerddf89562008-01-17 19:59:44 +00001773 LN0->isUnindexed() && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001774 MVT::ValueType EVT, LoadedVT;
Dan Gohman002e5d02008-03-13 22:13:53 +00001775 if (N1C->getAPIntValue() == 255)
Evan Cheng466685d2006-10-09 20:57:25 +00001776 EVT = MVT::i8;
Dan Gohman002e5d02008-03-13 22:13:53 +00001777 else if (N1C->getAPIntValue() == 65535)
Evan Cheng466685d2006-10-09 20:57:25 +00001778 EVT = MVT::i16;
Dan Gohman002e5d02008-03-13 22:13:53 +00001779 else if (N1C->getAPIntValue() == ~0U)
Evan Cheng466685d2006-10-09 20:57:25 +00001780 EVT = MVT::i32;
1781 else
1782 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001783
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001784 LoadedVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001785 if (EVT != MVT::Other && LoadedVT > EVT &&
1786 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1787 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1788 // For big endian targets, we need to add an offset to the pointer to
1789 // load the correct bytes. For little endian systems, we merely need to
1790 // read fewer bytes from the same pointer.
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001791 unsigned LVTStoreBytes = MVT::getStoreSizeInBits(LoadedVT)/8;
1792 unsigned EVTStoreBytes = MVT::getStoreSizeInBits(EVT)/8;
1793 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001794 unsigned Alignment = LN0->getAlignment();
Evan Cheng466685d2006-10-09 20:57:25 +00001795 SDOperand NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001796 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001797 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1798 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001799 Alignment = MinAlign(Alignment, PtrOff);
1800 }
Evan Cheng466685d2006-10-09 20:57:25 +00001801 AddToWorkList(NewPtr.Val);
1802 SDOperand Load =
1803 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001804 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001805 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001806 AddToWorkList(N);
1807 CombineTo(N0.Val, Load, Load.getValue(1));
1808 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1809 }
Chris Lattner15045b62006-02-28 06:35:35 +00001810 }
1811 }
1812
Nate Begeman83e75ec2005-09-06 04:43:02 +00001813 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001814}
1815
Nate Begeman83e75ec2005-09-06 04:43:02 +00001816SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001817 SDOperand N0 = N->getOperand(0);
1818 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001819 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001820 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1821 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001822 MVT::ValueType VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001823
Dan Gohman7f321562007-06-25 16:23:39 +00001824 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001825 if (MVT::isVector(VT)) {
1826 SDOperand FoldedVOp = SimplifyVBinOp(N);
1827 if (FoldedVOp.Val) return FoldedVOp;
1828 }
Dan Gohman7f321562007-06-25 16:23:39 +00001829
Dan Gohman613e0d82007-07-03 14:03:57 +00001830 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001831 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001832 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001833 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001834 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001835 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001836 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001837 if (N0C && !N1C)
1838 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001839 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001840 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001841 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001842 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001843 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001844 return N1;
1845 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001846 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001847 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001848 // reassociate or
1849 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1850 if (ROR.Val != 0)
1851 return ROR;
1852 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1853 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001854 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001855 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1856 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1857 N1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001858 DAG.getConstant(N1C->getAPIntValue() |
1859 C1->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001860 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001861 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1862 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1863 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1864 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1865
1866 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1867 MVT::isInteger(LL.getValueType())) {
1868 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1869 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001870 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001871 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1872 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001873 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001874 return DAG.getSetCC(VT, ORNode, LR, Op1);
1875 }
1876 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1877 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1878 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1879 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1880 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001881 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001882 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1883 }
1884 }
1885 // canonicalize equivalent to ll == rl
1886 if (LL == RR && LR == RL) {
1887 Op1 = ISD::getSetCCSwappedOperands(Op1);
1888 std::swap(RL, RR);
1889 }
1890 if (LL == RL && LR == RR) {
1891 bool isInteger = MVT::isInteger(LL.getValueType());
1892 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1893 if (Result != ISD::SETCC_INVALID)
1894 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1895 }
1896 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001897
1898 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1899 if (N0.getOpcode() == N1.getOpcode()) {
1900 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1901 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001902 }
Chris Lattner516b9622006-09-14 20:50:57 +00001903
Chris Lattner1ec72732006-09-14 21:11:37 +00001904 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1905 if (N0.getOpcode() == ISD::AND &&
1906 N1.getOpcode() == ISD::AND &&
1907 N0.getOperand(1).getOpcode() == ISD::Constant &&
1908 N1.getOperand(1).getOpcode() == ISD::Constant &&
1909 // Don't increase # computations.
1910 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1911 // We can only do this xform if we know that bits from X that are set in C2
1912 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001913 const APInt &LHSMask =
1914 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1915 const APInt &RHSMask =
1916 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00001917
Dan Gohmanea859be2007-06-22 14:59:07 +00001918 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1919 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001920 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1921 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1922 }
1923 }
1924
1925
Chris Lattner516b9622006-09-14 20:50:57 +00001926 // See if this is some rotate idiom.
1927 if (SDNode *Rot = MatchRotate(N0, N1))
1928 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001929
Nate Begeman83e75ec2005-09-06 04:43:02 +00001930 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001931}
1932
Chris Lattner516b9622006-09-14 20:50:57 +00001933
1934/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1935static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1936 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001937 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001938 Mask = Op.getOperand(1);
1939 Op = Op.getOperand(0);
1940 } else {
1941 return false;
1942 }
1943 }
1944
1945 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1946 Shift = Op;
1947 return true;
1948 }
1949 return false;
1950}
1951
1952
1953// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1954// idioms for rotate, and if the target supports rotation instructions, generate
1955// a rot[lr].
1956SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1957 // Must be a legal type. Expanded an promoted things won't work with rotates.
1958 MVT::ValueType VT = LHS.getValueType();
1959 if (!TLI.isTypeLegal(VT)) return 0;
1960
1961 // The target must have at least one rotate flavor.
1962 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1963 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1964 if (!HasROTL && !HasROTR) return 0;
1965
1966 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1967 SDOperand LHSShift; // The shift.
1968 SDOperand LHSMask; // AND value if any.
1969 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1970 return 0; // Not part of a rotate.
1971
1972 SDOperand RHSShift; // The shift.
1973 SDOperand RHSMask; // AND value if any.
1974 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1975 return 0; // Not part of a rotate.
1976
1977 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1978 return 0; // Not shifting the same value.
1979
1980 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1981 return 0; // Shifts must disagree.
1982
1983 // Canonicalize shl to left side in a shl/srl pair.
1984 if (RHSShift.getOpcode() == ISD::SHL) {
1985 std::swap(LHS, RHS);
1986 std::swap(LHSShift, RHSShift);
1987 std::swap(LHSMask , RHSMask );
1988 }
1989
1990 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Scott Michelc9dc1142007-04-02 21:36:32 +00001991 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1992 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1993 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001994
1995 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1996 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001997 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1998 RHSShiftAmt.getOpcode() == ISD::Constant) {
1999 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
2000 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00002001 if ((LShVal + RShVal) != OpSizeInBits)
2002 return 0;
2003
2004 SDOperand Rot;
2005 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002006 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002007 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002008 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002009
2010 // If there is an AND of either shifted operand, apply it to the result.
2011 if (LHSMask.Val || RHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002012 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Chris Lattner516b9622006-09-14 20:50:57 +00002013
2014 if (LHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002015 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2016 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002017 }
2018 if (RHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002019 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2020 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002021 }
2022
2023 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2024 }
2025
2026 return Rot.Val;
2027 }
2028
2029 // If there is a mask here, and we have a variable shift, we can't be sure
2030 // that we're masking out the right stuff.
2031 if (LHSMask.Val || RHSMask.Val)
2032 return 0;
2033
2034 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2035 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002036 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2037 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002038 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002039 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002040 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002041 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002042 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002043 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002044 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002045 }
Chris Lattner516b9622006-09-14 20:50:57 +00002046 }
2047 }
2048
2049 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2050 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002051 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2052 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002053 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002054 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002055 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002056 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002057 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002058 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002059 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002060 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002061 }
2062 }
2063
2064 // Look for sign/zext/any-extended cases:
2065 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2066 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2067 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
2068 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2069 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2070 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
2071 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
2072 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
2073 if (RExtOp0.getOpcode() == ISD::SUB &&
2074 RExtOp0.getOperand(1) == LExtOp0) {
2075 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2076 // (rotr x, y)
2077 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2078 // (rotl x, (sub 32, y))
2079 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002080 if (SUBC->getAPIntValue() == OpSizeInBits) {
Scott Michelc9dc1142007-04-02 21:36:32 +00002081 if (HasROTL)
2082 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2083 else
2084 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
2085 }
2086 }
2087 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2088 RExtOp0 == LExtOp0.getOperand(1)) {
2089 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2090 // (rotl x, y)
2091 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2092 // (rotr x, (sub 32, y))
2093 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002094 if (SUBC->getAPIntValue() == OpSizeInBits) {
Scott Michelc9dc1142007-04-02 21:36:32 +00002095 if (HasROTL)
2096 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
2097 else
2098 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2099 }
2100 }
Chris Lattner516b9622006-09-14 20:50:57 +00002101 }
2102 }
2103
2104 return 0;
2105}
2106
2107
Nate Begeman83e75ec2005-09-06 04:43:02 +00002108SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002109 SDOperand N0 = N->getOperand(0);
2110 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002111 SDOperand LHS, RHS, CC;
2112 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2113 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002114 MVT::ValueType VT = N0.getValueType();
2115
Dan Gohman7f321562007-06-25 16:23:39 +00002116 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00002117 if (MVT::isVector(VT)) {
2118 SDOperand FoldedVOp = SimplifyVBinOp(N);
2119 if (FoldedVOp.Val) return FoldedVOp;
2120 }
Dan Gohman7f321562007-06-25 16:23:39 +00002121
Evan Cheng26471c42008-03-25 20:08:07 +00002122 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2123 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2124 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002125 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002126 if (N0.getOpcode() == ISD::UNDEF)
2127 return N0;
2128 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002129 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002130 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002131 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002132 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00002133 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002134 if (N0C && !N1C)
2135 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002136 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002137 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002138 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002139 // reassociate xor
2140 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
2141 if (RXOR.Val != 0)
2142 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002143 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002144 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Nate Begeman646d7e22005-09-02 21:18:40 +00002145 bool isInt = MVT::isInteger(LHS.getValueType());
2146 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2147 isInt);
2148 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002149 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002150 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002151 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002152 assert(0 && "Unhandled SetCC Equivalent!");
2153 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002154 }
Chris Lattner61c5ff42007-09-10 21:39:07 +00002155 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002156 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Chris Lattner61c5ff42007-09-10 21:39:07 +00002157 N0.Val->hasOneUse() && isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
2158 SDOperand V = N0.getOperand(0);
2159 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002160 DAG.getConstant(1, V.getValueType()));
Chris Lattner61c5ff42007-09-10 21:39:07 +00002161 AddToWorkList(V.Val);
2162 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2163 }
2164
Nate Begeman99801192005-09-07 23:25:52 +00002165 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Dan Gohman002e5d02008-03-13 22:13:53 +00002166 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002167 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002168 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002169 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2170 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002171 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2172 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002173 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002174 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002175 }
2176 }
Nate Begeman99801192005-09-07 23:25:52 +00002177 // fold !(x or y) -> (!x and !y) iff x or y are constants
2178 if (N1C && N1C->isAllOnesValue() &&
2179 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002180 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002181 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2182 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002183 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2184 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002185 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002186 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002187 }
2188 }
Nate Begeman223df222005-09-08 20:18:10 +00002189 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2190 if (N1C && N0.getOpcode() == ISD::XOR) {
2191 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2192 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2193 if (N00C)
2194 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00002195 DAG.getConstant(N1C->getAPIntValue()^
2196 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002197 if (N01C)
2198 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
Dan Gohman002e5d02008-03-13 22:13:53 +00002199 DAG.getConstant(N1C->getAPIntValue()^
2200 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002201 }
2202 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002203 if (N0 == N1) {
2204 if (!MVT::isVector(VT)) {
2205 return DAG.getConstant(0, VT);
2206 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2207 // Produce a vector of zeros.
Dan Gohman51eaa862007-06-14 22:58:02 +00002208 SDOperand El = DAG.getConstant(0, MVT::getVectorElementType(VT));
Chris Lattner4fbdd592006-03-28 19:11:05 +00002209 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002210 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002211 }
2212 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002213
2214 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2215 if (N0.getOpcode() == N1.getOpcode()) {
2216 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
2217 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002218 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002219
Chris Lattner3e104b12006-04-08 04:15:24 +00002220 // Simplify the expression using non-local knowledge.
2221 if (!MVT::isVector(VT) &&
2222 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002223 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002224
Nate Begeman83e75ec2005-09-06 04:43:02 +00002225 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002226}
2227
Chris Lattnere70da202007-12-06 07:33:36 +00002228/// visitShiftByConstant - Handle transforms common to the three shifts, when
2229/// the shift amount is a constant.
2230SDOperand DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
2231 SDNode *LHS = N->getOperand(0).Val;
2232 if (!LHS->hasOneUse()) return SDOperand();
2233
2234 // We want to pull some binops through shifts, so that we have (and (shift))
2235 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2236 // thing happens with address calculations, so it's important to canonicalize
2237 // it.
2238 bool HighBitSet = false; // Can we transform this if the high bit is set?
2239
2240 switch (LHS->getOpcode()) {
2241 default: return SDOperand();
2242 case ISD::OR:
2243 case ISD::XOR:
2244 HighBitSet = false; // We can only transform sra if the high bit is clear.
2245 break;
2246 case ISD::AND:
2247 HighBitSet = true; // We can only transform sra if the high bit is set.
2248 break;
2249 case ISD::ADD:
2250 if (N->getOpcode() != ISD::SHL)
2251 return SDOperand(); // only shl(add) not sr[al](add).
2252 HighBitSet = false; // We can only transform sra if the high bit is clear.
2253 break;
2254 }
2255
2256 // We require the RHS of the binop to be a constant as well.
2257 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
2258 if (!BinOpCst) return SDOperand();
2259
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002260
2261 // FIXME: disable this for unless the input to the binop is a shift by a
2262 // constant. If it is not a shift, it pessimizes some common cases like:
2263 //
2264 //void foo(int *X, int i) { X[i & 1235] = 1; }
2265 //int bar(int *X, int i) { return X[i & 255]; }
2266 SDNode *BinOpLHSVal = LHS->getOperand(0).Val;
2267 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2268 BinOpLHSVal->getOpcode() != ISD::SRA &&
2269 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2270 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
2271 return SDOperand();
2272
Chris Lattnere70da202007-12-06 07:33:36 +00002273 MVT::ValueType VT = N->getValueType(0);
2274
2275 // If this is a signed shift right, and the high bit is modified
2276 // by the logical operation, do not perform the transformation.
2277 // The highBitSet boolean indicates the value of the high bit of
2278 // the constant which would cause it to be modified for this
2279 // operation.
2280 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002281 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2282 if (BinOpRHSSignSet != HighBitSet)
Chris Lattnere70da202007-12-06 07:33:36 +00002283 return SDOperand();
2284 }
2285
2286 // Fold the constants, shifting the binop RHS by the shift amount.
2287 SDOperand NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
2288 LHS->getOperand(1), N->getOperand(1));
2289
2290 // Create the new shift.
2291 SDOperand NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
2292 N->getOperand(1));
2293
2294 // Create the new binop.
2295 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2296}
2297
2298
Nate Begeman83e75ec2005-09-06 04:43:02 +00002299SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002300 SDOperand N0 = N->getOperand(0);
2301 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002302 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2303 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002304 MVT::ValueType VT = N0.getValueType();
2305 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2306
2307 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002308 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002309 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002310 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002311 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002312 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002313 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002314 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002315 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002316 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002317 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002318 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002319 // if (shl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002320 if (DAG.MaskedValueIsZero(SDOperand(N, 0),
2321 APInt::getAllOnesValue(MVT::getSizeInBits(VT))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002322 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00002323 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002324 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002325 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002326 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002327 N0.getOperand(1).getOpcode() == ISD::Constant) {
2328 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002329 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002330 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002331 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002332 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002333 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002334 }
2335 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2336 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002337 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002338 N0.getOperand(1).getOpcode() == ISD::Constant) {
2339 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002340 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002341 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
2342 DAG.getConstant(~0ULL << c1, VT));
2343 if (c2 > c1)
2344 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002345 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002346 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002347 return DAG.getNode(ISD::SRL, VT, Mask,
2348 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002349 }
2350 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002351 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002352 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002353 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002354
2355 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002356}
2357
Nate Begeman83e75ec2005-09-06 04:43:02 +00002358SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002359 SDOperand N0 = N->getOperand(0);
2360 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002361 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2362 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002363 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002364
2365 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002366 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002367 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002368 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002369 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002370 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002371 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002372 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002373 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002374 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00002375 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002376 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002377 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002378 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002379 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002380 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2381 // sext_inreg.
2382 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
2383 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
2384 MVT::ValueType EVT;
2385 switch (LowBits) {
2386 default: EVT = MVT::Other; break;
2387 case 1: EVT = MVT::i1; break;
2388 case 8: EVT = MVT::i8; break;
2389 case 16: EVT = MVT::i16; break;
2390 case 32: EVT = MVT::i32; break;
2391 }
2392 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
2393 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2394 DAG.getValueType(EVT));
2395 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002396
2397 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2398 if (N1C && N0.getOpcode() == ISD::SRA) {
2399 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2400 unsigned Sum = N1C->getValue() + C1->getValue();
2401 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
2402 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2403 DAG.getConstant(Sum, N1C->getValueType(0)));
2404 }
2405 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002406
2407 // fold sra (shl X, m), result_size - n
2408 // -> (sign_extend (trunc (shl X, result_size - n - m))) for
Christopher Lambb9b04282008-03-20 04:31:39 +00002409 // result_size - n != m.
2410 // If truncate is free for the target sext(shl) is likely to result in better
2411 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002412 if (N0.getOpcode() == ISD::SHL) {
2413 // Get the two constanst of the shifts, CN0 = m, CN = n.
2414 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2415 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002416 // Determine what the truncate's result bitsize and type would be.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002417 unsigned VTValSize = MVT::getSizeInBits(VT);
2418 MVT::ValueType TruncVT = MVT::getIntegerType(VTValSize - N1C->getValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002419 // Determine the residual right-shift amount.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002420 unsigned ShiftAmt = N1C->getValue() - N01C->getValue();
Christopher Lambb9b04282008-03-20 04:31:39 +00002421
2422 // If the shift is not a no-op (in which case this should be just a sign
2423 // extend already), the truncated to type is legal, sign_extend is legal
2424 // on that type, and the the truncate to that type is both legal and free,
2425 // perform the transform.
2426 if (ShiftAmt &&
2427 TLI.isTypeLegal(TruncVT) &&
2428 TLI.isOperationLegal(ISD::SIGN_EXTEND, TruncVT) &&
2429 TLI.isOperationLegal(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002430 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002431
2432 SDOperand Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
2433 SDOperand Shift = DAG.getNode(ISD::SRL, VT, N0.getOperand(0), Amt);
2434 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, TruncVT, Shift);
2435 return DAG.getNode(ISD::SIGN_EXTEND, N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002436 }
2437 }
2438 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002439
Chris Lattnera8504462006-05-08 20:51:54 +00002440 // Simplify, based on bits shifted out of the LHS.
2441 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2442 return SDOperand(N, 0);
2443
2444
Nate Begeman1d4d4142005-09-01 00:19:25 +00002445 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002446 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002447 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002448
2449 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002450}
2451
Nate Begeman83e75ec2005-09-06 04:43:02 +00002452SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002453 SDOperand N0 = N->getOperand(0);
2454 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002455 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2456 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002457 MVT::ValueType VT = N0.getValueType();
2458 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2459
2460 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002461 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002462 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002463 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002464 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002465 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002466 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002467 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002468 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002469 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002470 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002471 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002472 // if (srl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002473 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
2474 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002475 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002476
Nate Begeman1d4d4142005-09-01 00:19:25 +00002477 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002478 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002479 N0.getOperand(1).getOpcode() == ISD::Constant) {
2480 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002481 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002482 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002483 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002484 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002485 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002486 }
Chris Lattner350bec02006-04-02 06:11:11 +00002487
Chris Lattner06afe072006-05-05 22:53:17 +00002488 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2489 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2490 // Shifting in all undef bits?
2491 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
2492 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
2493 return DAG.getNode(ISD::UNDEF, VT);
2494
2495 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
2496 AddToWorkList(SmallShift.Val);
2497 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2498 }
2499
Chris Lattner3657ffe2006-10-12 20:23:19 +00002500 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2501 // bit, which is unmodified by sra.
2502 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
2503 if (N0.getOpcode() == ISD::SRA)
2504 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2505 }
2506
Chris Lattner350bec02006-04-02 06:11:11 +00002507 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2508 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002509 N1C->getAPIntValue() == Log2_32(MVT::getSizeInBits(VT))) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002510 APInt KnownZero, KnownOne;
2511 APInt Mask = APInt::getAllOnesValue(MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +00002512 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002513
2514 // If any of the input bits are KnownOne, then the input couldn't be all
2515 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002516 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002517
2518 // If all of the bits input the to ctlz node are known to be zero, then
2519 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002520 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002521 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2522
2523 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2524 if ((UnknownBits & (UnknownBits-1)) == 0) {
2525 // Okay, we know that only that the single bit specified by UnknownBits
2526 // could be set on input to the CTLZ node. If this bit is set, the SRL
2527 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2528 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002529 unsigned ShAmt = UnknownBits.countTrailingZeros();
Chris Lattner350bec02006-04-02 06:11:11 +00002530 SDOperand Op = N0.getOperand(0);
2531 if (ShAmt) {
2532 Op = DAG.getNode(ISD::SRL, VT, Op,
2533 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2534 AddToWorkList(Op.Val);
2535 }
2536 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2537 }
2538 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002539
2540 // fold operands of srl based on knowledge that the low bits are not
2541 // demanded.
2542 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2543 return SDOperand(N, 0);
2544
Chris Lattnere70da202007-12-06 07:33:36 +00002545 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002546}
2547
Nate Begeman83e75ec2005-09-06 04:43:02 +00002548SDOperand DAGCombiner::visitCTLZ(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 (ctlz 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::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002555 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002556}
2557
Nate Begeman83e75ec2005-09-06 04:43:02 +00002558SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002559 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002560 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002561
2562 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002563 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002564 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002565 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002566}
2567
Nate Begeman83e75ec2005-09-06 04:43:02 +00002568SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002569 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002570 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002571
2572 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002573 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002574 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002575 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002576}
2577
Nate Begeman452d7be2005-09-16 00:54:12 +00002578SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2579 SDOperand N0 = N->getOperand(0);
2580 SDOperand N1 = N->getOperand(1);
2581 SDOperand N2 = N->getOperand(2);
2582 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2583 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2584 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2585 MVT::ValueType VT = N->getValueType(0);
Evan Cheng571c4782007-08-18 05:57:05 +00002586 MVT::ValueType VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002587
Nate Begeman452d7be2005-09-16 00:54:12 +00002588 // fold select C, X, X -> X
2589 if (N1 == N2)
2590 return N1;
2591 // fold select true, X, Y -> X
2592 if (N0C && !N0C->isNullValue())
2593 return N1;
2594 // fold select false, X, Y -> Y
2595 if (N0C && N0C->isNullValue())
2596 return N2;
2597 // fold select C, 1, X -> C | X
Dan Gohman002e5d02008-03-13 22:13:53 +00002598 if (MVT::i1 == VT && N1C && N1C->getAPIntValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002599 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002600 // fold select C, 0, 1 -> ~C
2601 if (MVT::isInteger(VT) && MVT::isInteger(VT0) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002602 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Evan Cheng571c4782007-08-18 05:57:05 +00002603 SDOperand XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
2604 if (VT == VT0)
2605 return XORNode;
2606 AddToWorkList(XORNode.Val);
2607 if (MVT::getSizeInBits(VT) > MVT::getSizeInBits(VT0))
2608 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2609 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2610 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002611 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002612 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
2613 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002614 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002615 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2616 }
2617 // fold select C, X, 1 -> ~C | X
Dan Gohman002e5d02008-03-13 22:13:53 +00002618 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002619 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002620 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002621 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2622 }
2623 // fold select C, X, 0 -> C & X
2624 // FIXME: this should check for C type == X type, not i1?
2625 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2626 return DAG.getNode(ISD::AND, VT, N0, N1);
2627 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2628 if (MVT::i1 == VT && N0 == N1)
2629 return DAG.getNode(ISD::OR, VT, N0, N2);
2630 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2631 if (MVT::i1 == VT && N0 == N2)
2632 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002633
Chris Lattner40c62d52005-10-18 06:04:22 +00002634 // If we can fold this based on the true/false value, do so.
2635 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002636 return SDOperand(N, 0); // Don't revisit N.
2637
Nate Begeman44728a72005-09-19 22:34:01 +00002638 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002639 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002640 // FIXME:
2641 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2642 // having to say they don't support SELECT_CC on every type the DAG knows
2643 // about, since there is no way to mark an opcode illegal at all value types
2644 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2645 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2646 N1, N2, N0.getOperand(2));
2647 else
2648 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002649 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002650 return SDOperand();
2651}
2652
2653SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002654 SDOperand N0 = N->getOperand(0);
2655 SDOperand N1 = N->getOperand(1);
2656 SDOperand N2 = N->getOperand(2);
2657 SDOperand N3 = N->getOperand(3);
2658 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002659 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2660
Nate Begeman44728a72005-09-19 22:34:01 +00002661 // fold select_cc lhs, rhs, x, x, cc -> x
2662 if (N2 == N3)
2663 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002664
Chris Lattner5f42a242006-09-20 06:19:26 +00002665 // Determine if the condition we're dealing with is constant
Scott Michel5b8f82e2008-03-10 15:42:14 +00002666 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002667 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002668
2669 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002670 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00002671 return N2; // cond always true -> true val
2672 else
2673 return N3; // cond always false -> false val
2674 }
2675
2676 // Fold to a simpler select_cc
2677 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2678 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2679 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2680 SCC.getOperand(2));
2681
Chris Lattner40c62d52005-10-18 06:04:22 +00002682 // If we can fold this based on the true/false value, do so.
2683 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002684 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002685
Nate Begeman44728a72005-09-19 22:34:01 +00002686 // fold select_cc into other things, such as min/max/abs
2687 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002688}
2689
2690SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2691 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2692 cast<CondCodeSDNode>(N->getOperand(2))->get());
2693}
2694
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002695// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2696// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2697// transformation. Returns true if extension are possible and the above
2698// mentioned transformation is profitable.
2699static bool ExtendUsesToFormExtLoad(SDNode *N, SDOperand N0,
2700 unsigned ExtOpc,
2701 SmallVector<SDNode*, 4> &ExtendNodes,
2702 TargetLowering &TLI) {
2703 bool HasCopyToRegUses = false;
2704 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
2705 for (SDNode::use_iterator UI = N0.Val->use_begin(), UE = N0.Val->use_end();
2706 UI != UE; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00002707 SDNode *User = UI->getUser();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002708 if (User == N)
2709 continue;
2710 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2711 if (User->getOpcode() == ISD::SETCC) {
2712 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2713 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2714 // Sign bits will be lost after a zext.
2715 return false;
2716 bool Add = false;
2717 for (unsigned i = 0; i != 2; ++i) {
2718 SDOperand UseOp = User->getOperand(i);
2719 if (UseOp == N0)
2720 continue;
2721 if (!isa<ConstantSDNode>(UseOp))
2722 return false;
2723 Add = true;
2724 }
2725 if (Add)
2726 ExtendNodes.push_back(User);
2727 } else {
2728 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2729 SDOperand UseOp = User->getOperand(i);
2730 if (UseOp == N0) {
2731 // If truncate from extended type to original load type is free
2732 // on this target, then it's ok to extend a CopyToReg.
2733 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2734 HasCopyToRegUses = true;
2735 else
2736 return false;
2737 }
2738 }
2739 }
2740 }
2741
2742 if (HasCopyToRegUses) {
2743 bool BothLiveOut = false;
2744 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2745 UI != UE; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00002746 SDNode *User = UI->getUser();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002747 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2748 SDOperand UseOp = User->getOperand(i);
2749 if (UseOp.Val == N && UseOp.ResNo == 0) {
2750 BothLiveOut = true;
2751 break;
2752 }
2753 }
2754 }
2755 if (BothLiveOut)
2756 // Both unextended and extended values are live out. There had better be
2757 // good a reason for the transformation.
2758 return ExtendNodes.size();
2759 }
2760 return true;
2761}
2762
Nate Begeman83e75ec2005-09-06 04:43:02 +00002763SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002764 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002765 MVT::ValueType VT = N->getValueType(0);
2766
Nate Begeman1d4d4142005-09-01 00:19:25 +00002767 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002768 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002769 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002770
Nate Begeman1d4d4142005-09-01 00:19:25 +00002771 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002772 // fold (sext (aext x)) -> (sext x)
2773 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002774 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002775
Evan Chengc88138f2007-03-22 01:54:19 +00002776 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2777 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Chris Lattner22558872007-02-26 03:13:59 +00002778 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002779 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002780 if (NarrowLoad.Val) {
2781 if (NarrowLoad.Val != N0.Val)
2782 CombineTo(N0.Val, NarrowLoad);
2783 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2784 }
Evan Chengc88138f2007-03-22 01:54:19 +00002785 }
2786
2787 // See if the value being truncated is already sign extended. If so, just
2788 // eliminate the trunc/sext pair.
2789 if (N0.getOpcode() == ISD::TRUNCATE) {
Chris Lattner6007b842006-09-21 06:00:20 +00002790 SDOperand Op = N0.getOperand(0);
Chris Lattner22558872007-02-26 03:13:59 +00002791 unsigned OpBits = MVT::getSizeInBits(Op.getValueType());
2792 unsigned MidBits = MVT::getSizeInBits(N0.getValueType());
2793 unsigned DestBits = MVT::getSizeInBits(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002794 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002795
2796 if (OpBits == DestBits) {
2797 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2798 // bits, it is already ready.
2799 if (NumSignBits > DestBits-MidBits)
2800 return Op;
2801 } else if (OpBits < DestBits) {
2802 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2803 // bits, just sext from i32.
2804 if (NumSignBits > OpBits-MidBits)
2805 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2806 } else {
2807 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2808 // bits, just truncate to i32.
2809 if (NumSignBits > OpBits-MidBits)
2810 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002811 }
Chris Lattner22558872007-02-26 03:13:59 +00002812
2813 // fold (sext (truncate x)) -> (sextinreg x).
2814 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2815 N0.getValueType())) {
2816 if (Op.getValueType() < VT)
2817 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2818 else if (Op.getValueType() > VT)
2819 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2820 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2821 DAG.getValueType(N0.getValueType()));
2822 }
Chris Lattner6007b842006-09-21 06:00:20 +00002823 }
Chris Lattner310b5782006-05-06 23:06:26 +00002824
Evan Cheng110dec22005-12-14 02:19:23 +00002825 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002826 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002827 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002828 bool DoXform = true;
2829 SmallVector<SDNode*, 4> SetCCs;
2830 if (!N0.hasOneUse())
2831 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2832 if (DoXform) {
2833 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2834 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2835 LN0->getBasePtr(), LN0->getSrcValue(),
2836 LN0->getSrcValueOffset(),
2837 N0.getValueType(),
2838 LN0->isVolatile(),
2839 LN0->getAlignment());
2840 CombineTo(N, ExtLoad);
2841 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2842 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2843 // Extend SetCC uses if necessary.
2844 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2845 SDNode *SetCC = SetCCs[i];
2846 SmallVector<SDOperand, 4> Ops;
2847 for (unsigned j = 0; j != 2; ++j) {
2848 SDOperand SOp = SetCC->getOperand(j);
2849 if (SOp == Trunc)
2850 Ops.push_back(ExtLoad);
2851 else
2852 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2853 }
2854 Ops.push_back(SetCC->getOperand(2));
2855 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2856 &Ops[0], Ops.size()));
2857 }
2858 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2859 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002860 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002861
2862 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2863 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002864 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2865 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002866 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002867 MVT::ValueType EVT = LN0->getMemoryVT();
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002868 if (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
2869 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2870 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002871 LN0->getSrcValueOffset(), EVT,
2872 LN0->isVolatile(),
2873 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002874 CombineTo(N, ExtLoad);
2875 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2876 ExtLoad.getValue(1));
2877 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2878 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002879 }
2880
Chris Lattner20a35c32007-04-11 05:32:27 +00002881 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2882 if (N0.getOpcode() == ISD::SETCC) {
2883 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002884 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2885 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2886 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2887 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002888 }
2889
Dan Gohman8f0ad582008-04-28 16:58:24 +00002890 // fold (sext x) -> (zext x) if the sign bit is known zero.
2891 if (DAG.SignBitIsZero(N0))
2892 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2893
Nate Begeman83e75ec2005-09-06 04:43:02 +00002894 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002895}
2896
Nate Begeman83e75ec2005-09-06 04:43:02 +00002897SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002898 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002899 MVT::ValueType VT = N->getValueType(0);
2900
Nate Begeman1d4d4142005-09-01 00:19:25 +00002901 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002902 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002903 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002904 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002905 // fold (zext (aext x)) -> (zext x)
2906 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002907 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002908
Evan Chengc88138f2007-03-22 01:54:19 +00002909 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2910 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002911 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002912 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002913 if (NarrowLoad.Val) {
2914 if (NarrowLoad.Val != N0.Val)
2915 CombineTo(N0.Val, NarrowLoad);
2916 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2917 }
Evan Chengc88138f2007-03-22 01:54:19 +00002918 }
2919
Chris Lattner6007b842006-09-21 06:00:20 +00002920 // fold (zext (truncate x)) -> (and x, mask)
2921 if (N0.getOpcode() == ISD::TRUNCATE &&
2922 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2923 SDOperand Op = N0.getOperand(0);
2924 if (Op.getValueType() < VT) {
2925 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2926 } else if (Op.getValueType() > VT) {
2927 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2928 }
2929 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2930 }
2931
Chris Lattner111c2282006-09-21 06:14:31 +00002932 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2933 if (N0.getOpcode() == ISD::AND &&
2934 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2935 N0.getOperand(1).getOpcode() == ISD::Constant) {
2936 SDOperand X = N0.getOperand(0).getOperand(0);
2937 if (X.getValueType() < VT) {
2938 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2939 } else if (X.getValueType() > VT) {
2940 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2941 }
Dan Gohman220a8232008-03-03 23:51:38 +00002942 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2943 Mask.zext(MVT::getSizeInBits(VT));
Chris Lattner111c2282006-09-21 06:14:31 +00002944 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2945 }
2946
Evan Cheng110dec22005-12-14 02:19:23 +00002947 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002948 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002949 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002950 bool DoXform = true;
2951 SmallVector<SDNode*, 4> SetCCs;
2952 if (!N0.hasOneUse())
2953 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
2954 if (DoXform) {
2955 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2956 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2957 LN0->getBasePtr(), LN0->getSrcValue(),
2958 LN0->getSrcValueOffset(),
2959 N0.getValueType(),
2960 LN0->isVolatile(),
2961 LN0->getAlignment());
2962 CombineTo(N, ExtLoad);
2963 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2964 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2965 // Extend SetCC uses if necessary.
2966 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2967 SDNode *SetCC = SetCCs[i];
2968 SmallVector<SDOperand, 4> Ops;
2969 for (unsigned j = 0; j != 2; ++j) {
2970 SDOperand SOp = SetCC->getOperand(j);
2971 if (SOp == Trunc)
2972 Ops.push_back(ExtLoad);
2973 else
Evan Chengde1631b2007-10-30 20:11:21 +00002974 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002975 }
2976 Ops.push_back(SetCC->getOperand(2));
2977 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2978 &Ops[0], Ops.size()));
2979 }
2980 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2981 }
Evan Cheng110dec22005-12-14 02:19:23 +00002982 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002983
2984 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2985 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002986 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2987 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002988 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002989 MVT::ValueType EVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002990 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2991 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002992 LN0->getSrcValueOffset(), EVT,
2993 LN0->isVolatile(),
2994 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002995 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002996 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2997 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002998 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002999 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003000
3001 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3002 if (N0.getOpcode() == ISD::SETCC) {
3003 SDOperand SCC =
3004 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3005 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003006 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
3007 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003008 }
3009
Nate Begeman83e75ec2005-09-06 04:43:02 +00003010 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003011}
3012
Chris Lattner5ffc0662006-05-05 05:58:59 +00003013SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
3014 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003015 MVT::ValueType VT = N->getValueType(0);
3016
3017 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003018 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00003019 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
3020 // fold (aext (aext x)) -> (aext x)
3021 // fold (aext (zext x)) -> (zext x)
3022 // fold (aext (sext x)) -> (sext x)
3023 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3024 N0.getOpcode() == ISD::ZERO_EXTEND ||
3025 N0.getOpcode() == ISD::SIGN_EXTEND)
3026 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
3027
Evan Chengc88138f2007-03-22 01:54:19 +00003028 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3029 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3030 if (N0.getOpcode() == ISD::TRUNCATE) {
3031 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00003032 if (NarrowLoad.Val) {
3033 if (NarrowLoad.Val != N0.Val)
3034 CombineTo(N0.Val, NarrowLoad);
3035 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
3036 }
Evan Chengc88138f2007-03-22 01:54:19 +00003037 }
3038
Chris Lattner84750582006-09-20 06:29:17 +00003039 // fold (aext (truncate x))
3040 if (N0.getOpcode() == ISD::TRUNCATE) {
3041 SDOperand TruncOp = N0.getOperand(0);
3042 if (TruncOp.getValueType() == VT)
3043 return TruncOp; // x iff x size == zext size.
3044 if (TruncOp.getValueType() > VT)
3045 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
3046 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
3047 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00003048
3049 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3050 if (N0.getOpcode() == ISD::AND &&
3051 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3052 N0.getOperand(1).getOpcode() == ISD::Constant) {
3053 SDOperand X = N0.getOperand(0).getOperand(0);
3054 if (X.getValueType() < VT) {
3055 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
3056 } else if (X.getValueType() > VT) {
3057 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3058 }
Dan Gohman220a8232008-03-03 23:51:38 +00003059 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
3060 Mask.zext(MVT::getSizeInBits(VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00003061 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3062 }
3063
Chris Lattner5ffc0662006-05-05 05:58:59 +00003064 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00003065 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003066 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003067 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3068 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3069 LN0->getBasePtr(), LN0->getSrcValue(),
3070 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003071 N0.getValueType(),
3072 LN0->isVolatile(),
3073 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003074 CombineTo(N, ExtLoad);
3075 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3076 ExtLoad.getValue(1));
3077 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3078 }
3079
3080 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3081 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3082 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003083 if (N0.getOpcode() == ISD::LOAD &&
3084 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003085 N0.hasOneUse()) {
3086 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003087 MVT::ValueType EVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00003088 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
3089 LN0->getChain(), LN0->getBasePtr(),
3090 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003091 LN0->getSrcValueOffset(), EVT,
3092 LN0->isVolatile(),
3093 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003094 CombineTo(N, ExtLoad);
3095 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3096 ExtLoad.getValue(1));
3097 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3098 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003099
3100 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3101 if (N0.getOpcode() == ISD::SETCC) {
3102 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003103 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3104 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003105 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00003106 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003107 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003108 }
3109
Chris Lattner5ffc0662006-05-05 05:58:59 +00003110 return SDOperand();
3111}
3112
Chris Lattner2b4c2792007-10-13 06:35:54 +00003113/// GetDemandedBits - See if the specified operand can be simplified with the
3114/// knowledge that only the bits specified by Mask are used. If so, return the
3115/// simpler operand, otherwise return a null SDOperand.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003116SDOperand DAGCombiner::GetDemandedBits(SDOperand V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003117 switch (V.getOpcode()) {
3118 default: break;
3119 case ISD::OR:
3120 case ISD::XOR:
3121 // If the LHS or RHS don't contribute bits to the or, drop them.
3122 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3123 return V.getOperand(1);
3124 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3125 return V.getOperand(0);
3126 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003127 case ISD::SRL:
3128 // Only look at single-use SRLs.
3129 if (!V.Val->hasOneUse())
3130 break;
3131 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3132 // See if we can recursively simplify the LHS.
3133 unsigned Amt = RHSC->getValue();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003134 APInt NewMask = Mask << Amt;
3135 SDOperand SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Chris Lattnere33544c2007-10-13 06:58:48 +00003136 if (SimplifyLHS.Val) {
3137 return DAG.getNode(ISD::SRL, V.getValueType(),
3138 SimplifyLHS, V.getOperand(1));
3139 }
3140 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003141 }
3142 return SDOperand();
3143}
3144
Evan Chengc88138f2007-03-22 01:54:19 +00003145/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3146/// bits and then truncated to a narrower type and where N is a multiple
3147/// of number of bits of the narrower type, transform it to a narrower load
3148/// from address + N / num of bits of new type. If the result is to be
3149/// extended, also fold the extension to form a extending load.
3150SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
3151 unsigned Opc = N->getOpcode();
3152 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
3153 SDOperand N0 = N->getOperand(0);
3154 MVT::ValueType VT = N->getValueType(0);
3155 MVT::ValueType EVT = N->getValueType(0);
3156
Evan Chenge177e302007-03-23 22:13:36 +00003157 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3158 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003159 if (Opc == ISD::SIGN_EXTEND_INREG) {
3160 ExtType = ISD::SEXTLOAD;
3161 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00003162 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
3163 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00003164 }
3165
3166 unsigned EVTBits = MVT::getSizeInBits(EVT);
3167 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003168 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003169 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3170 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3171 ShAmt = N01->getValue();
3172 // Is the shift amount a multiple of size of VT?
3173 if ((ShAmt & (EVTBits-1)) == 0) {
3174 N0 = N0.getOperand(0);
3175 if (MVT::getSizeInBits(N0.getValueType()) <= EVTBits)
3176 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00003177 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003178 }
3179 }
3180 }
3181
3182 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
3183 // Do not allow folding to i1 here. i1 is implicitly stored in memory in
3184 // zero extended form: by shrinking the load, we lose track of the fact
3185 // that it is already zero extended.
3186 // FIXME: This should be reevaluated.
3187 VT != MVT::i1) {
3188 assert(MVT::getSizeInBits(N0.getValueType()) > EVTBits &&
3189 "Cannot truncate to larger type!");
3190 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3191 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003192 // For big endian targets, we need to adjust the offset to the pointer to
3193 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003194 if (TLI.isBigEndian()) {
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003195 unsigned LVTStoreBits = MVT::getStoreSizeInBits(N0.getValueType());
3196 unsigned EVTStoreBits = MVT::getStoreSizeInBits(EVT);
3197 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3198 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003199 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003200 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Evan Chengc88138f2007-03-22 01:54:19 +00003201 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
3202 DAG.getConstant(PtrOff, PtrType));
3203 AddToWorkList(NewPtr.Val);
3204 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
3205 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003206 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Duncan Sandsdc846502007-10-28 12:59:45 +00003207 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003208 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003209 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00003210 LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003211 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003212 if (CombineSRL) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003213 WorkListRemover DeadNodes(*this);
3214 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3215 &DeadNodes);
Evan Chengb37b80c2007-03-23 20:55:21 +00003216 CombineTo(N->getOperand(0).Val, Load);
3217 } else
3218 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003219 if (ShAmt) {
3220 if (Opc == ISD::SIGN_EXTEND_INREG)
3221 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3222 else
3223 return DAG.getNode(Opc, VT, Load);
3224 }
Evan Chengc88138f2007-03-22 01:54:19 +00003225 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3226 }
3227
3228 return SDOperand();
3229}
3230
Chris Lattner5ffc0662006-05-05 05:58:59 +00003231
Nate Begeman83e75ec2005-09-06 04:43:02 +00003232SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003233 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003234 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003235 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003236 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003237 unsigned VTBits = MVT::getSizeInBits(VT);
Nate Begeman07ed4172005-10-10 21:26:48 +00003238 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003239
Nate Begeman1d4d4142005-09-01 00:19:25 +00003240 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003241 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003242 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003243
Chris Lattner541a24f2006-05-06 22:43:44 +00003244 // If the input is already sign extended, just drop the extension.
Dan Gohmanea859be2007-06-22 14:59:07 +00003245 if (DAG.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003246 return N0;
3247
Nate Begeman646d7e22005-09-02 21:18:40 +00003248 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3249 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3250 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003251 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003252 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003253
Chris Lattner95a5e052007-04-17 19:03:21 +00003254 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003255 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003256 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003257
Chris Lattner95a5e052007-04-17 19:03:21 +00003258 // fold operands of sext_in_reg based on knowledge that the top bits are not
3259 // demanded.
3260 if (SimplifyDemandedBits(SDOperand(N, 0)))
3261 return SDOperand(N, 0);
3262
Evan Chengc88138f2007-03-22 01:54:19 +00003263 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3264 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
3265 SDOperand NarrowLoad = ReduceLoadWidth(N);
3266 if (NarrowLoad.Val)
3267 return NarrowLoad;
3268
Chris Lattner4b37e872006-05-08 21:18:59 +00003269 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3270 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3271 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3272 if (N0.getOpcode() == ISD::SRL) {
3273 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
3274 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
3275 // We can turn this into an SRA iff the input to the SRL is already sign
3276 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003277 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Chris Lattner4b37e872006-05-08 21:18:59 +00003278 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
3279 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3280 }
3281 }
Evan Chengc88138f2007-03-22 01:54:19 +00003282
Nate Begemanded49632005-10-13 03:11:28 +00003283 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00003284 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003285 ISD::isUNINDEXEDLoad(N0.Val) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003286 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003287 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003288 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3289 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3290 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003291 LN0->getSrcValueOffset(), EVT,
3292 LN0->isVolatile(),
3293 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003294 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003295 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003296 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003297 }
3298 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00003299 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
3300 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003301 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003302 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003303 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3304 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3305 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003306 LN0->getSrcValueOffset(), EVT,
3307 LN0->isVolatile(),
3308 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003309 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003310 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003311 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003312 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003313 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003314}
3315
Nate Begeman83e75ec2005-09-06 04:43:02 +00003316SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003317 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003318 MVT::ValueType VT = N->getValueType(0);
3319
3320 // noop truncate
3321 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003322 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003323 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003324 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003325 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003326 // fold (truncate (truncate x)) -> (truncate x)
3327 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003328 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003329 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003330 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3331 N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003332 if (N0.getOperand(0).getValueType() < VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003333 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003334 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003335 else if (N0.getOperand(0).getValueType() > VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003336 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003337 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003338 else
3339 // if the source and dest are the same type, we can drop both the extend
3340 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003341 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003342 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003343
Chris Lattner2b4c2792007-10-13 06:35:54 +00003344 // See if we can simplify the input to this truncate through knowledge that
3345 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3346 // -> trunc y
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003347 SDOperand Shorter =
3348 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
3349 MVT::getSizeInBits(VT)));
Chris Lattner2b4c2792007-10-13 06:35:54 +00003350 if (Shorter.Val)
3351 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3352
Nate Begeman3df4d522005-10-12 20:40:40 +00003353 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003354 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003355 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003356}
3357
Chris Lattner94683772005-12-23 05:30:37 +00003358SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3359 SDOperand N0 = N->getOperand(0);
3360 MVT::ValueType VT = N->getValueType(0);
3361
Dan Gohman7f321562007-06-25 16:23:39 +00003362 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3363 // Only do this before legalize, since afterward the target may be depending
3364 // on the bitconvert.
3365 // First check to see if this is all constant.
3366 if (!AfterLegalize &&
3367 N0.getOpcode() == ISD::BUILD_VECTOR && N0.Val->hasOneUse() &&
3368 MVT::isVector(VT)) {
3369 bool isSimple = true;
3370 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3371 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3372 N0.getOperand(i).getOpcode() != ISD::Constant &&
3373 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3374 isSimple = false;
3375 break;
3376 }
3377
3378 MVT::ValueType DestEltVT = MVT::getVectorElementType(N->getValueType(0));
3379 assert(!MVT::isVector(DestEltVT) &&
3380 "Element type of vector ValueType must not be vector!");
3381 if (isSimple) {
3382 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.Val, DestEltVT);
3383 }
3384 }
3385
Chris Lattner94683772005-12-23 05:30:37 +00003386 // If the input is a constant, let getNode() fold it.
3387 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
3388 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3389 if (Res.Val != N) return Res;
3390 }
3391
Chris Lattnerc8547d82005-12-23 05:37:50 +00003392 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3393 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003394
Chris Lattner57104102005-12-23 05:44:41 +00003395 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003396 // If the resultant load doesn't need a higher alignment than the original!
3397 if (ISD::isNormalLoad(N0.Val) && N0.hasOneUse() &&
Evan Cheng59d5b682007-05-07 21:27:48 +00003398 TLI.isOperationLegal(ISD::LOAD, VT)) {
Evan Cheng466685d2006-10-09 20:57:25 +00003399 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00003400 unsigned Align = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00003401 getABITypeAlignment(MVT::getTypeForValueType(VT));
Evan Cheng59d5b682007-05-07 21:27:48 +00003402 unsigned OrigAlign = LN0->getAlignment();
3403 if (Align <= OrigAlign) {
3404 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
3405 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003406 LN0->isVolatile(), Align);
Evan Cheng59d5b682007-05-07 21:27:48 +00003407 AddToWorkList(N);
3408 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
3409 Load.getValue(1));
3410 return Load;
3411 }
Chris Lattner57104102005-12-23 05:44:41 +00003412 }
3413
Chris Lattner3bd39d42008-01-27 17:42:27 +00003414 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3415 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3416 // This often reduces constant pool loads.
3417 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
3418 N0.Val->hasOneUse() && MVT::isInteger(VT) && !MVT::isVector(VT)) {
3419 SDOperand NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3420 AddToWorkList(NewConv.Val);
3421
Dan Gohman220a8232008-03-03 23:51:38 +00003422 APInt SignBit = APInt::getSignBit(MVT::getSizeInBits(VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00003423 if (N0.getOpcode() == ISD::FNEG)
3424 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3425 assert(N0.getOpcode() == ISD::FABS);
3426 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3427 }
3428
3429 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3430 // Note that we don't handle copysign(x,cst) because this can always be folded
3431 // to an fneg or fabs.
3432 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003433 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
3434 MVT::isInteger(VT) && !MVT::isVector(VT)) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003435 unsigned OrigXWidth = MVT::getSizeInBits(N0.getOperand(1).getValueType());
3436 SDOperand X = DAG.getNode(ISD::BIT_CONVERT, MVT::getIntegerType(OrigXWidth),
3437 N0.getOperand(1));
3438 AddToWorkList(X.Val);
3439
3440 // If X has a different width than the result/lhs, sext it or truncate it.
3441 unsigned VTWidth = MVT::getSizeInBits(VT);
3442 if (OrigXWidth < VTWidth) {
3443 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
3444 AddToWorkList(X.Val);
3445 } else if (OrigXWidth > VTWidth) {
3446 // To get the sign bit in the right place, we have to shift it right
3447 // before truncating.
3448 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3449 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3450 AddToWorkList(X.Val);
3451 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3452 AddToWorkList(X.Val);
3453 }
3454
Dan Gohman220a8232008-03-03 23:51:38 +00003455 APInt SignBit = APInt::getSignBit(MVT::getSizeInBits(VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00003456 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
3457 AddToWorkList(X.Val);
3458
3459 SDOperand Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3460 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
3461 AddToWorkList(Cst.Val);
3462
3463 return DAG.getNode(ISD::OR, VT, X, Cst);
3464 }
3465
Chris Lattner94683772005-12-23 05:30:37 +00003466 return SDOperand();
3467}
3468
Dan Gohman7f321562007-06-25 16:23:39 +00003469/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003470/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3471/// destination element value type.
3472SDOperand DAGCombiner::
Dan Gohman7f321562007-06-25 16:23:39 +00003473ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003474 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
3475
3476 // If this is already the right type, we're done.
3477 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
3478
3479 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
3480 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
3481
3482 // If this is a conversion of N elements of one type to N elements of another
3483 // type, convert each element. This handles FP<->INT cases.
3484 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003485 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003486 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003487 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00003488 AddToWorkList(Ops.back().Val);
3489 }
Dan Gohman7f321562007-06-25 16:23:39 +00003490 MVT::ValueType VT =
3491 MVT::getVectorType(DstEltVT,
3492 MVT::getVectorNumElements(BV->getValueType(0)));
3493 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003494 }
3495
3496 // Otherwise, we're growing or shrinking the elements. To avoid having to
3497 // handle annoying details of growing/shrinking FP values, we convert them to
3498 // int first.
3499 if (MVT::isFloatingPoint(SrcEltVT)) {
3500 // Convert the input float vector to a int vector where the elements are the
3501 // same sizes.
3502 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
3503 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003504 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003505 SrcEltVT = IntVT;
3506 }
3507
3508 // Now we know the input is an integer vector. If the output is a FP type,
3509 // convert to integer first, then to FP of the right size.
3510 if (MVT::isFloatingPoint(DstEltVT)) {
3511 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
3512 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003513 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003514
3515 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003516 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003517 }
3518
3519 // Okay, we know the src/dst types are both integers of differing types.
3520 // Handling growing first.
3521 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
3522 if (SrcBitSize < DstBitSize) {
3523 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3524
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003525 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003526 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003527 i += NumInputsPerOutput) {
3528 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003529 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003530 bool EltIsUndef = true;
3531 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3532 // Shift the previously computed bits over.
3533 NewBits <<= SrcBitSize;
3534 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
3535 if (Op.getOpcode() == ISD::UNDEF) continue;
3536 EltIsUndef = false;
3537
Dan Gohman220a8232008-03-03 23:51:38 +00003538 NewBits |=
3539 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003540 }
3541
3542 if (EltIsUndef)
3543 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3544 else
3545 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3546 }
3547
Evan Chengefec7512008-02-18 23:04:32 +00003548 MVT::ValueType VT = MVT::getVectorType(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003549 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003550 }
3551
3552 // Finally, this must be the case where we are shrinking elements: each input
3553 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003554 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003555 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Evan Chengefec7512008-02-18 23:04:32 +00003556 MVT::ValueType VT = MVT::getVectorType(DstEltVT,
3557 NumOutputsPerInput * BV->getNumOperands());
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003558 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003559 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003560 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3561 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3562 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3563 continue;
3564 }
Dan Gohman220a8232008-03-03 23:51:38 +00003565 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003566 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003567 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003568 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003569 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003570 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3571 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003572 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003573 }
3574
3575 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003576 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003577 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3578 }
Dan Gohman7f321562007-06-25 16:23:39 +00003579 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003580}
3581
3582
3583
Chris Lattner01b3d732005-09-28 22:28:18 +00003584SDOperand DAGCombiner::visitFADD(SDNode *N) {
3585 SDOperand N0 = N->getOperand(0);
3586 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003587 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3588 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003589 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003590
Dan Gohman7f321562007-06-25 16:23:39 +00003591 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003592 if (MVT::isVector(VT)) {
3593 SDOperand FoldedVOp = SimplifyVBinOp(N);
3594 if (FoldedVOp.Val) return FoldedVOp;
3595 }
Dan Gohman7f321562007-06-25 16:23:39 +00003596
Nate Begemana0e221d2005-10-18 00:28:13 +00003597 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003598 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003599 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003600 // canonicalize constant to RHS
3601 if (N0CFP && !N1CFP)
3602 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003603 // fold (A + (-B)) -> A-B
Chris Lattner0254e702008-02-26 07:04:54 +00003604 if (isNegatibleForFree(N1, AfterLegalize) == 2)
3605 return DAG.getNode(ISD::FSUB, VT, N0,
3606 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner01b3d732005-09-28 22:28:18 +00003607 // fold ((-A) + B) -> B-A
Chris Lattner0254e702008-02-26 07:04:54 +00003608 if (isNegatibleForFree(N0, AfterLegalize) == 2)
3609 return DAG.getNode(ISD::FSUB, VT, N1,
3610 GetNegatedExpression(N0, DAG, AfterLegalize));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003611
3612 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3613 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
3614 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3615 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3616 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3617
Chris Lattner01b3d732005-09-28 22:28:18 +00003618 return SDOperand();
3619}
3620
3621SDOperand DAGCombiner::visitFSUB(SDNode *N) {
3622 SDOperand N0 = N->getOperand(0);
3623 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003624 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3625 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003626 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003627
Dan Gohman7f321562007-06-25 16:23:39 +00003628 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003629 if (MVT::isVector(VT)) {
3630 SDOperand FoldedVOp = SimplifyVBinOp(N);
3631 if (FoldedVOp.Val) return FoldedVOp;
3632 }
Dan Gohman7f321562007-06-25 16:23:39 +00003633
Nate Begemana0e221d2005-10-18 00:28:13 +00003634 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003635 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003636 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003637 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003638 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Chris Lattner0254e702008-02-26 07:04:54 +00003639 if (isNegatibleForFree(N1, AfterLegalize))
3640 return GetNegatedExpression(N1, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003641 return DAG.getNode(ISD::FNEG, VT, N1);
3642 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003643 // fold (A-(-B)) -> A+B
Chris Lattner0254e702008-02-26 07:04:54 +00003644 if (isNegatibleForFree(N1, AfterLegalize))
3645 return DAG.getNode(ISD::FADD, VT, N0,
3646 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003647
Chris Lattner01b3d732005-09-28 22:28:18 +00003648 return SDOperand();
3649}
3650
3651SDOperand DAGCombiner::visitFMUL(SDNode *N) {
3652 SDOperand N0 = N->getOperand(0);
3653 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003654 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3655 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003656 MVT::ValueType VT = N->getValueType(0);
3657
Dan Gohman7f321562007-06-25 16:23:39 +00003658 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003659 if (MVT::isVector(VT)) {
3660 SDOperand FoldedVOp = SimplifyVBinOp(N);
3661 if (FoldedVOp.Val) return FoldedVOp;
3662 }
Dan Gohman7f321562007-06-25 16:23:39 +00003663
Nate Begeman11af4ea2005-10-17 20:40:11 +00003664 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003665 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003666 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003667 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003668 if (N0CFP && !N1CFP)
3669 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003670 // fold (fmul X, 2.0) -> (fadd X, X)
3671 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3672 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003673 // fold (fmul X, -1.0) -> (fneg X)
3674 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3675 return DAG.getNode(ISD::FNEG, VT, N0);
3676
3677 // -X * -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003678 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3679 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003680 // Both can be negated for free, check to see if at least one is cheaper
3681 // negated.
3682 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003683 return DAG.getNode(ISD::FMUL, VT,
3684 GetNegatedExpression(N0, DAG, AfterLegalize),
3685 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003686 }
3687 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003688
3689 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3690 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
3691 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3692 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3693 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3694
Chris Lattner01b3d732005-09-28 22:28:18 +00003695 return SDOperand();
3696}
3697
3698SDOperand DAGCombiner::visitFDIV(SDNode *N) {
3699 SDOperand N0 = N->getOperand(0);
3700 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003701 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3702 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003703 MVT::ValueType VT = N->getValueType(0);
3704
Dan Gohman7f321562007-06-25 16:23:39 +00003705 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003706 if (MVT::isVector(VT)) {
3707 SDOperand FoldedVOp = SimplifyVBinOp(N);
3708 if (FoldedVOp.Val) return FoldedVOp;
3709 }
Dan Gohman7f321562007-06-25 16:23:39 +00003710
Nate Begemana148d982006-01-18 22:35:16 +00003711 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003712 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003713 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003714
3715
3716 // -X / -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003717 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3718 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003719 // Both can be negated for free, check to see if at least one is cheaper
3720 // negated.
3721 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003722 return DAG.getNode(ISD::FDIV, VT,
3723 GetNegatedExpression(N0, DAG, AfterLegalize),
3724 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003725 }
3726 }
3727
Chris Lattner01b3d732005-09-28 22:28:18 +00003728 return SDOperand();
3729}
3730
3731SDOperand DAGCombiner::visitFREM(SDNode *N) {
3732 SDOperand N0 = N->getOperand(0);
3733 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003734 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3735 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003736 MVT::ValueType VT = N->getValueType(0);
3737
Nate Begemana148d982006-01-18 22:35:16 +00003738 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003739 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003740 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003741
Chris Lattner01b3d732005-09-28 22:28:18 +00003742 return SDOperand();
3743}
3744
Chris Lattner12d83032006-03-05 05:30:57 +00003745SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3746 SDOperand N0 = N->getOperand(0);
3747 SDOperand N1 = N->getOperand(1);
3748 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3749 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3750 MVT::ValueType VT = N->getValueType(0);
3751
Dale Johannesendb44bf82007-10-16 23:38:29 +00003752 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003753 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3754
3755 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003756 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003757 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3758 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003759 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003760 return DAG.getNode(ISD::FABS, VT, N0);
3761 else
3762 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3763 }
3764
3765 // copysign(fabs(x), y) -> copysign(x, y)
3766 // copysign(fneg(x), y) -> copysign(x, y)
3767 // copysign(copysign(x,z), y) -> copysign(x, y)
3768 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3769 N0.getOpcode() == ISD::FCOPYSIGN)
3770 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3771
3772 // copysign(x, abs(y)) -> abs(x)
3773 if (N1.getOpcode() == ISD::FABS)
3774 return DAG.getNode(ISD::FABS, VT, N0);
3775
3776 // copysign(x, copysign(y,z)) -> copysign(x, z)
3777 if (N1.getOpcode() == ISD::FCOPYSIGN)
3778 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3779
3780 // copysign(x, fp_extend(y)) -> copysign(x, y)
3781 // copysign(x, fp_round(y)) -> copysign(x, y)
3782 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3783 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3784
3785 return SDOperand();
3786}
3787
3788
Chris Lattner01b3d732005-09-28 22:28:18 +00003789
Nate Begeman83e75ec2005-09-06 04:43:02 +00003790SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003791 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003792 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003793 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003794
3795 // fold (sint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003796 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003797 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003798 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003799}
3800
Nate Begeman83e75ec2005-09-06 04:43:02 +00003801SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003802 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003803 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003804 MVT::ValueType VT = N->getValueType(0);
3805
Nate Begeman1d4d4142005-09-01 00:19:25 +00003806 // fold (uint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003807 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003808 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003809 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003810}
3811
Nate Begeman83e75ec2005-09-06 04:43:02 +00003812SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003813 SDOperand N0 = N->getOperand(0);
3814 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3815 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003816
3817 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003818 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003819 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003820 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003821}
3822
Nate Begeman83e75ec2005-09-06 04:43:02 +00003823SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003824 SDOperand N0 = N->getOperand(0);
3825 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3826 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003827
3828 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00003829 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003830 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003831 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003832}
3833
Nate Begeman83e75ec2005-09-06 04:43:02 +00003834SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003835 SDOperand N0 = N->getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003836 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003837 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3838 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003839
3840 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003841 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00003842 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003843
3844 // fold (fp_round (fp_extend x)) -> x
3845 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3846 return N0.getOperand(0);
3847
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00003848 // fold (fp_round (fp_round x)) -> (fp_round x)
3849 if (N0.getOpcode() == ISD::FP_ROUND) {
3850 // This is a value preserving truncation if both round's are.
3851 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
3852 N0.Val->getConstantOperandVal(1) == 1;
3853 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
3854 DAG.getIntPtrConstant(IsTrunc));
3855 }
3856
Chris Lattner79dbea52006-03-13 06:26:26 +00003857 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3858 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003859 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003860 AddToWorkList(Tmp.Val);
3861 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3862 }
3863
Nate Begeman83e75ec2005-09-06 04:43:02 +00003864 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003865}
3866
Nate Begeman83e75ec2005-09-06 04:43:02 +00003867SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003868 SDOperand N0 = N->getOperand(0);
3869 MVT::ValueType VT = N->getValueType(0);
3870 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003871 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003872
Nate Begeman1d4d4142005-09-01 00:19:25 +00003873 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003874 if (N0CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003875 SDOperand Round = DAG.getConstantFP(N0CFP->getValueAPF(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003876 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003877 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003878 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003879}
3880
Nate Begeman83e75ec2005-09-06 04:43:02 +00003881SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003882 SDOperand N0 = N->getOperand(0);
3883 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3884 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003885
Chris Lattner5938bef2007-12-29 06:55:23 +00003886 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003887 if (N->hasOneUse() &&
3888 N->use_begin()->getSDOperand().getOpcode() == ISD::FP_ROUND)
Chris Lattner5938bef2007-12-29 06:55:23 +00003889 return SDOperand();
Chris Lattner0bd48932008-01-17 07:00:52 +00003890
Nate Begeman1d4d4142005-09-01 00:19:25 +00003891 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003892 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003893 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003894
3895 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
3896 // value of X.
3897 if (N0.getOpcode() == ISD::FP_ROUND && N0.Val->getConstantOperandVal(1) == 1){
3898 SDOperand In = N0.getOperand(0);
3899 if (In.getValueType() == VT) return In;
3900 if (VT < In.getValueType())
3901 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
3902 return DAG.getNode(ISD::FP_EXTEND, VT, In);
3903 }
3904
3905 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Dale Johannesenb6210fc2007-10-19 20:29:00 +00003906 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003907 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003908 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3909 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3910 LN0->getBasePtr(), LN0->getSrcValue(),
3911 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003912 N0.getValueType(),
3913 LN0->isVolatile(),
3914 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003915 CombineTo(N, ExtLoad);
Chris Lattner0bd48932008-01-17 07:00:52 +00003916 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad,
3917 DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00003918 ExtLoad.getValue(1));
3919 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3920 }
3921
3922
Nate Begeman83e75ec2005-09-06 04:43:02 +00003923 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003924}
3925
Nate Begeman83e75ec2005-09-06 04:43:02 +00003926SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003927 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003928
Chris Lattner0254e702008-02-26 07:04:54 +00003929 if (isNegatibleForFree(N0, AfterLegalize))
3930 return GetNegatedExpression(N0, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003931
Chris Lattner3bd39d42008-01-27 17:42:27 +00003932 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
3933 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00003934 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
3935 MVT::isInteger(N0.getOperand(0).getValueType()) &&
3936 !MVT::isVector(N0.getOperand(0).getValueType())) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003937 SDOperand Int = N0.getOperand(0);
3938 MVT::ValueType IntVT = Int.getValueType();
3939 if (MVT::isInteger(IntVT) && !MVT::isVector(IntVT)) {
3940 Int = DAG.getNode(ISD::XOR, IntVT, Int,
3941 DAG.getConstant(MVT::getIntVTSignBit(IntVT), IntVT));
3942 AddToWorkList(Int.Val);
3943 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
3944 }
3945 }
3946
Nate Begeman83e75ec2005-09-06 04:43:02 +00003947 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003948}
3949
Nate Begeman83e75ec2005-09-06 04:43:02 +00003950SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003951 SDOperand N0 = N->getOperand(0);
3952 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3953 MVT::ValueType VT = N->getValueType(0);
3954
Nate Begeman1d4d4142005-09-01 00:19:25 +00003955 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003956 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003957 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003958 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003959 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003960 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003961 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003962 // fold (fabs (fcopysign x, y)) -> (fabs x)
3963 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
3964 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
3965
Chris Lattner3bd39d42008-01-27 17:42:27 +00003966 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
3967 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00003968 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
3969 MVT::isInteger(N0.getOperand(0).getValueType()) &&
3970 !MVT::isVector(N0.getOperand(0).getValueType())) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003971 SDOperand Int = N0.getOperand(0);
3972 MVT::ValueType IntVT = Int.getValueType();
3973 if (MVT::isInteger(IntVT) && !MVT::isVector(IntVT)) {
3974 Int = DAG.getNode(ISD::AND, IntVT, Int,
3975 DAG.getConstant(~MVT::getIntVTSignBit(IntVT), IntVT));
3976 AddToWorkList(Int.Val);
3977 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
3978 }
3979 }
3980
Nate Begeman83e75ec2005-09-06 04:43:02 +00003981 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003982}
3983
Nate Begeman44728a72005-09-19 22:34:01 +00003984SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
3985 SDOperand Chain = N->getOperand(0);
3986 SDOperand N1 = N->getOperand(1);
3987 SDOperand N2 = N->getOperand(2);
3988 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3989
3990 // never taken branch, fold to chain
3991 if (N1C && N1C->isNullValue())
3992 return Chain;
3993 // unconditional branch
Dan Gohman002e5d02008-03-13 22:13:53 +00003994 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00003995 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003996 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
3997 // on the target.
3998 if (N1.getOpcode() == ISD::SETCC &&
3999 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
4000 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
4001 N1.getOperand(0), N1.getOperand(1), N2);
4002 }
Nate Begeman44728a72005-09-19 22:34:01 +00004003 return SDOperand();
4004}
4005
Chris Lattner3ea0b472005-10-05 06:47:48 +00004006// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4007//
Nate Begeman44728a72005-09-19 22:34:01 +00004008SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00004009 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
4010 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
4011
4012 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00004013 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00004014 if (Simp.Val) AddToWorkList(Simp.Val);
4015
Nate Begemane17daeb2005-10-05 21:43:42 +00004016 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
4017
4018 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman002e5d02008-03-13 22:13:53 +00004019 if (SCCC && !SCCC->isNullValue())
Nate Begemane17daeb2005-10-05 21:43:42 +00004020 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
4021 N->getOperand(4));
4022 // fold br_cc false, dest -> unconditional fall through
4023 if (SCCC && SCCC->isNullValue())
4024 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00004025
Nate Begemane17daeb2005-10-05 21:43:42 +00004026 // fold to a simpler setcc
4027 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
4028 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4029 Simp.getOperand(2), Simp.getOperand(0),
4030 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00004031 return SDOperand();
4032}
4033
Chris Lattner448f2192006-11-11 00:39:41 +00004034
4035/// CombineToPreIndexedLoadStore - Try turning a load / store and a
4036/// pre-indexed load / store when the base pointer is a add or subtract
4037/// and it has other uses besides the load / store. After the
4038/// transformation, the new indexed load / store has effectively folded
4039/// the add / subtract in and all of its other uses are redirected to the
4040/// new load / store.
4041bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
4042 if (!AfterLegalize)
4043 return false;
4044
4045 bool isLoad = true;
4046 SDOperand Ptr;
4047 MVT::ValueType VT;
4048 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004049 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004050 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004051 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00004052 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00004053 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4054 return false;
4055 Ptr = LD->getBasePtr();
4056 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004057 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004058 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004059 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004060 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4061 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4062 return false;
4063 Ptr = ST->getBasePtr();
4064 isLoad = false;
4065 } else
4066 return false;
4067
Chris Lattner9f1794e2006-11-11 00:56:29 +00004068 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4069 // out. There is no reason to make this a preinc/predec.
4070 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
4071 Ptr.Val->hasOneUse())
4072 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004073
Chris Lattner9f1794e2006-11-11 00:56:29 +00004074 // Ask the target to do addressing mode selection.
4075 SDOperand BasePtr;
4076 SDOperand Offset;
4077 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4078 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4079 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004080 // Don't create a indexed load / store with zero offset.
4081 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004082 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004083 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004084
Chris Lattner41e53fd2006-11-11 01:00:15 +00004085 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004086 // 1) The new base ptr is a frame index.
4087 // 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 +00004088 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004089 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004090 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004091 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004092
Chris Lattner41e53fd2006-11-11 01:00:15 +00004093 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4094 // (plus the implicit offset) to a register to preinc anyway.
4095 if (isa<FrameIndexSDNode>(BasePtr))
4096 return false;
4097
4098 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004099 if (!isLoad) {
4100 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Cheng917be682008-03-04 00:41:45 +00004101 if (Val == BasePtr || BasePtr.Val->isPredecessorOf(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004102 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004103 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004104
Evan Chengc843abe2007-05-24 02:35:39 +00004105 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004106 bool RealUse = false;
4107 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4108 E = Ptr.Val->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004109 SDNode *Use = I->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004110 if (Use == N)
4111 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004112 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004113 return false;
4114
4115 if (!((Use->getOpcode() == ISD::LOAD &&
4116 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004117 (Use->getOpcode() == ISD::STORE &&
4118 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004119 RealUse = true;
4120 }
4121 if (!RealUse)
4122 return false;
4123
4124 SDOperand Result;
4125 if (isLoad)
4126 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
4127 else
4128 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4129 ++PreIndexedNodes;
4130 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004131 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004132 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4133 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004134 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004135 if (isLoad) {
4136 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004137 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004138 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004139 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004140 } else {
4141 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004142 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004143 }
4144
Chris Lattner9f1794e2006-11-11 00:56:29 +00004145 // Finally, since the node is now dead, remove it from the graph.
4146 DAG.DeleteNode(N);
4147
4148 // Replace the uses of Ptr with uses of the updated base value.
4149 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004150 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004151 removeFromWorkList(Ptr.Val);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004152 DAG.DeleteNode(Ptr.Val);
4153
4154 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004155}
4156
4157/// CombineToPostIndexedLoadStore - Try combine a load / store with a
4158/// add / sub of the base pointer node into a post-indexed load / store.
4159/// The transformation folded the add / subtract into the new indexed
4160/// load / store effectively and all of its uses are redirected to the
4161/// new load / store.
4162bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
4163 if (!AfterLegalize)
4164 return false;
4165
4166 bool isLoad = true;
4167 SDOperand Ptr;
4168 MVT::ValueType VT;
4169 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004170 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004171 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004172 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004173 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4174 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4175 return false;
4176 Ptr = LD->getBasePtr();
4177 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004178 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004179 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004180 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004181 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4182 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4183 return false;
4184 Ptr = ST->getBasePtr();
4185 isLoad = false;
4186 } else
4187 return false;
4188
Evan Chengcc470212006-11-16 00:08:20 +00004189 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004190 return false;
4191
4192 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4193 E = Ptr.Val->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004194 SDNode *Op = I->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004195 if (Op == N ||
4196 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4197 continue;
4198
4199 SDOperand BasePtr;
4200 SDOperand Offset;
4201 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4202 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4203 if (Ptr == Offset)
4204 std::swap(BasePtr, Offset);
4205 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004206 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004207 // Don't create a indexed load / store with zero offset.
4208 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004209 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004210 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004211
Chris Lattner9f1794e2006-11-11 00:56:29 +00004212 // Try turning it into a post-indexed load / store except when
4213 // 1) All uses are load / store ops that use it as base ptr.
4214 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4215 // nor a successor of N. Otherwise, if Op is folded that would
4216 // create a cycle.
4217
4218 // Check for #1.
4219 bool TryNext = false;
4220 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
4221 EE = BasePtr.Val->use_end(); II != EE; ++II) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004222 SDNode *Use = II->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004223 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00004224 continue;
4225
Chris Lattner9f1794e2006-11-11 00:56:29 +00004226 // If all the uses are load / store addresses, then don't do the
4227 // transformation.
4228 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4229 bool RealUse = false;
4230 for (SDNode::use_iterator III = Use->use_begin(),
4231 EEE = Use->use_end(); III != EEE; ++III) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004232 SDNode *UseUse = III->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004233 if (!((UseUse->getOpcode() == ISD::LOAD &&
4234 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004235 (UseUse->getOpcode() == ISD::STORE &&
4236 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004237 RealUse = true;
4238 }
Chris Lattner448f2192006-11-11 00:39:41 +00004239
Chris Lattner9f1794e2006-11-11 00:56:29 +00004240 if (!RealUse) {
4241 TryNext = true;
4242 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004243 }
4244 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004245 }
4246 if (TryNext)
4247 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004248
Chris Lattner9f1794e2006-11-11 00:56:29 +00004249 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004250 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Chris Lattner9f1794e2006-11-11 00:56:29 +00004251 SDOperand Result = isLoad
4252 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
4253 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4254 ++PostIndexedNodes;
4255 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004256 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004257 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4258 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004259 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004260 if (isLoad) {
4261 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004262 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004263 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004264 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004265 } else {
4266 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004267 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004268 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004269
Chris Lattner9f1794e2006-11-11 00:56:29 +00004270 // Finally, since the node is now dead, remove it from the graph.
4271 DAG.DeleteNode(N);
4272
4273 // Replace the uses of Use with uses of the updated base value.
4274 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
4275 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004276 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004277 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004278 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004279 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004280 }
4281 }
4282 }
4283 return false;
4284}
4285
Chris Lattner00161a62008-01-25 07:20:16 +00004286/// InferAlignment - If we can infer some alignment information from this
4287/// pointer, return it.
4288static unsigned InferAlignment(SDOperand Ptr, SelectionDAG &DAG) {
4289 // If this is a direct reference to a stack slot, use information about the
4290 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004291 int FrameIdx = 1 << 31;
4292 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004293 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004294 FrameIdx = FI->getIndex();
4295 } else if (Ptr.getOpcode() == ISD::ADD &&
4296 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4297 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4298 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4299 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004300 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004301
4302 if (FrameIdx != (1 << 31)) {
4303 // FIXME: Handle FI+CST.
4304 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4305 if (MFI.isFixedObjectIndex(FrameIdx)) {
4306 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx);
4307
4308 // The alignment of the frame index can be determined from its offset from
4309 // the incoming frame position. If the frame object is at offset 32 and
4310 // the stack is guaranteed to be 16-byte aligned, then we know that the
4311 // object is 16-byte aligned.
4312 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4313 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4314
4315 // Finally, the frame object itself may have a known alignment. Factor
4316 // the alignment + offset into a new alignment. For example, if we know
4317 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4318 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4319 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4320 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4321 FrameOffset);
4322 return std::max(Align, FIInfoAlign);
4323 }
4324 }
Chris Lattner00161a62008-01-25 07:20:16 +00004325
4326 return 0;
4327}
Chris Lattner448f2192006-11-11 00:39:41 +00004328
Chris Lattner01a22022005-10-10 22:04:48 +00004329SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004330 LoadSDNode *LD = cast<LoadSDNode>(N);
4331 SDOperand Chain = LD->getChain();
4332 SDOperand Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004333
4334 // Try to infer better alignment information than the load already has.
4335 if (LD->isUnindexed()) {
4336 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4337 if (Align > LD->getAlignment())
4338 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4339 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004340 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004341 LD->isVolatile(), Align);
4342 }
4343 }
4344
Evan Cheng45a7ca92007-05-01 00:38:21 +00004345
4346 // If load is not volatile and there are no uses of the loaded value (and
4347 // the updated indexed value in case of indexed loads), change uses of the
4348 // chain value into uses of the chain input (i.e. delete the dead load).
4349 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004350 if (N->getValueType(1) == MVT::Other) {
4351 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004352 if (N->hasNUsesOfValue(0, 0)) {
4353 // It's not safe to use the two value CombineTo variant here. e.g.
4354 // v1, chain2 = load chain1, loc
4355 // v2, chain3 = load chain2, loc
4356 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004357 // Now we replace use of chain2 with chain1. This makes the second load
4358 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004359 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004360 DOUT << "\nWith chain: "; DEBUG(Chain.Val->dump(&DAG));
4361 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004362 WorkListRemover DeadNodes(*this);
4363 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004364 if (N->use_empty()) {
4365 removeFromWorkList(N);
4366 DAG.DeleteNode(N);
4367 }
Evan Cheng02c42852008-01-16 23:11:54 +00004368 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
4369 }
Evan Cheng498f5592007-05-01 08:53:39 +00004370 } else {
4371 // Indexed loads.
4372 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4373 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Evan Cheng02c42852008-01-16 23:11:54 +00004374 SDOperand Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
4375 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
4376 DOUT << "\nWith: "; DEBUG(Undef.Val->dump(&DAG));
4377 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004378 WorkListRemover DeadNodes(*this);
4379 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Undef, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004380 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004381 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004382 &DeadNodes);
4383 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004384 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004385 DAG.DeleteNode(N);
4386 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004387 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004388 }
4389 }
Chris Lattner01a22022005-10-10 22:04:48 +00004390
4391 // If this load is directly stored, replace the load value with the stored
4392 // value.
4393 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004394 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00004395 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4396 !LD->isVolatile()) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004397 if (ISD::isNON_TRUNCStore(Chain.Val)) {
4398 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4399 if (PrevST->getBasePtr() == Ptr &&
4400 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004401 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004402 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004403 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004404
Jim Laskey7ca56af2006-10-11 13:47:09 +00004405 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004406 // Walk up chain skipping non-aliasing memory nodes.
4407 SDOperand BetterChain = FindBetterChain(N, Chain);
4408
Jim Laskey6ff23e52006-10-04 16:53:27 +00004409 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004410 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004411 SDOperand ReplLoad;
4412
Jim Laskey279f0532006-09-25 16:29:54 +00004413 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004414 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4415 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004416 LD->getSrcValue(), LD->getSrcValueOffset(),
4417 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004418 } else {
4419 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4420 LD->getValueType(0),
4421 BetterChain, Ptr, LD->getSrcValue(),
4422 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004423 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004424 LD->isVolatile(),
4425 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004426 }
Jim Laskey279f0532006-09-25 16:29:54 +00004427
Jim Laskey6ff23e52006-10-04 16:53:27 +00004428 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00004429 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
4430 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004431
Jim Laskey274062c2006-10-13 23:32:28 +00004432 // Replace uses with load result and token factor. Don't add users
4433 // to work list.
4434 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004435 }
4436 }
4437
Evan Cheng7fc033a2006-11-03 03:06:21 +00004438 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004439 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00004440 return SDOperand(N, 0);
4441
Chris Lattner01a22022005-10-10 22:04:48 +00004442 return SDOperand();
4443}
4444
Chris Lattner07649d92008-01-08 23:08:06 +00004445
Chris Lattner87514ca2005-10-10 22:31:19 +00004446SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004447 StoreSDNode *ST = cast<StoreSDNode>(N);
4448 SDOperand Chain = ST->getChain();
4449 SDOperand Value = ST->getValue();
4450 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004451
Chris Lattner00161a62008-01-25 07:20:16 +00004452 // Try to infer better alignment information than the store already has.
4453 if (ST->isUnindexed()) {
4454 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4455 if (Align > ST->getAlignment())
4456 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004457 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004458 ST->isVolatile(), Align);
4459 }
4460 }
4461
Evan Cheng59d5b682007-05-07 21:27:48 +00004462 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004463 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004464 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004465 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004466 unsigned Align = ST->getAlignment();
4467 MVT::ValueType SVT = Value.getOperand(0).getValueType();
4468 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00004469 getABITypeAlignment(MVT::getTypeForValueType(SVT));
Evan Chengc2cd2b22007-05-07 21:36:06 +00004470 if (Align <= OrigAlign && TLI.isOperationLegal(ISD::STORE, SVT))
Evan Cheng59d5b682007-05-07 21:27:48 +00004471 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004472 ST->getSrcValueOffset(), ST->isVolatile(), Align);
Jim Laskey279f0532006-09-25 16:29:54 +00004473 }
4474
Nate Begeman2cbba892006-12-11 02:23:46 +00004475 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004476 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Evan Cheng25ece662006-12-11 17:25:19 +00004477 if (Value.getOpcode() != ISD::TargetConstantFP) {
4478 SDOperand Tmp;
Chris Lattner62be1a72006-12-12 04:16:14 +00004479 switch (CFP->getValueType(0)) {
4480 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004481 case MVT::f80: // We don't do this for these yet.
4482 case MVT::f128:
4483 case MVT::ppcf128:
4484 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004485 case MVT::f32:
4486 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004487 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
4488 convertToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004489 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004490 ST->getSrcValueOffset(), ST->isVolatile(),
4491 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004492 }
4493 break;
4494 case MVT::f64:
4495 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004496 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
4497 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004498 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004499 ST->getSrcValueOffset(), ST->isVolatile(),
4500 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004501 } else if (TLI.isTypeLegal(MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004502 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004503 // argument passing. Since this is so common, custom legalize the
4504 // 64-bit integer store into two 32-bit stores.
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004505 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
Chris Lattner62be1a72006-12-12 04:16:14 +00004506 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4507 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004508 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004509
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004510 int SVOffset = ST->getSrcValueOffset();
4511 unsigned Alignment = ST->getAlignment();
4512 bool isVolatile = ST->isVolatile();
4513
Chris Lattner62be1a72006-12-12 04:16:14 +00004514 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004515 ST->getSrcValueOffset(),
4516 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004517 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4518 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004519 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004520 Alignment = MinAlign(Alignment, 4U);
Chris Lattner62be1a72006-12-12 04:16:14 +00004521 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004522 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004523 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4524 }
4525 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004526 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004527 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004528 }
4529
Jim Laskey279f0532006-09-25 16:29:54 +00004530 if (CombinerAA) {
4531 // Walk up chain skipping non-aliasing memory nodes.
4532 SDOperand BetterChain = FindBetterChain(N, Chain);
4533
Jim Laskey6ff23e52006-10-04 16:53:27 +00004534 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004535 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004536 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004537 SDOperand ReplStore;
4538 if (ST->isTruncatingStore()) {
4539 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004540 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004541 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004542 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004543 } else {
4544 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004545 ST->getSrcValue(), ST->getSrcValueOffset(),
4546 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004547 }
4548
Jim Laskey279f0532006-09-25 16:29:54 +00004549 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00004550 SDOperand Token =
4551 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4552
4553 // Don't add users to work list.
4554 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004555 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004556 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004557
Evan Cheng33dbedc2006-11-05 09:31:14 +00004558 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004559 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00004560 return SDOperand(N, 0);
4561
Chris Lattner3c872852007-12-29 06:26:16 +00004562 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004563 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Chris Lattner2b4c2792007-10-13 06:35:54 +00004564 MVT::isInteger(Value.getValueType())) {
4565 // See if we can simplify the input to this truncstore with knowledge that
4566 // only the low bits are being used. For example:
4567 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
4568 SDOperand Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004569 GetDemandedBits(Value,
4570 APInt::getLowBitsSet(Value.getValueSizeInBits(),
4571 MVT::getSizeInBits(ST->getMemoryVT())));
Chris Lattner2b4c2792007-10-13 06:35:54 +00004572 AddToWorkList(Value.Val);
4573 if (Shorter.Val)
4574 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004575 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004576 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004577
4578 // Otherwise, see if we can simplify the operation with
4579 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004580 if (SimplifyDemandedBits(Value,
4581 APInt::getLowBitsSet(
4582 Value.getValueSizeInBits(),
4583 MVT::getSizeInBits(ST->getMemoryVT()))))
Chris Lattnere33544c2007-10-13 06:58:48 +00004584 return SDOperand(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004585 }
4586
Chris Lattner3c872852007-12-29 06:26:16 +00004587 // If this is a load followed by a store to the same location, then the store
4588 // is dead/noop.
4589 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004590 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004591 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004592 // There can't be any side effects between the load and store, such as
4593 // a call or store.
Chris Lattner572dee72008-01-16 05:49:24 +00004594 Chain.reachesChainWithoutSideEffects(SDOperand(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004595 // The store is dead, remove it.
4596 return Chain;
4597 }
4598 }
4599
Chris Lattnerddf89562008-01-17 19:59:44 +00004600 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4601 // truncating store. We can do this even if this is already a truncstore.
4602 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
4603 && TLI.isTypeLegal(Value.getOperand(0).getValueType()) &&
4604 Value.Val->hasOneUse() && ST->isUnindexed() &&
4605 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004606 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004607 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004608 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004609 ST->isVolatile(), ST->getAlignment());
4610 }
4611
Chris Lattner87514ca2005-10-10 22:31:19 +00004612 return SDOperand();
4613}
4614
Chris Lattnerca242442006-03-19 01:27:56 +00004615SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4616 SDOperand InVec = N->getOperand(0);
4617 SDOperand InVal = N->getOperand(1);
4618 SDOperand EltNo = N->getOperand(2);
4619
4620 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4621 // vector with the inserted element.
4622 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
4623 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004624 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004625 if (Elt < Ops.size())
4626 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004627 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4628 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004629 }
4630
4631 return SDOperand();
4632}
4633
Evan Cheng513da432007-10-06 08:19:55 +00004634SDOperand DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
4635 SDOperand InVec = N->getOperand(0);
4636 SDOperand EltNo = N->getOperand(1);
4637
4638 // (vextract (v4f32 s2v (f32 load $addr)), 0) -> (f32 load $addr)
4639 // (vextract (v4i32 bc (v4f32 s2v (f32 load $addr))), 0) -> (i32 load $addr)
4640 if (isa<ConstantSDNode>(EltNo)) {
4641 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
4642 bool NewLoad = false;
4643 if (Elt == 0) {
4644 MVT::ValueType VT = InVec.getValueType();
4645 MVT::ValueType EVT = MVT::getVectorElementType(VT);
4646 MVT::ValueType LVT = EVT;
4647 unsigned NumElts = MVT::getVectorNumElements(VT);
4648 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
4649 MVT::ValueType BCVT = InVec.getOperand(0).getValueType();
Dan Gohman090b38a2007-10-29 20:44:42 +00004650 if (!MVT::isVector(BCVT) ||
4651 NumElts != MVT::getVectorNumElements(BCVT))
Evan Cheng513da432007-10-06 08:19:55 +00004652 return SDOperand();
4653 InVec = InVec.getOperand(0);
4654 EVT = MVT::getVectorElementType(BCVT);
4655 NewLoad = true;
4656 }
4657 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4658 InVec.getOperand(0).getValueType() == EVT &&
4659 ISD::isNormalLoad(InVec.getOperand(0).Val) &&
4660 InVec.getOperand(0).hasOneUse()) {
4661 LoadSDNode *LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4662 unsigned Align = LN0->getAlignment();
4663 if (NewLoad) {
4664 // Check the resultant load doesn't need a higher alignment than the
4665 // original load.
4666 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
4667 getABITypeAlignment(MVT::getTypeForValueType(LVT));
4668 if (!TLI.isOperationLegal(ISD::LOAD, LVT) || NewAlign > Align)
4669 return SDOperand();
4670 Align = NewAlign;
4671 }
4672
4673 return DAG.getLoad(LVT, LN0->getChain(), LN0->getBasePtr(),
4674 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4675 LN0->isVolatile(), Align);
4676 }
4677 }
4678 }
4679 return SDOperand();
4680}
4681
4682
Dan Gohman7f321562007-06-25 16:23:39 +00004683SDOperand DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
4684 unsigned NumInScalars = N->getNumOperands();
4685 MVT::ValueType VT = N->getValueType(0);
4686 unsigned NumElts = MVT::getVectorNumElements(VT);
4687 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattnerca242442006-03-19 01:27:56 +00004688
Dan Gohman7f321562007-06-25 16:23:39 +00004689 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4690 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4691 // at most two distinct vectors, turn this into a shuffle node.
Chris Lattnerd7648c82006-03-28 20:28:38 +00004692 SDOperand VecIn1, VecIn2;
4693 for (unsigned i = 0; i != NumInScalars; ++i) {
4694 // Ignore undef inputs.
4695 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4696
Dan Gohman7f321562007-06-25 16:23:39 +00004697 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004698 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004699 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004700 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
4701 VecIn1 = VecIn2 = SDOperand(0, 0);
4702 break;
4703 }
4704
Dan Gohman7f321562007-06-25 16:23:39 +00004705 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004706 // we can't make a shuffle.
4707 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004708 if (ExtractedFromVec.getValueType() != VT) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004709 VecIn1 = VecIn2 = SDOperand(0, 0);
4710 break;
4711 }
4712
4713 // Otherwise, remember this. We allow up to two distinct input vectors.
4714 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4715 continue;
4716
4717 if (VecIn1.Val == 0) {
4718 VecIn1 = ExtractedFromVec;
4719 } else if (VecIn2.Val == 0) {
4720 VecIn2 = ExtractedFromVec;
4721 } else {
4722 // Too many inputs.
4723 VecIn1 = VecIn2 = SDOperand(0, 0);
4724 break;
4725 }
4726 }
4727
4728 // If everything is good, we can make a shuffle operation.
4729 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004730 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004731 for (unsigned i = 0; i != NumInScalars; ++i) {
4732 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004733 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004734 continue;
4735 }
4736
4737 SDOperand Extract = N->getOperand(i);
4738
4739 // If extracting from the first vector, just use the index directly.
4740 if (Extract.getOperand(0) == VecIn1) {
4741 BuildVecIndices.push_back(Extract.getOperand(1));
4742 continue;
4743 }
4744
4745 // Otherwise, use InIdx + VecSize
4746 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004747 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004748 }
4749
4750 // Add count and size info.
Chris Lattner0bd48932008-01-17 07:00:52 +00004751 MVT::ValueType BuildVecVT = MVT::getVectorType(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004752
Dan Gohman7f321562007-06-25 16:23:39 +00004753 // Return the new VECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004754 SDOperand Ops[5];
4755 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00004756 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004757 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004758 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004759 // Use an undef build_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00004760 std::vector<SDOperand> UnOps(NumInScalars,
4761 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004762 EltType));
4763 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004764 &UnOps[0], UnOps.size());
4765 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00004766 }
Dan Gohman7f321562007-06-25 16:23:39 +00004767 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004768 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004769 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004770 }
4771
4772 return SDOperand();
4773}
4774
Dan Gohman7f321562007-06-25 16:23:39 +00004775SDOperand DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
4776 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
4777 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
4778 // inputs come from at most two distinct vectors, turn this into a shuffle
4779 // node.
4780
4781 // If we only have one input vector, we don't need to do any concatenation.
4782 if (N->getNumOperands() == 1) {
4783 return N->getOperand(0);
4784 }
4785
4786 return SDOperand();
4787}
4788
Chris Lattner66445d32006-03-28 22:11:53 +00004789SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004790 SDOperand ShufMask = N->getOperand(2);
4791 unsigned NumElts = ShufMask.getNumOperands();
4792
4793 // If the shuffle mask is an identity operation on the LHS, return the LHS.
4794 bool isIdentity = true;
4795 for (unsigned i = 0; i != NumElts; ++i) {
4796 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4797 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
4798 isIdentity = false;
4799 break;
4800 }
4801 }
4802 if (isIdentity) return N->getOperand(0);
4803
4804 // If the shuffle mask is an identity operation on the RHS, return the RHS.
4805 isIdentity = true;
4806 for (unsigned i = 0; i != NumElts; ++i) {
4807 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4808 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
4809 isIdentity = false;
4810 break;
4811 }
4812 }
4813 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004814
4815 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
4816 // needed at all.
4817 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00004818 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004819 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00004820 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004821 for (unsigned i = 0; i != NumElts; ++i)
4822 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
4823 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
4824 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00004825 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004826 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004827 BaseIdx = Idx;
4828 } else {
4829 if (BaseIdx != Idx)
4830 isSplat = false;
4831 if (VecNum != V) {
4832 isUnary = false;
4833 break;
4834 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004835 }
4836 }
4837
4838 SDOperand N0 = N->getOperand(0);
4839 SDOperand N1 = N->getOperand(1);
4840 // Normalize unary shuffle so the RHS is undef.
4841 if (isUnary && VecNum == 1)
4842 std::swap(N0, N1);
4843
Evan Cheng917ec982006-07-21 08:25:53 +00004844 // If it is a splat, check if the argument vector is a build_vector with
4845 // all scalar elements the same.
4846 if (isSplat) {
4847 SDNode *V = N0.Val;
Evan Cheng917ec982006-07-21 08:25:53 +00004848
Dan Gohman7f321562007-06-25 16:23:39 +00004849 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004850 // not the number of vector elements, look through it. Be careful not to
4851 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004852 if (V->getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng59569222006-10-16 22:49:37 +00004853 SDOperand ConvInput = V->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004854 if (MVT::getVectorNumElements(ConvInput.getValueType()) == NumElts)
Evan Cheng59569222006-10-16 22:49:37 +00004855 V = ConvInput.Val;
4856 }
4857
Dan Gohman7f321562007-06-25 16:23:39 +00004858 if (V->getOpcode() == ISD::BUILD_VECTOR) {
4859 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00004860 if (NumElems > BaseIdx) {
4861 SDOperand Base;
4862 bool AllSame = true;
4863 for (unsigned i = 0; i != NumElems; ++i) {
4864 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4865 Base = V->getOperand(i);
4866 break;
4867 }
4868 }
4869 // Splat of <u, u, u, u>, return <u, u, u, u>
4870 if (!Base.Val)
4871 return N0;
4872 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00004873 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00004874 AllSame = false;
4875 break;
4876 }
4877 }
4878 // Splat of <x, x, x, x>, return <x, x, x, x>
4879 if (AllSame)
4880 return N0;
4881 }
4882 }
4883 }
4884
Evan Chenge7bec0d2006-07-20 22:44:41 +00004885 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4886 // into an undef.
4887 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004888 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4889 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004890 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004891 for (unsigned i = 0; i != NumElts; ++i) {
4892 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4893 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4894 MappedOps.push_back(ShufMask.getOperand(i));
4895 } else {
4896 unsigned NewIdx =
4897 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
4898 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
4899 }
4900 }
Dan Gohman7f321562007-06-25 16:23:39 +00004901 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004902 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004903 AddToWorkList(ShufMask.Val);
Dan Gohman7f321562007-06-25 16:23:39 +00004904 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
4905 N0,
4906 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
4907 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00004908 }
Dan Gohman7f321562007-06-25 16:23:39 +00004909
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004910 return SDOperand();
4911}
4912
Evan Cheng44f1f092006-04-20 08:56:16 +00004913/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00004914/// an AND to a vector_shuffle with the destination vector and a zero vector.
4915/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00004916/// vector_shuffle V, Zero, <0, 4, 2, 4>
4917SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
4918 SDOperand LHS = N->getOperand(0);
4919 SDOperand RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00004920 if (N->getOpcode() == ISD::AND) {
4921 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00004922 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004923 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Evan Cheng44f1f092006-04-20 08:56:16 +00004924 std::vector<SDOperand> IdxOps;
4925 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00004926 unsigned NumElts = NumOps;
4927 MVT::ValueType EVT = MVT::getVectorElementType(RHS.getValueType());
Evan Cheng44f1f092006-04-20 08:56:16 +00004928 for (unsigned i = 0; i != NumElts; ++i) {
4929 SDOperand Elt = RHS.getOperand(i);
4930 if (!isa<ConstantSDNode>(Elt))
4931 return SDOperand();
4932 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
4933 IdxOps.push_back(DAG.getConstant(i, EVT));
4934 else if (cast<ConstantSDNode>(Elt)->isNullValue())
4935 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
4936 else
4937 return SDOperand();
4938 }
4939
4940 // Let's see if the target supports this vector_shuffle.
4941 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
4942 return SDOperand();
4943
Dan Gohman7f321562007-06-25 16:23:39 +00004944 // Return the new VECTOR_SHUFFLE node.
4945 MVT::ValueType VT = MVT::getVectorType(EVT, NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00004946 std::vector<SDOperand> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004947 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00004948 Ops.push_back(LHS);
4949 AddToWorkList(LHS.Val);
4950 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00004951 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004952 &ZeroOps[0], ZeroOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004953 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004954 &IdxOps[0], IdxOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004955 SDOperand Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004956 &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004957 if (VT != LHS.getValueType()) {
4958 Result = DAG.getNode(ISD::BIT_CONVERT, LHS.getValueType(), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00004959 }
4960 return Result;
4961 }
4962 }
4963 return SDOperand();
4964}
4965
Dan Gohman7f321562007-06-25 16:23:39 +00004966/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
4967SDOperand DAGCombiner::SimplifyVBinOp(SDNode *N) {
4968 // After legalize, the target may be depending on adds and other
4969 // binary ops to provide legal ways to construct constants or other
4970 // things. Simplifying them may result in a loss of legality.
4971 if (AfterLegalize) return SDOperand();
4972
4973 MVT::ValueType VT = N->getValueType(0);
Dan Gohman05d92fe2007-07-13 20:03:40 +00004974 assert(MVT::isVector(VT) && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00004975
4976 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattneredab1b92006-04-02 03:25:57 +00004977 SDOperand LHS = N->getOperand(0);
4978 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00004979 SDOperand Shuffle = XformToShuffleWithZero(N);
4980 if (Shuffle.Val) return Shuffle;
4981
Dan Gohman7f321562007-06-25 16:23:39 +00004982 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00004983 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00004984 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
4985 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004986 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004987 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Chris Lattneredab1b92006-04-02 03:25:57 +00004988 SDOperand LHSOp = LHS.getOperand(i);
4989 SDOperand RHSOp = RHS.getOperand(i);
4990 // If these two elements can't be folded, bail out.
4991 if ((LHSOp.getOpcode() != ISD::UNDEF &&
4992 LHSOp.getOpcode() != ISD::Constant &&
4993 LHSOp.getOpcode() != ISD::ConstantFP) ||
4994 (RHSOp.getOpcode() != ISD::UNDEF &&
4995 RHSOp.getOpcode() != ISD::Constant &&
4996 RHSOp.getOpcode() != ISD::ConstantFP))
4997 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00004998 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00004999 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5000 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00005001 if ((RHSOp.getOpcode() == ISD::Constant &&
5002 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
5003 (RHSOp.getOpcode() == ISD::ConstantFP &&
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00005004 cast<ConstantFPSDNode>(RHSOp.Val)->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00005005 break;
5006 }
Dan Gohman7f321562007-06-25 16:23:39 +00005007 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00005008 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00005009 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5010 Ops.back().getOpcode() == ISD::Constant ||
5011 Ops.back().getOpcode() == ISD::ConstantFP) &&
5012 "Scalar binop didn't fold!");
5013 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005014
Dan Gohman7f321562007-06-25 16:23:39 +00005015 if (Ops.size() == LHS.getNumOperands()) {
5016 MVT::ValueType VT = LHS.getValueType();
5017 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005018 }
Chris Lattneredab1b92006-04-02 03:25:57 +00005019 }
5020
5021 return SDOperand();
5022}
5023
Nate Begeman44728a72005-09-19 22:34:01 +00005024SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00005025 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5026
5027 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
5028 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5029 // If we got a simplified select_cc node back from SimplifySelectCC, then
5030 // break it down into a new SETCC node, and a new SELECT node, and then return
5031 // the SELECT node, since we were called with a SELECT node.
5032 if (SCC.Val) {
5033 // Check to see if we got a select_cc back (to turn into setcc/select).
5034 // Otherwise, just return whatever node we got back, like fabs.
5035 if (SCC.getOpcode() == ISD::SELECT_CC) {
5036 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
5037 SCC.getOperand(0), SCC.getOperand(1),
5038 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00005039 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005040 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5041 SCC.getOperand(3), SETCC);
5042 }
5043 return SCC;
5044 }
Nate Begeman44728a72005-09-19 22:34:01 +00005045 return SDOperand();
5046}
5047
Chris Lattner40c62d52005-10-18 06:04:22 +00005048/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5049/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00005050/// select. Callers of this should assume that TheSelect is deleted if this
5051/// returns true. As such, they should return the appropriate thing (e.g. the
5052/// node) back to the top-level of the DAG combiner loop to avoid it being
5053/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00005054///
5055bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
5056 SDOperand RHS) {
5057
5058 // If this is a select from two identical things, try to pull the operation
5059 // through the select.
5060 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00005061 // If this is a load and the token chain is identical, replace the select
5062 // of two loads with a load through a select of the address to load from.
5063 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5064 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005065 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005066 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005067 LHS.getOperand(0) == RHS.getOperand(0)) {
5068 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5069 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5070
5071 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005072 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005073 // FIXME: this conflates two src values, discarding one. This is not
5074 // the right thing to do, but nothing uses srcvalues now. When they do,
5075 // turn SrcValue into a list of locations.
5076 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005077 if (TheSelect->getOpcode() == ISD::SELECT) {
5078 // Check that the condition doesn't reach either load. If so, folding
5079 // this will induce a cycle into the DAG.
Evan Cheng917be682008-03-04 00:41:45 +00005080 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5081 !RLD->isPredecessorOf(TheSelect->getOperand(0).Val)) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005082 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5083 TheSelect->getOperand(0), LLD->getBasePtr(),
5084 RLD->getBasePtr());
5085 }
5086 } else {
5087 // Check that the condition doesn't reach either load. If so, folding
5088 // this will induce a cycle into the DAG.
Evan Cheng917be682008-03-04 00:41:45 +00005089 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5090 !RLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5091 !LLD->isPredecessorOf(TheSelect->getOperand(1).Val) &&
5092 !RLD->isPredecessorOf(TheSelect->getOperand(1).Val)) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005093 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005094 TheSelect->getOperand(0),
5095 TheSelect->getOperand(1),
5096 LLD->getBasePtr(), RLD->getBasePtr(),
5097 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005098 }
Evan Cheng466685d2006-10-09 20:57:25 +00005099 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005100
5101 if (Addr.Val) {
5102 SDOperand Load;
5103 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5104 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5105 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005106 LLD->getSrcValueOffset(),
5107 LLD->isVolatile(),
5108 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005109 else {
5110 Load = DAG.getExtLoad(LLD->getExtensionType(),
5111 TheSelect->getValueType(0),
5112 LLD->getChain(), Addr, LLD->getSrcValue(),
5113 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005114 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005115 LLD->isVolatile(),
5116 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005117 }
5118 // Users of the select now use the result of the load.
5119 CombineTo(TheSelect, Load);
5120
5121 // Users of the old loads now use the new load's chain. We know the
5122 // old-load value is dead now.
5123 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
5124 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
5125 return true;
5126 }
Evan Chengc5484282006-10-04 00:56:09 +00005127 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005128 }
5129 }
5130
5131 return false;
5132}
5133
Nate Begeman44728a72005-09-19 22:34:01 +00005134SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
5135 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00005136 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005137
5138 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00005139 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
5140 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
5141 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
5142
5143 // Determine if the condition we're dealing with is constant
Scott Michel5b8f82e2008-03-10 15:42:14 +00005144 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00005145 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005146 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
5147
5148 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00005149 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005150 return N2;
5151 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00005152 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005153 return N3;
5154
5155 // Check to see if we can simplify the select into an fabs node
5156 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5157 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005158 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005159 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5160 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5161 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5162 N2 == N3.getOperand(0))
5163 return DAG.getNode(ISD::FABS, VT, N0);
5164
5165 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5166 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5167 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5168 N2.getOperand(0) == N3)
5169 return DAG.getNode(ISD::FABS, VT, N3);
5170 }
5171 }
5172
5173 // Check to see if we can perform the "gzip trick", transforming
5174 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005175 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00005176 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00005177 MVT::isInteger(N2.getValueType()) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005178 (N1C->isNullValue() || // (a < 0) ? b : 0
5179 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00005180 MVT::ValueType XType = N0.getValueType();
5181 MVT::ValueType AType = N2.getValueType();
5182 if (XType >= AType) {
5183 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005184 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00005185 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5186 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Nate Begemanf845b452005-10-08 00:29:44 +00005187 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
5188 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5189 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00005190 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005191 if (XType > AType) {
5192 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005193 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005194 }
5195 return DAG.getNode(ISD::AND, AType, Shift, N2);
5196 }
5197 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5198 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5199 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00005200 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005201 if (XType > AType) {
5202 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005203 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005204 }
5205 return DAG.getNode(ISD::AND, AType, Shift, N2);
5206 }
5207 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005208
5209 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00005210 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Nate Begeman07ed4172005-10-10 21:26:48 +00005211 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005212
5213 // If the caller doesn't want us to simplify this into a zext of a compare,
5214 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00005215 if (NotExtCompare && N2C->getAPIntValue() == 1)
Chris Lattner1eba01e2007-04-11 06:50:51 +00005216 return SDOperand();
5217
Nate Begeman07ed4172005-10-10 21:26:48 +00005218 // Get a SetCC of the condition
5219 // FIXME: Should probably make sure that setcc is legal if we ever have a
5220 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00005221 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005222 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00005223 if (AfterLegalize) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005224 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Chris Lattner555d8d62006-12-07 22:36:47 +00005225 if (N2.getValueType() < SCC.getValueType())
5226 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5227 else
5228 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005229 } else {
5230 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005231 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005232 }
Chris Lattner5750df92006-03-01 04:03:14 +00005233 AddToWorkList(SCC.Val);
5234 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005235
Dan Gohman002e5d02008-03-13 22:13:53 +00005236 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005237 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005238 // shl setcc result by log2 n2c
5239 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00005240 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Nate Begeman07ed4172005-10-10 21:26:48 +00005241 TLI.getShiftAmountTy()));
5242 }
5243
Nate Begemanf845b452005-10-08 00:29:44 +00005244 // Check to see if this is the equivalent of setcc
5245 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5246 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00005247 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005248 MVT::ValueType XType = N0.getValueType();
Scott Michel5b8f82e2008-03-10 15:42:14 +00005249 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(N0))) {
5250 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00005251 if (Res.getValueType() != VT)
5252 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5253 return Res;
5254 }
5255
5256 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5257 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
5258 TLI.isOperationLegal(ISD::CTLZ, XType)) {
5259 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
5260 return DAG.getNode(ISD::SRL, XType, Ctlz,
5261 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
5262 TLI.getShiftAmountTy()));
5263 }
5264 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5265 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
5266 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
5267 N0);
5268 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
5269 DAG.getConstant(~0ULL, XType));
5270 return DAG.getNode(ISD::SRL, XType,
5271 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
5272 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5273 TLI.getShiftAmountTy()));
5274 }
5275 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5276 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
5277 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
5278 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5279 TLI.getShiftAmountTy()));
5280 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5281 }
5282 }
5283
5284 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5285 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5286 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005287 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
5288 N2.getOperand(0) == N1 && MVT::isInteger(N0.getValueType())) {
5289 MVT::ValueType XType = N0.getValueType();
5290 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5291 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5292 TLI.getShiftAmountTy()));
5293 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
5294 AddToWorkList(Shift.Val);
5295 AddToWorkList(Add.Val);
5296 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5297 }
5298 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5299 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5300 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5301 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5302 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Nate Begemanf845b452005-10-08 00:29:44 +00005303 MVT::ValueType XType = N0.getValueType();
5304 if (SubC->isNullValue() && MVT::isInteger(XType)) {
5305 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5306 DAG.getConstant(MVT::getSizeInBits(XType)-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005307 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00005308 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005309 AddToWorkList(Shift.Val);
5310 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005311 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5312 }
5313 }
5314 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005315
Nate Begeman44728a72005-09-19 22:34:01 +00005316 return SDOperand();
5317}
5318
Evan Chengfa1eb272007-02-08 22:13:59 +00005319/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Nate Begeman452d7be2005-09-16 00:54:12 +00005320SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00005321 SDOperand N1, ISD::CondCode Cond,
5322 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005323 TargetLowering::DAGCombinerInfo
5324 DagCombineInfo(DAG, !AfterLegalize, false, this);
5325 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005326}
5327
Nate Begeman69575232005-10-20 02:15:44 +00005328/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5329/// return a DAG expression to select that will generate the same value by
5330/// multiplying by a magic number. See:
5331/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5332SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005333 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005334 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
5335
Andrew Lenharth232c9102006-06-12 16:07:18 +00005336 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005337 ii != ee; ++ii)
5338 AddToWorkList(*ii);
5339 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005340}
5341
5342/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5343/// return a DAG expression to select that will generate the same value by
5344/// multiplying by a magic number. See:
5345/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5346SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005347 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005348 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005349
Andrew Lenharth232c9102006-06-12 16:07:18 +00005350 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005351 ii != ee; ++ii)
5352 AddToWorkList(*ii);
5353 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005354}
5355
Jim Laskey71382342006-10-07 23:37:56 +00005356/// FindBaseOffset - Return true if base is known not to alias with anything
5357/// but itself. Provides base object and offset as results.
5358static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
5359 // Assume it is a primitive operation.
5360 Base = Ptr; Offset = 0;
5361
5362 // If it's an adding a simple constant then integrate the offset.
5363 if (Base.getOpcode() == ISD::ADD) {
5364 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5365 Base = Base.getOperand(0);
5366 Offset += C->getValue();
5367 }
5368 }
5369
5370 // If it's any of the following then it can't alias with anything but itself.
5371 return isa<FrameIndexSDNode>(Base) ||
5372 isa<ConstantPoolSDNode>(Base) ||
5373 isa<GlobalAddressSDNode>(Base);
5374}
5375
5376/// isAlias - Return true if there is any possibility that the two addresses
5377/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00005378bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
5379 const Value *SrcValue1, int SrcValueOffset1,
5380 SDOperand Ptr2, int64_t Size2,
5381 const Value *SrcValue2, int SrcValueOffset2)
5382{
Jim Laskey71382342006-10-07 23:37:56 +00005383 // If they are the same then they must be aliases.
5384 if (Ptr1 == Ptr2) return true;
5385
5386 // Gather base node and offset information.
5387 SDOperand Base1, Base2;
5388 int64_t Offset1, Offset2;
5389 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5390 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5391
5392 // If they have a same base address then...
5393 if (Base1 == Base2) {
5394 // Check to see if the addresses overlap.
5395 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5396 }
5397
Jim Laskey096c22e2006-10-18 12:29:57 +00005398 // If we know both bases then they can't alias.
5399 if (KnownBase1 && KnownBase2) return false;
5400
Jim Laskey07a27092006-10-18 19:08:31 +00005401 if (CombinerGlobalAA) {
5402 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005403 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5404 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5405 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005406 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005407 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005408 if (AAResult == AliasAnalysis::NoAlias)
5409 return false;
5410 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005411
5412 // Otherwise we have to assume they alias.
5413 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005414}
5415
5416/// FindAliasInfo - Extracts the relevant alias information from the memory
5417/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005418bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00005419 SDOperand &Ptr, int64_t &Size,
5420 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005421 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5422 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005423 Size = MVT::getSizeInBits(LD->getMemoryVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005424 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005425 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005426 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005427 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005428 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005429 Size = MVT::getSizeInBits(ST->getMemoryVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005430 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005431 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005432 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005433 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005434 }
5435
5436 return false;
5437}
5438
Jim Laskey6ff23e52006-10-04 16:53:27 +00005439/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5440/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00005441void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005442 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005443 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005444 std::set<SDNode *> Visited; // Visited node set.
5445
Jim Laskey279f0532006-09-25 16:29:54 +00005446 // Get alias information for node.
5447 SDOperand Ptr;
5448 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005449 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005450 int SrcValueOffset;
5451 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005452
Jim Laskey6ff23e52006-10-04 16:53:27 +00005453 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005454 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005455
Jim Laskeybc588b82006-10-05 15:07:25 +00005456 // Look at each chain and determine if it is an alias. If so, add it to the
5457 // aliases list. If not, then continue up the chain looking for the next
5458 // candidate.
5459 while (!Chains.empty()) {
5460 SDOperand Chain = Chains.back();
5461 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005462
Jim Laskeybc588b82006-10-05 15:07:25 +00005463 // Don't bother if we've been before.
5464 if (Visited.find(Chain.Val) != Visited.end()) continue;
5465 Visited.insert(Chain.Val);
5466
5467 switch (Chain.getOpcode()) {
5468 case ISD::EntryToken:
5469 // Entry token is ideal chain operand, but handled in FindBetterChain.
5470 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005471
Jim Laskeybc588b82006-10-05 15:07:25 +00005472 case ISD::LOAD:
5473 case ISD::STORE: {
5474 // Get alias information for Chain.
5475 SDOperand OpPtr;
5476 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005477 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005478 int OpSrcValueOffset;
5479 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
5480 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005481
5482 // If chain is alias then stop here.
5483 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005484 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5485 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005486 Aliases.push_back(Chain);
5487 } else {
5488 // Look further up the chain.
5489 Chains.push_back(Chain.getOperand(0));
5490 // Clean up old chain.
5491 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00005492 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005493 break;
5494 }
5495
5496 case ISD::TokenFactor:
5497 // We have to check each of the operands of the token factor, so we queue
5498 // then up. Adding the operands to the queue (stack) in reverse order
5499 // maintains the original order and increases the likelihood that getNode
5500 // will find a matching token factor (CSE.)
5501 for (unsigned n = Chain.getNumOperands(); n;)
5502 Chains.push_back(Chain.getOperand(--n));
5503 // Eliminate the token factor if we can.
5504 AddToWorkList(Chain.Val);
5505 break;
5506
5507 default:
5508 // For all other instructions we will just have to take what we can get.
5509 Aliases.push_back(Chain);
5510 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005511 }
5512 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005513}
5514
5515/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5516/// for a better chain (aliasing node.)
5517SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
5518 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005519
Jim Laskey6ff23e52006-10-04 16:53:27 +00005520 // Accumulate all the aliases to this node.
5521 GatherAllAliases(N, OldChain, Aliases);
5522
5523 if (Aliases.size() == 0) {
5524 // If no operands then chain to entry token.
5525 return DAG.getEntryNode();
5526 } else if (Aliases.size() == 1) {
5527 // If a single operand then chain to it. We don't need to revisit it.
5528 return Aliases[0];
5529 }
5530
5531 // Construct a custom tailored token factor.
5532 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5533 &Aliases[0], Aliases.size());
5534
5535 // Make sure the old chain gets cleaned up.
5536 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
5537
5538 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005539}
5540
Nate Begeman1d4d4142005-09-01 00:19:25 +00005541// SelectionDAG::Combine - This is the entry point for the file.
5542//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005543void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00005544 if (!RunningAfterLegalize && ViewDAGCombine1)
5545 viewGraph();
5546 if (RunningAfterLegalize && ViewDAGCombine2)
5547 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005548 /// run - This is the main entry point to this class.
5549 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005550 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005551}