blob: cd57d3ad3697d3808e1d631f10654022bea34b04 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begeman1d4d4142005-09-01 00:19:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
Nate Begeman1d4d4142005-09-01 00:19:25 +000012//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "dagcombine"
Nate Begeman1d4d4142005-09-01 00:19:25 +000016#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner00161a62008-01-25 07:20:16 +000017#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000019#include "llvm/Analysis/AliasAnalysis.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000020#include "llvm/Target/TargetData.h"
Chris Lattner1329cb82008-01-26 19:45:50 +000021#include "llvm/Target/TargetFrameInfo.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000022#include "llvm/Target/TargetLowering.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000024#include "llvm/Target/TargetOptions.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000025#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/Statistic.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000028#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000031#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000032using namespace llvm;
33
Chris Lattnercd3245a2006-12-19 22:41:21 +000034STATISTIC(NodesCombined , "Number of dag nodes combined");
35STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
36STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
37
Nate Begeman1d4d4142005-09-01 00:19:25 +000038namespace {
Chris Lattner938ab022007-01-16 04:55:25 +000039#ifndef NDEBUG
40 static cl::opt<bool>
41 ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden,
42 cl::desc("Pop up a window to show dags before the first "
43 "dag combine pass"));
44 static cl::opt<bool>
45 ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden,
46 cl::desc("Pop up a window to show dags before the second "
47 "dag combine pass"));
48#else
49 static const bool ViewDAGCombine1 = false;
50 static const bool ViewDAGCombine2 = false;
51#endif
52
Jim Laskey71382342006-10-07 23:37:56 +000053 static cl::opt<bool>
54 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000055 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000056
Jim Laskey07a27092006-10-18 19:08:31 +000057 static cl::opt<bool>
58 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
59 cl::desc("Include global information in alias analysis"));
60
Jim Laskeybc588b82006-10-05 15:07:25 +000061//------------------------------ DAGCombiner ---------------------------------//
62
Jim Laskey71382342006-10-07 23:37:56 +000063 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000064 SelectionDAG &DAG;
65 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000066 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000067
68 // Worklist of all of the nodes that need to be simplified.
69 std::vector<SDNode*> WorkList;
70
Jim Laskeyc7c3f112006-10-16 20:52:31 +000071 // AA - Used for DAG load/store alias analysis.
72 AliasAnalysis &AA;
73
Nate Begeman1d4d4142005-09-01 00:19:25 +000074 /// AddUsersToWorkList - When an instruction is simplified, add all users of
75 /// the instruction to the work lists because they might get more simplified
76 /// now.
77 ///
78 void AddUsersToWorkList(SDNode *N) {
79 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000080 UI != UE; ++UI)
Jim Laskey6ff23e52006-10-04 16:53:27 +000081 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000082 }
83
Dan Gohman389079b2007-10-08 17:57:15 +000084 /// visit - call the node-specific routine that knows how to fold each
85 /// particular type of node.
86 SDOperand visit(SDNode *N);
87
Chris Lattner24664722006-03-01 04:53:38 +000088 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000089 /// AddToWorkList - Add to the work list making sure it's instance is at the
90 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000091 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000092 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000093 WorkList.push_back(N);
94 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000095
Chris Lattnerf8dc0612008-02-03 06:49:24 +000096 /// removeFromWorkList - remove all instances of N from the worklist.
97 ///
98 void removeFromWorkList(SDNode *N) {
99 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
100 WorkList.end());
Chris Lattner01a22022005-10-10 22:04:48 +0000101 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000102
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000103 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
104 bool AddTo = true);
105
Jim Laskey274062c2006-10-13 23:32:28 +0000106 SDOperand CombineTo(SDNode *N, SDOperand Res, bool AddTo = true) {
107 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000108 }
109
Jim Laskey274062c2006-10-13 23:32:28 +0000110 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1,
111 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000112 SDOperand To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000113 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000114 }
Chris Lattner0bd48932008-01-17 07:00:52 +0000115
Chris Lattner24664722006-03-01 04:53:38 +0000116 private:
117
Chris Lattner012f2412006-02-17 21:58:01 +0000118 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000119 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000120 /// propagation. If so, return true.
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000121 bool SimplifyDemandedBits(SDOperand Op, uint64_t Demanded = ~0ULL);
Chris Lattner87514ca2005-10-10 22:31:19 +0000122
Chris Lattner448f2192006-11-11 00:39:41 +0000123 bool CombineToPreIndexedLoadStore(SDNode *N);
124 bool CombineToPostIndexedLoadStore(SDNode *N);
125
126
Dan Gohman389079b2007-10-08 17:57:15 +0000127 /// combine - call the node-specific routine that knows how to fold each
128 /// particular type of node. If that doesn't do anything, try the
129 /// target-specific DAG combines.
130 SDOperand combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000131
132 // Visitation implementation - Implement dag node combining for different
133 // node types. The semantics are as follows:
134 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000135 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000136 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000137 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000138 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000139 SDOperand visitTokenFactor(SDNode *N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000140 SDOperand visitMERGE_VALUES(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000141 SDOperand visitADD(SDNode *N);
142 SDOperand visitSUB(SDNode *N);
Chris Lattner91153682007-03-04 20:03:15 +0000143 SDOperand visitADDC(SDNode *N);
144 SDOperand visitADDE(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000145 SDOperand visitMUL(SDNode *N);
146 SDOperand visitSDIV(SDNode *N);
147 SDOperand visitUDIV(SDNode *N);
148 SDOperand visitSREM(SDNode *N);
149 SDOperand visitUREM(SDNode *N);
150 SDOperand visitMULHU(SDNode *N);
151 SDOperand visitMULHS(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +0000152 SDOperand visitSMUL_LOHI(SDNode *N);
153 SDOperand visitUMUL_LOHI(SDNode *N);
154 SDOperand visitSDIVREM(SDNode *N);
155 SDOperand visitUDIVREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000156 SDOperand visitAND(SDNode *N);
157 SDOperand visitOR(SDNode *N);
158 SDOperand visitXOR(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000159 SDOperand SimplifyVBinOp(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000160 SDOperand visitSHL(SDNode *N);
161 SDOperand visitSRA(SDNode *N);
162 SDOperand visitSRL(SDNode *N);
163 SDOperand visitCTLZ(SDNode *N);
164 SDOperand visitCTTZ(SDNode *N);
165 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000166 SDOperand visitSELECT(SDNode *N);
167 SDOperand visitSELECT_CC(SDNode *N);
168 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000169 SDOperand visitSIGN_EXTEND(SDNode *N);
170 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000171 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000172 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
173 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000174 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000175 SDOperand visitFADD(SDNode *N);
176 SDOperand visitFSUB(SDNode *N);
177 SDOperand visitFMUL(SDNode *N);
178 SDOperand visitFDIV(SDNode *N);
179 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000180 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000181 SDOperand visitSINT_TO_FP(SDNode *N);
182 SDOperand visitUINT_TO_FP(SDNode *N);
183 SDOperand visitFP_TO_SINT(SDNode *N);
184 SDOperand visitFP_TO_UINT(SDNode *N);
185 SDOperand visitFP_ROUND(SDNode *N);
186 SDOperand visitFP_ROUND_INREG(SDNode *N);
187 SDOperand visitFP_EXTEND(SDNode *N);
188 SDOperand visitFNEG(SDNode *N);
189 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000190 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000191 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000192 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000193 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000194 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
Evan Cheng513da432007-10-06 08:19:55 +0000195 SDOperand visitEXTRACT_VECTOR_ELT(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000196 SDOperand visitBUILD_VECTOR(SDNode *N);
197 SDOperand visitCONCAT_VECTORS(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000198 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000199
Evan Cheng44f1f092006-04-20 08:56:16 +0000200 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000201 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
202
Chris Lattnere70da202007-12-06 07:33:36 +0000203 SDOperand visitShiftByConstant(SDNode *N, unsigned Amt);
204
Chris Lattner40c62d52005-10-18 06:04:22 +0000205 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000206 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000207 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
208 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000209 SDOperand N3, ISD::CondCode CC,
210 bool NotExtCompare = false);
Nate Begeman452d7be2005-09-16 00:54:12 +0000211 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000212 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner5eee4272008-01-26 01:09:19 +0000213 SDOperand SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
214 unsigned HiOp);
Dan Gohman7f321562007-06-25 16:23:39 +0000215 SDOperand ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000216 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000217 SDOperand BuildUDIV(SDNode *N);
218 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Evan Chengc88138f2007-03-22 01:54:19 +0000219 SDOperand ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000220
Chris Lattner2b4c2792007-10-13 06:35:54 +0000221 SDOperand GetDemandedBits(SDOperand V, uint64_t Mask);
222
Jim Laskey6ff23e52006-10-04 16:53:27 +0000223 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
224 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000225 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000226 SmallVector<SDOperand, 8> &Aliases);
227
Jim Laskey096c22e2006-10-18 12:29:57 +0000228 /// isAlias - Return true if there is any possibility that the two addresses
229 /// overlap.
230 bool isAlias(SDOperand Ptr1, int64_t Size1,
231 const Value *SrcValue1, int SrcValueOffset1,
232 SDOperand Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000233 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000234
Jim Laskey7ca56af2006-10-11 13:47:09 +0000235 /// FindAliasInfo - Extracts the relevant alias information from the memory
236 /// node. Returns true if the operand was a load.
237 bool FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +0000238 SDOperand &Ptr, int64_t &Size,
239 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000240
Jim Laskey279f0532006-09-25 16:29:54 +0000241 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000242 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000243 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
244
Nate Begeman1d4d4142005-09-01 00:19:25 +0000245public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000246 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
247 : DAG(D),
248 TLI(D.getTargetLoweringInfo()),
249 AfterLegalize(false),
250 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000251
252 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000253 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000254 };
255}
256
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000257
258namespace {
259/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
260/// nodes from the worklist.
261class VISIBILITY_HIDDEN WorkListRemover :
262 public SelectionDAG::DAGUpdateListener {
263 DAGCombiner &DC;
264public:
265 WorkListRemover(DAGCombiner &dc) : DC(dc) {}
266
267 virtual void NodeDeleted(SDNode *N) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000268 DC.removeFromWorkList(N);
269 }
270
271 virtual void NodeUpdated(SDNode *N) {
272 // Ignore updates.
273 }
274};
275}
276
Chris Lattner24664722006-03-01 04:53:38 +0000277//===----------------------------------------------------------------------===//
278// TargetLowering::DAGCombinerInfo implementation
279//===----------------------------------------------------------------------===//
280
281void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
282 ((DAGCombiner*)DC)->AddToWorkList(N);
283}
284
285SDOperand TargetLowering::DAGCombinerInfo::
286CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000287 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000288}
289
290SDOperand TargetLowering::DAGCombinerInfo::
291CombineTo(SDNode *N, SDOperand Res) {
292 return ((DAGCombiner*)DC)->CombineTo(N, Res);
293}
294
295
296SDOperand TargetLowering::DAGCombinerInfo::
297CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
298 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
299}
300
301
Chris Lattner24664722006-03-01 04:53:38 +0000302//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000303// Helper Functions
304//===----------------------------------------------------------------------===//
305
306/// isNegatibleForFree - Return 1 if we can compute the negated form of the
307/// specified expression for the same cost as the expression itself, or 2 if we
308/// can compute the negated form more cheaply than the expression itself.
Chris Lattner501fee72007-05-23 07:35:22 +0000309static char isNegatibleForFree(SDOperand Op, unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000310 // No compile time optimizations on this type.
311 if (Op.getValueType() == MVT::ppcf128)
312 return 0;
313
Chris Lattner29446522007-05-14 22:04:50 +0000314 // fneg is removable even if it has multiple uses.
315 if (Op.getOpcode() == ISD::FNEG) return 2;
316
317 // Don't allow anything with multiple uses.
318 if (!Op.hasOneUse()) return 0;
319
Chris Lattner3adf9512007-05-25 02:19:06 +0000320 // Don't recurse exponentially.
321 if (Depth > 6) return 0;
322
Chris Lattner29446522007-05-14 22:04:50 +0000323 switch (Op.getOpcode()) {
324 default: return false;
325 case ISD::ConstantFP:
326 return 1;
327 case ISD::FADD:
328 // FIXME: determine better conditions for this xform.
329 if (!UnsafeFPMath) return 0;
330
331 // -(A+B) -> -A - B
Chris Lattner501fee72007-05-23 07:35:22 +0000332 if (char V = isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000333 return V;
334 // -(A+B) -> -B - A
Chris Lattner501fee72007-05-23 07:35:22 +0000335 return isNegatibleForFree(Op.getOperand(1), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000336 case ISD::FSUB:
337 // We can't turn -(A-B) into B-A when we honor signed zeros.
338 if (!UnsafeFPMath) return 0;
339
340 // -(A-B) -> B-A
341 return 1;
342
343 case ISD::FMUL:
344 case ISD::FDIV:
345 if (HonorSignDependentRoundingFPMath()) return 0;
346
347 // -(X*Y) -> (-X * Y) or (X*-Y)
Chris Lattner501fee72007-05-23 07:35:22 +0000348 if (char V = isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000349 return V;
350
Chris Lattner501fee72007-05-23 07:35:22 +0000351 return isNegatibleForFree(Op.getOperand(1), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000352
353 case ISD::FP_EXTEND:
354 case ISD::FP_ROUND:
355 case ISD::FSIN:
Chris Lattner501fee72007-05-23 07:35:22 +0000356 return isNegatibleForFree(Op.getOperand(0), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000357 }
358}
359
360/// GetNegatedExpression - If isNegatibleForFree returns true, this function
361/// returns the newly negated expression.
Chris Lattner3adf9512007-05-25 02:19:06 +0000362static SDOperand GetNegatedExpression(SDOperand Op, SelectionDAG &DAG,
363 unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000364 // fneg is removable even if it has multiple uses.
365 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
366
367 // Don't allow anything with multiple uses.
368 assert(Op.hasOneUse() && "Unknown reuse!");
369
Chris Lattner3adf9512007-05-25 02:19:06 +0000370 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000371 switch (Op.getOpcode()) {
372 default: assert(0 && "Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000373 case ISD::ConstantFP: {
374 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
375 V.changeSign();
376 return DAG.getConstantFP(V, Op.getValueType());
377 }
Chris Lattner29446522007-05-14 22:04:50 +0000378 case ISD::FADD:
379 // FIXME: determine better conditions for this xform.
380 assert(UnsafeFPMath);
381
382 // -(A+B) -> -A - B
Chris Lattner3adf9512007-05-25 02:19:06 +0000383 if (isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000384 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000385 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000386 Op.getOperand(1));
387 // -(A+B) -> -B - A
388 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000389 GetNegatedExpression(Op.getOperand(1), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000390 Op.getOperand(0));
391 case ISD::FSUB:
392 // We can't turn -(A-B) into B-A when we honor signed zeros.
393 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000394
395 // -(0-B) -> B
396 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000397 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000398 return Op.getOperand(1);
Chris Lattner29446522007-05-14 22:04:50 +0000399
400 // -(A-B) -> B-A
401 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
402 Op.getOperand(0));
403
404 case ISD::FMUL:
405 case ISD::FDIV:
406 assert(!HonorSignDependentRoundingFPMath());
407
408 // -(X*Y) -> -X * Y
Chris Lattner3adf9512007-05-25 02:19:06 +0000409 if (isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000410 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000411 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000412 Op.getOperand(1));
413
414 // -(X*Y) -> X * -Y
415 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
416 Op.getOperand(0),
Chris Lattner3adf9512007-05-25 02:19:06 +0000417 GetNegatedExpression(Op.getOperand(1), DAG, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000418
419 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000420 case ISD::FSIN:
421 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000422 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000423 case ISD::FP_ROUND:
424 return DAG.getNode(ISD::FP_ROUND, Op.getValueType(),
425 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1),
426 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000427 }
428}
Chris Lattner24664722006-03-01 04:53:38 +0000429
430
Nate Begeman4ebd8052005-09-01 23:24:04 +0000431// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
432// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000433// Also, set the incoming LHS, RHS, and CC references to the appropriate
434// nodes based on the type of node we are checking. This simplifies life a
435// bit for the callers.
436static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
437 SDOperand &CC) {
438 if (N.getOpcode() == ISD::SETCC) {
439 LHS = N.getOperand(0);
440 RHS = N.getOperand(1);
441 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000442 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000443 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000444 if (N.getOpcode() == ISD::SELECT_CC &&
445 N.getOperand(2).getOpcode() == ISD::Constant &&
446 N.getOperand(3).getOpcode() == ISD::Constant &&
447 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000448 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
449 LHS = N.getOperand(0);
450 RHS = N.getOperand(1);
451 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000452 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000453 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000454 return false;
455}
456
Nate Begeman99801192005-09-07 23:25:52 +0000457// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
458// one use. If this is true, it allows the users to invert the operation for
459// free when it is profitable to do so.
460static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000461 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000462 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000463 return true;
464 return false;
465}
466
Nate Begemancd4d58c2006-02-03 06:46:56 +0000467SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
468 MVT::ValueType VT = N0.getValueType();
469 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
470 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
471 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
472 if (isa<ConstantSDNode>(N1)) {
473 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000474 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000475 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
476 } else if (N0.hasOneUse()) {
477 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000478 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000479 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
480 }
481 }
482 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
483 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
484 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
485 if (isa<ConstantSDNode>(N0)) {
486 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000487 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000488 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
489 } else if (N1.hasOneUse()) {
490 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000491 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000492 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
493 }
494 }
495 return SDOperand();
496}
497
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000498SDOperand DAGCombiner::CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
499 bool AddTo) {
500 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
501 ++NodesCombined;
502 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
503 DOUT << "\nWith: "; DEBUG(To[0].Val->dump(&DAG));
504 DOUT << " and " << NumTo-1 << " other values\n";
505 WorkListRemover DeadNodes(*this);
506 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
507
508 if (AddTo) {
509 // Push the new nodes and any users onto the worklist
510 for (unsigned i = 0, e = NumTo; i != e; ++i) {
511 AddToWorkList(To[i].Val);
512 AddUsersToWorkList(To[i].Val);
513 }
514 }
515
516 // Nodes can be reintroduced into the worklist. Make sure we do not
517 // process a node that has been replaced.
518 removeFromWorkList(N);
519
520 // Finally, since the node is now dead, remove it from the graph.
521 DAG.DeleteNode(N);
522 return SDOperand(N, 0);
523}
524
525/// SimplifyDemandedBits - Check the specified integer node value to see if
526/// it can be simplified or if things it uses can be simplified by bit
527/// propagation. If so, return true.
528bool DAGCombiner::SimplifyDemandedBits(SDOperand Op, uint64_t Demanded) {
529 TargetLowering::TargetLoweringOpt TLO(DAG, AfterLegalize);
530 uint64_t KnownZero, KnownOne;
531 Demanded &= MVT::getIntVTBitMask(Op.getValueType());
532 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
533 return false;
534
535 // Revisit the node.
536 AddToWorkList(Op.Val);
537
538 // Replace the old value with the new one.
539 ++NodesCombined;
540 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.Val->dump(&DAG));
541 DOUT << "\nWith: "; DEBUG(TLO.New.Val->dump(&DAG));
542 DOUT << '\n';
543
544 // Replace all uses. If any nodes become isomorphic to other nodes and
545 // are deleted, make sure to remove them from our worklist.
546 WorkListRemover DeadNodes(*this);
547 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
548
549 // Push the new node and any (possibly new) users onto the worklist.
550 AddToWorkList(TLO.New.Val);
551 AddUsersToWorkList(TLO.New.Val);
552
553 // Finally, if the node is now dead, remove it from the graph. The node
554 // may not be dead if the replacement process recursively simplified to
555 // something else needing this node.
556 if (TLO.Old.Val->use_empty()) {
557 removeFromWorkList(TLO.Old.Val);
558
559 // If the operands of this node are only used by the node, they will now
560 // be dead. Make sure to visit them first to delete dead nodes early.
561 for (unsigned i = 0, e = TLO.Old.Val->getNumOperands(); i != e; ++i)
562 if (TLO.Old.Val->getOperand(i).Val->hasOneUse())
563 AddToWorkList(TLO.Old.Val->getOperand(i).Val);
564
565 DAG.DeleteNode(TLO.Old.Val);
566 }
567 return true;
568}
569
Chris Lattner29446522007-05-14 22:04:50 +0000570//===----------------------------------------------------------------------===//
571// Main DAG Combiner implementation
572//===----------------------------------------------------------------------===//
573
Nate Begeman4ebd8052005-09-01 23:24:04 +0000574void DAGCombiner::Run(bool RunningAfterLegalize) {
575 // set the instance variable, so that the various visit routines may use it.
576 AfterLegalize = RunningAfterLegalize;
577
Nate Begeman646d7e22005-09-02 21:18:40 +0000578 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000579 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
580 E = DAG.allnodes_end(); I != E; ++I)
581 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000582
Chris Lattner95038592005-10-05 06:35:28 +0000583 // Create a dummy node (which is not added to allnodes), that adds a reference
584 // to the root node, preventing it from being deleted, and tracking any
585 // changes of the root.
586 HandleSDNode Dummy(DAG.getRoot());
587
Jim Laskey26f7fa72006-10-17 19:33:52 +0000588 // The root of the dag may dangle to deleted nodes until the dag combiner is
589 // done. Set it to null to avoid confusion.
590 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000591
Nate Begeman1d4d4142005-09-01 00:19:25 +0000592 // while the worklist isn't empty, inspect the node on the end of it and
593 // try and combine it.
594 while (!WorkList.empty()) {
595 SDNode *N = WorkList.back();
596 WorkList.pop_back();
597
598 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000599 // N is deleted from the DAG, since they too may now be dead or may have a
600 // reduced number of uses, allowing other xforms.
601 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000602 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000603 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000604
Chris Lattner95038592005-10-05 06:35:28 +0000605 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000606 continue;
607 }
608
Dan Gohman389079b2007-10-08 17:57:15 +0000609 SDOperand RV = combine(N);
Chris Lattner24664722006-03-01 04:53:38 +0000610
Chris Lattner50d8e492008-01-25 23:34:24 +0000611 if (RV.Val == 0)
612 continue;
613
614 ++NodesCombined;
Chris Lattner5eee4272008-01-26 01:09:19 +0000615
Chris Lattner50d8e492008-01-25 23:34:24 +0000616 // If we get back the same node we passed in, rather than a new node or
617 // zero, we know that the node must have defined multiple values and
618 // CombineTo was used. Since CombineTo takes care of the worklist
619 // mechanics for us, we have no work to do in this case.
620 if (RV.Val == N)
621 continue;
622
623 assert(N->getOpcode() != ISD::DELETED_NODE &&
624 RV.Val->getOpcode() != ISD::DELETED_NODE &&
625 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000626
Chris Lattner50d8e492008-01-25 23:34:24 +0000627 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
628 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
629 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000630 WorkListRemover DeadNodes(*this);
Chris Lattner50d8e492008-01-25 23:34:24 +0000631 if (N->getNumValues() == RV.Val->getNumValues())
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000632 DAG.ReplaceAllUsesWith(N, RV.Val, &DeadNodes);
Chris Lattner50d8e492008-01-25 23:34:24 +0000633 else {
Chris Lattner5eee4272008-01-26 01:09:19 +0000634 assert(N->getValueType(0) == RV.getValueType() &&
635 N->getNumValues() == 1 && "Type mismatch");
Chris Lattner50d8e492008-01-25 23:34:24 +0000636 SDOperand OpV = RV;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000637 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000638 }
Chris Lattner50d8e492008-01-25 23:34:24 +0000639
640 // Push the new node and any users onto the worklist
641 AddToWorkList(RV.Val);
642 AddUsersToWorkList(RV.Val);
643
644 // Add any uses of the old node to the worklist in case this node is the
645 // last one that uses them. They may become dead after this node is
646 // deleted.
647 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
648 AddToWorkList(N->getOperand(i).Val);
649
650 // Nodes can be reintroduced into the worklist. Make sure we do not
651 // process a node that has been replaced.
652 removeFromWorkList(N);
Chris Lattner50d8e492008-01-25 23:34:24 +0000653
654 // Finally, since the node is now dead, remove it from the graph.
655 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000656 }
Chris Lattner95038592005-10-05 06:35:28 +0000657
658 // If the root changed (e.g. it was a dead load, update the root).
659 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000660}
661
Nate Begeman83e75ec2005-09-06 04:43:02 +0000662SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000663 switch(N->getOpcode()) {
664 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000665 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000666 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000667 case ISD::ADD: return visitADD(N);
668 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000669 case ISD::ADDC: return visitADDC(N);
670 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000671 case ISD::MUL: return visitMUL(N);
672 case ISD::SDIV: return visitSDIV(N);
673 case ISD::UDIV: return visitUDIV(N);
674 case ISD::SREM: return visitSREM(N);
675 case ISD::UREM: return visitUREM(N);
676 case ISD::MULHU: return visitMULHU(N);
677 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000678 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
679 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
680 case ISD::SDIVREM: return visitSDIVREM(N);
681 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000682 case ISD::AND: return visitAND(N);
683 case ISD::OR: return visitOR(N);
684 case ISD::XOR: return visitXOR(N);
685 case ISD::SHL: return visitSHL(N);
686 case ISD::SRA: return visitSRA(N);
687 case ISD::SRL: return visitSRL(N);
688 case ISD::CTLZ: return visitCTLZ(N);
689 case ISD::CTTZ: return visitCTTZ(N);
690 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000691 case ISD::SELECT: return visitSELECT(N);
692 case ISD::SELECT_CC: return visitSELECT_CC(N);
693 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000694 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
695 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000696 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000697 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
698 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000699 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000700 case ISD::FADD: return visitFADD(N);
701 case ISD::FSUB: return visitFSUB(N);
702 case ISD::FMUL: return visitFMUL(N);
703 case ISD::FDIV: return visitFDIV(N);
704 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000705 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000706 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
707 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
708 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
709 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
710 case ISD::FP_ROUND: return visitFP_ROUND(N);
711 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
712 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
713 case ISD::FNEG: return visitFNEG(N);
714 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000715 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000716 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000717 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000718 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000719 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000720 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000721 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
722 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000723 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000724 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000725 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000726}
727
Dan Gohman389079b2007-10-08 17:57:15 +0000728SDOperand DAGCombiner::combine(SDNode *N) {
729
730 SDOperand RV = visit(N);
731
732 // If nothing happened, try a target-specific DAG combine.
733 if (RV.Val == 0) {
734 assert(N->getOpcode() != ISD::DELETED_NODE &&
735 "Node was deleted but visit returned NULL!");
736
737 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
738 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
739
740 // Expose the DAG combiner to the target combiner impls.
741 TargetLowering::DAGCombinerInfo
742 DagCombineInfo(DAG, !AfterLegalize, false, this);
743
744 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
745 }
746 }
747
748 return RV;
749}
750
Chris Lattner6270f682006-10-08 22:57:01 +0000751/// getInputChainForNode - Given a node, return its input chain if it has one,
752/// otherwise return a null sd operand.
753static SDOperand getInputChainForNode(SDNode *N) {
754 if (unsigned NumOps = N->getNumOperands()) {
755 if (N->getOperand(0).getValueType() == MVT::Other)
756 return N->getOperand(0);
757 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
758 return N->getOperand(NumOps-1);
759 for (unsigned i = 1; i < NumOps-1; ++i)
760 if (N->getOperand(i).getValueType() == MVT::Other)
761 return N->getOperand(i);
762 }
763 return SDOperand(0, 0);
764}
765
Nate Begeman83e75ec2005-09-06 04:43:02 +0000766SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000767 // If N has two operands, where one has an input chain equal to the other,
768 // the 'other' chain is redundant.
769 if (N->getNumOperands() == 2) {
770 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
771 return N->getOperand(0);
772 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
773 return N->getOperand(1);
774 }
775
Chris Lattnerc76d4412007-05-16 06:37:59 +0000776 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
777 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
778 SmallPtrSet<SDNode*, 16> SeenOps;
779 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000780
781 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000782 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000783
Jim Laskey71382342006-10-07 23:37:56 +0000784 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000785 // encountered.
786 for (unsigned i = 0; i < TFs.size(); ++i) {
787 SDNode *TF = TFs[i];
788
Jim Laskey6ff23e52006-10-04 16:53:27 +0000789 // Check each of the operands.
790 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
791 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000792
Jim Laskey6ff23e52006-10-04 16:53:27 +0000793 switch (Op.getOpcode()) {
794 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000795 // Entry tokens don't need to be added to the list. They are
796 // rededundant.
797 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000798 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000799
Jim Laskey6ff23e52006-10-04 16:53:27 +0000800 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000801 if ((CombinerAA || Op.hasOneUse()) &&
802 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000803 // Queue up for processing.
804 TFs.push_back(Op.Val);
805 // Clean up in case the token factor is removed.
806 AddToWorkList(Op.Val);
807 Changed = true;
808 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000809 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000810 // Fall thru
811
812 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000813 // Only add if it isn't already in the list.
814 if (SeenOps.insert(Op.Val))
Jim Laskeybc588b82006-10-05 15:07:25 +0000815 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000816 else
817 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000818 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000819 }
820 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000821 }
822
823 SDOperand Result;
824
825 // If we've change things around then replace token factor.
826 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000827 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000828 // The entry token is the only possible outcome.
829 Result = DAG.getEntryNode();
830 } else {
831 // New and improved token factor.
832 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000833 }
Jim Laskey274062c2006-10-13 23:32:28 +0000834
835 // Don't add users to work list.
836 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000837 }
Jim Laskey279f0532006-09-25 16:29:54 +0000838
Jim Laskey6ff23e52006-10-04 16:53:27 +0000839 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000840}
841
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000842/// MERGE_VALUES can always be eliminated.
843SDOperand DAGCombiner::visitMERGE_VALUES(SDNode *N) {
844 WorkListRemover DeadNodes(*this);
845 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
846 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, i), N->getOperand(i),
847 &DeadNodes);
848 removeFromWorkList(N);
849 DAG.DeleteNode(N);
850 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
851}
852
853
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000854static
855SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
856 MVT::ValueType VT = N0.getValueType();
857 SDOperand N00 = N0.getOperand(0);
858 SDOperand N01 = N0.getOperand(1);
859 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
860 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
861 isa<ConstantSDNode>(N00.getOperand(1))) {
862 N0 = DAG.getNode(ISD::ADD, VT,
863 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
864 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
865 return DAG.getNode(ISD::ADD, VT, N0, N1);
866 }
867 return SDOperand();
868}
869
Evan Chengb13cdbd2007-06-21 07:39:16 +0000870static
871SDOperand combineSelectAndUse(SDNode *N, SDOperand Slct, SDOperand OtherOp,
872 SelectionDAG &DAG) {
873 MVT::ValueType VT = N->getValueType(0);
874 unsigned Opc = N->getOpcode();
875 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
876 SDOperand LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
877 SDOperand RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
878 ISD::CondCode CC = ISD::SETCC_INVALID;
879 if (isSlctCC)
880 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
881 else {
882 SDOperand CCOp = Slct.getOperand(0);
883 if (CCOp.getOpcode() == ISD::SETCC)
884 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
885 }
886
887 bool DoXform = false;
888 bool InvCC = false;
889 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
890 "Bad input!");
891 if (LHS.getOpcode() == ISD::Constant &&
892 cast<ConstantSDNode>(LHS)->isNullValue())
893 DoXform = true;
894 else if (CC != ISD::SETCC_INVALID &&
895 RHS.getOpcode() == ISD::Constant &&
896 cast<ConstantSDNode>(RHS)->isNullValue()) {
897 std::swap(LHS, RHS);
Chris Lattner4626b252008-01-17 07:20:38 +0000898 SDOperand Op0 = Slct.getOperand(0);
899 bool isInt = MVT::isInteger(isSlctCC ? Op0.getValueType()
900 : Op0.getOperand(0).getValueType());
Evan Chengb13cdbd2007-06-21 07:39:16 +0000901 CC = ISD::getSetCCInverse(CC, isInt);
902 DoXform = true;
903 InvCC = true;
904 }
905
906 if (DoXform) {
907 SDOperand Result = DAG.getNode(Opc, VT, OtherOp, RHS);
908 if (isSlctCC)
909 return DAG.getSelectCC(OtherOp, Result,
910 Slct.getOperand(0), Slct.getOperand(1), CC);
911 SDOperand CCOp = Slct.getOperand(0);
912 if (InvCC)
913 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
914 CCOp.getOperand(1), CC);
915 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
916 }
917 return SDOperand();
918}
919
Nate Begeman83e75ec2005-09-06 04:43:02 +0000920SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000921 SDOperand N0 = N->getOperand(0);
922 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000923 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
924 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000925 MVT::ValueType VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000926
927 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +0000928 if (MVT::isVector(VT)) {
929 SDOperand FoldedVOp = SimplifyVBinOp(N);
930 if (FoldedVOp.Val) return FoldedVOp;
931 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000932
Dan Gohman613e0d82007-07-03 14:03:57 +0000933 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000934 if (N0.getOpcode() == ISD::UNDEF)
935 return N0;
936 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000937 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000938 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000939 if (N0C && N1C)
Bill Wendling91b9ad12008-02-10 08:10:24 +0000940 return DAG.getConstant(N0C->getValue() + N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000941 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000942 if (N0C && !N1C)
943 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000944 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000945 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000946 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000947 // fold ((c1-A)+c2) -> (c1+c2)-A
948 if (N1C && N0.getOpcode() == ISD::SUB)
949 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
950 return DAG.getNode(ISD::SUB, VT,
951 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
952 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000953 // reassociate add
954 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
955 if (RADD.Val != 0)
956 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000957 // fold ((0-A) + B) -> B-A
958 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
959 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000960 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000961 // fold (A + (0-B)) -> A-B
962 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
963 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000964 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000965 // fold (A+(B-A)) -> B
966 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000967 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000968
Evan Cheng860771d2006-03-01 01:09:54 +0000969 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000970 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000971
972 // fold (a+b) -> (a|b) iff a and b share no bits.
973 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
974 uint64_t LHSZero, LHSOne;
975 uint64_t RHSZero, RHSOne;
976 uint64_t Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +0000977 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000978 if (LHSZero) {
Dan Gohmanea859be2007-06-22 14:59:07 +0000979 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000980
981 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
982 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
983 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
984 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
985 return DAG.getNode(ISD::OR, VT, N0, N1);
986 }
987 }
Evan Cheng3ef554d2006-11-06 08:14:30 +0000988
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000989 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
990 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
991 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
992 if (Result.Val) return Result;
993 }
994 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
995 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
996 if (Result.Val) return Result;
997 }
998
Evan Chengb13cdbd2007-06-21 07:39:16 +0000999 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
1000 if (N0.getOpcode() == ISD::SELECT && N0.Val->hasOneUse()) {
1001 SDOperand Result = combineSelectAndUse(N, N0, N1, DAG);
1002 if (Result.Val) return Result;
1003 }
1004 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1005 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1006 if (Result.Val) return Result;
1007 }
1008
Nate Begeman83e75ec2005-09-06 04:43:02 +00001009 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001010}
1011
Chris Lattner91153682007-03-04 20:03:15 +00001012SDOperand DAGCombiner::visitADDC(SDNode *N) {
1013 SDOperand N0 = N->getOperand(0);
1014 SDOperand N1 = N->getOperand(1);
1015 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1016 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1017 MVT::ValueType VT = N0.getValueType();
1018
1019 // If the flag result is dead, turn this into an ADD.
1020 if (N->hasNUsesOfValue(0, 1))
1021 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +00001022 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +00001023
1024 // canonicalize constant to RHS.
Chris Lattnerbcf24842007-03-04 20:08:45 +00001025 if (N0C && !N1C) {
1026 SDOperand Ops[] = { N1, N0 };
1027 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
1028 }
Chris Lattner91153682007-03-04 20:03:15 +00001029
Chris Lattnerb6541762007-03-04 20:40:38 +00001030 // fold (addc x, 0) -> x + no carry out
1031 if (N1C && N1C->isNullValue())
1032 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1033
1034 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
1035 uint64_t LHSZero, LHSOne;
1036 uint64_t RHSZero, RHSOne;
1037 uint64_t Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00001038 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001039 if (LHSZero) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001040 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001041
1042 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1043 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1044 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1045 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1046 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1047 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1048 }
Chris Lattner91153682007-03-04 20:03:15 +00001049
1050 return SDOperand();
1051}
1052
1053SDOperand DAGCombiner::visitADDE(SDNode *N) {
1054 SDOperand N0 = N->getOperand(0);
1055 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +00001056 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001057 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1058 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Chris Lattnerbcf24842007-03-04 20:08:45 +00001059 //MVT::ValueType VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001060
1061 // canonicalize constant to RHS
Chris Lattnerbcf24842007-03-04 20:08:45 +00001062 if (N0C && !N1C) {
Chris Lattnerb6541762007-03-04 20:40:38 +00001063 SDOperand Ops[] = { N1, N0, CarryIn };
Chris Lattnerbcf24842007-03-04 20:08:45 +00001064 return DAG.getNode(ISD::ADDE, N->getVTList(), Ops, 3);
1065 }
Chris Lattner91153682007-03-04 20:03:15 +00001066
Chris Lattnerb6541762007-03-04 20:40:38 +00001067 // fold (adde x, y, false) -> (addc x, y)
1068 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) {
1069 SDOperand Ops[] = { N1, N0 };
1070 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
1071 }
Chris Lattner91153682007-03-04 20:03:15 +00001072
1073 return SDOperand();
1074}
1075
1076
1077
Nate Begeman83e75ec2005-09-06 04:43:02 +00001078SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001079 SDOperand N0 = N->getOperand(0);
1080 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001081 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1082 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001083 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001084
Dan Gohman7f321562007-06-25 16:23:39 +00001085 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001086 if (MVT::isVector(VT)) {
1087 SDOperand FoldedVOp = SimplifyVBinOp(N);
1088 if (FoldedVOp.Val) return FoldedVOp;
1089 }
Dan Gohman7f321562007-06-25 16:23:39 +00001090
Chris Lattner854077d2005-10-17 01:07:11 +00001091 // fold (sub x, x) -> 0
1092 if (N0 == N1)
1093 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001094 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001095 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001096 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001097 // fold (sub x, c) -> (add x, -c)
1098 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001099 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001100 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001101 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001102 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001103 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001104 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001105 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001106 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1107 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1108 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1109 if (Result.Val) return Result;
1110 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001111 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001112 if (N0.getOpcode() == ISD::UNDEF)
1113 return N0;
1114 if (N1.getOpcode() == ISD::UNDEF)
1115 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001116
Nate Begeman83e75ec2005-09-06 04:43:02 +00001117 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001118}
1119
Nate Begeman83e75ec2005-09-06 04:43:02 +00001120SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001121 SDOperand N0 = N->getOperand(0);
1122 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001123 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1124 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +00001125 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001126
Dan Gohman7f321562007-06-25 16:23:39 +00001127 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001128 if (MVT::isVector(VT)) {
1129 SDOperand FoldedVOp = SimplifyVBinOp(N);
1130 if (FoldedVOp.Val) return FoldedVOp;
1131 }
Dan Gohman7f321562007-06-25 16:23:39 +00001132
Dan Gohman613e0d82007-07-03 14:03:57 +00001133 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001134 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001135 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001136 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001137 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001138 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001139 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001140 if (N0C && !N1C)
1141 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001142 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001143 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001144 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001145 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001146 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001147 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001148 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +00001149 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +00001150 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001151 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001152 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001153 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1154 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1155 // FIXME: If the input is something that is easily negated (e.g. a
1156 // single-use add), we should put the negate there.
1157 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1158 DAG.getNode(ISD::SHL, VT, N0,
1159 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1160 TLI.getShiftAmountTy())));
1161 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001162
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001163 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1164 if (N1C && N0.getOpcode() == ISD::SHL &&
1165 isa<ConstantSDNode>(N0.getOperand(1))) {
1166 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001167 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001168 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1169 }
1170
1171 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1172 // use.
1173 {
1174 SDOperand Sh(0,0), Y(0,0);
1175 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1176 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1177 N0.Val->hasOneUse()) {
1178 Sh = N0; Y = N1;
1179 } else if (N1.getOpcode() == ISD::SHL &&
1180 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1181 Sh = N1; Y = N0;
1182 }
1183 if (Sh.Val) {
1184 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1185 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1186 }
1187 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001188 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1189 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1190 isa<ConstantSDNode>(N0.getOperand(1))) {
1191 return DAG.getNode(ISD::ADD, VT,
1192 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1193 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1194 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001195
Nate Begemancd4d58c2006-02-03 06:46:56 +00001196 // reassociate mul
1197 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1198 if (RMUL.Val != 0)
1199 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001200
Nate Begeman83e75ec2005-09-06 04:43:02 +00001201 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001202}
1203
Nate Begeman83e75ec2005-09-06 04:43:02 +00001204SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001205 SDOperand N0 = N->getOperand(0);
1206 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001207 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1208 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001209 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001210
Dan Gohman7f321562007-06-25 16:23:39 +00001211 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001212 if (MVT::isVector(VT)) {
1213 SDOperand FoldedVOp = SimplifyVBinOp(N);
1214 if (FoldedVOp.Val) return FoldedVOp;
1215 }
Dan Gohman7f321562007-06-25 16:23:39 +00001216
Nate Begeman1d4d4142005-09-01 00:19:25 +00001217 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001218 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001219 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001220 // fold (sdiv X, 1) -> X
1221 if (N1C && N1C->getSignExtended() == 1LL)
1222 return N0;
1223 // fold (sdiv X, -1) -> 0-X
1224 if (N1C && N1C->isAllOnesValue())
1225 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001226 // If we know the sign bits of both operands are zero, strength reduce to a
1227 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Chris Lattnerf32aac32008-01-27 23:32:17 +00001228 if (!MVT::isVector(VT)) {
1229 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1230 if (DAG.MaskedValueIsZero(N1, SignBit) &&
1231 DAG.MaskedValueIsZero(N0, SignBit))
1232 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1233 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001234 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +00001235 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001236 (isPowerOf2_64(N1C->getSignExtended()) ||
1237 isPowerOf2_64(-N1C->getSignExtended()))) {
1238 // If dividing by powers of two is cheap, then don't perform the following
1239 // fold.
1240 if (TLI.isPow2DivCheap())
1241 return SDOperand();
1242 int64_t pow2 = N1C->getSignExtended();
1243 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001244 unsigned lg2 = Log2_64(abs2);
1245 // Splat the sign bit into the register
1246 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001247 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1248 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001249 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001250 // Add (N0 < 0) ? abs2 - 1 : 0;
1251 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1252 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001253 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001254 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001255 AddToWorkList(SRL.Val);
1256 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001257 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1258 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001259 // If we're dividing by a positive value, we're done. Otherwise, we must
1260 // negate the result.
1261 if (pow2 > 0)
1262 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001263 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001264 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1265 }
Nate Begeman69575232005-10-20 02:15:44 +00001266 // if integer divide is expensive and we satisfy the requirements, emit an
1267 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001268 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001269 !TLI.isIntDivCheap()) {
1270 SDOperand Op = BuildSDIV(N);
1271 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001272 }
Dan Gohman7f321562007-06-25 16:23:39 +00001273
Dan Gohman613e0d82007-07-03 14:03:57 +00001274 // undef / X -> 0
1275 if (N0.getOpcode() == ISD::UNDEF)
1276 return DAG.getConstant(0, VT);
1277 // X / undef -> undef
1278 if (N1.getOpcode() == ISD::UNDEF)
1279 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001280
Nate Begeman83e75ec2005-09-06 04:43:02 +00001281 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001282}
1283
Nate Begeman83e75ec2005-09-06 04:43:02 +00001284SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001285 SDOperand N0 = N->getOperand(0);
1286 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001287 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1288 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001289 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001290
Dan Gohman7f321562007-06-25 16:23:39 +00001291 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001292 if (MVT::isVector(VT)) {
1293 SDOperand FoldedVOp = SimplifyVBinOp(N);
1294 if (FoldedVOp.Val) return FoldedVOp;
1295 }
Dan Gohman7f321562007-06-25 16:23:39 +00001296
Nate Begeman1d4d4142005-09-01 00:19:25 +00001297 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001298 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001299 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001300 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +00001301 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001302 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001303 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001304 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001305 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1306 if (N1.getOpcode() == ISD::SHL) {
1307 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1308 if (isPowerOf2_64(SHC->getValue())) {
1309 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001310 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
1311 DAG.getConstant(Log2_64(SHC->getValue()),
1312 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001313 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001314 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001315 }
1316 }
1317 }
Nate Begeman69575232005-10-20 02:15:44 +00001318 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +00001319 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
1320 SDOperand Op = BuildUDIV(N);
1321 if (Op.Val) return Op;
1322 }
Dan Gohman7f321562007-06-25 16:23:39 +00001323
Dan Gohman613e0d82007-07-03 14:03:57 +00001324 // undef / X -> 0
1325 if (N0.getOpcode() == ISD::UNDEF)
1326 return DAG.getConstant(0, VT);
1327 // X / undef -> undef
1328 if (N1.getOpcode() == ISD::UNDEF)
1329 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001330
Nate Begeman83e75ec2005-09-06 04:43:02 +00001331 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001332}
1333
Nate Begeman83e75ec2005-09-06 04:43:02 +00001334SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001335 SDOperand N0 = N->getOperand(0);
1336 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001337 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1338 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001339 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001340
1341 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001342 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001343 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001344 // If we know the sign bits of both operands are zero, strength reduce to a
1345 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Chris Lattneree339f42008-01-27 23:21:58 +00001346 if (!MVT::isVector(VT)) {
1347 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1348 if (DAG.MaskedValueIsZero(N1, SignBit) &&
1349 DAG.MaskedValueIsZero(N0, SignBit))
1350 return DAG.getNode(ISD::UREM, VT, N0, N1);
1351 }
Chris Lattner26d29902006-10-12 20:58:32 +00001352
Dan Gohman77003042007-11-26 23:46:11 +00001353 // If X/C can be simplified by the division-by-constant logic, lower
1354 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001355 if (N1C && !N1C->isNullValue()) {
1356 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Chris Lattner5eee4272008-01-26 01:09:19 +00001357 AddToWorkList(Div.Val);
Dan Gohman77003042007-11-26 23:46:11 +00001358 SDOperand OptimizedDiv = combine(Div.Val);
1359 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1360 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1361 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1362 AddToWorkList(Mul.Val);
1363 return Sub;
1364 }
Chris Lattner26d29902006-10-12 20:58:32 +00001365 }
1366
Dan Gohman613e0d82007-07-03 14:03:57 +00001367 // undef % X -> 0
1368 if (N0.getOpcode() == ISD::UNDEF)
1369 return DAG.getConstant(0, VT);
1370 // X % undef -> undef
1371 if (N1.getOpcode() == ISD::UNDEF)
1372 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001373
Nate Begeman83e75ec2005-09-06 04:43:02 +00001374 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001375}
1376
Nate Begeman83e75ec2005-09-06 04:43:02 +00001377SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001378 SDOperand N0 = N->getOperand(0);
1379 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001380 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1381 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001382 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001383
1384 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001385 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001386 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001387 // fold (urem x, pow2) -> (and x, pow2-1)
1388 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +00001389 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001390 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1391 if (N1.getOpcode() == ISD::SHL) {
1392 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1393 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +00001394 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001395 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001396 return DAG.getNode(ISD::AND, VT, N0, Add);
1397 }
1398 }
1399 }
Chris Lattner26d29902006-10-12 20:58:32 +00001400
Dan Gohman77003042007-11-26 23:46:11 +00001401 // If X/C can be simplified by the division-by-constant logic, lower
1402 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001403 if (N1C && !N1C->isNullValue()) {
1404 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman77003042007-11-26 23:46:11 +00001405 SDOperand OptimizedDiv = combine(Div.Val);
1406 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1407 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1408 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1409 AddToWorkList(Mul.Val);
1410 return Sub;
1411 }
Chris Lattner26d29902006-10-12 20:58:32 +00001412 }
1413
Dan Gohman613e0d82007-07-03 14:03:57 +00001414 // undef % X -> 0
1415 if (N0.getOpcode() == ISD::UNDEF)
1416 return DAG.getConstant(0, VT);
1417 // X % undef -> undef
1418 if (N1.getOpcode() == ISD::UNDEF)
1419 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001420
Nate Begeman83e75ec2005-09-06 04:43:02 +00001421 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001422}
1423
Nate Begeman83e75ec2005-09-06 04:43:02 +00001424SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001425 SDOperand N0 = N->getOperand(0);
1426 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001427 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001428 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001429
1430 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001431 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001432 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001433 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001434 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001435 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1436 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001437 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001438 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001439 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001440 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001441
Nate Begeman83e75ec2005-09-06 04:43:02 +00001442 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001443}
1444
Nate Begeman83e75ec2005-09-06 04:43:02 +00001445SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001446 SDOperand N0 = N->getOperand(0);
1447 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001448 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001449 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001450
1451 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001452 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001453 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001454 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001455 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001456 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001457 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001458 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001459 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001460
Nate Begeman83e75ec2005-09-06 04:43:02 +00001461 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001462}
1463
Dan Gohman389079b2007-10-08 17:57:15 +00001464/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1465/// compute two values. LoOp and HiOp give the opcodes for the two computations
1466/// that are being performed. Return true if a simplification was made.
1467///
Chris Lattner5eee4272008-01-26 01:09:19 +00001468SDOperand DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1469 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001470 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001471 bool HiExists = N->hasAnyUseOfValue(1);
1472 if (!HiExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001473 (!AfterLegalize ||
1474 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001475 SDOperand Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
1476 N->getNumOperands());
1477 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001478 }
1479
1480 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001481 bool LoExists = N->hasAnyUseOfValue(0);
1482 if (!LoExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001483 (!AfterLegalize ||
1484 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001485 SDOperand Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
1486 N->getNumOperands());
1487 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001488 }
1489
Evan Cheng44711942007-11-08 09:25:29 +00001490 // If both halves are used, return as it is.
1491 if (LoExists && HiExists)
Chris Lattner5eee4272008-01-26 01:09:19 +00001492 return SDOperand();
Evan Cheng44711942007-11-08 09:25:29 +00001493
1494 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001495 if (LoExists) {
1496 SDOperand Lo = DAG.getNode(LoOp, N->getValueType(0),
1497 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001498 AddToWorkList(Lo.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001499 SDOperand LoOpt = combine(Lo.Val);
Chris Lattner5eee4272008-01-26 01:09:19 +00001500 if (LoOpt.Val && LoOpt.Val != Lo.Val &&
1501 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType()))
1502 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001503 }
1504
Evan Cheng44711942007-11-08 09:25:29 +00001505 if (HiExists) {
1506 SDOperand Hi = DAG.getNode(HiOp, N->getValueType(1),
1507 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001508 AddToWorkList(Hi.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001509 SDOperand HiOpt = combine(Hi.Val);
1510 if (HiOpt.Val && HiOpt != Hi &&
Chris Lattner5eee4272008-01-26 01:09:19 +00001511 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType()))
1512 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001513 }
Chris Lattner5eee4272008-01-26 01:09:19 +00001514 return SDOperand();
Dan Gohman389079b2007-10-08 17:57:15 +00001515}
1516
1517SDOperand DAGCombiner::visitSMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001518 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
1519 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001520
1521 return SDOperand();
1522}
1523
1524SDOperand DAGCombiner::visitUMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001525 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
1526 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001527
1528 return SDOperand();
1529}
1530
1531SDOperand DAGCombiner::visitSDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001532 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
1533 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001534
1535 return SDOperand();
1536}
1537
1538SDOperand DAGCombiner::visitUDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001539 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
1540 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001541
1542 return SDOperand();
1543}
1544
Chris Lattner35e5c142006-05-05 05:51:50 +00001545/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1546/// two operands of the same opcode, try to simplify it.
1547SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1548 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1549 MVT::ValueType VT = N0.getValueType();
1550 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1551
Chris Lattner540121f2006-05-05 06:31:05 +00001552 // For each of OP in AND/OR/XOR:
1553 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1554 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1555 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001556 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001557 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001558 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001559 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1560 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1561 N0.getOperand(0).getValueType(),
1562 N0.getOperand(0), N1.getOperand(0));
1563 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001564 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001565 }
1566
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001567 // For each of OP in SHL/SRL/SRA/AND...
1568 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1569 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1570 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001571 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001572 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001573 N0.getOperand(1) == N1.getOperand(1)) {
1574 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1575 N0.getOperand(0).getValueType(),
1576 N0.getOperand(0), N1.getOperand(0));
1577 AddToWorkList(ORNode.Val);
1578 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1579 }
1580
1581 return SDOperand();
1582}
1583
Nate Begeman83e75ec2005-09-06 04:43:02 +00001584SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001585 SDOperand N0 = N->getOperand(0);
1586 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001587 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001588 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1589 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001590 MVT::ValueType VT = N1.getValueType();
1591
Dan Gohman7f321562007-06-25 16:23:39 +00001592 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001593 if (MVT::isVector(VT)) {
1594 SDOperand FoldedVOp = SimplifyVBinOp(N);
1595 if (FoldedVOp.Val) return FoldedVOp;
1596 }
Dan Gohman7f321562007-06-25 16:23:39 +00001597
Dan Gohman613e0d82007-07-03 14:03:57 +00001598 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001599 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001600 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001601 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001602 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001603 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001604 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001605 if (N0C && !N1C)
1606 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001607 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001608 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001609 return N0;
1610 // if (and x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001611 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001612 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001613 // reassociate and
1614 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1615 if (RAND.Val != 0)
1616 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001617 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001618 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001619 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001620 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001621 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001622 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1623 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001624 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Dan Gohmanea859be2007-06-22 14:59:07 +00001625 if (DAG.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001626 ~N1C->getValue() & InMask)) {
1627 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1628 N0.getOperand(0));
1629
1630 // Replace uses of the AND with uses of the Zero extend node.
1631 CombineTo(N, Zext);
1632
Chris Lattner3603cd62006-02-02 07:17:31 +00001633 // We actually want to replace all uses of the any_extend with the
1634 // zero_extend, to avoid duplicating things. This will later cause this
1635 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001636 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001637 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001638 }
1639 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001640 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1641 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1642 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1643 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1644
1645 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1646 MVT::isInteger(LL.getValueType())) {
1647 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1648 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1649 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001650 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001651 return DAG.getSetCC(VT, ORNode, LR, Op1);
1652 }
1653 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1654 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1655 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001656 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001657 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1658 }
1659 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1660 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1661 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001662 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001663 return DAG.getSetCC(VT, ORNode, LR, Op1);
1664 }
1665 }
1666 // canonicalize equivalent to ll == rl
1667 if (LL == RR && LR == RL) {
1668 Op1 = ISD::getSetCCSwappedOperands(Op1);
1669 std::swap(RL, RR);
1670 }
1671 if (LL == RL && LR == RR) {
1672 bool isInteger = MVT::isInteger(LL.getValueType());
1673 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1674 if (Result != ISD::SETCC_INVALID)
1675 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1676 }
1677 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001678
1679 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1680 if (N0.getOpcode() == N1.getOpcode()) {
1681 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1682 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001683 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001684
Nate Begemande996292006-02-03 22:24:05 +00001685 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1686 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001687 if (!MVT::isVector(VT) &&
1688 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001689 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001690 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001691 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001692 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001693 MVT::ValueType EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001694 // If we zero all the possible extended bits, then we can turn this into
1695 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmanea859be2007-06-22 14:59:07 +00001696 if (DAG.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001697 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001698 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1699 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001700 LN0->getSrcValueOffset(), EVT,
1701 LN0->isVolatile(),
1702 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001703 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001704 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001705 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001706 }
1707 }
1708 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001709 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1710 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001711 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001712 MVT::ValueType EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001713 // If we zero all the possible extended bits, then we can turn this into
1714 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmanea859be2007-06-22 14:59:07 +00001715 if (DAG.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001716 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001717 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1718 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001719 LN0->getSrcValueOffset(), EVT,
1720 LN0->isVolatile(),
1721 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001722 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001723 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001724 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001725 }
1726 }
Chris Lattner15045b62006-02-28 06:35:35 +00001727
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001728 // fold (and (load x), 255) -> (zextload x, i8)
1729 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001730 if (N1C && N0.getOpcode() == ISD::LOAD) {
1731 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1732 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Chris Lattnerddf89562008-01-17 19:59:44 +00001733 LN0->isUnindexed() && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001734 MVT::ValueType EVT, LoadedVT;
1735 if (N1C->getValue() == 255)
1736 EVT = MVT::i8;
1737 else if (N1C->getValue() == 65535)
1738 EVT = MVT::i16;
1739 else if (N1C->getValue() == ~0U)
1740 EVT = MVT::i32;
1741 else
1742 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001743
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001744 LoadedVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001745 if (EVT != MVT::Other && LoadedVT > EVT &&
1746 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1747 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1748 // For big endian targets, we need to add an offset to the pointer to
1749 // load the correct bytes. For little endian systems, we merely need to
1750 // read fewer bytes from the same pointer.
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001751 unsigned LVTStoreBytes = MVT::getStoreSizeInBits(LoadedVT)/8;
1752 unsigned EVTStoreBytes = MVT::getStoreSizeInBits(EVT)/8;
1753 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001754 unsigned Alignment = LN0->getAlignment();
Evan Cheng466685d2006-10-09 20:57:25 +00001755 SDOperand NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001756 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001757 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1758 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001759 Alignment = MinAlign(Alignment, PtrOff);
1760 }
Evan Cheng466685d2006-10-09 20:57:25 +00001761 AddToWorkList(NewPtr.Val);
1762 SDOperand Load =
1763 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001764 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001765 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001766 AddToWorkList(N);
1767 CombineTo(N0.Val, Load, Load.getValue(1));
1768 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1769 }
Chris Lattner15045b62006-02-28 06:35:35 +00001770 }
1771 }
1772
Nate Begeman83e75ec2005-09-06 04:43:02 +00001773 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001774}
1775
Nate Begeman83e75ec2005-09-06 04:43:02 +00001776SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001777 SDOperand N0 = N->getOperand(0);
1778 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001779 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001780 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1781 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001782 MVT::ValueType VT = N1.getValueType();
1783 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001784
Dan Gohman7f321562007-06-25 16:23:39 +00001785 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001786 if (MVT::isVector(VT)) {
1787 SDOperand FoldedVOp = SimplifyVBinOp(N);
1788 if (FoldedVOp.Val) return FoldedVOp;
1789 }
Dan Gohman7f321562007-06-25 16:23:39 +00001790
Dan Gohman613e0d82007-07-03 14:03:57 +00001791 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001792 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001793 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001794 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001795 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001796 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001797 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001798 if (N0C && !N1C)
1799 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001800 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001801 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001802 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001803 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001804 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001805 return N1;
1806 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001807 if (N1C &&
Dan Gohmanea859be2007-06-22 14:59:07 +00001808 DAG.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001809 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001810 // reassociate or
1811 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1812 if (ROR.Val != 0)
1813 return ROR;
1814 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1815 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001816 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001817 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1818 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1819 N1),
1820 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001821 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001822 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1823 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1824 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1825 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1826
1827 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1828 MVT::isInteger(LL.getValueType())) {
1829 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1830 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1831 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1832 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1833 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001834 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001835 return DAG.getSetCC(VT, ORNode, LR, Op1);
1836 }
1837 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1838 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1839 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1840 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1841 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001842 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001843 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1844 }
1845 }
1846 // canonicalize equivalent to ll == rl
1847 if (LL == RR && LR == RL) {
1848 Op1 = ISD::getSetCCSwappedOperands(Op1);
1849 std::swap(RL, RR);
1850 }
1851 if (LL == RL && LR == RR) {
1852 bool isInteger = MVT::isInteger(LL.getValueType());
1853 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1854 if (Result != ISD::SETCC_INVALID)
1855 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1856 }
1857 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001858
1859 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1860 if (N0.getOpcode() == N1.getOpcode()) {
1861 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1862 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001863 }
Chris Lattner516b9622006-09-14 20:50:57 +00001864
Chris Lattner1ec72732006-09-14 21:11:37 +00001865 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1866 if (N0.getOpcode() == ISD::AND &&
1867 N1.getOpcode() == ISD::AND &&
1868 N0.getOperand(1).getOpcode() == ISD::Constant &&
1869 N1.getOperand(1).getOpcode() == ISD::Constant &&
1870 // Don't increase # computations.
1871 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1872 // We can only do this xform if we know that bits from X that are set in C2
1873 // but not in C1 are already zero. Likewise for Y.
1874 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1875 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1876
Dan Gohmanea859be2007-06-22 14:59:07 +00001877 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1878 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001879 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1880 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1881 }
1882 }
1883
1884
Chris Lattner516b9622006-09-14 20:50:57 +00001885 // See if this is some rotate idiom.
1886 if (SDNode *Rot = MatchRotate(N0, N1))
1887 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001888
Nate Begeman83e75ec2005-09-06 04:43:02 +00001889 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001890}
1891
Chris Lattner516b9622006-09-14 20:50:57 +00001892
1893/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1894static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1895 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001896 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001897 Mask = Op.getOperand(1);
1898 Op = Op.getOperand(0);
1899 } else {
1900 return false;
1901 }
1902 }
1903
1904 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1905 Shift = Op;
1906 return true;
1907 }
1908 return false;
1909}
1910
1911
1912// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1913// idioms for rotate, and if the target supports rotation instructions, generate
1914// a rot[lr].
1915SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1916 // Must be a legal type. Expanded an promoted things won't work with rotates.
1917 MVT::ValueType VT = LHS.getValueType();
1918 if (!TLI.isTypeLegal(VT)) return 0;
1919
1920 // The target must have at least one rotate flavor.
1921 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1922 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1923 if (!HasROTL && !HasROTR) return 0;
1924
1925 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1926 SDOperand LHSShift; // The shift.
1927 SDOperand LHSMask; // AND value if any.
1928 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1929 return 0; // Not part of a rotate.
1930
1931 SDOperand RHSShift; // The shift.
1932 SDOperand RHSMask; // AND value if any.
1933 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1934 return 0; // Not part of a rotate.
1935
1936 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1937 return 0; // Not shifting the same value.
1938
1939 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1940 return 0; // Shifts must disagree.
1941
1942 // Canonicalize shl to left side in a shl/srl pair.
1943 if (RHSShift.getOpcode() == ISD::SHL) {
1944 std::swap(LHS, RHS);
1945 std::swap(LHSShift, RHSShift);
1946 std::swap(LHSMask , RHSMask );
1947 }
1948
1949 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Scott Michelc9dc1142007-04-02 21:36:32 +00001950 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1951 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1952 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001953
1954 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1955 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001956 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1957 RHSShiftAmt.getOpcode() == ISD::Constant) {
1958 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
1959 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00001960 if ((LShVal + RShVal) != OpSizeInBits)
1961 return 0;
1962
1963 SDOperand Rot;
1964 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001965 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001966 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001967 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001968
1969 // If there is an AND of either shifted operand, apply it to the result.
1970 if (LHSMask.Val || RHSMask.Val) {
1971 uint64_t Mask = MVT::getIntVTBitMask(VT);
1972
1973 if (LHSMask.Val) {
1974 uint64_t RHSBits = (1ULL << LShVal)-1;
1975 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1976 }
1977 if (RHSMask.Val) {
1978 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1979 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1980 }
1981
1982 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1983 }
1984
1985 return Rot.Val;
1986 }
1987
1988 // If there is a mask here, and we have a variable shift, we can't be sure
1989 // that we're masking out the right stuff.
1990 if (LHSMask.Val || RHSMask.Val)
1991 return 0;
1992
1993 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1994 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001995 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
1996 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001997 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001998 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001999 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002000 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002001 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002002 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002003 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002004 }
Chris Lattner516b9622006-09-14 20:50:57 +00002005 }
2006 }
2007
2008 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2009 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002010 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2011 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002012 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002013 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002014 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002015 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002016 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002017 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002018 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002019 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002020 }
2021 }
2022
2023 // Look for sign/zext/any-extended cases:
2024 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2025 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2026 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
2027 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2028 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2029 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
2030 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
2031 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
2032 if (RExtOp0.getOpcode() == ISD::SUB &&
2033 RExtOp0.getOperand(1) == LExtOp0) {
2034 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2035 // (rotr x, y)
2036 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2037 // (rotl x, (sub 32, y))
2038 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
2039 if (SUBC->getValue() == OpSizeInBits) {
2040 if (HasROTL)
2041 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2042 else
2043 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
2044 }
2045 }
2046 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2047 RExtOp0 == LExtOp0.getOperand(1)) {
2048 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2049 // (rotl x, y)
2050 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2051 // (rotr x, (sub 32, y))
2052 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
2053 if (SUBC->getValue() == OpSizeInBits) {
2054 if (HasROTL)
2055 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
2056 else
2057 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2058 }
2059 }
Chris Lattner516b9622006-09-14 20:50:57 +00002060 }
2061 }
2062
2063 return 0;
2064}
2065
2066
Nate Begeman83e75ec2005-09-06 04:43:02 +00002067SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002068 SDOperand N0 = N->getOperand(0);
2069 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002070 SDOperand LHS, RHS, CC;
2071 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2072 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002073 MVT::ValueType VT = N0.getValueType();
2074
Dan Gohman7f321562007-06-25 16:23:39 +00002075 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00002076 if (MVT::isVector(VT)) {
2077 SDOperand FoldedVOp = SimplifyVBinOp(N);
2078 if (FoldedVOp.Val) return FoldedVOp;
2079 }
Dan Gohman7f321562007-06-25 16:23:39 +00002080
Dan Gohman613e0d82007-07-03 14:03:57 +00002081 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002082 if (N0.getOpcode() == ISD::UNDEF)
2083 return N0;
2084 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002085 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002086 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002087 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002088 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00002089 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002090 if (N0C && !N1C)
2091 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002092 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002093 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002094 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002095 // reassociate xor
2096 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
2097 if (RXOR.Val != 0)
2098 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002099 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00002100 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
2101 bool isInt = MVT::isInteger(LHS.getValueType());
2102 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2103 isInt);
2104 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002105 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002106 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002107 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002108 assert(0 && "Unhandled SetCC Equivalent!");
2109 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002110 }
Chris Lattner61c5ff42007-09-10 21:39:07 +00002111 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
2112 if (N1C && N1C->getValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
2113 N0.Val->hasOneUse() && isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
2114 SDOperand V = N0.getOperand(0);
2115 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002116 DAG.getConstant(1, V.getValueType()));
Chris Lattner61c5ff42007-09-10 21:39:07 +00002117 AddToWorkList(V.Val);
2118 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2119 }
2120
Nate Begeman99801192005-09-07 23:25:52 +00002121 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Chris Lattner734c91d2006-11-10 21:37:15 +00002122 if (N1C && N1C->getValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002123 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002124 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002125 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2126 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002127 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2128 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002129 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002130 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002131 }
2132 }
Nate Begeman99801192005-09-07 23:25:52 +00002133 // fold !(x or y) -> (!x and !y) iff x or y are constants
2134 if (N1C && N1C->isAllOnesValue() &&
2135 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002136 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002137 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2138 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002139 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2140 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002141 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002142 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002143 }
2144 }
Nate Begeman223df222005-09-08 20:18:10 +00002145 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2146 if (N1C && N0.getOpcode() == ISD::XOR) {
2147 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2148 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2149 if (N00C)
2150 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
2151 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
2152 if (N01C)
2153 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
2154 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
2155 }
2156 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002157 if (N0 == N1) {
2158 if (!MVT::isVector(VT)) {
2159 return DAG.getConstant(0, VT);
2160 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2161 // Produce a vector of zeros.
Dan Gohman51eaa862007-06-14 22:58:02 +00002162 SDOperand El = DAG.getConstant(0, MVT::getVectorElementType(VT));
Chris Lattner4fbdd592006-03-28 19:11:05 +00002163 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002164 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002165 }
2166 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002167
2168 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2169 if (N0.getOpcode() == N1.getOpcode()) {
2170 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
2171 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002172 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002173
Chris Lattner3e104b12006-04-08 04:15:24 +00002174 // Simplify the expression using non-local knowledge.
2175 if (!MVT::isVector(VT) &&
2176 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002177 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002178
Nate Begeman83e75ec2005-09-06 04:43:02 +00002179 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002180}
2181
Chris Lattnere70da202007-12-06 07:33:36 +00002182/// visitShiftByConstant - Handle transforms common to the three shifts, when
2183/// the shift amount is a constant.
2184SDOperand DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
2185 SDNode *LHS = N->getOperand(0).Val;
2186 if (!LHS->hasOneUse()) return SDOperand();
2187
2188 // We want to pull some binops through shifts, so that we have (and (shift))
2189 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2190 // thing happens with address calculations, so it's important to canonicalize
2191 // it.
2192 bool HighBitSet = false; // Can we transform this if the high bit is set?
2193
2194 switch (LHS->getOpcode()) {
2195 default: return SDOperand();
2196 case ISD::OR:
2197 case ISD::XOR:
2198 HighBitSet = false; // We can only transform sra if the high bit is clear.
2199 break;
2200 case ISD::AND:
2201 HighBitSet = true; // We can only transform sra if the high bit is set.
2202 break;
2203 case ISD::ADD:
2204 if (N->getOpcode() != ISD::SHL)
2205 return SDOperand(); // only shl(add) not sr[al](add).
2206 HighBitSet = false; // We can only transform sra if the high bit is clear.
2207 break;
2208 }
2209
2210 // We require the RHS of the binop to be a constant as well.
2211 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
2212 if (!BinOpCst) return SDOperand();
2213
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002214
2215 // FIXME: disable this for unless the input to the binop is a shift by a
2216 // constant. If it is not a shift, it pessimizes some common cases like:
2217 //
2218 //void foo(int *X, int i) { X[i & 1235] = 1; }
2219 //int bar(int *X, int i) { return X[i & 255]; }
2220 SDNode *BinOpLHSVal = LHS->getOperand(0).Val;
2221 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2222 BinOpLHSVal->getOpcode() != ISD::SRA &&
2223 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2224 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
2225 return SDOperand();
2226
Chris Lattnere70da202007-12-06 07:33:36 +00002227 MVT::ValueType VT = N->getValueType(0);
2228
2229 // If this is a signed shift right, and the high bit is modified
2230 // by the logical operation, do not perform the transformation.
2231 // The highBitSet boolean indicates the value of the high bit of
2232 // the constant which would cause it to be modified for this
2233 // operation.
2234 if (N->getOpcode() == ISD::SRA) {
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002235 uint64_t BinOpRHSSign = BinOpCst->getValue() >> (MVT::getSizeInBits(VT)-1);
Chris Lattnere70da202007-12-06 07:33:36 +00002236 if ((bool)BinOpRHSSign != HighBitSet)
2237 return SDOperand();
2238 }
2239
2240 // Fold the constants, shifting the binop RHS by the shift amount.
2241 SDOperand NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
2242 LHS->getOperand(1), N->getOperand(1));
2243
2244 // Create the new shift.
2245 SDOperand NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
2246 N->getOperand(1));
2247
2248 // Create the new binop.
2249 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2250}
2251
2252
Nate Begeman83e75ec2005-09-06 04:43:02 +00002253SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002254 SDOperand N0 = N->getOperand(0);
2255 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002256 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2257 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002258 MVT::ValueType VT = N0.getValueType();
2259 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2260
2261 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002262 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002263 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002264 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002265 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002266 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002267 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002268 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002269 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002270 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002271 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002272 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002273 // if (shl x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002274 if (DAG.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002275 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00002276 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002277 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002278 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002279 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002280 N0.getOperand(1).getOpcode() == ISD::Constant) {
2281 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002282 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002283 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002284 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002285 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002286 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002287 }
2288 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2289 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002290 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002291 N0.getOperand(1).getOpcode() == ISD::Constant) {
2292 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002293 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002294 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
2295 DAG.getConstant(~0ULL << c1, VT));
2296 if (c2 > c1)
2297 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002298 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002299 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002300 return DAG.getNode(ISD::SRL, VT, Mask,
2301 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002302 }
2303 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002304 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002305 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002306 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002307
2308 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002309}
2310
Nate Begeman83e75ec2005-09-06 04:43:02 +00002311SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002312 SDOperand N0 = N->getOperand(0);
2313 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002314 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2315 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002316 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002317
2318 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002319 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002320 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002321 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002322 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002323 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002324 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002325 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002326 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002327 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00002328 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002329 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002330 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002331 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002332 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002333 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2334 // sext_inreg.
2335 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
2336 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
2337 MVT::ValueType EVT;
2338 switch (LowBits) {
2339 default: EVT = MVT::Other; break;
2340 case 1: EVT = MVT::i1; break;
2341 case 8: EVT = MVT::i8; break;
2342 case 16: EVT = MVT::i16; break;
2343 case 32: EVT = MVT::i32; break;
2344 }
2345 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
2346 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2347 DAG.getValueType(EVT));
2348 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002349
2350 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2351 if (N1C && N0.getOpcode() == ISD::SRA) {
2352 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2353 unsigned Sum = N1C->getValue() + C1->getValue();
2354 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
2355 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2356 DAG.getConstant(Sum, N1C->getValueType(0)));
2357 }
2358 }
2359
Chris Lattnera8504462006-05-08 20:51:54 +00002360 // Simplify, based on bits shifted out of the LHS.
2361 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2362 return SDOperand(N, 0);
2363
2364
Nate Begeman1d4d4142005-09-01 00:19:25 +00002365 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohmanea859be2007-06-22 14:59:07 +00002366 if (DAG.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002367 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002368
2369 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002370}
2371
Nate Begeman83e75ec2005-09-06 04:43:02 +00002372SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002373 SDOperand N0 = N->getOperand(0);
2374 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002375 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2376 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002377 MVT::ValueType VT = N0.getValueType();
2378 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2379
2380 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002381 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002382 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002383 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002384 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002385 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002386 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002387 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002388 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002389 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002390 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002391 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002392 // if (srl x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002393 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002394 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002395
Nate Begeman1d4d4142005-09-01 00:19:25 +00002396 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002397 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002398 N0.getOperand(1).getOpcode() == ISD::Constant) {
2399 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002400 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002401 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002402 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002403 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002404 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002405 }
Chris Lattner350bec02006-04-02 06:11:11 +00002406
Chris Lattner06afe072006-05-05 22:53:17 +00002407 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2408 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2409 // Shifting in all undef bits?
2410 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
2411 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
2412 return DAG.getNode(ISD::UNDEF, VT);
2413
2414 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
2415 AddToWorkList(SmallShift.Val);
2416 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2417 }
2418
Chris Lattner3657ffe2006-10-12 20:23:19 +00002419 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2420 // bit, which is unmodified by sra.
2421 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
2422 if (N0.getOpcode() == ISD::SRA)
2423 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2424 }
2425
Chris Lattner350bec02006-04-02 06:11:11 +00002426 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2427 if (N1C && N0.getOpcode() == ISD::CTLZ &&
2428 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
2429 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002430 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002431
2432 // If any of the input bits are KnownOne, then the input couldn't be all
2433 // zeros, thus the result of the srl will always be zero.
2434 if (KnownOne) return DAG.getConstant(0, VT);
2435
2436 // If all of the bits input the to ctlz node are known to be zero, then
2437 // the result of the ctlz is "32" and the result of the shift is one.
2438 uint64_t UnknownBits = ~KnownZero & Mask;
2439 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2440
2441 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2442 if ((UnknownBits & (UnknownBits-1)) == 0) {
2443 // Okay, we know that only that the single bit specified by UnknownBits
2444 // could be set on input to the CTLZ node. If this bit is set, the SRL
2445 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2446 // to an SRL,XOR pair, which is likely to simplify more.
2447 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
2448 SDOperand Op = N0.getOperand(0);
2449 if (ShAmt) {
2450 Op = DAG.getNode(ISD::SRL, VT, Op,
2451 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2452 AddToWorkList(Op.Val);
2453 }
2454 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2455 }
2456 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002457
2458 // fold operands of srl based on knowledge that the low bits are not
2459 // demanded.
2460 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2461 return SDOperand(N, 0);
2462
Chris Lattnere70da202007-12-06 07:33:36 +00002463 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002464}
2465
Nate Begeman83e75ec2005-09-06 04:43:02 +00002466SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002467 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002468 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002469
2470 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002471 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002472 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002473 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002474}
2475
Nate Begeman83e75ec2005-09-06 04:43:02 +00002476SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002477 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002478 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002479
2480 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002481 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002482 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002483 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002484}
2485
Nate Begeman83e75ec2005-09-06 04:43:02 +00002486SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002487 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002488 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002489
2490 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002491 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002492 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002493 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002494}
2495
Nate Begeman452d7be2005-09-16 00:54:12 +00002496SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2497 SDOperand N0 = N->getOperand(0);
2498 SDOperand N1 = N->getOperand(1);
2499 SDOperand N2 = N->getOperand(2);
2500 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2501 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2502 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2503 MVT::ValueType VT = N->getValueType(0);
Evan Cheng571c4782007-08-18 05:57:05 +00002504 MVT::ValueType VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002505
Nate Begeman452d7be2005-09-16 00:54:12 +00002506 // fold select C, X, X -> X
2507 if (N1 == N2)
2508 return N1;
2509 // fold select true, X, Y -> X
2510 if (N0C && !N0C->isNullValue())
2511 return N1;
2512 // fold select false, X, Y -> Y
2513 if (N0C && N0C->isNullValue())
2514 return N2;
2515 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002516 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002517 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002518 // fold select C, 0, 1 -> ~C
2519 if (MVT::isInteger(VT) && MVT::isInteger(VT0) &&
2520 N1C && N2C && N1C->isNullValue() && N2C->getValue() == 1) {
2521 SDOperand XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
2522 if (VT == VT0)
2523 return XORNode;
2524 AddToWorkList(XORNode.Val);
2525 if (MVT::getSizeInBits(VT) > MVT::getSizeInBits(VT0))
2526 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2527 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2528 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002529 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002530 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
2531 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002532 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002533 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2534 }
2535 // fold select C, X, 1 -> ~C | X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002536 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getValue() == 1) {
2537 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002538 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002539 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2540 }
2541 // fold select C, X, 0 -> C & X
2542 // FIXME: this should check for C type == X type, not i1?
2543 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2544 return DAG.getNode(ISD::AND, VT, N0, N1);
2545 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2546 if (MVT::i1 == VT && N0 == N1)
2547 return DAG.getNode(ISD::OR, VT, N0, N2);
2548 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2549 if (MVT::i1 == VT && N0 == N2)
2550 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002551
Chris Lattner40c62d52005-10-18 06:04:22 +00002552 // If we can fold this based on the true/false value, do so.
2553 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002554 return SDOperand(N, 0); // Don't revisit N.
2555
Nate Begeman44728a72005-09-19 22:34:01 +00002556 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002557 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002558 // FIXME:
2559 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2560 // having to say they don't support SELECT_CC on every type the DAG knows
2561 // about, since there is no way to mark an opcode illegal at all value types
2562 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2563 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2564 N1, N2, N0.getOperand(2));
2565 else
2566 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002567 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002568 return SDOperand();
2569}
2570
2571SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002572 SDOperand N0 = N->getOperand(0);
2573 SDOperand N1 = N->getOperand(1);
2574 SDOperand N2 = N->getOperand(2);
2575 SDOperand N3 = N->getOperand(3);
2576 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002577 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2578
Nate Begeman44728a72005-09-19 22:34:01 +00002579 // fold select_cc lhs, rhs, x, x, cc -> x
2580 if (N2 == N3)
2581 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002582
Chris Lattner5f42a242006-09-20 06:19:26 +00002583 // Determine if the condition we're dealing with is constant
2584 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002585 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002586
2587 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
2588 if (SCCC->getValue())
2589 return N2; // cond always true -> true val
2590 else
2591 return N3; // cond always false -> false val
2592 }
2593
2594 // Fold to a simpler select_cc
2595 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2596 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2597 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2598 SCC.getOperand(2));
2599
Chris Lattner40c62d52005-10-18 06:04:22 +00002600 // If we can fold this based on the true/false value, do so.
2601 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002602 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002603
Nate Begeman44728a72005-09-19 22:34:01 +00002604 // fold select_cc into other things, such as min/max/abs
2605 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002606}
2607
2608SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2609 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2610 cast<CondCodeSDNode>(N->getOperand(2))->get());
2611}
2612
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002613// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2614// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2615// transformation. Returns true if extension are possible and the above
2616// mentioned transformation is profitable.
2617static bool ExtendUsesToFormExtLoad(SDNode *N, SDOperand N0,
2618 unsigned ExtOpc,
2619 SmallVector<SDNode*, 4> &ExtendNodes,
2620 TargetLowering &TLI) {
2621 bool HasCopyToRegUses = false;
2622 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
2623 for (SDNode::use_iterator UI = N0.Val->use_begin(), UE = N0.Val->use_end();
2624 UI != UE; ++UI) {
2625 SDNode *User = *UI;
2626 if (User == N)
2627 continue;
2628 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2629 if (User->getOpcode() == ISD::SETCC) {
2630 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2631 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2632 // Sign bits will be lost after a zext.
2633 return false;
2634 bool Add = false;
2635 for (unsigned i = 0; i != 2; ++i) {
2636 SDOperand UseOp = User->getOperand(i);
2637 if (UseOp == N0)
2638 continue;
2639 if (!isa<ConstantSDNode>(UseOp))
2640 return false;
2641 Add = true;
2642 }
2643 if (Add)
2644 ExtendNodes.push_back(User);
2645 } else {
2646 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2647 SDOperand UseOp = User->getOperand(i);
2648 if (UseOp == N0) {
2649 // If truncate from extended type to original load type is free
2650 // on this target, then it's ok to extend a CopyToReg.
2651 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2652 HasCopyToRegUses = true;
2653 else
2654 return false;
2655 }
2656 }
2657 }
2658 }
2659
2660 if (HasCopyToRegUses) {
2661 bool BothLiveOut = false;
2662 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2663 UI != UE; ++UI) {
2664 SDNode *User = *UI;
2665 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2666 SDOperand UseOp = User->getOperand(i);
2667 if (UseOp.Val == N && UseOp.ResNo == 0) {
2668 BothLiveOut = true;
2669 break;
2670 }
2671 }
2672 }
2673 if (BothLiveOut)
2674 // Both unextended and extended values are live out. There had better be
2675 // good a reason for the transformation.
2676 return ExtendNodes.size();
2677 }
2678 return true;
2679}
2680
Nate Begeman83e75ec2005-09-06 04:43:02 +00002681SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002682 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002683 MVT::ValueType VT = N->getValueType(0);
2684
Nate Begeman1d4d4142005-09-01 00:19:25 +00002685 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002686 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002687 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002688
Nate Begeman1d4d4142005-09-01 00:19:25 +00002689 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002690 // fold (sext (aext x)) -> (sext x)
2691 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002692 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002693
Evan Chengc88138f2007-03-22 01:54:19 +00002694 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2695 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Chris Lattner22558872007-02-26 03:13:59 +00002696 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002697 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002698 if (NarrowLoad.Val) {
2699 if (NarrowLoad.Val != N0.Val)
2700 CombineTo(N0.Val, NarrowLoad);
2701 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2702 }
Evan Chengc88138f2007-03-22 01:54:19 +00002703 }
2704
2705 // See if the value being truncated is already sign extended. If so, just
2706 // eliminate the trunc/sext pair.
2707 if (N0.getOpcode() == ISD::TRUNCATE) {
Chris Lattner6007b842006-09-21 06:00:20 +00002708 SDOperand Op = N0.getOperand(0);
Chris Lattner22558872007-02-26 03:13:59 +00002709 unsigned OpBits = MVT::getSizeInBits(Op.getValueType());
2710 unsigned MidBits = MVT::getSizeInBits(N0.getValueType());
2711 unsigned DestBits = MVT::getSizeInBits(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002712 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002713
2714 if (OpBits == DestBits) {
2715 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2716 // bits, it is already ready.
2717 if (NumSignBits > DestBits-MidBits)
2718 return Op;
2719 } else if (OpBits < DestBits) {
2720 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2721 // bits, just sext from i32.
2722 if (NumSignBits > OpBits-MidBits)
2723 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2724 } else {
2725 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2726 // bits, just truncate to i32.
2727 if (NumSignBits > OpBits-MidBits)
2728 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002729 }
Chris Lattner22558872007-02-26 03:13:59 +00002730
2731 // fold (sext (truncate x)) -> (sextinreg x).
2732 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2733 N0.getValueType())) {
2734 if (Op.getValueType() < VT)
2735 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2736 else if (Op.getValueType() > VT)
2737 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2738 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2739 DAG.getValueType(N0.getValueType()));
2740 }
Chris Lattner6007b842006-09-21 06:00:20 +00002741 }
Chris Lattner310b5782006-05-06 23:06:26 +00002742
Evan Cheng110dec22005-12-14 02:19:23 +00002743 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002744 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002745 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002746 bool DoXform = true;
2747 SmallVector<SDNode*, 4> SetCCs;
2748 if (!N0.hasOneUse())
2749 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2750 if (DoXform) {
2751 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2752 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2753 LN0->getBasePtr(), LN0->getSrcValue(),
2754 LN0->getSrcValueOffset(),
2755 N0.getValueType(),
2756 LN0->isVolatile(),
2757 LN0->getAlignment());
2758 CombineTo(N, ExtLoad);
2759 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2760 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2761 // Extend SetCC uses if necessary.
2762 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2763 SDNode *SetCC = SetCCs[i];
2764 SmallVector<SDOperand, 4> Ops;
2765 for (unsigned j = 0; j != 2; ++j) {
2766 SDOperand SOp = SetCC->getOperand(j);
2767 if (SOp == Trunc)
2768 Ops.push_back(ExtLoad);
2769 else
2770 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2771 }
2772 Ops.push_back(SetCC->getOperand(2));
2773 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2774 &Ops[0], Ops.size()));
2775 }
2776 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2777 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002778 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002779
2780 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2781 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002782 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2783 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002784 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002785 MVT::ValueType EVT = LN0->getMemoryVT();
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002786 if (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
2787 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2788 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002789 LN0->getSrcValueOffset(), EVT,
2790 LN0->isVolatile(),
2791 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002792 CombineTo(N, ExtLoad);
2793 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2794 ExtLoad.getValue(1));
2795 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2796 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002797 }
2798
Chris Lattner20a35c32007-04-11 05:32:27 +00002799 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2800 if (N0.getOpcode() == ISD::SETCC) {
2801 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002802 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2803 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2804 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2805 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002806 }
2807
Nate Begeman83e75ec2005-09-06 04:43:02 +00002808 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002809}
2810
Nate Begeman83e75ec2005-09-06 04:43:02 +00002811SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002812 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002813 MVT::ValueType VT = N->getValueType(0);
2814
Nate Begeman1d4d4142005-09-01 00:19:25 +00002815 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002816 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002817 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002818 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002819 // fold (zext (aext x)) -> (zext x)
2820 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002821 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002822
Evan Chengc88138f2007-03-22 01:54:19 +00002823 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2824 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002825 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002826 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002827 if (NarrowLoad.Val) {
2828 if (NarrowLoad.Val != N0.Val)
2829 CombineTo(N0.Val, NarrowLoad);
2830 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2831 }
Evan Chengc88138f2007-03-22 01:54:19 +00002832 }
2833
Chris Lattner6007b842006-09-21 06:00:20 +00002834 // fold (zext (truncate x)) -> (and x, mask)
2835 if (N0.getOpcode() == ISD::TRUNCATE &&
2836 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2837 SDOperand Op = N0.getOperand(0);
2838 if (Op.getValueType() < VT) {
2839 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2840 } else if (Op.getValueType() > VT) {
2841 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2842 }
2843 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2844 }
2845
Chris Lattner111c2282006-09-21 06:14:31 +00002846 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2847 if (N0.getOpcode() == ISD::AND &&
2848 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2849 N0.getOperand(1).getOpcode() == ISD::Constant) {
2850 SDOperand X = N0.getOperand(0).getOperand(0);
2851 if (X.getValueType() < VT) {
2852 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2853 } else if (X.getValueType() > VT) {
2854 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2855 }
2856 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2857 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2858 }
2859
Evan Cheng110dec22005-12-14 02:19:23 +00002860 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002861 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002862 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002863 bool DoXform = true;
2864 SmallVector<SDNode*, 4> SetCCs;
2865 if (!N0.hasOneUse())
2866 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
2867 if (DoXform) {
2868 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2869 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2870 LN0->getBasePtr(), LN0->getSrcValue(),
2871 LN0->getSrcValueOffset(),
2872 N0.getValueType(),
2873 LN0->isVolatile(),
2874 LN0->getAlignment());
2875 CombineTo(N, ExtLoad);
2876 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2877 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2878 // Extend SetCC uses if necessary.
2879 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2880 SDNode *SetCC = SetCCs[i];
2881 SmallVector<SDOperand, 4> Ops;
2882 for (unsigned j = 0; j != 2; ++j) {
2883 SDOperand SOp = SetCC->getOperand(j);
2884 if (SOp == Trunc)
2885 Ops.push_back(ExtLoad);
2886 else
Evan Chengde1631b2007-10-30 20:11:21 +00002887 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002888 }
2889 Ops.push_back(SetCC->getOperand(2));
2890 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2891 &Ops[0], Ops.size()));
2892 }
2893 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2894 }
Evan Cheng110dec22005-12-14 02:19:23 +00002895 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002896
2897 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2898 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002899 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2900 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002901 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002902 MVT::ValueType EVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002903 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2904 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002905 LN0->getSrcValueOffset(), EVT,
2906 LN0->isVolatile(),
2907 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002908 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002909 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2910 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002911 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002912 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002913
2914 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2915 if (N0.getOpcode() == ISD::SETCC) {
2916 SDOperand SCC =
2917 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2918 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00002919 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2920 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002921 }
2922
Nate Begeman83e75ec2005-09-06 04:43:02 +00002923 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002924}
2925
Chris Lattner5ffc0662006-05-05 05:58:59 +00002926SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2927 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002928 MVT::ValueType VT = N->getValueType(0);
2929
2930 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002931 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002932 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2933 // fold (aext (aext x)) -> (aext x)
2934 // fold (aext (zext x)) -> (zext x)
2935 // fold (aext (sext x)) -> (sext x)
2936 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2937 N0.getOpcode() == ISD::ZERO_EXTEND ||
2938 N0.getOpcode() == ISD::SIGN_EXTEND)
2939 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2940
Evan Chengc88138f2007-03-22 01:54:19 +00002941 // fold (aext (truncate (load x))) -> (aext (smaller load x))
2942 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
2943 if (N0.getOpcode() == ISD::TRUNCATE) {
2944 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002945 if (NarrowLoad.Val) {
2946 if (NarrowLoad.Val != N0.Val)
2947 CombineTo(N0.Val, NarrowLoad);
2948 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
2949 }
Evan Chengc88138f2007-03-22 01:54:19 +00002950 }
2951
Chris Lattner84750582006-09-20 06:29:17 +00002952 // fold (aext (truncate x))
2953 if (N0.getOpcode() == ISD::TRUNCATE) {
2954 SDOperand TruncOp = N0.getOperand(0);
2955 if (TruncOp.getValueType() == VT)
2956 return TruncOp; // x iff x size == zext size.
2957 if (TruncOp.getValueType() > VT)
2958 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2959 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2960 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002961
2962 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2963 if (N0.getOpcode() == ISD::AND &&
2964 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2965 N0.getOperand(1).getOpcode() == ISD::Constant) {
2966 SDOperand X = N0.getOperand(0).getOperand(0);
2967 if (X.getValueType() < VT) {
2968 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2969 } else if (X.getValueType() > VT) {
2970 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2971 }
2972 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2973 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2974 }
2975
Chris Lattner5ffc0662006-05-05 05:58:59 +00002976 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002977 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002978 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002979 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2980 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2981 LN0->getBasePtr(), LN0->getSrcValue(),
2982 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002983 N0.getValueType(),
2984 LN0->isVolatile(),
2985 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002986 CombineTo(N, ExtLoad);
2987 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2988 ExtLoad.getValue(1));
2989 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2990 }
2991
2992 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2993 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2994 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002995 if (N0.getOpcode() == ISD::LOAD &&
2996 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00002997 N0.hasOneUse()) {
2998 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002999 MVT::ValueType EVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00003000 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
3001 LN0->getChain(), LN0->getBasePtr(),
3002 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003003 LN0->getSrcValueOffset(), EVT,
3004 LN0->isVolatile(),
3005 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003006 CombineTo(N, ExtLoad);
3007 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3008 ExtLoad.getValue(1));
3009 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3010 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003011
3012 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3013 if (N0.getOpcode() == ISD::SETCC) {
3014 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003015 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3016 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003017 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00003018 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003019 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003020 }
3021
Chris Lattner5ffc0662006-05-05 05:58:59 +00003022 return SDOperand();
3023}
3024
Chris Lattner2b4c2792007-10-13 06:35:54 +00003025/// GetDemandedBits - See if the specified operand can be simplified with the
3026/// knowledge that only the bits specified by Mask are used. If so, return the
3027/// simpler operand, otherwise return a null SDOperand.
3028SDOperand DAGCombiner::GetDemandedBits(SDOperand V, uint64_t Mask) {
3029 switch (V.getOpcode()) {
3030 default: break;
3031 case ISD::OR:
3032 case ISD::XOR:
3033 // If the LHS or RHS don't contribute bits to the or, drop them.
3034 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3035 return V.getOperand(1);
3036 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3037 return V.getOperand(0);
3038 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003039 case ISD::SRL:
3040 // Only look at single-use SRLs.
3041 if (!V.Val->hasOneUse())
3042 break;
3043 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3044 // See if we can recursively simplify the LHS.
3045 unsigned Amt = RHSC->getValue();
3046 Mask = (Mask << Amt) & MVT::getIntVTBitMask(V.getValueType());
3047 SDOperand SimplifyLHS = GetDemandedBits(V.getOperand(0), Mask);
3048 if (SimplifyLHS.Val) {
3049 return DAG.getNode(ISD::SRL, V.getValueType(),
3050 SimplifyLHS, V.getOperand(1));
3051 }
3052 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003053 }
3054 return SDOperand();
3055}
3056
Evan Chengc88138f2007-03-22 01:54:19 +00003057/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3058/// bits and then truncated to a narrower type and where N is a multiple
3059/// of number of bits of the narrower type, transform it to a narrower load
3060/// from address + N / num of bits of new type. If the result is to be
3061/// extended, also fold the extension to form a extending load.
3062SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
3063 unsigned Opc = N->getOpcode();
3064 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
3065 SDOperand N0 = N->getOperand(0);
3066 MVT::ValueType VT = N->getValueType(0);
3067 MVT::ValueType EVT = N->getValueType(0);
3068
Evan Chenge177e302007-03-23 22:13:36 +00003069 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3070 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003071 if (Opc == ISD::SIGN_EXTEND_INREG) {
3072 ExtType = ISD::SEXTLOAD;
3073 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00003074 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
3075 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00003076 }
3077
3078 unsigned EVTBits = MVT::getSizeInBits(EVT);
3079 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003080 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003081 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3082 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3083 ShAmt = N01->getValue();
3084 // Is the shift amount a multiple of size of VT?
3085 if ((ShAmt & (EVTBits-1)) == 0) {
3086 N0 = N0.getOperand(0);
3087 if (MVT::getSizeInBits(N0.getValueType()) <= EVTBits)
3088 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00003089 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003090 }
3091 }
3092 }
3093
3094 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
3095 // Do not allow folding to i1 here. i1 is implicitly stored in memory in
3096 // zero extended form: by shrinking the load, we lose track of the fact
3097 // that it is already zero extended.
3098 // FIXME: This should be reevaluated.
3099 VT != MVT::i1) {
3100 assert(MVT::getSizeInBits(N0.getValueType()) > EVTBits &&
3101 "Cannot truncate to larger type!");
3102 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3103 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003104 // For big endian targets, we need to adjust the offset to the pointer to
3105 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003106 if (TLI.isBigEndian()) {
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003107 unsigned LVTStoreBits = MVT::getStoreSizeInBits(N0.getValueType());
3108 unsigned EVTStoreBits = MVT::getStoreSizeInBits(EVT);
3109 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3110 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003111 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003112 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Evan Chengc88138f2007-03-22 01:54:19 +00003113 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
3114 DAG.getConstant(PtrOff, PtrType));
3115 AddToWorkList(NewPtr.Val);
3116 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
3117 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003118 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Duncan Sandsdc846502007-10-28 12:59:45 +00003119 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003120 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003121 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00003122 LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003123 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003124 if (CombineSRL) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003125 WorkListRemover DeadNodes(*this);
3126 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3127 &DeadNodes);
Evan Chengb37b80c2007-03-23 20:55:21 +00003128 CombineTo(N->getOperand(0).Val, Load);
3129 } else
3130 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003131 if (ShAmt) {
3132 if (Opc == ISD::SIGN_EXTEND_INREG)
3133 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3134 else
3135 return DAG.getNode(Opc, VT, Load);
3136 }
Evan Chengc88138f2007-03-22 01:54:19 +00003137 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3138 }
3139
3140 return SDOperand();
3141}
3142
Chris Lattner5ffc0662006-05-05 05:58:59 +00003143
Nate Begeman83e75ec2005-09-06 04:43:02 +00003144SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003145 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003146 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003147 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003148 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00003149 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003150
Nate Begeman1d4d4142005-09-01 00:19:25 +00003151 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003152 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003153 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003154
Chris Lattner541a24f2006-05-06 22:43:44 +00003155 // If the input is already sign extended, just drop the extension.
Dan Gohmanea859be2007-06-22 14:59:07 +00003156 if (DAG.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003157 return N0;
3158
Nate Begeman646d7e22005-09-02 21:18:40 +00003159 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3160 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3161 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003162 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003163 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003164
Chris Lattner95a5e052007-04-17 19:03:21 +00003165 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohmanea859be2007-06-22 14:59:07 +00003166 if (DAG.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00003167 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003168
Chris Lattner95a5e052007-04-17 19:03:21 +00003169 // fold operands of sext_in_reg based on knowledge that the top bits are not
3170 // demanded.
3171 if (SimplifyDemandedBits(SDOperand(N, 0)))
3172 return SDOperand(N, 0);
3173
Evan Chengc88138f2007-03-22 01:54:19 +00003174 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3175 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
3176 SDOperand NarrowLoad = ReduceLoadWidth(N);
3177 if (NarrowLoad.Val)
3178 return NarrowLoad;
3179
Chris Lattner4b37e872006-05-08 21:18:59 +00003180 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3181 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3182 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3183 if (N0.getOpcode() == ISD::SRL) {
3184 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
3185 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
3186 // We can turn this into an SRA iff the input to the SRL is already sign
3187 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003188 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Chris Lattner4b37e872006-05-08 21:18:59 +00003189 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
3190 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3191 }
3192 }
Evan Chengc88138f2007-03-22 01:54:19 +00003193
Nate Begemanded49632005-10-13 03:11:28 +00003194 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00003195 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003196 ISD::isUNINDEXEDLoad(N0.Val) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003197 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003198 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003199 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3200 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3201 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003202 LN0->getSrcValueOffset(), EVT,
3203 LN0->isVolatile(),
3204 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003205 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003206 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003207 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003208 }
3209 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00003210 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
3211 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003212 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003213 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003214 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3215 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3216 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003217 LN0->getSrcValueOffset(), EVT,
3218 LN0->isVolatile(),
3219 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003220 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003221 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003222 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003223 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003224 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003225}
3226
Nate Begeman83e75ec2005-09-06 04:43:02 +00003227SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003228 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003229 MVT::ValueType VT = N->getValueType(0);
3230
3231 // noop truncate
3232 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003233 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003234 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003235 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003236 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003237 // fold (truncate (truncate x)) -> (truncate x)
3238 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003239 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003240 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003241 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3242 N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003243 if (N0.getOperand(0).getValueType() < VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003244 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003245 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003246 else if (N0.getOperand(0).getValueType() > VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003247 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003248 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003249 else
3250 // if the source and dest are the same type, we can drop both the extend
3251 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003252 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003253 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003254
Chris Lattner2b4c2792007-10-13 06:35:54 +00003255 // See if we can simplify the input to this truncate through knowledge that
3256 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3257 // -> trunc y
3258 SDOperand Shorter = GetDemandedBits(N0, MVT::getIntVTBitMask(VT));
3259 if (Shorter.Val)
3260 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3261
Nate Begeman3df4d522005-10-12 20:40:40 +00003262 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003263 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003264 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003265}
3266
Chris Lattner94683772005-12-23 05:30:37 +00003267SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3268 SDOperand N0 = N->getOperand(0);
3269 MVT::ValueType VT = N->getValueType(0);
3270
Dan Gohman7f321562007-06-25 16:23:39 +00003271 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3272 // Only do this before legalize, since afterward the target may be depending
3273 // on the bitconvert.
3274 // First check to see if this is all constant.
3275 if (!AfterLegalize &&
3276 N0.getOpcode() == ISD::BUILD_VECTOR && N0.Val->hasOneUse() &&
3277 MVT::isVector(VT)) {
3278 bool isSimple = true;
3279 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3280 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3281 N0.getOperand(i).getOpcode() != ISD::Constant &&
3282 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3283 isSimple = false;
3284 break;
3285 }
3286
3287 MVT::ValueType DestEltVT = MVT::getVectorElementType(N->getValueType(0));
3288 assert(!MVT::isVector(DestEltVT) &&
3289 "Element type of vector ValueType must not be vector!");
3290 if (isSimple) {
3291 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.Val, DestEltVT);
3292 }
3293 }
3294
Chris Lattner94683772005-12-23 05:30:37 +00003295 // If the input is a constant, let getNode() fold it.
3296 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
3297 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3298 if (Res.Val != N) return Res;
3299 }
3300
Chris Lattnerc8547d82005-12-23 05:37:50 +00003301 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3302 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003303
Chris Lattner57104102005-12-23 05:44:41 +00003304 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003305 // If the resultant load doesn't need a higher alignment than the original!
3306 if (ISD::isNormalLoad(N0.Val) && N0.hasOneUse() &&
Evan Cheng59d5b682007-05-07 21:27:48 +00003307 TLI.isOperationLegal(ISD::LOAD, VT)) {
Evan Cheng466685d2006-10-09 20:57:25 +00003308 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00003309 unsigned Align = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00003310 getABITypeAlignment(MVT::getTypeForValueType(VT));
Evan Cheng59d5b682007-05-07 21:27:48 +00003311 unsigned OrigAlign = LN0->getAlignment();
3312 if (Align <= OrigAlign) {
3313 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
3314 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003315 LN0->isVolatile(), Align);
Evan Cheng59d5b682007-05-07 21:27:48 +00003316 AddToWorkList(N);
3317 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
3318 Load.getValue(1));
3319 return Load;
3320 }
Chris Lattner57104102005-12-23 05:44:41 +00003321 }
3322
Chris Lattner3bd39d42008-01-27 17:42:27 +00003323 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3324 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3325 // This often reduces constant pool loads.
3326 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
3327 N0.Val->hasOneUse() && MVT::isInteger(VT) && !MVT::isVector(VT)) {
3328 SDOperand NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3329 AddToWorkList(NewConv.Val);
3330
3331 uint64_t SignBit = MVT::getIntVTSignBit(VT);
3332 if (N0.getOpcode() == ISD::FNEG)
3333 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3334 assert(N0.getOpcode() == ISD::FABS);
3335 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3336 }
3337
3338 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3339 // Note that we don't handle copysign(x,cst) because this can always be folded
3340 // to an fneg or fabs.
3341 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003342 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
3343 MVT::isInteger(VT) && !MVT::isVector(VT)) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003344 unsigned OrigXWidth = MVT::getSizeInBits(N0.getOperand(1).getValueType());
3345 SDOperand X = DAG.getNode(ISD::BIT_CONVERT, MVT::getIntegerType(OrigXWidth),
3346 N0.getOperand(1));
3347 AddToWorkList(X.Val);
3348
3349 // If X has a different width than the result/lhs, sext it or truncate it.
3350 unsigned VTWidth = MVT::getSizeInBits(VT);
3351 if (OrigXWidth < VTWidth) {
3352 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
3353 AddToWorkList(X.Val);
3354 } else if (OrigXWidth > VTWidth) {
3355 // To get the sign bit in the right place, we have to shift it right
3356 // before truncating.
3357 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3358 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3359 AddToWorkList(X.Val);
3360 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3361 AddToWorkList(X.Val);
3362 }
3363
3364 uint64_t SignBit = MVT::getIntVTSignBit(VT);
3365 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
3366 AddToWorkList(X.Val);
3367
3368 SDOperand Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3369 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
3370 AddToWorkList(Cst.Val);
3371
3372 return DAG.getNode(ISD::OR, VT, X, Cst);
3373 }
3374
Chris Lattner94683772005-12-23 05:30:37 +00003375 return SDOperand();
3376}
3377
Dan Gohman7f321562007-06-25 16:23:39 +00003378/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003379/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3380/// destination element value type.
3381SDOperand DAGCombiner::
Dan Gohman7f321562007-06-25 16:23:39 +00003382ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003383 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
3384
3385 // If this is already the right type, we're done.
3386 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
3387
3388 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
3389 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
3390
3391 // If this is a conversion of N elements of one type to N elements of another
3392 // type, convert each element. This handles FP<->INT cases.
3393 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003394 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003395 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003396 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00003397 AddToWorkList(Ops.back().Val);
3398 }
Dan Gohman7f321562007-06-25 16:23:39 +00003399 MVT::ValueType VT =
3400 MVT::getVectorType(DstEltVT,
3401 MVT::getVectorNumElements(BV->getValueType(0)));
3402 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003403 }
3404
3405 // Otherwise, we're growing or shrinking the elements. To avoid having to
3406 // handle annoying details of growing/shrinking FP values, we convert them to
3407 // int first.
3408 if (MVT::isFloatingPoint(SrcEltVT)) {
3409 // Convert the input float vector to a int vector where the elements are the
3410 // same sizes.
3411 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
3412 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003413 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003414 SrcEltVT = IntVT;
3415 }
3416
3417 // Now we know the input is an integer vector. If the output is a FP type,
3418 // convert to integer first, then to FP of the right size.
3419 if (MVT::isFloatingPoint(DstEltVT)) {
3420 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
3421 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003422 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003423
3424 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003425 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003426 }
3427
3428 // Okay, we know the src/dst types are both integers of differing types.
3429 // Handling growing first.
3430 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
3431 if (SrcBitSize < DstBitSize) {
3432 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3433
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003434 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003435 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003436 i += NumInputsPerOutput) {
3437 bool isLE = TLI.isLittleEndian();
3438 uint64_t NewBits = 0;
3439 bool EltIsUndef = true;
3440 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3441 // Shift the previously computed bits over.
3442 NewBits <<= SrcBitSize;
3443 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
3444 if (Op.getOpcode() == ISD::UNDEF) continue;
3445 EltIsUndef = false;
3446
3447 NewBits |= cast<ConstantSDNode>(Op)->getValue();
3448 }
3449
3450 if (EltIsUndef)
3451 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3452 else
3453 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3454 }
3455
Evan Chengefec7512008-02-18 23:04:32 +00003456 MVT::ValueType VT = MVT::getVectorType(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003457 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003458 }
3459
3460 // Finally, this must be the case where we are shrinking elements: each input
3461 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003462 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003463 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Evan Chengefec7512008-02-18 23:04:32 +00003464 MVT::ValueType VT = MVT::getVectorType(DstEltVT,
3465 NumOutputsPerInput * BV->getNumOperands());
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003466 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003467 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003468 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3469 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3470 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3471 continue;
3472 }
3473 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003474 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
3475 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
Chris Lattner6258fb22006-04-02 02:53:43 +00003476 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Evan Chengefec7512008-02-18 23:04:32 +00003477 if (isS2V && i == 0 && j == 0 && ThisVal == OpVal)
3478 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3479 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
3480 OpVal >>= DstBitSize;
Chris Lattner6258fb22006-04-02 02:53:43 +00003481 }
3482
3483 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003484 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003485 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3486 }
Dan Gohman7f321562007-06-25 16:23:39 +00003487 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003488}
3489
3490
3491
Chris Lattner01b3d732005-09-28 22:28:18 +00003492SDOperand DAGCombiner::visitFADD(SDNode *N) {
3493 SDOperand N0 = N->getOperand(0);
3494 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003495 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3496 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003497 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003498
Dan Gohman7f321562007-06-25 16:23:39 +00003499 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003500 if (MVT::isVector(VT)) {
3501 SDOperand FoldedVOp = SimplifyVBinOp(N);
3502 if (FoldedVOp.Val) return FoldedVOp;
3503 }
Dan Gohman7f321562007-06-25 16:23:39 +00003504
Nate Begemana0e221d2005-10-18 00:28:13 +00003505 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003506 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003507 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003508 // canonicalize constant to RHS
3509 if (N0CFP && !N1CFP)
3510 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003511 // fold (A + (-B)) -> A-B
Chris Lattner29446522007-05-14 22:04:50 +00003512 if (isNegatibleForFree(N1) == 2)
3513 return DAG.getNode(ISD::FSUB, VT, N0, GetNegatedExpression(N1, DAG));
Chris Lattner01b3d732005-09-28 22:28:18 +00003514 // fold ((-A) + B) -> B-A
Chris Lattner29446522007-05-14 22:04:50 +00003515 if (isNegatibleForFree(N0) == 2)
3516 return DAG.getNode(ISD::FSUB, VT, N1, GetNegatedExpression(N0, DAG));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003517
3518 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3519 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
3520 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3521 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3522 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3523
Chris Lattner01b3d732005-09-28 22:28:18 +00003524 return SDOperand();
3525}
3526
3527SDOperand DAGCombiner::visitFSUB(SDNode *N) {
3528 SDOperand N0 = N->getOperand(0);
3529 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003530 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3531 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003532 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003533
Dan Gohman7f321562007-06-25 16:23:39 +00003534 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003535 if (MVT::isVector(VT)) {
3536 SDOperand FoldedVOp = SimplifyVBinOp(N);
3537 if (FoldedVOp.Val) return FoldedVOp;
3538 }
Dan Gohman7f321562007-06-25 16:23:39 +00003539
Nate Begemana0e221d2005-10-18 00:28:13 +00003540 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003541 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003542 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003543 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003544 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Dan Gohman23ff1822007-07-02 15:48:56 +00003545 if (isNegatibleForFree(N1))
3546 return GetNegatedExpression(N1, DAG);
3547 return DAG.getNode(ISD::FNEG, VT, N1);
3548 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003549 // fold (A-(-B)) -> A+B
Chris Lattner29446522007-05-14 22:04:50 +00003550 if (isNegatibleForFree(N1))
3551 return DAG.getNode(ISD::FADD, VT, N0, GetNegatedExpression(N1, DAG));
3552
Chris Lattner01b3d732005-09-28 22:28:18 +00003553 return SDOperand();
3554}
3555
3556SDOperand DAGCombiner::visitFMUL(SDNode *N) {
3557 SDOperand N0 = N->getOperand(0);
3558 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003559 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3560 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003561 MVT::ValueType VT = N->getValueType(0);
3562
Dan Gohman7f321562007-06-25 16:23:39 +00003563 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003564 if (MVT::isVector(VT)) {
3565 SDOperand FoldedVOp = SimplifyVBinOp(N);
3566 if (FoldedVOp.Val) return FoldedVOp;
3567 }
Dan Gohman7f321562007-06-25 16:23:39 +00003568
Nate Begeman11af4ea2005-10-17 20:40:11 +00003569 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003570 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003571 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003572 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003573 if (N0CFP && !N1CFP)
3574 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003575 // fold (fmul X, 2.0) -> (fadd X, X)
3576 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3577 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003578 // fold (fmul X, -1.0) -> (fneg X)
3579 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3580 return DAG.getNode(ISD::FNEG, VT, N0);
3581
3582 // -X * -Y -> X*Y
3583 if (char LHSNeg = isNegatibleForFree(N0)) {
3584 if (char RHSNeg = isNegatibleForFree(N1)) {
3585 // Both can be negated for free, check to see if at least one is cheaper
3586 // negated.
3587 if (LHSNeg == 2 || RHSNeg == 2)
3588 return DAG.getNode(ISD::FMUL, VT, GetNegatedExpression(N0, DAG),
3589 GetNegatedExpression(N1, DAG));
3590 }
3591 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003592
3593 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3594 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
3595 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3596 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3597 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3598
Chris Lattner01b3d732005-09-28 22:28:18 +00003599 return SDOperand();
3600}
3601
3602SDOperand DAGCombiner::visitFDIV(SDNode *N) {
3603 SDOperand N0 = N->getOperand(0);
3604 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003605 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3606 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003607 MVT::ValueType VT = N->getValueType(0);
3608
Dan Gohman7f321562007-06-25 16:23:39 +00003609 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003610 if (MVT::isVector(VT)) {
3611 SDOperand FoldedVOp = SimplifyVBinOp(N);
3612 if (FoldedVOp.Val) return FoldedVOp;
3613 }
Dan Gohman7f321562007-06-25 16:23:39 +00003614
Nate Begemana148d982006-01-18 22:35:16 +00003615 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003616 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003617 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003618
3619
3620 // -X / -Y -> X*Y
3621 if (char LHSNeg = isNegatibleForFree(N0)) {
3622 if (char RHSNeg = isNegatibleForFree(N1)) {
3623 // Both can be negated for free, check to see if at least one is cheaper
3624 // negated.
3625 if (LHSNeg == 2 || RHSNeg == 2)
3626 return DAG.getNode(ISD::FDIV, VT, GetNegatedExpression(N0, DAG),
3627 GetNegatedExpression(N1, DAG));
3628 }
3629 }
3630
Chris Lattner01b3d732005-09-28 22:28:18 +00003631 return SDOperand();
3632}
3633
3634SDOperand DAGCombiner::visitFREM(SDNode *N) {
3635 SDOperand N0 = N->getOperand(0);
3636 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003637 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3638 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003639 MVT::ValueType VT = N->getValueType(0);
3640
Nate Begemana148d982006-01-18 22:35:16 +00003641 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003642 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003643 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003644
Chris Lattner01b3d732005-09-28 22:28:18 +00003645 return SDOperand();
3646}
3647
Chris Lattner12d83032006-03-05 05:30:57 +00003648SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3649 SDOperand N0 = N->getOperand(0);
3650 SDOperand N1 = N->getOperand(1);
3651 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3652 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3653 MVT::ValueType VT = N->getValueType(0);
3654
Dale Johannesendb44bf82007-10-16 23:38:29 +00003655 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003656 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3657
3658 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003659 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003660 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3661 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003662 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003663 return DAG.getNode(ISD::FABS, VT, N0);
3664 else
3665 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3666 }
3667
3668 // copysign(fabs(x), y) -> copysign(x, y)
3669 // copysign(fneg(x), y) -> copysign(x, y)
3670 // copysign(copysign(x,z), y) -> copysign(x, y)
3671 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3672 N0.getOpcode() == ISD::FCOPYSIGN)
3673 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3674
3675 // copysign(x, abs(y)) -> abs(x)
3676 if (N1.getOpcode() == ISD::FABS)
3677 return DAG.getNode(ISD::FABS, VT, N0);
3678
3679 // copysign(x, copysign(y,z)) -> copysign(x, z)
3680 if (N1.getOpcode() == ISD::FCOPYSIGN)
3681 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3682
3683 // copysign(x, fp_extend(y)) -> copysign(x, y)
3684 // copysign(x, fp_round(y)) -> copysign(x, y)
3685 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3686 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3687
3688 return SDOperand();
3689}
3690
3691
Chris Lattner01b3d732005-09-28 22:28:18 +00003692
Nate Begeman83e75ec2005-09-06 04:43:02 +00003693SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003694 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003695 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003696 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003697
3698 // fold (sint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003699 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003700 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003701 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003702}
3703
Nate Begeman83e75ec2005-09-06 04:43:02 +00003704SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003705 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003706 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003707 MVT::ValueType VT = N->getValueType(0);
3708
Nate Begeman1d4d4142005-09-01 00:19:25 +00003709 // fold (uint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003710 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003711 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003712 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003713}
3714
Nate Begeman83e75ec2005-09-06 04:43:02 +00003715SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003716 SDOperand N0 = N->getOperand(0);
3717 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3718 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003719
3720 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003721 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003722 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003723 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003724}
3725
Nate Begeman83e75ec2005-09-06 04:43:02 +00003726SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003727 SDOperand N0 = N->getOperand(0);
3728 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3729 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003730
3731 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00003732 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003733 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003734 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003735}
3736
Nate Begeman83e75ec2005-09-06 04:43:02 +00003737SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003738 SDOperand N0 = N->getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003739 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003740 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3741 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003742
3743 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003744 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00003745 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003746
3747 // fold (fp_round (fp_extend x)) -> x
3748 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3749 return N0.getOperand(0);
3750
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00003751 // fold (fp_round (fp_round x)) -> (fp_round x)
3752 if (N0.getOpcode() == ISD::FP_ROUND) {
3753 // This is a value preserving truncation if both round's are.
3754 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
3755 N0.Val->getConstantOperandVal(1) == 1;
3756 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
3757 DAG.getIntPtrConstant(IsTrunc));
3758 }
3759
Chris Lattner79dbea52006-03-13 06:26:26 +00003760 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3761 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003762 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003763 AddToWorkList(Tmp.Val);
3764 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3765 }
3766
Nate Begeman83e75ec2005-09-06 04:43:02 +00003767 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003768}
3769
Nate Begeman83e75ec2005-09-06 04:43:02 +00003770SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003771 SDOperand N0 = N->getOperand(0);
3772 MVT::ValueType VT = N->getValueType(0);
3773 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003774 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003775
Nate Begeman1d4d4142005-09-01 00:19:25 +00003776 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003777 if (N0CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003778 SDOperand Round = DAG.getConstantFP(N0CFP->getValueAPF(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003779 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003780 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003781 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003782}
3783
Nate Begeman83e75ec2005-09-06 04:43:02 +00003784SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003785 SDOperand N0 = N->getOperand(0);
3786 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3787 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003788
Chris Lattner5938bef2007-12-29 06:55:23 +00003789 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
3790 if (N->hasOneUse() && (*N->use_begin())->getOpcode() == ISD::FP_ROUND)
3791 return SDOperand();
Chris Lattner0bd48932008-01-17 07:00:52 +00003792
Nate Begeman1d4d4142005-09-01 00:19:25 +00003793 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003794 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003795 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003796
3797 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
3798 // value of X.
3799 if (N0.getOpcode() == ISD::FP_ROUND && N0.Val->getConstantOperandVal(1) == 1){
3800 SDOperand In = N0.getOperand(0);
3801 if (In.getValueType() == VT) return In;
3802 if (VT < In.getValueType())
3803 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
3804 return DAG.getNode(ISD::FP_EXTEND, VT, In);
3805 }
3806
3807 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Dale Johannesenb6210fc2007-10-19 20:29:00 +00003808 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003809 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003810 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3811 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3812 LN0->getBasePtr(), LN0->getSrcValue(),
3813 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003814 N0.getValueType(),
3815 LN0->isVolatile(),
3816 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003817 CombineTo(N, ExtLoad);
Chris Lattner0bd48932008-01-17 07:00:52 +00003818 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad,
3819 DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00003820 ExtLoad.getValue(1));
3821 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3822 }
3823
3824
Nate Begeman83e75ec2005-09-06 04:43:02 +00003825 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003826}
3827
Nate Begeman83e75ec2005-09-06 04:43:02 +00003828SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003829 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003830
Dan Gohman23ff1822007-07-02 15:48:56 +00003831 if (isNegatibleForFree(N0))
3832 return GetNegatedExpression(N0, DAG);
3833
Chris Lattner3bd39d42008-01-27 17:42:27 +00003834 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
3835 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00003836 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
3837 MVT::isInteger(N0.getOperand(0).getValueType()) &&
3838 !MVT::isVector(N0.getOperand(0).getValueType())) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003839 SDOperand Int = N0.getOperand(0);
3840 MVT::ValueType IntVT = Int.getValueType();
3841 if (MVT::isInteger(IntVT) && !MVT::isVector(IntVT)) {
3842 Int = DAG.getNode(ISD::XOR, IntVT, Int,
3843 DAG.getConstant(MVT::getIntVTSignBit(IntVT), IntVT));
3844 AddToWorkList(Int.Val);
3845 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
3846 }
3847 }
3848
Nate Begeman83e75ec2005-09-06 04:43:02 +00003849 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003850}
3851
Nate Begeman83e75ec2005-09-06 04:43:02 +00003852SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003853 SDOperand N0 = N->getOperand(0);
3854 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3855 MVT::ValueType VT = N->getValueType(0);
3856
Nate Begeman1d4d4142005-09-01 00:19:25 +00003857 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003858 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003859 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003860 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003861 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003862 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003863 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003864 // fold (fabs (fcopysign x, y)) -> (fabs x)
3865 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
3866 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
3867
Chris Lattner3bd39d42008-01-27 17:42:27 +00003868 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
3869 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00003870 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
3871 MVT::isInteger(N0.getOperand(0).getValueType()) &&
3872 !MVT::isVector(N0.getOperand(0).getValueType())) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003873 SDOperand Int = N0.getOperand(0);
3874 MVT::ValueType IntVT = Int.getValueType();
3875 if (MVT::isInteger(IntVT) && !MVT::isVector(IntVT)) {
3876 Int = DAG.getNode(ISD::AND, IntVT, Int,
3877 DAG.getConstant(~MVT::getIntVTSignBit(IntVT), IntVT));
3878 AddToWorkList(Int.Val);
3879 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
3880 }
3881 }
3882
Nate Begeman83e75ec2005-09-06 04:43:02 +00003883 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003884}
3885
Nate Begeman44728a72005-09-19 22:34:01 +00003886SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
3887 SDOperand Chain = N->getOperand(0);
3888 SDOperand N1 = N->getOperand(1);
3889 SDOperand N2 = N->getOperand(2);
3890 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3891
3892 // never taken branch, fold to chain
3893 if (N1C && N1C->isNullValue())
3894 return Chain;
3895 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00003896 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00003897 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003898 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
3899 // on the target.
3900 if (N1.getOpcode() == ISD::SETCC &&
3901 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
3902 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
3903 N1.getOperand(0), N1.getOperand(1), N2);
3904 }
Nate Begeman44728a72005-09-19 22:34:01 +00003905 return SDOperand();
3906}
3907
Chris Lattner3ea0b472005-10-05 06:47:48 +00003908// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
3909//
Nate Begeman44728a72005-09-19 22:34:01 +00003910SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00003911 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
3912 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
3913
3914 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00003915 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003916 if (Simp.Val) AddToWorkList(Simp.Val);
3917
Nate Begemane17daeb2005-10-05 21:43:42 +00003918 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
3919
3920 // fold br_cc true, dest -> br dest (unconditional branch)
3921 if (SCCC && SCCC->getValue())
3922 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
3923 N->getOperand(4));
3924 // fold br_cc false, dest -> unconditional fall through
3925 if (SCCC && SCCC->isNullValue())
3926 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00003927
Nate Begemane17daeb2005-10-05 21:43:42 +00003928 // fold to a simpler setcc
3929 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
3930 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
3931 Simp.getOperand(2), Simp.getOperand(0),
3932 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00003933 return SDOperand();
3934}
3935
Chris Lattner448f2192006-11-11 00:39:41 +00003936
3937/// CombineToPreIndexedLoadStore - Try turning a load / store and a
3938/// pre-indexed load / store when the base pointer is a add or subtract
3939/// and it has other uses besides the load / store. After the
3940/// transformation, the new indexed load / store has effectively folded
3941/// the add / subtract in and all of its other uses are redirected to the
3942/// new load / store.
3943bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
3944 if (!AfterLegalize)
3945 return false;
3946
3947 bool isLoad = true;
3948 SDOperand Ptr;
3949 MVT::ValueType VT;
3950 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00003951 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00003952 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003953 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00003954 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00003955 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
3956 return false;
3957 Ptr = LD->getBasePtr();
3958 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00003959 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00003960 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003961 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00003962 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
3963 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
3964 return false;
3965 Ptr = ST->getBasePtr();
3966 isLoad = false;
3967 } else
3968 return false;
3969
Chris Lattner9f1794e2006-11-11 00:56:29 +00003970 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
3971 // out. There is no reason to make this a preinc/predec.
3972 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
3973 Ptr.Val->hasOneUse())
3974 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003975
Chris Lattner9f1794e2006-11-11 00:56:29 +00003976 // Ask the target to do addressing mode selection.
3977 SDOperand BasePtr;
3978 SDOperand Offset;
3979 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3980 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
3981 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00003982 // Don't create a indexed load / store with zero offset.
3983 if (isa<ConstantSDNode>(Offset) &&
3984 cast<ConstantSDNode>(Offset)->getValue() == 0)
3985 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00003986
Chris Lattner41e53fd2006-11-11 01:00:15 +00003987 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00003988 // 1) The new base ptr is a frame index.
3989 // 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 +00003990 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00003991 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00003992 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00003993 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00003994
Chris Lattner41e53fd2006-11-11 01:00:15 +00003995 // Check #1. Preinc'ing a frame index would require copying the stack pointer
3996 // (plus the implicit offset) to a register to preinc anyway.
3997 if (isa<FrameIndexSDNode>(BasePtr))
3998 return false;
3999
4000 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004001 if (!isLoad) {
4002 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Chengc843abe2007-05-24 02:35:39 +00004003 if (Val == BasePtr || BasePtr.Val->isPredecessor(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004004 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004005 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004006
Evan Chengc843abe2007-05-24 02:35:39 +00004007 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004008 bool RealUse = false;
4009 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4010 E = Ptr.Val->use_end(); I != E; ++I) {
4011 SDNode *Use = *I;
4012 if (Use == N)
4013 continue;
4014 if (Use->isPredecessor(N))
4015 return false;
4016
4017 if (!((Use->getOpcode() == ISD::LOAD &&
4018 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004019 (Use->getOpcode() == ISD::STORE &&
4020 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004021 RealUse = true;
4022 }
4023 if (!RealUse)
4024 return false;
4025
4026 SDOperand Result;
4027 if (isLoad)
4028 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
4029 else
4030 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4031 ++PreIndexedNodes;
4032 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004033 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004034 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4035 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004036 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004037 if (isLoad) {
4038 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004039 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004040 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004041 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004042 } else {
4043 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004044 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004045 }
4046
Chris Lattner9f1794e2006-11-11 00:56:29 +00004047 // Finally, since the node is now dead, remove it from the graph.
4048 DAG.DeleteNode(N);
4049
4050 // Replace the uses of Ptr with uses of the updated base value.
4051 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004052 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004053 removeFromWorkList(Ptr.Val);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004054 DAG.DeleteNode(Ptr.Val);
4055
4056 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004057}
4058
4059/// CombineToPostIndexedLoadStore - Try combine a load / store with a
4060/// add / sub of the base pointer node into a post-indexed load / store.
4061/// The transformation folded the add / subtract into the new indexed
4062/// load / store effectively and all of its uses are redirected to the
4063/// new load / store.
4064bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
4065 if (!AfterLegalize)
4066 return false;
4067
4068 bool isLoad = true;
4069 SDOperand Ptr;
4070 MVT::ValueType VT;
4071 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004072 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004073 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004074 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004075 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4076 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4077 return false;
4078 Ptr = LD->getBasePtr();
4079 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004080 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004081 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004082 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004083 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4084 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4085 return false;
4086 Ptr = ST->getBasePtr();
4087 isLoad = false;
4088 } else
4089 return false;
4090
Evan Chengcc470212006-11-16 00:08:20 +00004091 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004092 return false;
4093
4094 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4095 E = Ptr.Val->use_end(); I != E; ++I) {
4096 SDNode *Op = *I;
4097 if (Op == N ||
4098 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4099 continue;
4100
4101 SDOperand BasePtr;
4102 SDOperand Offset;
4103 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4104 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4105 if (Ptr == Offset)
4106 std::swap(BasePtr, Offset);
4107 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004108 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004109 // Don't create a indexed load / store with zero offset.
4110 if (isa<ConstantSDNode>(Offset) &&
4111 cast<ConstantSDNode>(Offset)->getValue() == 0)
4112 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004113
Chris Lattner9f1794e2006-11-11 00:56:29 +00004114 // Try turning it into a post-indexed load / store except when
4115 // 1) All uses are load / store ops that use it as base ptr.
4116 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4117 // nor a successor of N. Otherwise, if Op is folded that would
4118 // create a cycle.
4119
4120 // Check for #1.
4121 bool TryNext = false;
4122 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
4123 EE = BasePtr.Val->use_end(); II != EE; ++II) {
4124 SDNode *Use = *II;
4125 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00004126 continue;
4127
Chris Lattner9f1794e2006-11-11 00:56:29 +00004128 // If all the uses are load / store addresses, then don't do the
4129 // transformation.
4130 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4131 bool RealUse = false;
4132 for (SDNode::use_iterator III = Use->use_begin(),
4133 EEE = Use->use_end(); III != EEE; ++III) {
4134 SDNode *UseUse = *III;
4135 if (!((UseUse->getOpcode() == ISD::LOAD &&
4136 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004137 (UseUse->getOpcode() == ISD::STORE &&
4138 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004139 RealUse = true;
4140 }
Chris Lattner448f2192006-11-11 00:39:41 +00004141
Chris Lattner9f1794e2006-11-11 00:56:29 +00004142 if (!RealUse) {
4143 TryNext = true;
4144 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004145 }
4146 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004147 }
4148 if (TryNext)
4149 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004150
Chris Lattner9f1794e2006-11-11 00:56:29 +00004151 // Check for #2
4152 if (!Op->isPredecessor(N) && !N->isPredecessor(Op)) {
4153 SDOperand Result = isLoad
4154 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
4155 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4156 ++PostIndexedNodes;
4157 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004158 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004159 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4160 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004161 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004162 if (isLoad) {
4163 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004164 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004165 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004166 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004167 } else {
4168 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004169 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004170 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004171
Chris Lattner9f1794e2006-11-11 00:56:29 +00004172 // Finally, since the node is now dead, remove it from the graph.
4173 DAG.DeleteNode(N);
4174
4175 // Replace the uses of Use with uses of the updated base value.
4176 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
4177 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004178 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004179 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004180 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004181 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004182 }
4183 }
4184 }
4185 return false;
4186}
4187
Chris Lattner00161a62008-01-25 07:20:16 +00004188/// InferAlignment - If we can infer some alignment information from this
4189/// pointer, return it.
4190static unsigned InferAlignment(SDOperand Ptr, SelectionDAG &DAG) {
4191 // If this is a direct reference to a stack slot, use information about the
4192 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004193 int FrameIdx = 1 << 31;
4194 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004195 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004196 FrameIdx = FI->getIndex();
4197 } else if (Ptr.getOpcode() == ISD::ADD &&
4198 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4199 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4200 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4201 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004202 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004203
4204 if (FrameIdx != (1 << 31)) {
4205 // FIXME: Handle FI+CST.
4206 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4207 if (MFI.isFixedObjectIndex(FrameIdx)) {
4208 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx);
4209
4210 // The alignment of the frame index can be determined from its offset from
4211 // the incoming frame position. If the frame object is at offset 32 and
4212 // the stack is guaranteed to be 16-byte aligned, then we know that the
4213 // object is 16-byte aligned.
4214 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4215 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4216
4217 // Finally, the frame object itself may have a known alignment. Factor
4218 // the alignment + offset into a new alignment. For example, if we know
4219 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4220 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4221 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4222 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4223 FrameOffset);
4224 return std::max(Align, FIInfoAlign);
4225 }
4226 }
Chris Lattner00161a62008-01-25 07:20:16 +00004227
4228 return 0;
4229}
Chris Lattner448f2192006-11-11 00:39:41 +00004230
Chris Lattner01a22022005-10-10 22:04:48 +00004231SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004232 LoadSDNode *LD = cast<LoadSDNode>(N);
4233 SDOperand Chain = LD->getChain();
4234 SDOperand Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004235
4236 // Try to infer better alignment information than the load already has.
4237 if (LD->isUnindexed()) {
4238 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4239 if (Align > LD->getAlignment())
4240 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4241 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004242 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004243 LD->isVolatile(), Align);
4244 }
4245 }
4246
Evan Cheng45a7ca92007-05-01 00:38:21 +00004247
4248 // If load is not volatile and there are no uses of the loaded value (and
4249 // the updated indexed value in case of indexed loads), change uses of the
4250 // chain value into uses of the chain input (i.e. delete the dead load).
4251 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004252 if (N->getValueType(1) == MVT::Other) {
4253 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004254 if (N->hasNUsesOfValue(0, 0)) {
4255 // It's not safe to use the two value CombineTo variant here. e.g.
4256 // v1, chain2 = load chain1, loc
4257 // v2, chain3 = load chain2, loc
4258 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004259 // Now we replace use of chain2 with chain1. This makes the second load
4260 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004261 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004262 DOUT << "\nWith chain: "; DEBUG(Chain.Val->dump(&DAG));
4263 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004264 WorkListRemover DeadNodes(*this);
4265 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004266 if (N->use_empty()) {
4267 removeFromWorkList(N);
4268 DAG.DeleteNode(N);
4269 }
Evan Cheng02c42852008-01-16 23:11:54 +00004270 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
4271 }
Evan Cheng498f5592007-05-01 08:53:39 +00004272 } else {
4273 // Indexed loads.
4274 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4275 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Evan Cheng02c42852008-01-16 23:11:54 +00004276 SDOperand Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
4277 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
4278 DOUT << "\nWith: "; DEBUG(Undef.Val->dump(&DAG));
4279 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004280 WorkListRemover DeadNodes(*this);
4281 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Undef, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004282 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004283 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004284 &DeadNodes);
4285 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004286 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004287 DAG.DeleteNode(N);
4288 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004289 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004290 }
4291 }
Chris Lattner01a22022005-10-10 22:04:48 +00004292
4293 // If this load is directly stored, replace the load value with the stored
4294 // value.
4295 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004296 // TODO: Handle TRUNCSTORE/LOADEXT
4297 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004298 if (ISD::isNON_TRUNCStore(Chain.Val)) {
4299 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4300 if (PrevST->getBasePtr() == Ptr &&
4301 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004302 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004303 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004304 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004305
Jim Laskey7ca56af2006-10-11 13:47:09 +00004306 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004307 // Walk up chain skipping non-aliasing memory nodes.
4308 SDOperand BetterChain = FindBetterChain(N, Chain);
4309
Jim Laskey6ff23e52006-10-04 16:53:27 +00004310 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004311 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004312 SDOperand ReplLoad;
4313
Jim Laskey279f0532006-09-25 16:29:54 +00004314 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004315 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4316 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004317 LD->getSrcValue(), LD->getSrcValueOffset(),
4318 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004319 } else {
4320 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4321 LD->getValueType(0),
4322 BetterChain, Ptr, LD->getSrcValue(),
4323 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004324 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004325 LD->isVolatile(),
4326 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004327 }
Jim Laskey279f0532006-09-25 16:29:54 +00004328
Jim Laskey6ff23e52006-10-04 16:53:27 +00004329 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00004330 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
4331 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004332
Jim Laskey274062c2006-10-13 23:32:28 +00004333 // Replace uses with load result and token factor. Don't add users
4334 // to work list.
4335 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004336 }
4337 }
4338
Evan Cheng7fc033a2006-11-03 03:06:21 +00004339 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004340 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00004341 return SDOperand(N, 0);
4342
Chris Lattner01a22022005-10-10 22:04:48 +00004343 return SDOperand();
4344}
4345
Chris Lattner07649d92008-01-08 23:08:06 +00004346
Chris Lattner87514ca2005-10-10 22:31:19 +00004347SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004348 StoreSDNode *ST = cast<StoreSDNode>(N);
4349 SDOperand Chain = ST->getChain();
4350 SDOperand Value = ST->getValue();
4351 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004352
Chris Lattner00161a62008-01-25 07:20:16 +00004353 // Try to infer better alignment information than the store already has.
4354 if (ST->isUnindexed()) {
4355 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4356 if (Align > ST->getAlignment())
4357 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004358 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004359 ST->isVolatile(), Align);
4360 }
4361 }
4362
Evan Cheng59d5b682007-05-07 21:27:48 +00004363 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004364 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004365 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004366 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004367 unsigned Align = ST->getAlignment();
4368 MVT::ValueType SVT = Value.getOperand(0).getValueType();
4369 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00004370 getABITypeAlignment(MVT::getTypeForValueType(SVT));
Evan Chengc2cd2b22007-05-07 21:36:06 +00004371 if (Align <= OrigAlign && TLI.isOperationLegal(ISD::STORE, SVT))
Evan Cheng59d5b682007-05-07 21:27:48 +00004372 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004373 ST->getSrcValueOffset(), ST->isVolatile(), Align);
Jim Laskey279f0532006-09-25 16:29:54 +00004374 }
4375
Nate Begeman2cbba892006-12-11 02:23:46 +00004376 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004377 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Evan Cheng25ece662006-12-11 17:25:19 +00004378 if (Value.getOpcode() != ISD::TargetConstantFP) {
4379 SDOperand Tmp;
Chris Lattner62be1a72006-12-12 04:16:14 +00004380 switch (CFP->getValueType(0)) {
4381 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004382 case MVT::f80: // We don't do this for these yet.
4383 case MVT::f128:
4384 case MVT::ppcf128:
4385 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004386 case MVT::f32:
4387 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004388 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
4389 convertToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004390 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004391 ST->getSrcValueOffset(), ST->isVolatile(),
4392 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004393 }
4394 break;
4395 case MVT::f64:
4396 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004397 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
4398 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004399 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004400 ST->getSrcValueOffset(), ST->isVolatile(),
4401 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004402 } else if (TLI.isTypeLegal(MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004403 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004404 // argument passing. Since this is so common, custom legalize the
4405 // 64-bit integer store into two 32-bit stores.
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004406 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
Chris Lattner62be1a72006-12-12 04:16:14 +00004407 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4408 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004409 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004410
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004411 int SVOffset = ST->getSrcValueOffset();
4412 unsigned Alignment = ST->getAlignment();
4413 bool isVolatile = ST->isVolatile();
4414
Chris Lattner62be1a72006-12-12 04:16:14 +00004415 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004416 ST->getSrcValueOffset(),
4417 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004418 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4419 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004420 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004421 Alignment = MinAlign(Alignment, 4U);
Chris Lattner62be1a72006-12-12 04:16:14 +00004422 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004423 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004424 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4425 }
4426 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004427 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004428 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004429 }
4430
Jim Laskey279f0532006-09-25 16:29:54 +00004431 if (CombinerAA) {
4432 // Walk up chain skipping non-aliasing memory nodes.
4433 SDOperand BetterChain = FindBetterChain(N, Chain);
4434
Jim Laskey6ff23e52006-10-04 16:53:27 +00004435 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004436 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004437 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004438 SDOperand ReplStore;
4439 if (ST->isTruncatingStore()) {
4440 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004441 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004442 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004443 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004444 } else {
4445 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004446 ST->getSrcValue(), ST->getSrcValueOffset(),
4447 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004448 }
4449
Jim Laskey279f0532006-09-25 16:29:54 +00004450 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00004451 SDOperand Token =
4452 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4453
4454 // Don't add users to work list.
4455 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004456 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004457 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004458
Evan Cheng33dbedc2006-11-05 09:31:14 +00004459 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004460 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00004461 return SDOperand(N, 0);
4462
Chris Lattner3c872852007-12-29 06:26:16 +00004463 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004464 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Chris Lattner2b4c2792007-10-13 06:35:54 +00004465 MVT::isInteger(Value.getValueType())) {
4466 // See if we can simplify the input to this truncstore with knowledge that
4467 // only the low bits are being used. For example:
4468 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
4469 SDOperand Shorter =
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004470 GetDemandedBits(Value, MVT::getIntVTBitMask(ST->getMemoryVT()));
Chris Lattner2b4c2792007-10-13 06:35:54 +00004471 AddToWorkList(Value.Val);
4472 if (Shorter.Val)
4473 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004474 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004475 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004476
4477 // Otherwise, see if we can simplify the operation with
4478 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004479 if (SimplifyDemandedBits(Value, MVT::getIntVTBitMask(ST->getMemoryVT())))
Chris Lattnere33544c2007-10-13 06:58:48 +00004480 return SDOperand(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004481 }
4482
Chris Lattner3c872852007-12-29 06:26:16 +00004483 // If this is a load followed by a store to the same location, then the store
4484 // is dead/noop.
4485 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004486 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004487 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004488 // There can't be any side effects between the load and store, such as
4489 // a call or store.
Chris Lattner572dee72008-01-16 05:49:24 +00004490 Chain.reachesChainWithoutSideEffects(SDOperand(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004491 // The store is dead, remove it.
4492 return Chain;
4493 }
4494 }
4495
Chris Lattnerddf89562008-01-17 19:59:44 +00004496 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4497 // truncating store. We can do this even if this is already a truncstore.
4498 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
4499 && TLI.isTypeLegal(Value.getOperand(0).getValueType()) &&
4500 Value.Val->hasOneUse() && ST->isUnindexed() &&
4501 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004502 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004503 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004504 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004505 ST->isVolatile(), ST->getAlignment());
4506 }
4507
Chris Lattner87514ca2005-10-10 22:31:19 +00004508 return SDOperand();
4509}
4510
Chris Lattnerca242442006-03-19 01:27:56 +00004511SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4512 SDOperand InVec = N->getOperand(0);
4513 SDOperand InVal = N->getOperand(1);
4514 SDOperand EltNo = N->getOperand(2);
4515
4516 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4517 // vector with the inserted element.
4518 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
4519 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004520 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004521 if (Elt < Ops.size())
4522 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004523 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4524 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004525 }
4526
4527 return SDOperand();
4528}
4529
Evan Cheng513da432007-10-06 08:19:55 +00004530SDOperand DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
4531 SDOperand InVec = N->getOperand(0);
4532 SDOperand EltNo = N->getOperand(1);
4533
4534 // (vextract (v4f32 s2v (f32 load $addr)), 0) -> (f32 load $addr)
4535 // (vextract (v4i32 bc (v4f32 s2v (f32 load $addr))), 0) -> (i32 load $addr)
4536 if (isa<ConstantSDNode>(EltNo)) {
4537 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
4538 bool NewLoad = false;
4539 if (Elt == 0) {
4540 MVT::ValueType VT = InVec.getValueType();
4541 MVT::ValueType EVT = MVT::getVectorElementType(VT);
4542 MVT::ValueType LVT = EVT;
4543 unsigned NumElts = MVT::getVectorNumElements(VT);
4544 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
4545 MVT::ValueType BCVT = InVec.getOperand(0).getValueType();
Dan Gohman090b38a2007-10-29 20:44:42 +00004546 if (!MVT::isVector(BCVT) ||
4547 NumElts != MVT::getVectorNumElements(BCVT))
Evan Cheng513da432007-10-06 08:19:55 +00004548 return SDOperand();
4549 InVec = InVec.getOperand(0);
4550 EVT = MVT::getVectorElementType(BCVT);
4551 NewLoad = true;
4552 }
4553 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4554 InVec.getOperand(0).getValueType() == EVT &&
4555 ISD::isNormalLoad(InVec.getOperand(0).Val) &&
4556 InVec.getOperand(0).hasOneUse()) {
4557 LoadSDNode *LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4558 unsigned Align = LN0->getAlignment();
4559 if (NewLoad) {
4560 // Check the resultant load doesn't need a higher alignment than the
4561 // original load.
4562 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
4563 getABITypeAlignment(MVT::getTypeForValueType(LVT));
4564 if (!TLI.isOperationLegal(ISD::LOAD, LVT) || NewAlign > Align)
4565 return SDOperand();
4566 Align = NewAlign;
4567 }
4568
4569 return DAG.getLoad(LVT, LN0->getChain(), LN0->getBasePtr(),
4570 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4571 LN0->isVolatile(), Align);
4572 }
4573 }
4574 }
4575 return SDOperand();
4576}
4577
4578
Dan Gohman7f321562007-06-25 16:23:39 +00004579SDOperand DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
4580 unsigned NumInScalars = N->getNumOperands();
4581 MVT::ValueType VT = N->getValueType(0);
4582 unsigned NumElts = MVT::getVectorNumElements(VT);
4583 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattnerca242442006-03-19 01:27:56 +00004584
Dan Gohman7f321562007-06-25 16:23:39 +00004585 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4586 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4587 // at most two distinct vectors, turn this into a shuffle node.
Chris Lattnerd7648c82006-03-28 20:28:38 +00004588 SDOperand VecIn1, VecIn2;
4589 for (unsigned i = 0; i != NumInScalars; ++i) {
4590 // Ignore undef inputs.
4591 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4592
Dan Gohman7f321562007-06-25 16:23:39 +00004593 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004594 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004595 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004596 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
4597 VecIn1 = VecIn2 = SDOperand(0, 0);
4598 break;
4599 }
4600
Dan Gohman7f321562007-06-25 16:23:39 +00004601 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004602 // we can't make a shuffle.
4603 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004604 if (ExtractedFromVec.getValueType() != VT) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004605 VecIn1 = VecIn2 = SDOperand(0, 0);
4606 break;
4607 }
4608
4609 // Otherwise, remember this. We allow up to two distinct input vectors.
4610 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4611 continue;
4612
4613 if (VecIn1.Val == 0) {
4614 VecIn1 = ExtractedFromVec;
4615 } else if (VecIn2.Val == 0) {
4616 VecIn2 = ExtractedFromVec;
4617 } else {
4618 // Too many inputs.
4619 VecIn1 = VecIn2 = SDOperand(0, 0);
4620 break;
4621 }
4622 }
4623
4624 // If everything is good, we can make a shuffle operation.
4625 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004626 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004627 for (unsigned i = 0; i != NumInScalars; ++i) {
4628 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004629 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004630 continue;
4631 }
4632
4633 SDOperand Extract = N->getOperand(i);
4634
4635 // If extracting from the first vector, just use the index directly.
4636 if (Extract.getOperand(0) == VecIn1) {
4637 BuildVecIndices.push_back(Extract.getOperand(1));
4638 continue;
4639 }
4640
4641 // Otherwise, use InIdx + VecSize
4642 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004643 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004644 }
4645
4646 // Add count and size info.
Chris Lattner0bd48932008-01-17 07:00:52 +00004647 MVT::ValueType BuildVecVT = MVT::getVectorType(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004648
Dan Gohman7f321562007-06-25 16:23:39 +00004649 // Return the new VECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004650 SDOperand Ops[5];
4651 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00004652 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004653 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004654 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004655 // Use an undef build_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00004656 std::vector<SDOperand> UnOps(NumInScalars,
4657 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004658 EltType));
4659 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004660 &UnOps[0], UnOps.size());
4661 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00004662 }
Dan Gohman7f321562007-06-25 16:23:39 +00004663 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004664 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004665 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004666 }
4667
4668 return SDOperand();
4669}
4670
Dan Gohman7f321562007-06-25 16:23:39 +00004671SDOperand DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
4672 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
4673 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
4674 // inputs come from at most two distinct vectors, turn this into a shuffle
4675 // node.
4676
4677 // If we only have one input vector, we don't need to do any concatenation.
4678 if (N->getNumOperands() == 1) {
4679 return N->getOperand(0);
4680 }
4681
4682 return SDOperand();
4683}
4684
Chris Lattner66445d32006-03-28 22:11:53 +00004685SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004686 SDOperand ShufMask = N->getOperand(2);
4687 unsigned NumElts = ShufMask.getNumOperands();
4688
4689 // If the shuffle mask is an identity operation on the LHS, return the LHS.
4690 bool isIdentity = true;
4691 for (unsigned i = 0; i != NumElts; ++i) {
4692 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4693 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
4694 isIdentity = false;
4695 break;
4696 }
4697 }
4698 if (isIdentity) return N->getOperand(0);
4699
4700 // If the shuffle mask is an identity operation on the RHS, return the RHS.
4701 isIdentity = true;
4702 for (unsigned i = 0; i != NumElts; ++i) {
4703 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4704 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
4705 isIdentity = false;
4706 break;
4707 }
4708 }
4709 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004710
4711 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
4712 // needed at all.
4713 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00004714 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004715 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00004716 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004717 for (unsigned i = 0; i != NumElts; ++i)
4718 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
4719 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
4720 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00004721 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004722 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004723 BaseIdx = Idx;
4724 } else {
4725 if (BaseIdx != Idx)
4726 isSplat = false;
4727 if (VecNum != V) {
4728 isUnary = false;
4729 break;
4730 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004731 }
4732 }
4733
4734 SDOperand N0 = N->getOperand(0);
4735 SDOperand N1 = N->getOperand(1);
4736 // Normalize unary shuffle so the RHS is undef.
4737 if (isUnary && VecNum == 1)
4738 std::swap(N0, N1);
4739
Evan Cheng917ec982006-07-21 08:25:53 +00004740 // If it is a splat, check if the argument vector is a build_vector with
4741 // all scalar elements the same.
4742 if (isSplat) {
4743 SDNode *V = N0.Val;
Evan Cheng917ec982006-07-21 08:25:53 +00004744
Dan Gohman7f321562007-06-25 16:23:39 +00004745 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004746 // not the number of vector elements, look through it. Be careful not to
4747 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004748 if (V->getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng59569222006-10-16 22:49:37 +00004749 SDOperand ConvInput = V->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004750 if (MVT::getVectorNumElements(ConvInput.getValueType()) == NumElts)
Evan Cheng59569222006-10-16 22:49:37 +00004751 V = ConvInput.Val;
4752 }
4753
Dan Gohman7f321562007-06-25 16:23:39 +00004754 if (V->getOpcode() == ISD::BUILD_VECTOR) {
4755 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00004756 if (NumElems > BaseIdx) {
4757 SDOperand Base;
4758 bool AllSame = true;
4759 for (unsigned i = 0; i != NumElems; ++i) {
4760 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4761 Base = V->getOperand(i);
4762 break;
4763 }
4764 }
4765 // Splat of <u, u, u, u>, return <u, u, u, u>
4766 if (!Base.Val)
4767 return N0;
4768 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00004769 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00004770 AllSame = false;
4771 break;
4772 }
4773 }
4774 // Splat of <x, x, x, x>, return <x, x, x, x>
4775 if (AllSame)
4776 return N0;
4777 }
4778 }
4779 }
4780
Evan Chenge7bec0d2006-07-20 22:44:41 +00004781 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4782 // into an undef.
4783 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004784 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4785 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004786 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004787 for (unsigned i = 0; i != NumElts; ++i) {
4788 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4789 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4790 MappedOps.push_back(ShufMask.getOperand(i));
4791 } else {
4792 unsigned NewIdx =
4793 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
4794 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
4795 }
4796 }
Dan Gohman7f321562007-06-25 16:23:39 +00004797 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004798 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004799 AddToWorkList(ShufMask.Val);
Dan Gohman7f321562007-06-25 16:23:39 +00004800 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
4801 N0,
4802 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
4803 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00004804 }
Dan Gohman7f321562007-06-25 16:23:39 +00004805
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004806 return SDOperand();
4807}
4808
Evan Cheng44f1f092006-04-20 08:56:16 +00004809/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00004810/// an AND to a vector_shuffle with the destination vector and a zero vector.
4811/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00004812/// vector_shuffle V, Zero, <0, 4, 2, 4>
4813SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
4814 SDOperand LHS = N->getOperand(0);
4815 SDOperand RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00004816 if (N->getOpcode() == ISD::AND) {
4817 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00004818 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004819 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Evan Cheng44f1f092006-04-20 08:56:16 +00004820 std::vector<SDOperand> IdxOps;
4821 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00004822 unsigned NumElts = NumOps;
4823 MVT::ValueType EVT = MVT::getVectorElementType(RHS.getValueType());
Evan Cheng44f1f092006-04-20 08:56:16 +00004824 for (unsigned i = 0; i != NumElts; ++i) {
4825 SDOperand Elt = RHS.getOperand(i);
4826 if (!isa<ConstantSDNode>(Elt))
4827 return SDOperand();
4828 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
4829 IdxOps.push_back(DAG.getConstant(i, EVT));
4830 else if (cast<ConstantSDNode>(Elt)->isNullValue())
4831 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
4832 else
4833 return SDOperand();
4834 }
4835
4836 // Let's see if the target supports this vector_shuffle.
4837 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
4838 return SDOperand();
4839
Dan Gohman7f321562007-06-25 16:23:39 +00004840 // Return the new VECTOR_SHUFFLE node.
4841 MVT::ValueType VT = MVT::getVectorType(EVT, NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00004842 std::vector<SDOperand> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004843 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00004844 Ops.push_back(LHS);
4845 AddToWorkList(LHS.Val);
4846 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00004847 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004848 &ZeroOps[0], ZeroOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004849 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004850 &IdxOps[0], IdxOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004851 SDOperand Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004852 &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004853 if (VT != LHS.getValueType()) {
4854 Result = DAG.getNode(ISD::BIT_CONVERT, LHS.getValueType(), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00004855 }
4856 return Result;
4857 }
4858 }
4859 return SDOperand();
4860}
4861
Dan Gohman7f321562007-06-25 16:23:39 +00004862/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
4863SDOperand DAGCombiner::SimplifyVBinOp(SDNode *N) {
4864 // After legalize, the target may be depending on adds and other
4865 // binary ops to provide legal ways to construct constants or other
4866 // things. Simplifying them may result in a loss of legality.
4867 if (AfterLegalize) return SDOperand();
4868
4869 MVT::ValueType VT = N->getValueType(0);
Dan Gohman05d92fe2007-07-13 20:03:40 +00004870 assert(MVT::isVector(VT) && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00004871
4872 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattneredab1b92006-04-02 03:25:57 +00004873 SDOperand LHS = N->getOperand(0);
4874 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00004875 SDOperand Shuffle = XformToShuffleWithZero(N);
4876 if (Shuffle.Val) return Shuffle;
4877
Dan Gohman7f321562007-06-25 16:23:39 +00004878 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00004879 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00004880 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
4881 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004882 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004883 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Chris Lattneredab1b92006-04-02 03:25:57 +00004884 SDOperand LHSOp = LHS.getOperand(i);
4885 SDOperand RHSOp = RHS.getOperand(i);
4886 // If these two elements can't be folded, bail out.
4887 if ((LHSOp.getOpcode() != ISD::UNDEF &&
4888 LHSOp.getOpcode() != ISD::Constant &&
4889 LHSOp.getOpcode() != ISD::ConstantFP) ||
4890 (RHSOp.getOpcode() != ISD::UNDEF &&
4891 RHSOp.getOpcode() != ISD::Constant &&
4892 RHSOp.getOpcode() != ISD::ConstantFP))
4893 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00004894 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00004895 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
4896 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00004897 if ((RHSOp.getOpcode() == ISD::Constant &&
4898 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
4899 (RHSOp.getOpcode() == ISD::ConstantFP &&
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00004900 cast<ConstantFPSDNode>(RHSOp.Val)->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00004901 break;
4902 }
Dan Gohman7f321562007-06-25 16:23:39 +00004903 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00004904 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00004905 assert((Ops.back().getOpcode() == ISD::UNDEF ||
4906 Ops.back().getOpcode() == ISD::Constant ||
4907 Ops.back().getOpcode() == ISD::ConstantFP) &&
4908 "Scalar binop didn't fold!");
4909 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004910
Dan Gohman7f321562007-06-25 16:23:39 +00004911 if (Ops.size() == LHS.getNumOperands()) {
4912 MVT::ValueType VT = LHS.getValueType();
4913 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004914 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004915 }
4916
4917 return SDOperand();
4918}
4919
Nate Begeman44728a72005-09-19 22:34:01 +00004920SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00004921 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
4922
4923 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
4924 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4925 // If we got a simplified select_cc node back from SimplifySelectCC, then
4926 // break it down into a new SETCC node, and a new SELECT node, and then return
4927 // the SELECT node, since we were called with a SELECT node.
4928 if (SCC.Val) {
4929 // Check to see if we got a select_cc back (to turn into setcc/select).
4930 // Otherwise, just return whatever node we got back, like fabs.
4931 if (SCC.getOpcode() == ISD::SELECT_CC) {
4932 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
4933 SCC.getOperand(0), SCC.getOperand(1),
4934 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00004935 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004936 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
4937 SCC.getOperand(3), SETCC);
4938 }
4939 return SCC;
4940 }
Nate Begeman44728a72005-09-19 22:34:01 +00004941 return SDOperand();
4942}
4943
Chris Lattner40c62d52005-10-18 06:04:22 +00004944/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
4945/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00004946/// select. Callers of this should assume that TheSelect is deleted if this
4947/// returns true. As such, they should return the appropriate thing (e.g. the
4948/// node) back to the top-level of the DAG combiner loop to avoid it being
4949/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00004950///
4951bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
4952 SDOperand RHS) {
4953
4954 // If this is a select from two identical things, try to pull the operation
4955 // through the select.
4956 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00004957 // If this is a load and the token chain is identical, replace the select
4958 // of two loads with a load through a select of the address to load from.
4959 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
4960 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00004961 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00004962 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00004963 LHS.getOperand(0) == RHS.getOperand(0)) {
4964 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
4965 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
4966
4967 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004968 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004969 // FIXME: this conflates two src values, discarding one. This is not
4970 // the right thing to do, but nothing uses srcvalues now. When they do,
4971 // turn SrcValue into a list of locations.
4972 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004973 if (TheSelect->getOpcode() == ISD::SELECT) {
4974 // Check that the condition doesn't reach either load. If so, folding
4975 // this will induce a cycle into the DAG.
4976 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4977 !RLD->isPredecessor(TheSelect->getOperand(0).Val)) {
4978 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
4979 TheSelect->getOperand(0), LLD->getBasePtr(),
4980 RLD->getBasePtr());
4981 }
4982 } else {
4983 // Check that the condition doesn't reach either load. If so, folding
4984 // this will induce a cycle into the DAG.
4985 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4986 !RLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4987 !LLD->isPredecessor(TheSelect->getOperand(1).Val) &&
4988 !RLD->isPredecessor(TheSelect->getOperand(1).Val)) {
4989 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00004990 TheSelect->getOperand(0),
4991 TheSelect->getOperand(1),
4992 LLD->getBasePtr(), RLD->getBasePtr(),
4993 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004994 }
Evan Cheng466685d2006-10-09 20:57:25 +00004995 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004996
4997 if (Addr.Val) {
4998 SDOperand Load;
4999 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5000 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5001 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005002 LLD->getSrcValueOffset(),
5003 LLD->isVolatile(),
5004 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005005 else {
5006 Load = DAG.getExtLoad(LLD->getExtensionType(),
5007 TheSelect->getValueType(0),
5008 LLD->getChain(), Addr, LLD->getSrcValue(),
5009 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005010 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005011 LLD->isVolatile(),
5012 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005013 }
5014 // Users of the select now use the result of the load.
5015 CombineTo(TheSelect, Load);
5016
5017 // Users of the old loads now use the new load's chain. We know the
5018 // old-load value is dead now.
5019 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
5020 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
5021 return true;
5022 }
Evan Chengc5484282006-10-04 00:56:09 +00005023 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005024 }
5025 }
5026
5027 return false;
5028}
5029
Nate Begeman44728a72005-09-19 22:34:01 +00005030SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
5031 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00005032 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005033
5034 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00005035 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
5036 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
5037 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
5038
5039 // Determine if the condition we're dealing with is constant
5040 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00005041 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005042 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
5043
5044 // fold select_cc true, x, y -> x
5045 if (SCCC && SCCC->getValue())
5046 return N2;
5047 // fold select_cc false, x, y -> y
5048 if (SCCC && SCCC->getValue() == 0)
5049 return N3;
5050
5051 // Check to see if we can simplify the select into an fabs node
5052 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5053 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005054 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005055 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5056 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5057 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5058 N2 == N3.getOperand(0))
5059 return DAG.getNode(ISD::FABS, VT, N0);
5060
5061 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5062 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5063 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5064 N2.getOperand(0) == N3)
5065 return DAG.getNode(ISD::FABS, VT, N3);
5066 }
5067 }
5068
5069 // Check to see if we can perform the "gzip trick", transforming
5070 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005071 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00005072 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00005073 MVT::isInteger(N2.getValueType()) &&
5074 (N1C->isNullValue() || // (a < 0) ? b : 0
5075 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00005076 MVT::ValueType XType = N0.getValueType();
5077 MVT::ValueType AType = N2.getValueType();
5078 if (XType >= AType) {
5079 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005080 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00005081 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
5082 unsigned ShCtV = Log2_64(N2C->getValue());
5083 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
5084 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5085 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00005086 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005087 if (XType > AType) {
5088 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005089 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005090 }
5091 return DAG.getNode(ISD::AND, AType, Shift, N2);
5092 }
5093 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5094 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5095 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00005096 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005097 if (XType > AType) {
5098 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005099 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005100 }
5101 return DAG.getNode(ISD::AND, AType, Shift, N2);
5102 }
5103 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005104
5105 // fold select C, 16, 0 -> shl C, 4
5106 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
5107 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005108
5109 // If the caller doesn't want us to simplify this into a zext of a compare,
5110 // don't do it.
5111 if (NotExtCompare && N2C->getValue() == 1)
5112 return SDOperand();
5113
Nate Begeman07ed4172005-10-10 21:26:48 +00005114 // Get a SetCC of the condition
5115 // FIXME: Should probably make sure that setcc is legal if we ever have a
5116 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00005117 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005118 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00005119 if (AfterLegalize) {
5120 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Chris Lattner555d8d62006-12-07 22:36:47 +00005121 if (N2.getValueType() < SCC.getValueType())
5122 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5123 else
5124 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005125 } else {
5126 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005127 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005128 }
Chris Lattner5750df92006-03-01 04:03:14 +00005129 AddToWorkList(SCC.Val);
5130 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005131
5132 if (N2C->getValue() == 1)
5133 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005134 // shl setcc result by log2 n2c
5135 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
5136 DAG.getConstant(Log2_64(N2C->getValue()),
5137 TLI.getShiftAmountTy()));
5138 }
5139
Nate Begemanf845b452005-10-08 00:29:44 +00005140 // Check to see if this is the equivalent of setcc
5141 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5142 // otherwise, go ahead with the folds.
5143 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
5144 MVT::ValueType XType = N0.getValueType();
5145 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
5146 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
5147 if (Res.getValueType() != VT)
5148 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5149 return Res;
5150 }
5151
5152 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5153 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
5154 TLI.isOperationLegal(ISD::CTLZ, XType)) {
5155 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
5156 return DAG.getNode(ISD::SRL, XType, Ctlz,
5157 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
5158 TLI.getShiftAmountTy()));
5159 }
5160 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5161 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
5162 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
5163 N0);
5164 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
5165 DAG.getConstant(~0ULL, XType));
5166 return DAG.getNode(ISD::SRL, XType,
5167 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
5168 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5169 TLI.getShiftAmountTy()));
5170 }
5171 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5172 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
5173 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
5174 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5175 TLI.getShiftAmountTy()));
5176 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5177 }
5178 }
5179
5180 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5181 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5182 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005183 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
5184 N2.getOperand(0) == N1 && MVT::isInteger(N0.getValueType())) {
5185 MVT::ValueType XType = N0.getValueType();
5186 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5187 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5188 TLI.getShiftAmountTy()));
5189 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
5190 AddToWorkList(Shift.Val);
5191 AddToWorkList(Add.Val);
5192 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5193 }
5194 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5195 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5196 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5197 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5198 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Nate Begemanf845b452005-10-08 00:29:44 +00005199 MVT::ValueType XType = N0.getValueType();
5200 if (SubC->isNullValue() && MVT::isInteger(XType)) {
5201 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5202 DAG.getConstant(MVT::getSizeInBits(XType)-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005203 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00005204 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005205 AddToWorkList(Shift.Val);
5206 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005207 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5208 }
5209 }
5210 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005211
Nate Begeman44728a72005-09-19 22:34:01 +00005212 return SDOperand();
5213}
5214
Evan Chengfa1eb272007-02-08 22:13:59 +00005215/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Nate Begeman452d7be2005-09-16 00:54:12 +00005216SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00005217 SDOperand N1, ISD::CondCode Cond,
5218 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005219 TargetLowering::DAGCombinerInfo
5220 DagCombineInfo(DAG, !AfterLegalize, false, this);
5221 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005222}
5223
Nate Begeman69575232005-10-20 02:15:44 +00005224/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5225/// return a DAG expression to select that will generate the same value by
5226/// multiplying by a magic number. See:
5227/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5228SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005229 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005230 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
5231
Andrew Lenharth232c9102006-06-12 16:07:18 +00005232 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005233 ii != ee; ++ii)
5234 AddToWorkList(*ii);
5235 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005236}
5237
5238/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5239/// return a DAG expression to select that will generate the same value by
5240/// multiplying by a magic number. See:
5241/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5242SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005243 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005244 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005245
Andrew Lenharth232c9102006-06-12 16:07:18 +00005246 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005247 ii != ee; ++ii)
5248 AddToWorkList(*ii);
5249 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005250}
5251
Jim Laskey71382342006-10-07 23:37:56 +00005252/// FindBaseOffset - Return true if base is known not to alias with anything
5253/// but itself. Provides base object and offset as results.
5254static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
5255 // Assume it is a primitive operation.
5256 Base = Ptr; Offset = 0;
5257
5258 // If it's an adding a simple constant then integrate the offset.
5259 if (Base.getOpcode() == ISD::ADD) {
5260 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5261 Base = Base.getOperand(0);
5262 Offset += C->getValue();
5263 }
5264 }
5265
5266 // If it's any of the following then it can't alias with anything but itself.
5267 return isa<FrameIndexSDNode>(Base) ||
5268 isa<ConstantPoolSDNode>(Base) ||
5269 isa<GlobalAddressSDNode>(Base);
5270}
5271
5272/// isAlias - Return true if there is any possibility that the two addresses
5273/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00005274bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
5275 const Value *SrcValue1, int SrcValueOffset1,
5276 SDOperand Ptr2, int64_t Size2,
5277 const Value *SrcValue2, int SrcValueOffset2)
5278{
Jim Laskey71382342006-10-07 23:37:56 +00005279 // If they are the same then they must be aliases.
5280 if (Ptr1 == Ptr2) return true;
5281
5282 // Gather base node and offset information.
5283 SDOperand Base1, Base2;
5284 int64_t Offset1, Offset2;
5285 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5286 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5287
5288 // If they have a same base address then...
5289 if (Base1 == Base2) {
5290 // Check to see if the addresses overlap.
5291 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5292 }
5293
Jim Laskey096c22e2006-10-18 12:29:57 +00005294 // If we know both bases then they can't alias.
5295 if (KnownBase1 && KnownBase2) return false;
5296
Jim Laskey07a27092006-10-18 19:08:31 +00005297 if (CombinerGlobalAA) {
5298 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005299 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5300 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5301 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005302 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005303 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005304 if (AAResult == AliasAnalysis::NoAlias)
5305 return false;
5306 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005307
5308 // Otherwise we have to assume they alias.
5309 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005310}
5311
5312/// FindAliasInfo - Extracts the relevant alias information from the memory
5313/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005314bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00005315 SDOperand &Ptr, int64_t &Size,
5316 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005317 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5318 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005319 Size = MVT::getSizeInBits(LD->getMemoryVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005320 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005321 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005322 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005323 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005324 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005325 Size = MVT::getSizeInBits(ST->getMemoryVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005326 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005327 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005328 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005329 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005330 }
5331
5332 return false;
5333}
5334
Jim Laskey6ff23e52006-10-04 16:53:27 +00005335/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5336/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00005337void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005338 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005339 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005340 std::set<SDNode *> Visited; // Visited node set.
5341
Jim Laskey279f0532006-09-25 16:29:54 +00005342 // Get alias information for node.
5343 SDOperand Ptr;
5344 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005345 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005346 int SrcValueOffset;
5347 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005348
Jim Laskey6ff23e52006-10-04 16:53:27 +00005349 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005350 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005351
Jim Laskeybc588b82006-10-05 15:07:25 +00005352 // Look at each chain and determine if it is an alias. If so, add it to the
5353 // aliases list. If not, then continue up the chain looking for the next
5354 // candidate.
5355 while (!Chains.empty()) {
5356 SDOperand Chain = Chains.back();
5357 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005358
Jim Laskeybc588b82006-10-05 15:07:25 +00005359 // Don't bother if we've been before.
5360 if (Visited.find(Chain.Val) != Visited.end()) continue;
5361 Visited.insert(Chain.Val);
5362
5363 switch (Chain.getOpcode()) {
5364 case ISD::EntryToken:
5365 // Entry token is ideal chain operand, but handled in FindBetterChain.
5366 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005367
Jim Laskeybc588b82006-10-05 15:07:25 +00005368 case ISD::LOAD:
5369 case ISD::STORE: {
5370 // Get alias information for Chain.
5371 SDOperand OpPtr;
5372 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005373 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005374 int OpSrcValueOffset;
5375 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
5376 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005377
5378 // If chain is alias then stop here.
5379 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005380 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5381 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005382 Aliases.push_back(Chain);
5383 } else {
5384 // Look further up the chain.
5385 Chains.push_back(Chain.getOperand(0));
5386 // Clean up old chain.
5387 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00005388 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005389 break;
5390 }
5391
5392 case ISD::TokenFactor:
5393 // We have to check each of the operands of the token factor, so we queue
5394 // then up. Adding the operands to the queue (stack) in reverse order
5395 // maintains the original order and increases the likelihood that getNode
5396 // will find a matching token factor (CSE.)
5397 for (unsigned n = Chain.getNumOperands(); n;)
5398 Chains.push_back(Chain.getOperand(--n));
5399 // Eliminate the token factor if we can.
5400 AddToWorkList(Chain.Val);
5401 break;
5402
5403 default:
5404 // For all other instructions we will just have to take what we can get.
5405 Aliases.push_back(Chain);
5406 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005407 }
5408 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005409}
5410
5411/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5412/// for a better chain (aliasing node.)
5413SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
5414 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005415
Jim Laskey6ff23e52006-10-04 16:53:27 +00005416 // Accumulate all the aliases to this node.
5417 GatherAllAliases(N, OldChain, Aliases);
5418
5419 if (Aliases.size() == 0) {
5420 // If no operands then chain to entry token.
5421 return DAG.getEntryNode();
5422 } else if (Aliases.size() == 1) {
5423 // If a single operand then chain to it. We don't need to revisit it.
5424 return Aliases[0];
5425 }
5426
5427 // Construct a custom tailored token factor.
5428 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5429 &Aliases[0], Aliases.size());
5430
5431 // Make sure the old chain gets cleaned up.
5432 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
5433
5434 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005435}
5436
Nate Begeman1d4d4142005-09-01 00:19:25 +00005437// SelectionDAG::Combine - This is the entry point for the file.
5438//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005439void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00005440 if (!RunningAfterLegalize && ViewDAGCombine1)
5441 viewGraph();
5442 if (RunningAfterLegalize && ViewDAGCombine2)
5443 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005444 /// run - This is the main entry point to this class.
5445 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005446 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005447}