blob: e6c3314b2459f7bd3b01489e47c3a74f9540e6ac [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>
Dan Gohmanee335e32008-05-23 20:40:06 +000032#include <set>
Nate Begeman1d4d4142005-09-01 00:19:25 +000033using namespace llvm;
34
Chris Lattnercd3245a2006-12-19 22:41:21 +000035STATISTIC(NodesCombined , "Number of dag nodes combined");
36STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
37STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
38
Nate Begeman1d4d4142005-09-01 00:19:25 +000039namespace {
Jim Laskey71382342006-10-07 23:37:56 +000040 static cl::opt<bool>
41 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000042 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000043
Jim Laskey07a27092006-10-18 19:08:31 +000044 static cl::opt<bool>
45 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
46 cl::desc("Include global information in alias analysis"));
47
Jim Laskeybc588b82006-10-05 15:07:25 +000048//------------------------------ DAGCombiner ---------------------------------//
49
Jim Laskey71382342006-10-07 23:37:56 +000050 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000051 SelectionDAG &DAG;
52 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000053 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000054
55 // Worklist of all of the nodes that need to be simplified.
56 std::vector<SDNode*> WorkList;
57
Jim Laskeyc7c3f112006-10-16 20:52:31 +000058 // AA - Used for DAG load/store alias analysis.
59 AliasAnalysis &AA;
60
Nate Begeman1d4d4142005-09-01 00:19:25 +000061 /// AddUsersToWorkList - When an instruction is simplified, add all users of
62 /// the instruction to the work lists because they might get more simplified
63 /// now.
64 ///
65 void AddUsersToWorkList(SDNode *N) {
66 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000067 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +000068 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000069 }
70
Dan Gohman389079b2007-10-08 17:57:15 +000071 /// visit - call the node-specific routine that knows how to fold each
72 /// particular type of node.
73 SDOperand visit(SDNode *N);
74
Chris Lattner24664722006-03-01 04:53:38 +000075 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000076 /// AddToWorkList - Add to the work list making sure it's instance is at the
77 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000078 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000079 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000080 WorkList.push_back(N);
81 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000082
Chris Lattnerf8dc0612008-02-03 06:49:24 +000083 /// removeFromWorkList - remove all instances of N from the worklist.
84 ///
85 void removeFromWorkList(SDNode *N) {
86 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
87 WorkList.end());
Chris Lattner01a22022005-10-10 22:04:48 +000088 }
Nate Begeman368e18d2006-02-16 21:11:51 +000089
Chris Lattnerf8dc0612008-02-03 06:49:24 +000090 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
91 bool AddTo = true);
92
Jim Laskey274062c2006-10-13 23:32:28 +000093 SDOperand CombineTo(SDNode *N, SDOperand Res, bool AddTo = true) {
94 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +000095 }
96
Jim Laskey274062c2006-10-13 23:32:28 +000097 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1,
98 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +000099 SDOperand To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000100 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000101 }
Chris Lattner0bd48932008-01-17 07:00:52 +0000102
Chris Lattner24664722006-03-01 04:53:38 +0000103 private:
104
Chris Lattner012f2412006-02-17 21:58:01 +0000105 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000106 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000107 /// propagation. If so, return true.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000108 bool SimplifyDemandedBits(SDOperand Op) {
109 APInt Demanded = APInt::getAllOnesValue(Op.getValueSizeInBits());
110 return SimplifyDemandedBits(Op, Demanded);
111 }
112
113 bool SimplifyDemandedBits(SDOperand Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000114
Chris Lattner448f2192006-11-11 00:39:41 +0000115 bool CombineToPreIndexedLoadStore(SDNode *N);
116 bool CombineToPostIndexedLoadStore(SDNode *N);
117
118
Dan Gohman389079b2007-10-08 17:57:15 +0000119 /// combine - call the node-specific routine that knows how to fold each
120 /// particular type of node. If that doesn't do anything, try the
121 /// target-specific DAG combines.
122 SDOperand combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000123
124 // Visitation implementation - Implement dag node combining for different
125 // node types. The semantics are as follows:
126 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000127 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000128 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000129 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000130 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000131 SDOperand visitTokenFactor(SDNode *N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000132 SDOperand visitMERGE_VALUES(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000133 SDOperand visitADD(SDNode *N);
134 SDOperand visitSUB(SDNode *N);
Chris Lattner91153682007-03-04 20:03:15 +0000135 SDOperand visitADDC(SDNode *N);
136 SDOperand visitADDE(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000137 SDOperand visitMUL(SDNode *N);
138 SDOperand visitSDIV(SDNode *N);
139 SDOperand visitUDIV(SDNode *N);
140 SDOperand visitSREM(SDNode *N);
141 SDOperand visitUREM(SDNode *N);
142 SDOperand visitMULHU(SDNode *N);
143 SDOperand visitMULHS(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +0000144 SDOperand visitSMUL_LOHI(SDNode *N);
145 SDOperand visitUMUL_LOHI(SDNode *N);
146 SDOperand visitSDIVREM(SDNode *N);
147 SDOperand visitUDIVREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000148 SDOperand visitAND(SDNode *N);
149 SDOperand visitOR(SDNode *N);
150 SDOperand visitXOR(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000151 SDOperand SimplifyVBinOp(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000152 SDOperand visitSHL(SDNode *N);
153 SDOperand visitSRA(SDNode *N);
154 SDOperand visitSRL(SDNode *N);
155 SDOperand visitCTLZ(SDNode *N);
156 SDOperand visitCTTZ(SDNode *N);
157 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000158 SDOperand visitSELECT(SDNode *N);
159 SDOperand visitSELECT_CC(SDNode *N);
160 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000161 SDOperand visitSIGN_EXTEND(SDNode *N);
162 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000163 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000164 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
165 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000166 SDOperand visitBIT_CONVERT(SDNode *N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000167 SDOperand visitBUILD_PAIR(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000168 SDOperand visitFADD(SDNode *N);
169 SDOperand visitFSUB(SDNode *N);
170 SDOperand visitFMUL(SDNode *N);
171 SDOperand visitFDIV(SDNode *N);
172 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000173 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000174 SDOperand visitSINT_TO_FP(SDNode *N);
175 SDOperand visitUINT_TO_FP(SDNode *N);
176 SDOperand visitFP_TO_SINT(SDNode *N);
177 SDOperand visitFP_TO_UINT(SDNode *N);
178 SDOperand visitFP_ROUND(SDNode *N);
179 SDOperand visitFP_ROUND_INREG(SDNode *N);
180 SDOperand visitFP_EXTEND(SDNode *N);
181 SDOperand visitFNEG(SDNode *N);
182 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000183 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000184 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000185 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000186 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000187 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
Evan Cheng513da432007-10-06 08:19:55 +0000188 SDOperand visitEXTRACT_VECTOR_ELT(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000189 SDOperand visitBUILD_VECTOR(SDNode *N);
190 SDOperand visitCONCAT_VECTORS(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000191 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000192
Evan Cheng44f1f092006-04-20 08:56:16 +0000193 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000194 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
195
Chris Lattnere70da202007-12-06 07:33:36 +0000196 SDOperand visitShiftByConstant(SDNode *N, unsigned Amt);
197
Chris Lattner40c62d52005-10-18 06:04:22 +0000198 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000199 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000200 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
201 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000202 SDOperand N3, ISD::CondCode CC,
203 bool NotExtCompare = false);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000204 SDOperand SimplifySetCC(MVT VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000205 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner5eee4272008-01-26 01:09:19 +0000206 SDOperand SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
207 unsigned HiOp);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000208 SDOperand CombineConsecutiveLoads(SDNode *N, MVT VT);
209 SDOperand ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT);
Nate Begeman69575232005-10-20 02:15:44 +0000210 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000211 SDOperand BuildUDIV(SDNode *N);
212 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Evan Chengc88138f2007-03-22 01:54:19 +0000213 SDOperand ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000214
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000215 SDOperand GetDemandedBits(SDOperand V, const APInt &Mask);
Chris Lattner2b4c2792007-10-13 06:35:54 +0000216
Jim Laskey6ff23e52006-10-04 16:53:27 +0000217 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
218 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000219 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000220 SmallVector<SDOperand, 8> &Aliases);
221
Jim Laskey096c22e2006-10-18 12:29:57 +0000222 /// isAlias - Return true if there is any possibility that the two addresses
223 /// overlap.
224 bool isAlias(SDOperand Ptr1, int64_t Size1,
225 const Value *SrcValue1, int SrcValueOffset1,
226 SDOperand Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000227 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000228
Jim Laskey7ca56af2006-10-11 13:47:09 +0000229 /// FindAliasInfo - Extracts the relevant alias information from the memory
230 /// node. Returns true if the operand was a load.
231 bool FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +0000232 SDOperand &Ptr, int64_t &Size,
233 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000234
Jim Laskey279f0532006-09-25 16:29:54 +0000235 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000236 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000237 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
238
Nate Begeman1d4d4142005-09-01 00:19:25 +0000239public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000240 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
241 : DAG(D),
242 TLI(D.getTargetLoweringInfo()),
243 AfterLegalize(false),
244 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000245
246 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000247 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000248 };
249}
250
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000251
252namespace {
253/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
254/// nodes from the worklist.
255class VISIBILITY_HIDDEN WorkListRemover :
256 public SelectionDAG::DAGUpdateListener {
257 DAGCombiner &DC;
258public:
Dan Gohmanb5660dc2008-02-20 16:44:09 +0000259 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000260
Duncan Sandsedfcf592008-06-11 11:42:12 +0000261 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000262 DC.removeFromWorkList(N);
263 }
264
265 virtual void NodeUpdated(SDNode *N) {
266 // Ignore updates.
267 }
268};
269}
270
Chris Lattner24664722006-03-01 04:53:38 +0000271//===----------------------------------------------------------------------===//
272// TargetLowering::DAGCombinerInfo implementation
273//===----------------------------------------------------------------------===//
274
275void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
276 ((DAGCombiner*)DC)->AddToWorkList(N);
277}
278
279SDOperand TargetLowering::DAGCombinerInfo::
280CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000281 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000282}
283
284SDOperand TargetLowering::DAGCombinerInfo::
285CombineTo(SDNode *N, SDOperand Res) {
286 return ((DAGCombiner*)DC)->CombineTo(N, Res);
287}
288
289
290SDOperand TargetLowering::DAGCombinerInfo::
291CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
292 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
293}
294
295
Chris Lattner24664722006-03-01 04:53:38 +0000296//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000297// Helper Functions
298//===----------------------------------------------------------------------===//
299
300/// isNegatibleForFree - Return 1 if we can compute the negated form of the
301/// specified expression for the same cost as the expression itself, or 2 if we
302/// can compute the negated form more cheaply than the expression itself.
Chris Lattner0254e702008-02-26 07:04:54 +0000303static char isNegatibleForFree(SDOperand Op, bool AfterLegalize,
304 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000305 // No compile time optimizations on this type.
306 if (Op.getValueType() == MVT::ppcf128)
307 return 0;
308
Chris Lattner29446522007-05-14 22:04:50 +0000309 // fneg is removable even if it has multiple uses.
310 if (Op.getOpcode() == ISD::FNEG) return 2;
311
312 // Don't allow anything with multiple uses.
313 if (!Op.hasOneUse()) return 0;
314
Chris Lattner3adf9512007-05-25 02:19:06 +0000315 // Don't recurse exponentially.
316 if (Depth > 6) return 0;
317
Chris Lattner29446522007-05-14 22:04:50 +0000318 switch (Op.getOpcode()) {
319 default: return false;
320 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000321 // Don't invert constant FP values after legalize. The negated constant
322 // isn't necessarily legal.
323 return AfterLegalize ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000324 case ISD::FADD:
325 // FIXME: determine better conditions for this xform.
326 if (!UnsafeFPMath) return 0;
327
328 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000329 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000330 return V;
331 // -(A+B) -> -B - A
Chris Lattner0254e702008-02-26 07:04:54 +0000332 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000333 case ISD::FSUB:
334 // We can't turn -(A-B) into B-A when we honor signed zeros.
335 if (!UnsafeFPMath) return 0;
336
337 // -(A-B) -> B-A
338 return 1;
339
340 case ISD::FMUL:
341 case ISD::FDIV:
342 if (HonorSignDependentRoundingFPMath()) return 0;
343
344 // -(X*Y) -> (-X * Y) or (X*-Y)
Chris Lattner0254e702008-02-26 07:04:54 +0000345 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000346 return V;
347
Chris Lattner0254e702008-02-26 07:04:54 +0000348 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000349
350 case ISD::FP_EXTEND:
351 case ISD::FP_ROUND:
352 case ISD::FSIN:
Chris Lattner0254e702008-02-26 07:04:54 +0000353 return isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000354 }
355}
356
357/// GetNegatedExpression - If isNegatibleForFree returns true, this function
358/// returns the newly negated expression.
Chris Lattner3adf9512007-05-25 02:19:06 +0000359static SDOperand GetNegatedExpression(SDOperand Op, SelectionDAG &DAG,
Chris Lattner0254e702008-02-26 07:04:54 +0000360 bool AfterLegalize, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000361 // fneg is removable even if it has multiple uses.
362 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
363
364 // Don't allow anything with multiple uses.
365 assert(Op.hasOneUse() && "Unknown reuse!");
366
Chris Lattner3adf9512007-05-25 02:19:06 +0000367 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000368 switch (Op.getOpcode()) {
369 default: assert(0 && "Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000370 case ISD::ConstantFP: {
371 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
372 V.changeSign();
373 return DAG.getConstantFP(V, Op.getValueType());
374 }
Chris Lattner29446522007-05-14 22:04:50 +0000375 case ISD::FADD:
376 // FIXME: determine better conditions for this xform.
377 assert(UnsafeFPMath);
378
379 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000380 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000381 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000382 GetNegatedExpression(Op.getOperand(0), DAG,
383 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000384 Op.getOperand(1));
385 // -(A+B) -> -B - A
386 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000387 GetNegatedExpression(Op.getOperand(1), DAG,
388 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000389 Op.getOperand(0));
390 case ISD::FSUB:
391 // We can't turn -(A-B) into B-A when we honor signed zeros.
392 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000393
394 // -(0-B) -> B
395 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000396 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000397 return Op.getOperand(1);
Chris Lattner29446522007-05-14 22:04:50 +0000398
399 // -(A-B) -> B-A
400 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
401 Op.getOperand(0));
402
403 case ISD::FMUL:
404 case ISD::FDIV:
405 assert(!HonorSignDependentRoundingFPMath());
406
407 // -(X*Y) -> -X * Y
Chris Lattneraeecb6c2008-02-26 17:09:59 +0000408 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000409 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000410 GetNegatedExpression(Op.getOperand(0), DAG,
411 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000412 Op.getOperand(1));
413
414 // -(X*Y) -> X * -Y
415 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
416 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000417 GetNegatedExpression(Op.getOperand(1), DAG,
418 AfterLegalize, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000419
420 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000421 case ISD::FSIN:
422 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000423 GetNegatedExpression(Op.getOperand(0), DAG,
424 AfterLegalize, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000425 case ISD::FP_ROUND:
426 return DAG.getNode(ISD::FP_ROUND, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000427 GetNegatedExpression(Op.getOperand(0), DAG,
428 AfterLegalize, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000429 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000430 }
431}
Chris Lattner24664722006-03-01 04:53:38 +0000432
433
Nate Begeman4ebd8052005-09-01 23:24:04 +0000434// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
435// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000436// Also, set the incoming LHS, RHS, and CC references to the appropriate
437// nodes based on the type of node we are checking. This simplifies life a
438// bit for the callers.
439static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
440 SDOperand &CC) {
441 if (N.getOpcode() == ISD::SETCC) {
442 LHS = N.getOperand(0);
443 RHS = N.getOperand(1);
444 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000445 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000446 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000447 if (N.getOpcode() == ISD::SELECT_CC &&
448 N.getOperand(2).getOpcode() == ISD::Constant &&
449 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000450 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000451 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
452 LHS = N.getOperand(0);
453 RHS = N.getOperand(1);
454 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000455 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000456 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000457 return false;
458}
459
Nate Begeman99801192005-09-07 23:25:52 +0000460// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
461// one use. If this is true, it allows the users to invert the operation for
462// free when it is profitable to do so.
463static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000464 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000465 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000466 return true;
467 return false;
468}
469
Nate Begemancd4d58c2006-02-03 06:46:56 +0000470SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
Duncan Sands83ec4b62008-06-06 12:08:01 +0000471 MVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000472 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
473 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
474 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
475 if (isa<ConstantSDNode>(N1)) {
476 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000477 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000478 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
479 } else if (N0.hasOneUse()) {
480 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000481 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000482 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
483 }
484 }
485 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
486 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
487 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
488 if (isa<ConstantSDNode>(N0)) {
489 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000490 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000491 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
492 } else if (N1.hasOneUse()) {
493 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000494 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000495 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
496 }
497 }
498 return SDOperand();
499}
500
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000501SDOperand DAGCombiner::CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
502 bool AddTo) {
503 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
504 ++NodesCombined;
505 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
506 DOUT << "\nWith: "; DEBUG(To[0].Val->dump(&DAG));
507 DOUT << " and " << NumTo-1 << " other values\n";
508 WorkListRemover DeadNodes(*this);
509 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
510
511 if (AddTo) {
512 // Push the new nodes and any users onto the worklist
513 for (unsigned i = 0, e = NumTo; i != e; ++i) {
514 AddToWorkList(To[i].Val);
515 AddUsersToWorkList(To[i].Val);
516 }
517 }
518
519 // Nodes can be reintroduced into the worklist. Make sure we do not
520 // process a node that has been replaced.
521 removeFromWorkList(N);
522
523 // Finally, since the node is now dead, remove it from the graph.
524 DAG.DeleteNode(N);
525 return SDOperand(N, 0);
526}
527
528/// SimplifyDemandedBits - Check the specified integer node value to see if
529/// it can be simplified or if things it uses can be simplified by bit
530/// propagation. If so, return true.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000531bool DAGCombiner::SimplifyDemandedBits(SDOperand Op, const APInt &Demanded) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000532 TargetLowering::TargetLoweringOpt TLO(DAG, AfterLegalize);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000533 APInt KnownZero, KnownOne;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000534 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
535 return false;
536
537 // Revisit the node.
538 AddToWorkList(Op.Val);
539
540 // Replace the old value with the new one.
541 ++NodesCombined;
542 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.Val->dump(&DAG));
543 DOUT << "\nWith: "; DEBUG(TLO.New.Val->dump(&DAG));
544 DOUT << '\n';
545
546 // Replace all uses. If any nodes become isomorphic to other nodes and
547 // are deleted, make sure to remove them from our worklist.
548 WorkListRemover DeadNodes(*this);
549 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
550
551 // Push the new node and any (possibly new) users onto the worklist.
552 AddToWorkList(TLO.New.Val);
553 AddUsersToWorkList(TLO.New.Val);
554
555 // Finally, if the node is now dead, remove it from the graph. The node
556 // may not be dead if the replacement process recursively simplified to
557 // something else needing this node.
558 if (TLO.Old.Val->use_empty()) {
559 removeFromWorkList(TLO.Old.Val);
560
561 // If the operands of this node are only used by the node, they will now
562 // be dead. Make sure to visit them first to delete dead nodes early.
563 for (unsigned i = 0, e = TLO.Old.Val->getNumOperands(); i != e; ++i)
564 if (TLO.Old.Val->getOperand(i).Val->hasOneUse())
565 AddToWorkList(TLO.Old.Val->getOperand(i).Val);
566
567 DAG.DeleteNode(TLO.Old.Val);
568 }
569 return true;
570}
571
Chris Lattner29446522007-05-14 22:04:50 +0000572//===----------------------------------------------------------------------===//
573// Main DAG Combiner implementation
574//===----------------------------------------------------------------------===//
575
Nate Begeman4ebd8052005-09-01 23:24:04 +0000576void DAGCombiner::Run(bool RunningAfterLegalize) {
577 // set the instance variable, so that the various visit routines may use it.
578 AfterLegalize = RunningAfterLegalize;
579
Nate Begeman646d7e22005-09-02 21:18:40 +0000580 // Add all the dag nodes to the worklist.
Dan Gohmancf8462f2008-06-30 21:04:06 +0000581 WorkList.reserve(DAG.allnodes_size());
Chris Lattnerde202b32005-11-09 23:47:37 +0000582 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
583 E = DAG.allnodes_end(); I != E; ++I)
584 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000585
Chris Lattner95038592005-10-05 06:35:28 +0000586 // Create a dummy node (which is not added to allnodes), that adds a reference
587 // to the root node, preventing it from being deleted, and tracking any
588 // changes of the root.
589 HandleSDNode Dummy(DAG.getRoot());
590
Jim Laskey26f7fa72006-10-17 19:33:52 +0000591 // The root of the dag may dangle to deleted nodes until the dag combiner is
592 // done. Set it to null to avoid confusion.
593 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000594
Nate Begeman1d4d4142005-09-01 00:19:25 +0000595 // while the worklist isn't empty, inspect the node on the end of it and
596 // try and combine it.
597 while (!WorkList.empty()) {
598 SDNode *N = WorkList.back();
599 WorkList.pop_back();
600
601 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000602 // N is deleted from the DAG, since they too may now be dead or may have a
603 // reduced number of uses, allowing other xforms.
604 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000605 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000606 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000607
Chris Lattner95038592005-10-05 06:35:28 +0000608 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000609 continue;
610 }
611
Dan Gohman389079b2007-10-08 17:57:15 +0000612 SDOperand RV = combine(N);
Chris Lattner24664722006-03-01 04:53:38 +0000613
Chris Lattner50d8e492008-01-25 23:34:24 +0000614 if (RV.Val == 0)
615 continue;
616
617 ++NodesCombined;
Chris Lattner5eee4272008-01-26 01:09:19 +0000618
Chris Lattner50d8e492008-01-25 23:34:24 +0000619 // If we get back the same node we passed in, rather than a new node or
620 // zero, we know that the node must have defined multiple values and
621 // CombineTo was used. Since CombineTo takes care of the worklist
622 // mechanics for us, we have no work to do in this case.
623 if (RV.Val == N)
624 continue;
625
626 assert(N->getOpcode() != ISD::DELETED_NODE &&
627 RV.Val->getOpcode() != ISD::DELETED_NODE &&
628 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000629
Chris Lattner50d8e492008-01-25 23:34:24 +0000630 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
631 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
632 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000633 WorkListRemover DeadNodes(*this);
Chris Lattner50d8e492008-01-25 23:34:24 +0000634 if (N->getNumValues() == RV.Val->getNumValues())
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000635 DAG.ReplaceAllUsesWith(N, RV.Val, &DeadNodes);
Chris Lattner50d8e492008-01-25 23:34:24 +0000636 else {
Chris Lattner5eee4272008-01-26 01:09:19 +0000637 assert(N->getValueType(0) == RV.getValueType() &&
638 N->getNumValues() == 1 && "Type mismatch");
Chris Lattner50d8e492008-01-25 23:34:24 +0000639 SDOperand OpV = RV;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000640 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000641 }
Chris Lattner50d8e492008-01-25 23:34:24 +0000642
643 // Push the new node and any users onto the worklist
644 AddToWorkList(RV.Val);
645 AddUsersToWorkList(RV.Val);
646
647 // Add any uses of the old node to the worklist in case this node is the
648 // last one that uses them. They may become dead after this node is
649 // deleted.
650 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
651 AddToWorkList(N->getOperand(i).Val);
652
653 // Nodes can be reintroduced into the worklist. Make sure we do not
654 // process a node that has been replaced.
655 removeFromWorkList(N);
Chris Lattner50d8e492008-01-25 23:34:24 +0000656
657 // Finally, since the node is now dead, remove it from the graph.
658 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000659 }
Chris Lattner95038592005-10-05 06:35:28 +0000660
661 // If the root changed (e.g. it was a dead load, update the root).
662 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000663}
664
Nate Begeman83e75ec2005-09-06 04:43:02 +0000665SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000666 switch(N->getOpcode()) {
667 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000668 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000669 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000670 case ISD::ADD: return visitADD(N);
671 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000672 case ISD::ADDC: return visitADDC(N);
673 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000674 case ISD::MUL: return visitMUL(N);
675 case ISD::SDIV: return visitSDIV(N);
676 case ISD::UDIV: return visitUDIV(N);
677 case ISD::SREM: return visitSREM(N);
678 case ISD::UREM: return visitUREM(N);
679 case ISD::MULHU: return visitMULHU(N);
680 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000681 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
682 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
683 case ISD::SDIVREM: return visitSDIVREM(N);
684 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000685 case ISD::AND: return visitAND(N);
686 case ISD::OR: return visitOR(N);
687 case ISD::XOR: return visitXOR(N);
688 case ISD::SHL: return visitSHL(N);
689 case ISD::SRA: return visitSRA(N);
690 case ISD::SRL: return visitSRL(N);
691 case ISD::CTLZ: return visitCTLZ(N);
692 case ISD::CTTZ: return visitCTTZ(N);
693 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000694 case ISD::SELECT: return visitSELECT(N);
695 case ISD::SELECT_CC: return visitSELECT_CC(N);
696 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000697 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
698 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000699 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000700 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
701 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000702 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000703 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000704 case ISD::FADD: return visitFADD(N);
705 case ISD::FSUB: return visitFSUB(N);
706 case ISD::FMUL: return visitFMUL(N);
707 case ISD::FDIV: return visitFDIV(N);
708 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000709 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000710 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
711 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
712 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
713 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
714 case ISD::FP_ROUND: return visitFP_ROUND(N);
715 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
716 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
717 case ISD::FNEG: return visitFNEG(N);
718 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000719 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000720 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000721 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000722 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000723 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000724 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000725 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
726 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000727 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000728 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000729 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000730}
731
Dan Gohman389079b2007-10-08 17:57:15 +0000732SDOperand DAGCombiner::combine(SDNode *N) {
733
734 SDOperand RV = visit(N);
735
736 // If nothing happened, try a target-specific DAG combine.
737 if (RV.Val == 0) {
738 assert(N->getOpcode() != ISD::DELETED_NODE &&
739 "Node was deleted but visit returned NULL!");
740
741 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
742 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
743
744 // Expose the DAG combiner to the target combiner impls.
745 TargetLowering::DAGCombinerInfo
746 DagCombineInfo(DAG, !AfterLegalize, false, this);
747
748 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
749 }
750 }
751
Evan Cheng08b11732008-03-22 01:55:50 +0000752 // If N is a commutative binary node, try commuting it to enable more
753 // sdisel CSE.
754 if (RV.Val == 0 &&
755 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
756 N->getNumValues() == 1) {
757 SDOperand N0 = N->getOperand(0);
758 SDOperand N1 = N->getOperand(1);
759 // Constant operands are canonicalized to RHS.
760 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
761 SDOperand Ops[] = { N1, N0 };
762 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
763 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +0000764 if (CSENode)
Evan Cheng08b11732008-03-22 01:55:50 +0000765 return SDOperand(CSENode, 0);
766 }
767 }
768
Dan Gohman389079b2007-10-08 17:57:15 +0000769 return RV;
770}
771
Chris Lattner6270f682006-10-08 22:57:01 +0000772/// getInputChainForNode - Given a node, return its input chain if it has one,
773/// otherwise return a null sd operand.
774static SDOperand getInputChainForNode(SDNode *N) {
775 if (unsigned NumOps = N->getNumOperands()) {
776 if (N->getOperand(0).getValueType() == MVT::Other)
777 return N->getOperand(0);
778 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
779 return N->getOperand(NumOps-1);
780 for (unsigned i = 1; i < NumOps-1; ++i)
781 if (N->getOperand(i).getValueType() == MVT::Other)
782 return N->getOperand(i);
783 }
784 return SDOperand(0, 0);
785}
786
Nate Begeman83e75ec2005-09-06 04:43:02 +0000787SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000788 // If N has two operands, where one has an input chain equal to the other,
789 // the 'other' chain is redundant.
790 if (N->getNumOperands() == 2) {
791 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
792 return N->getOperand(0);
793 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
794 return N->getOperand(1);
795 }
796
Chris Lattnerc76d4412007-05-16 06:37:59 +0000797 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
798 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
799 SmallPtrSet<SDNode*, 16> SeenOps;
800 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000801
802 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000803 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000804
Jim Laskey71382342006-10-07 23:37:56 +0000805 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000806 // encountered.
807 for (unsigned i = 0; i < TFs.size(); ++i) {
808 SDNode *TF = TFs[i];
809
Jim Laskey6ff23e52006-10-04 16:53:27 +0000810 // Check each of the operands.
811 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
812 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000813
Jim Laskey6ff23e52006-10-04 16:53:27 +0000814 switch (Op.getOpcode()) {
815 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000816 // Entry tokens don't need to be added to the list. They are
817 // rededundant.
818 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000819 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000820
Jim Laskey6ff23e52006-10-04 16:53:27 +0000821 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000822 if ((CombinerAA || Op.hasOneUse()) &&
823 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000824 // Queue up for processing.
825 TFs.push_back(Op.Val);
826 // Clean up in case the token factor is removed.
827 AddToWorkList(Op.Val);
828 Changed = true;
829 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000830 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000831 // Fall thru
832
833 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000834 // Only add if it isn't already in the list.
835 if (SeenOps.insert(Op.Val))
Jim Laskeybc588b82006-10-05 15:07:25 +0000836 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000837 else
838 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000839 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000840 }
841 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000842 }
843
844 SDOperand Result;
845
846 // If we've change things around then replace token factor.
847 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000848 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000849 // The entry token is the only possible outcome.
850 Result = DAG.getEntryNode();
851 } else {
852 // New and improved token factor.
853 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000854 }
Jim Laskey274062c2006-10-13 23:32:28 +0000855
856 // Don't add users to work list.
857 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000858 }
Jim Laskey279f0532006-09-25 16:29:54 +0000859
Jim Laskey6ff23e52006-10-04 16:53:27 +0000860 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000861}
862
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000863/// MERGE_VALUES can always be eliminated.
864SDOperand DAGCombiner::visitMERGE_VALUES(SDNode *N) {
865 WorkListRemover DeadNodes(*this);
866 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
867 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, i), N->getOperand(i),
868 &DeadNodes);
869 removeFromWorkList(N);
870 DAG.DeleteNode(N);
871 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
872}
873
874
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000875static
876SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000877 MVT VT = N0.getValueType();
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000878 SDOperand N00 = N0.getOperand(0);
879 SDOperand N01 = N0.getOperand(1);
880 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
881 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
882 isa<ConstantSDNode>(N00.getOperand(1))) {
883 N0 = DAG.getNode(ISD::ADD, VT,
884 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
885 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
886 return DAG.getNode(ISD::ADD, VT, N0, N1);
887 }
888 return SDOperand();
889}
890
Evan Chengb13cdbd2007-06-21 07:39:16 +0000891static
892SDOperand combineSelectAndUse(SDNode *N, SDOperand Slct, SDOperand OtherOp,
893 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000894 MVT VT = N->getValueType(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000895 unsigned Opc = N->getOpcode();
896 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
897 SDOperand LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
898 SDOperand RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
899 ISD::CondCode CC = ISD::SETCC_INVALID;
900 if (isSlctCC)
901 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
902 else {
903 SDOperand CCOp = Slct.getOperand(0);
904 if (CCOp.getOpcode() == ISD::SETCC)
905 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
906 }
907
908 bool DoXform = false;
909 bool InvCC = false;
910 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
911 "Bad input!");
912 if (LHS.getOpcode() == ISD::Constant &&
913 cast<ConstantSDNode>(LHS)->isNullValue())
914 DoXform = true;
915 else if (CC != ISD::SETCC_INVALID &&
916 RHS.getOpcode() == ISD::Constant &&
917 cast<ConstantSDNode>(RHS)->isNullValue()) {
918 std::swap(LHS, RHS);
Chris Lattner4626b252008-01-17 07:20:38 +0000919 SDOperand Op0 = Slct.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000920 bool isInt = (isSlctCC ? Op0.getValueType() :
921 Op0.getOperand(0).getValueType()).isInteger();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000922 CC = ISD::getSetCCInverse(CC, isInt);
923 DoXform = true;
924 InvCC = true;
925 }
926
927 if (DoXform) {
928 SDOperand Result = DAG.getNode(Opc, VT, OtherOp, RHS);
929 if (isSlctCC)
930 return DAG.getSelectCC(OtherOp, Result,
931 Slct.getOperand(0), Slct.getOperand(1), CC);
932 SDOperand CCOp = Slct.getOperand(0);
933 if (InvCC)
934 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
935 CCOp.getOperand(1), CC);
936 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
937 }
938 return SDOperand();
939}
940
Nate Begeman83e75ec2005-09-06 04:43:02 +0000941SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000942 SDOperand N0 = N->getOperand(0);
943 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000944 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
945 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000946 MVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000947
948 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +0000949 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +0000950 SDOperand FoldedVOp = SimplifyVBinOp(N);
951 if (FoldedVOp.Val) return FoldedVOp;
952 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000953
Dan Gohman613e0d82007-07-03 14:03:57 +0000954 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000955 if (N0.getOpcode() == ISD::UNDEF)
956 return N0;
957 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000958 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000959 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000960 if (N0C && N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +0000961 return DAG.getConstant(N0C->getAPIntValue() + N1C->getAPIntValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000962 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000963 if (N0C && !N1C)
964 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000965 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000966 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000967 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000968 // fold ((c1-A)+c2) -> (c1+c2)-A
969 if (N1C && N0.getOpcode() == ISD::SUB)
970 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
971 return DAG.getNode(ISD::SUB, VT,
Dan Gohman002e5d02008-03-13 22:13:53 +0000972 DAG.getConstant(N1C->getAPIntValue()+
973 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000974 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000975 // reassociate add
976 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
977 if (RADD.Val != 0)
978 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000979 // fold ((0-A) + B) -> B-A
980 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
981 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000982 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000983 // fold (A + (0-B)) -> A-B
984 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
985 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000986 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000987 // fold (A+(B-A)) -> B
988 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000989 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000990
Duncan Sands83ec4b62008-06-06 12:08:01 +0000991 if (!VT.isVector() && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000992 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000993
994 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000995 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +0000996 APInt LHSZero, LHSOne;
997 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000998 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +0000999 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001000 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001001 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +00001002
1003 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1004 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1005 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1006 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1007 return DAG.getNode(ISD::OR, VT, N0, N1);
1008 }
1009 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001010
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001011 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
1012 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
1013 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
1014 if (Result.Val) return Result;
1015 }
1016 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
1017 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
1018 if (Result.Val) return Result;
1019 }
1020
Evan Chengb13cdbd2007-06-21 07:39:16 +00001021 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
1022 if (N0.getOpcode() == ISD::SELECT && N0.Val->hasOneUse()) {
1023 SDOperand Result = combineSelectAndUse(N, N0, N1, DAG);
1024 if (Result.Val) return Result;
1025 }
1026 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1027 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1028 if (Result.Val) return Result;
1029 }
1030
Nate Begeman83e75ec2005-09-06 04:43:02 +00001031 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001032}
1033
Chris Lattner91153682007-03-04 20:03:15 +00001034SDOperand DAGCombiner::visitADDC(SDNode *N) {
1035 SDOperand N0 = N->getOperand(0);
1036 SDOperand N1 = N->getOperand(1);
1037 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1038 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001039 MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001040
1041 // If the flag result is dead, turn this into an ADD.
1042 if (N->hasNUsesOfValue(0, 1))
1043 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +00001044 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +00001045
1046 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001047 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001048 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001049
Chris Lattnerb6541762007-03-04 20:40:38 +00001050 // fold (addc x, 0) -> x + no carry out
1051 if (N1C && N1C->isNullValue())
1052 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1053
1054 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001055 APInt LHSZero, LHSOne;
1056 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001057 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001058 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001059 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001060 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001061
1062 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1063 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1064 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1065 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1066 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1067 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1068 }
Chris Lattner91153682007-03-04 20:03:15 +00001069
1070 return SDOperand();
1071}
1072
1073SDOperand DAGCombiner::visitADDE(SDNode *N) {
1074 SDOperand N0 = N->getOperand(0);
1075 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +00001076 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001077 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1078 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001079 //MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001080
1081 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001082 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001083 return DAG.getNode(ISD::ADDE, N->getVTList(), N1, N0, CarryIn);
Chris Lattner91153682007-03-04 20:03:15 +00001084
Chris Lattnerb6541762007-03-04 20:40:38 +00001085 // fold (adde x, y, false) -> (addc x, y)
Dan Gohman0a4627d2008-06-23 15:29:14 +00001086 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Dan Gohman56867522008-06-21 22:06:07 +00001087 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001088
1089 return SDOperand();
1090}
1091
1092
1093
Nate Begeman83e75ec2005-09-06 04:43:02 +00001094SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001095 SDOperand N0 = N->getOperand(0);
1096 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001097 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1098 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001099 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001100
Dan Gohman7f321562007-06-25 16:23:39 +00001101 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001102 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001103 SDOperand FoldedVOp = SimplifyVBinOp(N);
1104 if (FoldedVOp.Val) return FoldedVOp;
1105 }
Dan Gohman7f321562007-06-25 16:23:39 +00001106
Chris Lattner854077d2005-10-17 01:07:11 +00001107 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001108 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001109 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001110 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001111 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001112 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001113 // fold (sub x, c) -> (add x, -c)
1114 if (N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +00001115 return DAG.getNode(ISD::ADD, VT, N0,
1116 DAG.getConstant(-N1C->getAPIntValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001117 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001118 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001119 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001120 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001121 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001122 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001123 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1124 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1125 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1126 if (Result.Val) return Result;
1127 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001128 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001129 if (N0.getOpcode() == ISD::UNDEF)
1130 return N0;
1131 if (N1.getOpcode() == ISD::UNDEF)
1132 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001133
Nate Begeman83e75ec2005-09-06 04:43:02 +00001134 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001135}
1136
Nate Begeman83e75ec2005-09-06 04:43:02 +00001137SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001138 SDOperand N0 = N->getOperand(0);
1139 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001140 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1141 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001142 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001143
Dan Gohman7f321562007-06-25 16:23:39 +00001144 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001145 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001146 SDOperand FoldedVOp = SimplifyVBinOp(N);
1147 if (FoldedVOp.Val) return FoldedVOp;
1148 }
Dan Gohman7f321562007-06-25 16:23:39 +00001149
Dan Gohman613e0d82007-07-03 14:03:57 +00001150 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001151 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001152 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001153 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001154 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001155 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001156 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001157 if (N0C && !N1C)
1158 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001159 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001160 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001161 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001162 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001163 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001164 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001165 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001166 if (N1C && N1C->getAPIntValue().isPowerOf2())
Chris Lattner3e6099b2005-10-30 06:41:49 +00001167 return DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001168 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001169 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001170 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1171 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1172 // FIXME: If the input is something that is easily negated (e.g. a
1173 // single-use add), we should put the negate there.
1174 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1175 DAG.getNode(ISD::SHL, VT, N0,
1176 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1177 TLI.getShiftAmountTy())));
1178 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001179
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001180 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1181 if (N1C && N0.getOpcode() == ISD::SHL &&
1182 isa<ConstantSDNode>(N0.getOperand(1))) {
1183 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001184 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001185 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1186 }
1187
1188 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1189 // use.
1190 {
1191 SDOperand Sh(0,0), Y(0,0);
1192 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1193 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1194 N0.Val->hasOneUse()) {
1195 Sh = N0; Y = N1;
1196 } else if (N1.getOpcode() == ISD::SHL &&
1197 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1198 Sh = N1; Y = N0;
1199 }
1200 if (Sh.Val) {
1201 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1202 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1203 }
1204 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001205 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1206 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1207 isa<ConstantSDNode>(N0.getOperand(1))) {
1208 return DAG.getNode(ISD::ADD, VT,
1209 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1210 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1211 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001212
Nate Begemancd4d58c2006-02-03 06:46:56 +00001213 // reassociate mul
1214 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1215 if (RMUL.Val != 0)
1216 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001217
Nate Begeman83e75ec2005-09-06 04:43:02 +00001218 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001219}
1220
Nate Begeman83e75ec2005-09-06 04:43:02 +00001221SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001222 SDOperand N0 = N->getOperand(0);
1223 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001224 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1225 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001226 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001227
Dan Gohman7f321562007-06-25 16:23:39 +00001228 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001229 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001230 SDOperand FoldedVOp = SimplifyVBinOp(N);
1231 if (FoldedVOp.Val) return FoldedVOp;
1232 }
Dan Gohman7f321562007-06-25 16:23:39 +00001233
Nate Begeman1d4d4142005-09-01 00:19:25 +00001234 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001235 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001236 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001237 // fold (sdiv X, 1) -> X
1238 if (N1C && N1C->getSignExtended() == 1LL)
1239 return N0;
1240 // fold (sdiv X, -1) -> 0-X
1241 if (N1C && N1C->isAllOnesValue())
1242 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001243 // If we know the sign bits of both operands are zero, strength reduce to a
1244 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001245 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001246 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerf32aac32008-01-27 23:32:17 +00001247 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1248 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001249 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001250 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001251 (isPowerOf2_64(N1C->getSignExtended()) ||
1252 isPowerOf2_64(-N1C->getSignExtended()))) {
1253 // If dividing by powers of two is cheap, then don't perform the following
1254 // fold.
1255 if (TLI.isPow2DivCheap())
1256 return SDOperand();
1257 int64_t pow2 = N1C->getSignExtended();
1258 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001259 unsigned lg2 = Log2_64(abs2);
1260 // Splat the sign bit into the register
1261 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001262 DAG.getConstant(VT.getSizeInBits()-1,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001263 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001264 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001265 // Add (N0 < 0) ? abs2 - 1 : 0;
1266 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001267 DAG.getConstant(VT.getSizeInBits()-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001268 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001269 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001270 AddToWorkList(SRL.Val);
1271 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001272 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1273 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001274 // If we're dividing by a positive value, we're done. Otherwise, we must
1275 // negate the result.
1276 if (pow2 > 0)
1277 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001278 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001279 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1280 }
Nate Begeman69575232005-10-20 02:15:44 +00001281 // if integer divide is expensive and we satisfy the requirements, emit an
1282 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001283 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001284 !TLI.isIntDivCheap()) {
1285 SDOperand Op = BuildSDIV(N);
1286 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001287 }
Dan Gohman7f321562007-06-25 16:23:39 +00001288
Dan Gohman613e0d82007-07-03 14:03:57 +00001289 // undef / X -> 0
1290 if (N0.getOpcode() == ISD::UNDEF)
1291 return DAG.getConstant(0, VT);
1292 // X / undef -> undef
1293 if (N1.getOpcode() == ISD::UNDEF)
1294 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001295
Nate Begeman83e75ec2005-09-06 04:43:02 +00001296 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001297}
1298
Nate Begeman83e75ec2005-09-06 04:43:02 +00001299SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001300 SDOperand N0 = N->getOperand(0);
1301 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001302 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1303 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001304 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001305
Dan Gohman7f321562007-06-25 16:23:39 +00001306 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001307 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001308 SDOperand FoldedVOp = SimplifyVBinOp(N);
1309 if (FoldedVOp.Val) return FoldedVOp;
1310 }
Dan Gohman7f321562007-06-25 16:23:39 +00001311
Nate Begeman1d4d4142005-09-01 00:19:25 +00001312 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001313 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001314 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001315 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001316 if (N1C && N1C->getAPIntValue().isPowerOf2())
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001317 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001318 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001319 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001320 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1321 if (N1.getOpcode() == ISD::SHL) {
1322 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001323 if (SHC->getAPIntValue().isPowerOf2()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001324 MVT ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001325 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001326 DAG.getConstant(SHC->getAPIntValue()
1327 .logBase2(),
Nate Begemanc031e332006-02-05 07:36:48 +00001328 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001329 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001330 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001331 }
1332 }
1333 }
Nate Begeman69575232005-10-20 02:15:44 +00001334 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001335 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Chris Lattnere9936d12005-10-22 18:50:15 +00001336 SDOperand Op = BuildUDIV(N);
1337 if (Op.Val) return Op;
1338 }
Dan Gohman7f321562007-06-25 16:23:39 +00001339
Dan Gohman613e0d82007-07-03 14:03:57 +00001340 // undef / X -> 0
1341 if (N0.getOpcode() == ISD::UNDEF)
1342 return DAG.getConstant(0, VT);
1343 // X / undef -> undef
1344 if (N1.getOpcode() == ISD::UNDEF)
1345 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001346
Nate Begeman83e75ec2005-09-06 04:43:02 +00001347 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001348}
1349
Nate Begeman83e75ec2005-09-06 04:43:02 +00001350SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001351 SDOperand N0 = N->getOperand(0);
1352 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001353 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1354 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001355 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001356
1357 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001358 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001359 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001360 // If we know the sign bits of both operands are zero, strength reduce to a
1361 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001362 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001363 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001364 return DAG.getNode(ISD::UREM, VT, N0, N1);
1365 }
Chris Lattner26d29902006-10-12 20:58:32 +00001366
Dan Gohman77003042007-11-26 23:46:11 +00001367 // If X/C can be simplified by the division-by-constant logic, lower
1368 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001369 if (N1C && !N1C->isNullValue()) {
1370 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Chris Lattner5eee4272008-01-26 01:09:19 +00001371 AddToWorkList(Div.Val);
Dan Gohman77003042007-11-26 23:46:11 +00001372 SDOperand OptimizedDiv = combine(Div.Val);
1373 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1374 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1375 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1376 AddToWorkList(Mul.Val);
1377 return Sub;
1378 }
Chris Lattner26d29902006-10-12 20:58:32 +00001379 }
1380
Dan Gohman613e0d82007-07-03 14:03:57 +00001381 // undef % X -> 0
1382 if (N0.getOpcode() == ISD::UNDEF)
1383 return DAG.getConstant(0, VT);
1384 // X % undef -> undef
1385 if (N1.getOpcode() == ISD::UNDEF)
1386 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001387
Nate Begeman83e75ec2005-09-06 04:43:02 +00001388 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001389}
1390
Nate Begeman83e75ec2005-09-06 04:43:02 +00001391SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001392 SDOperand N0 = N->getOperand(0);
1393 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001394 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1395 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001396 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001397
1398 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001399 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001400 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001401 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001402 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
1403 return DAG.getNode(ISD::AND, VT, N0,
1404 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001405 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1406 if (N1.getOpcode() == ISD::SHL) {
1407 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001408 if (SHC->getAPIntValue().isPowerOf2()) {
1409 SDOperand Add =
1410 DAG.getNode(ISD::ADD, VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001411 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00001412 VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001413 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001414 return DAG.getNode(ISD::AND, VT, N0, Add);
1415 }
1416 }
1417 }
Chris Lattner26d29902006-10-12 20:58:32 +00001418
Dan Gohman77003042007-11-26 23:46:11 +00001419 // If X/C can be simplified by the division-by-constant logic, lower
1420 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001421 if (N1C && !N1C->isNullValue()) {
1422 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman77003042007-11-26 23:46:11 +00001423 SDOperand OptimizedDiv = combine(Div.Val);
1424 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1425 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1426 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1427 AddToWorkList(Mul.Val);
1428 return Sub;
1429 }
Chris Lattner26d29902006-10-12 20:58:32 +00001430 }
1431
Dan Gohman613e0d82007-07-03 14:03:57 +00001432 // undef % X -> 0
1433 if (N0.getOpcode() == ISD::UNDEF)
1434 return DAG.getConstant(0, VT);
1435 // X % undef -> undef
1436 if (N1.getOpcode() == ISD::UNDEF)
1437 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001438
Nate Begeman83e75ec2005-09-06 04:43:02 +00001439 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001440}
1441
Nate Begeman83e75ec2005-09-06 04:43:02 +00001442SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001443 SDOperand N0 = N->getOperand(0);
1444 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001445 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001446 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001447
1448 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001449 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001450 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001451 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001452 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001453 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001454 DAG.getConstant(N0.getValueType().getSizeInBits()-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001455 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001456 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001457 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001458 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001459
Nate Begeman83e75ec2005-09-06 04:43:02 +00001460 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001461}
1462
Nate Begeman83e75ec2005-09-06 04:43:02 +00001463SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001464 SDOperand N0 = N->getOperand(0);
1465 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001466 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001467 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001468
1469 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001470 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001471 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001472 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001473 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001474 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001475 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001476 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001477 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001478
Nate Begeman83e75ec2005-09-06 04:43:02 +00001479 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001480}
1481
Dan Gohman389079b2007-10-08 17:57:15 +00001482/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1483/// compute two values. LoOp and HiOp give the opcodes for the two computations
1484/// that are being performed. Return true if a simplification was made.
1485///
Chris Lattner5eee4272008-01-26 01:09:19 +00001486SDOperand DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1487 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001488 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001489 bool HiExists = N->hasAnyUseOfValue(1);
1490 if (!HiExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001491 (!AfterLegalize ||
1492 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001493 SDOperand Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
1494 N->getNumOperands());
1495 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001496 }
1497
1498 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001499 bool LoExists = N->hasAnyUseOfValue(0);
1500 if (!LoExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001501 (!AfterLegalize ||
1502 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001503 SDOperand Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
1504 N->getNumOperands());
1505 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001506 }
1507
Evan Cheng44711942007-11-08 09:25:29 +00001508 // If both halves are used, return as it is.
1509 if (LoExists && HiExists)
Chris Lattner5eee4272008-01-26 01:09:19 +00001510 return SDOperand();
Evan Cheng44711942007-11-08 09:25:29 +00001511
1512 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001513 if (LoExists) {
1514 SDOperand Lo = DAG.getNode(LoOp, N->getValueType(0),
1515 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001516 AddToWorkList(Lo.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001517 SDOperand LoOpt = combine(Lo.Val);
Chris Lattner5eee4272008-01-26 01:09:19 +00001518 if (LoOpt.Val && LoOpt.Val != Lo.Val &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001519 (!AfterLegalize ||
1520 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001521 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001522 }
1523
Evan Cheng44711942007-11-08 09:25:29 +00001524 if (HiExists) {
1525 SDOperand Hi = DAG.getNode(HiOp, N->getValueType(1),
1526 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001527 AddToWorkList(Hi.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001528 SDOperand HiOpt = combine(Hi.Val);
1529 if (HiOpt.Val && HiOpt != Hi &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001530 (!AfterLegalize ||
1531 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001532 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001533 }
Chris Lattner5eee4272008-01-26 01:09:19 +00001534 return SDOperand();
Dan Gohman389079b2007-10-08 17:57:15 +00001535}
1536
1537SDOperand DAGCombiner::visitSMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001538 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
1539 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001540
1541 return SDOperand();
1542}
1543
1544SDOperand DAGCombiner::visitUMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001545 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
1546 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001547
1548 return SDOperand();
1549}
1550
1551SDOperand DAGCombiner::visitSDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001552 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
1553 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001554
1555 return SDOperand();
1556}
1557
1558SDOperand DAGCombiner::visitUDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001559 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
1560 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001561
1562 return SDOperand();
1563}
1564
Chris Lattner35e5c142006-05-05 05:51:50 +00001565/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1566/// two operands of the same opcode, try to simplify it.
1567SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1568 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001569 MVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00001570 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1571
Chris Lattner540121f2006-05-05 06:31:05 +00001572 // For each of OP in AND/OR/XOR:
1573 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1574 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1575 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001576 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001577 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001578 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001579 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1580 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1581 N0.getOperand(0).getValueType(),
1582 N0.getOperand(0), N1.getOperand(0));
1583 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001584 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001585 }
1586
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001587 // For each of OP in SHL/SRL/SRA/AND...
1588 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1589 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1590 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001591 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001592 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001593 N0.getOperand(1) == N1.getOperand(1)) {
1594 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1595 N0.getOperand(0).getValueType(),
1596 N0.getOperand(0), N1.getOperand(0));
1597 AddToWorkList(ORNode.Val);
1598 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1599 }
1600
1601 return SDOperand();
1602}
1603
Nate Begeman83e75ec2005-09-06 04:43:02 +00001604SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001605 SDOperand N0 = N->getOperand(0);
1606 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001607 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001608 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1609 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001610 MVT VT = N1.getValueType();
1611 unsigned BitWidth = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001612
Dan Gohman7f321562007-06-25 16:23:39 +00001613 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001614 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001615 SDOperand FoldedVOp = SimplifyVBinOp(N);
1616 if (FoldedVOp.Val) return FoldedVOp;
1617 }
Dan Gohman7f321562007-06-25 16:23:39 +00001618
Dan Gohman613e0d82007-07-03 14:03:57 +00001619 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001620 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001621 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001622 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001623 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001624 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001625 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001626 if (N0C && !N1C)
1627 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001628 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001629 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001630 return N0;
1631 // if (and x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001632 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
1633 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001634 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001635 // reassociate and
1636 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1637 if (RAND.Val != 0)
1638 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001639 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001640 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001641 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001642 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001643 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001644 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1645 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001646 SDOperand N0Op0 = N0.getOperand(0);
1647 APInt Mask = ~N1C->getAPIntValue();
1648 Mask.trunc(N0Op0.getValueSizeInBits());
1649 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001650 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001651 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001652
1653 // Replace uses of the AND with uses of the Zero extend node.
1654 CombineTo(N, Zext);
1655
Chris Lattner3603cd62006-02-02 07:17:31 +00001656 // We actually want to replace all uses of the any_extend with the
1657 // zero_extend, to avoid duplicating things. This will later cause this
1658 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001659 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001660 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001661 }
1662 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001663 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1664 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1665 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1666 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1667
1668 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001669 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001670 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001671 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001672 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001673 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001674 return DAG.getSetCC(VT, ORNode, LR, Op1);
1675 }
1676 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1677 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1678 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001679 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001680 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1681 }
1682 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1683 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1684 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001685 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001686 return DAG.getSetCC(VT, ORNode, LR, Op1);
1687 }
1688 }
1689 // canonicalize equivalent to ll == rl
1690 if (LL == RR && LR == RL) {
1691 Op1 = ISD::getSetCCSwappedOperands(Op1);
1692 std::swap(RL, RR);
1693 }
1694 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001695 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001696 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1697 if (Result != ISD::SETCC_INVALID)
1698 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1699 }
1700 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001701
1702 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1703 if (N0.getOpcode() == N1.getOpcode()) {
1704 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1705 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001706 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001707
Nate Begemande996292006-02-03 22:24:05 +00001708 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1709 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001710 if (!VT.isVector() &&
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001711 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001712 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001713 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001714 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001715 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001716 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001717 // If we zero all the possible extended bits, then we can turn this into
1718 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001719 unsigned BitWidth = N1.getValueSizeInBits();
1720 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001721 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001722 ((!AfterLegalize && !LN0->isVolatile()) ||
1723 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001724 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1725 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001726 LN0->getSrcValueOffset(), EVT,
1727 LN0->isVolatile(),
1728 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001729 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001730 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001731 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001732 }
1733 }
1734 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001735 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1736 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001737 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001738 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001739 // If we zero all the possible extended bits, then we can turn this into
1740 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001741 unsigned BitWidth = N1.getValueSizeInBits();
1742 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001743 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001744 ((!AfterLegalize && !LN0->isVolatile()) ||
1745 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001746 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1747 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001748 LN0->getSrcValueOffset(), EVT,
1749 LN0->isVolatile(),
1750 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001751 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001752 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001753 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001754 }
1755 }
Chris Lattner15045b62006-02-28 06:35:35 +00001756
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001757 // fold (and (load x), 255) -> (zextload x, i8)
1758 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001759 if (N1C && N0.getOpcode() == ISD::LOAD) {
1760 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1761 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001762 LN0->isUnindexed() && N0.hasOneUse() &&
1763 // Do not change the width of a volatile load.
1764 !LN0->isVolatile()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00001765 MVT EVT = MVT::Other;
1766 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
1767 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue()))
1768 EVT = MVT::getIntegerVT(ActiveBits);
1769
1770 MVT LoadedVT = LN0->getMemoryVT();
Duncan Sandsad205a72008-06-16 08:14:38 +00001771 // Do not generate loads of non-round integer types since these can
1772 // be expensive (and would be wrong if the type is not byte sized).
1773 if (EVT != MVT::Other && LoadedVT.bitsGT(EVT) && EVT.isRound() &&
Evan Cheng466685d2006-10-09 20:57:25 +00001774 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001775 MVT PtrType = N0.getOperand(1).getValueType();
Evan Cheng466685d2006-10-09 20:57:25 +00001776 // For big endian targets, we need to add an offset to the pointer to
1777 // load the correct bytes. For little endian systems, we merely need to
1778 // read fewer bytes from the same pointer.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001779 unsigned LVTStoreBytes = LoadedVT.getStoreSizeInBits()/8;
1780 unsigned EVTStoreBytes = EVT.getStoreSizeInBits()/8;
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001781 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001782 unsigned Alignment = LN0->getAlignment();
Evan Cheng466685d2006-10-09 20:57:25 +00001783 SDOperand NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001784 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001785 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1786 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001787 Alignment = MinAlign(Alignment, PtrOff);
1788 }
Evan Cheng466685d2006-10-09 20:57:25 +00001789 AddToWorkList(NewPtr.Val);
1790 SDOperand Load =
1791 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001792 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001793 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001794 AddToWorkList(N);
1795 CombineTo(N0.Val, Load, Load.getValue(1));
1796 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1797 }
Chris Lattner15045b62006-02-28 06:35:35 +00001798 }
1799 }
1800
Nate Begeman83e75ec2005-09-06 04:43:02 +00001801 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001802}
1803
Nate Begeman83e75ec2005-09-06 04:43:02 +00001804SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001805 SDOperand N0 = N->getOperand(0);
1806 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001807 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001808 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1809 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001810 MVT VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001811
Dan Gohman7f321562007-06-25 16:23:39 +00001812 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001813 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001814 SDOperand FoldedVOp = SimplifyVBinOp(N);
1815 if (FoldedVOp.Val) return FoldedVOp;
1816 }
Dan Gohman7f321562007-06-25 16:23:39 +00001817
Dan Gohman613e0d82007-07-03 14:03:57 +00001818 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001819 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001820 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001821 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001822 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001823 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001824 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001825 if (N0C && !N1C)
1826 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001827 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001828 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001829 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001830 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001831 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001832 return N1;
1833 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001834 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001835 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001836 // reassociate or
1837 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1838 if (ROR.Val != 0)
1839 return ROR;
1840 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1841 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001842 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001843 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1844 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1845 N1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001846 DAG.getConstant(N1C->getAPIntValue() |
1847 C1->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001848 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001849 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1850 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1851 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1852 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1853
1854 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001855 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001856 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1857 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001858 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001859 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1860 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001861 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001862 return DAG.getSetCC(VT, ORNode, LR, Op1);
1863 }
1864 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1865 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1866 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1867 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1868 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001869 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001870 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1871 }
1872 }
1873 // canonicalize equivalent to ll == rl
1874 if (LL == RR && LR == RL) {
1875 Op1 = ISD::getSetCCSwappedOperands(Op1);
1876 std::swap(RL, RR);
1877 }
1878 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001879 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001880 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1881 if (Result != ISD::SETCC_INVALID)
1882 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1883 }
1884 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001885
1886 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1887 if (N0.getOpcode() == N1.getOpcode()) {
1888 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1889 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001890 }
Chris Lattner516b9622006-09-14 20:50:57 +00001891
Chris Lattner1ec72732006-09-14 21:11:37 +00001892 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1893 if (N0.getOpcode() == ISD::AND &&
1894 N1.getOpcode() == ISD::AND &&
1895 N0.getOperand(1).getOpcode() == ISD::Constant &&
1896 N1.getOperand(1).getOpcode() == ISD::Constant &&
1897 // Don't increase # computations.
1898 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1899 // We can only do this xform if we know that bits from X that are set in C2
1900 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001901 const APInt &LHSMask =
1902 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1903 const APInt &RHSMask =
1904 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00001905
Dan Gohmanea859be2007-06-22 14:59:07 +00001906 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1907 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001908 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1909 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1910 }
1911 }
1912
1913
Chris Lattner516b9622006-09-14 20:50:57 +00001914 // See if this is some rotate idiom.
1915 if (SDNode *Rot = MatchRotate(N0, N1))
1916 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001917
Nate Begeman83e75ec2005-09-06 04:43:02 +00001918 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001919}
1920
Chris Lattner516b9622006-09-14 20:50:57 +00001921
1922/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1923static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1924 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001925 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001926 Mask = Op.getOperand(1);
1927 Op = Op.getOperand(0);
1928 } else {
1929 return false;
1930 }
1931 }
1932
1933 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1934 Shift = Op;
1935 return true;
1936 }
1937 return false;
1938}
1939
1940
1941// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1942// idioms for rotate, and if the target supports rotation instructions, generate
1943// a rot[lr].
1944SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001945 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001946 MVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00001947 if (!TLI.isTypeLegal(VT)) return 0;
1948
1949 // The target must have at least one rotate flavor.
1950 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1951 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1952 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001953
Chris Lattner516b9622006-09-14 20:50:57 +00001954 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1955 SDOperand LHSShift; // The shift.
1956 SDOperand LHSMask; // AND value if any.
1957 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1958 return 0; // Not part of a rotate.
1959
1960 SDOperand RHSShift; // The shift.
1961 SDOperand RHSMask; // AND value if any.
1962 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1963 return 0; // Not part of a rotate.
1964
1965 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1966 return 0; // Not shifting the same value.
1967
1968 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1969 return 0; // Shifts must disagree.
1970
1971 // Canonicalize shl to left side in a shl/srl pair.
1972 if (RHSShift.getOpcode() == ISD::SHL) {
1973 std::swap(LHS, RHS);
1974 std::swap(LHSShift, RHSShift);
1975 std::swap(LHSMask , RHSMask );
1976 }
1977
Duncan Sands83ec4b62008-06-06 12:08:01 +00001978 unsigned OpSizeInBits = VT.getSizeInBits();
Scott Michelc9dc1142007-04-02 21:36:32 +00001979 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1980 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1981 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001982
1983 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1984 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001985 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1986 RHSShiftAmt.getOpcode() == ISD::Constant) {
1987 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
1988 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00001989 if ((LShVal + RShVal) != OpSizeInBits)
1990 return 0;
1991
1992 SDOperand Rot;
1993 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001994 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001995 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001996 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001997
1998 // If there is an AND of either shifted operand, apply it to the result.
1999 if (LHSMask.Val || RHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002000 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Chris Lattner516b9622006-09-14 20:50:57 +00002001
2002 if (LHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002003 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2004 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002005 }
2006 if (RHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002007 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2008 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002009 }
2010
2011 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2012 }
2013
2014 return Rot.Val;
2015 }
2016
2017 // If there is a mask here, and we have a variable shift, we can't be sure
2018 // that we're masking out the right stuff.
2019 if (LHSMask.Val || RHSMask.Val)
2020 return 0;
2021
2022 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2023 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002024 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2025 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002026 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002027 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002028 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002029 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002030 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002031 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002032 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002033 }
Chris Lattner516b9622006-09-14 20:50:57 +00002034 }
2035 }
2036
2037 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2038 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002039 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2040 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002041 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002042 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002043 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002044 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002045 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002046 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002047 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002048 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002049 }
2050 }
2051
2052 // Look for sign/zext/any-extended cases:
2053 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2054 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2055 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
2056 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2057 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2058 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
2059 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
2060 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
2061 if (RExtOp0.getOpcode() == ISD::SUB &&
2062 RExtOp0.getOperand(1) == LExtOp0) {
2063 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2064 // (rotr x, y)
2065 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2066 // (rotl x, (sub 32, y))
2067 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002068 if (SUBC->getAPIntValue() == OpSizeInBits) {
Scott Michelc9dc1142007-04-02 21:36:32 +00002069 if (HasROTL)
2070 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2071 else
2072 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
2073 }
2074 }
2075 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2076 RExtOp0 == LExtOp0.getOperand(1)) {
2077 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2078 // (rotl x, y)
2079 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2080 // (rotr x, (sub 32, y))
2081 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002082 if (SUBC->getAPIntValue() == OpSizeInBits) {
Scott Michelc9dc1142007-04-02 21:36:32 +00002083 if (HasROTL)
2084 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
2085 else
2086 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2087 }
2088 }
Chris Lattner516b9622006-09-14 20:50:57 +00002089 }
2090 }
2091
2092 return 0;
2093}
2094
2095
Nate Begeman83e75ec2005-09-06 04:43:02 +00002096SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002097 SDOperand N0 = N->getOperand(0);
2098 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002099 SDOperand LHS, RHS, CC;
2100 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2101 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002102 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002103
Dan Gohman7f321562007-06-25 16:23:39 +00002104 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002105 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00002106 SDOperand FoldedVOp = SimplifyVBinOp(N);
2107 if (FoldedVOp.Val) return FoldedVOp;
2108 }
Dan Gohman7f321562007-06-25 16:23:39 +00002109
Evan Cheng26471c42008-03-25 20:08:07 +00002110 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2111 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2112 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002113 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002114 if (N0.getOpcode() == ISD::UNDEF)
2115 return N0;
2116 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002117 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002118 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002119 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002120 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00002121 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002122 if (N0C && !N1C)
2123 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002124 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002125 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002126 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002127 // reassociate xor
2128 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
2129 if (RXOR.Val != 0)
2130 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002131 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002132 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002133 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00002134 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2135 isInt);
2136 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002137 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002138 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002139 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002140 assert(0 && "Unhandled SetCC Equivalent!");
2141 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002142 }
Chris Lattner61c5ff42007-09-10 21:39:07 +00002143 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002144 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Chris Lattner61c5ff42007-09-10 21:39:07 +00002145 N0.Val->hasOneUse() && isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
2146 SDOperand V = N0.getOperand(0);
2147 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002148 DAG.getConstant(1, V.getValueType()));
Chris Lattner61c5ff42007-09-10 21:39:07 +00002149 AddToWorkList(V.Val);
2150 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2151 }
2152
Nate Begeman99801192005-09-07 23:25:52 +00002153 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Dan Gohman002e5d02008-03-13 22:13:53 +00002154 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002155 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002156 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002157 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2158 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002159 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2160 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002161 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002162 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002163 }
2164 }
Nate Begeman99801192005-09-07 23:25:52 +00002165 // fold !(x or y) -> (!x and !y) iff x or y are constants
2166 if (N1C && N1C->isAllOnesValue() &&
2167 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002168 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002169 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2170 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002171 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2172 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002173 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002174 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002175 }
2176 }
Nate Begeman223df222005-09-08 20:18:10 +00002177 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2178 if (N1C && N0.getOpcode() == ISD::XOR) {
2179 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2180 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2181 if (N00C)
2182 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00002183 DAG.getConstant(N1C->getAPIntValue()^
2184 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002185 if (N01C)
2186 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
Dan Gohman002e5d02008-03-13 22:13:53 +00002187 DAG.getConstant(N1C->getAPIntValue()^
2188 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002189 }
2190 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002191 if (N0 == N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002192 if (!VT.isVector()) {
Chris Lattner4fbdd592006-03-28 19:11:05 +00002193 return DAG.getConstant(0, VT);
2194 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2195 // Produce a vector of zeros.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002196 SDOperand El = DAG.getConstant(0, VT.getVectorElementType());
2197 std::vector<SDOperand> Ops(VT.getVectorNumElements(), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002198 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002199 }
2200 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002201
2202 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2203 if (N0.getOpcode() == N1.getOpcode()) {
2204 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
2205 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002206 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002207
Chris Lattner3e104b12006-04-08 04:15:24 +00002208 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002209 if (!VT.isVector() &&
Chris Lattner3e104b12006-04-08 04:15:24 +00002210 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002211 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002212
Nate Begeman83e75ec2005-09-06 04:43:02 +00002213 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002214}
2215
Chris Lattnere70da202007-12-06 07:33:36 +00002216/// visitShiftByConstant - Handle transforms common to the three shifts, when
2217/// the shift amount is a constant.
2218SDOperand DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
2219 SDNode *LHS = N->getOperand(0).Val;
2220 if (!LHS->hasOneUse()) return SDOperand();
2221
2222 // We want to pull some binops through shifts, so that we have (and (shift))
2223 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2224 // thing happens with address calculations, so it's important to canonicalize
2225 // it.
2226 bool HighBitSet = false; // Can we transform this if the high bit is set?
2227
2228 switch (LHS->getOpcode()) {
2229 default: return SDOperand();
2230 case ISD::OR:
2231 case ISD::XOR:
2232 HighBitSet = false; // We can only transform sra if the high bit is clear.
2233 break;
2234 case ISD::AND:
2235 HighBitSet = true; // We can only transform sra if the high bit is set.
2236 break;
2237 case ISD::ADD:
2238 if (N->getOpcode() != ISD::SHL)
2239 return SDOperand(); // only shl(add) not sr[al](add).
2240 HighBitSet = false; // We can only transform sra if the high bit is clear.
2241 break;
2242 }
2243
2244 // We require the RHS of the binop to be a constant as well.
2245 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
2246 if (!BinOpCst) return SDOperand();
2247
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002248
2249 // FIXME: disable this for unless the input to the binop is a shift by a
2250 // constant. If it is not a shift, it pessimizes some common cases like:
2251 //
2252 //void foo(int *X, int i) { X[i & 1235] = 1; }
2253 //int bar(int *X, int i) { return X[i & 255]; }
2254 SDNode *BinOpLHSVal = LHS->getOperand(0).Val;
2255 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2256 BinOpLHSVal->getOpcode() != ISD::SRA &&
2257 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2258 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
2259 return SDOperand();
2260
Duncan Sands83ec4b62008-06-06 12:08:01 +00002261 MVT VT = N->getValueType(0);
Chris Lattnere70da202007-12-06 07:33:36 +00002262
2263 // If this is a signed shift right, and the high bit is modified
2264 // by the logical operation, do not perform the transformation.
2265 // The highBitSet boolean indicates the value of the high bit of
2266 // the constant which would cause it to be modified for this
2267 // operation.
2268 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002269 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2270 if (BinOpRHSSignSet != HighBitSet)
Chris Lattnere70da202007-12-06 07:33:36 +00002271 return SDOperand();
2272 }
2273
2274 // Fold the constants, shifting the binop RHS by the shift amount.
2275 SDOperand NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
2276 LHS->getOperand(1), N->getOperand(1));
2277
2278 // Create the new shift.
2279 SDOperand NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
2280 N->getOperand(1));
2281
2282 // Create the new binop.
2283 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2284}
2285
2286
Nate Begeman83e75ec2005-09-06 04:43:02 +00002287SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002288 SDOperand N0 = N->getOperand(0);
2289 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002290 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2291 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002292 MVT VT = N0.getValueType();
2293 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002294
2295 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002296 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002297 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002298 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002299 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002300 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002301 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002302 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002303 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002304 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002305 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002306 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002307 // if (shl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002308 if (DAG.MaskedValueIsZero(SDOperand(N, 0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002309 APInt::getAllOnesValue(VT.getSizeInBits())))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002310 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00002311 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002312 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002313 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002314 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002315 N0.getOperand(1).getOpcode() == ISD::Constant) {
2316 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002317 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002318 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002319 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002320 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002321 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002322 }
2323 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2324 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002325 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002326 N0.getOperand(1).getOpcode() == ISD::Constant) {
2327 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002328 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002329 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
2330 DAG.getConstant(~0ULL << c1, VT));
2331 if (c2 > c1)
2332 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002333 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002334 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002335 return DAG.getNode(ISD::SRL, VT, Mask,
2336 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002337 }
2338 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002339 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002340 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002341 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002342
2343 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002344}
2345
Nate Begeman83e75ec2005-09-06 04:43:02 +00002346SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002347 SDOperand N0 = N->getOperand(0);
2348 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002349 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2350 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002351 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002352
2353 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002354 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002355 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002356 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002357 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002358 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002359 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002360 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002361 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002362 // fold (sra x, c >= size(x)) -> undef
Duncan Sands83ec4b62008-06-06 12:08:01 +00002363 if (N1C && N1C->getValue() >= VT.getSizeInBits())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002364 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002365 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002366 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002367 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002368 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2369 // sext_inreg.
2370 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002371 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getValue();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002372 MVT EVT = MVT::getIntegerVT(LowBits);
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002373 if (EVT.isSimple() && // TODO: remove when apint codegen support lands.
2374 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Nate Begemanfb7217b2006-02-17 19:54:08 +00002375 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2376 DAG.getValueType(EVT));
2377 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002378
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002379 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2380 if (N1C && N0.getOpcode() == ISD::SRA) {
2381 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2382 unsigned Sum = N1C->getValue() + C1->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002383 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002384 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2385 DAG.getConstant(Sum, N1C->getValueType(0)));
2386 }
2387 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002388
2389 // fold sra (shl X, m), result_size - n
2390 // -> (sign_extend (trunc (shl X, result_size - n - m))) for
Christopher Lambb9b04282008-03-20 04:31:39 +00002391 // result_size - n != m.
2392 // If truncate is free for the target sext(shl) is likely to result in better
2393 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002394 if (N0.getOpcode() == ISD::SHL) {
2395 // Get the two constanst of the shifts, CN0 = m, CN = n.
2396 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2397 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002398 // Determine what the truncate's result bitsize and type would be.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002399 unsigned VTValSize = VT.getSizeInBits();
2400 MVT TruncVT =
2401 MVT::getIntegerVT(VTValSize - N1C->getValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002402 // Determine the residual right-shift amount.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002403 unsigned ShiftAmt = N1C->getValue() - N01C->getValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002404
Christopher Lambb9b04282008-03-20 04:31:39 +00002405 // If the shift is not a no-op (in which case this should be just a sign
2406 // extend already), the truncated to type is legal, sign_extend is legal
2407 // on that type, and the the truncate to that type is both legal and free,
2408 // perform the transform.
2409 if (ShiftAmt &&
Christopher Lambb9b04282008-03-20 04:31:39 +00002410 TLI.isOperationLegal(ISD::SIGN_EXTEND, TruncVT) &&
2411 TLI.isOperationLegal(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002412 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002413
2414 SDOperand Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
2415 SDOperand Shift = DAG.getNode(ISD::SRL, VT, N0.getOperand(0), Amt);
2416 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, TruncVT, Shift);
2417 return DAG.getNode(ISD::SIGN_EXTEND, N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002418 }
2419 }
2420 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002421
Chris Lattnera8504462006-05-08 20:51:54 +00002422 // Simplify, based on bits shifted out of the LHS.
2423 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2424 return SDOperand(N, 0);
2425
2426
Nate Begeman1d4d4142005-09-01 00:19:25 +00002427 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002428 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002429 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002430
2431 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002432}
2433
Nate Begeman83e75ec2005-09-06 04:43:02 +00002434SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002435 SDOperand N0 = N->getOperand(0);
2436 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002437 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2438 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002439 MVT VT = N0.getValueType();
2440 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002441
2442 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002443 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002444 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002445 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002446 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002447 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002448 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002449 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002450 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002451 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002452 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002453 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002454 // if (srl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002455 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
2456 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002457 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002458
Nate Begeman1d4d4142005-09-01 00:19:25 +00002459 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002460 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002461 N0.getOperand(1).getOpcode() == ISD::Constant) {
2462 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002463 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002464 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002465 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002466 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002467 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002468 }
Chris Lattner350bec02006-04-02 06:11:11 +00002469
Chris Lattner06afe072006-05-05 22:53:17 +00002470 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2471 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2472 // Shifting in all undef bits?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002473 MVT SmallVT = N0.getOperand(0).getValueType();
2474 if (N1C->getValue() >= SmallVT.getSizeInBits())
Chris Lattner06afe072006-05-05 22:53:17 +00002475 return DAG.getNode(ISD::UNDEF, VT);
2476
2477 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
2478 AddToWorkList(SmallShift.Val);
2479 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2480 }
2481
Chris Lattner3657ffe2006-10-12 20:23:19 +00002482 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2483 // bit, which is unmodified by sra.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002484 if (N1C && N1C->getValue()+1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00002485 if (N0.getOpcode() == ISD::SRA)
2486 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2487 }
2488
Chris Lattner350bec02006-04-02 06:11:11 +00002489 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2490 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002491 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002492 APInt KnownZero, KnownOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002493 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00002494 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002495
2496 // If any of the input bits are KnownOne, then the input couldn't be all
2497 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002498 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002499
2500 // If all of the bits input the to ctlz node are known to be zero, then
2501 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002502 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002503 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2504
2505 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2506 if ((UnknownBits & (UnknownBits-1)) == 0) {
2507 // Okay, we know that only that the single bit specified by UnknownBits
2508 // could be set on input to the CTLZ node. If this bit is set, the SRL
2509 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2510 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002511 unsigned ShAmt = UnknownBits.countTrailingZeros();
Chris Lattner350bec02006-04-02 06:11:11 +00002512 SDOperand Op = N0.getOperand(0);
2513 if (ShAmt) {
2514 Op = DAG.getNode(ISD::SRL, VT, Op,
2515 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2516 AddToWorkList(Op.Val);
2517 }
2518 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2519 }
2520 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002521
2522 // fold operands of srl based on knowledge that the low bits are not
2523 // demanded.
2524 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2525 return SDOperand(N, 0);
2526
Chris Lattnere70da202007-12-06 07:33:36 +00002527 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002528}
2529
Nate Begeman83e75ec2005-09-06 04:43:02 +00002530SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002531 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002532 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002533
2534 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002535 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002536 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002537 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002538}
2539
Nate Begeman83e75ec2005-09-06 04:43:02 +00002540SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002541 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002542 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002543
2544 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002545 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002546 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002547 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002548}
2549
Nate Begeman83e75ec2005-09-06 04:43:02 +00002550SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002551 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002552 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002553
2554 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002555 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002556 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002557 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002558}
2559
Nate Begeman452d7be2005-09-16 00:54:12 +00002560SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2561 SDOperand N0 = N->getOperand(0);
2562 SDOperand N1 = N->getOperand(1);
2563 SDOperand N2 = N->getOperand(2);
2564 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2565 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2566 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002567 MVT VT = N->getValueType(0);
2568 MVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002569
Nate Begeman452d7be2005-09-16 00:54:12 +00002570 // fold select C, X, X -> X
2571 if (N1 == N2)
2572 return N1;
2573 // fold select true, X, Y -> X
2574 if (N0C && !N0C->isNullValue())
2575 return N1;
2576 // fold select false, X, Y -> Y
2577 if (N0C && N0C->isNullValue())
2578 return N2;
2579 // fold select C, 1, X -> C | X
Duncan Sands83ec4b62008-06-06 12:08:01 +00002580 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002581 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002582 // fold select C, 0, 1 -> ~C
Duncan Sands83ec4b62008-06-06 12:08:01 +00002583 if (VT.isInteger() && VT0.isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002584 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Evan Cheng571c4782007-08-18 05:57:05 +00002585 SDOperand XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
2586 if (VT == VT0)
2587 return XORNode;
2588 AddToWorkList(XORNode.Val);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002589 if (VT.bitsGT(VT0))
Evan Cheng571c4782007-08-18 05:57:05 +00002590 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2591 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2592 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002593 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002594 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
2595 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002596 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002597 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2598 }
2599 // fold select C, X, 1 -> ~C | X
Dan Gohman002e5d02008-03-13 22:13:53 +00002600 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002601 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002602 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002603 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2604 }
2605 // fold select C, X, 0 -> C & X
2606 // FIXME: this should check for C type == X type, not i1?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002607 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Nate Begeman452d7be2005-09-16 00:54:12 +00002608 return DAG.getNode(ISD::AND, VT, N0, N1);
2609 // fold X ? X : Y --> X ? 1 : Y --> X | Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002610 if (VT == MVT::i1 && N0 == N1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002611 return DAG.getNode(ISD::OR, VT, N0, N2);
2612 // fold X ? Y : X --> X ? Y : 0 --> X & Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002613 if (VT == MVT::i1 && N0 == N2)
Nate Begeman452d7be2005-09-16 00:54:12 +00002614 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002615
Chris Lattner40c62d52005-10-18 06:04:22 +00002616 // If we can fold this based on the true/false value, do so.
2617 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002618 return SDOperand(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002619
Nate Begeman44728a72005-09-19 22:34:01 +00002620 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002621 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002622 // FIXME:
2623 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2624 // having to say they don't support SELECT_CC on every type the DAG knows
2625 // about, since there is no way to mark an opcode illegal at all value types
2626 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2627 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2628 N1, N2, N0.getOperand(2));
2629 else
2630 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002631 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002632 return SDOperand();
2633}
2634
2635SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002636 SDOperand N0 = N->getOperand(0);
2637 SDOperand N1 = N->getOperand(1);
2638 SDOperand N2 = N->getOperand(2);
2639 SDOperand N3 = N->getOperand(3);
2640 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002641 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2642
Nate Begeman44728a72005-09-19 22:34:01 +00002643 // fold select_cc lhs, rhs, x, x, cc -> x
2644 if (N2 == N3)
2645 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002646
Chris Lattner5f42a242006-09-20 06:19:26 +00002647 // Determine if the condition we're dealing with is constant
Scott Michel5b8f82e2008-03-10 15:42:14 +00002648 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002649 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002650
2651 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002652 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00002653 return N2; // cond always true -> true val
2654 else
2655 return N3; // cond always false -> false val
2656 }
2657
2658 // Fold to a simpler select_cc
2659 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2660 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2661 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2662 SCC.getOperand(2));
2663
Chris Lattner40c62d52005-10-18 06:04:22 +00002664 // If we can fold this based on the true/false value, do so.
2665 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002666 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002667
Nate Begeman44728a72005-09-19 22:34:01 +00002668 // fold select_cc into other things, such as min/max/abs
2669 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002670}
2671
2672SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2673 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2674 cast<CondCodeSDNode>(N->getOperand(2))->get());
2675}
2676
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002677// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2678// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2679// transformation. Returns true if extension are possible and the above
2680// mentioned transformation is profitable.
2681static bool ExtendUsesToFormExtLoad(SDNode *N, SDOperand N0,
2682 unsigned ExtOpc,
2683 SmallVector<SDNode*, 4> &ExtendNodes,
2684 TargetLowering &TLI) {
2685 bool HasCopyToRegUses = false;
2686 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
2687 for (SDNode::use_iterator UI = N0.Val->use_begin(), UE = N0.Val->use_end();
2688 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002689 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002690 if (User == N)
2691 continue;
2692 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2693 if (User->getOpcode() == ISD::SETCC) {
2694 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2695 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2696 // Sign bits will be lost after a zext.
2697 return false;
2698 bool Add = false;
2699 for (unsigned i = 0; i != 2; ++i) {
2700 SDOperand UseOp = User->getOperand(i);
2701 if (UseOp == N0)
2702 continue;
2703 if (!isa<ConstantSDNode>(UseOp))
2704 return false;
2705 Add = true;
2706 }
2707 if (Add)
2708 ExtendNodes.push_back(User);
2709 } else {
2710 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2711 SDOperand UseOp = User->getOperand(i);
2712 if (UseOp == N0) {
2713 // If truncate from extended type to original load type is free
2714 // on this target, then it's ok to extend a CopyToReg.
2715 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2716 HasCopyToRegUses = true;
2717 else
2718 return false;
2719 }
2720 }
2721 }
2722 }
2723
2724 if (HasCopyToRegUses) {
2725 bool BothLiveOut = false;
2726 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2727 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002728 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002729 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2730 SDOperand UseOp = User->getOperand(i);
2731 if (UseOp.Val == N && UseOp.ResNo == 0) {
2732 BothLiveOut = true;
2733 break;
2734 }
2735 }
2736 }
2737 if (BothLiveOut)
2738 // Both unextended and extended values are live out. There had better be
2739 // good a reason for the transformation.
2740 return ExtendNodes.size();
2741 }
2742 return true;
2743}
2744
Nate Begeman83e75ec2005-09-06 04:43:02 +00002745SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002746 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002747 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002748
Nate Begeman1d4d4142005-09-01 00:19:25 +00002749 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002750 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002751 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002752
Nate Begeman1d4d4142005-09-01 00:19:25 +00002753 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002754 // fold (sext (aext x)) -> (sext x)
2755 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002756 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002757
Chris Lattner22558872007-02-26 03:13:59 +00002758 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002759 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2760 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Evan Chengc88138f2007-03-22 01:54:19 +00002761 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002762 if (NarrowLoad.Val) {
2763 if (NarrowLoad.Val != N0.Val)
2764 CombineTo(N0.Val, NarrowLoad);
2765 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2766 }
Evan Chengc88138f2007-03-22 01:54:19 +00002767
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002768 // See if the value being truncated is already sign extended. If so, just
2769 // eliminate the trunc/sext pair.
Chris Lattner6007b842006-09-21 06:00:20 +00002770 SDOperand Op = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002771 unsigned OpBits = Op.getValueType().getSizeInBits();
2772 unsigned MidBits = N0.getValueType().getSizeInBits();
2773 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002774 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002775
2776 if (OpBits == DestBits) {
2777 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2778 // bits, it is already ready.
2779 if (NumSignBits > DestBits-MidBits)
2780 return Op;
2781 } else if (OpBits < DestBits) {
2782 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2783 // bits, just sext from i32.
2784 if (NumSignBits > OpBits-MidBits)
2785 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2786 } else {
2787 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2788 // bits, just truncate to i32.
2789 if (NumSignBits > OpBits-MidBits)
2790 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002791 }
Chris Lattner22558872007-02-26 03:13:59 +00002792
2793 // fold (sext (truncate x)) -> (sextinreg x).
2794 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2795 N0.getValueType())) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00002796 if (Op.getValueType().bitsLT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002797 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002798 else if (Op.getValueType().bitsGT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002799 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2800 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2801 DAG.getValueType(N0.getValueType()));
2802 }
Chris Lattner6007b842006-09-21 06:00:20 +00002803 }
Chris Lattner310b5782006-05-06 23:06:26 +00002804
Evan Cheng110dec22005-12-14 02:19:23 +00002805 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002806 if (ISD::isNON_EXTLoad(N0.Val) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002807 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
2808 TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002809 bool DoXform = true;
2810 SmallVector<SDNode*, 4> SetCCs;
2811 if (!N0.hasOneUse())
2812 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2813 if (DoXform) {
2814 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2815 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2816 LN0->getBasePtr(), LN0->getSrcValue(),
2817 LN0->getSrcValueOffset(),
2818 N0.getValueType(),
2819 LN0->isVolatile(),
2820 LN0->getAlignment());
2821 CombineTo(N, ExtLoad);
2822 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2823 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2824 // Extend SetCC uses if necessary.
2825 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2826 SDNode *SetCC = SetCCs[i];
2827 SmallVector<SDOperand, 4> Ops;
2828 for (unsigned j = 0; j != 2; ++j) {
2829 SDOperand SOp = SetCC->getOperand(j);
2830 if (SOp == Trunc)
2831 Ops.push_back(ExtLoad);
2832 else
2833 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2834 }
2835 Ops.push_back(SetCC->getOperand(2));
2836 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2837 &Ops[0], Ops.size()));
2838 }
2839 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2840 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002841 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002842
2843 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2844 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002845 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2846 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002847 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002848 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002849 if ((!AfterLegalize && !LN0->isVolatile()) ||
2850 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002851 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2852 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002853 LN0->getSrcValueOffset(), EVT,
2854 LN0->isVolatile(),
2855 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002856 CombineTo(N, ExtLoad);
2857 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2858 ExtLoad.getValue(1));
2859 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2860 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002861 }
2862
Chris Lattner20a35c32007-04-11 05:32:27 +00002863 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2864 if (N0.getOpcode() == ISD::SETCC) {
2865 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002866 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2867 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2868 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2869 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002870 }
2871
Dan Gohman8f0ad582008-04-28 16:58:24 +00002872 // fold (sext x) -> (zext x) if the sign bit is known zero.
Dan Gohman187db7b2008-04-28 18:47:17 +00002873 if ((!AfterLegalize || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
2874 DAG.SignBitIsZero(N0))
Dan Gohman8f0ad582008-04-28 16:58:24 +00002875 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2876
Nate Begeman83e75ec2005-09-06 04:43:02 +00002877 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002878}
2879
Nate Begeman83e75ec2005-09-06 04:43:02 +00002880SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002881 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002882 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002883
Nate Begeman1d4d4142005-09-01 00:19:25 +00002884 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002885 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002886 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002887 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002888 // fold (zext (aext x)) -> (zext x)
2889 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002890 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002891
Evan Chengc88138f2007-03-22 01:54:19 +00002892 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2893 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002894 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002895 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002896 if (NarrowLoad.Val) {
2897 if (NarrowLoad.Val != N0.Val)
2898 CombineTo(N0.Val, NarrowLoad);
2899 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2900 }
Evan Chengc88138f2007-03-22 01:54:19 +00002901 }
2902
Chris Lattner6007b842006-09-21 06:00:20 +00002903 // fold (zext (truncate x)) -> (and x, mask)
2904 if (N0.getOpcode() == ISD::TRUNCATE &&
2905 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2906 SDOperand Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002907 if (Op.getValueType().bitsLT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00002908 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002909 } else if (Op.getValueType().bitsGT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00002910 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2911 }
2912 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2913 }
2914
Chris Lattner111c2282006-09-21 06:14:31 +00002915 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2916 if (N0.getOpcode() == ISD::AND &&
2917 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2918 N0.getOperand(1).getOpcode() == ISD::Constant) {
2919 SDOperand X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002920 if (X.getValueType().bitsLT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00002921 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002922 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00002923 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2924 }
Dan Gohman220a8232008-03-03 23:51:38 +00002925 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002926 Mask.zext(VT.getSizeInBits());
Chris Lattner111c2282006-09-21 06:14:31 +00002927 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2928 }
2929
Evan Cheng110dec22005-12-14 02:19:23 +00002930 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002931 if (ISD::isNON_EXTLoad(N0.Val) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002932 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
2933 TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002934 bool DoXform = true;
2935 SmallVector<SDNode*, 4> SetCCs;
2936 if (!N0.hasOneUse())
2937 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
2938 if (DoXform) {
2939 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2940 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2941 LN0->getBasePtr(), LN0->getSrcValue(),
2942 LN0->getSrcValueOffset(),
2943 N0.getValueType(),
2944 LN0->isVolatile(),
2945 LN0->getAlignment());
2946 CombineTo(N, ExtLoad);
2947 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2948 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2949 // Extend SetCC uses if necessary.
2950 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2951 SDNode *SetCC = SetCCs[i];
2952 SmallVector<SDOperand, 4> Ops;
2953 for (unsigned j = 0; j != 2; ++j) {
2954 SDOperand SOp = SetCC->getOperand(j);
2955 if (SOp == Trunc)
2956 Ops.push_back(ExtLoad);
2957 else
Evan Chengde1631b2007-10-30 20:11:21 +00002958 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002959 }
2960 Ops.push_back(SetCC->getOperand(2));
2961 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2962 &Ops[0], Ops.size()));
2963 }
2964 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2965 }
Evan Cheng110dec22005-12-14 02:19:23 +00002966 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002967
2968 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2969 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002970 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2971 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002972 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002973 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002974 if ((!AfterLegalize && !LN0->isVolatile()) ||
2975 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT)) {
2976 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2977 LN0->getBasePtr(), LN0->getSrcValue(),
2978 LN0->getSrcValueOffset(), EVT,
2979 LN0->isVolatile(),
2980 LN0->getAlignment());
2981 CombineTo(N, ExtLoad);
2982 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2983 ExtLoad.getValue(1));
2984 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2985 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002986 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002987
2988 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2989 if (N0.getOpcode() == ISD::SETCC) {
2990 SDOperand SCC =
2991 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2992 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00002993 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2994 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002995 }
2996
Nate Begeman83e75ec2005-09-06 04:43:02 +00002997 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002998}
2999
Chris Lattner5ffc0662006-05-05 05:58:59 +00003000SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
3001 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003002 MVT VT = N->getValueType(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003003
3004 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003005 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00003006 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
3007 // fold (aext (aext x)) -> (aext x)
3008 // fold (aext (zext x)) -> (zext x)
3009 // fold (aext (sext x)) -> (sext x)
3010 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3011 N0.getOpcode() == ISD::ZERO_EXTEND ||
3012 N0.getOpcode() == ISD::SIGN_EXTEND)
3013 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
3014
Evan Chengc88138f2007-03-22 01:54:19 +00003015 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3016 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3017 if (N0.getOpcode() == ISD::TRUNCATE) {
3018 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00003019 if (NarrowLoad.Val) {
3020 if (NarrowLoad.Val != N0.Val)
3021 CombineTo(N0.Val, NarrowLoad);
3022 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
3023 }
Evan Chengc88138f2007-03-22 01:54:19 +00003024 }
3025
Chris Lattner84750582006-09-20 06:29:17 +00003026 // fold (aext (truncate x))
3027 if (N0.getOpcode() == ISD::TRUNCATE) {
3028 SDOperand TruncOp = N0.getOperand(0);
3029 if (TruncOp.getValueType() == VT)
3030 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003031 if (TruncOp.getValueType().bitsGT(VT))
Chris Lattner84750582006-09-20 06:29:17 +00003032 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
3033 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
3034 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00003035
3036 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3037 if (N0.getOpcode() == ISD::AND &&
3038 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3039 N0.getOperand(1).getOpcode() == ISD::Constant) {
3040 SDOperand X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003041 if (X.getValueType().bitsLT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003042 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003043 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003044 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3045 }
Dan Gohman220a8232008-03-03 23:51:38 +00003046 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003047 Mask.zext(VT.getSizeInBits());
Chris Lattner0e4b9222006-09-21 06:40:43 +00003048 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3049 }
3050
Chris Lattner5ffc0662006-05-05 05:58:59 +00003051 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00003052 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003053 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3054 TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003055 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3056 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3057 LN0->getBasePtr(), LN0->getSrcValue(),
3058 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003059 N0.getValueType(),
3060 LN0->isVolatile(),
3061 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003062 CombineTo(N, ExtLoad);
3063 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3064 ExtLoad.getValue(1));
3065 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3066 }
3067
3068 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3069 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3070 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003071 if (N0.getOpcode() == ISD::LOAD &&
3072 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003073 N0.hasOneUse()) {
3074 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003075 MVT EVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00003076 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
3077 LN0->getChain(), LN0->getBasePtr(),
3078 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003079 LN0->getSrcValueOffset(), EVT,
3080 LN0->isVolatile(),
3081 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003082 CombineTo(N, ExtLoad);
3083 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3084 ExtLoad.getValue(1));
3085 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3086 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003087
3088 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3089 if (N0.getOpcode() == ISD::SETCC) {
3090 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003091 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3092 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003093 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00003094 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003095 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003096 }
3097
Chris Lattner5ffc0662006-05-05 05:58:59 +00003098 return SDOperand();
3099}
3100
Chris Lattner2b4c2792007-10-13 06:35:54 +00003101/// GetDemandedBits - See if the specified operand can be simplified with the
3102/// knowledge that only the bits specified by Mask are used. If so, return the
3103/// simpler operand, otherwise return a null SDOperand.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003104SDOperand DAGCombiner::GetDemandedBits(SDOperand V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003105 switch (V.getOpcode()) {
3106 default: break;
3107 case ISD::OR:
3108 case ISD::XOR:
3109 // If the LHS or RHS don't contribute bits to the or, drop them.
3110 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3111 return V.getOperand(1);
3112 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3113 return V.getOperand(0);
3114 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003115 case ISD::SRL:
3116 // Only look at single-use SRLs.
3117 if (!V.Val->hasOneUse())
3118 break;
3119 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3120 // See if we can recursively simplify the LHS.
3121 unsigned Amt = RHSC->getValue();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003122 APInt NewMask = Mask << Amt;
3123 SDOperand SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Chris Lattnere33544c2007-10-13 06:58:48 +00003124 if (SimplifyLHS.Val) {
3125 return DAG.getNode(ISD::SRL, V.getValueType(),
3126 SimplifyLHS, V.getOperand(1));
3127 }
3128 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003129 }
3130 return SDOperand();
3131}
3132
Evan Chengc88138f2007-03-22 01:54:19 +00003133/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3134/// bits and then truncated to a narrower type and where N is a multiple
3135/// of number of bits of the narrower type, transform it to a narrower load
3136/// from address + N / num of bits of new type. If the result is to be
3137/// extended, also fold the extension to form a extending load.
3138SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
3139 unsigned Opc = N->getOpcode();
3140 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
3141 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003142 MVT VT = N->getValueType(0);
3143 MVT EVT = N->getValueType(0);
Evan Chengc88138f2007-03-22 01:54:19 +00003144
Evan Chenge177e302007-03-23 22:13:36 +00003145 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3146 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003147 if (Opc == ISD::SIGN_EXTEND_INREG) {
3148 ExtType = ISD::SEXTLOAD;
3149 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00003150 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
3151 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00003152 }
3153
Duncan Sands83ec4b62008-06-06 12:08:01 +00003154 unsigned EVTBits = EVT.getSizeInBits();
Evan Chengc88138f2007-03-22 01:54:19 +00003155 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003156 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003157 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3158 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3159 ShAmt = N01->getValue();
3160 // Is the shift amount a multiple of size of VT?
3161 if ((ShAmt & (EVTBits-1)) == 0) {
3162 N0 = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003163 if (N0.getValueType().getSizeInBits() <= EVTBits)
Evan Chengc88138f2007-03-22 01:54:19 +00003164 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00003165 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003166 }
3167 }
3168 }
3169
Duncan Sandsad205a72008-06-16 08:14:38 +00003170 // Do not generate loads of non-round integer types since these can
3171 // be expensive (and would be wrong if the type is not byte sized).
3172 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() && VT.isRound() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003173 // Do not change the width of a volatile load.
3174 !cast<LoadSDNode>(N0)->isVolatile()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003175 assert(N0.getValueType().getSizeInBits() > EVTBits &&
Evan Chengc88138f2007-03-22 01:54:19 +00003176 "Cannot truncate to larger type!");
3177 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003178 MVT PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003179 // For big endian targets, we need to adjust the offset to the pointer to
3180 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003181 if (TLI.isBigEndian()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003182 unsigned LVTStoreBits = N0.getValueType().getStoreSizeInBits();
3183 unsigned EVTStoreBits = EVT.getStoreSizeInBits();
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003184 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3185 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003186 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003187 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Evan Chengc88138f2007-03-22 01:54:19 +00003188 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
3189 DAG.getConstant(PtrOff, PtrType));
3190 AddToWorkList(NewPtr.Val);
3191 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
3192 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003193 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Duncan Sandsdc846502007-10-28 12:59:45 +00003194 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003195 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003196 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00003197 LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003198 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003199 if (CombineSRL) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003200 WorkListRemover DeadNodes(*this);
3201 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3202 &DeadNodes);
Evan Chengb37b80c2007-03-23 20:55:21 +00003203 CombineTo(N->getOperand(0).Val, Load);
3204 } else
3205 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003206 if (ShAmt) {
3207 if (Opc == ISD::SIGN_EXTEND_INREG)
3208 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3209 else
3210 return DAG.getNode(Opc, VT, Load);
3211 }
Evan Chengc88138f2007-03-22 01:54:19 +00003212 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3213 }
3214
3215 return SDOperand();
3216}
3217
Chris Lattner5ffc0662006-05-05 05:58:59 +00003218
Nate Begeman83e75ec2005-09-06 04:43:02 +00003219SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003220 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003221 SDOperand N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003222 MVT VT = N->getValueType(0);
3223 MVT EVT = cast<VTSDNode>(N1)->getVT();
3224 unsigned VTBits = VT.getSizeInBits();
3225 unsigned EVTBits = EVT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003226
Nate Begeman1d4d4142005-09-01 00:19:25 +00003227 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003228 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003229 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003230
Chris Lattner541a24f2006-05-06 22:43:44 +00003231 // If the input is already sign extended, just drop the extension.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003232 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003233 return N0;
3234
Nate Begeman646d7e22005-09-02 21:18:40 +00003235 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3236 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003237 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003238 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003239 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003240
Chris Lattner95a5e052007-04-17 19:03:21 +00003241 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003242 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003243 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003244
Chris Lattner95a5e052007-04-17 19:03:21 +00003245 // fold operands of sext_in_reg based on knowledge that the top bits are not
3246 // demanded.
3247 if (SimplifyDemandedBits(SDOperand(N, 0)))
3248 return SDOperand(N, 0);
3249
Evan Chengc88138f2007-03-22 01:54:19 +00003250 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3251 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
3252 SDOperand NarrowLoad = ReduceLoadWidth(N);
3253 if (NarrowLoad.Val)
3254 return NarrowLoad;
3255
Chris Lattner4b37e872006-05-08 21:18:59 +00003256 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3257 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3258 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3259 if (N0.getOpcode() == ISD::SRL) {
3260 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00003261 if (ShAmt->getValue()+EVTBits <= VT.getSizeInBits()) {
Chris Lattner4b37e872006-05-08 21:18:59 +00003262 // We can turn this into an SRA iff the input to the SRL is already sign
3263 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003264 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00003265 if (VT.getSizeInBits()-(ShAmt->getValue()+EVTBits) < InSignBits)
Chris Lattner4b37e872006-05-08 21:18:59 +00003266 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3267 }
3268 }
Evan Chengc88138f2007-03-22 01:54:19 +00003269
Nate Begemanded49632005-10-13 03:11:28 +00003270 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00003271 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003272 ISD::isUNINDEXEDLoad(N0.Val) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003273 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003274 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3275 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003276 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3277 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3278 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003279 LN0->getSrcValueOffset(), EVT,
3280 LN0->isVolatile(),
3281 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003282 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003283 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003284 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003285 }
3286 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00003287 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
3288 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003289 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003290 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3291 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003292 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3293 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3294 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003295 LN0->getSrcValueOffset(), EVT,
3296 LN0->isVolatile(),
3297 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003298 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003299 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003300 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003301 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003302 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003303}
3304
Nate Begeman83e75ec2005-09-06 04:43:02 +00003305SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003306 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003307 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003308
3309 // noop truncate
3310 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003311 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003312 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003313 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003314 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003315 // fold (truncate (truncate x)) -> (truncate x)
3316 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003317 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003318 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003319 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3320 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00003321 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003322 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003323 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00003324 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003325 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003326 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003327 else
3328 // if the source and dest are the same type, we can drop both the extend
3329 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003330 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003331 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003332
Chris Lattner2b4c2792007-10-13 06:35:54 +00003333 // See if we can simplify the input to this truncate through knowledge that
3334 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3335 // -> trunc y
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003336 SDOperand Shorter =
3337 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00003338 VT.getSizeInBits()));
Chris Lattner2b4c2792007-10-13 06:35:54 +00003339 if (Shorter.Val)
3340 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3341
Nate Begeman3df4d522005-10-12 20:40:40 +00003342 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003343 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003344 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003345}
3346
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003347static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
3348 SDOperand Elt = N->getOperand(i);
3349 if (Elt.getOpcode() != ISD::MERGE_VALUES)
3350 return Elt.Val;
3351 return Elt.getOperand(Elt.ResNo).Val;
3352}
3353
3354/// CombineConsecutiveLoads - build_pair (load, load) -> load
3355/// if load locations are consecutive.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003356SDOperand DAGCombiner::CombineConsecutiveLoads(SDNode *N, MVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003357 assert(N->getOpcode() == ISD::BUILD_PAIR);
3358
3359 SDNode *LD1 = getBuildPairElt(N, 0);
3360 if (!ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
3361 return SDOperand();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003362 MVT LD1VT = LD1->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003363 SDNode *LD2 = getBuildPairElt(N, 1);
3364 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3365 if (ISD::isNON_EXTLoad(LD2) &&
3366 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003367 // If both are volatile this would reduce the number of volatile loads.
3368 // If one is volatile it might be ok, but play conservative and bail out.
3369 !cast<LoadSDNode>(LD1)->isVolatile() &&
3370 !cast<LoadSDNode>(LD2)->isVolatile() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003371 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003372 LoadSDNode *LD = cast<LoadSDNode>(LD1);
3373 unsigned Align = LD->getAlignment();
3374 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003375 getABITypeAlignment(VT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003376 if (NewAlign <= Align &&
3377 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT)))
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003378 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(),
3379 LD->getSrcValue(), LD->getSrcValueOffset(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003380 false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003381 }
3382 return SDOperand();
3383}
3384
Chris Lattner94683772005-12-23 05:30:37 +00003385SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3386 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003387 MVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00003388
Dan Gohman7f321562007-06-25 16:23:39 +00003389 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3390 // Only do this before legalize, since afterward the target may be depending
3391 // on the bitconvert.
3392 // First check to see if this is all constant.
3393 if (!AfterLegalize &&
3394 N0.getOpcode() == ISD::BUILD_VECTOR && N0.Val->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003395 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003396 bool isSimple = true;
3397 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3398 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3399 N0.getOperand(i).getOpcode() != ISD::Constant &&
3400 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3401 isSimple = false;
3402 break;
3403 }
3404
Duncan Sands83ec4b62008-06-06 12:08:01 +00003405 MVT DestEltVT = N->getValueType(0).getVectorElementType();
3406 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00003407 "Element type of vector ValueType must not be vector!");
3408 if (isSimple) {
3409 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.Val, DestEltVT);
3410 }
3411 }
3412
Chris Lattner94683772005-12-23 05:30:37 +00003413 // If the input is a constant, let getNode() fold it.
3414 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
3415 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3416 if (Res.Val != N) return Res;
3417 }
3418
Chris Lattnerc8547d82005-12-23 05:37:50 +00003419 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3420 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003421
Chris Lattner57104102005-12-23 05:44:41 +00003422 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003423 // If the resultant load doesn't need a higher alignment than the original!
3424 if (ISD::isNormalLoad(N0.Val) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003425 // Do not change the width of a volatile load.
3426 !cast<LoadSDNode>(N0)->isVolatile() &&
3427 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003428 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00003429 unsigned Align = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003430 getABITypeAlignment(VT.getTypeForMVT());
Evan Cheng59d5b682007-05-07 21:27:48 +00003431 unsigned OrigAlign = LN0->getAlignment();
3432 if (Align <= OrigAlign) {
3433 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
3434 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohman77455612008-06-28 00:45:22 +00003435 LN0->isVolatile(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00003436 AddToWorkList(N);
3437 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
3438 Load.getValue(1));
3439 return Load;
3440 }
Chris Lattner57104102005-12-23 05:44:41 +00003441 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003442
Chris Lattner3bd39d42008-01-27 17:42:27 +00003443 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3444 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3445 // This often reduces constant pool loads.
3446 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003447 N0.Val->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003448 SDOperand NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3449 AddToWorkList(NewConv.Val);
3450
Duncan Sands83ec4b62008-06-06 12:08:01 +00003451 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003452 if (N0.getOpcode() == ISD::FNEG)
3453 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3454 assert(N0.getOpcode() == ISD::FABS);
3455 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3456 }
3457
3458 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3459 // Note that we don't handle copysign(x,cst) because this can always be folded
3460 // to an fneg or fabs.
3461 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003462 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003463 VT.isInteger() && !VT.isVector()) {
3464 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
3465 SDOperand X = DAG.getNode(ISD::BIT_CONVERT,
3466 MVT::getIntegerVT(OrigXWidth),
Chris Lattner3bd39d42008-01-27 17:42:27 +00003467 N0.getOperand(1));
3468 AddToWorkList(X.Val);
3469
3470 // If X has a different width than the result/lhs, sext it or truncate it.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003471 unsigned VTWidth = VT.getSizeInBits();
Chris Lattner3bd39d42008-01-27 17:42:27 +00003472 if (OrigXWidth < VTWidth) {
3473 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
3474 AddToWorkList(X.Val);
3475 } else if (OrigXWidth > VTWidth) {
3476 // To get the sign bit in the right place, we have to shift it right
3477 // before truncating.
3478 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3479 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3480 AddToWorkList(X.Val);
3481 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3482 AddToWorkList(X.Val);
3483 }
3484
Duncan Sands83ec4b62008-06-06 12:08:01 +00003485 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003486 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
3487 AddToWorkList(X.Val);
3488
3489 SDOperand Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3490 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
3491 AddToWorkList(Cst.Val);
3492
3493 return DAG.getNode(ISD::OR, VT, X, Cst);
3494 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003495
3496 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
3497 if (N0.getOpcode() == ISD::BUILD_PAIR) {
3498 SDOperand CombineLD = CombineConsecutiveLoads(N0.Val, VT);
3499 if (CombineLD.Val)
3500 return CombineLD;
3501 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003502
Chris Lattner94683772005-12-23 05:30:37 +00003503 return SDOperand();
3504}
3505
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003506SDOperand DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003507 MVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003508 return CombineConsecutiveLoads(N, VT);
3509}
3510
Dan Gohman7f321562007-06-25 16:23:39 +00003511/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003512/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3513/// destination element value type.
3514SDOperand DAGCombiner::
Duncan Sands83ec4b62008-06-06 12:08:01 +00003515ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT DstEltVT) {
3516 MVT SrcEltVT = BV->getOperand(0).getValueType();
Chris Lattner6258fb22006-04-02 02:53:43 +00003517
3518 // If this is already the right type, we're done.
3519 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
3520
Duncan Sands83ec4b62008-06-06 12:08:01 +00003521 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3522 unsigned DstBitSize = DstEltVT.getSizeInBits();
Chris Lattner6258fb22006-04-02 02:53:43 +00003523
3524 // If this is a conversion of N elements of one type to N elements of another
3525 // type, convert each element. This handles FP<->INT cases.
3526 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003527 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003528 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003529 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00003530 AddToWorkList(Ops.back().Val);
3531 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003532 MVT VT = MVT::getVectorVT(DstEltVT,
3533 BV->getValueType(0).getVectorNumElements());
Dan Gohman7f321562007-06-25 16:23:39 +00003534 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003535 }
3536
3537 // Otherwise, we're growing or shrinking the elements. To avoid having to
3538 // handle annoying details of growing/shrinking FP values, we convert them to
3539 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003540 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003541 // Convert the input float vector to a int vector where the elements are the
3542 // same sizes.
3543 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003544 MVT IntVT = MVT::getIntegerVT(SrcEltVT.getSizeInBits());
Dan Gohman7f321562007-06-25 16:23:39 +00003545 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003546 SrcEltVT = IntVT;
3547 }
3548
3549 // Now we know the input is an integer vector. If the output is a FP type,
3550 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003551 if (DstEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003552 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003553 MVT TmpVT = MVT::getIntegerVT(DstEltVT.getSizeInBits());
Dan Gohman7f321562007-06-25 16:23:39 +00003554 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003555
3556 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003557 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003558 }
3559
3560 // Okay, we know the src/dst types are both integers of differing types.
3561 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003562 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00003563 if (SrcBitSize < DstBitSize) {
3564 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3565
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003566 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003567 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003568 i += NumInputsPerOutput) {
3569 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003570 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003571 bool EltIsUndef = true;
3572 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3573 // Shift the previously computed bits over.
3574 NewBits <<= SrcBitSize;
3575 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
3576 if (Op.getOpcode() == ISD::UNDEF) continue;
3577 EltIsUndef = false;
3578
Dan Gohman220a8232008-03-03 23:51:38 +00003579 NewBits |=
3580 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003581 }
3582
3583 if (EltIsUndef)
3584 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3585 else
3586 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3587 }
3588
Duncan Sands83ec4b62008-06-06 12:08:01 +00003589 MVT VT = MVT::getVectorVT(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003590 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003591 }
3592
3593 // Finally, this must be the case where we are shrinking elements: each input
3594 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003595 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003596 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003597 MVT VT = MVT::getVectorVT(DstEltVT, NumOutputsPerInput*BV->getNumOperands());
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003598 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003599 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003600 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3601 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3602 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3603 continue;
3604 }
Dan Gohman220a8232008-03-03 23:51:38 +00003605 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003606 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003607 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003608 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003609 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003610 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3611 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003612 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003613 }
3614
3615 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003616 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003617 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3618 }
Dan Gohman7f321562007-06-25 16:23:39 +00003619 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003620}
3621
3622
3623
Chris Lattner01b3d732005-09-28 22:28:18 +00003624SDOperand DAGCombiner::visitFADD(SDNode *N) {
3625 SDOperand N0 = N->getOperand(0);
3626 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003627 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3628 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003629 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003630
Dan Gohman7f321562007-06-25 16:23:39 +00003631 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003632 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00003633 SDOperand FoldedVOp = SimplifyVBinOp(N);
3634 if (FoldedVOp.Val) return FoldedVOp;
3635 }
Dan Gohman7f321562007-06-25 16:23:39 +00003636
Nate Begemana0e221d2005-10-18 00:28:13 +00003637 // fold (fadd 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::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003640 // canonicalize constant to RHS
3641 if (N0CFP && !N1CFP)
3642 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003643 // fold (A + (-B)) -> A-B
Chris Lattner0254e702008-02-26 07:04:54 +00003644 if (isNegatibleForFree(N1, AfterLegalize) == 2)
3645 return DAG.getNode(ISD::FSUB, VT, N0,
3646 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner01b3d732005-09-28 22:28:18 +00003647 // fold ((-A) + B) -> B-A
Chris Lattner0254e702008-02-26 07:04:54 +00003648 if (isNegatibleForFree(N0, AfterLegalize) == 2)
3649 return DAG.getNode(ISD::FSUB, VT, N1,
3650 GetNegatedExpression(N0, DAG, AfterLegalize));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003651
3652 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3653 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
3654 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3655 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3656 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3657
Chris Lattner01b3d732005-09-28 22:28:18 +00003658 return SDOperand();
3659}
3660
3661SDOperand DAGCombiner::visitFSUB(SDNode *N) {
3662 SDOperand N0 = N->getOperand(0);
3663 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003664 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3665 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003666 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003667
Dan Gohman7f321562007-06-25 16:23:39 +00003668 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003669 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00003670 SDOperand FoldedVOp = SimplifyVBinOp(N);
3671 if (FoldedVOp.Val) return FoldedVOp;
3672 }
Dan Gohman7f321562007-06-25 16:23:39 +00003673
Nate Begemana0e221d2005-10-18 00:28:13 +00003674 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003675 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003676 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003677 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003678 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Chris Lattner0254e702008-02-26 07:04:54 +00003679 if (isNegatibleForFree(N1, AfterLegalize))
3680 return GetNegatedExpression(N1, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003681 return DAG.getNode(ISD::FNEG, VT, N1);
3682 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003683 // fold (A-(-B)) -> A+B
Chris Lattner0254e702008-02-26 07:04:54 +00003684 if (isNegatibleForFree(N1, AfterLegalize))
3685 return DAG.getNode(ISD::FADD, VT, N0,
3686 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003687
Chris Lattner01b3d732005-09-28 22:28:18 +00003688 return SDOperand();
3689}
3690
3691SDOperand DAGCombiner::visitFMUL(SDNode *N) {
3692 SDOperand N0 = N->getOperand(0);
3693 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003694 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3695 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003696 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003697
Dan Gohman7f321562007-06-25 16:23:39 +00003698 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003699 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00003700 SDOperand FoldedVOp = SimplifyVBinOp(N);
3701 if (FoldedVOp.Val) return FoldedVOp;
3702 }
Dan Gohman7f321562007-06-25 16:23:39 +00003703
Nate Begeman11af4ea2005-10-17 20:40:11 +00003704 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003705 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003706 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003707 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003708 if (N0CFP && !N1CFP)
3709 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003710 // fold (fmul X, 2.0) -> (fadd X, X)
3711 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3712 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003713 // fold (fmul X, -1.0) -> (fneg X)
3714 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3715 return DAG.getNode(ISD::FNEG, VT, N0);
3716
3717 // -X * -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003718 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3719 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003720 // Both can be negated for free, check to see if at least one is cheaper
3721 // negated.
3722 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003723 return DAG.getNode(ISD::FMUL, VT,
3724 GetNegatedExpression(N0, DAG, AfterLegalize),
3725 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003726 }
3727 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003728
3729 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3730 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
3731 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3732 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3733 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3734
Chris Lattner01b3d732005-09-28 22:28:18 +00003735 return SDOperand();
3736}
3737
3738SDOperand DAGCombiner::visitFDIV(SDNode *N) {
3739 SDOperand N0 = N->getOperand(0);
3740 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003741 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3742 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003743 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003744
Dan Gohman7f321562007-06-25 16:23:39 +00003745 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003746 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00003747 SDOperand FoldedVOp = SimplifyVBinOp(N);
3748 if (FoldedVOp.Val) return FoldedVOp;
3749 }
Dan Gohman7f321562007-06-25 16:23:39 +00003750
Nate Begemana148d982006-01-18 22:35:16 +00003751 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003752 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003753 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003754
3755
3756 // -X / -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003757 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3758 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003759 // Both can be negated for free, check to see if at least one is cheaper
3760 // negated.
3761 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003762 return DAG.getNode(ISD::FDIV, VT,
3763 GetNegatedExpression(N0, DAG, AfterLegalize),
3764 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003765 }
3766 }
3767
Chris Lattner01b3d732005-09-28 22:28:18 +00003768 return SDOperand();
3769}
3770
3771SDOperand DAGCombiner::visitFREM(SDNode *N) {
3772 SDOperand N0 = N->getOperand(0);
3773 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003774 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3775 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003776 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003777
Nate Begemana148d982006-01-18 22:35:16 +00003778 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003779 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003780 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003781
Chris Lattner01b3d732005-09-28 22:28:18 +00003782 return SDOperand();
3783}
3784
Chris Lattner12d83032006-03-05 05:30:57 +00003785SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3786 SDOperand N0 = N->getOperand(0);
3787 SDOperand N1 = N->getOperand(1);
3788 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3789 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003790 MVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00003791
Dale Johannesendb44bf82007-10-16 23:38:29 +00003792 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003793 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3794
3795 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003796 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003797 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3798 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003799 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003800 return DAG.getNode(ISD::FABS, VT, N0);
3801 else
3802 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3803 }
3804
3805 // copysign(fabs(x), y) -> copysign(x, y)
3806 // copysign(fneg(x), y) -> copysign(x, y)
3807 // copysign(copysign(x,z), y) -> copysign(x, y)
3808 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3809 N0.getOpcode() == ISD::FCOPYSIGN)
3810 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3811
3812 // copysign(x, abs(y)) -> abs(x)
3813 if (N1.getOpcode() == ISD::FABS)
3814 return DAG.getNode(ISD::FABS, VT, N0);
3815
3816 // copysign(x, copysign(y,z)) -> copysign(x, z)
3817 if (N1.getOpcode() == ISD::FCOPYSIGN)
3818 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3819
3820 // copysign(x, fp_extend(y)) -> copysign(x, y)
3821 // copysign(x, fp_round(y)) -> copysign(x, y)
3822 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3823 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3824
3825 return SDOperand();
3826}
3827
3828
Chris Lattner01b3d732005-09-28 22:28:18 +00003829
Nate Begeman83e75ec2005-09-06 04:43:02 +00003830SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003831 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003832 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003833 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00003834 MVT OpVT = N0.getValueType();
3835
Nate Begeman1d4d4142005-09-01 00:19:25 +00003836 // fold (sint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00003837 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003838 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00003839
3840 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
3841 // but UINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00003842 if (!TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00003843 TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT)) {
3844 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
3845 if (DAG.SignBitIsZero(N0))
3846 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
3847 }
3848
3849
Nate Begeman83e75ec2005-09-06 04:43:02 +00003850 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003851}
3852
Nate Begeman83e75ec2005-09-06 04:43:02 +00003853SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003854 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003855 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003856 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00003857 MVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00003858
Nate Begeman1d4d4142005-09-01 00:19:25 +00003859 // fold (uint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00003860 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003861 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00003862
3863 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
3864 // but SINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00003865 if (!TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00003866 TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT)) {
3867 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
3868 if (DAG.SignBitIsZero(N0))
3869 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
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::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003876 SDOperand N0 = N->getOperand(0);
3877 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003878 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003879
3880 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003881 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003882 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003883 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003884}
3885
Nate Begeman83e75ec2005-09-06 04:43:02 +00003886SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003887 SDOperand N0 = N->getOperand(0);
3888 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003889 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003890
3891 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00003892 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003893 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003894 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003895}
3896
Nate Begeman83e75ec2005-09-06 04:43:02 +00003897SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003898 SDOperand N0 = N->getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003899 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003900 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003901 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003902
3903 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003904 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00003905 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003906
3907 // fold (fp_round (fp_extend x)) -> x
3908 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3909 return N0.getOperand(0);
3910
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00003911 // fold (fp_round (fp_round x)) -> (fp_round x)
3912 if (N0.getOpcode() == ISD::FP_ROUND) {
3913 // This is a value preserving truncation if both round's are.
3914 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
3915 N0.Val->getConstantOperandVal(1) == 1;
3916 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
3917 DAG.getIntPtrConstant(IsTrunc));
3918 }
3919
Chris Lattner79dbea52006-03-13 06:26:26 +00003920 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3921 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003922 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003923 AddToWorkList(Tmp.Val);
3924 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3925 }
3926
Nate Begeman83e75ec2005-09-06 04:43:02 +00003927 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003928}
3929
Nate Begeman83e75ec2005-09-06 04:43:02 +00003930SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003931 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003932 MVT VT = N->getValueType(0);
3933 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003934 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003935
Nate Begeman1d4d4142005-09-01 00:19:25 +00003936 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003937 if (N0CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003938 SDOperand Round = DAG.getConstantFP(N0CFP->getValueAPF(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003939 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003940 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003941 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003942}
3943
Nate Begeman83e75ec2005-09-06 04:43:02 +00003944SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003945 SDOperand N0 = N->getOperand(0);
3946 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003947 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003948
Chris Lattner5938bef2007-12-29 06:55:23 +00003949 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003950 if (N->hasOneUse() &&
Dan Gohman89684502008-07-27 20:43:25 +00003951 N->use_begin().getUse().getSDOperand().getOpcode() == ISD::FP_ROUND)
Chris Lattner5938bef2007-12-29 06:55:23 +00003952 return SDOperand();
Chris Lattner0bd48932008-01-17 07:00:52 +00003953
Nate Begeman1d4d4142005-09-01 00:19:25 +00003954 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003955 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003956 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003957
3958 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
3959 // value of X.
3960 if (N0.getOpcode() == ISD::FP_ROUND && N0.Val->getConstantOperandVal(1) == 1){
3961 SDOperand In = N0.getOperand(0);
3962 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00003963 if (VT.bitsLT(In.getValueType()))
Chris Lattner0bd48932008-01-17 07:00:52 +00003964 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
3965 return DAG.getNode(ISD::FP_EXTEND, VT, In);
3966 }
3967
3968 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Dale Johannesenb6210fc2007-10-19 20:29:00 +00003969 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003970 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3971 TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003972 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3973 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3974 LN0->getBasePtr(), LN0->getSrcValue(),
3975 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003976 N0.getValueType(),
3977 LN0->isVolatile(),
3978 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003979 CombineTo(N, ExtLoad);
Chris Lattner0bd48932008-01-17 07:00:52 +00003980 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad,
3981 DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00003982 ExtLoad.getValue(1));
3983 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3984 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003985
Nate Begeman83e75ec2005-09-06 04:43:02 +00003986 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003987}
3988
Nate Begeman83e75ec2005-09-06 04:43:02 +00003989SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003990 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003991
Chris Lattner0254e702008-02-26 07:04:54 +00003992 if (isNegatibleForFree(N0, AfterLegalize))
3993 return GetNegatedExpression(N0, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003994
Chris Lattner3bd39d42008-01-27 17:42:27 +00003995 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
3996 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00003997 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003998 N0.getOperand(0).getValueType().isInteger() &&
3999 !N0.getOperand(0).getValueType().isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004000 SDOperand Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004001 MVT IntVT = Int.getValueType();
4002 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004003 Int = DAG.getNode(ISD::XOR, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004004 DAG.getConstant(IntVT.getIntegerVTSignBit(), IntVT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00004005 AddToWorkList(Int.Val);
4006 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4007 }
4008 }
4009
Nate Begeman83e75ec2005-09-06 04:43:02 +00004010 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004011}
4012
Nate Begeman83e75ec2005-09-06 04:43:02 +00004013SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00004014 SDOperand N0 = N->getOperand(0);
4015 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004016 MVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00004017
Nate Begeman1d4d4142005-09-01 00:19:25 +00004018 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00004019 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004020 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004021 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004022 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004023 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004024 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004025 // fold (fabs (fcopysign x, y)) -> (fabs x)
4026 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
4027 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
4028
Chris Lattner3bd39d42008-01-27 17:42:27 +00004029 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4030 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00004031 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004032 N0.getOperand(0).getValueType().isInteger() &&
4033 !N0.getOperand(0).getValueType().isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004034 SDOperand Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004035 MVT IntVT = Int.getValueType();
4036 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004037 Int = DAG.getNode(ISD::AND, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004038 DAG.getConstant(~IntVT.getIntegerVTSignBit(), IntVT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00004039 AddToWorkList(Int.Val);
4040 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4041 }
4042 }
4043
Nate Begeman83e75ec2005-09-06 04:43:02 +00004044 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004045}
4046
Nate Begeman44728a72005-09-19 22:34:01 +00004047SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
4048 SDOperand Chain = N->getOperand(0);
4049 SDOperand N1 = N->getOperand(1);
4050 SDOperand N2 = N->getOperand(2);
4051 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4052
4053 // never taken branch, fold to chain
4054 if (N1C && N1C->isNullValue())
4055 return Chain;
4056 // unconditional branch
Dan Gohman002e5d02008-03-13 22:13:53 +00004057 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00004058 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004059 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4060 // on the target.
4061 if (N1.getOpcode() == ISD::SETCC &&
4062 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
4063 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
4064 N1.getOperand(0), N1.getOperand(1), N2);
4065 }
Nate Begeman44728a72005-09-19 22:34:01 +00004066 return SDOperand();
4067}
4068
Chris Lattner3ea0b472005-10-05 06:47:48 +00004069// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4070//
Nate Begeman44728a72005-09-19 22:34:01 +00004071SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00004072 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
4073 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
4074
Duncan Sands8eab8a22008-06-09 11:32:28 +00004075 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00004076 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00004077 if (Simp.Val) AddToWorkList(Simp.Val);
4078
Nate Begemane17daeb2005-10-05 21:43:42 +00004079 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
4080
4081 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman002e5d02008-03-13 22:13:53 +00004082 if (SCCC && !SCCC->isNullValue())
Nate Begemane17daeb2005-10-05 21:43:42 +00004083 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
4084 N->getOperand(4));
4085 // fold br_cc false, dest -> unconditional fall through
4086 if (SCCC && SCCC->isNullValue())
4087 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00004088
Nate Begemane17daeb2005-10-05 21:43:42 +00004089 // fold to a simpler setcc
4090 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
4091 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4092 Simp.getOperand(2), Simp.getOperand(0),
4093 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00004094 return SDOperand();
4095}
4096
Chris Lattner448f2192006-11-11 00:39:41 +00004097
Duncan Sandsec87aa82008-06-15 20:12:31 +00004098/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4099/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00004100/// and it has other uses besides the load / store. After the
4101/// transformation, the new indexed load / store has effectively folded
4102/// the add / subtract in and all of its other uses are redirected to the
4103/// new load / store.
4104bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
4105 if (!AfterLegalize)
4106 return false;
4107
4108 bool isLoad = true;
4109 SDOperand Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004110 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004111 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004112 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004113 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004114 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00004115 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00004116 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4117 return false;
4118 Ptr = LD->getBasePtr();
4119 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004120 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004121 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004122 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004123 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4124 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4125 return false;
4126 Ptr = ST->getBasePtr();
4127 isLoad = false;
4128 } else
4129 return false;
4130
Chris Lattner9f1794e2006-11-11 00:56:29 +00004131 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4132 // out. There is no reason to make this a preinc/predec.
4133 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
4134 Ptr.Val->hasOneUse())
4135 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004136
Chris Lattner9f1794e2006-11-11 00:56:29 +00004137 // Ask the target to do addressing mode selection.
4138 SDOperand BasePtr;
4139 SDOperand Offset;
4140 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4141 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4142 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004143 // Don't create a indexed load / store with zero offset.
4144 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004145 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004146 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004147
Chris Lattner41e53fd2006-11-11 01:00:15 +00004148 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004149 // 1) The new base ptr is a frame index.
4150 // 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 +00004151 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004152 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004153 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004154 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004155
Chris Lattner41e53fd2006-11-11 01:00:15 +00004156 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4157 // (plus the implicit offset) to a register to preinc anyway.
4158 if (isa<FrameIndexSDNode>(BasePtr))
4159 return false;
4160
4161 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004162 if (!isLoad) {
4163 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Cheng917be682008-03-04 00:41:45 +00004164 if (Val == BasePtr || BasePtr.Val->isPredecessorOf(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004165 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004166 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004167
Evan Chengc843abe2007-05-24 02:35:39 +00004168 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004169 bool RealUse = false;
4170 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4171 E = Ptr.Val->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004172 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004173 if (Use == N)
4174 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004175 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004176 return false;
4177
4178 if (!((Use->getOpcode() == ISD::LOAD &&
4179 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004180 (Use->getOpcode() == ISD::STORE &&
4181 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004182 RealUse = true;
4183 }
4184 if (!RealUse)
4185 return false;
4186
4187 SDOperand Result;
4188 if (isLoad)
4189 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
4190 else
4191 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4192 ++PreIndexedNodes;
4193 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004194 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004195 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4196 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004197 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004198 if (isLoad) {
4199 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004200 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004201 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004202 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004203 } else {
4204 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004205 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004206 }
4207
Chris Lattner9f1794e2006-11-11 00:56:29 +00004208 // Finally, since the node is now dead, remove it from the graph.
4209 DAG.DeleteNode(N);
4210
4211 // Replace the uses of Ptr with uses of the updated base value.
4212 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004213 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004214 removeFromWorkList(Ptr.Val);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004215 DAG.DeleteNode(Ptr.Val);
4216
4217 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004218}
4219
Duncan Sandsec87aa82008-06-15 20:12:31 +00004220/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00004221/// add / sub of the base pointer node into a post-indexed load / store.
4222/// The transformation folded the add / subtract into the new indexed
4223/// load / store effectively and all of its uses are redirected to the
4224/// new load / store.
4225bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
4226 if (!AfterLegalize)
4227 return false;
4228
4229 bool isLoad = true;
4230 SDOperand Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004231 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004232 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004233 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004234 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004235 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004236 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4237 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4238 return false;
4239 Ptr = LD->getBasePtr();
4240 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004241 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004242 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004243 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004244 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4245 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4246 return false;
4247 Ptr = ST->getBasePtr();
4248 isLoad = false;
4249 } else
4250 return false;
4251
Evan Chengcc470212006-11-16 00:08:20 +00004252 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004253 return false;
4254
4255 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4256 E = Ptr.Val->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004257 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004258 if (Op == N ||
4259 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4260 continue;
4261
4262 SDOperand BasePtr;
4263 SDOperand Offset;
4264 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4265 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4266 if (Ptr == Offset)
4267 std::swap(BasePtr, Offset);
4268 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004269 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004270 // Don't create a indexed load / store with zero offset.
4271 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004272 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004273 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004274
Chris Lattner9f1794e2006-11-11 00:56:29 +00004275 // Try turning it into a post-indexed load / store except when
4276 // 1) All uses are load / store ops that use it as base ptr.
4277 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4278 // nor a successor of N. Otherwise, if Op is folded that would
4279 // create a cycle.
4280
4281 // Check for #1.
4282 bool TryNext = false;
4283 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
4284 EE = BasePtr.Val->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00004285 SDNode *Use = *II;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004286 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00004287 continue;
4288
Chris Lattner9f1794e2006-11-11 00:56:29 +00004289 // If all the uses are load / store addresses, then don't do the
4290 // transformation.
4291 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4292 bool RealUse = false;
4293 for (SDNode::use_iterator III = Use->use_begin(),
4294 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00004295 SDNode *UseUse = *III;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004296 if (!((UseUse->getOpcode() == ISD::LOAD &&
4297 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004298 (UseUse->getOpcode() == ISD::STORE &&
4299 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004300 RealUse = true;
4301 }
Chris Lattner448f2192006-11-11 00:39:41 +00004302
Chris Lattner9f1794e2006-11-11 00:56:29 +00004303 if (!RealUse) {
4304 TryNext = true;
4305 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004306 }
4307 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004308 }
4309 if (TryNext)
4310 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004311
Chris Lattner9f1794e2006-11-11 00:56:29 +00004312 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004313 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Chris Lattner9f1794e2006-11-11 00:56:29 +00004314 SDOperand Result = isLoad
4315 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
4316 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4317 ++PostIndexedNodes;
4318 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004319 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004320 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4321 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004322 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004323 if (isLoad) {
4324 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004325 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004326 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004327 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004328 } else {
4329 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004330 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004331 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004332
Chris Lattner9f1794e2006-11-11 00:56:29 +00004333 // Finally, since the node is now dead, remove it from the graph.
4334 DAG.DeleteNode(N);
4335
4336 // Replace the uses of Use with uses of the updated base value.
4337 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
4338 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004339 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004340 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004341 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004342 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004343 }
4344 }
4345 }
4346 return false;
4347}
4348
Chris Lattner00161a62008-01-25 07:20:16 +00004349/// InferAlignment - If we can infer some alignment information from this
4350/// pointer, return it.
4351static unsigned InferAlignment(SDOperand Ptr, SelectionDAG &DAG) {
4352 // If this is a direct reference to a stack slot, use information about the
4353 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004354 int FrameIdx = 1 << 31;
4355 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004356 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004357 FrameIdx = FI->getIndex();
4358 } else if (Ptr.getOpcode() == ISD::ADD &&
4359 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4360 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4361 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4362 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004363 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004364
4365 if (FrameIdx != (1 << 31)) {
4366 // FIXME: Handle FI+CST.
4367 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4368 if (MFI.isFixedObjectIndex(FrameIdx)) {
4369 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx);
4370
4371 // The alignment of the frame index can be determined from its offset from
4372 // the incoming frame position. If the frame object is at offset 32 and
4373 // the stack is guaranteed to be 16-byte aligned, then we know that the
4374 // object is 16-byte aligned.
4375 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4376 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4377
4378 // Finally, the frame object itself may have a known alignment. Factor
4379 // the alignment + offset into a new alignment. For example, if we know
4380 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4381 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4382 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4383 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4384 FrameOffset);
4385 return std::max(Align, FIInfoAlign);
4386 }
4387 }
Chris Lattner00161a62008-01-25 07:20:16 +00004388
4389 return 0;
4390}
Chris Lattner448f2192006-11-11 00:39:41 +00004391
Chris Lattner01a22022005-10-10 22:04:48 +00004392SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004393 LoadSDNode *LD = cast<LoadSDNode>(N);
4394 SDOperand Chain = LD->getChain();
4395 SDOperand Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004396
4397 // Try to infer better alignment information than the load already has.
4398 if (LD->isUnindexed()) {
4399 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4400 if (Align > LD->getAlignment())
4401 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4402 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004403 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004404 LD->isVolatile(), Align);
4405 }
4406 }
4407
Evan Cheng45a7ca92007-05-01 00:38:21 +00004408
4409 // If load is not volatile and there are no uses of the loaded value (and
4410 // the updated indexed value in case of indexed loads), change uses of the
4411 // chain value into uses of the chain input (i.e. delete the dead load).
4412 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004413 if (N->getValueType(1) == MVT::Other) {
4414 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004415 if (N->hasNUsesOfValue(0, 0)) {
4416 // It's not safe to use the two value CombineTo variant here. e.g.
4417 // v1, chain2 = load chain1, loc
4418 // v2, chain3 = load chain2, loc
4419 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004420 // Now we replace use of chain2 with chain1. This makes the second load
4421 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004422 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004423 DOUT << "\nWith chain: "; DEBUG(Chain.Val->dump(&DAG));
4424 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004425 WorkListRemover DeadNodes(*this);
4426 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004427 if (N->use_empty()) {
4428 removeFromWorkList(N);
4429 DAG.DeleteNode(N);
4430 }
Evan Cheng02c42852008-01-16 23:11:54 +00004431 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
4432 }
Evan Cheng498f5592007-05-01 08:53:39 +00004433 } else {
4434 // Indexed loads.
4435 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4436 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Evan Cheng02c42852008-01-16 23:11:54 +00004437 SDOperand Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
4438 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
4439 DOUT << "\nWith: "; DEBUG(Undef.Val->dump(&DAG));
4440 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004441 WorkListRemover DeadNodes(*this);
4442 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Undef, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004443 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004444 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004445 &DeadNodes);
4446 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004447 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004448 DAG.DeleteNode(N);
4449 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004450 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004451 }
4452 }
Chris Lattner01a22022005-10-10 22:04:48 +00004453
4454 // If this load is directly stored, replace the load value with the stored
4455 // value.
4456 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004457 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00004458 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4459 !LD->isVolatile()) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004460 if (ISD::isNON_TRUNCStore(Chain.Val)) {
4461 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4462 if (PrevST->getBasePtr() == Ptr &&
4463 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004464 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004465 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004466 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004467
Jim Laskey7ca56af2006-10-11 13:47:09 +00004468 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004469 // Walk up chain skipping non-aliasing memory nodes.
4470 SDOperand BetterChain = FindBetterChain(N, Chain);
4471
Jim Laskey6ff23e52006-10-04 16:53:27 +00004472 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004473 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004474 SDOperand ReplLoad;
4475
Jim Laskey279f0532006-09-25 16:29:54 +00004476 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004477 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4478 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004479 LD->getSrcValue(), LD->getSrcValueOffset(),
4480 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004481 } else {
4482 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4483 LD->getValueType(0),
4484 BetterChain, Ptr, LD->getSrcValue(),
4485 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004486 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004487 LD->isVolatile(),
4488 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004489 }
Jim Laskey279f0532006-09-25 16:29:54 +00004490
Jim Laskey6ff23e52006-10-04 16:53:27 +00004491 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00004492 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
4493 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004494
Jim Laskey274062c2006-10-13 23:32:28 +00004495 // Replace uses with load result and token factor. Don't add users
4496 // to work list.
4497 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004498 }
4499 }
4500
Evan Cheng7fc033a2006-11-03 03:06:21 +00004501 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004502 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00004503 return SDOperand(N, 0);
4504
Chris Lattner01a22022005-10-10 22:04:48 +00004505 return SDOperand();
4506}
4507
Chris Lattner07649d92008-01-08 23:08:06 +00004508
Chris Lattner87514ca2005-10-10 22:31:19 +00004509SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004510 StoreSDNode *ST = cast<StoreSDNode>(N);
4511 SDOperand Chain = ST->getChain();
4512 SDOperand Value = ST->getValue();
4513 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004514
Chris Lattner00161a62008-01-25 07:20:16 +00004515 // Try to infer better alignment information than the store already has.
4516 if (ST->isUnindexed()) {
4517 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4518 if (Align > ST->getAlignment())
4519 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004520 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004521 ST->isVolatile(), Align);
4522 }
4523 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004524
Evan Cheng59d5b682007-05-07 21:27:48 +00004525 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004526 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004527 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004528 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004529 unsigned Align = ST->getAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004530 MVT SVT = Value.getOperand(0).getValueType();
Evan Cheng59d5b682007-05-07 21:27:48 +00004531 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004532 getABITypeAlignment(SVT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004533 if (Align <= OrigAlign &&
4534 ((!AfterLegalize && !ST->isVolatile()) ||
4535 TLI.isOperationLegal(ISD::STORE, SVT)))
Evan Cheng59d5b682007-05-07 21:27:48 +00004536 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohman77455612008-06-28 00:45:22 +00004537 ST->getSrcValueOffset(), ST->isVolatile(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00004538 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004539
Nate Begeman2cbba892006-12-11 02:23:46 +00004540 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004541 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004542 // NOTE: If the original store is volatile, this transform must not increase
4543 // the number of stores. For example, on x86-32 an f64 can be stored in one
4544 // processor operation but an i64 (which is not legal) requires two. So the
4545 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00004546 if (Value.getOpcode() != ISD::TargetConstantFP) {
4547 SDOperand Tmp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004548 switch (CFP->getValueType(0).getSimpleVT()) {
Chris Lattner62be1a72006-12-12 04:16:14 +00004549 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004550 case MVT::f80: // We don't do this for these yet.
4551 case MVT::f128:
4552 case MVT::ppcf128:
4553 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004554 case MVT::f32:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004555 if ((!AfterLegalize && !ST->isVolatile()) ||
4556 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004557 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
4558 convertToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004559 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004560 ST->getSrcValueOffset(), ST->isVolatile(),
4561 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004562 }
4563 break;
4564 case MVT::f64:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004565 if ((!AfterLegalize && !ST->isVolatile()) ||
4566 TLI.isOperationLegal(ISD::STORE, MVT::i64)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004567 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
4568 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004569 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004570 ST->getSrcValueOffset(), ST->isVolatile(),
4571 ST->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004572 } else if (!ST->isVolatile() &&
4573 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004574 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004575 // argument passing. Since this is so common, custom legalize the
4576 // 64-bit integer store into two 32-bit stores.
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004577 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
Chris Lattner62be1a72006-12-12 04:16:14 +00004578 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4579 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004580 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004581
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004582 int SVOffset = ST->getSrcValueOffset();
4583 unsigned Alignment = ST->getAlignment();
4584 bool isVolatile = ST->isVolatile();
4585
Chris Lattner62be1a72006-12-12 04:16:14 +00004586 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004587 ST->getSrcValueOffset(),
4588 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004589 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4590 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004591 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004592 Alignment = MinAlign(Alignment, 4U);
Chris Lattner62be1a72006-12-12 04:16:14 +00004593 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004594 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004595 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4596 }
4597 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004598 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004599 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004600 }
4601
Jim Laskey279f0532006-09-25 16:29:54 +00004602 if (CombinerAA) {
4603 // Walk up chain skipping non-aliasing memory nodes.
4604 SDOperand BetterChain = FindBetterChain(N, Chain);
4605
Jim Laskey6ff23e52006-10-04 16:53:27 +00004606 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004607 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004608 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004609 SDOperand ReplStore;
4610 if (ST->isTruncatingStore()) {
4611 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004612 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004613 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004614 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004615 } else {
4616 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004617 ST->getSrcValue(), ST->getSrcValueOffset(),
4618 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004619 }
4620
Jim Laskey279f0532006-09-25 16:29:54 +00004621 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00004622 SDOperand Token =
4623 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4624
4625 // Don't add users to work list.
4626 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004627 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004628 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004629
Evan Cheng33dbedc2006-11-05 09:31:14 +00004630 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004631 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00004632 return SDOperand(N, 0);
4633
Chris Lattner3c872852007-12-29 06:26:16 +00004634 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004635 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004636 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00004637 // See if we can simplify the input to this truncstore with knowledge that
4638 // only the low bits are being used. For example:
4639 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
4640 SDOperand Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004641 GetDemandedBits(Value,
4642 APInt::getLowBitsSet(Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004643 ST->getMemoryVT().getSizeInBits()));
Chris Lattner2b4c2792007-10-13 06:35:54 +00004644 AddToWorkList(Value.Val);
4645 if (Shorter.Val)
4646 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004647 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004648 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004649
4650 // Otherwise, see if we can simplify the operation with
4651 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004652 if (SimplifyDemandedBits(Value,
4653 APInt::getLowBitsSet(
4654 Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004655 ST->getMemoryVT().getSizeInBits())))
Chris Lattnere33544c2007-10-13 06:58:48 +00004656 return SDOperand(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004657 }
4658
Chris Lattner3c872852007-12-29 06:26:16 +00004659 // If this is a load followed by a store to the same location, then the store
4660 // is dead/noop.
4661 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004662 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004663 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004664 // There can't be any side effects between the load and store, such as
4665 // a call or store.
Chris Lattner572dee72008-01-16 05:49:24 +00004666 Chain.reachesChainWithoutSideEffects(SDOperand(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004667 // The store is dead, remove it.
4668 return Chain;
4669 }
4670 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004671
Chris Lattnerddf89562008-01-17 19:59:44 +00004672 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4673 // truncating store. We can do this even if this is already a truncstore.
4674 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004675 && Value.Val->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004676 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004677 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004678 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004679 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004680 ST->isVolatile(), ST->getAlignment());
4681 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004682
Chris Lattner87514ca2005-10-10 22:31:19 +00004683 return SDOperand();
4684}
4685
Chris Lattnerca242442006-03-19 01:27:56 +00004686SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4687 SDOperand InVec = N->getOperand(0);
4688 SDOperand InVal = N->getOperand(1);
4689 SDOperand EltNo = N->getOperand(2);
4690
4691 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4692 // vector with the inserted element.
4693 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
4694 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004695 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004696 if (Elt < Ops.size())
4697 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004698 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4699 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004700 }
4701
4702 return SDOperand();
4703}
4704
Evan Cheng513da432007-10-06 08:19:55 +00004705SDOperand DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004706 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
4707 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
4708 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
4709
4710 // Perform only after legalization to ensure build_vector / vector_shuffle
4711 // optimizations have already been done.
4712 if (!AfterLegalize) return SDOperand();
4713
Evan Cheng513da432007-10-06 08:19:55 +00004714 SDOperand InVec = N->getOperand(0);
4715 SDOperand EltNo = N->getOperand(1);
4716
Evan Cheng513da432007-10-06 08:19:55 +00004717 if (isa<ConstantSDNode>(EltNo)) {
4718 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
4719 bool NewLoad = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004720 MVT VT = InVec.getValueType();
4721 MVT EVT = VT.getVectorElementType();
4722 MVT LVT = EVT;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004723 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004724 MVT BCVT = InVec.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004725 if (!BCVT.isVector() || EVT.bitsGT(BCVT.getVectorElementType()))
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004726 return SDOperand();
4727 InVec = InVec.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004728 EVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004729 NewLoad = true;
4730 }
Evan Cheng513da432007-10-06 08:19:55 +00004731
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004732 LoadSDNode *LN0 = NULL;
4733 if (ISD::isNormalLoad(InVec.Val))
4734 LN0 = cast<LoadSDNode>(InVec);
4735 else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4736 InVec.getOperand(0).getValueType() == EVT &&
4737 ISD::isNormalLoad(InVec.getOperand(0).Val)) {
4738 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4739 } else if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE) {
4740 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
4741 // =>
4742 // (load $addr+1*size)
4743 unsigned Idx = cast<ConstantSDNode>(InVec.getOperand(2).
4744 getOperand(Elt))->getValue();
4745 unsigned NumElems = InVec.getOperand(2).getNumOperands();
4746 InVec = (Idx < NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
4747 if (InVec.getOpcode() == ISD::BIT_CONVERT)
4748 InVec = InVec.getOperand(0);
4749 if (ISD::isNormalLoad(InVec.Val)) {
4750 LN0 = cast<LoadSDNode>(InVec);
4751 Elt = (Idx < NumElems) ? Idx : Idx - NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00004752 }
4753 }
Duncan Sandsec87aa82008-06-15 20:12:31 +00004754 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004755 return SDOperand();
4756
4757 unsigned Align = LN0->getAlignment();
4758 if (NewLoad) {
4759 // Check the resultant load doesn't need a higher alignment than the
4760 // original load.
4761 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004762 getABITypeAlignment(LVT.getTypeForMVT());
Duncan Sands184a8762008-06-14 17:48:34 +00004763 if (NewAlign > Align || !TLI.isOperationLegal(ISD::LOAD, LVT))
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004764 return SDOperand();
4765 Align = NewAlign;
4766 }
4767
4768 SDOperand NewPtr = LN0->getBasePtr();
4769 if (Elt) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004770 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
4771 MVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004772 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00004773 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004774 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
4775 DAG.getConstant(PtrOff, PtrType));
4776 }
4777 return DAG.getLoad(LVT, LN0->getChain(), NewPtr,
4778 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4779 LN0->isVolatile(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00004780 }
4781 return SDOperand();
4782}
4783
4784
Dan Gohman7f321562007-06-25 16:23:39 +00004785SDOperand DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
4786 unsigned NumInScalars = N->getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004787 MVT VT = N->getValueType(0);
4788 unsigned NumElts = VT.getVectorNumElements();
4789 MVT EltType = VT.getVectorElementType();
Chris Lattnerca242442006-03-19 01:27:56 +00004790
Dan Gohman7f321562007-06-25 16:23:39 +00004791 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4792 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4793 // at most two distinct vectors, turn this into a shuffle node.
Chris Lattnerd7648c82006-03-28 20:28:38 +00004794 SDOperand VecIn1, VecIn2;
4795 for (unsigned i = 0; i != NumInScalars; ++i) {
4796 // Ignore undef inputs.
4797 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4798
Dan Gohman7f321562007-06-25 16:23:39 +00004799 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004800 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004801 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004802 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
4803 VecIn1 = VecIn2 = SDOperand(0, 0);
4804 break;
4805 }
4806
Dan Gohman7f321562007-06-25 16:23:39 +00004807 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004808 // we can't make a shuffle.
4809 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004810 if (ExtractedFromVec.getValueType() != VT) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004811 VecIn1 = VecIn2 = SDOperand(0, 0);
4812 break;
4813 }
4814
4815 // Otherwise, remember this. We allow up to two distinct input vectors.
4816 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4817 continue;
4818
4819 if (VecIn1.Val == 0) {
4820 VecIn1 = ExtractedFromVec;
4821 } else if (VecIn2.Val == 0) {
4822 VecIn2 = ExtractedFromVec;
4823 } else {
4824 // Too many inputs.
4825 VecIn1 = VecIn2 = SDOperand(0, 0);
4826 break;
4827 }
4828 }
4829
4830 // If everything is good, we can make a shuffle operation.
4831 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004832 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004833 for (unsigned i = 0; i != NumInScalars; ++i) {
4834 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004835 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004836 continue;
4837 }
4838
4839 SDOperand Extract = N->getOperand(i);
4840
4841 // If extracting from the first vector, just use the index directly.
4842 if (Extract.getOperand(0) == VecIn1) {
4843 BuildVecIndices.push_back(Extract.getOperand(1));
4844 continue;
4845 }
4846
4847 // Otherwise, use InIdx + VecSize
4848 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004849 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004850 }
4851
4852 // Add count and size info.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004853 MVT BuildVecVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004854
Dan Gohman7f321562007-06-25 16:23:39 +00004855 // Return the new VECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004856 SDOperand Ops[5];
4857 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00004858 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004859 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004860 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004861 // Use an undef build_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00004862 std::vector<SDOperand> UnOps(NumInScalars,
4863 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004864 EltType));
4865 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004866 &UnOps[0], UnOps.size());
4867 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00004868 }
Dan Gohman7f321562007-06-25 16:23:39 +00004869 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004870 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004871 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004872 }
4873
4874 return SDOperand();
4875}
4876
Dan Gohman7f321562007-06-25 16:23:39 +00004877SDOperand DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
4878 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
4879 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
4880 // inputs come from at most two distinct vectors, turn this into a shuffle
4881 // node.
4882
4883 // If we only have one input vector, we don't need to do any concatenation.
4884 if (N->getNumOperands() == 1) {
4885 return N->getOperand(0);
4886 }
4887
4888 return SDOperand();
4889}
4890
Chris Lattner66445d32006-03-28 22:11:53 +00004891SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004892 SDOperand ShufMask = N->getOperand(2);
4893 unsigned NumElts = ShufMask.getNumOperands();
4894
4895 // If the shuffle mask is an identity operation on the LHS, return the LHS.
4896 bool isIdentity = true;
4897 for (unsigned i = 0; i != NumElts; ++i) {
4898 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4899 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
4900 isIdentity = false;
4901 break;
4902 }
4903 }
4904 if (isIdentity) return N->getOperand(0);
4905
4906 // If the shuffle mask is an identity operation on the RHS, return the RHS.
4907 isIdentity = true;
4908 for (unsigned i = 0; i != NumElts; ++i) {
4909 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4910 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
4911 isIdentity = false;
4912 break;
4913 }
4914 }
4915 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004916
4917 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
4918 // needed at all.
4919 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00004920 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004921 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00004922 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004923 for (unsigned i = 0; i != NumElts; ++i)
4924 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
4925 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
4926 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00004927 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004928 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004929 BaseIdx = Idx;
4930 } else {
4931 if (BaseIdx != Idx)
4932 isSplat = false;
4933 if (VecNum != V) {
4934 isUnary = false;
4935 break;
4936 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004937 }
4938 }
4939
4940 SDOperand N0 = N->getOperand(0);
4941 SDOperand N1 = N->getOperand(1);
4942 // Normalize unary shuffle so the RHS is undef.
4943 if (isUnary && VecNum == 1)
4944 std::swap(N0, N1);
4945
Evan Cheng917ec982006-07-21 08:25:53 +00004946 // If it is a splat, check if the argument vector is a build_vector with
4947 // all scalar elements the same.
4948 if (isSplat) {
4949 SDNode *V = N0.Val;
Evan Cheng917ec982006-07-21 08:25:53 +00004950
Dan Gohman7f321562007-06-25 16:23:39 +00004951 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004952 // not the number of vector elements, look through it. Be careful not to
4953 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004954 if (V->getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng59569222006-10-16 22:49:37 +00004955 SDOperand ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00004956 if (ConvInput.getValueType().isVector() &&
4957 ConvInput.getValueType().getVectorNumElements() == NumElts)
Evan Cheng59569222006-10-16 22:49:37 +00004958 V = ConvInput.Val;
4959 }
4960
Dan Gohman7f321562007-06-25 16:23:39 +00004961 if (V->getOpcode() == ISD::BUILD_VECTOR) {
4962 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00004963 if (NumElems > BaseIdx) {
4964 SDOperand Base;
4965 bool AllSame = true;
4966 for (unsigned i = 0; i != NumElems; ++i) {
4967 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4968 Base = V->getOperand(i);
4969 break;
4970 }
4971 }
4972 // Splat of <u, u, u, u>, return <u, u, u, u>
4973 if (!Base.Val)
4974 return N0;
4975 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00004976 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00004977 AllSame = false;
4978 break;
4979 }
4980 }
4981 // Splat of <x, x, x, x>, return <x, x, x, x>
4982 if (AllSame)
4983 return N0;
4984 }
4985 }
4986 }
4987
Evan Chenge7bec0d2006-07-20 22:44:41 +00004988 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4989 // into an undef.
4990 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004991 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4992 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004993 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004994 for (unsigned i = 0; i != NumElts; ++i) {
4995 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4996 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4997 MappedOps.push_back(ShufMask.getOperand(i));
4998 } else {
4999 unsigned NewIdx =
5000 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
Duncan Sandsd038e042008-07-21 10:20:31 +00005001 MappedOps.push_back(DAG.getConstant(NewIdx,
5002 ShufMask.getOperand(i).getValueType()));
Chris Lattner17614ea2006-04-08 05:34:25 +00005003 }
5004 }
Dan Gohman7f321562007-06-25 16:23:39 +00005005 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005006 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00005007 AddToWorkList(ShufMask.Val);
Dan Gohman7f321562007-06-25 16:23:39 +00005008 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
5009 N0,
5010 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
5011 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00005012 }
Dan Gohman7f321562007-06-25 16:23:39 +00005013
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005014 return SDOperand();
5015}
5016
Evan Cheng44f1f092006-04-20 08:56:16 +00005017/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00005018/// an AND to a vector_shuffle with the destination vector and a zero vector.
5019/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00005020/// vector_shuffle V, Zero, <0, 4, 2, 4>
5021SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
5022 SDOperand LHS = N->getOperand(0);
5023 SDOperand RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00005024 if (N->getOpcode() == ISD::AND) {
5025 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00005026 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005027 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Evan Cheng44f1f092006-04-20 08:56:16 +00005028 std::vector<SDOperand> IdxOps;
5029 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00005030 unsigned NumElts = NumOps;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005031 MVT EVT = RHS.getValueType().getVectorElementType();
Evan Cheng44f1f092006-04-20 08:56:16 +00005032 for (unsigned i = 0; i != NumElts; ++i) {
5033 SDOperand Elt = RHS.getOperand(i);
5034 if (!isa<ConstantSDNode>(Elt))
5035 return SDOperand();
5036 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Duncan Sands77926da2008-07-18 20:12:05 +00005037 IdxOps.push_back(DAG.getConstant(i, EVT));
Evan Cheng44f1f092006-04-20 08:56:16 +00005038 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Duncan Sands77926da2008-07-18 20:12:05 +00005039 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
Evan Cheng44f1f092006-04-20 08:56:16 +00005040 else
5041 return SDOperand();
5042 }
5043
5044 // Let's see if the target supports this vector_shuffle.
5045 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
5046 return SDOperand();
5047
Dan Gohman7f321562007-06-25 16:23:39 +00005048 // Return the new VECTOR_SHUFFLE node.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005049 MVT VT = MVT::getVectorVT(EVT, NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00005050 std::vector<SDOperand> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005051 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00005052 Ops.push_back(LHS);
5053 AddToWorkList(LHS.Val);
5054 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00005055 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005056 &ZeroOps[0], ZeroOps.size()));
Duncan Sands77926da2008-07-18 20:12:05 +00005057 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005058 &IdxOps[0], IdxOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00005059 SDOperand Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005060 &Ops[0], Ops.size());
Dan Gohman7a9a5af2008-07-16 16:13:58 +00005061 if (VT != N->getValueType(0))
5062 Result = DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00005063 return Result;
5064 }
5065 }
5066 return SDOperand();
5067}
5068
Dan Gohman7f321562007-06-25 16:23:39 +00005069/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
5070SDOperand DAGCombiner::SimplifyVBinOp(SDNode *N) {
5071 // After legalize, the target may be depending on adds and other
5072 // binary ops to provide legal ways to construct constants or other
5073 // things. Simplifying them may result in a loss of legality.
5074 if (AfterLegalize) return SDOperand();
5075
Duncan Sands83ec4b62008-06-06 12:08:01 +00005076 MVT VT = N->getValueType(0);
5077 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00005078
Duncan Sands83ec4b62008-06-06 12:08:01 +00005079 MVT EltType = VT.getVectorElementType();
Chris Lattneredab1b92006-04-02 03:25:57 +00005080 SDOperand LHS = N->getOperand(0);
5081 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00005082 SDOperand Shuffle = XformToShuffleWithZero(N);
5083 if (Shuffle.Val) return Shuffle;
5084
Dan Gohman7f321562007-06-25 16:23:39 +00005085 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00005086 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00005087 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
5088 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005089 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005090 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Chris Lattneredab1b92006-04-02 03:25:57 +00005091 SDOperand LHSOp = LHS.getOperand(i);
5092 SDOperand RHSOp = RHS.getOperand(i);
5093 // If these two elements can't be folded, bail out.
5094 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5095 LHSOp.getOpcode() != ISD::Constant &&
5096 LHSOp.getOpcode() != ISD::ConstantFP) ||
5097 (RHSOp.getOpcode() != ISD::UNDEF &&
5098 RHSOp.getOpcode() != ISD::Constant &&
5099 RHSOp.getOpcode() != ISD::ConstantFP))
5100 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00005101 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00005102 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5103 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00005104 if ((RHSOp.getOpcode() == ISD::Constant &&
5105 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
5106 (RHSOp.getOpcode() == ISD::ConstantFP &&
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00005107 cast<ConstantFPSDNode>(RHSOp.Val)->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00005108 break;
5109 }
Dan Gohman7f321562007-06-25 16:23:39 +00005110 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00005111 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00005112 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5113 Ops.back().getOpcode() == ISD::Constant ||
5114 Ops.back().getOpcode() == ISD::ConstantFP) &&
5115 "Scalar binop didn't fold!");
5116 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005117
Dan Gohman7f321562007-06-25 16:23:39 +00005118 if (Ops.size() == LHS.getNumOperands()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005119 MVT VT = LHS.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00005120 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005121 }
Chris Lattneredab1b92006-04-02 03:25:57 +00005122 }
5123
5124 return SDOperand();
5125}
5126
Nate Begeman44728a72005-09-19 22:34:01 +00005127SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00005128 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5129
5130 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
5131 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5132 // If we got a simplified select_cc node back from SimplifySelectCC, then
5133 // break it down into a new SETCC node, and a new SELECT node, and then return
5134 // the SELECT node, since we were called with a SELECT node.
5135 if (SCC.Val) {
5136 // Check to see if we got a select_cc back (to turn into setcc/select).
5137 // Otherwise, just return whatever node we got back, like fabs.
5138 if (SCC.getOpcode() == ISD::SELECT_CC) {
5139 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
5140 SCC.getOperand(0), SCC.getOperand(1),
5141 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00005142 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005143 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5144 SCC.getOperand(3), SETCC);
5145 }
5146 return SCC;
5147 }
Nate Begeman44728a72005-09-19 22:34:01 +00005148 return SDOperand();
5149}
5150
Chris Lattner40c62d52005-10-18 06:04:22 +00005151/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5152/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00005153/// select. Callers of this should assume that TheSelect is deleted if this
5154/// returns true. As such, they should return the appropriate thing (e.g. the
5155/// node) back to the top-level of the DAG combiner loop to avoid it being
5156/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00005157///
5158bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
5159 SDOperand RHS) {
5160
5161 // If this is a select from two identical things, try to pull the operation
5162 // through the select.
5163 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00005164 // If this is a load and the token chain is identical, replace the select
5165 // of two loads with a load through a select of the address to load from.
5166 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5167 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005168 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005169 // Do not let this transformation reduce the number of volatile loads.
5170 !cast<LoadSDNode>(LHS)->isVolatile() &&
5171 !cast<LoadSDNode>(RHS)->isVolatile() &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005172 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005173 LHS.getOperand(0) == RHS.getOperand(0)) {
5174 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5175 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5176
5177 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005178 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005179 // FIXME: this conflates two src values, discarding one. This is not
5180 // the right thing to do, but nothing uses srcvalues now. When they do,
5181 // turn SrcValue into a list of locations.
5182 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005183 if (TheSelect->getOpcode() == ISD::SELECT) {
5184 // Check that the condition doesn't reach either load. If so, folding
5185 // this will induce a cycle into the DAG.
Evan Cheng917be682008-03-04 00:41:45 +00005186 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5187 !RLD->isPredecessorOf(TheSelect->getOperand(0).Val)) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005188 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5189 TheSelect->getOperand(0), LLD->getBasePtr(),
5190 RLD->getBasePtr());
5191 }
5192 } else {
5193 // Check that the condition doesn't reach either load. If so, folding
5194 // this will induce a cycle into the DAG.
Evan Cheng917be682008-03-04 00:41:45 +00005195 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5196 !RLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5197 !LLD->isPredecessorOf(TheSelect->getOperand(1).Val) &&
5198 !RLD->isPredecessorOf(TheSelect->getOperand(1).Val)) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005199 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005200 TheSelect->getOperand(0),
5201 TheSelect->getOperand(1),
5202 LLD->getBasePtr(), RLD->getBasePtr(),
5203 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005204 }
Evan Cheng466685d2006-10-09 20:57:25 +00005205 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005206
5207 if (Addr.Val) {
5208 SDOperand Load;
5209 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5210 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5211 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005212 LLD->getSrcValueOffset(),
5213 LLD->isVolatile(),
5214 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005215 else {
5216 Load = DAG.getExtLoad(LLD->getExtensionType(),
5217 TheSelect->getValueType(0),
5218 LLD->getChain(), Addr, LLD->getSrcValue(),
5219 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005220 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005221 LLD->isVolatile(),
5222 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005223 }
5224 // Users of the select now use the result of the load.
5225 CombineTo(TheSelect, Load);
5226
5227 // Users of the old loads now use the new load's chain. We know the
5228 // old-load value is dead now.
5229 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
5230 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
5231 return true;
5232 }
Evan Chengc5484282006-10-04 00:56:09 +00005233 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005234 }
5235 }
5236
5237 return false;
5238}
5239
Nate Begeman44728a72005-09-19 22:34:01 +00005240SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
5241 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00005242 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005243
Duncan Sands83ec4b62008-06-06 12:08:01 +00005244 MVT VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00005245 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
5246 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
5247 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
5248
5249 // Determine if the condition we're dealing with is constant
Scott Michel5b8f82e2008-03-10 15:42:14 +00005250 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00005251 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005252 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
5253
5254 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00005255 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005256 return N2;
5257 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00005258 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005259 return N3;
5260
5261 // Check to see if we can simplify the select into an fabs node
5262 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5263 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005264 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005265 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5266 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5267 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5268 N2 == N3.getOperand(0))
5269 return DAG.getNode(ISD::FABS, VT, N0);
5270
5271 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5272 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5273 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5274 N2.getOperand(0) == N3)
5275 return DAG.getNode(ISD::FABS, VT, N3);
5276 }
5277 }
5278
5279 // Check to see if we can perform the "gzip trick", transforming
5280 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005281 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005282 N0.getValueType().isInteger() &&
5283 N2.getValueType().isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005284 (N1C->isNullValue() || // (a < 0) ? b : 0
5285 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Duncan Sands83ec4b62008-06-06 12:08:01 +00005286 MVT XType = N0.getValueType();
5287 MVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00005288 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005289 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005290 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00005291 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5292 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005293 ShCtV = XType.getSizeInBits()-ShCtV-1;
Nate Begemanf845b452005-10-08 00:29:44 +00005294 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5295 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00005296 AddToWorkList(Shift.Val);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005297 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005298 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005299 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005300 }
5301 return DAG.getNode(ISD::AND, AType, Shift, N2);
5302 }
5303 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005304 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005305 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00005306 AddToWorkList(Shift.Val);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005307 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005308 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005309 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005310 }
5311 return DAG.getNode(ISD::AND, AType, Shift, N2);
5312 }
5313 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005314
5315 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00005316 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Nate Begeman07ed4172005-10-10 21:26:48 +00005317 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005318
5319 // If the caller doesn't want us to simplify this into a zext of a compare,
5320 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00005321 if (NotExtCompare && N2C->getAPIntValue() == 1)
Chris Lattner1eba01e2007-04-11 06:50:51 +00005322 return SDOperand();
5323
Nate Begeman07ed4172005-10-10 21:26:48 +00005324 // Get a SetCC of the condition
5325 // FIXME: Should probably make sure that setcc is legal if we ever have a
5326 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00005327 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005328 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00005329 if (AfterLegalize) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005330 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005331 if (N2.getValueType().bitsLT(SCC.getValueType()))
Chris Lattner555d8d62006-12-07 22:36:47 +00005332 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5333 else
5334 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005335 } else {
5336 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005337 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005338 }
Chris Lattner5750df92006-03-01 04:03:14 +00005339 AddToWorkList(SCC.Val);
5340 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005341
Dan Gohman002e5d02008-03-13 22:13:53 +00005342 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005343 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005344 // shl setcc result by log2 n2c
5345 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00005346 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Nate Begeman07ed4172005-10-10 21:26:48 +00005347 TLI.getShiftAmountTy()));
5348 }
5349
Nate Begemanf845b452005-10-08 00:29:44 +00005350 // Check to see if this is the equivalent of setcc
5351 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5352 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00005353 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005354 MVT XType = N0.getValueType();
Duncan Sands184a8762008-06-14 17:48:34 +00005355 if (!AfterLegalize ||
5356 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(N0))) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005357 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00005358 if (Res.getValueType() != VT)
5359 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5360 return Res;
5361 }
5362
5363 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5364 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands184a8762008-06-14 17:48:34 +00005365 (!AfterLegalize ||
5366 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Nate Begemanf845b452005-10-08 00:29:44 +00005367 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
5368 return DAG.getNode(ISD::SRL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005369 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Nate Begemanf845b452005-10-08 00:29:44 +00005370 TLI.getShiftAmountTy()));
5371 }
5372 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5373 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
5374 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
5375 N0);
5376 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
5377 DAG.getConstant(~0ULL, XType));
5378 return DAG.getNode(ISD::SRL, XType,
5379 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00005380 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005381 TLI.getShiftAmountTy()));
5382 }
5383 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5384 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
5385 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005386 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005387 TLI.getShiftAmountTy()));
5388 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5389 }
5390 }
5391
5392 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5393 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5394 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005395 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005396 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
5397 MVT XType = N0.getValueType();
Chris Lattner1982ef22007-04-11 05:11:38 +00005398 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005399 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005400 TLI.getShiftAmountTy()));
5401 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
5402 AddToWorkList(Shift.Val);
5403 AddToWorkList(Add.Val);
5404 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5405 }
5406 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5407 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5408 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5409 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5410 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005411 MVT XType = N0.getValueType();
5412 if (SubC->isNullValue() && XType.isInteger()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005413 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005414 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005415 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00005416 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005417 AddToWorkList(Shift.Val);
5418 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005419 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5420 }
5421 }
5422 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005423
Nate Begeman44728a72005-09-19 22:34:01 +00005424 return SDOperand();
5425}
5426
Evan Chengfa1eb272007-02-08 22:13:59 +00005427/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005428SDOperand DAGCombiner::SimplifySetCC(MVT VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00005429 SDOperand N1, ISD::CondCode Cond,
5430 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005431 TargetLowering::DAGCombinerInfo
5432 DagCombineInfo(DAG, !AfterLegalize, false, this);
5433 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005434}
5435
Nate Begeman69575232005-10-20 02:15:44 +00005436/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5437/// return a DAG expression to select that will generate the same value by
5438/// multiplying by a magic number. See:
5439/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5440SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005441 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005442 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
5443
Andrew Lenharth232c9102006-06-12 16:07:18 +00005444 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005445 ii != ee; ++ii)
5446 AddToWorkList(*ii);
5447 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005448}
5449
5450/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5451/// return a DAG expression to select that will generate the same value by
5452/// multiplying by a magic number. See:
5453/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5454SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005455 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005456 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005457
Andrew Lenharth232c9102006-06-12 16:07:18 +00005458 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005459 ii != ee; ++ii)
5460 AddToWorkList(*ii);
5461 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005462}
5463
Jim Laskey71382342006-10-07 23:37:56 +00005464/// FindBaseOffset - Return true if base is known not to alias with anything
5465/// but itself. Provides base object and offset as results.
5466static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
5467 // Assume it is a primitive operation.
5468 Base = Ptr; Offset = 0;
5469
5470 // If it's an adding a simple constant then integrate the offset.
5471 if (Base.getOpcode() == ISD::ADD) {
5472 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5473 Base = Base.getOperand(0);
5474 Offset += C->getValue();
5475 }
5476 }
5477
5478 // If it's any of the following then it can't alias with anything but itself.
5479 return isa<FrameIndexSDNode>(Base) ||
5480 isa<ConstantPoolSDNode>(Base) ||
5481 isa<GlobalAddressSDNode>(Base);
5482}
5483
5484/// isAlias - Return true if there is any possibility that the two addresses
5485/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00005486bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
5487 const Value *SrcValue1, int SrcValueOffset1,
5488 SDOperand Ptr2, int64_t Size2,
5489 const Value *SrcValue2, int SrcValueOffset2)
5490{
Jim Laskey71382342006-10-07 23:37:56 +00005491 // If they are the same then they must be aliases.
5492 if (Ptr1 == Ptr2) return true;
5493
5494 // Gather base node and offset information.
5495 SDOperand Base1, Base2;
5496 int64_t Offset1, Offset2;
5497 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5498 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5499
5500 // If they have a same base address then...
5501 if (Base1 == Base2) {
5502 // Check to see if the addresses overlap.
5503 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5504 }
5505
Jim Laskey096c22e2006-10-18 12:29:57 +00005506 // If we know both bases then they can't alias.
5507 if (KnownBase1 && KnownBase2) return false;
5508
Jim Laskey07a27092006-10-18 19:08:31 +00005509 if (CombinerGlobalAA) {
5510 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005511 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5512 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5513 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005514 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005515 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005516 if (AAResult == AliasAnalysis::NoAlias)
5517 return false;
5518 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005519
5520 // Otherwise we have to assume they alias.
5521 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005522}
5523
5524/// FindAliasInfo - Extracts the relevant alias information from the memory
5525/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005526bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00005527 SDOperand &Ptr, int64_t &Size,
5528 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005529 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5530 Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005531 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005532 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005533 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005534 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005535 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005536 Ptr = ST->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005537 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005538 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005539 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005540 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005541 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005542 }
5543
5544 return false;
5545}
5546
Jim Laskey6ff23e52006-10-04 16:53:27 +00005547/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5548/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00005549void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005550 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005551 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005552 std::set<SDNode *> Visited; // Visited node set.
5553
Jim Laskey279f0532006-09-25 16:29:54 +00005554 // Get alias information for node.
5555 SDOperand Ptr;
5556 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005557 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005558 int SrcValueOffset;
5559 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005560
Jim Laskey6ff23e52006-10-04 16:53:27 +00005561 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005562 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005563
Jim Laskeybc588b82006-10-05 15:07:25 +00005564 // Look at each chain and determine if it is an alias. If so, add it to the
5565 // aliases list. If not, then continue up the chain looking for the next
5566 // candidate.
5567 while (!Chains.empty()) {
5568 SDOperand Chain = Chains.back();
5569 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005570
Jim Laskeybc588b82006-10-05 15:07:25 +00005571 // Don't bother if we've been before.
5572 if (Visited.find(Chain.Val) != Visited.end()) continue;
5573 Visited.insert(Chain.Val);
5574
5575 switch (Chain.getOpcode()) {
5576 case ISD::EntryToken:
5577 // Entry token is ideal chain operand, but handled in FindBetterChain.
5578 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005579
Jim Laskeybc588b82006-10-05 15:07:25 +00005580 case ISD::LOAD:
5581 case ISD::STORE: {
5582 // Get alias information for Chain.
5583 SDOperand OpPtr;
5584 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005585 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005586 int OpSrcValueOffset;
5587 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
5588 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005589
5590 // If chain is alias then stop here.
5591 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005592 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5593 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005594 Aliases.push_back(Chain);
5595 } else {
5596 // Look further up the chain.
5597 Chains.push_back(Chain.getOperand(0));
5598 // Clean up old chain.
5599 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00005600 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005601 break;
5602 }
5603
5604 case ISD::TokenFactor:
5605 // We have to check each of the operands of the token factor, so we queue
5606 // then up. Adding the operands to the queue (stack) in reverse order
5607 // maintains the original order and increases the likelihood that getNode
5608 // will find a matching token factor (CSE.)
5609 for (unsigned n = Chain.getNumOperands(); n;)
5610 Chains.push_back(Chain.getOperand(--n));
5611 // Eliminate the token factor if we can.
5612 AddToWorkList(Chain.Val);
5613 break;
5614
5615 default:
5616 // For all other instructions we will just have to take what we can get.
5617 Aliases.push_back(Chain);
5618 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005619 }
5620 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005621}
5622
5623/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5624/// for a better chain (aliasing node.)
5625SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
5626 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005627
Jim Laskey6ff23e52006-10-04 16:53:27 +00005628 // Accumulate all the aliases to this node.
5629 GatherAllAliases(N, OldChain, Aliases);
5630
5631 if (Aliases.size() == 0) {
5632 // If no operands then chain to entry token.
5633 return DAG.getEntryNode();
5634 } else if (Aliases.size() == 1) {
5635 // If a single operand then chain to it. We don't need to revisit it.
5636 return Aliases[0];
5637 }
5638
5639 // Construct a custom tailored token factor.
5640 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5641 &Aliases[0], Aliases.size());
5642
5643 // Make sure the old chain gets cleaned up.
5644 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
5645
5646 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005647}
5648
Nate Begeman1d4d4142005-09-01 00:19:25 +00005649// SelectionDAG::Combine - This is the entry point for the file.
5650//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005651void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00005652 /// run - This is the main entry point to this class.
5653 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005654 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005655}