blob: 144b976da9a011bb38aac7f278cf7e61b852ec71 [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
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000221 SDOperand GetDemandedBits(SDOperand V, const APInt &Mask);
Chris Lattner2b4c2792007-10-13 06:35:54 +0000222
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:
Dan Gohmanb5660dc2008-02-20 16:44:09 +0000265 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000266
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 Lattner0254e702008-02-26 07:04:54 +0000309static char isNegatibleForFree(SDOperand Op, bool AfterLegalize,
310 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000311 // No compile time optimizations on this type.
312 if (Op.getValueType() == MVT::ppcf128)
313 return 0;
314
Chris Lattner29446522007-05-14 22:04:50 +0000315 // fneg is removable even if it has multiple uses.
316 if (Op.getOpcode() == ISD::FNEG) return 2;
317
318 // Don't allow anything with multiple uses.
319 if (!Op.hasOneUse()) return 0;
320
Chris Lattner3adf9512007-05-25 02:19:06 +0000321 // Don't recurse exponentially.
322 if (Depth > 6) return 0;
323
Chris Lattner29446522007-05-14 22:04:50 +0000324 switch (Op.getOpcode()) {
325 default: return false;
326 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000327 // Don't invert constant FP values after legalize. The negated constant
328 // isn't necessarily legal.
329 return AfterLegalize ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000330 case ISD::FADD:
331 // FIXME: determine better conditions for this xform.
332 if (!UnsafeFPMath) return 0;
333
334 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000335 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000336 return V;
337 // -(A+B) -> -B - A
Chris Lattner0254e702008-02-26 07:04:54 +0000338 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000339 case ISD::FSUB:
340 // We can't turn -(A-B) into B-A when we honor signed zeros.
341 if (!UnsafeFPMath) return 0;
342
343 // -(A-B) -> B-A
344 return 1;
345
346 case ISD::FMUL:
347 case ISD::FDIV:
348 if (HonorSignDependentRoundingFPMath()) return 0;
349
350 // -(X*Y) -> (-X * Y) or (X*-Y)
Chris Lattner0254e702008-02-26 07:04:54 +0000351 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000352 return V;
353
Chris Lattner0254e702008-02-26 07:04:54 +0000354 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000355
356 case ISD::FP_EXTEND:
357 case ISD::FP_ROUND:
358 case ISD::FSIN:
Chris Lattner0254e702008-02-26 07:04:54 +0000359 return isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000360 }
361}
362
363/// GetNegatedExpression - If isNegatibleForFree returns true, this function
364/// returns the newly negated expression.
Chris Lattner3adf9512007-05-25 02:19:06 +0000365static SDOperand GetNegatedExpression(SDOperand Op, SelectionDAG &DAG,
Chris Lattner0254e702008-02-26 07:04:54 +0000366 bool AfterLegalize, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000367 // fneg is removable even if it has multiple uses.
368 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
369
370 // Don't allow anything with multiple uses.
371 assert(Op.hasOneUse() && "Unknown reuse!");
372
Chris Lattner3adf9512007-05-25 02:19:06 +0000373 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000374 switch (Op.getOpcode()) {
375 default: assert(0 && "Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000376 case ISD::ConstantFP: {
377 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
378 V.changeSign();
379 return DAG.getConstantFP(V, Op.getValueType());
380 }
Chris Lattner29446522007-05-14 22:04:50 +0000381 case ISD::FADD:
382 // FIXME: determine better conditions for this xform.
383 assert(UnsafeFPMath);
384
385 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000386 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000387 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000388 GetNegatedExpression(Op.getOperand(0), DAG,
389 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000390 Op.getOperand(1));
391 // -(A+B) -> -B - A
392 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000393 GetNegatedExpression(Op.getOperand(1), DAG,
394 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000395 Op.getOperand(0));
396 case ISD::FSUB:
397 // We can't turn -(A-B) into B-A when we honor signed zeros.
398 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000399
400 // -(0-B) -> B
401 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000402 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000403 return Op.getOperand(1);
Chris Lattner29446522007-05-14 22:04:50 +0000404
405 // -(A-B) -> B-A
406 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
407 Op.getOperand(0));
408
409 case ISD::FMUL:
410 case ISD::FDIV:
411 assert(!HonorSignDependentRoundingFPMath());
412
413 // -(X*Y) -> -X * Y
Chris Lattner3adf9512007-05-25 02:19:06 +0000414 if (isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000415 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000416 GetNegatedExpression(Op.getOperand(0), DAG,
417 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000418 Op.getOperand(1));
419
420 // -(X*Y) -> X * -Y
421 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
422 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000423 GetNegatedExpression(Op.getOperand(1), DAG,
424 AfterLegalize, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000425
426 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000427 case ISD::FSIN:
428 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000429 GetNegatedExpression(Op.getOperand(0), DAG,
430 AfterLegalize, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000431 case ISD::FP_ROUND:
432 return DAG.getNode(ISD::FP_ROUND, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000433 GetNegatedExpression(Op.getOperand(0), DAG,
434 AfterLegalize, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000435 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000436 }
437}
Chris Lattner24664722006-03-01 04:53:38 +0000438
439
Nate Begeman4ebd8052005-09-01 23:24:04 +0000440// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
441// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000442// Also, set the incoming LHS, RHS, and CC references to the appropriate
443// nodes based on the type of node we are checking. This simplifies life a
444// bit for the callers.
445static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
446 SDOperand &CC) {
447 if (N.getOpcode() == ISD::SETCC) {
448 LHS = N.getOperand(0);
449 RHS = N.getOperand(1);
450 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000451 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000452 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000453 if (N.getOpcode() == ISD::SELECT_CC &&
454 N.getOperand(2).getOpcode() == ISD::Constant &&
455 N.getOperand(3).getOpcode() == ISD::Constant &&
456 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000457 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
458 LHS = N.getOperand(0);
459 RHS = N.getOperand(1);
460 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000461 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000462 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000463 return false;
464}
465
Nate Begeman99801192005-09-07 23:25:52 +0000466// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
467// one use. If this is true, it allows the users to invert the operation for
468// free when it is profitable to do so.
469static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000470 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000471 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000472 return true;
473 return false;
474}
475
Nate Begemancd4d58c2006-02-03 06:46:56 +0000476SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
477 MVT::ValueType VT = N0.getValueType();
478 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
479 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
480 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
481 if (isa<ConstantSDNode>(N1)) {
482 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000483 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000484 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
485 } else if (N0.hasOneUse()) {
486 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
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, N0.getOperand(1));
489 }
490 }
491 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
492 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
493 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
494 if (isa<ConstantSDNode>(N0)) {
495 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000496 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000497 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
498 } else if (N1.hasOneUse()) {
499 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000500 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000501 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
502 }
503 }
504 return SDOperand();
505}
506
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000507SDOperand DAGCombiner::CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
508 bool AddTo) {
509 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
510 ++NodesCombined;
511 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
512 DOUT << "\nWith: "; DEBUG(To[0].Val->dump(&DAG));
513 DOUT << " and " << NumTo-1 << " other values\n";
514 WorkListRemover DeadNodes(*this);
515 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
516
517 if (AddTo) {
518 // Push the new nodes and any users onto the worklist
519 for (unsigned i = 0, e = NumTo; i != e; ++i) {
520 AddToWorkList(To[i].Val);
521 AddUsersToWorkList(To[i].Val);
522 }
523 }
524
525 // Nodes can be reintroduced into the worklist. Make sure we do not
526 // process a node that has been replaced.
527 removeFromWorkList(N);
528
529 // Finally, since the node is now dead, remove it from the graph.
530 DAG.DeleteNode(N);
531 return SDOperand(N, 0);
532}
533
534/// SimplifyDemandedBits - Check the specified integer node value to see if
535/// it can be simplified or if things it uses can be simplified by bit
536/// propagation. If so, return true.
537bool DAGCombiner::SimplifyDemandedBits(SDOperand Op, uint64_t Demanded) {
538 TargetLowering::TargetLoweringOpt TLO(DAG, AfterLegalize);
539 uint64_t KnownZero, KnownOne;
540 Demanded &= MVT::getIntVTBitMask(Op.getValueType());
541 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
542 return false;
543
544 // Revisit the node.
545 AddToWorkList(Op.Val);
546
547 // Replace the old value with the new one.
548 ++NodesCombined;
549 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.Val->dump(&DAG));
550 DOUT << "\nWith: "; DEBUG(TLO.New.Val->dump(&DAG));
551 DOUT << '\n';
552
553 // Replace all uses. If any nodes become isomorphic to other nodes and
554 // are deleted, make sure to remove them from our worklist.
555 WorkListRemover DeadNodes(*this);
556 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
557
558 // Push the new node and any (possibly new) users onto the worklist.
559 AddToWorkList(TLO.New.Val);
560 AddUsersToWorkList(TLO.New.Val);
561
562 // Finally, if the node is now dead, remove it from the graph. The node
563 // may not be dead if the replacement process recursively simplified to
564 // something else needing this node.
565 if (TLO.Old.Val->use_empty()) {
566 removeFromWorkList(TLO.Old.Val);
567
568 // If the operands of this node are only used by the node, they will now
569 // be dead. Make sure to visit them first to delete dead nodes early.
570 for (unsigned i = 0, e = TLO.Old.Val->getNumOperands(); i != e; ++i)
571 if (TLO.Old.Val->getOperand(i).Val->hasOneUse())
572 AddToWorkList(TLO.Old.Val->getOperand(i).Val);
573
574 DAG.DeleteNode(TLO.Old.Val);
575 }
576 return true;
577}
578
Chris Lattner29446522007-05-14 22:04:50 +0000579//===----------------------------------------------------------------------===//
580// Main DAG Combiner implementation
581//===----------------------------------------------------------------------===//
582
Nate Begeman4ebd8052005-09-01 23:24:04 +0000583void DAGCombiner::Run(bool RunningAfterLegalize) {
584 // set the instance variable, so that the various visit routines may use it.
585 AfterLegalize = RunningAfterLegalize;
586
Nate Begeman646d7e22005-09-02 21:18:40 +0000587 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000588 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
589 E = DAG.allnodes_end(); I != E; ++I)
590 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000591
Chris Lattner95038592005-10-05 06:35:28 +0000592 // Create a dummy node (which is not added to allnodes), that adds a reference
593 // to the root node, preventing it from being deleted, and tracking any
594 // changes of the root.
595 HandleSDNode Dummy(DAG.getRoot());
596
Jim Laskey26f7fa72006-10-17 19:33:52 +0000597 // The root of the dag may dangle to deleted nodes until the dag combiner is
598 // done. Set it to null to avoid confusion.
599 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000600
Nate Begeman1d4d4142005-09-01 00:19:25 +0000601 // while the worklist isn't empty, inspect the node on the end of it and
602 // try and combine it.
603 while (!WorkList.empty()) {
604 SDNode *N = WorkList.back();
605 WorkList.pop_back();
606
607 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000608 // N is deleted from the DAG, since they too may now be dead or may have a
609 // reduced number of uses, allowing other xforms.
610 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000611 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000612 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000613
Chris Lattner95038592005-10-05 06:35:28 +0000614 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000615 continue;
616 }
617
Dan Gohman389079b2007-10-08 17:57:15 +0000618 SDOperand RV = combine(N);
Chris Lattner24664722006-03-01 04:53:38 +0000619
Chris Lattner50d8e492008-01-25 23:34:24 +0000620 if (RV.Val == 0)
621 continue;
622
623 ++NodesCombined;
Chris Lattner5eee4272008-01-26 01:09:19 +0000624
Chris Lattner50d8e492008-01-25 23:34:24 +0000625 // If we get back the same node we passed in, rather than a new node or
626 // zero, we know that the node must have defined multiple values and
627 // CombineTo was used. Since CombineTo takes care of the worklist
628 // mechanics for us, we have no work to do in this case.
629 if (RV.Val == N)
630 continue;
631
632 assert(N->getOpcode() != ISD::DELETED_NODE &&
633 RV.Val->getOpcode() != ISD::DELETED_NODE &&
634 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000635
Chris Lattner50d8e492008-01-25 23:34:24 +0000636 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
637 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
638 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000639 WorkListRemover DeadNodes(*this);
Chris Lattner50d8e492008-01-25 23:34:24 +0000640 if (N->getNumValues() == RV.Val->getNumValues())
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000641 DAG.ReplaceAllUsesWith(N, RV.Val, &DeadNodes);
Chris Lattner50d8e492008-01-25 23:34:24 +0000642 else {
Chris Lattner5eee4272008-01-26 01:09:19 +0000643 assert(N->getValueType(0) == RV.getValueType() &&
644 N->getNumValues() == 1 && "Type mismatch");
Chris Lattner50d8e492008-01-25 23:34:24 +0000645 SDOperand OpV = RV;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000646 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000647 }
Chris Lattner50d8e492008-01-25 23:34:24 +0000648
649 // Push the new node and any users onto the worklist
650 AddToWorkList(RV.Val);
651 AddUsersToWorkList(RV.Val);
652
653 // Add any uses of the old node to the worklist in case this node is the
654 // last one that uses them. They may become dead after this node is
655 // deleted.
656 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
657 AddToWorkList(N->getOperand(i).Val);
658
659 // Nodes can be reintroduced into the worklist. Make sure we do not
660 // process a node that has been replaced.
661 removeFromWorkList(N);
Chris Lattner50d8e492008-01-25 23:34:24 +0000662
663 // Finally, since the node is now dead, remove it from the graph.
664 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000665 }
Chris Lattner95038592005-10-05 06:35:28 +0000666
667 // If the root changed (e.g. it was a dead load, update the root).
668 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000669}
670
Nate Begeman83e75ec2005-09-06 04:43:02 +0000671SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000672 switch(N->getOpcode()) {
673 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000674 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000675 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000676 case ISD::ADD: return visitADD(N);
677 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000678 case ISD::ADDC: return visitADDC(N);
679 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000680 case ISD::MUL: return visitMUL(N);
681 case ISD::SDIV: return visitSDIV(N);
682 case ISD::UDIV: return visitUDIV(N);
683 case ISD::SREM: return visitSREM(N);
684 case ISD::UREM: return visitUREM(N);
685 case ISD::MULHU: return visitMULHU(N);
686 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000687 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
688 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
689 case ISD::SDIVREM: return visitSDIVREM(N);
690 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000691 case ISD::AND: return visitAND(N);
692 case ISD::OR: return visitOR(N);
693 case ISD::XOR: return visitXOR(N);
694 case ISD::SHL: return visitSHL(N);
695 case ISD::SRA: return visitSRA(N);
696 case ISD::SRL: return visitSRL(N);
697 case ISD::CTLZ: return visitCTLZ(N);
698 case ISD::CTTZ: return visitCTTZ(N);
699 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000700 case ISD::SELECT: return visitSELECT(N);
701 case ISD::SELECT_CC: return visitSELECT_CC(N);
702 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000703 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
704 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000705 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000706 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
707 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000708 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000709 case ISD::FADD: return visitFADD(N);
710 case ISD::FSUB: return visitFSUB(N);
711 case ISD::FMUL: return visitFMUL(N);
712 case ISD::FDIV: return visitFDIV(N);
713 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000714 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000715 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
716 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
717 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
718 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
719 case ISD::FP_ROUND: return visitFP_ROUND(N);
720 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
721 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
722 case ISD::FNEG: return visitFNEG(N);
723 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000724 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000725 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000726 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000727 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000728 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000729 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000730 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
731 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000732 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000733 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000734 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000735}
736
Dan Gohman389079b2007-10-08 17:57:15 +0000737SDOperand DAGCombiner::combine(SDNode *N) {
738
739 SDOperand RV = visit(N);
740
741 // If nothing happened, try a target-specific DAG combine.
742 if (RV.Val == 0) {
743 assert(N->getOpcode() != ISD::DELETED_NODE &&
744 "Node was deleted but visit returned NULL!");
745
746 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
747 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
748
749 // Expose the DAG combiner to the target combiner impls.
750 TargetLowering::DAGCombinerInfo
751 DagCombineInfo(DAG, !AfterLegalize, false, this);
752
753 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
754 }
755 }
756
757 return RV;
758}
759
Chris Lattner6270f682006-10-08 22:57:01 +0000760/// getInputChainForNode - Given a node, return its input chain if it has one,
761/// otherwise return a null sd operand.
762static SDOperand getInputChainForNode(SDNode *N) {
763 if (unsigned NumOps = N->getNumOperands()) {
764 if (N->getOperand(0).getValueType() == MVT::Other)
765 return N->getOperand(0);
766 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
767 return N->getOperand(NumOps-1);
768 for (unsigned i = 1; i < NumOps-1; ++i)
769 if (N->getOperand(i).getValueType() == MVT::Other)
770 return N->getOperand(i);
771 }
772 return SDOperand(0, 0);
773}
774
Nate Begeman83e75ec2005-09-06 04:43:02 +0000775SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000776 // If N has two operands, where one has an input chain equal to the other,
777 // the 'other' chain is redundant.
778 if (N->getNumOperands() == 2) {
779 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
780 return N->getOperand(0);
781 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
782 return N->getOperand(1);
783 }
784
Chris Lattnerc76d4412007-05-16 06:37:59 +0000785 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
786 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
787 SmallPtrSet<SDNode*, 16> SeenOps;
788 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000789
790 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000791 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000792
Jim Laskey71382342006-10-07 23:37:56 +0000793 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000794 // encountered.
795 for (unsigned i = 0; i < TFs.size(); ++i) {
796 SDNode *TF = TFs[i];
797
Jim Laskey6ff23e52006-10-04 16:53:27 +0000798 // Check each of the operands.
799 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
800 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000801
Jim Laskey6ff23e52006-10-04 16:53:27 +0000802 switch (Op.getOpcode()) {
803 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000804 // Entry tokens don't need to be added to the list. They are
805 // rededundant.
806 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000807 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000808
Jim Laskey6ff23e52006-10-04 16:53:27 +0000809 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000810 if ((CombinerAA || Op.hasOneUse()) &&
811 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000812 // Queue up for processing.
813 TFs.push_back(Op.Val);
814 // Clean up in case the token factor is removed.
815 AddToWorkList(Op.Val);
816 Changed = true;
817 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000818 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000819 // Fall thru
820
821 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000822 // Only add if it isn't already in the list.
823 if (SeenOps.insert(Op.Val))
Jim Laskeybc588b82006-10-05 15:07:25 +0000824 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000825 else
826 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000827 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000828 }
829 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000830 }
831
832 SDOperand Result;
833
834 // If we've change things around then replace token factor.
835 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000836 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000837 // The entry token is the only possible outcome.
838 Result = DAG.getEntryNode();
839 } else {
840 // New and improved token factor.
841 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000842 }
Jim Laskey274062c2006-10-13 23:32:28 +0000843
844 // Don't add users to work list.
845 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000846 }
Jim Laskey279f0532006-09-25 16:29:54 +0000847
Jim Laskey6ff23e52006-10-04 16:53:27 +0000848 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000849}
850
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000851/// MERGE_VALUES can always be eliminated.
852SDOperand DAGCombiner::visitMERGE_VALUES(SDNode *N) {
853 WorkListRemover DeadNodes(*this);
854 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
855 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, i), N->getOperand(i),
856 &DeadNodes);
857 removeFromWorkList(N);
858 DAG.DeleteNode(N);
859 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
860}
861
862
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000863static
864SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
865 MVT::ValueType VT = N0.getValueType();
866 SDOperand N00 = N0.getOperand(0);
867 SDOperand N01 = N0.getOperand(1);
868 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
869 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
870 isa<ConstantSDNode>(N00.getOperand(1))) {
871 N0 = DAG.getNode(ISD::ADD, VT,
872 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
873 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
874 return DAG.getNode(ISD::ADD, VT, N0, N1);
875 }
876 return SDOperand();
877}
878
Evan Chengb13cdbd2007-06-21 07:39:16 +0000879static
880SDOperand combineSelectAndUse(SDNode *N, SDOperand Slct, SDOperand OtherOp,
881 SelectionDAG &DAG) {
882 MVT::ValueType VT = N->getValueType(0);
883 unsigned Opc = N->getOpcode();
884 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
885 SDOperand LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
886 SDOperand RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
887 ISD::CondCode CC = ISD::SETCC_INVALID;
888 if (isSlctCC)
889 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
890 else {
891 SDOperand CCOp = Slct.getOperand(0);
892 if (CCOp.getOpcode() == ISD::SETCC)
893 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
894 }
895
896 bool DoXform = false;
897 bool InvCC = false;
898 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
899 "Bad input!");
900 if (LHS.getOpcode() == ISD::Constant &&
901 cast<ConstantSDNode>(LHS)->isNullValue())
902 DoXform = true;
903 else if (CC != ISD::SETCC_INVALID &&
904 RHS.getOpcode() == ISD::Constant &&
905 cast<ConstantSDNode>(RHS)->isNullValue()) {
906 std::swap(LHS, RHS);
Chris Lattner4626b252008-01-17 07:20:38 +0000907 SDOperand Op0 = Slct.getOperand(0);
908 bool isInt = MVT::isInteger(isSlctCC ? Op0.getValueType()
909 : Op0.getOperand(0).getValueType());
Evan Chengb13cdbd2007-06-21 07:39:16 +0000910 CC = ISD::getSetCCInverse(CC, isInt);
911 DoXform = true;
912 InvCC = true;
913 }
914
915 if (DoXform) {
916 SDOperand Result = DAG.getNode(Opc, VT, OtherOp, RHS);
917 if (isSlctCC)
918 return DAG.getSelectCC(OtherOp, Result,
919 Slct.getOperand(0), Slct.getOperand(1), CC);
920 SDOperand CCOp = Slct.getOperand(0);
921 if (InvCC)
922 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
923 CCOp.getOperand(1), CC);
924 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
925 }
926 return SDOperand();
927}
928
Nate Begeman83e75ec2005-09-06 04:43:02 +0000929SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000930 SDOperand N0 = N->getOperand(0);
931 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000932 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
933 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000934 MVT::ValueType VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000935
936 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +0000937 if (MVT::isVector(VT)) {
938 SDOperand FoldedVOp = SimplifyVBinOp(N);
939 if (FoldedVOp.Val) return FoldedVOp;
940 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000941
Dan Gohman613e0d82007-07-03 14:03:57 +0000942 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000943 if (N0.getOpcode() == ISD::UNDEF)
944 return N0;
945 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000946 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000947 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000948 if (N0C && N1C)
Bill Wendling91b9ad12008-02-10 08:10:24 +0000949 return DAG.getConstant(N0C->getValue() + N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000950 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000951 if (N0C && !N1C)
952 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000953 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000954 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000955 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000956 // fold ((c1-A)+c2) -> (c1+c2)-A
957 if (N1C && N0.getOpcode() == ISD::SUB)
958 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
959 return DAG.getNode(ISD::SUB, VT,
960 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
961 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000962 // reassociate add
963 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
964 if (RADD.Val != 0)
965 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000966 // fold ((0-A) + B) -> B-A
967 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
968 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000969 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000970 // fold (A + (0-B)) -> A-B
971 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
972 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000973 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000974 // fold (A+(B-A)) -> B
975 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000976 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000977
Evan Cheng860771d2006-03-01 01:09:54 +0000978 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000979 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000980
981 // fold (a+b) -> (a|b) iff a and b share no bits.
982 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
Dan Gohman948d8ea2008-02-20 16:33:30 +0000983 APInt LHSZero, LHSOne;
984 APInt RHSZero, RHSOne;
985 APInt Mask = APInt::getAllOnesValue(MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +0000986 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +0000987 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +0000988 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000989
990 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
991 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
992 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
993 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
994 return DAG.getNode(ISD::OR, VT, N0, N1);
995 }
996 }
Evan Cheng3ef554d2006-11-06 08:14:30 +0000997
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000998 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
999 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
1000 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
1001 if (Result.Val) return Result;
1002 }
1003 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
1004 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
1005 if (Result.Val) return Result;
1006 }
1007
Evan Chengb13cdbd2007-06-21 07:39:16 +00001008 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
1009 if (N0.getOpcode() == ISD::SELECT && N0.Val->hasOneUse()) {
1010 SDOperand Result = combineSelectAndUse(N, N0, N1, DAG);
1011 if (Result.Val) return Result;
1012 }
1013 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1014 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1015 if (Result.Val) return Result;
1016 }
1017
Nate Begeman83e75ec2005-09-06 04:43:02 +00001018 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001019}
1020
Chris Lattner91153682007-03-04 20:03:15 +00001021SDOperand DAGCombiner::visitADDC(SDNode *N) {
1022 SDOperand N0 = N->getOperand(0);
1023 SDOperand N1 = N->getOperand(1);
1024 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1025 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1026 MVT::ValueType VT = N0.getValueType();
1027
1028 // If the flag result is dead, turn this into an ADD.
1029 if (N->hasNUsesOfValue(0, 1))
1030 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +00001031 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +00001032
1033 // canonicalize constant to RHS.
Chris Lattnerbcf24842007-03-04 20:08:45 +00001034 if (N0C && !N1C) {
1035 SDOperand Ops[] = { N1, N0 };
1036 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
1037 }
Chris Lattner91153682007-03-04 20:03:15 +00001038
Chris Lattnerb6541762007-03-04 20:40:38 +00001039 // fold (addc x, 0) -> x + no carry out
1040 if (N1C && N1C->isNullValue())
1041 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1042
1043 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001044 APInt LHSZero, LHSOne;
1045 APInt RHSZero, RHSOne;
1046 APInt Mask = APInt::getAllOnesValue(MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +00001047 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001048 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001049 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001050
1051 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1052 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1053 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1054 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1055 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1056 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1057 }
Chris Lattner91153682007-03-04 20:03:15 +00001058
1059 return SDOperand();
1060}
1061
1062SDOperand DAGCombiner::visitADDE(SDNode *N) {
1063 SDOperand N0 = N->getOperand(0);
1064 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +00001065 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001066 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1067 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Chris Lattnerbcf24842007-03-04 20:08:45 +00001068 //MVT::ValueType VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001069
1070 // canonicalize constant to RHS
Chris Lattnerbcf24842007-03-04 20:08:45 +00001071 if (N0C && !N1C) {
Chris Lattnerb6541762007-03-04 20:40:38 +00001072 SDOperand Ops[] = { N1, N0, CarryIn };
Chris Lattnerbcf24842007-03-04 20:08:45 +00001073 return DAG.getNode(ISD::ADDE, N->getVTList(), Ops, 3);
1074 }
Chris Lattner91153682007-03-04 20:03:15 +00001075
Chris Lattnerb6541762007-03-04 20:40:38 +00001076 // fold (adde x, y, false) -> (addc x, y)
1077 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) {
1078 SDOperand Ops[] = { N1, N0 };
1079 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
1080 }
Chris Lattner91153682007-03-04 20:03:15 +00001081
1082 return SDOperand();
1083}
1084
1085
1086
Nate Begeman83e75ec2005-09-06 04:43:02 +00001087SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001088 SDOperand N0 = N->getOperand(0);
1089 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001090 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1091 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001092 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001093
Dan Gohman7f321562007-06-25 16:23:39 +00001094 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001095 if (MVT::isVector(VT)) {
1096 SDOperand FoldedVOp = SimplifyVBinOp(N);
1097 if (FoldedVOp.Val) return FoldedVOp;
1098 }
Dan Gohman7f321562007-06-25 16:23:39 +00001099
Chris Lattner854077d2005-10-17 01:07:11 +00001100 // fold (sub x, x) -> 0
1101 if (N0 == N1)
1102 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001103 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001104 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001105 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001106 // fold (sub x, c) -> (add x, -c)
1107 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001108 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001109 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001110 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001111 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001112 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001113 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001114 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001115 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1116 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1117 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1118 if (Result.Val) return Result;
1119 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001120 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001121 if (N0.getOpcode() == ISD::UNDEF)
1122 return N0;
1123 if (N1.getOpcode() == ISD::UNDEF)
1124 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001125
Nate Begeman83e75ec2005-09-06 04:43:02 +00001126 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001127}
1128
Nate Begeman83e75ec2005-09-06 04:43:02 +00001129SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001130 SDOperand N0 = N->getOperand(0);
1131 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001132 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1133 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +00001134 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001135
Dan Gohman7f321562007-06-25 16:23:39 +00001136 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001137 if (MVT::isVector(VT)) {
1138 SDOperand FoldedVOp = SimplifyVBinOp(N);
1139 if (FoldedVOp.Val) return FoldedVOp;
1140 }
Dan Gohman7f321562007-06-25 16:23:39 +00001141
Dan Gohman613e0d82007-07-03 14:03:57 +00001142 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001143 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001144 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001145 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001146 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001147 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001148 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001149 if (N0C && !N1C)
1150 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001151 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001152 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001153 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001154 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001155 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001156 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001157 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +00001158 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +00001159 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001160 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001161 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001162 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1163 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1164 // FIXME: If the input is something that is easily negated (e.g. a
1165 // single-use add), we should put the negate there.
1166 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1167 DAG.getNode(ISD::SHL, VT, N0,
1168 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1169 TLI.getShiftAmountTy())));
1170 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001171
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001172 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1173 if (N1C && N0.getOpcode() == ISD::SHL &&
1174 isa<ConstantSDNode>(N0.getOperand(1))) {
1175 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001176 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001177 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1178 }
1179
1180 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1181 // use.
1182 {
1183 SDOperand Sh(0,0), Y(0,0);
1184 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1185 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1186 N0.Val->hasOneUse()) {
1187 Sh = N0; Y = N1;
1188 } else if (N1.getOpcode() == ISD::SHL &&
1189 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1190 Sh = N1; Y = N0;
1191 }
1192 if (Sh.Val) {
1193 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1194 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1195 }
1196 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001197 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1198 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1199 isa<ConstantSDNode>(N0.getOperand(1))) {
1200 return DAG.getNode(ISD::ADD, VT,
1201 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1202 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1203 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001204
Nate Begemancd4d58c2006-02-03 06:46:56 +00001205 // reassociate mul
1206 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1207 if (RMUL.Val != 0)
1208 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001209
Nate Begeman83e75ec2005-09-06 04:43:02 +00001210 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001211}
1212
Nate Begeman83e75ec2005-09-06 04:43:02 +00001213SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001214 SDOperand N0 = N->getOperand(0);
1215 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001216 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1217 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001218 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001219
Dan Gohman7f321562007-06-25 16:23:39 +00001220 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001221 if (MVT::isVector(VT)) {
1222 SDOperand FoldedVOp = SimplifyVBinOp(N);
1223 if (FoldedVOp.Val) return FoldedVOp;
1224 }
Dan Gohman7f321562007-06-25 16:23:39 +00001225
Nate Begeman1d4d4142005-09-01 00:19:25 +00001226 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001227 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001228 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001229 // fold (sdiv X, 1) -> X
1230 if (N1C && N1C->getSignExtended() == 1LL)
1231 return N0;
1232 // fold (sdiv X, -1) -> 0-X
1233 if (N1C && N1C->isAllOnesValue())
1234 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001235 // If we know the sign bits of both operands are zero, strength reduce to a
1236 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Chris Lattnerf32aac32008-01-27 23:32:17 +00001237 if (!MVT::isVector(VT)) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001238 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerf32aac32008-01-27 23:32:17 +00001239 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1240 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001241 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +00001242 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001243 (isPowerOf2_64(N1C->getSignExtended()) ||
1244 isPowerOf2_64(-N1C->getSignExtended()))) {
1245 // If dividing by powers of two is cheap, then don't perform the following
1246 // fold.
1247 if (TLI.isPow2DivCheap())
1248 return SDOperand();
1249 int64_t pow2 = N1C->getSignExtended();
1250 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001251 unsigned lg2 = Log2_64(abs2);
1252 // Splat the sign bit into the register
1253 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001254 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1255 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001256 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001257 // Add (N0 < 0) ? abs2 - 1 : 0;
1258 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1259 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001260 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001261 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001262 AddToWorkList(SRL.Val);
1263 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001264 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1265 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001266 // If we're dividing by a positive value, we're done. Otherwise, we must
1267 // negate the result.
1268 if (pow2 > 0)
1269 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001270 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001271 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1272 }
Nate Begeman69575232005-10-20 02:15:44 +00001273 // if integer divide is expensive and we satisfy the requirements, emit an
1274 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001275 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001276 !TLI.isIntDivCheap()) {
1277 SDOperand Op = BuildSDIV(N);
1278 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001279 }
Dan Gohman7f321562007-06-25 16:23:39 +00001280
Dan Gohman613e0d82007-07-03 14:03:57 +00001281 // undef / X -> 0
1282 if (N0.getOpcode() == ISD::UNDEF)
1283 return DAG.getConstant(0, VT);
1284 // X / undef -> undef
1285 if (N1.getOpcode() == ISD::UNDEF)
1286 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001287
Nate Begeman83e75ec2005-09-06 04:43:02 +00001288 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001289}
1290
Nate Begeman83e75ec2005-09-06 04:43:02 +00001291SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001292 SDOperand N0 = N->getOperand(0);
1293 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001294 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1295 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001296 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001297
Dan Gohman7f321562007-06-25 16:23:39 +00001298 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001299 if (MVT::isVector(VT)) {
1300 SDOperand FoldedVOp = SimplifyVBinOp(N);
1301 if (FoldedVOp.Val) return FoldedVOp;
1302 }
Dan Gohman7f321562007-06-25 16:23:39 +00001303
Nate Begeman1d4d4142005-09-01 00:19:25 +00001304 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001305 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001306 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001307 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +00001308 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001309 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001310 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001311 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001312 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1313 if (N1.getOpcode() == ISD::SHL) {
1314 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1315 if (isPowerOf2_64(SHC->getValue())) {
1316 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001317 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
1318 DAG.getConstant(Log2_64(SHC->getValue()),
1319 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001320 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001321 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001322 }
1323 }
1324 }
Nate Begeman69575232005-10-20 02:15:44 +00001325 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +00001326 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
1327 SDOperand Op = BuildUDIV(N);
1328 if (Op.Val) return Op;
1329 }
Dan Gohman7f321562007-06-25 16:23:39 +00001330
Dan Gohman613e0d82007-07-03 14:03:57 +00001331 // undef / X -> 0
1332 if (N0.getOpcode() == ISD::UNDEF)
1333 return DAG.getConstant(0, VT);
1334 // X / undef -> undef
1335 if (N1.getOpcode() == ISD::UNDEF)
1336 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001337
Nate Begeman83e75ec2005-09-06 04:43:02 +00001338 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001339}
1340
Nate Begeman83e75ec2005-09-06 04:43:02 +00001341SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001342 SDOperand N0 = N->getOperand(0);
1343 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001344 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1345 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001346 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001347
1348 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001349 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001350 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001351 // If we know the sign bits of both operands are zero, strength reduce to a
1352 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Chris Lattneree339f42008-01-27 23:21:58 +00001353 if (!MVT::isVector(VT)) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001354 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001355 return DAG.getNode(ISD::UREM, VT, N0, N1);
1356 }
Chris Lattner26d29902006-10-12 20:58:32 +00001357
Dan Gohman77003042007-11-26 23:46:11 +00001358 // If X/C can be simplified by the division-by-constant logic, lower
1359 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001360 if (N1C && !N1C->isNullValue()) {
1361 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Chris Lattner5eee4272008-01-26 01:09:19 +00001362 AddToWorkList(Div.Val);
Dan Gohman77003042007-11-26 23:46:11 +00001363 SDOperand OptimizedDiv = combine(Div.Val);
1364 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1365 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1366 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1367 AddToWorkList(Mul.Val);
1368 return Sub;
1369 }
Chris Lattner26d29902006-10-12 20:58:32 +00001370 }
1371
Dan Gohman613e0d82007-07-03 14:03:57 +00001372 // undef % X -> 0
1373 if (N0.getOpcode() == ISD::UNDEF)
1374 return DAG.getConstant(0, VT);
1375 // X % undef -> undef
1376 if (N1.getOpcode() == ISD::UNDEF)
1377 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001378
Nate Begeman83e75ec2005-09-06 04:43:02 +00001379 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001380}
1381
Nate Begeman83e75ec2005-09-06 04:43:02 +00001382SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001383 SDOperand N0 = N->getOperand(0);
1384 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001385 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1386 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001387 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001388
1389 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001390 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001391 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001392 // fold (urem x, pow2) -> (and x, pow2-1)
1393 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +00001394 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001395 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1396 if (N1.getOpcode() == ISD::SHL) {
1397 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1398 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +00001399 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001400 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001401 return DAG.getNode(ISD::AND, VT, N0, Add);
1402 }
1403 }
1404 }
Chris Lattner26d29902006-10-12 20:58:32 +00001405
Dan Gohman77003042007-11-26 23:46:11 +00001406 // If X/C can be simplified by the division-by-constant logic, lower
1407 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001408 if (N1C && !N1C->isNullValue()) {
1409 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman77003042007-11-26 23:46:11 +00001410 SDOperand OptimizedDiv = combine(Div.Val);
1411 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1412 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1413 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1414 AddToWorkList(Mul.Val);
1415 return Sub;
1416 }
Chris Lattner26d29902006-10-12 20:58:32 +00001417 }
1418
Dan Gohman613e0d82007-07-03 14:03:57 +00001419 // undef % X -> 0
1420 if (N0.getOpcode() == ISD::UNDEF)
1421 return DAG.getConstant(0, VT);
1422 // X % undef -> undef
1423 if (N1.getOpcode() == ISD::UNDEF)
1424 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001425
Nate Begeman83e75ec2005-09-06 04:43:02 +00001426 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001427}
1428
Nate Begeman83e75ec2005-09-06 04:43:02 +00001429SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001430 SDOperand N0 = N->getOperand(0);
1431 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001432 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001433 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001434
1435 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001436 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001437 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001438 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001439 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001440 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1441 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001442 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001443 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001444 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001445 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001446
Nate Begeman83e75ec2005-09-06 04:43:02 +00001447 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001448}
1449
Nate Begeman83e75ec2005-09-06 04:43:02 +00001450SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001451 SDOperand N0 = N->getOperand(0);
1452 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001453 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001454 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001455
1456 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001457 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001458 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001459 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001460 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001461 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001462 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001463 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001464 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001465
Nate Begeman83e75ec2005-09-06 04:43:02 +00001466 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001467}
1468
Dan Gohman389079b2007-10-08 17:57:15 +00001469/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1470/// compute two values. LoOp and HiOp give the opcodes for the two computations
1471/// that are being performed. Return true if a simplification was made.
1472///
Chris Lattner5eee4272008-01-26 01:09:19 +00001473SDOperand DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1474 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001475 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001476 bool HiExists = N->hasAnyUseOfValue(1);
1477 if (!HiExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001478 (!AfterLegalize ||
1479 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001480 SDOperand Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
1481 N->getNumOperands());
1482 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001483 }
1484
1485 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001486 bool LoExists = N->hasAnyUseOfValue(0);
1487 if (!LoExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001488 (!AfterLegalize ||
1489 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001490 SDOperand Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
1491 N->getNumOperands());
1492 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001493 }
1494
Evan Cheng44711942007-11-08 09:25:29 +00001495 // If both halves are used, return as it is.
1496 if (LoExists && HiExists)
Chris Lattner5eee4272008-01-26 01:09:19 +00001497 return SDOperand();
Evan Cheng44711942007-11-08 09:25:29 +00001498
1499 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001500 if (LoExists) {
1501 SDOperand Lo = DAG.getNode(LoOp, N->getValueType(0),
1502 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001503 AddToWorkList(Lo.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001504 SDOperand LoOpt = combine(Lo.Val);
Chris Lattner5eee4272008-01-26 01:09:19 +00001505 if (LoOpt.Val && LoOpt.Val != Lo.Val &&
1506 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType()))
1507 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001508 }
1509
Evan Cheng44711942007-11-08 09:25:29 +00001510 if (HiExists) {
1511 SDOperand Hi = DAG.getNode(HiOp, N->getValueType(1),
1512 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001513 AddToWorkList(Hi.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001514 SDOperand HiOpt = combine(Hi.Val);
1515 if (HiOpt.Val && HiOpt != Hi &&
Chris Lattner5eee4272008-01-26 01:09:19 +00001516 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType()))
1517 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001518 }
Chris Lattner5eee4272008-01-26 01:09:19 +00001519 return SDOperand();
Dan Gohman389079b2007-10-08 17:57:15 +00001520}
1521
1522SDOperand DAGCombiner::visitSMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001523 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
1524 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001525
1526 return SDOperand();
1527}
1528
1529SDOperand DAGCombiner::visitUMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001530 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
1531 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001532
1533 return SDOperand();
1534}
1535
1536SDOperand DAGCombiner::visitSDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001537 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
1538 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001539
1540 return SDOperand();
1541}
1542
1543SDOperand DAGCombiner::visitUDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001544 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
1545 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001546
1547 return SDOperand();
1548}
1549
Chris Lattner35e5c142006-05-05 05:51:50 +00001550/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1551/// two operands of the same opcode, try to simplify it.
1552SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1553 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1554 MVT::ValueType VT = N0.getValueType();
1555 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1556
Chris Lattner540121f2006-05-05 06:31:05 +00001557 // For each of OP in AND/OR/XOR:
1558 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1559 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1560 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001561 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001562 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001563 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001564 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1565 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1566 N0.getOperand(0).getValueType(),
1567 N0.getOperand(0), N1.getOperand(0));
1568 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001569 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001570 }
1571
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001572 // For each of OP in SHL/SRL/SRA/AND...
1573 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1574 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1575 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001576 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001577 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001578 N0.getOperand(1) == N1.getOperand(1)) {
1579 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1580 N0.getOperand(0).getValueType(),
1581 N0.getOperand(0), N1.getOperand(0));
1582 AddToWorkList(ORNode.Val);
1583 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1584 }
1585
1586 return SDOperand();
1587}
1588
Nate Begeman83e75ec2005-09-06 04:43:02 +00001589SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001590 SDOperand N0 = N->getOperand(0);
1591 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001592 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001593 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1594 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001595 MVT::ValueType VT = N1.getValueType();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001596 unsigned BitWidth = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001597
Dan Gohman7f321562007-06-25 16:23:39 +00001598 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001599 if (MVT::isVector(VT)) {
1600 SDOperand FoldedVOp = SimplifyVBinOp(N);
1601 if (FoldedVOp.Val) return FoldedVOp;
1602 }
Dan Gohman7f321562007-06-25 16:23:39 +00001603
Dan Gohman613e0d82007-07-03 14:03:57 +00001604 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001605 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001606 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001607 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001608 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001609 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001610 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001611 if (N0C && !N1C)
1612 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001613 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001614 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001615 return N0;
1616 // if (and x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001617 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
1618 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001619 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001620 // reassociate and
1621 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1622 if (RAND.Val != 0)
1623 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001624 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001625 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001626 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001627 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001628 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001629 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1630 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001631 SDOperand N0Op0 = N0.getOperand(0);
1632 APInt Mask = ~N1C->getAPIntValue();
1633 Mask.trunc(N0Op0.getValueSizeInBits());
1634 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001635 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001636 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001637
1638 // Replace uses of the AND with uses of the Zero extend node.
1639 CombineTo(N, Zext);
1640
Chris Lattner3603cd62006-02-02 07:17:31 +00001641 // We actually want to replace all uses of the any_extend with the
1642 // zero_extend, to avoid duplicating things. This will later cause this
1643 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001644 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001645 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001646 }
1647 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001648 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1649 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1650 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1651 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1652
1653 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1654 MVT::isInteger(LL.getValueType())) {
1655 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1656 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1657 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001658 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001659 return DAG.getSetCC(VT, ORNode, LR, Op1);
1660 }
1661 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1662 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1663 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001664 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001665 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1666 }
1667 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1668 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1669 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001670 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001671 return DAG.getSetCC(VT, ORNode, LR, Op1);
1672 }
1673 }
1674 // canonicalize equivalent to ll == rl
1675 if (LL == RR && LR == RL) {
1676 Op1 = ISD::getSetCCSwappedOperands(Op1);
1677 std::swap(RL, RR);
1678 }
1679 if (LL == RL && LR == RR) {
1680 bool isInteger = MVT::isInteger(LL.getValueType());
1681 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1682 if (Result != ISD::SETCC_INVALID)
1683 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1684 }
1685 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001686
1687 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1688 if (N0.getOpcode() == N1.getOpcode()) {
1689 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1690 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001691 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001692
Nate Begemande996292006-02-03 22:24:05 +00001693 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1694 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001695 if (!MVT::isVector(VT) &&
1696 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001697 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001698 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001699 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001700 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001701 MVT::ValueType EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001702 // If we zero all the possible extended bits, then we can turn this into
1703 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001704 unsigned BitWidth = N1.getValueSizeInBits();
1705 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
1706 BitWidth - MVT::getSizeInBits(EVT))) &&
Evan Chengc5484282006-10-04 00:56:09 +00001707 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001708 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1709 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001710 LN0->getSrcValueOffset(), EVT,
1711 LN0->isVolatile(),
1712 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001713 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001714 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001715 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001716 }
1717 }
1718 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001719 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1720 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001721 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001722 MVT::ValueType EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001723 // If we zero all the possible extended bits, then we can turn this into
1724 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001725 unsigned BitWidth = N1.getValueSizeInBits();
1726 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
1727 BitWidth - MVT::getSizeInBits(EVT))) &&
Evan Chengc5484282006-10-04 00:56:09 +00001728 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001729 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1730 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001731 LN0->getSrcValueOffset(), EVT,
1732 LN0->isVolatile(),
1733 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001734 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001735 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001736 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001737 }
1738 }
Chris Lattner15045b62006-02-28 06:35:35 +00001739
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001740 // fold (and (load x), 255) -> (zextload x, i8)
1741 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001742 if (N1C && N0.getOpcode() == ISD::LOAD) {
1743 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1744 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Chris Lattnerddf89562008-01-17 19:59:44 +00001745 LN0->isUnindexed() && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001746 MVT::ValueType EVT, LoadedVT;
1747 if (N1C->getValue() == 255)
1748 EVT = MVT::i8;
1749 else if (N1C->getValue() == 65535)
1750 EVT = MVT::i16;
1751 else if (N1C->getValue() == ~0U)
1752 EVT = MVT::i32;
1753 else
1754 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001755
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001756 LoadedVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001757 if (EVT != MVT::Other && LoadedVT > EVT &&
1758 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1759 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1760 // For big endian targets, we need to add an offset to the pointer to
1761 // load the correct bytes. For little endian systems, we merely need to
1762 // read fewer bytes from the same pointer.
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001763 unsigned LVTStoreBytes = MVT::getStoreSizeInBits(LoadedVT)/8;
1764 unsigned EVTStoreBytes = MVT::getStoreSizeInBits(EVT)/8;
1765 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001766 unsigned Alignment = LN0->getAlignment();
Evan Cheng466685d2006-10-09 20:57:25 +00001767 SDOperand NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001768 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001769 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1770 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001771 Alignment = MinAlign(Alignment, PtrOff);
1772 }
Evan Cheng466685d2006-10-09 20:57:25 +00001773 AddToWorkList(NewPtr.Val);
1774 SDOperand Load =
1775 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001776 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001777 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001778 AddToWorkList(N);
1779 CombineTo(N0.Val, Load, Load.getValue(1));
1780 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1781 }
Chris Lattner15045b62006-02-28 06:35:35 +00001782 }
1783 }
1784
Nate Begeman83e75ec2005-09-06 04:43:02 +00001785 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001786}
1787
Nate Begeman83e75ec2005-09-06 04:43:02 +00001788SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001789 SDOperand N0 = N->getOperand(0);
1790 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001791 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001792 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1793 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001794 MVT::ValueType VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001795
Dan Gohman7f321562007-06-25 16:23:39 +00001796 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001797 if (MVT::isVector(VT)) {
1798 SDOperand FoldedVOp = SimplifyVBinOp(N);
1799 if (FoldedVOp.Val) return FoldedVOp;
1800 }
Dan Gohman7f321562007-06-25 16:23:39 +00001801
Dan Gohman613e0d82007-07-03 14:03:57 +00001802 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001803 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001804 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001805 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001806 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001807 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001808 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001809 if (N0C && !N1C)
1810 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001811 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001812 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001813 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001814 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001815 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001816 return N1;
1817 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001818 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001819 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001820 // reassociate or
1821 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1822 if (ROR.Val != 0)
1823 return ROR;
1824 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1825 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001826 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001827 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1828 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1829 N1),
1830 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001831 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001832 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1833 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1834 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1835 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1836
1837 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1838 MVT::isInteger(LL.getValueType())) {
1839 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1840 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1841 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1842 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1843 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001844 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001845 return DAG.getSetCC(VT, ORNode, LR, Op1);
1846 }
1847 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1848 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1849 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1850 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1851 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001852 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001853 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1854 }
1855 }
1856 // canonicalize equivalent to ll == rl
1857 if (LL == RR && LR == RL) {
1858 Op1 = ISD::getSetCCSwappedOperands(Op1);
1859 std::swap(RL, RR);
1860 }
1861 if (LL == RL && LR == RR) {
1862 bool isInteger = MVT::isInteger(LL.getValueType());
1863 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1864 if (Result != ISD::SETCC_INVALID)
1865 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1866 }
1867 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001868
1869 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1870 if (N0.getOpcode() == N1.getOpcode()) {
1871 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1872 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001873 }
Chris Lattner516b9622006-09-14 20:50:57 +00001874
Chris Lattner1ec72732006-09-14 21:11:37 +00001875 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1876 if (N0.getOpcode() == ISD::AND &&
1877 N1.getOpcode() == ISD::AND &&
1878 N0.getOperand(1).getOpcode() == ISD::Constant &&
1879 N1.getOperand(1).getOpcode() == ISD::Constant &&
1880 // Don't increase # computations.
1881 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1882 // We can only do this xform if we know that bits from X that are set in C2
1883 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001884 const APInt &LHSMask =
1885 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1886 const APInt &RHSMask =
1887 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00001888
Dan Gohmanea859be2007-06-22 14:59:07 +00001889 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1890 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001891 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1892 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1893 }
1894 }
1895
1896
Chris Lattner516b9622006-09-14 20:50:57 +00001897 // See if this is some rotate idiom.
1898 if (SDNode *Rot = MatchRotate(N0, N1))
1899 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001900
Nate Begeman83e75ec2005-09-06 04:43:02 +00001901 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001902}
1903
Chris Lattner516b9622006-09-14 20:50:57 +00001904
1905/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1906static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1907 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001908 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001909 Mask = Op.getOperand(1);
1910 Op = Op.getOperand(0);
1911 } else {
1912 return false;
1913 }
1914 }
1915
1916 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1917 Shift = Op;
1918 return true;
1919 }
1920 return false;
1921}
1922
1923
1924// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1925// idioms for rotate, and if the target supports rotation instructions, generate
1926// a rot[lr].
1927SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1928 // Must be a legal type. Expanded an promoted things won't work with rotates.
1929 MVT::ValueType VT = LHS.getValueType();
1930 if (!TLI.isTypeLegal(VT)) return 0;
1931
1932 // The target must have at least one rotate flavor.
1933 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1934 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1935 if (!HasROTL && !HasROTR) return 0;
1936
1937 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1938 SDOperand LHSShift; // The shift.
1939 SDOperand LHSMask; // AND value if any.
1940 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1941 return 0; // Not part of a rotate.
1942
1943 SDOperand RHSShift; // The shift.
1944 SDOperand RHSMask; // AND value if any.
1945 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1946 return 0; // Not part of a rotate.
1947
1948 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1949 return 0; // Not shifting the same value.
1950
1951 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1952 return 0; // Shifts must disagree.
1953
1954 // Canonicalize shl to left side in a shl/srl pair.
1955 if (RHSShift.getOpcode() == ISD::SHL) {
1956 std::swap(LHS, RHS);
1957 std::swap(LHSShift, RHSShift);
1958 std::swap(LHSMask , RHSMask );
1959 }
1960
1961 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Scott Michelc9dc1142007-04-02 21:36:32 +00001962 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1963 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1964 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001965
1966 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1967 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001968 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1969 RHSShiftAmt.getOpcode() == ISD::Constant) {
1970 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
1971 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00001972 if ((LShVal + RShVal) != OpSizeInBits)
1973 return 0;
1974
1975 SDOperand Rot;
1976 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001977 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001978 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001979 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001980
1981 // If there is an AND of either shifted operand, apply it to the result.
1982 if (LHSMask.Val || RHSMask.Val) {
1983 uint64_t Mask = MVT::getIntVTBitMask(VT);
1984
1985 if (LHSMask.Val) {
1986 uint64_t RHSBits = (1ULL << LShVal)-1;
1987 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1988 }
1989 if (RHSMask.Val) {
1990 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1991 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1992 }
1993
1994 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1995 }
1996
1997 return Rot.Val;
1998 }
1999
2000 // If there is a mask here, and we have a variable shift, we can't be sure
2001 // that we're masking out the right stuff.
2002 if (LHSMask.Val || RHSMask.Val)
2003 return 0;
2004
2005 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2006 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002007 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2008 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002009 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002010 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002011 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002012 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002013 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002014 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002015 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002016 }
Chris Lattner516b9622006-09-14 20:50:57 +00002017 }
2018 }
2019
2020 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2021 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002022 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2023 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002024 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002025 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002026 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002027 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002028 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002029 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002030 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002031 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002032 }
2033 }
2034
2035 // Look for sign/zext/any-extended cases:
2036 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2037 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2038 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
2039 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2040 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2041 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
2042 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
2043 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
2044 if (RExtOp0.getOpcode() == ISD::SUB &&
2045 RExtOp0.getOperand(1) == LExtOp0) {
2046 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2047 // (rotr x, y)
2048 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2049 // (rotl x, (sub 32, y))
2050 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
2051 if (SUBC->getValue() == OpSizeInBits) {
2052 if (HasROTL)
2053 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2054 else
2055 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
2056 }
2057 }
2058 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2059 RExtOp0 == LExtOp0.getOperand(1)) {
2060 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2061 // (rotl x, y)
2062 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2063 // (rotr x, (sub 32, y))
2064 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
2065 if (SUBC->getValue() == OpSizeInBits) {
2066 if (HasROTL)
2067 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
2068 else
2069 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2070 }
2071 }
Chris Lattner516b9622006-09-14 20:50:57 +00002072 }
2073 }
2074
2075 return 0;
2076}
2077
2078
Nate Begeman83e75ec2005-09-06 04:43:02 +00002079SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002080 SDOperand N0 = N->getOperand(0);
2081 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002082 SDOperand LHS, RHS, CC;
2083 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2084 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002085 MVT::ValueType VT = N0.getValueType();
2086
Dan Gohman7f321562007-06-25 16:23:39 +00002087 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00002088 if (MVT::isVector(VT)) {
2089 SDOperand FoldedVOp = SimplifyVBinOp(N);
2090 if (FoldedVOp.Val) return FoldedVOp;
2091 }
Dan Gohman7f321562007-06-25 16:23:39 +00002092
Dan Gohman613e0d82007-07-03 14:03:57 +00002093 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002094 if (N0.getOpcode() == ISD::UNDEF)
2095 return N0;
2096 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002097 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002098 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002099 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002100 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00002101 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002102 if (N0C && !N1C)
2103 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002104 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002105 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002106 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002107 // reassociate xor
2108 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
2109 if (RXOR.Val != 0)
2110 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002111 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00002112 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
2113 bool isInt = MVT::isInteger(LHS.getValueType());
2114 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2115 isInt);
2116 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002117 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002118 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002119 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002120 assert(0 && "Unhandled SetCC Equivalent!");
2121 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002122 }
Chris Lattner61c5ff42007-09-10 21:39:07 +00002123 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
2124 if (N1C && N1C->getValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
2125 N0.Val->hasOneUse() && isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
2126 SDOperand V = N0.getOperand(0);
2127 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002128 DAG.getConstant(1, V.getValueType()));
Chris Lattner61c5ff42007-09-10 21:39:07 +00002129 AddToWorkList(V.Val);
2130 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2131 }
2132
Nate Begeman99801192005-09-07 23:25:52 +00002133 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Chris Lattner734c91d2006-11-10 21:37:15 +00002134 if (N1C && N1C->getValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002135 (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 (isOneUseSetCC(RHS) || isOneUseSetCC(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 Begeman99801192005-09-07 23:25:52 +00002145 // fold !(x or y) -> (!x and !y) iff x or y are constants
2146 if (N1C && N1C->isAllOnesValue() &&
2147 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002148 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002149 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2150 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002151 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2152 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002153 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002154 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002155 }
2156 }
Nate Begeman223df222005-09-08 20:18:10 +00002157 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2158 if (N1C && N0.getOpcode() == ISD::XOR) {
2159 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2160 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2161 if (N00C)
2162 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
2163 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
2164 if (N01C)
2165 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
2166 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
2167 }
2168 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002169 if (N0 == N1) {
2170 if (!MVT::isVector(VT)) {
2171 return DAG.getConstant(0, VT);
2172 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2173 // Produce a vector of zeros.
Dan Gohman51eaa862007-06-14 22:58:02 +00002174 SDOperand El = DAG.getConstant(0, MVT::getVectorElementType(VT));
Chris Lattner4fbdd592006-03-28 19:11:05 +00002175 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002176 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002177 }
2178 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002179
2180 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2181 if (N0.getOpcode() == N1.getOpcode()) {
2182 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
2183 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002184 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002185
Chris Lattner3e104b12006-04-08 04:15:24 +00002186 // Simplify the expression using non-local knowledge.
2187 if (!MVT::isVector(VT) &&
2188 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002189 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002190
Nate Begeman83e75ec2005-09-06 04:43:02 +00002191 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002192}
2193
Chris Lattnere70da202007-12-06 07:33:36 +00002194/// visitShiftByConstant - Handle transforms common to the three shifts, when
2195/// the shift amount is a constant.
2196SDOperand DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
2197 SDNode *LHS = N->getOperand(0).Val;
2198 if (!LHS->hasOneUse()) return SDOperand();
2199
2200 // We want to pull some binops through shifts, so that we have (and (shift))
2201 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2202 // thing happens with address calculations, so it's important to canonicalize
2203 // it.
2204 bool HighBitSet = false; // Can we transform this if the high bit is set?
2205
2206 switch (LHS->getOpcode()) {
2207 default: return SDOperand();
2208 case ISD::OR:
2209 case ISD::XOR:
2210 HighBitSet = false; // We can only transform sra if the high bit is clear.
2211 break;
2212 case ISD::AND:
2213 HighBitSet = true; // We can only transform sra if the high bit is set.
2214 break;
2215 case ISD::ADD:
2216 if (N->getOpcode() != ISD::SHL)
2217 return SDOperand(); // only shl(add) not sr[al](add).
2218 HighBitSet = false; // We can only transform sra if the high bit is clear.
2219 break;
2220 }
2221
2222 // We require the RHS of the binop to be a constant as well.
2223 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
2224 if (!BinOpCst) return SDOperand();
2225
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002226
2227 // FIXME: disable this for unless the input to the binop is a shift by a
2228 // constant. If it is not a shift, it pessimizes some common cases like:
2229 //
2230 //void foo(int *X, int i) { X[i & 1235] = 1; }
2231 //int bar(int *X, int i) { return X[i & 255]; }
2232 SDNode *BinOpLHSVal = LHS->getOperand(0).Val;
2233 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2234 BinOpLHSVal->getOpcode() != ISD::SRA &&
2235 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2236 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
2237 return SDOperand();
2238
Chris Lattnere70da202007-12-06 07:33:36 +00002239 MVT::ValueType VT = N->getValueType(0);
2240
2241 // If this is a signed shift right, and the high bit is modified
2242 // by the logical operation, do not perform the transformation.
2243 // The highBitSet boolean indicates the value of the high bit of
2244 // the constant which would cause it to be modified for this
2245 // operation.
2246 if (N->getOpcode() == ISD::SRA) {
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002247 uint64_t BinOpRHSSign = BinOpCst->getValue() >> (MVT::getSizeInBits(VT)-1);
Chris Lattnere70da202007-12-06 07:33:36 +00002248 if ((bool)BinOpRHSSign != HighBitSet)
2249 return SDOperand();
2250 }
2251
2252 // Fold the constants, shifting the binop RHS by the shift amount.
2253 SDOperand NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
2254 LHS->getOperand(1), N->getOperand(1));
2255
2256 // Create the new shift.
2257 SDOperand NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
2258 N->getOperand(1));
2259
2260 // Create the new binop.
2261 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2262}
2263
2264
Nate Begeman83e75ec2005-09-06 04:43:02 +00002265SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002266 SDOperand N0 = N->getOperand(0);
2267 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002268 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2269 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002270 MVT::ValueType VT = N0.getValueType();
2271 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2272
2273 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002274 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002275 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002276 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002277 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002278 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002279 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002280 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002281 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002282 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002283 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002284 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002285 // if (shl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002286 if (DAG.MaskedValueIsZero(SDOperand(N, 0),
2287 APInt::getAllOnesValue(MVT::getSizeInBits(VT))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002288 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00002289 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002290 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002291 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002292 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002293 N0.getOperand(1).getOpcode() == ISD::Constant) {
2294 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002295 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002296 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002297 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002298 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002299 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002300 }
2301 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2302 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002303 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002304 N0.getOperand(1).getOpcode() == ISD::Constant) {
2305 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002306 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002307 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
2308 DAG.getConstant(~0ULL << c1, VT));
2309 if (c2 > c1)
2310 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002311 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002312 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002313 return DAG.getNode(ISD::SRL, VT, Mask,
2314 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002315 }
2316 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002317 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002318 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002319 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002320
2321 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002322}
2323
Nate Begeman83e75ec2005-09-06 04:43:02 +00002324SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002325 SDOperand N0 = N->getOperand(0);
2326 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002327 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2328 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002329 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002330
2331 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002332 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002333 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002334 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002335 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002336 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002337 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002338 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002339 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002340 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00002341 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002342 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002343 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002344 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002345 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002346 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2347 // sext_inreg.
2348 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
2349 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
2350 MVT::ValueType EVT;
2351 switch (LowBits) {
2352 default: EVT = MVT::Other; break;
2353 case 1: EVT = MVT::i1; break;
2354 case 8: EVT = MVT::i8; break;
2355 case 16: EVT = MVT::i16; break;
2356 case 32: EVT = MVT::i32; break;
2357 }
2358 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
2359 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2360 DAG.getValueType(EVT));
2361 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002362
2363 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2364 if (N1C && N0.getOpcode() == ISD::SRA) {
2365 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2366 unsigned Sum = N1C->getValue() + C1->getValue();
2367 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
2368 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2369 DAG.getConstant(Sum, N1C->getValueType(0)));
2370 }
2371 }
2372
Chris Lattnera8504462006-05-08 20:51:54 +00002373 // Simplify, based on bits shifted out of the LHS.
2374 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2375 return SDOperand(N, 0);
2376
2377
Nate Begeman1d4d4142005-09-01 00:19:25 +00002378 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002379 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002380 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002381
2382 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002383}
2384
Nate Begeman83e75ec2005-09-06 04:43:02 +00002385SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002386 SDOperand N0 = N->getOperand(0);
2387 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002388 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2389 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002390 MVT::ValueType VT = N0.getValueType();
2391 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2392
2393 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002394 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002395 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002396 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002397 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002398 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002399 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002400 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002401 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002402 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002403 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002404 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002405 // if (srl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002406 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
2407 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002408 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002409
Nate Begeman1d4d4142005-09-01 00:19:25 +00002410 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002411 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002412 N0.getOperand(1).getOpcode() == ISD::Constant) {
2413 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002414 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002415 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002416 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002417 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002418 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002419 }
Chris Lattner350bec02006-04-02 06:11:11 +00002420
Chris Lattner06afe072006-05-05 22:53:17 +00002421 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2422 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2423 // Shifting in all undef bits?
2424 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
2425 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
2426 return DAG.getNode(ISD::UNDEF, VT);
2427
2428 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
2429 AddToWorkList(SmallShift.Val);
2430 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2431 }
2432
Chris Lattner3657ffe2006-10-12 20:23:19 +00002433 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2434 // bit, which is unmodified by sra.
2435 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
2436 if (N0.getOpcode() == ISD::SRA)
2437 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2438 }
2439
Chris Lattner350bec02006-04-02 06:11:11 +00002440 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2441 if (N1C && N0.getOpcode() == ISD::CTLZ &&
2442 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002443 APInt KnownZero, KnownOne;
2444 APInt Mask = APInt::getAllOnesValue(MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +00002445 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002446
2447 // If any of the input bits are KnownOne, then the input couldn't be all
2448 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002449 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002450
2451 // If all of the bits input the to ctlz node are known to be zero, then
2452 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002453 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002454 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2455
2456 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2457 if ((UnknownBits & (UnknownBits-1)) == 0) {
2458 // Okay, we know that only that the single bit specified by UnknownBits
2459 // could be set on input to the CTLZ node. If this bit is set, the SRL
2460 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2461 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002462 unsigned ShAmt = UnknownBits.countTrailingZeros();
Chris Lattner350bec02006-04-02 06:11:11 +00002463 SDOperand Op = N0.getOperand(0);
2464 if (ShAmt) {
2465 Op = DAG.getNode(ISD::SRL, VT, Op,
2466 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2467 AddToWorkList(Op.Val);
2468 }
2469 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2470 }
2471 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002472
2473 // fold operands of srl based on knowledge that the low bits are not
2474 // demanded.
2475 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2476 return SDOperand(N, 0);
2477
Chris Lattnere70da202007-12-06 07:33:36 +00002478 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002479}
2480
Nate Begeman83e75ec2005-09-06 04:43:02 +00002481SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002482 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002483 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002484
2485 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002486 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002487 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002488 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002489}
2490
Nate Begeman83e75ec2005-09-06 04:43:02 +00002491SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002492 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002493 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002494
2495 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002496 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002497 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002498 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002499}
2500
Nate Begeman83e75ec2005-09-06 04:43:02 +00002501SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002502 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002503 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002504
2505 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002506 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002507 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002508 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002509}
2510
Nate Begeman452d7be2005-09-16 00:54:12 +00002511SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2512 SDOperand N0 = N->getOperand(0);
2513 SDOperand N1 = N->getOperand(1);
2514 SDOperand N2 = N->getOperand(2);
2515 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2516 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2517 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2518 MVT::ValueType VT = N->getValueType(0);
Evan Cheng571c4782007-08-18 05:57:05 +00002519 MVT::ValueType VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002520
Nate Begeman452d7be2005-09-16 00:54:12 +00002521 // fold select C, X, X -> X
2522 if (N1 == N2)
2523 return N1;
2524 // fold select true, X, Y -> X
2525 if (N0C && !N0C->isNullValue())
2526 return N1;
2527 // fold select false, X, Y -> Y
2528 if (N0C && N0C->isNullValue())
2529 return N2;
2530 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002531 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002532 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002533 // fold select C, 0, 1 -> ~C
2534 if (MVT::isInteger(VT) && MVT::isInteger(VT0) &&
2535 N1C && N2C && N1C->isNullValue() && N2C->getValue() == 1) {
2536 SDOperand XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
2537 if (VT == VT0)
2538 return XORNode;
2539 AddToWorkList(XORNode.Val);
2540 if (MVT::getSizeInBits(VT) > MVT::getSizeInBits(VT0))
2541 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2542 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2543 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002544 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002545 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
2546 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002547 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002548 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2549 }
2550 // fold select C, X, 1 -> ~C | X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002551 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getValue() == 1) {
2552 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002553 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002554 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2555 }
2556 // fold select C, X, 0 -> C & X
2557 // FIXME: this should check for C type == X type, not i1?
2558 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2559 return DAG.getNode(ISD::AND, VT, N0, N1);
2560 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2561 if (MVT::i1 == VT && N0 == N1)
2562 return DAG.getNode(ISD::OR, VT, N0, N2);
2563 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2564 if (MVT::i1 == VT && N0 == N2)
2565 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002566
Chris Lattner40c62d52005-10-18 06:04:22 +00002567 // If we can fold this based on the true/false value, do so.
2568 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002569 return SDOperand(N, 0); // Don't revisit N.
2570
Nate Begeman44728a72005-09-19 22:34:01 +00002571 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002572 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002573 // FIXME:
2574 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2575 // having to say they don't support SELECT_CC on every type the DAG knows
2576 // about, since there is no way to mark an opcode illegal at all value types
2577 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2578 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2579 N1, N2, N0.getOperand(2));
2580 else
2581 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002582 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002583 return SDOperand();
2584}
2585
2586SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002587 SDOperand N0 = N->getOperand(0);
2588 SDOperand N1 = N->getOperand(1);
2589 SDOperand N2 = N->getOperand(2);
2590 SDOperand N3 = N->getOperand(3);
2591 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002592 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2593
Nate Begeman44728a72005-09-19 22:34:01 +00002594 // fold select_cc lhs, rhs, x, x, cc -> x
2595 if (N2 == N3)
2596 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002597
Chris Lattner5f42a242006-09-20 06:19:26 +00002598 // Determine if the condition we're dealing with is constant
2599 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002600 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002601
2602 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
2603 if (SCCC->getValue())
2604 return N2; // cond always true -> true val
2605 else
2606 return N3; // cond always false -> false val
2607 }
2608
2609 // Fold to a simpler select_cc
2610 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2611 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2612 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2613 SCC.getOperand(2));
2614
Chris Lattner40c62d52005-10-18 06:04:22 +00002615 // If we can fold this based on the true/false value, do so.
2616 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002617 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002618
Nate Begeman44728a72005-09-19 22:34:01 +00002619 // fold select_cc into other things, such as min/max/abs
2620 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002621}
2622
2623SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2624 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2625 cast<CondCodeSDNode>(N->getOperand(2))->get());
2626}
2627
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002628// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2629// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2630// transformation. Returns true if extension are possible and the above
2631// mentioned transformation is profitable.
2632static bool ExtendUsesToFormExtLoad(SDNode *N, SDOperand N0,
2633 unsigned ExtOpc,
2634 SmallVector<SDNode*, 4> &ExtendNodes,
2635 TargetLowering &TLI) {
2636 bool HasCopyToRegUses = false;
2637 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
2638 for (SDNode::use_iterator UI = N0.Val->use_begin(), UE = N0.Val->use_end();
2639 UI != UE; ++UI) {
2640 SDNode *User = *UI;
2641 if (User == N)
2642 continue;
2643 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2644 if (User->getOpcode() == ISD::SETCC) {
2645 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2646 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2647 // Sign bits will be lost after a zext.
2648 return false;
2649 bool Add = false;
2650 for (unsigned i = 0; i != 2; ++i) {
2651 SDOperand UseOp = User->getOperand(i);
2652 if (UseOp == N0)
2653 continue;
2654 if (!isa<ConstantSDNode>(UseOp))
2655 return false;
2656 Add = true;
2657 }
2658 if (Add)
2659 ExtendNodes.push_back(User);
2660 } else {
2661 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2662 SDOperand UseOp = User->getOperand(i);
2663 if (UseOp == N0) {
2664 // If truncate from extended type to original load type is free
2665 // on this target, then it's ok to extend a CopyToReg.
2666 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2667 HasCopyToRegUses = true;
2668 else
2669 return false;
2670 }
2671 }
2672 }
2673 }
2674
2675 if (HasCopyToRegUses) {
2676 bool BothLiveOut = false;
2677 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2678 UI != UE; ++UI) {
2679 SDNode *User = *UI;
2680 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2681 SDOperand UseOp = User->getOperand(i);
2682 if (UseOp.Val == N && UseOp.ResNo == 0) {
2683 BothLiveOut = true;
2684 break;
2685 }
2686 }
2687 }
2688 if (BothLiveOut)
2689 // Both unextended and extended values are live out. There had better be
2690 // good a reason for the transformation.
2691 return ExtendNodes.size();
2692 }
2693 return true;
2694}
2695
Nate Begeman83e75ec2005-09-06 04:43:02 +00002696SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002697 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002698 MVT::ValueType VT = N->getValueType(0);
2699
Nate Begeman1d4d4142005-09-01 00:19:25 +00002700 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002701 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002702 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002703
Nate Begeman1d4d4142005-09-01 00:19:25 +00002704 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002705 // fold (sext (aext x)) -> (sext x)
2706 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002707 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002708
Evan Chengc88138f2007-03-22 01:54:19 +00002709 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2710 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Chris Lattner22558872007-02-26 03:13:59 +00002711 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002712 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002713 if (NarrowLoad.Val) {
2714 if (NarrowLoad.Val != N0.Val)
2715 CombineTo(N0.Val, NarrowLoad);
2716 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2717 }
Evan Chengc88138f2007-03-22 01:54:19 +00002718 }
2719
2720 // See if the value being truncated is already sign extended. If so, just
2721 // eliminate the trunc/sext pair.
2722 if (N0.getOpcode() == ISD::TRUNCATE) {
Chris Lattner6007b842006-09-21 06:00:20 +00002723 SDOperand Op = N0.getOperand(0);
Chris Lattner22558872007-02-26 03:13:59 +00002724 unsigned OpBits = MVT::getSizeInBits(Op.getValueType());
2725 unsigned MidBits = MVT::getSizeInBits(N0.getValueType());
2726 unsigned DestBits = MVT::getSizeInBits(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002727 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002728
2729 if (OpBits == DestBits) {
2730 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2731 // bits, it is already ready.
2732 if (NumSignBits > DestBits-MidBits)
2733 return Op;
2734 } else if (OpBits < DestBits) {
2735 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2736 // bits, just sext from i32.
2737 if (NumSignBits > OpBits-MidBits)
2738 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2739 } else {
2740 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2741 // bits, just truncate to i32.
2742 if (NumSignBits > OpBits-MidBits)
2743 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002744 }
Chris Lattner22558872007-02-26 03:13:59 +00002745
2746 // fold (sext (truncate x)) -> (sextinreg x).
2747 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2748 N0.getValueType())) {
2749 if (Op.getValueType() < VT)
2750 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2751 else if (Op.getValueType() > VT)
2752 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2753 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2754 DAG.getValueType(N0.getValueType()));
2755 }
Chris Lattner6007b842006-09-21 06:00:20 +00002756 }
Chris Lattner310b5782006-05-06 23:06:26 +00002757
Evan Cheng110dec22005-12-14 02:19:23 +00002758 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002759 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002760 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002761 bool DoXform = true;
2762 SmallVector<SDNode*, 4> SetCCs;
2763 if (!N0.hasOneUse())
2764 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2765 if (DoXform) {
2766 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2767 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2768 LN0->getBasePtr(), LN0->getSrcValue(),
2769 LN0->getSrcValueOffset(),
2770 N0.getValueType(),
2771 LN0->isVolatile(),
2772 LN0->getAlignment());
2773 CombineTo(N, ExtLoad);
2774 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2775 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2776 // Extend SetCC uses if necessary.
2777 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2778 SDNode *SetCC = SetCCs[i];
2779 SmallVector<SDOperand, 4> Ops;
2780 for (unsigned j = 0; j != 2; ++j) {
2781 SDOperand SOp = SetCC->getOperand(j);
2782 if (SOp == Trunc)
2783 Ops.push_back(ExtLoad);
2784 else
2785 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2786 }
2787 Ops.push_back(SetCC->getOperand(2));
2788 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2789 &Ops[0], Ops.size()));
2790 }
2791 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2792 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002793 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002794
2795 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2796 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002797 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2798 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002799 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002800 MVT::ValueType EVT = LN0->getMemoryVT();
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002801 if (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
2802 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2803 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002804 LN0->getSrcValueOffset(), EVT,
2805 LN0->isVolatile(),
2806 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002807 CombineTo(N, ExtLoad);
2808 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2809 ExtLoad.getValue(1));
2810 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2811 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002812 }
2813
Chris Lattner20a35c32007-04-11 05:32:27 +00002814 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2815 if (N0.getOpcode() == ISD::SETCC) {
2816 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002817 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2818 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2819 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2820 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002821 }
2822
Nate Begeman83e75ec2005-09-06 04:43:02 +00002823 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002824}
2825
Nate Begeman83e75ec2005-09-06 04:43:02 +00002826SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002827 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002828 MVT::ValueType VT = N->getValueType(0);
2829
Nate Begeman1d4d4142005-09-01 00:19:25 +00002830 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002831 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002832 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002833 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002834 // fold (zext (aext x)) -> (zext x)
2835 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002836 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002837
Evan Chengc88138f2007-03-22 01:54:19 +00002838 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2839 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002840 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002841 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002842 if (NarrowLoad.Val) {
2843 if (NarrowLoad.Val != N0.Val)
2844 CombineTo(N0.Val, NarrowLoad);
2845 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2846 }
Evan Chengc88138f2007-03-22 01:54:19 +00002847 }
2848
Chris Lattner6007b842006-09-21 06:00:20 +00002849 // fold (zext (truncate x)) -> (and x, mask)
2850 if (N0.getOpcode() == ISD::TRUNCATE &&
2851 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2852 SDOperand Op = N0.getOperand(0);
2853 if (Op.getValueType() < VT) {
2854 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2855 } else if (Op.getValueType() > VT) {
2856 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2857 }
2858 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2859 }
2860
Chris Lattner111c2282006-09-21 06:14:31 +00002861 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2862 if (N0.getOpcode() == ISD::AND &&
2863 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2864 N0.getOperand(1).getOpcode() == ISD::Constant) {
2865 SDOperand X = N0.getOperand(0).getOperand(0);
2866 if (X.getValueType() < VT) {
2867 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2868 } else if (X.getValueType() > VT) {
2869 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2870 }
2871 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2872 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2873 }
2874
Evan Cheng110dec22005-12-14 02:19:23 +00002875 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002876 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002877 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002878 bool DoXform = true;
2879 SmallVector<SDNode*, 4> SetCCs;
2880 if (!N0.hasOneUse())
2881 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
2882 if (DoXform) {
2883 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2884 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2885 LN0->getBasePtr(), LN0->getSrcValue(),
2886 LN0->getSrcValueOffset(),
2887 N0.getValueType(),
2888 LN0->isVolatile(),
2889 LN0->getAlignment());
2890 CombineTo(N, ExtLoad);
2891 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2892 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2893 // Extend SetCC uses if necessary.
2894 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2895 SDNode *SetCC = SetCCs[i];
2896 SmallVector<SDOperand, 4> Ops;
2897 for (unsigned j = 0; j != 2; ++j) {
2898 SDOperand SOp = SetCC->getOperand(j);
2899 if (SOp == Trunc)
2900 Ops.push_back(ExtLoad);
2901 else
Evan Chengde1631b2007-10-30 20:11:21 +00002902 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002903 }
2904 Ops.push_back(SetCC->getOperand(2));
2905 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2906 &Ops[0], Ops.size()));
2907 }
2908 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2909 }
Evan Cheng110dec22005-12-14 02:19:23 +00002910 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002911
2912 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2913 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002914 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2915 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002916 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002917 MVT::ValueType EVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002918 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2919 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002920 LN0->getSrcValueOffset(), EVT,
2921 LN0->isVolatile(),
2922 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002923 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002924 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2925 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002926 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002927 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002928
2929 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2930 if (N0.getOpcode() == ISD::SETCC) {
2931 SDOperand SCC =
2932 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2933 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00002934 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2935 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002936 }
2937
Nate Begeman83e75ec2005-09-06 04:43:02 +00002938 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002939}
2940
Chris Lattner5ffc0662006-05-05 05:58:59 +00002941SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2942 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002943 MVT::ValueType VT = N->getValueType(0);
2944
2945 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002946 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002947 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2948 // fold (aext (aext x)) -> (aext x)
2949 // fold (aext (zext x)) -> (zext x)
2950 // fold (aext (sext x)) -> (sext x)
2951 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2952 N0.getOpcode() == ISD::ZERO_EXTEND ||
2953 N0.getOpcode() == ISD::SIGN_EXTEND)
2954 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2955
Evan Chengc88138f2007-03-22 01:54:19 +00002956 // fold (aext (truncate (load x))) -> (aext (smaller load x))
2957 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
2958 if (N0.getOpcode() == ISD::TRUNCATE) {
2959 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002960 if (NarrowLoad.Val) {
2961 if (NarrowLoad.Val != N0.Val)
2962 CombineTo(N0.Val, NarrowLoad);
2963 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
2964 }
Evan Chengc88138f2007-03-22 01:54:19 +00002965 }
2966
Chris Lattner84750582006-09-20 06:29:17 +00002967 // fold (aext (truncate x))
2968 if (N0.getOpcode() == ISD::TRUNCATE) {
2969 SDOperand TruncOp = N0.getOperand(0);
2970 if (TruncOp.getValueType() == VT)
2971 return TruncOp; // x iff x size == zext size.
2972 if (TruncOp.getValueType() > VT)
2973 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2974 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2975 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002976
2977 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2978 if (N0.getOpcode() == ISD::AND &&
2979 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2980 N0.getOperand(1).getOpcode() == ISD::Constant) {
2981 SDOperand X = N0.getOperand(0).getOperand(0);
2982 if (X.getValueType() < VT) {
2983 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2984 } else if (X.getValueType() > VT) {
2985 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2986 }
2987 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2988 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2989 }
2990
Chris Lattner5ffc0662006-05-05 05:58:59 +00002991 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002992 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002993 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002994 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2995 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2996 LN0->getBasePtr(), LN0->getSrcValue(),
2997 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002998 N0.getValueType(),
2999 LN0->isVolatile(),
3000 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003001 CombineTo(N, ExtLoad);
3002 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3003 ExtLoad.getValue(1));
3004 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3005 }
3006
3007 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3008 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3009 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003010 if (N0.getOpcode() == ISD::LOAD &&
3011 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003012 N0.hasOneUse()) {
3013 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003014 MVT::ValueType EVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00003015 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
3016 LN0->getChain(), LN0->getBasePtr(),
3017 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003018 LN0->getSrcValueOffset(), EVT,
3019 LN0->isVolatile(),
3020 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003021 CombineTo(N, ExtLoad);
3022 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3023 ExtLoad.getValue(1));
3024 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3025 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003026
3027 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3028 if (N0.getOpcode() == ISD::SETCC) {
3029 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003030 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3031 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003032 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00003033 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003034 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003035 }
3036
Chris Lattner5ffc0662006-05-05 05:58:59 +00003037 return SDOperand();
3038}
3039
Chris Lattner2b4c2792007-10-13 06:35:54 +00003040/// GetDemandedBits - See if the specified operand can be simplified with the
3041/// knowledge that only the bits specified by Mask are used. If so, return the
3042/// simpler operand, otherwise return a null SDOperand.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003043SDOperand DAGCombiner::GetDemandedBits(SDOperand V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003044 switch (V.getOpcode()) {
3045 default: break;
3046 case ISD::OR:
3047 case ISD::XOR:
3048 // If the LHS or RHS don't contribute bits to the or, drop them.
3049 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3050 return V.getOperand(1);
3051 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3052 return V.getOperand(0);
3053 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003054 case ISD::SRL:
3055 // Only look at single-use SRLs.
3056 if (!V.Val->hasOneUse())
3057 break;
3058 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3059 // See if we can recursively simplify the LHS.
3060 unsigned Amt = RHSC->getValue();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003061 APInt NewMask = Mask << Amt;
3062 SDOperand SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Chris Lattnere33544c2007-10-13 06:58:48 +00003063 if (SimplifyLHS.Val) {
3064 return DAG.getNode(ISD::SRL, V.getValueType(),
3065 SimplifyLHS, V.getOperand(1));
3066 }
3067 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003068 }
3069 return SDOperand();
3070}
3071
Evan Chengc88138f2007-03-22 01:54:19 +00003072/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3073/// bits and then truncated to a narrower type and where N is a multiple
3074/// of number of bits of the narrower type, transform it to a narrower load
3075/// from address + N / num of bits of new type. If the result is to be
3076/// extended, also fold the extension to form a extending load.
3077SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
3078 unsigned Opc = N->getOpcode();
3079 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
3080 SDOperand N0 = N->getOperand(0);
3081 MVT::ValueType VT = N->getValueType(0);
3082 MVT::ValueType EVT = N->getValueType(0);
3083
Evan Chenge177e302007-03-23 22:13:36 +00003084 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3085 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003086 if (Opc == ISD::SIGN_EXTEND_INREG) {
3087 ExtType = ISD::SEXTLOAD;
3088 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00003089 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
3090 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00003091 }
3092
3093 unsigned EVTBits = MVT::getSizeInBits(EVT);
3094 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003095 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003096 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3097 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3098 ShAmt = N01->getValue();
3099 // Is the shift amount a multiple of size of VT?
3100 if ((ShAmt & (EVTBits-1)) == 0) {
3101 N0 = N0.getOperand(0);
3102 if (MVT::getSizeInBits(N0.getValueType()) <= EVTBits)
3103 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00003104 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003105 }
3106 }
3107 }
3108
3109 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
3110 // Do not allow folding to i1 here. i1 is implicitly stored in memory in
3111 // zero extended form: by shrinking the load, we lose track of the fact
3112 // that it is already zero extended.
3113 // FIXME: This should be reevaluated.
3114 VT != MVT::i1) {
3115 assert(MVT::getSizeInBits(N0.getValueType()) > EVTBits &&
3116 "Cannot truncate to larger type!");
3117 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3118 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003119 // For big endian targets, we need to adjust the offset to the pointer to
3120 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003121 if (TLI.isBigEndian()) {
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003122 unsigned LVTStoreBits = MVT::getStoreSizeInBits(N0.getValueType());
3123 unsigned EVTStoreBits = MVT::getStoreSizeInBits(EVT);
3124 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3125 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003126 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003127 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Evan Chengc88138f2007-03-22 01:54:19 +00003128 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
3129 DAG.getConstant(PtrOff, PtrType));
3130 AddToWorkList(NewPtr.Val);
3131 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
3132 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003133 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Duncan Sandsdc846502007-10-28 12:59:45 +00003134 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003135 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003136 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00003137 LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003138 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003139 if (CombineSRL) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003140 WorkListRemover DeadNodes(*this);
3141 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3142 &DeadNodes);
Evan Chengb37b80c2007-03-23 20:55:21 +00003143 CombineTo(N->getOperand(0).Val, Load);
3144 } else
3145 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003146 if (ShAmt) {
3147 if (Opc == ISD::SIGN_EXTEND_INREG)
3148 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3149 else
3150 return DAG.getNode(Opc, VT, Load);
3151 }
Evan Chengc88138f2007-03-22 01:54:19 +00003152 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3153 }
3154
3155 return SDOperand();
3156}
3157
Chris Lattner5ffc0662006-05-05 05:58:59 +00003158
Nate Begeman83e75ec2005-09-06 04:43:02 +00003159SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003160 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003161 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003162 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003163 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003164 unsigned VTBits = MVT::getSizeInBits(VT);
Nate Begeman07ed4172005-10-10 21:26:48 +00003165 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003166
Nate Begeman1d4d4142005-09-01 00:19:25 +00003167 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003168 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003169 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003170
Chris Lattner541a24f2006-05-06 22:43:44 +00003171 // If the input is already sign extended, just drop the extension.
Dan Gohmanea859be2007-06-22 14:59:07 +00003172 if (DAG.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003173 return N0;
3174
Nate Begeman646d7e22005-09-02 21:18:40 +00003175 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3176 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3177 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003178 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003179 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003180
Chris Lattner95a5e052007-04-17 19:03:21 +00003181 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003182 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003183 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003184
Chris Lattner95a5e052007-04-17 19:03:21 +00003185 // fold operands of sext_in_reg based on knowledge that the top bits are not
3186 // demanded.
3187 if (SimplifyDemandedBits(SDOperand(N, 0)))
3188 return SDOperand(N, 0);
3189
Evan Chengc88138f2007-03-22 01:54:19 +00003190 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3191 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
3192 SDOperand NarrowLoad = ReduceLoadWidth(N);
3193 if (NarrowLoad.Val)
3194 return NarrowLoad;
3195
Chris Lattner4b37e872006-05-08 21:18:59 +00003196 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3197 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3198 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3199 if (N0.getOpcode() == ISD::SRL) {
3200 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
3201 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
3202 // We can turn this into an SRA iff the input to the SRL is already sign
3203 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003204 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Chris Lattner4b37e872006-05-08 21:18:59 +00003205 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
3206 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3207 }
3208 }
Evan Chengc88138f2007-03-22 01:54:19 +00003209
Nate Begemanded49632005-10-13 03:11:28 +00003210 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00003211 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003212 ISD::isUNINDEXEDLoad(N0.Val) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003213 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003214 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003215 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3216 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3217 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003218 LN0->getSrcValueOffset(), EVT,
3219 LN0->isVolatile(),
3220 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003221 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003222 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003223 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003224 }
3225 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00003226 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
3227 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003228 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003229 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003230 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3231 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3232 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003233 LN0->getSrcValueOffset(), EVT,
3234 LN0->isVolatile(),
3235 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003236 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003237 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003238 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003239 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003240 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003241}
3242
Nate Begeman83e75ec2005-09-06 04:43:02 +00003243SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003244 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003245 MVT::ValueType VT = N->getValueType(0);
3246
3247 // noop truncate
3248 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003249 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003250 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003251 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003252 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003253 // fold (truncate (truncate x)) -> (truncate x)
3254 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003255 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003256 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003257 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3258 N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003259 if (N0.getOperand(0).getValueType() < VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003260 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003261 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003262 else if (N0.getOperand(0).getValueType() > VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003263 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003264 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003265 else
3266 // if the source and dest are the same type, we can drop both the extend
3267 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003268 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003269 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003270
Chris Lattner2b4c2792007-10-13 06:35:54 +00003271 // See if we can simplify the input to this truncate through knowledge that
3272 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3273 // -> trunc y
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003274 SDOperand Shorter =
3275 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
3276 MVT::getSizeInBits(VT)));
Chris Lattner2b4c2792007-10-13 06:35:54 +00003277 if (Shorter.Val)
3278 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3279
Nate Begeman3df4d522005-10-12 20:40:40 +00003280 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003281 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003282 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003283}
3284
Chris Lattner94683772005-12-23 05:30:37 +00003285SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3286 SDOperand N0 = N->getOperand(0);
3287 MVT::ValueType VT = N->getValueType(0);
3288
Dan Gohman7f321562007-06-25 16:23:39 +00003289 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3290 // Only do this before legalize, since afterward the target may be depending
3291 // on the bitconvert.
3292 // First check to see if this is all constant.
3293 if (!AfterLegalize &&
3294 N0.getOpcode() == ISD::BUILD_VECTOR && N0.Val->hasOneUse() &&
3295 MVT::isVector(VT)) {
3296 bool isSimple = true;
3297 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3298 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3299 N0.getOperand(i).getOpcode() != ISD::Constant &&
3300 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3301 isSimple = false;
3302 break;
3303 }
3304
3305 MVT::ValueType DestEltVT = MVT::getVectorElementType(N->getValueType(0));
3306 assert(!MVT::isVector(DestEltVT) &&
3307 "Element type of vector ValueType must not be vector!");
3308 if (isSimple) {
3309 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.Val, DestEltVT);
3310 }
3311 }
3312
Chris Lattner94683772005-12-23 05:30:37 +00003313 // If the input is a constant, let getNode() fold it.
3314 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
3315 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3316 if (Res.Val != N) return Res;
3317 }
3318
Chris Lattnerc8547d82005-12-23 05:37:50 +00003319 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3320 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003321
Chris Lattner57104102005-12-23 05:44:41 +00003322 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003323 // If the resultant load doesn't need a higher alignment than the original!
3324 if (ISD::isNormalLoad(N0.Val) && N0.hasOneUse() &&
Evan Cheng59d5b682007-05-07 21:27:48 +00003325 TLI.isOperationLegal(ISD::LOAD, VT)) {
Evan Cheng466685d2006-10-09 20:57:25 +00003326 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00003327 unsigned Align = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00003328 getABITypeAlignment(MVT::getTypeForValueType(VT));
Evan Cheng59d5b682007-05-07 21:27:48 +00003329 unsigned OrigAlign = LN0->getAlignment();
3330 if (Align <= OrigAlign) {
3331 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
3332 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003333 LN0->isVolatile(), Align);
Evan Cheng59d5b682007-05-07 21:27:48 +00003334 AddToWorkList(N);
3335 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
3336 Load.getValue(1));
3337 return Load;
3338 }
Chris Lattner57104102005-12-23 05:44:41 +00003339 }
3340
Chris Lattner3bd39d42008-01-27 17:42:27 +00003341 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3342 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3343 // This often reduces constant pool loads.
3344 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
3345 N0.Val->hasOneUse() && MVT::isInteger(VT) && !MVT::isVector(VT)) {
3346 SDOperand NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3347 AddToWorkList(NewConv.Val);
3348
3349 uint64_t SignBit = MVT::getIntVTSignBit(VT);
3350 if (N0.getOpcode() == ISD::FNEG)
3351 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3352 assert(N0.getOpcode() == ISD::FABS);
3353 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3354 }
3355
3356 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3357 // Note that we don't handle copysign(x,cst) because this can always be folded
3358 // to an fneg or fabs.
3359 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003360 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
3361 MVT::isInteger(VT) && !MVT::isVector(VT)) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003362 unsigned OrigXWidth = MVT::getSizeInBits(N0.getOperand(1).getValueType());
3363 SDOperand X = DAG.getNode(ISD::BIT_CONVERT, MVT::getIntegerType(OrigXWidth),
3364 N0.getOperand(1));
3365 AddToWorkList(X.Val);
3366
3367 // If X has a different width than the result/lhs, sext it or truncate it.
3368 unsigned VTWidth = MVT::getSizeInBits(VT);
3369 if (OrigXWidth < VTWidth) {
3370 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
3371 AddToWorkList(X.Val);
3372 } else if (OrigXWidth > VTWidth) {
3373 // To get the sign bit in the right place, we have to shift it right
3374 // before truncating.
3375 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3376 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3377 AddToWorkList(X.Val);
3378 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3379 AddToWorkList(X.Val);
3380 }
3381
3382 uint64_t SignBit = MVT::getIntVTSignBit(VT);
3383 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
3384 AddToWorkList(X.Val);
3385
3386 SDOperand Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3387 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
3388 AddToWorkList(Cst.Val);
3389
3390 return DAG.getNode(ISD::OR, VT, X, Cst);
3391 }
3392
Chris Lattner94683772005-12-23 05:30:37 +00003393 return SDOperand();
3394}
3395
Dan Gohman7f321562007-06-25 16:23:39 +00003396/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003397/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3398/// destination element value type.
3399SDOperand DAGCombiner::
Dan Gohman7f321562007-06-25 16:23:39 +00003400ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003401 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
3402
3403 // If this is already the right type, we're done.
3404 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
3405
3406 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
3407 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
3408
3409 // If this is a conversion of N elements of one type to N elements of another
3410 // type, convert each element. This handles FP<->INT cases.
3411 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003412 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003413 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003414 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00003415 AddToWorkList(Ops.back().Val);
3416 }
Dan Gohman7f321562007-06-25 16:23:39 +00003417 MVT::ValueType VT =
3418 MVT::getVectorType(DstEltVT,
3419 MVT::getVectorNumElements(BV->getValueType(0)));
3420 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003421 }
3422
3423 // Otherwise, we're growing or shrinking the elements. To avoid having to
3424 // handle annoying details of growing/shrinking FP values, we convert them to
3425 // int first.
3426 if (MVT::isFloatingPoint(SrcEltVT)) {
3427 // Convert the input float vector to a int vector where the elements are the
3428 // same sizes.
3429 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
3430 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003431 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003432 SrcEltVT = IntVT;
3433 }
3434
3435 // Now we know the input is an integer vector. If the output is a FP type,
3436 // convert to integer first, then to FP of the right size.
3437 if (MVT::isFloatingPoint(DstEltVT)) {
3438 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
3439 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003440 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003441
3442 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003443 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003444 }
3445
3446 // Okay, we know the src/dst types are both integers of differing types.
3447 // Handling growing first.
3448 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
3449 if (SrcBitSize < DstBitSize) {
3450 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3451
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003452 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003453 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003454 i += NumInputsPerOutput) {
3455 bool isLE = TLI.isLittleEndian();
3456 uint64_t NewBits = 0;
3457 bool EltIsUndef = true;
3458 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3459 // Shift the previously computed bits over.
3460 NewBits <<= SrcBitSize;
3461 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
3462 if (Op.getOpcode() == ISD::UNDEF) continue;
3463 EltIsUndef = false;
3464
3465 NewBits |= cast<ConstantSDNode>(Op)->getValue();
3466 }
3467
3468 if (EltIsUndef)
3469 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3470 else
3471 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3472 }
3473
Evan Chengefec7512008-02-18 23:04:32 +00003474 MVT::ValueType VT = MVT::getVectorType(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003475 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003476 }
3477
3478 // Finally, this must be the case where we are shrinking elements: each input
3479 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003480 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003481 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Evan Chengefec7512008-02-18 23:04:32 +00003482 MVT::ValueType VT = MVT::getVectorType(DstEltVT,
3483 NumOutputsPerInput * BV->getNumOperands());
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003484 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003485 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003486 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3487 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3488 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3489 continue;
3490 }
3491 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003492 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
3493 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
Chris Lattner6258fb22006-04-02 02:53:43 +00003494 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Evan Chengefec7512008-02-18 23:04:32 +00003495 if (isS2V && i == 0 && j == 0 && ThisVal == OpVal)
3496 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3497 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
3498 OpVal >>= DstBitSize;
Chris Lattner6258fb22006-04-02 02:53:43 +00003499 }
3500
3501 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003502 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003503 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3504 }
Dan Gohman7f321562007-06-25 16:23:39 +00003505 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003506}
3507
3508
3509
Chris Lattner01b3d732005-09-28 22:28:18 +00003510SDOperand DAGCombiner::visitFADD(SDNode *N) {
3511 SDOperand N0 = N->getOperand(0);
3512 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003513 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3514 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003515 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003516
Dan Gohman7f321562007-06-25 16:23:39 +00003517 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003518 if (MVT::isVector(VT)) {
3519 SDOperand FoldedVOp = SimplifyVBinOp(N);
3520 if (FoldedVOp.Val) return FoldedVOp;
3521 }
Dan Gohman7f321562007-06-25 16:23:39 +00003522
Nate Begemana0e221d2005-10-18 00:28:13 +00003523 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003524 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003525 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003526 // canonicalize constant to RHS
3527 if (N0CFP && !N1CFP)
3528 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003529 // fold (A + (-B)) -> A-B
Chris Lattner0254e702008-02-26 07:04:54 +00003530 if (isNegatibleForFree(N1, AfterLegalize) == 2)
3531 return DAG.getNode(ISD::FSUB, VT, N0,
3532 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner01b3d732005-09-28 22:28:18 +00003533 // fold ((-A) + B) -> B-A
Chris Lattner0254e702008-02-26 07:04:54 +00003534 if (isNegatibleForFree(N0, AfterLegalize) == 2)
3535 return DAG.getNode(ISD::FSUB, VT, N1,
3536 GetNegatedExpression(N0, DAG, AfterLegalize));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003537
3538 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3539 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
3540 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3541 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3542 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3543
Chris Lattner01b3d732005-09-28 22:28:18 +00003544 return SDOperand();
3545}
3546
3547SDOperand DAGCombiner::visitFSUB(SDNode *N) {
3548 SDOperand N0 = N->getOperand(0);
3549 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003550 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3551 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003552 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003553
Dan Gohman7f321562007-06-25 16:23:39 +00003554 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003555 if (MVT::isVector(VT)) {
3556 SDOperand FoldedVOp = SimplifyVBinOp(N);
3557 if (FoldedVOp.Val) return FoldedVOp;
3558 }
Dan Gohman7f321562007-06-25 16:23:39 +00003559
Nate Begemana0e221d2005-10-18 00:28:13 +00003560 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003561 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003562 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003563 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003564 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Chris Lattner0254e702008-02-26 07:04:54 +00003565 if (isNegatibleForFree(N1, AfterLegalize))
3566 return GetNegatedExpression(N1, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003567 return DAG.getNode(ISD::FNEG, VT, N1);
3568 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003569 // fold (A-(-B)) -> A+B
Chris Lattner0254e702008-02-26 07:04:54 +00003570 if (isNegatibleForFree(N1, AfterLegalize))
3571 return DAG.getNode(ISD::FADD, VT, N0,
3572 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003573
Chris Lattner01b3d732005-09-28 22:28:18 +00003574 return SDOperand();
3575}
3576
3577SDOperand DAGCombiner::visitFMUL(SDNode *N) {
3578 SDOperand N0 = N->getOperand(0);
3579 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003580 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3581 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003582 MVT::ValueType VT = N->getValueType(0);
3583
Dan Gohman7f321562007-06-25 16:23:39 +00003584 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003585 if (MVT::isVector(VT)) {
3586 SDOperand FoldedVOp = SimplifyVBinOp(N);
3587 if (FoldedVOp.Val) return FoldedVOp;
3588 }
Dan Gohman7f321562007-06-25 16:23:39 +00003589
Nate Begeman11af4ea2005-10-17 20:40:11 +00003590 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003591 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003592 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003593 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003594 if (N0CFP && !N1CFP)
3595 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003596 // fold (fmul X, 2.0) -> (fadd X, X)
3597 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3598 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003599 // fold (fmul X, -1.0) -> (fneg X)
3600 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3601 return DAG.getNode(ISD::FNEG, VT, N0);
3602
3603 // -X * -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003604 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3605 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003606 // Both can be negated for free, check to see if at least one is cheaper
3607 // negated.
3608 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003609 return DAG.getNode(ISD::FMUL, VT,
3610 GetNegatedExpression(N0, DAG, AfterLegalize),
3611 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003612 }
3613 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003614
3615 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3616 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
3617 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3618 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3619 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3620
Chris Lattner01b3d732005-09-28 22:28:18 +00003621 return SDOperand();
3622}
3623
3624SDOperand DAGCombiner::visitFDIV(SDNode *N) {
3625 SDOperand N0 = N->getOperand(0);
3626 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003627 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3628 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003629 MVT::ValueType VT = N->getValueType(0);
3630
Dan Gohman7f321562007-06-25 16:23:39 +00003631 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003632 if (MVT::isVector(VT)) {
3633 SDOperand FoldedVOp = SimplifyVBinOp(N);
3634 if (FoldedVOp.Val) return FoldedVOp;
3635 }
Dan Gohman7f321562007-06-25 16:23:39 +00003636
Nate Begemana148d982006-01-18 22:35:16 +00003637 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003638 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003639 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003640
3641
3642 // -X / -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003643 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3644 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003645 // Both can be negated for free, check to see if at least one is cheaper
3646 // negated.
3647 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003648 return DAG.getNode(ISD::FDIV, VT,
3649 GetNegatedExpression(N0, DAG, AfterLegalize),
3650 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003651 }
3652 }
3653
Chris Lattner01b3d732005-09-28 22:28:18 +00003654 return SDOperand();
3655}
3656
3657SDOperand DAGCombiner::visitFREM(SDNode *N) {
3658 SDOperand N0 = N->getOperand(0);
3659 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003660 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3661 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003662 MVT::ValueType VT = N->getValueType(0);
3663
Nate Begemana148d982006-01-18 22:35:16 +00003664 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003665 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003666 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003667
Chris Lattner01b3d732005-09-28 22:28:18 +00003668 return SDOperand();
3669}
3670
Chris Lattner12d83032006-03-05 05:30:57 +00003671SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3672 SDOperand N0 = N->getOperand(0);
3673 SDOperand N1 = N->getOperand(1);
3674 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3675 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3676 MVT::ValueType VT = N->getValueType(0);
3677
Dale Johannesendb44bf82007-10-16 23:38:29 +00003678 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003679 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3680
3681 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003682 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003683 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3684 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003685 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003686 return DAG.getNode(ISD::FABS, VT, N0);
3687 else
3688 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3689 }
3690
3691 // copysign(fabs(x), y) -> copysign(x, y)
3692 // copysign(fneg(x), y) -> copysign(x, y)
3693 // copysign(copysign(x,z), y) -> copysign(x, y)
3694 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3695 N0.getOpcode() == ISD::FCOPYSIGN)
3696 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3697
3698 // copysign(x, abs(y)) -> abs(x)
3699 if (N1.getOpcode() == ISD::FABS)
3700 return DAG.getNode(ISD::FABS, VT, N0);
3701
3702 // copysign(x, copysign(y,z)) -> copysign(x, z)
3703 if (N1.getOpcode() == ISD::FCOPYSIGN)
3704 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3705
3706 // copysign(x, fp_extend(y)) -> copysign(x, y)
3707 // copysign(x, fp_round(y)) -> copysign(x, y)
3708 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3709 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3710
3711 return SDOperand();
3712}
3713
3714
Chris Lattner01b3d732005-09-28 22:28:18 +00003715
Nate Begeman83e75ec2005-09-06 04:43:02 +00003716SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003717 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003718 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003719 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003720
3721 // fold (sint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003722 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003723 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003724 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003725}
3726
Nate Begeman83e75ec2005-09-06 04:43:02 +00003727SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003728 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003729 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003730 MVT::ValueType VT = N->getValueType(0);
3731
Nate Begeman1d4d4142005-09-01 00:19:25 +00003732 // fold (uint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003733 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003734 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003735 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003736}
3737
Nate Begeman83e75ec2005-09-06 04:43:02 +00003738SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003739 SDOperand N0 = N->getOperand(0);
3740 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3741 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003742
3743 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003744 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003745 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003746 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003747}
3748
Nate Begeman83e75ec2005-09-06 04:43:02 +00003749SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003750 SDOperand N0 = N->getOperand(0);
3751 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3752 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003753
3754 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00003755 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003756 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003757 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003758}
3759
Nate Begeman83e75ec2005-09-06 04:43:02 +00003760SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003761 SDOperand N0 = N->getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003762 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003763 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3764 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003765
3766 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003767 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00003768 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003769
3770 // fold (fp_round (fp_extend x)) -> x
3771 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3772 return N0.getOperand(0);
3773
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00003774 // fold (fp_round (fp_round x)) -> (fp_round x)
3775 if (N0.getOpcode() == ISD::FP_ROUND) {
3776 // This is a value preserving truncation if both round's are.
3777 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
3778 N0.Val->getConstantOperandVal(1) == 1;
3779 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
3780 DAG.getIntPtrConstant(IsTrunc));
3781 }
3782
Chris Lattner79dbea52006-03-13 06:26:26 +00003783 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3784 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003785 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003786 AddToWorkList(Tmp.Val);
3787 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3788 }
3789
Nate Begeman83e75ec2005-09-06 04:43:02 +00003790 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003791}
3792
Nate Begeman83e75ec2005-09-06 04:43:02 +00003793SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003794 SDOperand N0 = N->getOperand(0);
3795 MVT::ValueType VT = N->getValueType(0);
3796 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003797 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003798
Nate Begeman1d4d4142005-09-01 00:19:25 +00003799 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003800 if (N0CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003801 SDOperand Round = DAG.getConstantFP(N0CFP->getValueAPF(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003802 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003803 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003804 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003805}
3806
Nate Begeman83e75ec2005-09-06 04:43:02 +00003807SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003808 SDOperand N0 = N->getOperand(0);
3809 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3810 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003811
Chris Lattner5938bef2007-12-29 06:55:23 +00003812 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
3813 if (N->hasOneUse() && (*N->use_begin())->getOpcode() == ISD::FP_ROUND)
3814 return SDOperand();
Chris Lattner0bd48932008-01-17 07:00:52 +00003815
Nate Begeman1d4d4142005-09-01 00:19:25 +00003816 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003817 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003818 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003819
3820 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
3821 // value of X.
3822 if (N0.getOpcode() == ISD::FP_ROUND && N0.Val->getConstantOperandVal(1) == 1){
3823 SDOperand In = N0.getOperand(0);
3824 if (In.getValueType() == VT) return In;
3825 if (VT < In.getValueType())
3826 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
3827 return DAG.getNode(ISD::FP_EXTEND, VT, In);
3828 }
3829
3830 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Dale Johannesenb6210fc2007-10-19 20:29:00 +00003831 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003832 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003833 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3834 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3835 LN0->getBasePtr(), LN0->getSrcValue(),
3836 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003837 N0.getValueType(),
3838 LN0->isVolatile(),
3839 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003840 CombineTo(N, ExtLoad);
Chris Lattner0bd48932008-01-17 07:00:52 +00003841 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad,
3842 DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00003843 ExtLoad.getValue(1));
3844 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3845 }
3846
3847
Nate Begeman83e75ec2005-09-06 04:43:02 +00003848 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003849}
3850
Nate Begeman83e75ec2005-09-06 04:43:02 +00003851SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003852 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003853
Chris Lattner0254e702008-02-26 07:04:54 +00003854 if (isNegatibleForFree(N0, AfterLegalize))
3855 return GetNegatedExpression(N0, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003856
Chris Lattner3bd39d42008-01-27 17:42:27 +00003857 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
3858 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00003859 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
3860 MVT::isInteger(N0.getOperand(0).getValueType()) &&
3861 !MVT::isVector(N0.getOperand(0).getValueType())) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003862 SDOperand Int = N0.getOperand(0);
3863 MVT::ValueType IntVT = Int.getValueType();
3864 if (MVT::isInteger(IntVT) && !MVT::isVector(IntVT)) {
3865 Int = DAG.getNode(ISD::XOR, IntVT, Int,
3866 DAG.getConstant(MVT::getIntVTSignBit(IntVT), IntVT));
3867 AddToWorkList(Int.Val);
3868 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
3869 }
3870 }
3871
Nate Begeman83e75ec2005-09-06 04:43:02 +00003872 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003873}
3874
Nate Begeman83e75ec2005-09-06 04:43:02 +00003875SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003876 SDOperand N0 = N->getOperand(0);
3877 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3878 MVT::ValueType VT = N->getValueType(0);
3879
Nate Begeman1d4d4142005-09-01 00:19:25 +00003880 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003881 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003882 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003883 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003884 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003885 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003886 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003887 // fold (fabs (fcopysign x, y)) -> (fabs x)
3888 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
3889 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
3890
Chris Lattner3bd39d42008-01-27 17:42:27 +00003891 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
3892 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00003893 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
3894 MVT::isInteger(N0.getOperand(0).getValueType()) &&
3895 !MVT::isVector(N0.getOperand(0).getValueType())) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003896 SDOperand Int = N0.getOperand(0);
3897 MVT::ValueType IntVT = Int.getValueType();
3898 if (MVT::isInteger(IntVT) && !MVT::isVector(IntVT)) {
3899 Int = DAG.getNode(ISD::AND, IntVT, Int,
3900 DAG.getConstant(~MVT::getIntVTSignBit(IntVT), IntVT));
3901 AddToWorkList(Int.Val);
3902 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
3903 }
3904 }
3905
Nate Begeman83e75ec2005-09-06 04:43:02 +00003906 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003907}
3908
Nate Begeman44728a72005-09-19 22:34:01 +00003909SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
3910 SDOperand Chain = N->getOperand(0);
3911 SDOperand N1 = N->getOperand(1);
3912 SDOperand N2 = N->getOperand(2);
3913 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3914
3915 // never taken branch, fold to chain
3916 if (N1C && N1C->isNullValue())
3917 return Chain;
3918 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00003919 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00003920 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003921 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
3922 // on the target.
3923 if (N1.getOpcode() == ISD::SETCC &&
3924 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
3925 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
3926 N1.getOperand(0), N1.getOperand(1), N2);
3927 }
Nate Begeman44728a72005-09-19 22:34:01 +00003928 return SDOperand();
3929}
3930
Chris Lattner3ea0b472005-10-05 06:47:48 +00003931// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
3932//
Nate Begeman44728a72005-09-19 22:34:01 +00003933SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00003934 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
3935 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
3936
3937 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00003938 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003939 if (Simp.Val) AddToWorkList(Simp.Val);
3940
Nate Begemane17daeb2005-10-05 21:43:42 +00003941 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
3942
3943 // fold br_cc true, dest -> br dest (unconditional branch)
3944 if (SCCC && SCCC->getValue())
3945 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
3946 N->getOperand(4));
3947 // fold br_cc false, dest -> unconditional fall through
3948 if (SCCC && SCCC->isNullValue())
3949 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00003950
Nate Begemane17daeb2005-10-05 21:43:42 +00003951 // fold to a simpler setcc
3952 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
3953 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
3954 Simp.getOperand(2), Simp.getOperand(0),
3955 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00003956 return SDOperand();
3957}
3958
Chris Lattner448f2192006-11-11 00:39:41 +00003959
3960/// CombineToPreIndexedLoadStore - Try turning a load / store and a
3961/// pre-indexed load / store when the base pointer is a add or subtract
3962/// and it has other uses besides the load / store. After the
3963/// transformation, the new indexed load / store has effectively folded
3964/// the add / subtract in and all of its other uses are redirected to the
3965/// new load / store.
3966bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
3967 if (!AfterLegalize)
3968 return false;
3969
3970 bool isLoad = true;
3971 SDOperand Ptr;
3972 MVT::ValueType VT;
3973 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00003974 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00003975 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003976 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00003977 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00003978 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
3979 return false;
3980 Ptr = LD->getBasePtr();
3981 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00003982 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00003983 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003984 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00003985 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
3986 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
3987 return false;
3988 Ptr = ST->getBasePtr();
3989 isLoad = false;
3990 } else
3991 return false;
3992
Chris Lattner9f1794e2006-11-11 00:56:29 +00003993 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
3994 // out. There is no reason to make this a preinc/predec.
3995 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
3996 Ptr.Val->hasOneUse())
3997 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003998
Chris Lattner9f1794e2006-11-11 00:56:29 +00003999 // Ask the target to do addressing mode selection.
4000 SDOperand BasePtr;
4001 SDOperand Offset;
4002 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4003 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4004 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004005 // Don't create a indexed load / store with zero offset.
4006 if (isa<ConstantSDNode>(Offset) &&
4007 cast<ConstantSDNode>(Offset)->getValue() == 0)
4008 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004009
Chris Lattner41e53fd2006-11-11 01:00:15 +00004010 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004011 // 1) The new base ptr is a frame index.
4012 // 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 +00004013 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004014 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004015 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004016 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004017
Chris Lattner41e53fd2006-11-11 01:00:15 +00004018 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4019 // (plus the implicit offset) to a register to preinc anyway.
4020 if (isa<FrameIndexSDNode>(BasePtr))
4021 return false;
4022
4023 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004024 if (!isLoad) {
4025 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Chengc843abe2007-05-24 02:35:39 +00004026 if (Val == BasePtr || BasePtr.Val->isPredecessor(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004027 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004028 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004029
Evan Chengc843abe2007-05-24 02:35:39 +00004030 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004031 bool RealUse = false;
4032 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4033 E = Ptr.Val->use_end(); I != E; ++I) {
4034 SDNode *Use = *I;
4035 if (Use == N)
4036 continue;
4037 if (Use->isPredecessor(N))
4038 return false;
4039
4040 if (!((Use->getOpcode() == ISD::LOAD &&
4041 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004042 (Use->getOpcode() == ISD::STORE &&
4043 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004044 RealUse = true;
4045 }
4046 if (!RealUse)
4047 return false;
4048
4049 SDOperand Result;
4050 if (isLoad)
4051 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
4052 else
4053 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4054 ++PreIndexedNodes;
4055 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004056 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004057 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4058 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004059 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004060 if (isLoad) {
4061 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004062 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004063 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004064 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004065 } else {
4066 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004067 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004068 }
4069
Chris Lattner9f1794e2006-11-11 00:56:29 +00004070 // Finally, since the node is now dead, remove it from the graph.
4071 DAG.DeleteNode(N);
4072
4073 // Replace the uses of Ptr with uses of the updated base value.
4074 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004075 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004076 removeFromWorkList(Ptr.Val);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004077 DAG.DeleteNode(Ptr.Val);
4078
4079 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004080}
4081
4082/// CombineToPostIndexedLoadStore - Try combine a load / store with a
4083/// add / sub of the base pointer node into a post-indexed load / store.
4084/// The transformation folded the add / subtract into the new indexed
4085/// load / store effectively and all of its uses are redirected to the
4086/// new load / store.
4087bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
4088 if (!AfterLegalize)
4089 return false;
4090
4091 bool isLoad = true;
4092 SDOperand Ptr;
4093 MVT::ValueType VT;
4094 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004095 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004096 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004097 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004098 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4099 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4100 return false;
4101 Ptr = LD->getBasePtr();
4102 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004103 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004104 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004105 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004106 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4107 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4108 return false;
4109 Ptr = ST->getBasePtr();
4110 isLoad = false;
4111 } else
4112 return false;
4113
Evan Chengcc470212006-11-16 00:08:20 +00004114 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004115 return false;
4116
4117 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4118 E = Ptr.Val->use_end(); I != E; ++I) {
4119 SDNode *Op = *I;
4120 if (Op == N ||
4121 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4122 continue;
4123
4124 SDOperand BasePtr;
4125 SDOperand Offset;
4126 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4127 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4128 if (Ptr == Offset)
4129 std::swap(BasePtr, Offset);
4130 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004131 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004132 // Don't create a indexed load / store with zero offset.
4133 if (isa<ConstantSDNode>(Offset) &&
4134 cast<ConstantSDNode>(Offset)->getValue() == 0)
4135 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004136
Chris Lattner9f1794e2006-11-11 00:56:29 +00004137 // Try turning it into a post-indexed load / store except when
4138 // 1) All uses are load / store ops that use it as base ptr.
4139 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4140 // nor a successor of N. Otherwise, if Op is folded that would
4141 // create a cycle.
4142
4143 // Check for #1.
4144 bool TryNext = false;
4145 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
4146 EE = BasePtr.Val->use_end(); II != EE; ++II) {
4147 SDNode *Use = *II;
4148 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00004149 continue;
4150
Chris Lattner9f1794e2006-11-11 00:56:29 +00004151 // If all the uses are load / store addresses, then don't do the
4152 // transformation.
4153 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4154 bool RealUse = false;
4155 for (SDNode::use_iterator III = Use->use_begin(),
4156 EEE = Use->use_end(); III != EEE; ++III) {
4157 SDNode *UseUse = *III;
4158 if (!((UseUse->getOpcode() == ISD::LOAD &&
4159 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004160 (UseUse->getOpcode() == ISD::STORE &&
4161 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004162 RealUse = true;
4163 }
Chris Lattner448f2192006-11-11 00:39:41 +00004164
Chris Lattner9f1794e2006-11-11 00:56:29 +00004165 if (!RealUse) {
4166 TryNext = true;
4167 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004168 }
4169 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004170 }
4171 if (TryNext)
4172 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004173
Chris Lattner9f1794e2006-11-11 00:56:29 +00004174 // Check for #2
4175 if (!Op->isPredecessor(N) && !N->isPredecessor(Op)) {
4176 SDOperand Result = isLoad
4177 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
4178 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4179 ++PostIndexedNodes;
4180 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004181 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004182 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4183 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004184 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004185 if (isLoad) {
4186 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004187 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004188 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004189 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004190 } else {
4191 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004192 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004193 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004194
Chris Lattner9f1794e2006-11-11 00:56:29 +00004195 // Finally, since the node is now dead, remove it from the graph.
4196 DAG.DeleteNode(N);
4197
4198 // Replace the uses of Use with uses of the updated base value.
4199 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
4200 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004201 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004202 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004203 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004204 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004205 }
4206 }
4207 }
4208 return false;
4209}
4210
Chris Lattner00161a62008-01-25 07:20:16 +00004211/// InferAlignment - If we can infer some alignment information from this
4212/// pointer, return it.
4213static unsigned InferAlignment(SDOperand Ptr, SelectionDAG &DAG) {
4214 // If this is a direct reference to a stack slot, use information about the
4215 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004216 int FrameIdx = 1 << 31;
4217 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004218 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004219 FrameIdx = FI->getIndex();
4220 } else if (Ptr.getOpcode() == ISD::ADD &&
4221 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4222 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4223 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4224 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004225 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004226
4227 if (FrameIdx != (1 << 31)) {
4228 // FIXME: Handle FI+CST.
4229 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4230 if (MFI.isFixedObjectIndex(FrameIdx)) {
4231 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx);
4232
4233 // The alignment of the frame index can be determined from its offset from
4234 // the incoming frame position. If the frame object is at offset 32 and
4235 // the stack is guaranteed to be 16-byte aligned, then we know that the
4236 // object is 16-byte aligned.
4237 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4238 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4239
4240 // Finally, the frame object itself may have a known alignment. Factor
4241 // the alignment + offset into a new alignment. For example, if we know
4242 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4243 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4244 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4245 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4246 FrameOffset);
4247 return std::max(Align, FIInfoAlign);
4248 }
4249 }
Chris Lattner00161a62008-01-25 07:20:16 +00004250
4251 return 0;
4252}
Chris Lattner448f2192006-11-11 00:39:41 +00004253
Chris Lattner01a22022005-10-10 22:04:48 +00004254SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004255 LoadSDNode *LD = cast<LoadSDNode>(N);
4256 SDOperand Chain = LD->getChain();
4257 SDOperand Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004258
4259 // Try to infer better alignment information than the load already has.
4260 if (LD->isUnindexed()) {
4261 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4262 if (Align > LD->getAlignment())
4263 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4264 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004265 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004266 LD->isVolatile(), Align);
4267 }
4268 }
4269
Evan Cheng45a7ca92007-05-01 00:38:21 +00004270
4271 // If load is not volatile and there are no uses of the loaded value (and
4272 // the updated indexed value in case of indexed loads), change uses of the
4273 // chain value into uses of the chain input (i.e. delete the dead load).
4274 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004275 if (N->getValueType(1) == MVT::Other) {
4276 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004277 if (N->hasNUsesOfValue(0, 0)) {
4278 // It's not safe to use the two value CombineTo variant here. e.g.
4279 // v1, chain2 = load chain1, loc
4280 // v2, chain3 = load chain2, loc
4281 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004282 // Now we replace use of chain2 with chain1. This makes the second load
4283 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004284 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004285 DOUT << "\nWith chain: "; DEBUG(Chain.Val->dump(&DAG));
4286 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004287 WorkListRemover DeadNodes(*this);
4288 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004289 if (N->use_empty()) {
4290 removeFromWorkList(N);
4291 DAG.DeleteNode(N);
4292 }
Evan Cheng02c42852008-01-16 23:11:54 +00004293 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
4294 }
Evan Cheng498f5592007-05-01 08:53:39 +00004295 } else {
4296 // Indexed loads.
4297 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4298 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Evan Cheng02c42852008-01-16 23:11:54 +00004299 SDOperand Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
4300 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
4301 DOUT << "\nWith: "; DEBUG(Undef.Val->dump(&DAG));
4302 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004303 WorkListRemover DeadNodes(*this);
4304 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Undef, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004305 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004306 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004307 &DeadNodes);
4308 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004309 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004310 DAG.DeleteNode(N);
4311 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004312 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004313 }
4314 }
Chris Lattner01a22022005-10-10 22:04:48 +00004315
4316 // If this load is directly stored, replace the load value with the stored
4317 // value.
4318 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004319 // TODO: Handle TRUNCSTORE/LOADEXT
4320 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004321 if (ISD::isNON_TRUNCStore(Chain.Val)) {
4322 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4323 if (PrevST->getBasePtr() == Ptr &&
4324 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004325 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004326 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004327 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004328
Jim Laskey7ca56af2006-10-11 13:47:09 +00004329 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004330 // Walk up chain skipping non-aliasing memory nodes.
4331 SDOperand BetterChain = FindBetterChain(N, Chain);
4332
Jim Laskey6ff23e52006-10-04 16:53:27 +00004333 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004334 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004335 SDOperand ReplLoad;
4336
Jim Laskey279f0532006-09-25 16:29:54 +00004337 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004338 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4339 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004340 LD->getSrcValue(), LD->getSrcValueOffset(),
4341 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004342 } else {
4343 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4344 LD->getValueType(0),
4345 BetterChain, Ptr, LD->getSrcValue(),
4346 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004347 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004348 LD->isVolatile(),
4349 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004350 }
Jim Laskey279f0532006-09-25 16:29:54 +00004351
Jim Laskey6ff23e52006-10-04 16:53:27 +00004352 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00004353 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
4354 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004355
Jim Laskey274062c2006-10-13 23:32:28 +00004356 // Replace uses with load result and token factor. Don't add users
4357 // to work list.
4358 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004359 }
4360 }
4361
Evan Cheng7fc033a2006-11-03 03:06:21 +00004362 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004363 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00004364 return SDOperand(N, 0);
4365
Chris Lattner01a22022005-10-10 22:04:48 +00004366 return SDOperand();
4367}
4368
Chris Lattner07649d92008-01-08 23:08:06 +00004369
Chris Lattner87514ca2005-10-10 22:31:19 +00004370SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004371 StoreSDNode *ST = cast<StoreSDNode>(N);
4372 SDOperand Chain = ST->getChain();
4373 SDOperand Value = ST->getValue();
4374 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004375
Chris Lattner00161a62008-01-25 07:20:16 +00004376 // Try to infer better alignment information than the store already has.
4377 if (ST->isUnindexed()) {
4378 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4379 if (Align > ST->getAlignment())
4380 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004381 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004382 ST->isVolatile(), Align);
4383 }
4384 }
4385
Evan Cheng59d5b682007-05-07 21:27:48 +00004386 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004387 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004388 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004389 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004390 unsigned Align = ST->getAlignment();
4391 MVT::ValueType SVT = Value.getOperand(0).getValueType();
4392 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00004393 getABITypeAlignment(MVT::getTypeForValueType(SVT));
Evan Chengc2cd2b22007-05-07 21:36:06 +00004394 if (Align <= OrigAlign && TLI.isOperationLegal(ISD::STORE, SVT))
Evan Cheng59d5b682007-05-07 21:27:48 +00004395 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004396 ST->getSrcValueOffset(), ST->isVolatile(), Align);
Jim Laskey279f0532006-09-25 16:29:54 +00004397 }
4398
Nate Begeman2cbba892006-12-11 02:23:46 +00004399 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004400 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Evan Cheng25ece662006-12-11 17:25:19 +00004401 if (Value.getOpcode() != ISD::TargetConstantFP) {
4402 SDOperand Tmp;
Chris Lattner62be1a72006-12-12 04:16:14 +00004403 switch (CFP->getValueType(0)) {
4404 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004405 case MVT::f80: // We don't do this for these yet.
4406 case MVT::f128:
4407 case MVT::ppcf128:
4408 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004409 case MVT::f32:
4410 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004411 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
4412 convertToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004413 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004414 ST->getSrcValueOffset(), ST->isVolatile(),
4415 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004416 }
4417 break;
4418 case MVT::f64:
4419 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004420 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
4421 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004422 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004423 ST->getSrcValueOffset(), ST->isVolatile(),
4424 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004425 } else if (TLI.isTypeLegal(MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004426 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004427 // argument passing. Since this is so common, custom legalize the
4428 // 64-bit integer store into two 32-bit stores.
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004429 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
Chris Lattner62be1a72006-12-12 04:16:14 +00004430 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4431 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004432 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004433
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004434 int SVOffset = ST->getSrcValueOffset();
4435 unsigned Alignment = ST->getAlignment();
4436 bool isVolatile = ST->isVolatile();
4437
Chris Lattner62be1a72006-12-12 04:16:14 +00004438 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004439 ST->getSrcValueOffset(),
4440 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004441 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4442 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004443 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004444 Alignment = MinAlign(Alignment, 4U);
Chris Lattner62be1a72006-12-12 04:16:14 +00004445 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004446 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004447 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4448 }
4449 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004450 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004451 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004452 }
4453
Jim Laskey279f0532006-09-25 16:29:54 +00004454 if (CombinerAA) {
4455 // Walk up chain skipping non-aliasing memory nodes.
4456 SDOperand BetterChain = FindBetterChain(N, Chain);
4457
Jim Laskey6ff23e52006-10-04 16:53:27 +00004458 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004459 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004460 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004461 SDOperand ReplStore;
4462 if (ST->isTruncatingStore()) {
4463 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004464 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004465 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004466 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004467 } else {
4468 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004469 ST->getSrcValue(), ST->getSrcValueOffset(),
4470 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004471 }
4472
Jim Laskey279f0532006-09-25 16:29:54 +00004473 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00004474 SDOperand Token =
4475 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4476
4477 // Don't add users to work list.
4478 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004479 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004480 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004481
Evan Cheng33dbedc2006-11-05 09:31:14 +00004482 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004483 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00004484 return SDOperand(N, 0);
4485
Chris Lattner3c872852007-12-29 06:26:16 +00004486 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004487 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Chris Lattner2b4c2792007-10-13 06:35:54 +00004488 MVT::isInteger(Value.getValueType())) {
4489 // See if we can simplify the input to this truncstore with knowledge that
4490 // only the low bits are being used. For example:
4491 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
4492 SDOperand Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004493 GetDemandedBits(Value,
4494 APInt::getLowBitsSet(Value.getValueSizeInBits(),
4495 MVT::getSizeInBits(ST->getMemoryVT())));
Chris Lattner2b4c2792007-10-13 06:35:54 +00004496 AddToWorkList(Value.Val);
4497 if (Shorter.Val)
4498 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004499 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004500 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004501
4502 // Otherwise, see if we can simplify the operation with
4503 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004504 if (SimplifyDemandedBits(Value, MVT::getIntVTBitMask(ST->getMemoryVT())))
Chris Lattnere33544c2007-10-13 06:58:48 +00004505 return SDOperand(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004506 }
4507
Chris Lattner3c872852007-12-29 06:26:16 +00004508 // If this is a load followed by a store to the same location, then the store
4509 // is dead/noop.
4510 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004511 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004512 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004513 // There can't be any side effects between the load and store, such as
4514 // a call or store.
Chris Lattner572dee72008-01-16 05:49:24 +00004515 Chain.reachesChainWithoutSideEffects(SDOperand(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004516 // The store is dead, remove it.
4517 return Chain;
4518 }
4519 }
4520
Chris Lattnerddf89562008-01-17 19:59:44 +00004521 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4522 // truncating store. We can do this even if this is already a truncstore.
4523 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
4524 && TLI.isTypeLegal(Value.getOperand(0).getValueType()) &&
4525 Value.Val->hasOneUse() && ST->isUnindexed() &&
4526 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004527 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004528 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004529 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004530 ST->isVolatile(), ST->getAlignment());
4531 }
4532
Chris Lattner87514ca2005-10-10 22:31:19 +00004533 return SDOperand();
4534}
4535
Chris Lattnerca242442006-03-19 01:27:56 +00004536SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4537 SDOperand InVec = N->getOperand(0);
4538 SDOperand InVal = N->getOperand(1);
4539 SDOperand EltNo = N->getOperand(2);
4540
4541 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4542 // vector with the inserted element.
4543 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
4544 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004545 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004546 if (Elt < Ops.size())
4547 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004548 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4549 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004550 }
4551
4552 return SDOperand();
4553}
4554
Evan Cheng513da432007-10-06 08:19:55 +00004555SDOperand DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
4556 SDOperand InVec = N->getOperand(0);
4557 SDOperand EltNo = N->getOperand(1);
4558
4559 // (vextract (v4f32 s2v (f32 load $addr)), 0) -> (f32 load $addr)
4560 // (vextract (v4i32 bc (v4f32 s2v (f32 load $addr))), 0) -> (i32 load $addr)
4561 if (isa<ConstantSDNode>(EltNo)) {
4562 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
4563 bool NewLoad = false;
4564 if (Elt == 0) {
4565 MVT::ValueType VT = InVec.getValueType();
4566 MVT::ValueType EVT = MVT::getVectorElementType(VT);
4567 MVT::ValueType LVT = EVT;
4568 unsigned NumElts = MVT::getVectorNumElements(VT);
4569 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
4570 MVT::ValueType BCVT = InVec.getOperand(0).getValueType();
Dan Gohman090b38a2007-10-29 20:44:42 +00004571 if (!MVT::isVector(BCVT) ||
4572 NumElts != MVT::getVectorNumElements(BCVT))
Evan Cheng513da432007-10-06 08:19:55 +00004573 return SDOperand();
4574 InVec = InVec.getOperand(0);
4575 EVT = MVT::getVectorElementType(BCVT);
4576 NewLoad = true;
4577 }
4578 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4579 InVec.getOperand(0).getValueType() == EVT &&
4580 ISD::isNormalLoad(InVec.getOperand(0).Val) &&
4581 InVec.getOperand(0).hasOneUse()) {
4582 LoadSDNode *LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4583 unsigned Align = LN0->getAlignment();
4584 if (NewLoad) {
4585 // Check the resultant load doesn't need a higher alignment than the
4586 // original load.
4587 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
4588 getABITypeAlignment(MVT::getTypeForValueType(LVT));
4589 if (!TLI.isOperationLegal(ISD::LOAD, LVT) || NewAlign > Align)
4590 return SDOperand();
4591 Align = NewAlign;
4592 }
4593
4594 return DAG.getLoad(LVT, LN0->getChain(), LN0->getBasePtr(),
4595 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4596 LN0->isVolatile(), Align);
4597 }
4598 }
4599 }
4600 return SDOperand();
4601}
4602
4603
Dan Gohman7f321562007-06-25 16:23:39 +00004604SDOperand DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
4605 unsigned NumInScalars = N->getNumOperands();
4606 MVT::ValueType VT = N->getValueType(0);
4607 unsigned NumElts = MVT::getVectorNumElements(VT);
4608 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattnerca242442006-03-19 01:27:56 +00004609
Dan Gohman7f321562007-06-25 16:23:39 +00004610 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4611 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4612 // at most two distinct vectors, turn this into a shuffle node.
Chris Lattnerd7648c82006-03-28 20:28:38 +00004613 SDOperand VecIn1, VecIn2;
4614 for (unsigned i = 0; i != NumInScalars; ++i) {
4615 // Ignore undef inputs.
4616 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4617
Dan Gohman7f321562007-06-25 16:23:39 +00004618 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004619 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004620 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004621 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
4622 VecIn1 = VecIn2 = SDOperand(0, 0);
4623 break;
4624 }
4625
Dan Gohman7f321562007-06-25 16:23:39 +00004626 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004627 // we can't make a shuffle.
4628 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004629 if (ExtractedFromVec.getValueType() != VT) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004630 VecIn1 = VecIn2 = SDOperand(0, 0);
4631 break;
4632 }
4633
4634 // Otherwise, remember this. We allow up to two distinct input vectors.
4635 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4636 continue;
4637
4638 if (VecIn1.Val == 0) {
4639 VecIn1 = ExtractedFromVec;
4640 } else if (VecIn2.Val == 0) {
4641 VecIn2 = ExtractedFromVec;
4642 } else {
4643 // Too many inputs.
4644 VecIn1 = VecIn2 = SDOperand(0, 0);
4645 break;
4646 }
4647 }
4648
4649 // If everything is good, we can make a shuffle operation.
4650 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004651 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004652 for (unsigned i = 0; i != NumInScalars; ++i) {
4653 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004654 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004655 continue;
4656 }
4657
4658 SDOperand Extract = N->getOperand(i);
4659
4660 // If extracting from the first vector, just use the index directly.
4661 if (Extract.getOperand(0) == VecIn1) {
4662 BuildVecIndices.push_back(Extract.getOperand(1));
4663 continue;
4664 }
4665
4666 // Otherwise, use InIdx + VecSize
4667 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004668 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004669 }
4670
4671 // Add count and size info.
Chris Lattner0bd48932008-01-17 07:00:52 +00004672 MVT::ValueType BuildVecVT = MVT::getVectorType(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004673
Dan Gohman7f321562007-06-25 16:23:39 +00004674 // Return the new VECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004675 SDOperand Ops[5];
4676 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00004677 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004678 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004679 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004680 // Use an undef build_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00004681 std::vector<SDOperand> UnOps(NumInScalars,
4682 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004683 EltType));
4684 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004685 &UnOps[0], UnOps.size());
4686 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00004687 }
Dan Gohman7f321562007-06-25 16:23:39 +00004688 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004689 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004690 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004691 }
4692
4693 return SDOperand();
4694}
4695
Dan Gohman7f321562007-06-25 16:23:39 +00004696SDOperand DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
4697 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
4698 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
4699 // inputs come from at most two distinct vectors, turn this into a shuffle
4700 // node.
4701
4702 // If we only have one input vector, we don't need to do any concatenation.
4703 if (N->getNumOperands() == 1) {
4704 return N->getOperand(0);
4705 }
4706
4707 return SDOperand();
4708}
4709
Chris Lattner66445d32006-03-28 22:11:53 +00004710SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004711 SDOperand ShufMask = N->getOperand(2);
4712 unsigned NumElts = ShufMask.getNumOperands();
4713
4714 // If the shuffle mask is an identity operation on the LHS, return the LHS.
4715 bool isIdentity = true;
4716 for (unsigned i = 0; i != NumElts; ++i) {
4717 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4718 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
4719 isIdentity = false;
4720 break;
4721 }
4722 }
4723 if (isIdentity) return N->getOperand(0);
4724
4725 // If the shuffle mask is an identity operation on the RHS, return the RHS.
4726 isIdentity = true;
4727 for (unsigned i = 0; i != NumElts; ++i) {
4728 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4729 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
4730 isIdentity = false;
4731 break;
4732 }
4733 }
4734 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004735
4736 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
4737 // needed at all.
4738 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00004739 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004740 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00004741 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004742 for (unsigned i = 0; i != NumElts; ++i)
4743 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
4744 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
4745 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00004746 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004747 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004748 BaseIdx = Idx;
4749 } else {
4750 if (BaseIdx != Idx)
4751 isSplat = false;
4752 if (VecNum != V) {
4753 isUnary = false;
4754 break;
4755 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004756 }
4757 }
4758
4759 SDOperand N0 = N->getOperand(0);
4760 SDOperand N1 = N->getOperand(1);
4761 // Normalize unary shuffle so the RHS is undef.
4762 if (isUnary && VecNum == 1)
4763 std::swap(N0, N1);
4764
Evan Cheng917ec982006-07-21 08:25:53 +00004765 // If it is a splat, check if the argument vector is a build_vector with
4766 // all scalar elements the same.
4767 if (isSplat) {
4768 SDNode *V = N0.Val;
Evan Cheng917ec982006-07-21 08:25:53 +00004769
Dan Gohman7f321562007-06-25 16:23:39 +00004770 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004771 // not the number of vector elements, look through it. Be careful not to
4772 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004773 if (V->getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng59569222006-10-16 22:49:37 +00004774 SDOperand ConvInput = V->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004775 if (MVT::getVectorNumElements(ConvInput.getValueType()) == NumElts)
Evan Cheng59569222006-10-16 22:49:37 +00004776 V = ConvInput.Val;
4777 }
4778
Dan Gohman7f321562007-06-25 16:23:39 +00004779 if (V->getOpcode() == ISD::BUILD_VECTOR) {
4780 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00004781 if (NumElems > BaseIdx) {
4782 SDOperand Base;
4783 bool AllSame = true;
4784 for (unsigned i = 0; i != NumElems; ++i) {
4785 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4786 Base = V->getOperand(i);
4787 break;
4788 }
4789 }
4790 // Splat of <u, u, u, u>, return <u, u, u, u>
4791 if (!Base.Val)
4792 return N0;
4793 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00004794 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00004795 AllSame = false;
4796 break;
4797 }
4798 }
4799 // Splat of <x, x, x, x>, return <x, x, x, x>
4800 if (AllSame)
4801 return N0;
4802 }
4803 }
4804 }
4805
Evan Chenge7bec0d2006-07-20 22:44:41 +00004806 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4807 // into an undef.
4808 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004809 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4810 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004811 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004812 for (unsigned i = 0; i != NumElts; ++i) {
4813 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4814 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4815 MappedOps.push_back(ShufMask.getOperand(i));
4816 } else {
4817 unsigned NewIdx =
4818 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
4819 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
4820 }
4821 }
Dan Gohman7f321562007-06-25 16:23:39 +00004822 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004823 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004824 AddToWorkList(ShufMask.Val);
Dan Gohman7f321562007-06-25 16:23:39 +00004825 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
4826 N0,
4827 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
4828 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00004829 }
Dan Gohman7f321562007-06-25 16:23:39 +00004830
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004831 return SDOperand();
4832}
4833
Evan Cheng44f1f092006-04-20 08:56:16 +00004834/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00004835/// an AND to a vector_shuffle with the destination vector and a zero vector.
4836/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00004837/// vector_shuffle V, Zero, <0, 4, 2, 4>
4838SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
4839 SDOperand LHS = N->getOperand(0);
4840 SDOperand RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00004841 if (N->getOpcode() == ISD::AND) {
4842 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00004843 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004844 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Evan Cheng44f1f092006-04-20 08:56:16 +00004845 std::vector<SDOperand> IdxOps;
4846 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00004847 unsigned NumElts = NumOps;
4848 MVT::ValueType EVT = MVT::getVectorElementType(RHS.getValueType());
Evan Cheng44f1f092006-04-20 08:56:16 +00004849 for (unsigned i = 0; i != NumElts; ++i) {
4850 SDOperand Elt = RHS.getOperand(i);
4851 if (!isa<ConstantSDNode>(Elt))
4852 return SDOperand();
4853 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
4854 IdxOps.push_back(DAG.getConstant(i, EVT));
4855 else if (cast<ConstantSDNode>(Elt)->isNullValue())
4856 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
4857 else
4858 return SDOperand();
4859 }
4860
4861 // Let's see if the target supports this vector_shuffle.
4862 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
4863 return SDOperand();
4864
Dan Gohman7f321562007-06-25 16:23:39 +00004865 // Return the new VECTOR_SHUFFLE node.
4866 MVT::ValueType VT = MVT::getVectorType(EVT, NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00004867 std::vector<SDOperand> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004868 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00004869 Ops.push_back(LHS);
4870 AddToWorkList(LHS.Val);
4871 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00004872 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004873 &ZeroOps[0], ZeroOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004874 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004875 &IdxOps[0], IdxOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004876 SDOperand Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004877 &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004878 if (VT != LHS.getValueType()) {
4879 Result = DAG.getNode(ISD::BIT_CONVERT, LHS.getValueType(), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00004880 }
4881 return Result;
4882 }
4883 }
4884 return SDOperand();
4885}
4886
Dan Gohman7f321562007-06-25 16:23:39 +00004887/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
4888SDOperand DAGCombiner::SimplifyVBinOp(SDNode *N) {
4889 // After legalize, the target may be depending on adds and other
4890 // binary ops to provide legal ways to construct constants or other
4891 // things. Simplifying them may result in a loss of legality.
4892 if (AfterLegalize) return SDOperand();
4893
4894 MVT::ValueType VT = N->getValueType(0);
Dan Gohman05d92fe2007-07-13 20:03:40 +00004895 assert(MVT::isVector(VT) && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00004896
4897 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattneredab1b92006-04-02 03:25:57 +00004898 SDOperand LHS = N->getOperand(0);
4899 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00004900 SDOperand Shuffle = XformToShuffleWithZero(N);
4901 if (Shuffle.Val) return Shuffle;
4902
Dan Gohman7f321562007-06-25 16:23:39 +00004903 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00004904 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00004905 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
4906 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004907 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004908 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Chris Lattneredab1b92006-04-02 03:25:57 +00004909 SDOperand LHSOp = LHS.getOperand(i);
4910 SDOperand RHSOp = RHS.getOperand(i);
4911 // If these two elements can't be folded, bail out.
4912 if ((LHSOp.getOpcode() != ISD::UNDEF &&
4913 LHSOp.getOpcode() != ISD::Constant &&
4914 LHSOp.getOpcode() != ISD::ConstantFP) ||
4915 (RHSOp.getOpcode() != ISD::UNDEF &&
4916 RHSOp.getOpcode() != ISD::Constant &&
4917 RHSOp.getOpcode() != ISD::ConstantFP))
4918 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00004919 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00004920 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
4921 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00004922 if ((RHSOp.getOpcode() == ISD::Constant &&
4923 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
4924 (RHSOp.getOpcode() == ISD::ConstantFP &&
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00004925 cast<ConstantFPSDNode>(RHSOp.Val)->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00004926 break;
4927 }
Dan Gohman7f321562007-06-25 16:23:39 +00004928 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00004929 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00004930 assert((Ops.back().getOpcode() == ISD::UNDEF ||
4931 Ops.back().getOpcode() == ISD::Constant ||
4932 Ops.back().getOpcode() == ISD::ConstantFP) &&
4933 "Scalar binop didn't fold!");
4934 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004935
Dan Gohman7f321562007-06-25 16:23:39 +00004936 if (Ops.size() == LHS.getNumOperands()) {
4937 MVT::ValueType VT = LHS.getValueType();
4938 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004939 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004940 }
4941
4942 return SDOperand();
4943}
4944
Nate Begeman44728a72005-09-19 22:34:01 +00004945SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00004946 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
4947
4948 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
4949 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4950 // If we got a simplified select_cc node back from SimplifySelectCC, then
4951 // break it down into a new SETCC node, and a new SELECT node, and then return
4952 // the SELECT node, since we were called with a SELECT node.
4953 if (SCC.Val) {
4954 // Check to see if we got a select_cc back (to turn into setcc/select).
4955 // Otherwise, just return whatever node we got back, like fabs.
4956 if (SCC.getOpcode() == ISD::SELECT_CC) {
4957 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
4958 SCC.getOperand(0), SCC.getOperand(1),
4959 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00004960 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004961 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
4962 SCC.getOperand(3), SETCC);
4963 }
4964 return SCC;
4965 }
Nate Begeman44728a72005-09-19 22:34:01 +00004966 return SDOperand();
4967}
4968
Chris Lattner40c62d52005-10-18 06:04:22 +00004969/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
4970/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00004971/// select. Callers of this should assume that TheSelect is deleted if this
4972/// returns true. As such, they should return the appropriate thing (e.g. the
4973/// node) back to the top-level of the DAG combiner loop to avoid it being
4974/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00004975///
4976bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
4977 SDOperand RHS) {
4978
4979 // If this is a select from two identical things, try to pull the operation
4980 // through the select.
4981 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00004982 // If this is a load and the token chain is identical, replace the select
4983 // of two loads with a load through a select of the address to load from.
4984 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
4985 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00004986 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00004987 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00004988 LHS.getOperand(0) == RHS.getOperand(0)) {
4989 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
4990 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
4991
4992 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004993 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004994 // FIXME: this conflates two src values, discarding one. This is not
4995 // the right thing to do, but nothing uses srcvalues now. When they do,
4996 // turn SrcValue into a list of locations.
4997 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004998 if (TheSelect->getOpcode() == ISD::SELECT) {
4999 // Check that the condition doesn't reach either load. If so, folding
5000 // this will induce a cycle into the DAG.
5001 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
5002 !RLD->isPredecessor(TheSelect->getOperand(0).Val)) {
5003 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5004 TheSelect->getOperand(0), LLD->getBasePtr(),
5005 RLD->getBasePtr());
5006 }
5007 } else {
5008 // Check that the condition doesn't reach either load. If so, folding
5009 // this will induce a cycle into the DAG.
5010 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
5011 !RLD->isPredecessor(TheSelect->getOperand(0).Val) &&
5012 !LLD->isPredecessor(TheSelect->getOperand(1).Val) &&
5013 !RLD->isPredecessor(TheSelect->getOperand(1).Val)) {
5014 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005015 TheSelect->getOperand(0),
5016 TheSelect->getOperand(1),
5017 LLD->getBasePtr(), RLD->getBasePtr(),
5018 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005019 }
Evan Cheng466685d2006-10-09 20:57:25 +00005020 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005021
5022 if (Addr.Val) {
5023 SDOperand Load;
5024 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5025 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5026 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005027 LLD->getSrcValueOffset(),
5028 LLD->isVolatile(),
5029 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005030 else {
5031 Load = DAG.getExtLoad(LLD->getExtensionType(),
5032 TheSelect->getValueType(0),
5033 LLD->getChain(), Addr, LLD->getSrcValue(),
5034 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005035 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005036 LLD->isVolatile(),
5037 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005038 }
5039 // Users of the select now use the result of the load.
5040 CombineTo(TheSelect, Load);
5041
5042 // Users of the old loads now use the new load's chain. We know the
5043 // old-load value is dead now.
5044 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
5045 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
5046 return true;
5047 }
Evan Chengc5484282006-10-04 00:56:09 +00005048 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005049 }
5050 }
5051
5052 return false;
5053}
5054
Nate Begeman44728a72005-09-19 22:34:01 +00005055SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
5056 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00005057 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005058
5059 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00005060 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
5061 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
5062 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
5063
5064 // Determine if the condition we're dealing with is constant
5065 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00005066 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005067 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
5068
5069 // fold select_cc true, x, y -> x
5070 if (SCCC && SCCC->getValue())
5071 return N2;
5072 // fold select_cc false, x, y -> y
5073 if (SCCC && SCCC->getValue() == 0)
5074 return N3;
5075
5076 // Check to see if we can simplify the select into an fabs node
5077 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5078 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005079 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005080 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5081 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5082 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5083 N2 == N3.getOperand(0))
5084 return DAG.getNode(ISD::FABS, VT, N0);
5085
5086 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5087 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5088 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5089 N2.getOperand(0) == N3)
5090 return DAG.getNode(ISD::FABS, VT, N3);
5091 }
5092 }
5093
5094 // Check to see if we can perform the "gzip trick", transforming
5095 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005096 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00005097 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00005098 MVT::isInteger(N2.getValueType()) &&
5099 (N1C->isNullValue() || // (a < 0) ? b : 0
5100 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00005101 MVT::ValueType XType = N0.getValueType();
5102 MVT::ValueType AType = N2.getValueType();
5103 if (XType >= AType) {
5104 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005105 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00005106 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
5107 unsigned ShCtV = Log2_64(N2C->getValue());
5108 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
5109 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5110 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00005111 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005112 if (XType > AType) {
5113 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005114 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005115 }
5116 return DAG.getNode(ISD::AND, AType, Shift, N2);
5117 }
5118 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5119 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5120 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00005121 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005122 if (XType > AType) {
5123 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005124 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005125 }
5126 return DAG.getNode(ISD::AND, AType, Shift, N2);
5127 }
5128 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005129
5130 // fold select C, 16, 0 -> shl C, 4
5131 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
5132 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005133
5134 // If the caller doesn't want us to simplify this into a zext of a compare,
5135 // don't do it.
5136 if (NotExtCompare && N2C->getValue() == 1)
5137 return SDOperand();
5138
Nate Begeman07ed4172005-10-10 21:26:48 +00005139 // Get a SetCC of the condition
5140 // FIXME: Should probably make sure that setcc is legal if we ever have a
5141 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00005142 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005143 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00005144 if (AfterLegalize) {
5145 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Chris Lattner555d8d62006-12-07 22:36:47 +00005146 if (N2.getValueType() < SCC.getValueType())
5147 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5148 else
5149 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005150 } else {
5151 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005152 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005153 }
Chris Lattner5750df92006-03-01 04:03:14 +00005154 AddToWorkList(SCC.Val);
5155 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005156
5157 if (N2C->getValue() == 1)
5158 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005159 // shl setcc result by log2 n2c
5160 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
5161 DAG.getConstant(Log2_64(N2C->getValue()),
5162 TLI.getShiftAmountTy()));
5163 }
5164
Nate Begemanf845b452005-10-08 00:29:44 +00005165 // Check to see if this is the equivalent of setcc
5166 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5167 // otherwise, go ahead with the folds.
5168 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
5169 MVT::ValueType XType = N0.getValueType();
5170 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
5171 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
5172 if (Res.getValueType() != VT)
5173 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5174 return Res;
5175 }
5176
5177 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5178 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
5179 TLI.isOperationLegal(ISD::CTLZ, XType)) {
5180 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
5181 return DAG.getNode(ISD::SRL, XType, Ctlz,
5182 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
5183 TLI.getShiftAmountTy()));
5184 }
5185 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5186 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
5187 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
5188 N0);
5189 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
5190 DAG.getConstant(~0ULL, XType));
5191 return DAG.getNode(ISD::SRL, XType,
5192 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
5193 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5194 TLI.getShiftAmountTy()));
5195 }
5196 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5197 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
5198 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
5199 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5200 TLI.getShiftAmountTy()));
5201 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5202 }
5203 }
5204
5205 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5206 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5207 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005208 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
5209 N2.getOperand(0) == N1 && MVT::isInteger(N0.getValueType())) {
5210 MVT::ValueType XType = N0.getValueType();
5211 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5212 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5213 TLI.getShiftAmountTy()));
5214 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
5215 AddToWorkList(Shift.Val);
5216 AddToWorkList(Add.Val);
5217 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5218 }
5219 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5220 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5221 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5222 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5223 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Nate Begemanf845b452005-10-08 00:29:44 +00005224 MVT::ValueType XType = N0.getValueType();
5225 if (SubC->isNullValue() && MVT::isInteger(XType)) {
5226 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5227 DAG.getConstant(MVT::getSizeInBits(XType)-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005228 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00005229 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005230 AddToWorkList(Shift.Val);
5231 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005232 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5233 }
5234 }
5235 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005236
Nate Begeman44728a72005-09-19 22:34:01 +00005237 return SDOperand();
5238}
5239
Evan Chengfa1eb272007-02-08 22:13:59 +00005240/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Nate Begeman452d7be2005-09-16 00:54:12 +00005241SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00005242 SDOperand N1, ISD::CondCode Cond,
5243 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005244 TargetLowering::DAGCombinerInfo
5245 DagCombineInfo(DAG, !AfterLegalize, false, this);
5246 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005247}
5248
Nate Begeman69575232005-10-20 02:15:44 +00005249/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5250/// return a DAG expression to select that will generate the same value by
5251/// multiplying by a magic number. See:
5252/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5253SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005254 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005255 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
5256
Andrew Lenharth232c9102006-06-12 16:07:18 +00005257 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005258 ii != ee; ++ii)
5259 AddToWorkList(*ii);
5260 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005261}
5262
5263/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5264/// return a DAG expression to select that will generate the same value by
5265/// multiplying by a magic number. See:
5266/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5267SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005268 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005269 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005270
Andrew Lenharth232c9102006-06-12 16:07:18 +00005271 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005272 ii != ee; ++ii)
5273 AddToWorkList(*ii);
5274 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005275}
5276
Jim Laskey71382342006-10-07 23:37:56 +00005277/// FindBaseOffset - Return true if base is known not to alias with anything
5278/// but itself. Provides base object and offset as results.
5279static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
5280 // Assume it is a primitive operation.
5281 Base = Ptr; Offset = 0;
5282
5283 // If it's an adding a simple constant then integrate the offset.
5284 if (Base.getOpcode() == ISD::ADD) {
5285 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5286 Base = Base.getOperand(0);
5287 Offset += C->getValue();
5288 }
5289 }
5290
5291 // If it's any of the following then it can't alias with anything but itself.
5292 return isa<FrameIndexSDNode>(Base) ||
5293 isa<ConstantPoolSDNode>(Base) ||
5294 isa<GlobalAddressSDNode>(Base);
5295}
5296
5297/// isAlias - Return true if there is any possibility that the two addresses
5298/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00005299bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
5300 const Value *SrcValue1, int SrcValueOffset1,
5301 SDOperand Ptr2, int64_t Size2,
5302 const Value *SrcValue2, int SrcValueOffset2)
5303{
Jim Laskey71382342006-10-07 23:37:56 +00005304 // If they are the same then they must be aliases.
5305 if (Ptr1 == Ptr2) return true;
5306
5307 // Gather base node and offset information.
5308 SDOperand Base1, Base2;
5309 int64_t Offset1, Offset2;
5310 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5311 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5312
5313 // If they have a same base address then...
5314 if (Base1 == Base2) {
5315 // Check to see if the addresses overlap.
5316 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5317 }
5318
Jim Laskey096c22e2006-10-18 12:29:57 +00005319 // If we know both bases then they can't alias.
5320 if (KnownBase1 && KnownBase2) return false;
5321
Jim Laskey07a27092006-10-18 19:08:31 +00005322 if (CombinerGlobalAA) {
5323 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005324 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5325 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5326 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005327 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005328 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005329 if (AAResult == AliasAnalysis::NoAlias)
5330 return false;
5331 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005332
5333 // Otherwise we have to assume they alias.
5334 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005335}
5336
5337/// FindAliasInfo - Extracts the relevant alias information from the memory
5338/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005339bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00005340 SDOperand &Ptr, int64_t &Size,
5341 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005342 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5343 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005344 Size = MVT::getSizeInBits(LD->getMemoryVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005345 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005346 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005347 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005348 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005349 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005350 Size = MVT::getSizeInBits(ST->getMemoryVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005351 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005352 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005353 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005354 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005355 }
5356
5357 return false;
5358}
5359
Jim Laskey6ff23e52006-10-04 16:53:27 +00005360/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5361/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00005362void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005363 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005364 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005365 std::set<SDNode *> Visited; // Visited node set.
5366
Jim Laskey279f0532006-09-25 16:29:54 +00005367 // Get alias information for node.
5368 SDOperand Ptr;
5369 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005370 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005371 int SrcValueOffset;
5372 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005373
Jim Laskey6ff23e52006-10-04 16:53:27 +00005374 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005375 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005376
Jim Laskeybc588b82006-10-05 15:07:25 +00005377 // Look at each chain and determine if it is an alias. If so, add it to the
5378 // aliases list. If not, then continue up the chain looking for the next
5379 // candidate.
5380 while (!Chains.empty()) {
5381 SDOperand Chain = Chains.back();
5382 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005383
Jim Laskeybc588b82006-10-05 15:07:25 +00005384 // Don't bother if we've been before.
5385 if (Visited.find(Chain.Val) != Visited.end()) continue;
5386 Visited.insert(Chain.Val);
5387
5388 switch (Chain.getOpcode()) {
5389 case ISD::EntryToken:
5390 // Entry token is ideal chain operand, but handled in FindBetterChain.
5391 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005392
Jim Laskeybc588b82006-10-05 15:07:25 +00005393 case ISD::LOAD:
5394 case ISD::STORE: {
5395 // Get alias information for Chain.
5396 SDOperand OpPtr;
5397 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005398 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005399 int OpSrcValueOffset;
5400 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
5401 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005402
5403 // If chain is alias then stop here.
5404 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005405 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5406 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005407 Aliases.push_back(Chain);
5408 } else {
5409 // Look further up the chain.
5410 Chains.push_back(Chain.getOperand(0));
5411 // Clean up old chain.
5412 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00005413 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005414 break;
5415 }
5416
5417 case ISD::TokenFactor:
5418 // We have to check each of the operands of the token factor, so we queue
5419 // then up. Adding the operands to the queue (stack) in reverse order
5420 // maintains the original order and increases the likelihood that getNode
5421 // will find a matching token factor (CSE.)
5422 for (unsigned n = Chain.getNumOperands(); n;)
5423 Chains.push_back(Chain.getOperand(--n));
5424 // Eliminate the token factor if we can.
5425 AddToWorkList(Chain.Val);
5426 break;
5427
5428 default:
5429 // For all other instructions we will just have to take what we can get.
5430 Aliases.push_back(Chain);
5431 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005432 }
5433 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005434}
5435
5436/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5437/// for a better chain (aliasing node.)
5438SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
5439 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005440
Jim Laskey6ff23e52006-10-04 16:53:27 +00005441 // Accumulate all the aliases to this node.
5442 GatherAllAliases(N, OldChain, Aliases);
5443
5444 if (Aliases.size() == 0) {
5445 // If no operands then chain to entry token.
5446 return DAG.getEntryNode();
5447 } else if (Aliases.size() == 1) {
5448 // If a single operand then chain to it. We don't need to revisit it.
5449 return Aliases[0];
5450 }
5451
5452 // Construct a custom tailored token factor.
5453 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5454 &Aliases[0], Aliases.size());
5455
5456 // Make sure the old chain gets cleaned up.
5457 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
5458
5459 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005460}
5461
Nate Begeman1d4d4142005-09-01 00:19:25 +00005462// SelectionDAG::Combine - This is the entry point for the file.
5463//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005464void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00005465 if (!RunningAfterLegalize && ViewDAGCombine1)
5466 viewGraph();
5467 if (RunningAfterLegalize && ViewDAGCombine2)
5468 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005469 /// run - This is the main entry point to this class.
5470 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005471 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005472}